summaryrefslogtreecommitdiff
path: root/src/font/Collection.zig
diff options
context:
space:
mode:
authorMitchell Hashimoto <mitchell.hashimoto@gmail.com>2024-04-02 11:13:36 -0700
committerMitchell Hashimoto <mitchell.hashimoto@gmail.com>2024-04-05 09:29:41 -0700
commit0d0688404e39a96d4bfde378c32def5b88d9b229 (patch)
tree6e3cf68d0b34d55408f103daad0c5254537d70f4 /src/font/Collection.zig
parent833d54e441f3423f7133609ed39d56d3b15a47e5 (diff)
font: Collection.getIndex
Diffstat (limited to 'src/font/Collection.zig')
-rw-r--r--src/font/Collection.zig56
1 files changed, 56 insertions, 0 deletions
diff --git a/src/font/Collection.zig b/src/font/Collection.zig
index 162ec849a..a7e87d898 100644
--- a/src/font/Collection.zig
+++ b/src/font/Collection.zig
@@ -114,6 +114,31 @@ pub fn getFace(self: *Collection, index: Index) !*Face {
};
}
+/// Return the index of the font in this collection that contains
+/// the given codepoint, style, and presentation. If no font is found,
+/// null is returned.
+///
+/// This does not trigger font loading; deferred fonts can be
+/// searched for codepoints.
+pub fn getIndex(
+ self: *const Collection,
+ cp: u32,
+ style: Style,
+ p_mode: PresentationMode,
+) ?Index {
+ for (self.faces.get(style).items, 0..) |elem, i| {
+ if (elem.hasCodepoint(cp, p_mode)) {
+ return .{
+ .style = style,
+ .idx = @intCast(i),
+ };
+ }
+ }
+
+ // Not found
+ return null;
+}
+
/// Packed array of all Style enum cases mapped to a growable list of faces.
///
/// We use this data structure because there aren't many styles and all
@@ -384,3 +409,34 @@ test getFace {
try testing.expectEqual(@intFromPtr(face1), @intFromPtr(face2));
}
}
+
+test getIndex {
+ const testing = std.testing;
+ const alloc = testing.allocator;
+ const testFont = @import("test.zig").fontRegular;
+
+ var lib = try Library.init();
+ defer lib.deinit();
+
+ var c = try init(alloc);
+ defer c.deinit(alloc);
+
+ _ = try c.add(alloc, .regular, .{ .loaded = try Face.init(
+ lib,
+ testFont,
+ .{ .size = .{ .points = 12, .xdpi = 96, .ydpi = 96 } },
+ ) });
+
+ // Should find all visible ASCII
+ var i: u32 = 32;
+ while (i < 127) : (i += 1) {
+ const idx = c.getIndex(i, .regular, .{ .any = {} });
+ try testing.expect(idx != null);
+ }
+
+ // Should not find emoji
+ {
+ const idx = c.getIndex('🥸', .regular, .{ .any = {} });
+ try testing.expect(idx == null);
+ }
+}