From 303478956e0086467a9073d7f6fa67b51b6580ad Mon Sep 17 00:00:00 2001 From: Gabriel Schneider Date: Tue, 17 Nov 2026 09:06:20 -0300 Subject: text: sk.text measures anywhere, Frame.paragraph wraps, aligns, spans, reveals and finds a caret The ABI's one text entry is textRun (Host.VTable.textSize goes): a run in a face at a scale, as data, its advance and line box and each byte's x, a pure function of the font, the scale and the bytes, never of the display; and font, a face for a sketch's own TTF/OTF bytes (abi.Face is non-exhaustive: faces past the ten are a sketch's). The native host serves them from vendor/text.c (sk_text_run, sk_text_font), the web host from canvas measureText and FontFace (web/host.js), and the web draws each run in the page's monospace or sans-serif in the face's weight and style instead of 8 px a character. src/sketch/text.zig (sk.text) does the rest in plain Zig, the same on every host: size, layout and font anywhere, update included (the glue sets the host before each call into the sketch; a test binary has none and calls sketchbook-text directly, which App.addTest now links); a Paragraph is a face, scale and colour, a width to wrap at spaces, an alignment, spans (from a byte on: a colour, a face, a scale), reveal (the first N glyphs drawn, the layout the whole text's) and a caret byte, and its Layout the size, lines, glyphs and the caret's place. f.paragraph draws one, a Command.text a piece. f.text, f.textIn and f.print are a paragraph with no width when the bytes hold a \n, else the one command they were; host.textSize and textSizeIn stay, one line through textRun. Test: sk.text's own tests (a wrap, the space not counted, centred and right, a caret at a line's start and end and after a \n, a span twice the size, a word wider than the width, an empty text; reveal drawing the first glyphs of the same layout), run through sketchbook-text; and test/text.zig draws a centred paragraph with a red and a double-size span and a caret from its Layout, which test/text.script pins. zig build test-text runs these and the atlas's comparison with pardes's raster. Every other pinned pixel of test/text.script holds: the lines it pins are the same pixels Pango drew. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_016Y9MNBECMWcJD5pdS4SR6T --- test/text.script | 8 ++++++++ test/text.zig | 9 +++++++++ 2 files changed, 17 insertions(+) (limited to 'test') diff --git a/test/text.script b/test/text.script index 776cc41..2ed0b29 100644 --- a/test/text.script +++ b/test/text.script @@ -19,3 +19,11 @@ expect-pixel .zig-cache/text/native.png 127 256 16 16 16 255 # ligatures: twelve characters at scale 2 are 192 px, on its own taller line. expect-pixel .zig-cache/text/native.png 200 275 32 64 96 255 expect-pixel .zig-cache/text/native.png 206 290 16 16 16 255 +# A paragraph (sk.text) wrapped at 200 and centred: three lines, the last +# with "twice" at twice the size, 16 + 16 + 31 px tall from y 175; the caret +# its Layout gives before "twice" is that line's height, at x 440. +expect-pixel .zig-cache/text/native.png 425 237 32 64 96 255 +expect-pixel .zig-cache/text/native.png 425 238 16 16 16 255 +expect-pixel .zig-cache/text/native.png 440 207 255 255 0 255 +expect-pixel .zig-cache/text/native.png 440 237 255 255 0 255 +expect-pixel .zig-cache/text/native.png 440 206 32 64 96 255 diff --git a/test/text.zig b/test/text.zig index d0e0eea..f60f13a 100644 --- a/test/text.zig +++ b/test/text.zig @@ -36,4 +36,13 @@ pub fn draw(f: *sk.Frame) void { 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); } -- cgit v1.3