c3b6edc4
Resolve configured terminal font family
a73x 2026-04-09 06:39
diff --git a/build.zig b/build.zig index 9274716..ca8054e 100644 --- a/build.zig +++ b/build.zig @@ -3,6 +3,11 @@ const std = @import("std"); pub fn build(b: *std.Build) void { const target = b.standardTargetOptions(.{}); const optimize = b.standardOptimizeOption(.{}); const config_mod = b.createModule(.{ .root_source_file = b.path("src/config.zig"), .target = target, .optimize = optimize, }); // Lazy-fetch the ghostty dependency. On the first invocation this // materializes the package; subsequent builds use the local cache. @@ -130,6 +135,7 @@ pub fn build(b: *std.Build) void { .optimize = optimize, .link_libc = true, }); font_mod.addImport("config", config_mod); font_mod.linkSystemLibrary("fontconfig", .{}); font_mod.linkSystemLibrary("freetype2", .{}); exe_mod.addImport("font", font_mod); @@ -141,6 +147,7 @@ pub fn build(b: *std.Build) void { .optimize = optimize, .link_libc = true, }); font_test_mod.addImport("config", config_mod); font_test_mod.linkSystemLibrary("fontconfig", .{}); font_test_mod.linkSystemLibrary("freetype2", .{}); const font_tests = b.addTest(.{ diff --git a/src/font.zig b/src/font.zig index 6be6a1f..a31de21 100644 --- a/src/font.zig +++ b/src/font.zig @@ -1,4 +1,5 @@ const std = @import("std"); const config = @import("config"); const c = @cImport({ @cInclude("fontconfig/fontconfig.h"); @cInclude("ft2build.h"); @@ -14,13 +15,13 @@ pub const FontLookup = struct { } }; pub fn lookupMonospace(alloc: std.mem.Allocator) !FontLookup { pub fn lookupConfiguredFont(alloc: std.mem.Allocator) !FontLookup { if (c.FcInit() == c.FcFalse) return error.FcInitFailed; const pattern = c.FcPatternCreate() orelse return error.FcPatternCreate; defer c.FcPatternDestroy(pattern); _ = c.FcPatternAddString(pattern, c.FC_FAMILY, @ptrCast("monospace")); _ = c.FcPatternAddString(pattern, c.FC_FAMILY, @ptrCast(config.font_family)); _ = c.FcPatternAddInteger(pattern, c.FC_WEIGHT, c.FC_WEIGHT_REGULAR); _ = c.FcPatternAddInteger(pattern, c.FC_SLANT, c.FC_SLANT_ROMAN); @@ -44,6 +45,10 @@ pub fn lookupMonospace(alloc: std.mem.Allocator) !FontLookup { return .{ .path = dup, .index = index }; } pub fn lookupMonospace(alloc: std.mem.Allocator) !FontLookup { return lookupConfiguredFont(alloc); } pub const Glyph = struct { codepoint: u21, width: u32, @@ -247,8 +252,8 @@ pub const Atlas = struct { } }; test "lookupMonospace returns a valid font path" { var lookup = try lookupMonospace(std.testing.allocator); test "lookupConfiguredFont returns a valid configured font path" { var lookup = try lookupConfiguredFont(std.testing.allocator); defer lookup.deinit(std.testing.allocator); // Just check the file exists @@ -257,7 +262,7 @@ test "lookupMonospace returns a valid font path" { } test "Face rasterizes glyph 'M'" { var lookup = try lookupMonospace(std.testing.allocator); var lookup = try lookupConfiguredFont(std.testing.allocator); defer lookup.deinit(std.testing.allocator); var face = try Face.init(std.testing.allocator, lookup.path, lookup.index, 14); @@ -271,7 +276,7 @@ test "Face rasterizes glyph 'M'" { } test "Atlas packs multiple glyphs and returns UVs" { var lookup = try lookupMonospace(std.testing.allocator); var lookup = try lookupConfiguredFont(std.testing.allocator); defer lookup.deinit(std.testing.allocator); var face = try Face.init(std.testing.allocator, lookup.path, lookup.index, 14);