diff options
| -rw-r--r-- | README.md | 8 | ||||
| -rw-r--r-- | src/abi.zig | 7 | ||||
| -rw-r--r-- | src/host/gfx.zig | 8 | ||||
| -rw-r--r-- | src/sketch/root.zig | 8 | ||||
| -rw-r--r-- | test/text.script | 4 | ||||
| -rw-r--r-- | test/text.zig | 13 | ||||
| -rw-r--r-- | vendor/text.c | 13 | ||||
| -rw-r--r-- | vendor/text.h | 6 |
8 files changed, 56 insertions, 11 deletions
@@ -118,6 +118,14 @@ bytes)` measures it. The three mono faces advance alike (0.6 em: whole pixels at 25, 30, 35, 45 and 60 px and at their doubles), so columns of text need no measuring. The web host draws every face as its one. +Text is shaped for the size it is drawn at, so under a camera that zooms +its letters would shift against each other from frame to frame. +`f.textScaled(face, x, y, scale, raster, bytes, color)` lays the line out +and rasterizes it at `raster` and draws that at `scale`, unsnapped: give it +the scale the move ends on (the larger of its ends) while the camera +moves, and `textIn` again once it rests. One texture serves the whole +move. The web host ignores `raster`. + `f.openFile("open", .{ .pattern = "wav;flac" })` shows the system's file dialog; the answer comes back as the console line `open <path>`, so it reaches the sketch's `command` like anything typed there. `f.run(line)` 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) } }); } diff --git a/test/text.script b/test/text.script index 4381f94..c27df2a 100644 --- a/test/text.script +++ b/test/text.script @@ -11,3 +11,7 @@ expect-pixel .zig-cache/text/native.png 66 198 32 64 96 255 expect-pixel .zig-cache/text/native.png 80 185 16 16 16 255 expect-pixel .zig-cache/text/native.png 168 236 32 64 96 255 expect-pixel .zig-cache/text/native.png 180 222 16 16 16 255 +# Rasterized at scale 3 and drawn at 1.5, ten digits cover 115 px (half +# of scale 3's 230), not the 120 that scale 1.5 would shape. +expect-pixel .zig-cache/text/native.png 123 265 32 64 96 255 +expect-pixel .zig-cache/text/native.png 127 256 16 16 16 255 diff --git a/test/text.zig b/test/text.zig index e47e788..74bbaed 100644 --- a/test/text.zig +++ b/test/text.zig @@ -5,7 +5,7 @@ const sk = @import("sketch"); pub const width = 640; pub const height = 360; -const lines = [_]struct { scale: f32, text: []const u8, face: sk.Face = .mono }{ +const lines = [_]struct { scale: f32, text: []const u8, face: sk.Face = .mono, raster: f32 = 0 }{ .{ .scale = 1, .text = "0123456789" }, .{ .scale = 1.5, .text = "mov rax, [rbp-0x18] ; 1.5" }, .{ .scale = 2.25, .text = "0x7ffed849acf0 i -> W" }, @@ -13,6 +13,9 @@ const lines = [_]struct { scale: f32, text: []const u8, face: sk.Face = .mono }{ // The other faces: the sans is proportional, the bold mono is not. .{ .scale = 2, .text = "iiiiiiiiii", .face = .sans }, .{ .scale = 2, .text = "iiiiiiiiii", .face = .mono_bold }, + // Rasterized at 3 and drawn at 1.5: half of what scale 3 covers (115 px + // for ten digits), not what scale 1.5 would shape (120). + .{ .scale = 1.5, .text = "0123456789", .raster = 3 }, }; pub fn draw(f: *sk.Frame) void { @@ -21,9 +24,13 @@ pub fn draw(f: *sk.Frame) void { const k = f.in.window_height / height; var y: f32 = 10 * k; for (lines) |line| { - const size = f.host.textSizeIn(line.face, line.scale * k, line.text); + var size = f.host.textSizeIn(line.face, line.scale * k, line.text); + if (line.raster > 0) { + const at = f.host.textSizeIn(line.face, line.raster * k, line.text); + size = .{ at[0] * line.scale / line.raster, at[1] * line.scale / line.raster }; + } f.fill(sk.Rect{ .x = 10 * k, .y = y, .w = size[0], .h = size[1] }, 0x204060); - f.textIn(line.face, 10 * k, y, line.scale * k, line.text, 0xffffff); + if (line.raster > 0) f.textScaled(line.face, 10 * k, y, line.scale * k, line.raster * k, line.text, 0xffffff) else f.textIn(line.face, 10 * k, y, line.scale * k, line.text, 0xffffff); y += size[1] + 6 * k; } } diff --git a/vendor/text.c b/vendor/text.c index f8c04b3..c1548d6 100644 --- a/vendor/text.c +++ b/vendor/text.c @@ -246,12 +246,19 @@ bool sk_text_draw(SDL_Renderer *renderer, float x, float y, float scale, bool sk_text_draw_face(SDL_Renderer *renderer, int face, float x, float y, float scale, const char *utf8, size_t len, Uint8 r, Uint8 g, Uint8 b, Uint8 a) { + return sk_text_draw_scaled(renderer, face, x, y, scale, scale, utf8, len, r, g, b, a); +} + +bool sk_text_draw_scaled(SDL_Renderer *renderer, int face, float x, float y, float scale, float raster, + const char *utf8, size_t len, Uint8 r, Uint8 g, Uint8 b, Uint8 a) { if (!len || !a) return true; - if (!isfinite(scale) || scale <= 0 || face < 0 || face >= SK_TEXT_FACES) return false; + if (!isfinite(scale) || scale <= 0 || !isfinite(raster) || raster <= 0 || face < 0 || face >= SK_TEXT_FACES) return false; TextCache *c = cache_for(renderer); if (!c) return false; const float dpi = density(renderer); - const int size = font_size(scale, dpi); + const int size = font_size(raster, dpi); + /* How much larger than rasterized it is drawn: 1 when it is drawn as it is. */ + const float k = scale / raster; const Uint32 color = ((Uint32)r << 16) | ((Uint32)g << 8) | b; const uint64_t hash = hash_text(utf8, len); Text *entry = NULL; @@ -288,6 +295,8 @@ bool sk_text_draw_face(SDL_Renderer *renderer, int face, float x, float y, float SDL_SetTextureAlphaMod(entry->texture, a); SDL_FRect dest = {.x=(roundf(x*dpi)+entry->left)/dpi,.y=(roundf(y*dpi)+entry->top)/dpi, .w=entry->width/dpi,.h=entry->height/dpi}; + if (k != 1) dest = (SDL_FRect){.x=x+entry->left*k/dpi,.y=y+entry->top*k/dpi, + .w=entry->width*k/dpi,.h=entry->height*k/dpi}; const bool okay = SDL_RenderTexture(renderer, entry->texture, NULL, &dest); if (entry == &made) { /* Exceptional very dense frames remain bounded instead of retaining diff --git a/vendor/text.h b/vendor/text.h index 9611cb1..a421961 100644 --- a/vendor/text.h +++ b/vendor/text.h @@ -16,5 +16,11 @@ bool sk_text_draw_face(SDL_Renderer *, int face, float x, float y, float scale, const char *utf8, size_t len, Uint8 r, Uint8 g, Uint8 b, Uint8 a); bool sk_text_measure_face(SDL_Renderer *, int face, float scale, const char *utf8, size_t len, float *width, float *height); +/* The same, laid out and rasterized at `raster` and drawn at `scale`: the + glyphs keep their places against each other while the size changes (a + camera moving), and one texture serves every size on the way. Its + position is not snapped to a pixel. */ +bool sk_text_draw_scaled(SDL_Renderer *, int face, float x, float y, float scale, float raster, + const char *utf8, size_t len, Uint8 r, Uint8 g, Uint8 b, Uint8 a); void sk_text_begin_frame(SDL_Renderer *); #endif |
