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/abi.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/abi.zig')
| -rw-r--r-- | src/abi.zig | 40 |
1 files changed, 27 insertions, 13 deletions
diff --git a/src/abi.zig b/src/abi.zig index 6411b0d..e5fe372 100644 --- a/src/abi.zig +++ b/src/abi.zig @@ -62,9 +62,15 @@ pub const Color = extern struct { /// The face a text is set in: the Go family, and JetBrains Mono NL (`code`, /// without ligatures; its medium weight for small sizes on a dark ground) /// for programs, all bundled with the native host. Mono is for data, sans -/// for what is said about it. The web host has one face, the page's -/// `monospace`. -pub const Face = enum(u8) { mono, mono_bold, mono_italic, sans, sans_medium, sans_bold, code, code_bold, code_italic, code_medium }; +/// for what is said about it. Past these ten, a sketch's own fonts +/// (`Host.VTable.font`). The web host draws them in the page's +/// `monospace` and `sans-serif`. +pub const Face = enum(u8) { mono, mono_bold, mono_italic, sans, sans_medium, sans_bold, code, code_bold, code_italic, code_medium, _ }; + +/// A run of text in one face at one scale, measured (`Host.VTable.textRun`): +/// its advance, and its line box (the ascent, the baseline from the top, and +/// the height), in canvas units. +pub const TextRun = struct { width: f32 = 0, ascent: f32 = 0, height: f32 = 0 }; /// How 2D drawing combines with what is already there (`Command.blend`). /// Both are fixed-function on both hosts and match: `alpha` is source @@ -646,9 +652,15 @@ pub const Host = struct { writeBuffer: *const fn (*anyopaque, Buffer, u32, []const u8) anyerror!void, /// Zero while unknown. imageSize: *const fn (*anyopaque, Image) [2]u32, - /// What `Command.text` gives the bytes at a scale: the width they - /// advance and the height of their line, in canvas units. - textSize: *const fn (*anyopaque, f32, []const u8, Face) [2]f32, + /// What `Command.text` gives the bytes in a face at a scale, as data: + /// a pure function of the font, the scale and the bytes, never of the + /// display. With the last slice (empty, or one a byte), each byte's x + /// from the run's start; a byte inside a cluster has its cluster's. + /// `sketch.text` lays paragraphs out over it. + textRun: *const fn (*anyopaque, Face, f32, []const u8, []f32) TextRun, + /// A face for TTF or OTF bytes, copied: the same face for the same + /// bytes. Null when they are not a font. + font: *const fn (*anyopaque, []const u8) ?Face, /// `.none` if the file does not load. loadSound: *const fn (*anyopaque, []const u8) Audio, /// WAV, FLAC or Ogg Vorbis bytes in memory. Not cached. `.none` if @@ -705,16 +717,18 @@ pub const Host = struct { return h.vtable.imageSize(h.ctx, image); } /// The size `f.text(x, y, scale, bytes, ...)` covers from `x, y`: width - /// and line height in canvas units, for laying text out (in `draw`: - /// `update` gets no host). Native text is shaped and hinted at the size - /// it is drawn, so a width is not a multiple of the scale: measure at - /// the scale drawn. + /// and line height in canvas units, of one line (`sketch.text.size` + /// measures lines, anywhere, `update` too). Advances are whole pixels + /// at the size drawn, so a width is not a multiple of the scale: measure + /// at the scale drawn. pub fn textSize(h: Host, scale: f32, bytes: []const u8) [2]f32 { - return h.vtable.textSize(h.ctx, scale, bytes, .mono); + const run = h.vtable.textRun(h.ctx, .mono, scale, bytes, &.{}); + return .{ run.width, run.height }; } /// `textSize` for `f.textIn(face, ...)`. pub fn textSizeIn(h: Host, face: Face, scale: f32, bytes: []const u8) [2]f32 { - return h.vtable.textSize(h.ctx, scale, bytes, face); + const run = h.vtable.textRun(h.ctx, face, scale, bytes, &.{}); + return .{ run.width, run.height }; } pub fn loadSound(h: Host, path: []const u8) Audio { return h.vtable.loadSound(h.ctx, path); @@ -886,7 +900,7 @@ pub const hash: u64 = std.hash.Wyhash.hash(@import("abi_hash").layout, build); pub const layout_hash: u64 = blk: { @setEvalBranchQuota(1_000_000); var h: u64 = 0; - for (.{ Rect, Color, BlendMode, Corners, Event, Mode, Stream, Input, Image, Audio, Cursor, Voice, Command, gpu.Pass, gpu.Pipeline, gpu.Draw, gpu.Dispatch, gpu.PostProcess, Host, Host.VTable, Context, Field, Layout, Info, Memory, Sketch }) |T| { + for (.{ Rect, Color, Face, TextRun, BlendMode, Corners, Event, Mode, Stream, Input, Image, Audio, Cursor, Voice, Command, gpu.Pass, gpu.Pipeline, gpu.Draw, gpu.Dispatch, gpu.PostProcess, Host, Host.VTable, Context, Field, Layout, Info, Memory, Sketch }) |T| { h = std.hash.Wyhash.hash(h, @typeName(T) ++ layout.signature(T)); } break :blk h; |
