pkg/macos: fix crash when no fonts are found

This commit is contained in:
Mitchell Hashimoto
2022-10-01 21:20:15 -07:00
parent 8292e925fc
commit 0f34617eab

View File

@ -27,12 +27,18 @@ pub const FontCollection = opaque {
} }
pub fn createMatchingFontDescriptors(self: *FontCollection) *foundation.Array { pub fn createMatchingFontDescriptors(self: *FontCollection) *foundation.Array {
return @intToPtr( const result = c.CTFontCollectionCreateMatchingFontDescriptors(
*foundation.Array,
@ptrToInt(c.CTFontCollectionCreateMatchingFontDescriptors(
@ptrCast(c.CTFontCollectionRef, self), @ptrCast(c.CTFontCollectionRef, self),
)),
); );
if (result) |ptr| {
return @intToPtr(*foundation.Array, @ptrToInt(ptr));
}
// If we have no results, we create an empty array. This is not
// exactly matching the Mac API. We can fix this later if we want
// but I chose this to make it slightly more Zig-like at the cost
// of some memory in the rare case.
return foundation.Array.create(anyopaque, &[_]*const anyopaque{}) catch unreachable;
} }
}; };
@ -108,3 +114,29 @@ test "from descriptors" {
//try debugDumpList(list); //try debugDumpList(list);
} }
test "from descriptors no match" {
const testing = std.testing;
const name = try foundation.String.createWithBytes("ThisShouldNeverExist", .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);
}