diff options
| author | Gabriel Schneider <[email protected]> | 2026-11-17 04:02:44 -0300 |
|---|---|---|
| committer | Gabriel Schneider <[email protected]> | 2026-11-17 12:15:01 -0300 |
| commit | 1f9fa4fc66c68e66e7653498f04d17052833d6c3 (patch) | |
| tree | 177d92af30f09207a2c9164a9b425c7178d5845d /src/host/gfx.zig | |
| parent | 1229dae433b0036547b1e28e76af645b1b57d7a0 (diff) | |
| download | sketchbook-1f9fa4fc66c68e66e7653498f04d17052833d6c3.tar.gz sketchbook-1f9fa4fc66c68e66e7653498f04d17052833d6c3.zip | |
text: FreeType and HarfBuzz in the build, Pango, Cairo and glib out, glyphs from one atlas
build.zig.zon gains the freetype and harfbuzz packages pardes pins (lazy:
a gpu-only embedder brings its own), and build.zig compiles them, with the
flags pardes's window uses, together with pardes's vendor/font.c
(unchanged) and a new vendor/text.c into one static library,
sketchbook-text, which the host links. vendor/text.c replaces the Pango
shim: the ten bundled faces by #embed, opened on first use by FreeType
(font.c) and by HarfBuzz's own OpenType functions over the same bytes;
sk_text_shape shapes a run left to right at a pixel size, each advance
rounded to a whole pixel, with the line box font.c's cell metrics give;
a codepoint the face lacks comes from one fallback face (ponytail: no
fontconfig, no bidi); sk_text_font takes a sketch's own font from bytes.
src/host/Glyphs.zig is the renderer: a text command is shaped at its
canvas size, each glyph rastered by font.c's ui_font_raster at its device
size and kept, trimmed to its ink (Atlas.inkBounds), in shelves of one
2048x2048 atlas, white premultiplied by coverage, cleared and refilled
when full. Consecutive text commands are one SDL_RenderGeometry call;
upright text lands on whole device pixels, NEAREST; textScaled (and a
glyph past 512 device pixels) is unsnapped, LINEAR. The SDL debug font
fallback off Linux goes: the faces are built in everywhere.
Host.textSize measures with the same call. test/fonts.c (a Pango
specimen outside the build) goes.
Measured: a cold Debug build of the host 39.8 s before, 43.7 s after;
the Debug host 117.7 MB (35.6 MB stripped) before, 159.5 MB (46.8 MB)
after, with 6 shared libraries where it loaded 41 (pango, cairo, glib,
fontconfig, the system freetype and harfbuzz and theirs). A frame of
4500 glyphs, half changing every frame (ReleaseFast, headless): 27.5 M
instructions and 2.80 ms of CPU before, 5.33 M and 0.40 ms after; all
static: 6.44 M and 0.47 ms before, 5.28 M and 0.42 ms after.
zig build test: 192/192 steps, 146/146 tests; test/text.script's pinned
pixels hold.
Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_016Y9MNBECMWcJD5pdS4SR6T
Diffstat (limited to 'src/host/gfx.zig')
| -rw-r--r-- | src/host/gfx.zig | 75 |
1 files changed, 24 insertions, 51 deletions
diff --git a/src/host/gfx.zig b/src/host/gfx.zig index 94e154f..f84568a 100644 --- a/src/host/gfx.zig +++ b/src/host/gfx.zig @@ -2,6 +2,7 @@ const std = @import("std"); const c = @import("c"); const abi = @import("abi"); +const Glyphs = @import("Glyphs.zig"); pub const Images = struct { gpa: std.mem.Allocator, @@ -172,6 +173,7 @@ pub const Painter = struct { backdrop_state: ?*c.SDL_GPURenderState = null, backdrop_shader: ?*c.SDL_GPUShader = null, backdrop_hash: u64 = 0, + glyphs: Glyphs, const Layer = struct { big: ?*c.SDL_Texture = null, @@ -199,6 +201,7 @@ pub const Painter = struct { } pub fn deinit(p: *Painter) void { + p.glyphs.deinit(); if (p.backdrop_texture) |t| c.SDL_DestroyTexture(t); if (p.backdrop_state) |state| c.SDL_DestroyGPURenderState(state); if (p.backdrop_shader) |shader| if (c.SDL_GetGPURendererDevice(p.renderer)) |device| c.SDL_ReleaseGPUShader(device, shader); @@ -220,6 +223,7 @@ pub const Painter = struct { /// Back to the canvas, blending alpha, unclipped: where overlays draw /// and screenshots read. pub fn end(p: *Painter) void { + p.glyphs.flush(p.renderer); p.flush(); if (p.target != .none) { p.target = .none; @@ -231,11 +235,11 @@ pub const Painter = struct { /// Draws one graphics command. Other commands are not graphics: ignored. pub fn draw(p: *Painter, command: abi.Command) void { + if (command != .text) p.glyphs.flush(p.renderer); if (command != .shape) p.flush(); const renderer = p.renderer; switch (command) { .clear => |color| { - if (@import("builtin").os.tag == .linux and p.target == .none) c.sk_text_begin_frame(renderer); // A target holds premultiplied colour, as a canvas cleared // to a translucent colour does. setColor(renderer, if (p.target == .none) color else premultiply(color)); @@ -249,10 +253,7 @@ pub const Painter = struct { .shape => |s| p.shape(s.color, s.points, s.ends, s.indices, s.colors), .image => |img| p.drawImage(img.image, img.dst, img.src, img.tint, img.corners), .backdrop => |b| p.backdrop(b.dst, b.fragment, b.uniforms), - .text => |text| { - _ = c.SDL_SetRenderDrawBlendMode(renderer, c.SDL_BLENDMODE_BLEND); - drawText(renderer, text.x, text.y, text.scale, text.bytes, text.color, text.face, if (text.raster > 0) text.raster else text.scale); - }, + .text => |text| p.glyphs.draw(renderer, text), .clip => |clip| { if (clip) |r| { const rect: c.SDL_Rect = .{ .x = @intFromFloat(r.x), .y = @intFromFloat(r.y), .w = @intFromFloat(r.w), .h = @intFromFloat(r.h) }; @@ -410,11 +411,19 @@ pub const Painter = struct { _ = c.SDL_SetGPURenderState(p.renderer, null); } + fn missing(p: *Painter, dst: abi.Rect) void { + _ = c.SDL_SetRenderDrawBlendMode(p.renderer, c.SDL_BLENDMODE_BLEND); + setColor(p.renderer, .{ .r = 110, .g = 30, .b = 70 }); + _ = c.SDL_RenderFillRect(p.renderer, &frect(dst)); + p.glyphs.draw(p.renderer, .{ .x = dst.x + 4, .y = dst.y + 4, .scale = 1, .bytes = "missing image", .color = .{ .r = 255, .g = 210, .b = 240 } }); + p.glyphs.flush(p.renderer); + } + fn drawImage(p: *Painter, handle: abi.Image, dst: abi.Rect, src_rect: abi.Rect, tint: abi.Color, corners: ?abi.Corners) void { const renderer = p.renderer; const entry = p.images.get(handle); const texture = if (entry) |e| e.texture else null; - const t = texture orelse return missing(renderer, dst); + const t = texture orelse return p.missing(dst); const e = entry.?; if (handle == p.target) return; // Drawing into itself: nothing. if (e.stale) p.resolve(e); @@ -604,45 +613,15 @@ 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, raster: f32) void { - if (@import("builtin").os.tag == .linux and c.sk_text_draw_scaled(renderer, @backingInt(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]); - buf[len] = 0; - setColor(renderer, color); - var sx: f32 = 1; - var sy: f32 = 1; - _ = c.SDL_GetRenderScale(renderer, &sx, &sy); - const s = @max(scale, 1); - // The clip rect scales with the renderer: shrink it to match, to the - // whole pixels around it (so it may let up to `s - 1` pixels more through). - var clip: c.SDL_Rect = undefined; - const clipped = c.SDL_RenderClipEnabled(renderer) and c.SDL_GetRenderClipRect(renderer, &clip); - _ = c.SDL_SetRenderScale(renderer, sx * s, sy * s); - if (clipped and s != 1) { - const x0 = @floor(@as(f32, @floatFromInt(clip.x)) / s); - const y0 = @floor(@as(f32, @floatFromInt(clip.y)) / s); - const x1 = @ceil(@as(f32, @floatFromInt(clip.x + clip.w)) / s); - const y1 = @ceil(@as(f32, @floatFromInt(clip.y + clip.h)) / s); - const scaled: c.SDL_Rect = .{ .x = @intFromFloat(x0), .y = @intFromFloat(y0), .w = @intFromFloat(x1 - x0), .h = @intFromFloat(y1 - y0) }; - _ = c.SDL_SetRenderClipRect(renderer, &scaled); - } - _ = c.SDL_RenderDebugText(renderer, x / s, y / s, &buf); - _ = c.SDL_SetRenderScale(renderer, sx, sy); - if (clipped and s != 1) _ = c.SDL_SetRenderClipRect(renderer, &clip); -} - -/// What `drawText` gives `bytes` at `scale`: its width and the height of -/// its line, in canvas units (`Host.textSize`). -pub fn textSize(renderer: *c.SDL_Renderer, scale: f32, bytes: []const u8, face: abi.Face) [2]f32 { - var size: [2]f32 = .{ 0, 0 }; - if (@import("builtin").os.tag == .linux and c.sk_text_measure_face(renderer, @backingInt(face), scale, bytes.ptr, bytes.len, &size[0], &size[1])) return size; - // The debug font's 8 px cells. - const s = @max(scale, 1); - return .{ @as(f32, @floatFromInt(bytes.len)) * 8 * s, 8 * s }; +/// 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; + 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) }; } fn premultiply(color: abi.Color) abi.Color { @@ -650,12 +629,6 @@ fn premultiply(color: abi.Color) abi.Color { return .{ .r = @intCast((color.r * a + 127) / 255), .g = @intCast((color.g * a + 127) / 255), .b = @intCast((color.b * a + 127) / 255), .a = color.a }; } -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, 1); -} fn setColor(renderer: *c.SDL_Renderer, color: abi.Color) void { _ = c.SDL_SetRenderDrawColor(renderer, color.r, color.g, color.b, color.a); |
