From 7ad16df2813c0c3012bae6262014b07b7bc7277d Mon Sep 17 00:00:00 2001 From: Gabriel Schneider Date: Tue, 17 Nov 2026 09:29:57 -0300 Subject: text: sk.text.places, every byte's place in one layout An editor draws a range under its bytes, a caret on its glyph, a mark under a byte, and finds the byte under the pointer; with mixed sizes on a line a column is no longer a measure. `layout`'s `caret` gives one byte's place a layout, so a range, a caret and a hit test were a layout a byte. `sk.text.places(bytes, paragraph, out)` is the same layout with every byte's place in `out` (where a caret before that byte sits, a `Caret`: x, the line's top and height; a `\n` and the spaces a line wraps at are at its end, and one more for the end of the text). The byte at a point is then a loop over places: the last whose line holds its y and whose x is at or left of it. `lay` takes the slice to fill (empty: none), so `layout`, `places` and `f.paragraph` stay one function. Pure, as `layout`: in `update` too. The README's editor recipe and docs/text.md say so. Test: a paragraph's places are where its caret sits, byte by byte (wrapped at a space, a `\n`, a span twice the size, a two-byte codepoint, a last empty line). zig build test-text: 52 of 52 steps. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_016Y9MNBECMWcJD5pdS4SR6T --- README.md | 19 ++++++++++++++++--- 1 file changed, 16 insertions(+), 3 deletions(-) (limited to 'README.md') diff --git a/README.md b/README.md index 0602024..f54ab82 100644 --- a/README.md +++ b/README.md @@ -208,8 +208,12 @@ the paragraph's own); `reveal` (only the first N glyphs drawn, the layout still the whole text's: typewriter text costs nothing extra); and `caret` (a byte index). The `Layout` is `w`, `h`, `lines`, `glyphs` (a glyph is a codepoint) and `caret`: the left of the caret's glyph, the top of its line -and the line's height. `f.host.textSize(scale, bytes)` and -`textSizeIn(face, scale, bytes)` still measure one line. +and the line's height. `sk.text.places(bytes, paragraph, out)` is the +same layout with every byte's place in `out` (a `Caret` a byte and one for +the end, each where a caret before that byte sits): what an editor draws +ranges with and finds the byte under the pointer by, in one layout. +`f.host.textSize(scale, bytes)` and `textSizeIn(face, scale, bytes)` still +measure one line. Advances are whole pixels at the size drawn (HarfBuzz's, each rounded), so a width is not a multiple of the scale (ten digits are 80 px at scale 1, @@ -261,8 +265,17 @@ _ = f.paragraph(box.x, box.y, line, .{ .width = box.w, .face = .sans, .reveal = const done = s.shown >= sk.text.layout(line, .{ .width = box.w, .face = .sans }).glyphs; // in update too // A caret and per-token colours and sizes in source text. -const drawn = f.paragraph(x, y, source, .{ .face = .code, .spans = tokens, .caret = cursor }); +const src: sk.text.Paragraph = .{ .face = .code, .spans = tokens, .caret = cursor }; +const drawn = f.paragraph(x, y, source, src); f.fill(sk.Rect{ .x = drawn.caret.x, .y = drawn.caret.y, .w = 1, .h = drawn.caret.h }, 0xffcc00); + +// The byte under the pointer (in update too; px, py from the paragraph's +// x, y): the last whose line holds py and whose x is at or left of px. +_ = sk.text.places(source, src, places); // a Caret a byte and one more +var hit: ?usize = null; +for (places[0..source.len], 0..) |c, i| { + if (py >= c.y and py < c.y + c.h and px >= c.x) hit = i; +} ``` ## Drawing shapes -- cgit v1.3