summaryrefslogtreecommitdiff
path: root/src/fonts.zig
diff options
context:
space:
mode:
Diffstat (limited to 'src/fonts.zig')
-rw-r--r--src/fonts.zig28
1 files changed, 28 insertions, 0 deletions
diff --git a/src/fonts.zig b/src/fonts.zig
index 88cde63a..d50227cd 100644
--- a/src/fonts.zig
+++ b/src/fonts.zig
@@ -81,6 +81,34 @@ pub fn list(arena: std.mem.Allocator, want_name: ?[]const u8) []const Font {
return scan(arena, null, true);
}
+/// A remote config's absolute font path belongs to its machine. Resolve the
+/// same face by name on this machine, retaining the filename as a fallback.
+pub fn remote(arena: std.mem.Allocator, path: []const u8, name: []const u8) ?Font {
+ if (name.len > 0) {
+ const matches = list(arena, name);
+ if (matches.len > 0) return matches[0];
+ }
+ const base = std.fs.path.basename(path);
+ const stem = base[0 .. base.len - std.fs.path.extension(base).len];
+ if (stem.len == 0 or std.mem.eql(u8, stem, name)) return null;
+ const matches = list(arena, stem);
+ return if (matches.len > 0) matches[0] else null;
+}
+
+test "a remote font config resolves the installed face despite another machine's path" {
+ var arena: std.heap.ArenaAllocator = .init(std.testing.allocator);
+ defer arena.deinit();
+ const alloc = arena.allocator();
+ const installed = list(alloc, null);
+ if (installed.len == 0) return error.SkipZigTest;
+ const face = installed[0];
+ const foreign = try std.fmt.allocPrint(alloc, "/other-machine/fonts/{s}", .{std.fs.path.basename(face.path)});
+ const by_name = remote(alloc, foreign, face.name) orelse return error.MissingLocalFace;
+ try std.testing.expectEqualStrings(face.path, by_name.path);
+ const by_file = remote(alloc, foreign, "") orelse return error.MissingLocalFace;
+ try std.testing.expectEqualStrings(face.path, by_file.path);
+}
+
/// Installed members of `fallback_names`, returned in preference order. This
/// is runtime discovery only: no local font path or exploration result enters
/// the build, and a machine with none simply uses embedded Adwaita Mono.