diff options
| author | Gabriel Schneider <[email protected]> | 2026-11-10 12:53:22 -0300 |
|---|---|---|
| committer | Gabriel Schneider <[email protected]> | 2026-11-12 13:22:02 -0300 |
| commit | 45f705a5e590c3dfddc0a544e66beb9bf6205a4d (patch) | |
| tree | c98d19a1b2dfc4808a46b41cf1b931703ff467b1 /src | |
| parent | f8d3a32f4f995eeb7e162b0702998b0565c11e4e (diff) | |
| download | sketchbook-45f705a5e590c3dfddc0a544e66beb9bf6205a4d.tar.gz sketchbook-45f705a5e590c3dfddc0a544e66beb9bf6205a4d.zip | |
sketchbook: text rasterized at one scale and drawn at another (f.textScaled), steady under a moving camera
Diffstat (limited to 'src')
| -rw-r--r-- | src/abi.zig | 7 | ||||
| -rw-r--r-- | src/host/gfx.zig | 8 | ||||
| -rw-r--r-- | src/sketch/root.zig | 8 |
3 files changed, 17 insertions, 6 deletions
diff --git a/src/abi.zig b/src/abi.zig index 327110d..cf7b838 100644 --- a/src/abi.zig +++ b/src/abi.zig @@ -346,8 +346,11 @@ pub const Command = union(enum) { /// The blend for the 2D commands after it (`rect`, `shape`, `image`; /// `text` and `clear` ignore it). Each frame starts with `alpha`. blend: BlendMode, - /// One line from its top left corner; scale 1 is a 13 px font. - text: struct { x: f32, y: f32, scale: f32, color: Color, bytes: []const u8, face: Face = .mono }, + /// One line from its top left corner; scale 1 is a 13 px font. With + /// `raster` (not 0) it is laid out and rasterized at that scale and + /// drawn at `scale`: steady under a moving camera, where text shaped + /// anew at every size would shift letter against letter. + text: struct { x: f32, y: f32, scale: f32, color: Color, bytes: []const u8, face: Face = .mono, raster: f32 = 0 }, clip: ?Rect, /// Logical canvas size; the host scales the window to fit. canvas: struct { w: u32, h: u32 }, diff --git a/src/host/gfx.zig b/src/host/gfx.zig index 2aac380..95c3ecc 100644 --- a/src/host/gfx.zig +++ b/src/host/gfx.zig @@ -239,7 +239,7 @@ pub const Painter = struct { .image => |img| p.drawImage(img.image, img.dst, img.src, img.tint, img.corners), .text => |text| { _ = c.SDL_SetRenderDrawBlendMode(renderer, c.SDL_BLENDMODE_BLEND); - drawText(renderer, text.x, text.y, text.scale, text.bytes, text.color, text.face); + drawText(renderer, text.x, text.y, text.scale, text.bytes, text.color, text.face, if (text.raster > 0) text.raster else text.scale); }, .clip => |clip| { if (clip) |r| { @@ -527,8 +527,8 @@ fn fcolor(color: abi.Color) c.SDL_FColor { /// Native shaped text on Linux, using a 13px monospace face at scale 1. /// Other targets retain SDL's built-in 8x8 fallback. Coordinates are line-box tops. -pub fn drawText(renderer: *c.SDL_Renderer, x: f32, y: f32, scale: f32, bytes: []const u8, color: abi.Color, face: abi.Face) void { - if (@import("builtin").os.tag == .linux and c.sk_text_draw_face(renderer, @intFromEnum(face), x, y, scale, bytes.ptr, bytes.len, color.r, color.g, color.b, color.a)) return; +pub fn drawText(renderer: *c.SDL_Renderer, x: f32, y: f32, scale: f32, bytes: []const u8, color: abi.Color, face: abi.Face, raster: f32) void { + if (@import("builtin").os.tag == .linux and c.sk_text_draw_scaled(renderer, @intFromEnum(face), x, y, scale, raster, bytes.ptr, bytes.len, color.r, color.g, color.b, color.a)) return; var buf: [1024]u8 = undefined; const len = @min(std.mem.indexOfScalar(u8, bytes, 0) orelse bytes.len, buf.len - 1); @memcpy(buf[0..len], bytes[0..len]); @@ -575,7 +575,7 @@ fn missing(renderer: *c.SDL_Renderer, dst: abi.Rect) void { _ = c.SDL_SetRenderDrawBlendMode(renderer, c.SDL_BLENDMODE_BLEND); setColor(renderer, .{ .r = 110, .g = 30, .b = 70 }); _ = c.SDL_RenderFillRect(renderer, &frect(dst)); - drawText(renderer, dst.x + 4, dst.y + 4, 1, "missing image", .{ .r = 255, .g = 210, .b = 240 }, .mono); + drawText(renderer, dst.x + 4, dst.y + 4, 1, "missing image", .{ .r = 255, .g = 210, .b = 240 }, .mono, 1); } fn setColor(renderer: *c.SDL_Renderer, color: abi.Color) void { diff --git a/src/sketch/root.zig b/src/sketch/root.zig index 01aa977..81ebad5 100644 --- a/src/sketch/root.zig +++ b/src/sketch/root.zig @@ -152,6 +152,14 @@ pub const Frame = struct { 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 } }); } + /// `textIn` laid out and rasterized at `raster` and drawn at `scale`, + /// its position not snapped to a pixel: for text under a moving + /// camera. Rasterize at the scale the move ends on (or the larger of + /// its two ends): the letters keep their places against each other + /// all the way, and one texture serves every frame of the move. + pub fn textScaled(f: *Frame, face: Face, x: f32, y: f32, scale: f32, raster: 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, .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) } }); } |
