summaryrefslogtreecommitdiff
path: root/src/sketch/text.zig
diff options
context:
space:
mode:
Diffstat (limited to 'src/sketch/text.zig')
-rw-r--r--src/sketch/text.zig48
1 files changed, 43 insertions, 5 deletions
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);
+}