From 9d8da8fcc72f3894dc0fd5949d795e1246e941ef Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Mon, 1 Apr 2024 12:38:07 -0700 Subject: [PATCH] font: CodepointMap hashable, use for groupcacheset --- src/Surface.zig | 4 ++-- src/font/CodepointMap.zig | 20 ++++++++++++++++++++ src/font/GroupCacheSet.zig | 17 ++++++++++++++++- 3 files changed, 38 insertions(+), 3 deletions(-) diff --git a/src/Surface.zig b/src/Surface.zig index dac8ba705..36d4c00a4 100644 --- a/src/Surface.zig +++ b/src/Surface.zig @@ -354,8 +354,8 @@ pub fn init( }; // If we have codepoint mappings, set those. - if (config.@"font-codepoint-map".map.list.len > 0) { - group.codepoint_map = config.@"font-codepoint-map".map; + if (font_group_key.codepoint_map.list.len > 0) { + group.codepoint_map = font_group_key.codepoint_map; } // Set our styles diff --git a/src/font/CodepointMap.zig b/src/font/CodepointMap.zig index 58a8a7c43..c5b5b1ffb 100644 --- a/src/font/CodepointMap.zig +++ b/src/font/CodepointMap.zig @@ -53,6 +53,26 @@ pub fn get(self: *const CodepointMap, cp: u21) ?discovery.Descriptor { return null; } +/// Hash with the given hasher. +pub fn hash(self: *const CodepointMap, hasher: anytype) void { + const autoHash = std.hash.autoHash; + autoHash(hasher, self.list.len); + const slice = self.list.slice(); + for (0..slice.len) |i| { + const entry = slice.get(i); + autoHash(hasher, entry.range); + entry.descriptor.hash(hasher); + } +} + +/// Returns a hash code that can be used to uniquely identify this +/// action. +pub fn hashcode(self: *const CodepointMap) u64 { + var hasher = std.hash.Wyhash.init(0); + self.hash(&hasher); + return hasher.final(); +} + test "codepointmap" { const testing = std.testing; const alloc = testing.allocator; diff --git a/src/font/GroupCacheSet.zig b/src/font/GroupCacheSet.zig index c4786b216..c1d344947 100644 --- a/src/font/GroupCacheSet.zig +++ b/src/font/GroupCacheSet.zig @@ -17,7 +17,9 @@ const std = @import("std"); const assert = std.debug.assert; const Allocator = std.mem.Allocator; const ArenaAllocator = std.heap.ArenaAllocator; -const Style = @import("main.zig").Style; +const fontpkg = @import("main.zig"); +const Style = fontpkg.Style; +const CodepointMap = fontpkg.CodepointMap; const discovery = @import("discovery.zig"); const configpkg = @import("../config.zig"); const Config = configpkg.Config; @@ -38,6 +40,9 @@ pub const Key = struct { /// offsets[@intFromEnum(.bold)]. style_offsets: StyleOffsets = .{0} ** style_offsets_len, + /// The codepoint map configuration. + codepoint_map: CodepointMap, + const style_offsets_len = std.enums.directEnumArrayLen(Style, 0); const StyleOffsets = [style_offsets_len]usize; @@ -109,6 +114,14 @@ pub const Key = struct { }); } + // Setup the codepoint map + const codepoint_map: CodepointMap = map: { + const map = config.@"font-codepoint-map"; + if (map.map.list.len == 0) break :map .{}; + const clone = try config.@"font-codepoint-map".clone(alloc); + break :map clone.map; + }; + return .{ .arena = arena, .descriptors = try descriptors.toOwnedSlice(), @@ -118,6 +131,7 @@ pub const Key = struct { config.@"font-family-italic".list.items.len, config.@"font-family-bold-italic".list.items.len, }, + .codepoint_map = codepoint_map, }; } @@ -142,6 +156,7 @@ pub const Key = struct { const autoHash = std.hash.autoHash; autoHash(hasher, self.descriptors.len); for (self.descriptors) |d| d.hash(hasher); + autoHash(hasher, self.codepoint_map); } /// Returns a hash code that can be used to uniquely identify this