diff options
| author | Gabriel Schneider <[email protected]> | 2026-11-17 09:06:20 -0300 |
|---|---|---|
| committer | Gabriel Schneider <[email protected]> | 2026-11-17 12:15:01 -0300 |
| commit | 303478956e0086467a9073d7f6fa67b51b6580ad (patch) | |
| tree | 6c69e13eef575b514433373893e1f14c435b4f1a /src/sketch/root.zig | |
| parent | c94455e1a8d2a430d8dcd74603bcd0d7b27aeb68 (diff) | |
| download | sketchbook-303478956e0086467a9073d7f6fa67b51b6580ad.tar.gz sketchbook-303478956e0086467a9073d7f6fa67b51b6580ad.zip | |
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 <[email protected]>
Claude-Session: https://claude.ai/code/session_016Y9MNBECMWcJD5pdS4SR6T
Diffstat (limited to 'src/sketch/root.zig')
| -rw-r--r-- | src/sketch/root.zig | 26 |
1 files changed, 21 insertions, 5 deletions
diff --git a/src/sketch/root.zig b/src/sketch/root.zig index 755e446..8a24417 100644 --- a/src/sketch/root.zig +++ b/src/sketch/root.zig @@ -40,9 +40,13 @@ pub const List = @import("list.zig").List; /// Data computed in draw from a few State fields, kept in Session and rebuilt when they change (or after a reload). pub const Derived = @import("derived.zig").Derived; pub const web = @import("web.zig"); +/// Text measured and laid out anywhere (`update` too): `size`, `layout`, +/// `font`, and the `Paragraph` `Frame.paragraph` draws. +pub const text = @import("text.zig"); /// sfxr's sound effects, rendered to samples for `host.newSound`. pub const sfxr = @import("sfxr.zig"); const request = @import("request.zig"); +const text_module = text; pub const Request = request.Request; pub const Handled = request.Handled; @@ -157,13 +161,23 @@ pub const Frame = struct { pub fn blend(f: *Frame, mode: BlendMode) void { f.emit(.{ .blend = mode }); } + /// Text from its top left corner, scale 1 a 13 px face; a `\n` starts + /// a line. `sk.text.size(.mono, scale, bytes)` is what it covers. pub fn text(f: *Frame, x: f32, y: f32, scale: f32, bytes: []const u8, color: anytype) void { - f.emit(.{ .text = .{ .x = x, .y = y, .scale = scale, .color = paint(color), .bytes = f.keep(u8, bytes) } }); + f.textIn(.mono, x, y, scale, bytes, color); } - /// `text` in another face of the built-in family: bold or italic mono, - /// or the sans (`host.textSizeIn` measures it). + /// `text` in another face: bold or italic mono, the sans, JetBrains + /// Mono NL, or a sketch's own (`sk.text.font`). pub fn textIn(f: *Frame, face: Face, x: f32, y: f32, scale: f32, bytes: []const u8, color: anytype) void { - f.emit(.{ .text = .{ .x = x, .y = y, .scale = scale, .color = paint(color), .bytes = f.keep(u8, bytes), .face = face } }); + if (std.mem.indexOfScalar(u8, bytes, '\n') != null) { + _ = f.paragraph(x, y, bytes, .{ .face = face, .scale = scale, .color = paint(color) }); + } else f.emit(.{ .text = .{ .x = x, .y = y, .scale = scale, .color = paint(color), .bytes = f.keep(u8, bytes), .face = face } }); + } + /// A paragraph from its top left corner (`sk.text.Paragraph`: a width + /// 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); } /// `textIn` laid out and rasterized at `raster` and drawn at `scale`, /// its position not snapped to a pixel: for text under a moving @@ -174,7 +188,7 @@ pub const Frame = struct { f.emit(.{ .text = .{ .x = x, .y = y, .scale = scale, .color = paint(color), .bytes = f.keep(u8, bytes), .face = face, .raster = raster } }); } pub fn print(f: *Frame, x: f32, y: f32, scale: f32, color: anytype, comptime fmt: []const u8, args: anytype) void { - f.emit(.{ .text = .{ .x = x, .y = y, .scale = scale, .color = paint(color), .bytes = f.format(fmt, args) } }); + f.textIn(.mono, x, y, scale, f.format(fmt, args), color); } /// Formats into scratch memory. pub fn format(f: *Frame, comptime fmt: []const u8, args: anytype) []const u8 { @@ -369,6 +383,7 @@ pub fn Glue(comptime App: type, comptime made: Build) type { /// Calls `func` with the arguments its parameter types ask for. fn call(comptime func: anytype, comptime name: []const u8, offer: Offer) @typeInfo(@TypeOf(func)).@"fn".return_type.? { + text_module.host = offer.ctx.host; const F = @TypeOf(func); var args: std.meta.ArgsTuple(F) = undefined; inline for (@typeInfo(F).@"fn".param_types, 0..) |P, i| { @@ -461,6 +476,7 @@ test { _ = @import("list.zig"); _ = @import("derived.zig"); _ = sfxr; + _ = text; } test "setup receives session resources without a Frame and reports errors" { |
