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/host | |
| 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/host')
| -rw-r--r-- | src/host/Host.zig | 12 | ||||
| -rw-r--r-- | src/host/TimingAtlas.zig | 3 | ||||
| -rw-r--r-- | src/host/gfx.zig | 13 |
3 files changed, 16 insertions, 12 deletions
diff --git a/src/host/Host.zig b/src/host/Host.zig index ede02df..9a2c0fa 100644 --- a/src/host/Host.zig +++ b/src/host/Host.zig @@ -1065,7 +1065,8 @@ const api: abi.Host.VTable = .{ .freeBuffer = apiFreeBuffer, .writeBuffer = apiWriteBuffer, .imageSize = apiImageSize, - .textSize = apiTextSize, + .textRun = apiTextRun, + .font = apiFont, .loadSound = apiLoadSound, .decodeSound = apiDecodeSound, .newSound = apiNewSound, @@ -1127,9 +1128,12 @@ fn apiSamples(ctx: *anyopaque, audio: abi.Audio) []const f32 { fn apiImageSize(ctx: *anyopaque, image: abi.Image) [2]u32 { return hostOf(ctx).images.size(image); } -fn apiTextSize(ctx: *anyopaque, scale: f32, bytes: []const u8, face: abi.Face) [2]f32 { - _ = ctx; - return gfx.textSize(scale, bytes, face); +fn apiTextRun(_: *anyopaque, face: abi.Face, scale: f32, bytes: []const u8, xs: []f32) abi.TextRun { + return gfx.textRun(face, scale, bytes, xs); +} +fn apiFont(_: *anyopaque, bytes: []const u8) ?abi.Face { + const face = c.sk_text_font(bytes.ptr, bytes.len); + return if (face < 0) null else @enumFromInt(face); } fn apiLoadSound(ctx: *anyopaque, path: []const u8) abi.Audio { return hostOf(ctx).audio.load(path); diff --git a/src/host/TimingAtlas.zig b/src/host/TimingAtlas.zig index 4294b6a..a00a87e 100644 --- a/src/host/TimingAtlas.zig +++ b/src/host/TimingAtlas.zig @@ -19,7 +19,8 @@ const glyphs = blk: { /// Called once with Painter on its canvas. Images owns the texture until shutdown. pub fn init(images: *gfx.Images, painter: *gfx.Painter) !Atlas { - const size = gfx.textSize(1, "0", .mono); + const run = gfx.textRun(.mono, 1, "0", &.{}); + const size: [2]f32 = .{ run.width, run.height }; var atlas: Atlas = .{ .advance = size[0], .cell_width = @ceil(size[0]) + padding * 2, diff --git a/src/host/gfx.zig b/src/host/gfx.zig index f84568a..d679067 100644 --- a/src/host/gfx.zig +++ b/src/host/gfx.zig @@ -613,15 +613,14 @@ fn fcolor(color: abi.Color) c.SDL_FColor { }; } -/// What `Command.text` covers at `scale`: the width of `bytes` and the -/// height of its line, in canvas units (`Host.textSize`, `textRun`). -pub fn textSize(scale: f32, bytes: []const u8, face: abi.Face) [2]f32 { - if (!(scale > 0) or !std.math.isFinite(scale)) return .{ 0, 0 }; - var advance: i32 = 0; +/// What `Command.text` gives `bytes` in `face` at `scale`, in canvas units: +/// `Host.VTable.textRun`. `xs` is empty, or a byte each. +pub fn textRun(face: abi.Face, scale: f32, bytes: []const u8, xs: []f32) abi.TextRun { + if (!(scale > 0) or !std.math.isFinite(scale) or (xs.len != 0 and xs.len != bytes.len)) return .{}; var ascent: i32 = 0; var height: i32 = 0; - _ = c.sk_text_shape(@backingInt(face), Glyphs.pixels(scale), bytes.ptr, bytes.len, null, 0, &advance, &ascent, &height); - return .{ @floatFromInt(advance), @floatFromInt(height) }; + const advance = c.sk_text_run(@backingInt(face), Glyphs.pixels(scale), bytes.ptr, bytes.len, if (xs.len == 0) null else xs.ptr, &ascent, &height); + return .{ .width = @floatFromInt(advance), .ascent = @floatFromInt(ascent), .height = @floatFromInt(height) }; } fn premultiply(color: abi.Color) abi.Color { |
