From bc9a0a36a8e21c02a8369b8e5c7cb733ac089acb Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Sat, 17 Sep 2022 10:05:26 -0700 Subject: [PATCH] store requested size alongside deferred font for loading --- src/font/DeferredFace.zig | 11 +++++++++++ src/font/discovery.zig | 11 ++++++++--- 2 files changed, 19 insertions(+), 3 deletions(-) diff --git a/src/font/DeferredFace.zig b/src/font/DeferredFace.zig index 43efbcb72..8a4eb9939 100644 --- a/src/font/DeferredFace.zig +++ b/src/font/DeferredFace.zig @@ -23,10 +23,21 @@ fc: if (options.fontconfig) ?Fontconfig else void = if (options.fontconfig) null /// Fontconfig specific data. This is only present if building with fontconfig. pub const Fontconfig = struct { + /// The pattern for this font. This must be the "render prepared" pattern. + /// (i.e. call FcFontRenderPrepare). pattern: *fontconfig.Pattern, + + /// Charset and Langset are used for quick lookup if a codepoint and + /// presentation style are supported. They can be derived from pattern + /// but are cached since they're frequently used. charset: *const fontconfig.CharSet, langset: *const fontconfig.LangSet, + /// The requested size in points for this font. This is used for loading. + /// This can't be derived from pattern because the requested size may + /// differ from the size the font advertises supported. + req_size: u16, + pub fn deinit(self: *Fontconfig) void { self.pattern.destroy(); self.* = undefined; diff --git a/src/font/discovery.zig b/src/font/discovery.zig index 06790e01f..dcdb818f6 100644 --- a/src/font/discovery.zig +++ b/src/font/discovery.zig @@ -22,8 +22,10 @@ pub const Descriptor = struct { /// fontconfig pattern, such as "Fira Code-14:bold". family: [:0]const u8, - /// Font size in points that the font should support. - size: u16 = 0, + /// Font size in points that the font should support. For conversion + /// to pixels, we will use 72 DPI for Mac and 96 DPI for everything else. + /// (If pixel conversion is necessary, i.e. emoji fonts) + size: u16, /// True if we want to search specifically for a font that supports /// bold, italic, or both. @@ -81,6 +83,7 @@ pub const Fontconfig = struct { .set = res.fs, .fonts = res.fs.fonts(), .i = 0, + .req_size = @floatToInt(u16, (try pat.get(.size, 0)).double), }; } @@ -90,6 +93,7 @@ pub const Fontconfig = struct { set: *fontconfig.FontSet, fonts: []*fontconfig.Pattern, i: usize, + req_size: u16, pub fn deinit(self: *DiscoverIterator) void { self.set.destroy(); @@ -117,6 +121,7 @@ pub const Fontconfig = struct { .pattern = font_pattern, .charset = (try font_pattern.get(.charset, 0)).char_set, .langset = (try font_pattern.get(.lang, 0)).lang_set, + .req_size = self.req_size, }, }; } @@ -127,7 +132,7 @@ test { const testing = std.testing; var fc = Fontconfig.init(); - var it = try fc.discover(.{ .family = "monospace" }); + var it = try fc.discover(.{ .family = "monospace", .size = 12 }); defer it.deinit(); while (try it.next()) |face| { try testing.expect(!face.loaded());