diff --git a/src/font/CodepointMap.zig b/src/font/CodepointMap.zig index 82ec1462f..58a8a7c43 100644 --- a/src/font/CodepointMap.zig +++ b/src/font/CodepointMap.zig @@ -26,6 +26,10 @@ pub const Entry = struct { /// scenario where this will be a problem except people trying to fuck around. list: std.MultiArrayList(Entry) = .{}, +pub fn deinit(self: *CodepointMap, alloc: Allocator) void { + self.list.deinit(alloc); +} + /// Add an entry to the map. /// /// For conflicting codepoints, entries added later take priority over @@ -37,7 +41,9 @@ pub fn add(self: *CodepointMap, alloc: Allocator, entry: Entry) !void { /// Get a descriptor for a codepoint. pub fn get(self: *const CodepointMap, cp: u21) ?discovery.Descriptor { - for (self.list.items(.range), 0..) |range, i| { + const items = self.list.items(.range); + for (items, 0..) |range, forward_i| { + const i = items.len - forward_i - 1; if (range[0] <= cp and cp <= range[1]) { const descs = self.list.items(.descriptor); return descs[i]; @@ -46,3 +52,30 @@ pub fn get(self: *const CodepointMap, cp: u21) ?discovery.Descriptor { return null; } + +test "codepointmap" { + const testing = std.testing; + const alloc = testing.allocator; + + var m: CodepointMap = .{}; + defer m.deinit(alloc); + + // Exact range + try testing.expect(m.get(1) == null); + try m.add(alloc, .{ .range = .{ 1, 1 }, .descriptor = .{ .family = "A" } }); + { + const d = m.get(1).?; + try testing.expectEqualStrings("A", d.family.?); + } + + // Later entry takes priority + try m.add(alloc, .{ .range = .{ 1, 2 }, .descriptor = .{ .family = "B" } }); + { + const d = m.get(1).?; + try testing.expectEqualStrings("B", d.family.?); + } + + // Non-matching + try testing.expect(m.get(0) == null); + try testing.expect(m.get(3) == null); +} diff --git a/src/font/main.zig b/src/font/main.zig index 562c31e3a..d660e67de 100644 --- a/src/font/main.zig +++ b/src/font/main.zig @@ -5,6 +5,7 @@ const build_config = @import("../build_config.zig"); pub const Atlas = @import("Atlas.zig"); pub const discovery = @import("discovery.zig"); pub const face = @import("face.zig"); +pub const CodepointMap = @import("CodepointMap.zig"); pub const DeferredFace = @import("DeferredFace.zig"); pub const Face = face.Face; pub const Group = @import("Group.zig");