From c94455e1a8d2a430d8dcd74603bcd0d7b27aeb68 Mon Sep 17 00:00:00 2001 From: Gabriel Schneider Date: Tue, 17 Nov 2026 09:06:20 -0300 Subject: text: the atlas's glyphs are pardes's, byte for byte, tested A test of src/host/Glyphs.zig asks pardes's own Atlas (cachedGlyph, in its cells) and the host's atlas for the printable ASCII glyphs of six faces (Go Mono, its bold and italic, JetBrains Mono NL and its medium, Go's sans) at 13, 19.5, 26 and 52 px: 1980 of the 2256 have the same ink box, the same coverage bytes and the same place against the pen and baseline (the sans's advance centred in pardes's cell as pardes centres it); the other 276 reach out of pardes's cell (descenders, `|`, `_`), which cuts them off, and are counted, not compared. Both rasterize with font.c's ui_font_raster over one FreeType face. What differs is only where the coverage goes after: pardes mixes it in ui.frag with blending off, the host through SDL's premultiplied blend, the same mix(ground, ink, a) for an opaque ink. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_016Y9MNBECMWcJD5pdS4SR6T --- src/host/Glyphs.zig | 54 +++++++++++++++++++++++++++++++++++++++++++++++++++++ src/host/root.zig | 1 + 2 files changed, 55 insertions(+) diff --git a/src/host/Glyphs.zig b/src/host/Glyphs.zig index 8de40cf..b3fdbfb 100644 --- a/src/host/Glyphs.zig +++ b/src/host/Glyphs.zig @@ -223,3 +223,57 @@ fn density(renderer: *c.SDL_Renderer) f32 { } return @max(0.25, @max(sx, sy)); } + +test "the atlas's glyphs are pardes's: the same coverage bytes and the same place" { + const gpa = std.testing.allocator; + var g: Glyphs = .{ .gpa = gpa }; + g.stage = try gpa.alloc(u8, side * side); + @memset(g.stage, 0); + defer g.deinit(); + var compared: usize = 0; + var clipped: usize = 0; + // Grid faces (every advance the cell's) and a proportional one, which + // pardes centres in its cell. + for ([_]abi.Face{ .mono, .mono_bold, .mono_italic, .code, .code_medium, .sans }) |face| for ([_]f32{ 13, 19.5, 26, 52 }) |px| { + // pardes's Atlas over the same face: its own stage, its cells. + var pardes: Atlas = .{ .font = c.sk_text_ui(@backingInt(face)).?, .atlas_stage = try gpa.alloc(u8, side * side), .glyphs = .init(gpa) }; + defer { + gpa.free(pardes.atlas_stage); + pardes.glyphs.deinit(); + } + var cell_w: c_int = 0; + var height: c_int = 0; + var ascent: c_int = 0; + pardes.cellMetrics(px, &cell_w, &height, &ascent); + const body: Atlas.Size = .{ .px = px, .cell_w = @intCast(cell_w), .height = @intCast(height), .baseline = ascent }; + pardes.reset(body.cell_w); + for (0x21..0x7f) |cp| { + const glyph = c.ui_font_glyph(pardes.font, @intCast(cp)); + const ours = g.rasterize(.{ .face = @backingInt(face), .glyph = glyph, .px = @intFromFloat(px * 64) }).?; + // pardes centres the advance in its cell: nothing for a grid + // face, whose advances are all the cell's. + const shift = @divTrunc(cell_w - c.sk_text_advance(@backingInt(face), px, glyph), 2); + const x = shift + ours.dx; + const y = ascent + ours.dy; + // Ink pardes's cell cuts off is not comparable: count it. + if (x < 0 or y < 0 or x + ours.w > cell_w or y + ours.h > height) { + clipped += 1; + continue; + } + const at = pardes.cachedGlyph(.{ .face = 0, .glyph = glyph, .role = .body }, body, body, 1, 1).?; + const cell = pardes.atlas_stage[at.v * side + at.u ..]; + const theirs = Atlas.inkBounds(cell, side, body.cell_w, body.height).?; + try std.testing.expectEqual(theirs.max_x - theirs.min_x + 1, ours.w); + try std.testing.expectEqual(theirs.max_y - theirs.min_y + 1, ours.h); + try std.testing.expectEqual(@as(i64, @intCast(theirs.min_x)), x); + try std.testing.expectEqual(@as(i64, @intCast(theirs.min_y)), y); + for (0..ours.h) |row| try std.testing.expectEqualSlices(u8, cell[(theirs.min_y + row) * side + theirs.min_x ..][0..ours.w], g.stage[(ours.v + row) * side + ours.u ..][0..ours.w]); + compared += 1; + } + }; + // 6 faces at 4 sizes, 94 printable ASCII glyphs each: 2256, a few of + // which reach out of pardes's cell (`_` under it, `|` over it). + std.debug.print("pardes's raster: {d} glyphs equal byte for byte, {d} clipped by its cell\n", .{ compared, clipped }); + try std.testing.expectEqual(@as(usize, 2256), compared + clipped); + try std.testing.expect(compared > 1900); +} diff --git a/src/host/root.zig b/src/host/root.zig index f15493b..73a2a1b 100644 --- a/src/host/root.zig +++ b/src/host/root.zig @@ -302,4 +302,5 @@ test { _ = @import("control.zig"); _ = @import("state_files.zig"); _ = @import("timing_marker.zig"); + _ = @import("Glyphs.zig"); } -- cgit v1.3