From 966166015f148fe1a781be5000844acea5cf07d3 Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Sun, 24 Sep 2023 09:02:35 -0700 Subject: [PATCH] font/core-text: discovery supports style search --- src/cli/list_fonts.zig | 4 ++++ src/font/discovery.zig | 16 ++++++++++++++++ 2 files changed, 20 insertions(+) diff --git a/src/cli/list_fonts.zig b/src/cli/list_fonts.zig index f34df3cf1..1f02eb2cf 100644 --- a/src/cli/list_fonts.zig +++ b/src/cli/list_fonts.zig @@ -14,6 +14,9 @@ pub const Config = struct { /// matching this family will be listed. family: ?[:0]const u8 = null, + /// The style name to search for. + style: ?[:0]const u8 = null, + /// Font styles to search for. If this is set, then only fonts that /// match the given styles will be listed. bold: bool = false, @@ -84,6 +87,7 @@ fn runArgs(alloc_gpa: Allocator, argsIter: anytype) !u8 { defer disco.deinit(); var disco_it = try disco.discover(.{ .family = config.family, + .style = config.style, .bold = config.bold, .italic = config.italic, }); diff --git a/src/font/discovery.zig b/src/font/discovery.zig index 19b7141d6..828117865 100644 --- a/src/font/discovery.zig +++ b/src/font/discovery.zig @@ -31,6 +31,12 @@ pub const Descriptor = struct { /// fontconfig pattern, such as "Fira Code-14:bold". family: ?[:0]const u8 = null, + /// Specific font style to search for. This will filter the style + /// string the font advertises. The "bold/italic" booleans later in this + /// struct filter by the style trait the font has, not the string, so + /// these can be used in conjunction or not. + style: ?[:0]const u8 = null, + /// A codepoint that this font must be able to render. codepoint: u32 = 0, @@ -99,6 +105,16 @@ pub const Descriptor = struct { ); } + // Style + if (self.style) |style_bytes| { + const style = try macos.foundation.String.createWithBytes(style_bytes, .utf8, false); + defer style.release(); + attrs.setValue( + macos.text.FontAttribute.style_name.key(), + style, + ); + } + // Codepoint support if (self.codepoint > 0) { const cs = try macos.foundation.CharacterSet.createWithCharactersInRange(.{