From 22d631942c30dae1f3138ea08828325135c03d4f Mon Sep 17 00:00:00 2001 From: Kyaw Date: Fri, 15 Dec 2023 02:26:47 +0630 Subject: [PATCH] font/coretext: use `CTFontCopyFamilyName` Use `CTFontCopyFamilyName` instead of `CTFontCopyDisplayName` to get the font name to match the behavior of how it's done on freetype backend. --- pkg/macos/text/font.zig | 4 ++++ src/font/face/coretext.zig | 26 +++++++++++++++++++++++--- 2 files changed, 27 insertions(+), 3 deletions(-) diff --git a/pkg/macos/text/font.zig b/pkg/macos/text/font.zig index a4997503b..1141ab1fa 100644 --- a/pkg/macos/text/font.zig +++ b/pkg/macos/text/font.zig @@ -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)))); } diff --git a/src/font/face/coretext.zig b/src/font/face/coretext.zig index 66c7ca970..2f6bd6d16 100644 --- a/src/font/face/coretext.zig +++ b/src/font/face/coretext.zig @@ -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;