summaryrefslogtreecommitdiff
path: root/test/text.zig
blob: 1318d1f7a2dc483a3af0e7cde85adb83b0da2c27 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
//! Text for layout: each line sits on a box of the size `textSize` gives
//! it, at whole and fractional scales, the same picture at any canvas size.
const sk = @import("sketch");

pub const width = 640;
pub const height = 360;

const lines = [_]struct { scale: f32, text: []const u8, face: sk.Face = .mono, raster: f32 = 0 }{
    .{ .scale = 1, .text = "0123456789" },
    .{ .scale = 1.5, .text = "mov rax, [rbp-0x18]   ; 1.5" },
    .{ .scale = 2.25, .text = "0x7ffed849acf0  i -> W" },
    .{ .scale = 4, .text = "fn main() !void {}" },
    // The other faces: the sans is proportional, the bold mono is not.
    .{ .scale = 2, .text = "iiiiiiiiii", .face = .sans },
    .{ .scale = 2, .text = "iiiiiiiiii", .face = .mono_bold },
    // Rasterized at 3 and drawn at 1.5: half of what scale 3 covers (115 px
    // for ten digits), not what scale 1.5 would shape (120).
    .{ .scale = 1.5, .text = "0123456789", .raster = 3 },
    // JetBrains Mono, for code, advances as Go Mono does: ten columns
    // at scale 2 are the 160 px of the bold line above.
    .{ .scale = 2, .text = "rsp \u{2014}\u{25b8} 0x10", .face = .code },
};

pub fn draw(f: *sk.Frame) void {
    f.canvas(@intFromFloat(f.in.window_width), @intFromFloat(f.in.window_height));
    f.clear(0x101010);
    const k = f.in.window_height / height;
    var y: f32 = 10 * k;
    for (lines) |line| {
        var size = f.host.textSizeIn(line.face, line.scale * k, line.text);
        if (line.raster > 0) {
            const at = f.host.textSizeIn(line.face, line.raster * k, line.text);
            size = .{ at[0] * line.scale / line.raster, at[1] * line.scale / line.raster };
        }
        f.fill(sk.Rect{ .x = 10 * k, .y = y, .w = size[0], .h = size[1] }, 0x204060);
        if (line.raster > 0) f.textScaled(line.face, 10 * k, y, line.scale * k, line.raster * k, line.text, 0xffffff) else f.textIn(line.face, 10 * k, y, line.scale * k, line.text, 0xffffff);
        y += size[1] + 6 * k;
    }
}