summaryrefslogtreecommitdiff
path: root/src/host/Glyphs.zig
diff options
context:
space:
mode:
Diffstat (limited to 'src/host/Glyphs.zig')
-rw-r--r--src/host/Glyphs.zig54
1 files changed, 54 insertions, 0 deletions
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);
+}