//! 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 NL, for code, advances as Go Mono does. // No ligatures: `->` is two characters, as it is in a source. .{ .scale = 2, .text = "p->n != 0x10", .face = .code_medium }, }; 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; } // A paragraph (sk.text): wrapped at 200, centred, a span in red and // one twice the size, its box from sk.text.layout and a caret drawn // before "twice" from the Layout it returns. const words = "Centred, wrapped at spaces, in red and twice the size."; const p: sk.text.Paragraph = .{ .width = 200 * k, .@"align" = .center, .scale = k, .spans = &.{ .{ .start = 31, .color = .{ .r = 255, .g = 80, .b = 80 } }, .{ .start = 35 }, .{ .start = 39, .scale = 2 * k }, .{ .start = 44 } }, .caret = 39 }; const l = sk.text.layout(words, p); f.fill(sk.Rect{ .x = 420 * k, .y = 175 * k, .w = 200 * k, .h = l.h }, 0x204060); const drawn = f.paragraph(420 * k, 175 * k, words, p); f.fill(sk.Rect{ .x = drawn.caret.x, .y = drawn.caret.y, .w = k, .h = drawn.caret.h }, 0xffff00); }