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 --- vendor/text.c | 27 +++++++++++++++++++++++++++ vendor/text.h | 6 ++++++ 2 files changed, 33 insertions(+) (limited to 'vendor') diff --git a/vendor/text.c b/vendor/text.c index 818597d..09e3902 100644 --- a/vendor/text.c +++ b/vendor/text.c @@ -185,3 +185,30 @@ int sk_text_shape(int face, float px, const char *utf8, size_t len, SkGlyph *out *advance = pen; return n; } + +int32_t sk_text_run(int face, float px, const char *utf8, size_t len, float *xs, + int32_t *ascent, int32_t *height) { + static SkGlyph *glyphs; + static int room; + int32_t advance = 0; + int n = sk_text_shape(face, px, utf8, len, glyphs, room, &advance, ascent, height); + if (!xs) return advance; + if (n > room) { + SkGlyph *more = realloc(glyphs, (size_t)n * sizeof *glyphs); + if (!more) n = room; + else { + glyphs = more; + room = n; + sk_text_shape(face, px, utf8, len, glyphs, room, &advance, ascent, height); + } + } + for (size_t i = 0; i < len; i++) xs[i] = -1; + for (int k = 0; k < n; k++) + if (glyphs[k].cluster < len && xs[glyphs[k].cluster] < 0) xs[glyphs[k].cluster] = glyphs[k].x > 0 ? (float)glyphs[k].x : 0; + float last = 0; + for (size_t i = 0; i < len; i++) { + if (xs[i] < 0) xs[i] = last; + last = xs[i]; + } + return advance; +} diff --git a/vendor/text.h b/vendor/text.h index dd4eecf..355ad90 100644 --- a/vendor/text.h +++ b/vendor/text.h @@ -31,6 +31,12 @@ typedef struct { int sk_text_shape(int face, float px, const char *utf8, size_t len, SkGlyph *out, int max, int32_t *advance, int32_t *ascent, int32_t *height); +/* `sk_text_shape` measured: the run's advance, with `xs` (when not NULL, + `len` of them) the x of each byte from the run's start, a byte inside a + cluster at its cluster's. What `abi.Host.VTable.textRun` serves. */ +int32_t sk_text_run(int face, float px, const char *utf8, size_t len, float *xs, + int32_t *ascent, int32_t *height); + /* `glyph`'s own advance in `face` at `px`, in whole pixels (what the renderer centres font.c's raster in, so the glyph lands on its origin). */ int32_t sk_text_advance(int face, float px, uint32_t glyph); -- cgit v1.3