macos/text: create collection from descriptors

This commit is contained in:
Mitchell Hashimoto
2022-10-01 16:10:50 -07:00
parent 623e7f5916
commit c6dc2a3529

View File

@ -12,6 +12,16 @@ pub const FontCollection = opaque {
) orelse Allocator.Error.OutOfMemory; ) 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 { pub fn release(self: *FontCollection) void {
c.CFRelease(self); 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 "<no path>";
defer path.release();
const decoded = try foundation.URL.createStringByReplacingPercentEscapes(
path,
blank,
);
defer decoded.release();
break :path decoded.cstring(&buf2, .utf8) orelse
"<path cannot be converted to string>";
};
std.log.warn("i={d} name={s} path={s}", .{ i, cstr, path });
}
}
}
test "collection" { test "collection" {
const testing = std.testing; const testing = std.testing;
@ -36,37 +81,30 @@ test "collection" {
defer list.release(); defer list.release();
try testing.expect(list.getCount() > 0); try testing.expect(list.getCount() > 0);
}
// var i: usize = 0;
// while (i < list.getCount()) : (i += 1) { test "from descriptors" {
// const desc = list.getValueAtIndex(text.FontDescriptor, i); const testing = std.testing;
// {
// var buf: [128]u8 = undefined; const name = try foundation.String.createWithBytes("AppleColorEmoji", .utf8, false);
// const name = desc.copyAttribute(.name); defer name.release();
// defer name.release();
// const cstr = name.cstring(&buf, .utf8).?; const desc = try text.FontDescriptor.createWithNameAndSize(name, 12);
// defer desc.release();
// var buf2: [128]u8 = undefined;
// const url = desc.copyAttribute(.url); const arr = try foundation.Array.create(
// defer url.release(); text.FontDescriptor,
// const path = path: { &[_]*const text.FontDescriptor{desc},
// const blank = try foundation.String.createWithBytes("", .utf8, false); );
// defer blank.release(); defer arr.release();
//
// const path = url.copyPath() orelse break :path "<no path>"; const v = try FontCollection.createWithFontDescriptors(arr);
// defer path.release(); defer v.release();
//
// const decoded = try foundation.URL.createStringByReplacingPercentEscapes( const list = v.createMatchingFontDescriptors();
// path, defer list.release();
// blank,
// ); try testing.expect(list.getCount() > 0);
// defer decoded.release();
// //try debugDumpList(list);
// break :path decoded.cstring(&buf2, .utf8) orelse
// "<path cannot be converted to string>";
// };
//
// std.log.warn("i={d} name={s} path={s}", .{ i, cstr, path });
// }
// }
} }