font: CodepointMap hashable, use for groupcacheset

This commit is contained in:
Mitchell Hashimoto
2024-04-01 12:38:07 -07:00
parent bfcd5f380a
commit 9d8da8fcc7
3 changed files with 38 additions and 3 deletions

View File

@ -354,8 +354,8 @@ pub fn init(
}; };
// If we have codepoint mappings, set those. // If we have codepoint mappings, set those.
if (config.@"font-codepoint-map".map.list.len > 0) { if (font_group_key.codepoint_map.list.len > 0) {
group.codepoint_map = config.@"font-codepoint-map".map; group.codepoint_map = font_group_key.codepoint_map;
} }
// Set our styles // Set our styles

View File

@ -53,6 +53,26 @@ pub fn get(self: *const CodepointMap, cp: u21) ?discovery.Descriptor {
return null; 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" { test "codepointmap" {
const testing = std.testing; const testing = std.testing;
const alloc = testing.allocator; const alloc = testing.allocator;

View File

@ -17,7 +17,9 @@ const std = @import("std");
const assert = std.debug.assert; const assert = std.debug.assert;
const Allocator = std.mem.Allocator; const Allocator = std.mem.Allocator;
const ArenaAllocator = std.heap.ArenaAllocator; 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 discovery = @import("discovery.zig");
const configpkg = @import("../config.zig"); const configpkg = @import("../config.zig");
const Config = configpkg.Config; const Config = configpkg.Config;
@ -38,6 +40,9 @@ pub const Key = struct {
/// offsets[@intFromEnum(.bold)]. /// offsets[@intFromEnum(.bold)].
style_offsets: StyleOffsets = .{0} ** style_offsets_len, style_offsets: StyleOffsets = .{0} ** style_offsets_len,
/// The codepoint map configuration.
codepoint_map: CodepointMap,
const style_offsets_len = std.enums.directEnumArrayLen(Style, 0); const style_offsets_len = std.enums.directEnumArrayLen(Style, 0);
const StyleOffsets = [style_offsets_len]usize; 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 .{ return .{
.arena = arena, .arena = arena,
.descriptors = try descriptors.toOwnedSlice(), .descriptors = try descriptors.toOwnedSlice(),
@ -118,6 +131,7 @@ pub const Key = struct {
config.@"font-family-italic".list.items.len, config.@"font-family-italic".list.items.len,
config.@"font-family-bold-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; const autoHash = std.hash.autoHash;
autoHash(hasher, self.descriptors.len); autoHash(hasher, self.descriptors.len);
for (self.descriptors) |d| d.hash(hasher); for (self.descriptors) |d| d.hash(hasher);
autoHash(hasher, self.codepoint_map);
} }
/// Returns a hash code that can be used to uniquely identify this /// Returns a hash code that can be used to uniquely identify this