c3b6edc4
Resolve configured terminal font family
a73x 2026-04-09 06:39
Commit message
build.zig
| Old | New | ||
|---|---|---|---|
| @@ -3,6 +3,11 @@ const std = @import("std"); | |||
| 3 | pub fn build(b: *std.Build) void { | 3 | pub fn build(b: *std.Build) void { |
| 4 | const target = b.standardTargetOptions(.{}); | 4 | const target = b.standardTargetOptions(.{}); |
| 5 | const optimize = b.standardOptimizeOption(.{}); | 5 | const optimize = b.standardOptimizeOption(.{}); |
| 6 | const config_mod = b.createModule(.{ | ||
| 7 | .root_source_file = b.path("src/config.zig"), | ||
| 8 | .target = target, | ||
| 9 | .optimize = optimize, | ||
| 10 | }); | ||
| 6 | 11 | ||
| 7 | // Lazy-fetch the ghostty dependency. On the first invocation this | 12 | // Lazy-fetch the ghostty dependency. On the first invocation this |
| 8 | // materializes the package; subsequent builds use the local cache. | 13 | // materializes the package; subsequent builds use the local cache. |
| @@ -130,6 +135,7 @@ pub fn build(b: *std.Build) void { | |||
| 130 | .optimize = optimize, | 135 | .optimize = optimize, |
| 131 | .link_libc = true, | 136 | .link_libc = true, |
| 132 | }); | 137 | }); |
| 138 | font_mod.addImport("config", config_mod); | ||
| 133 | font_mod.linkSystemLibrary("fontconfig", .{}); | 139 | font_mod.linkSystemLibrary("fontconfig", .{}); |
| 134 | font_mod.linkSystemLibrary("freetype2", .{}); | 140 | font_mod.linkSystemLibrary("freetype2", .{}); |
| 135 | exe_mod.addImport("font", font_mod); | 141 | exe_mod.addImport("font", font_mod); |
| @@ -141,6 +147,7 @@ pub fn build(b: *std.Build) void { | |||
| 141 | .optimize = optimize, | 147 | .optimize = optimize, |
| 142 | .link_libc = true, | 148 | .link_libc = true, |
| 143 | }); | 149 | }); |
| 150 | font_test_mod.addImport("config", config_mod); | ||
| 144 | font_test_mod.linkSystemLibrary("fontconfig", .{}); | 151 | font_test_mod.linkSystemLibrary("fontconfig", .{}); |
| 145 | font_test_mod.linkSystemLibrary("freetype2", .{}); | 152 | font_test_mod.linkSystemLibrary("freetype2", .{}); |
| 146 | const font_tests = b.addTest(.{ | 153 | const font_tests = b.addTest(.{ |
src/font.zig
| Old | New | ||
|---|---|---|---|
| @@ -1,4 +1,5 @@ | |||
| 1 | const std = @import("std"); | 1 | const std = @import("std"); |
| 2 | const config = @import("config"); | ||
| 2 | const c = @cImport({ | 3 | const c = @cImport({ |
| 3 | @cInclude("fontconfig/fontconfig.h"); | 4 | @cInclude("fontconfig/fontconfig.h"); |
| 4 | @cInclude("ft2build.h"); | 5 | @cInclude("ft2build.h"); |
| @@ -14,13 +15,13 @@ pub const FontLookup = struct { | |||
| 14 | } | 15 | } |
| 15 | }; | 16 | }; |
| 16 | 17 | ||
| 17 | pub fn lookupMonospace(alloc: std.mem.Allocator) !FontLookup { | 18 | pub fn lookupConfiguredFont(alloc: std.mem.Allocator) !FontLookup { |
| 18 | if (c.FcInit() == c.FcFalse) return error.FcInitFailed; | 19 | if (c.FcInit() == c.FcFalse) return error.FcInitFailed; |
| 19 | 20 | ||
| 20 | const pattern = c.FcPatternCreate() orelse return error.FcPatternCreate; | 21 | const pattern = c.FcPatternCreate() orelse return error.FcPatternCreate; |
| 21 | defer c.FcPatternDestroy(pattern); | 22 | defer c.FcPatternDestroy(pattern); |
| 22 | 23 | ||
| 23 | _ = c.FcPatternAddString(pattern, c.FC_FAMILY, @ptrCast("monospace")); | 24 | _ = c.FcPatternAddString(pattern, c.FC_FAMILY, @ptrCast(config.font_family)); |
| 24 | _ = c.FcPatternAddInteger(pattern, c.FC_WEIGHT, c.FC_WEIGHT_REGULAR); | 25 | _ = c.FcPatternAddInteger(pattern, c.FC_WEIGHT, c.FC_WEIGHT_REGULAR); |
| 25 | _ = c.FcPatternAddInteger(pattern, c.FC_SLANT, c.FC_SLANT_ROMAN); | 26 | _ = c.FcPatternAddInteger(pattern, c.FC_SLANT, c.FC_SLANT_ROMAN); |
| 26 | 27 | ||
| @@ -44,6 +45,10 @@ pub fn lookupMonospace(alloc: std.mem.Allocator) !FontLookup { | |||
| 44 | return .{ .path = dup, .index = index }; | 45 | return .{ .path = dup, .index = index }; |
| 45 | } | 46 | } |
| 46 | 47 | ||
| 48 | pub fn lookupMonospace(alloc: std.mem.Allocator) !FontLookup { | ||
| 49 | return lookupConfiguredFont(alloc); | ||
| 50 | } | ||
| 51 | |||
| 47 | pub const Glyph = struct { | 52 | pub const Glyph = struct { |
| 48 | codepoint: u21, | 53 | codepoint: u21, |
| 49 | width: u32, | 54 | width: u32, |
| @@ -247,8 +252,8 @@ pub const Atlas = struct { | |||
| 247 | } | 252 | } |
| 248 | }; | 253 | }; |
| 249 | 254 | ||
| 250 | test "lookupMonospace returns a valid font path" { | 255 | test "lookupConfiguredFont returns a valid configured font path" { |
| 251 | var lookup = try lookupMonospace(std.testing.allocator); | 256 | var lookup = try lookupConfiguredFont(std.testing.allocator); |
| 252 | defer lookup.deinit(std.testing.allocator); | 257 | defer lookup.deinit(std.testing.allocator); |
| 253 | 258 | ||
| 254 | // Just check the file exists | 259 | // Just check the file exists |
| @@ -257,7 +262,7 @@ test "lookupMonospace returns a valid font path" { | |||
| 257 | } | 262 | } |
| 258 | 263 | ||
| 259 | test "Face rasterizes glyph 'M'" { | 264 | test "Face rasterizes glyph 'M'" { |
| 260 | var lookup = try lookupMonospace(std.testing.allocator); | 265 | var lookup = try lookupConfiguredFont(std.testing.allocator); |
| 261 | defer lookup.deinit(std.testing.allocator); | 266 | defer lookup.deinit(std.testing.allocator); |
| 262 | 267 | ||
| 263 | var face = try Face.init(std.testing.allocator, lookup.path, lookup.index, 14); | 268 | var face = try Face.init(std.testing.allocator, lookup.path, lookup.index, 14); |
| @@ -271,7 +276,7 @@ test "Face rasterizes glyph 'M'" { | |||
| 271 | } | 276 | } |
| 272 | 277 | ||
| 273 | test "Atlas packs multiple glyphs and returns UVs" { | 278 | test "Atlas packs multiple glyphs and returns UVs" { |
| 274 | var lookup = try lookupMonospace(std.testing.allocator); | 279 | var lookup = try lookupConfiguredFont(std.testing.allocator); |
| 275 | defer lookup.deinit(std.testing.allocator); | 280 | defer lookup.deinit(std.testing.allocator); |
| 276 | 281 | ||
| 277 | var face = try Face.init(std.testing.allocator, lookup.path, lookup.index, 14); | 282 | var face = try Face.init(std.testing.allocator, lookup.path, lookup.index, 14); |