font: CoreText discovery searches monospace only by default

This commit is contained in:
Mitchell Hashimoto
2023-09-24 08:22:50 -07:00
parent 8214471e2c
commit 2fb14eee09
2 changed files with 25 additions and 1 deletions

View File

@ -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 {

View File

@ -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) {