From f9e11273174756a474ef08808dbd0948c0942cff Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Fri, 30 Sep 2022 14:48:06 -0700 Subject: [PATCH] pkg/macos: add CoreText --- build.zig | 7 +++++- pkg/macos/build.zig | 1 + pkg/macos/foundation.zig | 1 + pkg/macos/foundation/dictionary.zig | 37 +++++++++++++++++++++++++++++ pkg/macos/main.zig | 1 + pkg/macos/text.zig | 5 ++++ pkg/macos/text/font_collection.zig | 22 +++++++++++++++++ 7 files changed, 73 insertions(+), 1 deletion(-) create mode 100644 pkg/macos/foundation/dictionary.zig create mode 100644 pkg/macos/text.zig create mode 100644 pkg/macos/text/font_collection.zig diff --git a/build.zig b/build.zig index c99995eb8..f227ee657 100644 --- a/build.zig +++ b/build.zig @@ -184,7 +184,12 @@ fn addDeps( step.addPackage(glfw.pkg); step.addPackage(libuv.pkg); step.addPackage(utf8proc.pkg); - if (step.target.isDarwin()) step.addPackage(macos.pkg); + + // Mac Stuff + if (step.target.isDarwin()) { + step.addPackage(macos.pkg); + _ = try macos.link(b, step, .{}); + } // We always statically compile glad step.addIncludePath("vendor/glad/include/"); diff --git a/pkg/macos/build.zig b/pkg/macos/build.zig index 9f1eb1796..78df21ee8 100644 --- a/pkg/macos/build.zig +++ b/pkg/macos/build.zig @@ -20,5 +20,6 @@ pub fn link( _ = opt; const lib = b.addStaticLibrary("macos", null); step.linkFramework("CoreFoundation"); + step.linkFramework("CoreText"); return lib; } diff --git a/pkg/macos/foundation.zig b/pkg/macos/foundation.zig index f2abe233a..9f23a611b 100644 --- a/pkg/macos/foundation.zig +++ b/pkg/macos/foundation.zig @@ -1,3 +1,4 @@ +pub usingnamespace @import("foundation/dictionary.zig"); pub usingnamespace @import("foundation/string.zig"); pub usingnamespace @import("foundation/type.zig"); diff --git a/pkg/macos/foundation/dictionary.zig b/pkg/macos/foundation/dictionary.zig new file mode 100644 index 000000000..495accd50 --- /dev/null +++ b/pkg/macos/foundation/dictionary.zig @@ -0,0 +1,37 @@ +const std = @import("std"); +const Allocator = std.mem.Allocator; +const cftype = @import("type.zig"); + +pub const Dictionary = opaque { + pub fn create() Allocator.Error!*Dictionary { + return CFDictionaryCreate( + null, + null, + null, + 0, + &kCFTypeDictionaryKeyCallBacks, + &kCFTypeDictionaryValueCallBacks, + ) orelse Allocator.Error.OutOfMemory; + } + + pub fn release(self: *Dictionary) void { + cftype.CFRelease(self); + } + + pub extern "c" fn CFDictionaryCreate( + allocator: ?*anyopaque, + keys: ?[*]*const anyopaque, + values: ?[*]*const anyopaque, + num_values: usize, + key_callbacks: *const anyopaque, + value_callbacks: *const anyopaque, + ) ?*Dictionary; + + extern "c" var kCFTypeDictionaryKeyCallBacks: anyopaque; + extern "c" var kCFTypeDictionaryValueCallBacks: anyopaque; +}; + +test "dictionary" { + const dict = try Dictionary.create(); + defer dict.release(); +} diff --git a/pkg/macos/main.zig b/pkg/macos/main.zig index e651c0103..d938a633d 100644 --- a/pkg/macos/main.zig +++ b/pkg/macos/main.zig @@ -1,4 +1,5 @@ pub const foundation = @import("foundation.zig"); +pub const text = @import("text.zig"); test { @import("std").testing.refAllDecls(@This()); diff --git a/pkg/macos/text.zig b/pkg/macos/text.zig new file mode 100644 index 000000000..7455fde5e --- /dev/null +++ b/pkg/macos/text.zig @@ -0,0 +1,5 @@ +pub usingnamespace @import("text/font_collection.zig"); + +test { + @import("std").testing.refAllDecls(@This()); +} diff --git a/pkg/macos/text/font_collection.zig b/pkg/macos/text/font_collection.zig new file mode 100644 index 000000000..7de026f29 --- /dev/null +++ b/pkg/macos/text/font_collection.zig @@ -0,0 +1,22 @@ +const std = @import("std"); +const Allocator = std.mem.Allocator; +const foundation = @import("../foundation.zig"); + +pub const FontCollection = opaque { + pub fn createFromAvailableFonts() Allocator.Error!*FontCollection { + return CTFontCollectionCreateFromAvailableFonts(null) orelse Allocator.Error.OutOfMemory; + } + + pub fn release(self: *FontCollection) void { + foundation.CFRelease(self); + } + + pub extern "c" fn CTFontCollectionCreateFromAvailableFonts( + options: ?*foundation.Dictionary, + ) ?*FontCollection; +}; + +test "collection" { + const v = try FontCollection.createFromAvailableFonts(); + defer v.release(); +}