diff options
Diffstat (limited to 'src/gui/gui.zig')
| -rw-r--r-- | src/gui/gui.zig | 84 |
1 files changed, 84 insertions, 0 deletions
diff --git a/src/gui/gui.zig b/src/gui/gui.zig index 51ec6bbf..a67602e8 100644 --- a/src/gui/gui.zig +++ b/src/gui/gui.zig @@ -9206,3 +9206,87 @@ fn prepareAddonGpu(g: *Gui, gpa: std.mem.Allocator, cmd: *c.SDL_GPUCommandBuffer } if (g.addon_gpu) |*gpu| gpu.endFrame(); } + +test "the atlas's bytes: a fixed key list at 27 px and at 54 px (2x) rasterizes to the recorded stage and slots" { + // The glyph atlas the frames sample, byte for byte and on any machine: + // FreeType and HarfBuzz are the build's, the faces the repo's (the + // embedded Adwaita Mono, assets/MapleMono-NF for ligatures and Nerd + // icons). CJK has no face of the repo's and no fallback of pardes's + // list: the frame corpus draws its .notdef. Recorded before the GPU + // move (.scratch/sketch/briefs/gpu-s0.md); a change here is a change + // to every frame. + const want = [2][2]u64{ .{ 0xa03053f14e2c5472, 0x9029886b638b07d5 }, .{ 0xcbaa53a0103aaff5, 0x7563d69768f7d867 } }; + const maple = try filesystem.readFile(std.testing.allocator, "assets/MapleMono-NF-Regular.ttf"); + defer std.testing.allocator.free(maple); + defer display_scale = 1; + var got: [2][2]u64 = undefined; + for ([_]f32{ 27, 54 }, [_]f32{ 1, 2 }, &got) |px, scale, *hashes| { + display_scale = scale; + var stage_hash = std.hash.Wyhash.init(0); + var slots_hash = std.hash.Wyhash.init(0); + // 0: Adwaita Mono, Maple its fallback; 1: Maple, whose ligatures shape. + for ([_][]const u8{ font_ttf, maple }) |primary| { + const font = c.ui_font_new(primary.ptr, @intCast(primary.len)) orelse return error.FontInit; + defer c.ui_font_free(font); + const fallback = c.ui_font_new(maple.ptr, @intCast(maple.len)) orelse return error.FontInit; + defer c.ui_font_free(fallback); + var g = try textTestGui(font, px); + defer font_allocator = std.heap.smp_allocator; + defer std.testing.allocator.free(g.atlas_stage); + defer g.glyphs.deinit(); + defer g.words.deinit(std.testing.allocator); + g.fallbacks[0] = .{ .face = fallback }; + g.fallback_count = 1; + try std.testing.expect(fitTaglineFont(&g, 80)); + const decorations = [_]GlyphDecoration{ + .{}, + .{ .bold = true }, + .{ .underline = .single }, + .{ .underline = .double }, + .{ .underline = .curly }, + .{ .underline = .dotted }, + .{ .underline = .dashed }, + .{ .strikethrough = true }, + }; + for (0x21..0x7f) |cp| { + const drawn = glyphForCodepoint(&g, @intCast(cp)); + for (decorations) |decoration| _ = cachedGlyph(&g, .{ .face = drawn.face, .glyph = drawn.glyph, .role = .body, .decoration = decoration }) orelse return error.AtlasFull; + for (decorations[0..2]) |decoration| _ = cachedGlyph(&g, .{ .face = drawn.face, .glyph = drawn.glyph, .role = .tagline, .decoration = decoration }) orelse return error.AtlasFull; + } + const space = glyphForCodepoint(&g, ' '); + _ = cachedGlyph(&g, .{ .face = space.face, .glyph = space.glyph, .role = .body, .decoration = .{ .underline = .single } }) orelse return error.AtlasFull; + for ([_]u32{ 0x2500, 0x2502, 0x250c, 0x2510, 0x2514, 0x2518, 0x251c, 0x2524, 0x252c, 0x2534, 0x253c }) |cp| for ([_]pardes.FontRole{ .body, .tagline }) |role| { + _ = cachedGlyph(&g, .{ .face = 0, .glyph = cp, .role = role, .rule = true }) orelse return error.AtlasFull; + }; + for ([_]u32{ '$', '^', 'j' }) |cp| for ([_]bool{ false, true }) |bold| { + const drawn = glyphForCodepoint(&g, cp); + _ = cachedGlyph(&g, .{ .face = drawn.face, .glyph = drawn.glyph, .role = .tagline, .decoration = .{ .bold = bold }, .centered = true }) orelse return error.AtlasFull; + }; + // Nerd icons (Yazi's folder, a file, powerline's arrow): Maple's, + // the primary's or the fallback's. + for ([_]u32{ 0xe5ff, 0xf07b, 0xe0b0 }) |cp| { + const drawn = glyphForCodepoint(&g, cp); + try std.testing.expect(drawn.glyph != 0); + _ = cachedGlyph(&g, .{ .face = drawn.face, .glyph = drawn.glyph, .role = .body }) orelse return error.AtlasFull; + } + // Ligature strips, shaped by the words' cache as a frame shapes them. + const line = textTestLine("a->b => != <= >= == === !== |> <| :: ++ -- www office"); + for (0..line.len) |i| _ = cellSlot(&g, &line, i, null, .body); + stage_hash.update(g.atlas_stage); + const Entry = struct { key: u64, u: u32, v: u32 }; + var entries: std.ArrayList(Entry) = .empty; + defer entries.deinit(std.testing.allocator); + var it = g.glyphs.iterator(); + while (it.next()) |e| try entries.append(std.testing.allocator, .{ .key = @bitCast(e.key_ptr.*), .u = e.value_ptr.u, .v = e.value_ptr.v }); + std.mem.sort(Entry, entries.items, {}, struct { + fn less(_: void, x: Entry, y: Entry) bool { + return x.key < y.key; + } + }.less); + for (entries.items) |e| slots_hash.update(std.mem.asBytes(&e)); + } + hashes.* = .{ stage_hash.final(), slots_hash.final() }; + } + if (!std.meta.eql(want, got)) std.debug.print("atlas hashes: .{{ .{{ 0x{x}, 0x{x} }}, .{{ 0x{x}, 0x{x} }} }}\n", .{ got[0][0], got[0][1], got[1][0], got[1][1] }); + try std.testing.expectEqual(want, got); +} |
