store requested size alongside deferred font for loading

This commit is contained in:
Mitchell Hashimoto
2022-09-17 10:05:26 -07:00
parent 92251ed913
commit bc9a0a36a8
2 changed files with 19 additions and 3 deletions

View File

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

View File

@ -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());