diff --git a/src/font/Group.zig b/src/font/Group.zig index c08f7bd34..76c69d0f8 100644 --- a/src/font/Group.zig +++ b/src/font/Group.zig @@ -53,7 +53,7 @@ const DescriptorCache = std.HashMapUnmanaged( pub fn hash(ctx: @This(), k: KeyType) u64 { _ = ctx; - return k.hash(); + return k.hashcode(); } pub fn eql(ctx: @This(), a: KeyType, b: KeyType) bool { diff --git a/src/font/discovery.zig b/src/font/discovery.zig index 8874c3436..c926445fd 100644 --- a/src/font/discovery.zig +++ b/src/font/discovery.zig @@ -57,27 +57,31 @@ pub const Descriptor = struct { /// will be preferred, but not guaranteed. variations: []const Variation = &.{}, - /// Returns a hash code that can be used to uniquely identify this - /// action. - pub fn hash(self: Descriptor) u64 { + /// Hash the descriptor with the given hasher. + pub fn hash(self: Descriptor, hasher: anytype) void { const autoHash = std.hash.autoHash; - var hasher = std.hash.Wyhash.init(0); - autoHash(&hasher, self.family); - autoHash(&hasher, self.style); - autoHash(&hasher, self.codepoint); - autoHash(&hasher, self.size); - autoHash(&hasher, self.bold); - autoHash(&hasher, self.italic); - autoHash(&hasher, self.monospace); - autoHash(&hasher, self.variations.len); + autoHash(hasher, self.family); + autoHash(hasher, self.style); + autoHash(hasher, self.codepoint); + autoHash(hasher, self.size); + autoHash(hasher, self.bold); + autoHash(hasher, self.italic); + autoHash(hasher, self.monospace); + autoHash(hasher, self.variations.len); for (self.variations) |variation| { - autoHash(&hasher, variation.id); + autoHash(hasher, variation.id); // This is not correct, but we don't currently depend on the // hash value being different based on decimal values of variations. - autoHash(&hasher, @as(u64, @intFromFloat(variation.value))); + autoHash(hasher, @as(u64, @intFromFloat(variation.value))); } + } + /// Returns a hash code that can be used to uniquely identify this + /// action. + pub fn hashcode(self: Descriptor) u64 { + var hasher = std.hash.Wyhash.init(0); + self.hash(&hasher); return hasher.final(); } @@ -552,7 +556,7 @@ test "descriptor hash" { const testing = std.testing; var d: Descriptor = .{}; - try testing.expect(d.hash() != 0); + try testing.expect(d.hashcode() != 0); } test "descriptor hash familiy names" { @@ -560,7 +564,7 @@ test "descriptor hash familiy names" { var d1: Descriptor = .{ .family = "A" }; var d2: Descriptor = .{ .family = "B" }; - try testing.expect(d1.hash() != d2.hash()); + try testing.expect(d1.hashcode() != d2.hashcode()); } test "fontconfig" {