From c6dc2a3529ec5d3e5698ab56e572ded1c4e85b11 Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Sat, 1 Oct 2022 16:10:50 -0700 Subject: [PATCH] macos/text: create collection from descriptors --- pkg/macos/text/font_collection.zig | 104 ++++++++++++++++++++--------- 1 file changed, 71 insertions(+), 33 deletions(-) diff --git a/pkg/macos/text/font_collection.zig b/pkg/macos/text/font_collection.zig index b1e8411f3..686db2be2 100644 --- a/pkg/macos/text/font_collection.zig +++ b/pkg/macos/text/font_collection.zig @@ -12,6 +12,16 @@ pub const FontCollection = opaque { ) orelse Allocator.Error.OutOfMemory; } + pub fn createWithFontDescriptors(descs: *foundation.Array) Allocator.Error!*FontCollection { + return @intToPtr( + ?*FontCollection, + @ptrToInt(c.CTFontCollectionCreateWithFontDescriptors( + @ptrCast(c.CFArrayRef, descs), + null, + )), + ) orelse Allocator.Error.OutOfMemory; + } + pub fn release(self: *FontCollection) void { c.CFRelease(self); } @@ -26,6 +36,41 @@ pub const FontCollection = opaque { } }; +fn debugDumpList(list: *foundation.Array) !void { + var i: usize = 0; + while (i < list.getCount()) : (i += 1) { + const desc = list.getValueAtIndex(text.FontDescriptor, i); + { + var buf: [128]u8 = undefined; + const name = desc.copyAttribute(.name); + defer name.release(); + const cstr = name.cstring(&buf, .utf8).?; + + var buf2: [128]u8 = undefined; + const url = desc.copyAttribute(.url); + defer url.release(); + const path = path: { + const blank = try foundation.String.createWithBytes("", .utf8, false); + defer blank.release(); + + const path = url.copyPath() orelse break :path ""; + defer path.release(); + + const decoded = try foundation.URL.createStringByReplacingPercentEscapes( + path, + blank, + ); + defer decoded.release(); + + break :path decoded.cstring(&buf2, .utf8) orelse + ""; + }; + + std.log.warn("i={d} name={s} path={s}", .{ i, cstr, path }); + } + } +} + test "collection" { const testing = std.testing; @@ -36,37 +81,30 @@ test "collection" { defer list.release(); try testing.expect(list.getCount() > 0); - - // var i: usize = 0; - // while (i < list.getCount()) : (i += 1) { - // const desc = list.getValueAtIndex(text.FontDescriptor, i); - // { - // var buf: [128]u8 = undefined; - // const name = desc.copyAttribute(.name); - // defer name.release(); - // const cstr = name.cstring(&buf, .utf8).?; - // - // var buf2: [128]u8 = undefined; - // const url = desc.copyAttribute(.url); - // defer url.release(); - // const path = path: { - // const blank = try foundation.String.createWithBytes("", .utf8, false); - // defer blank.release(); - // - // const path = url.copyPath() orelse break :path ""; - // defer path.release(); - // - // const decoded = try foundation.URL.createStringByReplacingPercentEscapes( - // path, - // blank, - // ); - // defer decoded.release(); - // - // break :path decoded.cstring(&buf2, .utf8) orelse - // ""; - // }; - // - // std.log.warn("i={d} name={s} path={s}", .{ i, cstr, path }); - // } - // } +} + +test "from descriptors" { + const testing = std.testing; + + const name = try foundation.String.createWithBytes("AppleColorEmoji", .utf8, false); + defer name.release(); + + const desc = try text.FontDescriptor.createWithNameAndSize(name, 12); + defer desc.release(); + + const arr = try foundation.Array.create( + text.FontDescriptor, + &[_]*const text.FontDescriptor{desc}, + ); + defer arr.release(); + + const v = try FontCollection.createWithFontDescriptors(arr); + defer v.release(); + + const list = v.createMatchingFontDescriptors(); + defer list.release(); + + try testing.expect(list.getCount() > 0); + + //try debugDumpList(list); }