From 2fb14eee09fa4cd368fb05731acd6d661ba6f785 Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Sun, 24 Sep 2023 08:22:50 -0700 Subject: [PATCH] font: CoreText discovery searches monospace only by default --- src/font/DeferredFace.zig | 22 ++++++++++++++++++++++ src/font/discovery.zig | 4 +++- 2 files changed, 25 insertions(+), 1 deletion(-) diff --git a/src/font/DeferredFace.zig b/src/font/DeferredFace.zig index 63d303d2d..c7146128d 100644 --- a/src/font/DeferredFace.zig +++ b/src/font/DeferredFace.zig @@ -101,6 +101,28 @@ pub fn deinit(self: *DeferredFace) void { self.* = undefined; } +/// Returns the family name of the font. +pub fn familyName(self: DeferredFace, buf: []u8) ![]const u8 { + switch (options.backend) { + .freetype => {}, + + .fontconfig_freetype => if (self.fc) |fc| + return (try fc.pattern.get(.family, 0)).string, + + .coretext, .coretext_freetype => if (self.ct) |ct| { + const family_name = ct.font.copyAttribute(.family_name); + return family_name.cstringPtr(.utf8) orelse unsupported: { + break :unsupported family_name.cstring(buf, .utf8) orelse + return error.OutOfMemory; + }; + }, + + .web_canvas => if (self.wc) |wc| return wc.font_str, + } + + return ""; +} + /// Returns the name of this face. The memory is always owned by the /// face so it doesn't have to be freed. pub fn name(self: DeferredFace, buf: []u8) ![]const u8 { diff --git a/src/font/discovery.zig b/src/font/discovery.zig index d1dc6a601..7bd7900f5 100644 --- a/src/font/discovery.zig +++ b/src/font/discovery.zig @@ -40,9 +40,10 @@ pub const Descriptor = struct { size: u16 = 0, /// True if we want to search specifically for a font that supports - /// bold, italic, or both. + /// specific styles. bold: bool = false, italic: bool = false, + monospace: bool = true, /// Variation axes to apply to the font. This also impacts searching /// for fonts since fonts with the ability to set these variations @@ -131,6 +132,7 @@ pub const Descriptor = struct { const traits: macos.text.FontSymbolicTraits = .{ .bold = self.bold, .italic = self.italic, + .monospace = self.monospace, }; const traits_cval: u32 = @bitCast(traits); if (traits_cval > 0) {