Merge pull request #1093 from mitchellh/coretext-font-family

font/coretext: use `CTFontCopyFamilyName`
This commit is contained in:
Mitchell Hashimoto
2023-12-14 14:00:19 -08:00
committed by GitHub
2 changed files with 27 additions and 3 deletions

View File

@ -120,6 +120,10 @@ pub const Font = opaque {
)));
}
pub fn copyFamilyName(self: *Font) *foundation.String {
return @ptrFromInt(@intFromPtr(c.CTFontCopyFamilyName(@ptrCast(self))));
}
pub fn copyDisplayName(self: *Font) *foundation.String {
return @ptrFromInt(@intFromPtr(c.CTFontCopyDisplayName(@ptrCast(self))));
}

View File

@ -160,13 +160,13 @@ pub const Face = struct {
/// but sometimes allocation isn't required and a static string is
/// returned.
pub fn name(self: *const Face, buf: []u8) Allocator.Error![]const u8 {
const display_name = self.font.copyDisplayName();
if (display_name.cstringPtr(.utf8)) |str| return str;
const family_name = self.font.copyFamilyName();
if (family_name.cstringPtr(.utf8)) |str| return str;
// "NULL if the internal storage of theString does not allow
// this to be returned efficiently." In this case, we need
// to allocate.
return display_name.cstring(buf, .utf8) orelse error.OutOfMemory;
return family_name.cstring(buf, .utf8) orelse error.OutOfMemory;
}
/// Resize the font in-place. If this succeeds, the caller is responsible
@ -549,6 +549,26 @@ test {
}
}
test "name" {
const testing = std.testing;
const name = try macos.foundation.String.createWithBytes("Menlo", .utf8, false);
defer name.release();
const desc = try macos.text.FontDescriptor.createWithNameAndSize(name, 12);
defer desc.release();
const ct_font = try macos.text.Font.createWithFontDescriptor(desc, 12);
defer ct_font.release();
var face = try Face.initFontCopy(ct_font, .{ .size = .{ .points = 12 } });
defer face.deinit();
try testing.expectEqual(font.Presentation.text, face.presentation);
var buf: [1024]u8 = undefined;
const font_name = try face.name(&buf);
try testing.expect(std.mem.eql(u8, font_name, "Menlo"));
}
test "emoji" {
const testing = std.testing;