From f67c2b70a776e11481105d99a7096933bd4066ee Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Tue, 9 Apr 2024 08:45:01 -0400 Subject: [PATCH] font: SharedGridSet must hash diff for font size change Fixes #1670 This fixes two issues: - If no font families are set, font points would not change the hash. - DPI changes would not change the hash. When the hash doesn't change when it should, we reuse a font stack due to changes in #1662. This would cause some font issues. :) --- src/font/SharedGridSet.zig | 47 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 47 insertions(+) diff --git a/src/font/SharedGridSet.zig b/src/font/SharedGridSet.zig index bcb2d1095..561843bbd 100644 --- a/src/font/SharedGridSet.zig +++ b/src/font/SharedGridSet.zig @@ -428,6 +428,11 @@ pub const Key = struct { /// The metric modifier set configuration. metric_modifiers: Metrics.ModifierSet = .{}, + /// The configured font size for this key. We don't use this + /// directly but it is used as part of the hash for the + /// font grid. + font_size: DesiredSize = .{ .points = 12 }, + const style_offsets_len = std.enums.directEnumArrayLen(Style, 0); const StyleOffsets = [style_offsets_len]usize; @@ -537,6 +542,7 @@ pub const Key = struct { }, .codepoint_map = codepoint_map, .metric_modifiers = metric_modifiers, + .font_size = font_size, }; } @@ -559,6 +565,7 @@ pub const Key = struct { /// Hash the key with the given hasher. pub fn hash(self: Key, hasher: anytype) void { const autoHash = std.hash.autoHash; + autoHash(hasher, self.font_size); autoHash(hasher, self.descriptors.len); for (self.descriptors) |d| d.hash(hasher); self.codepoint_map.hash(hasher); @@ -600,7 +607,47 @@ test "Key" { var k = try Key.init(alloc, &keycfg, .{ .points = 12 }); defer k.deinit(); + var k2 = try Key.init(alloc, &keycfg, .{ .points = 12 }); + defer k2.deinit(); + try testing.expect(k.hashcode() > 0); + try testing.expectEqual(k.hashcode(), k2.hashcode()); +} + +test "Key different font points" { + const testing = std.testing; + const alloc = testing.allocator; + var cfg = try Config.default(alloc); + defer cfg.deinit(); + + var keycfg = try DerivedConfig.init(alloc, &cfg); + defer keycfg.deinit(); + + var k = try Key.init(alloc, &keycfg, .{ .points = 12 }); + defer k.deinit(); + + var k2 = try Key.init(alloc, &keycfg, .{ .points = 16 }); + defer k2.deinit(); + + try testing.expect(k.hashcode() != k2.hashcode()); +} + +test "Key different font DPI" { + const testing = std.testing; + const alloc = testing.allocator; + var cfg = try Config.default(alloc); + defer cfg.deinit(); + + var keycfg = try DerivedConfig.init(alloc, &cfg); + defer keycfg.deinit(); + + var k = try Key.init(alloc, &keycfg, .{ .points = 12, .xdpi = 1 }); + defer k.deinit(); + + var k2 = try Key.init(alloc, &keycfg, .{ .points = 12, .xdpi = 2 }); + defer k2.deinit(); + + try testing.expect(k.hashcode() != k2.hashcode()); } test SharedGridSet {