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 --- src/sketch/root.zig | 2 +- src/sketch/text.zig | 48 +++++++++++++++++++++++++++++++++++++++++++----- 2 files changed, 44 insertions(+), 6 deletions(-) (limited to 'src') 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