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 ++++++++++++++++--- docs/text.md | 1 + src/sketch/root.zig | 2 +- src/sketch/text.zig | 48 +++++++++++++++++++++++++++++++++++++++++++----- 4 files changed, 61 insertions(+), 9 deletions(-) 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 diff --git a/docs/text.md b/docs/text.md index a4f2856..6ee3c96 100644 --- a/docs/text.md +++ b/docs/text.md @@ -14,6 +14,7 @@ Cairo, glib and fontconfig leave the build. | `f.textScaled(face, x, y, scale, raster, ...)` | draw | laid out at `raster`, drawn at `scale`, unsnapped | | `f.paragraph(x, y, bytes, .{...})` | draw | draws a paragraph and returns its `Layout` | | `sk.text.layout(bytes, .{...})` | anywhere | the same paragraph's `Layout`, nothing drawn | +| `sk.text.places(bytes, .{...}, out)` | anywhere | `layout`, and each byte's place (where a caret before it sits) in `out` | | `sk.text.size(face, scale, bytes)` | anywhere | width and height, `layout` with no width | | `sk.text.font(bytes)` | anywhere | a sketch's own face from TTF/OTF bytes, a `Face` beyond the ten | | `host.textSize`, `host.textSizeIn` | draw | kept: `sk.text.size` for one line | diff --git a/src/sketch/root.zig b/src/sketch/root.zig index 8a24417..490b2f9 100644 --- a/src/sketch/root.zig +++ b/src/sketch/root.zig @@ -177,7 +177,7 @@ pub const Frame = struct { /// to wrap at, alignment, spans, the glyphs revealed, a caret), drawn; /// its `Layout` returned, as `sk.text.layout` gives it, from `x`, `y`. pub fn paragraph(f: *Frame, x: f32, y: f32, bytes: []const u8, p: text_module.Paragraph) text_module.Layout { - return text_module.lay(f.keep(u8, bytes), p, x, y, f); + return text_module.lay(f.keep(u8, bytes), p, x, y, f, &.{}); } /// `textIn` laid out and rasterized at `raster` and drawn at `scale`, /// its position not snapped to a pixel: for text under a moving diff --git a/src/sketch/text.zig b/src/sketch/text.zig index e4543b0..636258f 100644 --- a/src/sketch/text.zig +++ b/src/sketch/text.zig @@ -83,7 +83,17 @@ pub fn size(face: Face, scale: f32, bytes: []const u8) [2]f32 { /// `p`'s layout of `bytes`, nothing drawn. pub fn layout(bytes: []const u8, p: Paragraph) Layout { - return lay(bytes, p, 0, 0, null); + return lay(bytes, p, 0, 0, null, &.{}); +} + +/// `p`'s layout of `bytes`, and in `out` the place of every byte: where a +/// caret before it sits (`layout` with `.caret = i`), `out[bytes.len]` the +/// end's. An editor draws ranges and hit-tests with it: the byte at a point +/// is the last whose line holds the point's y and whose x is at or left of +/// it. `out` has a place a byte and one more. +pub fn places(bytes: []const u8, p: Paragraph, out: []Caret) Layout { + std.debug.assert(out.len > bytes.len); + return lay(bytes, p, 0, 0, null, out); } /// The longest run measured at once; longer stretches are measured in @@ -163,8 +173,8 @@ fn fit(bytes: []const u8, p: Paragraph, start: usize) Line { } /// Lays `bytes` out from `x`, `y`, and with a frame, draws it: a -/// `Command.text` a piece. -pub fn lay(bytes: []const u8, p: Paragraph, x: f32, y: f32, frame: anytype) Layout { +/// `Command.text` a piece; with `out`, each byte's place (`places`). +pub fn lay(bytes: []const u8, p: Paragraph, x: f32, y: f32, frame: anytype, out_places: []Caret) Layout { const box = if (p.width > 0) p.width else if (p.@"align" != .left) blk: { var left = p; left.@"align" = .left; @@ -195,6 +205,9 @@ pub fn lay(bytes: []const u8, p: Paragraph, x: f32, y: f32, frame: anytype) Layo if (p.caret) |caret| if (caret >= at and caret <= end and caret <= line.end) { out.caret.x = pen + (if (caret < end) xs[caret - at] else r.width); }; + if (out_places.len > 0) for (at..end) |i| { + out_places[i] = .{ .x = pen + xs[i - at], .y = top, .h = line.height }; + }; const count: u32 = @intCast(std.unicode.utf8CountCodepoints(bytes[at..end]) catch end - at); if (@TypeOf(frame) != @TypeOf(null) and shown < p.reveal) { var cut = end; @@ -209,6 +222,11 @@ pub fn lay(bytes: []const u8, p: Paragraph, x: f32, y: f32, frame: anytype) Layo pen += r.width; at = end; } + // A `\n` or the spaces a line wraps at, and the end, sit at its end. + const after = if (line.next == bytes.len) line.next + 1 else line.next; + if (out_places.len > 0) for (line.end..after) |i| { + out_places[i] = .{ .x = pen, .y = top, .h = line.height }; + }; out.w = @max(out.w, line.w); out.lines += 1; top += line.height; @@ -267,15 +285,35 @@ test "reveal draws the first glyphs of the same layout" { } }; var f: Frame = .{}; - const whole = lay("ab cd\nef", .{ .width = 32 }, 0, 0, &f); + const whole = lay("ab cd\nef", .{ .width = 32 }, 0, 0, &f, &.{}); try t.expectEqual(@as(usize, 3), f.n); try t.expectEqualStrings("ab", f.drawn[0]); try t.expectEqualStrings("cd", f.drawn[1]); try t.expectEqualStrings("ef", f.drawn[2]); f = .{}; - const part = lay("ab cd\nef", .{ .width = 32, .reveal = 4 }, 0, 0, &f); + const part = lay("ab cd\nef", .{ .width = 32, .reveal = 4 }, 0, 0, &f, &.{}); try t.expectEqual(whole, part); try t.expectEqual(@as(u32, 7), whole.glyphs); try t.expectEqual(@as(usize, 2), f.n); try t.expectEqualStrings("c", f.drawn[1]); } + +test "a paragraph's places are where its caret sits, byte by byte" { + const t = std.testing; + // Wrapped at a space, a `\n`, a span twice the size on the first line, + // a two-byte codepoint and a last empty line. + const text = "ab cd efgh\nij é\n"; + const p: Paragraph = .{ .width = 80, .spans = &.{ .{ .start = 3, .scale = 2 }, .{ .start = 5, .scale = 1 } } }; + var out: [text.len + 1]Caret = undefined; + try t.expectEqual(layout(text, p), places(text, p, &out)); + for (0..text.len + 1) |i| { + var at = p; + at.caret = @intCast(i); + try t.expectEqual(layout(text, at).caret, out[i]); + } + // The big span's line is taller; the byte after its line, on the next. + try t.expect(out[0].h > 16); + try t.expectEqual(@as(f32, 3 * 8 + 2 * 8 * 2), out[5].x); + try t.expectEqual(out[6].y, out[7].y); + try t.expect(out[6].y > out[5].y); +} -- cgit v1.3