diff options
Diffstat (limited to 'src')
| -rw-r--r-- | src/fonts.zig | 89 | ||||
| -rw-r--r-- | src/gui/gui.zig | 2 |
2 files changed, 35 insertions, 56 deletions
diff --git a/src/fonts.zig b/src/fonts.zig index c4f96bad..a49ef9e4 100644 --- a/src/fonts.zig +++ b/src/fonts.zig @@ -1,19 +1,16 @@ -//! The fonts installed on the machine: the list the picker shows, the path a -//! `Font <name>` resolves to, and the one word the two sides of that say to -//! each other. builtins.zig reads this file to build the rows and to resolve a -//! name; gui.zig and macos.zig read it to learn which file to load. It is the -//! whole seam, because the core has no font and the shell has no builtin -//! dispatch. +//! The fonts installed on the machine: the list the picker shows (Output.zig), +//! the path a `Font <name>` resolves to (exec.zig), and the fallback faces a +//! window loads after its own (gui.zig, macos.zig). It is the whole seam, +//! because the core has no font and the shells have no builtin dispatch. //! -//! It lives at src/ rather than under gui/ because two shells now draw their -//! own text: builtins.zig imports it behind `platform == .gui or .macos`, so -//! the tui binary compiles not one line of this and never opens a font -//! directory, and the browser (which has no font directories to open) is out -//! for a better reason than taste. +//! It lives at src/ rather than under gui/ because two shells draw their own +//! text. It is compiled only behind `pardes.font_picker` (gui or macos), so +//! the tui binary never opens a font directory, and the browser has none to +//! open. //! //! No fontconfig and no CoreText. Enumerating fonts is a walk over a handful //! of well-known directories, and the one thing the picker must know about -//! each face — whether every glyph has the same advance — is four small sfnt +//! each face — whether every glyph has the same advance — is three small sfnt //! reads. That avoids initializing a FreeType face for every file in the //! directory, and it keeps the answer identical on both platforms: the shells //! disagree about how to RASTERIZE a file, never about which files there are. @@ -44,14 +41,11 @@ const home_dirs = switch (builtin.os.tag) { /// pointed at. A font tree is shallow and wide (one directory per family), so /// the depth cap is lower than find's. /// -/// `max_fonts` is a MEMORY bound and nothing else — the work is already bounded -/// by `max_steps`, since a face has to be walked past before it can be found. -/// It used to be 512, which is not a machine-sized number: a desktop with the -/// Noto and Nerd Font sets installed has nearly a thousand monospace faces, and -/// the picker silently served the first 512 the directories happened to hand -/// back. Sorting happens AFTER the cut, so the loss did not even look like -/// truncation — the list ran A to z with half the fonts missing out of the -/// middle of it, which is a far worse way to be wrong than a short list. +/// `max_fonts` is a MEMORY bound and nothing else (the work is bounded by +/// `max_steps`), and it is sized past any real machine: a desktop with the +/// Noto and Nerd Font sets has nearly a thousand monospace faces, and the list +/// is sorted AFTER the cut, so a cut list reads as complete with faces missing +/// out of its middle. const max_fonts = 4096; const max_steps = 20_000; const max_depth = 8; @@ -97,16 +91,13 @@ fn scan( wanted: ?[]const []const u8, require_monospaced: bool, ) []const Font { - // ...from the arena, not the stack: 4096 faces of {name, path} is 128 KiB, - // which is a fine thing to hand back to an arena that resets at the end of - // the keystroke and not a thing to put on a call stack. An allocator that - // cannot spare it answers with the embedded face, like every other failure - // in here. + // From the arena, not the stack: 4096 faces of {name, path} is 128 KiB. An + // allocator that cannot spare it answers with the embedded face, like + // every other failure in here. const found = arena.alloc(Font, max_fonts) catch return &.{}; var found_len: usize = 0; - // Zig 0.16 moved the filesystem behind std.Io; the blocking - // single-threaded implementation is the synchronous walk the core uses, - // the same one look.find uses. + // The blocking single-threaded Io: the walk is synchronous, inside the + // keystroke, as look.find's is. const io = std.Io.Threaded.global_single_threaded.io(); const home: []const u8 = if (libc.getenv("HOME")) |h| std.mem.span(h) else ""; var root_buf: [512]u8 = undefined; @@ -221,27 +212,21 @@ test "the picker lists every monospace face rather than the first max_fonts" { defer arena_state.deinit(); const installed = list(arena_state.allocator(), null); // A canary, and only meaningful on a machine that HAS fonts: reaching the - // cap means the list handed to the picker is a silent lie, sorted after - // the cut so it reads as complete while missing faces out of the middle. - // That shipped once at max_fonts = 512 on a desktop with ~1000 of them. + // cap is the silent cut max_fonts is sized against. try std.testing.expect(installed.len < max_fonts); - // ...and what it does return is sorted, which is what makes the picker the - // same list twice running. + // ...and what it does return is sorted. for (installed, 0..) |font, i| { try std.testing.expect(font.name.len != 0); if (i > 0) try std.testing.expect(!std.mem.lessThan(u8, font.name, installed[i - 1].name)); } } -/// Is every glyph in this font the same width? The terminal grid IS a -/// monospace cell — one advance for every column, chosen once from 'M' — so a -/// proportional font does not render badly in it, it renders as rubble: every -/// row a different length, every column misaligned, and the mouse pointing at -/// the wrong character. That is why the picker filters rather than listing all -/// nine hundred faces and letting you find out one step into walking them; the -/// list you get is the list you can actually wear. +/// Is every glyph in this font the same width? The grid is one advance for +/// every column, chosen once from 'M', so a proportional font in it has every +/// row a different length and the mouse pointing at the wrong character: the +/// picker lists only the faces that can be worn. /// -/// Four reads and no allocation, which is why the walk can afford it per file: +/// Three reads and no allocation, which is why the walk can afford it per file: /// the sfnt header and table directory, then `hhea`'s numberOfHMetrics, then /// the start of `hmtx` — one {advance, lsb} pair per glyph. A font whose first /// advances all agree is monospace. Zeros are skipped: .notdef and the @@ -314,13 +299,10 @@ fn sfntBase(head: []const u8) ?usize { return if (base + 12 <= head.len) base else null; } -/// A scratch font path only this process writes. -/// -/// Not a fixed name: `zig build unit-test` compiles this module into two test -/// binaries and runs them CONCURRENTLY, so a shared path in /tmp is one test -/// truncating the file another is halfway through reading. That surfaced as -/// `monospaced` flatly disagreeing with the bytes it had just been handed, -/// about one run in three, which reads like a bug in the probe and is not one. +/// A scratch font path only this process writes. Not a fixed name: `zig build +/// unit-test` compiles this module into two test binaries and runs them +/// CONCURRENTLY, so a shared path in /tmp is one test truncating the file +/// another is halfway through reading. fn scratchFont(buf: *[64:0]u8, ext: []const u8) [:0]const u8 { return std.fmt.bufPrintSentinel(buf, "/tmp/pardes-fonts-test-{d}{s}", .{ @as(u32, @intCast(libc.getpid())), ext, @@ -357,8 +339,7 @@ test "monospaced reads the advances out of a real sfnt layout" { try writeScratch(path, &f); try std.testing.expect(monospaced(path)); - // ...and one glyph a different width is the whole difference between a - // font this can wear and one it cannot + // ...and one glyph a different width makes it proportional std.mem.writeInt(u16, f[80 + 4 ..][0..2], 1200, .big); try writeScratch(path, &f); try std.testing.expect(!monospaced(path)); @@ -366,8 +347,7 @@ test "monospaced reads the advances out of a real sfnt layout" { test "a ttc collection is read through its first face" { // The same 92-byte font as above, moved 20 bytes down the file behind a - // `ttcf` header naming two faces. Table offsets stay absolute, which is - // the property that makes this work at all and the one a hand-rolled + // `ttcf` header naming two faces. Table offsets stay absolute, which an // "add the base" would silently break. const base = 20; var f: [base + 92]u8 = @splat(0); @@ -402,9 +382,8 @@ test "a ttc collection is read through its first face" { test "the walk finds this machine's monospace faces" { // Not a fixture: the directory list is the entire platform-specific part // of this file, and a wrong one produces an empty picker rather than an - // error. So this asserts against the machine — every OS pardes draws its - // own text on ships a monospace face, and finding NONE means the walk is - // looking somewhere that does not exist. + // error. Every OS pardes draws its own text on ships a monospace face, so + // finding NONE means the walk is looking somewhere that does not exist. var arena_state = std.heap.ArenaAllocator.init(std.testing.allocator); defer arena_state.deinit(); const found = list(arena_state.allocator(), null); diff --git a/src/gui/gui.zig b/src/gui/gui.zig index c7b52fc4..1bc8b959 100644 --- a/src/gui/gui.zig +++ b/src/gui/gui.zig @@ -16,7 +16,7 @@ const ShaderBuild = @import("../ShaderBuild.zig"); const deck = @import("deck.zig"); const Post = @import("Post.zig"); const pet = @import("pet.zig"); -const fonts = @import("../fonts.zig"); // the Font builtin's half of the seam +const fonts = @import("../fonts.zig"); // the fallback faces const selection_pipe = @import("../selection_pipe.zig"); const c_heap = @import("c_heap"); |
