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 48fa7179..84ce5307 100644
--- a/src/fonts.zig
+++ b/src/fonts.zig
@@ -76,6 +76,20 @@ 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.Io.Dir.path.basename(path);
+ const stem = base[0 .. base.len - std.Io.Dir.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;
+}
+
/// 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.
@@ -406,3 +420,17 @@ test "the walk finds this machine's monospace faces" {
try std.testing.expect(std.mem.endsWith(u8, menlo.path, ".ttc"));
}
}
+
+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.Io.Dir.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);
+}