From 303478956e0086467a9073d7f6fa67b51b6580ad Mon Sep 17 00:00:00 2001 From: Gabriel Schneider Date: Tue, 17 Nov 2026 09:06:20 -0300 Subject: 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 Claude-Session: https://claude.ai/code/session_016Y9MNBECMWcJD5pdS4SR6T --- src/sketch/web.zig | 25 ++++++++++++++++++------- 1 file changed, 18 insertions(+), 7 deletions(-) (limited to 'src/sketch/web.zig') diff --git a/src/sketch/web.zig b/src/sketch/web.zig index 804a4df..e12acb3 100644 --- a/src/sketch/web.zig +++ b/src/sketch/web.zig @@ -15,6 +15,11 @@ const js = struct { extern "host" fn newImage(w: u32, h: u32) u32; extern "host" fn freeImage(image: u32) void; extern "host" fn clock() f64; + /// canvas measureText: the run's width, ascent and height into `out`, + /// and with `xs_len` (the run's length) each byte's x. + extern "host" fn textRun(face: u32, scale: f32, ptr: [*]const u8, len: usize, xs: [*]f32, xs_len: usize, out: *[3]f32) void; + /// A FontFace from the bytes: its face, or -1. + extern "host" fn font(ptr: [*]const u8, len: usize) i32; extern "host" fn openFile(command: [*]const u8, command_len: usize, pattern: [*]const u8, pattern_len: usize) void; extern "canvas" fn clear(r: u32, g: u32, b: u32, a: u32) void; extern "canvas" fn fill(x: f32, y: f32, w: f32, h: f32, r: u32, g: u32, b: u32, a: u32) void; @@ -30,7 +35,7 @@ const js = struct { extern "canvas" fn target(id: u32) void; /// `abi.BlendMode` by value. extern "canvas" fn blend(mode: u32) void; - extern "canvas" fn text(x: f32, y: f32, scale: f32, ptr: [*]const u8, len: usize, r: u32, g: u32, b: u32, a: u32) void; + extern "canvas" fn text(x: f32, y: f32, scale: f32, face: u32, ptr: [*]const u8, len: usize, r: u32, g: u32, b: u32, a: u32) void; extern "canvas" fn clip(enabled: u32, x: f32, y: f32, w: f32, h: f32) void; extern "canvas" fn canvas(w: u32, h: u32) void; extern "canvas" fn cursor(kind: u32) void; @@ -207,7 +212,7 @@ fn Driver(comptime App: type) type { .shape => |v| js.shape(v.points.ptr, v.points.len, v.ends.ptr, v.ends.len, v.colors.ptr, v.colors.len, v.color.r, v.color.g, v.color.b, v.color.a), .target => |v| js.target(@backingInt(v)), .blend => |v| js.blend(@backingInt(v)), - .text => |v| js.text(v.x, v.y, v.scale, v.bytes.ptr, v.bytes.len, v.color.r, v.color.g, v.color.b, v.color.a), + .text => |v| js.text(v.x, v.y, v.scale, @backingInt(v.face), v.bytes.ptr, v.bytes.len, v.color.r, v.color.g, v.color.b, v.color.a), .clip => |v| if (v) |r| js.clip(1, r.x, r.y, r.w, r.h) else js.clip(0, 0, 0, 0, 0), .canvas => |v| { input.width = @floatFromInt(v.w); @@ -409,7 +414,8 @@ fn Driver(comptime App: type) type { .freeBuffer = freeBuffer, .writeBuffer = writeBuffer, .imageSize = imageSize, - .textSize = textSize, + .textRun = textRun, + .font = font, .loadSound = loadSound, .decodeSound = unsupportedSound, .newSound = newSound, @@ -449,10 +455,15 @@ fn Driver(comptime App: type) type { fn imageSize(_: *anyopaque, img: abi.Image) [2]u32 { return .{ js.imageWidth(@backingInt(img)), js.imageHeight(@backingInt(img)) }; } - /// As host.js draws text: 8 px a character and a 16 px line at scale 1. - fn textSize(_: *anyopaque, scale: f32, bytes: []const u8, _: abi.Face) [2]f32 { - const count = std.unicode.utf8CountCodepoints(bytes) catch bytes.len; - return .{ @as(f32, @floatFromInt(count)) * 8 * scale, 16 * scale }; + /// As host.js draws text: canvas measureText, in the page's fonts. + fn textRun(_: *anyopaque, face: abi.Face, scale: f32, bytes: []const u8, xs: []f32) abi.TextRun { + var out: [3]f32 = .{ 0, 0, 0 }; + js.textRun(@backingInt(face), scale, bytes.ptr, bytes.len, xs.ptr, xs.len, &out); + return .{ .width = out[0], .ascent = out[1], .height = out[2] }; + } + fn font(_: *anyopaque, bytes: []const u8) ?abi.Face { + const face = js.font(bytes.ptr, bytes.len); + return if (face < 0) null else @enumFromInt(face); } /// Only sounds the page has decoded (opened or dropped files): the /// browser decodes asynchronously, so there is no reading on demand. -- cgit v1.3