diff options
| author | Gabriel Schneider <[email protected]> | 2026-11-10 12:10:40 -0300 |
|---|---|---|
| committer | Gabriel Schneider <[email protected]> | 2026-11-12 13:22:02 -0300 |
| commit | faf40df6f31e98eb752fd5e96330221c8f7dbcc7 (patch) | |
| tree | ab9356732afbbbb18e85f973945249a738863ae7 | |
| parent | 71e534631bf68f33f802d64794340870374b9f69 (diff) | |
| download | sketchbook-faf40df6f31e98eb752fd5e96330221c8f7dbcc7.tar.gz sketchbook-faf40df6f31e98eb752fd5e96330221c8f7dbcc7.zip | |
sketchbook: text in six faces of the Go family, all bundled (f.textIn, host.textSizeIn, sk.Face)
| -rw-r--r-- | src/abi.zig | 17 | ||||
| -rw-r--r-- | src/anim/root.zig | 12 | ||||
| -rw-r--r-- | src/host/Host.zig | 4 | ||||
| -rw-r--r-- | src/host/gfx.zig | 12 | ||||
| -rw-r--r-- | src/sketch/root.zig | 6 | ||||
| -rw-r--r-- | src/sketch/web.zig | 2 | ||||
| -rw-r--r-- | test/text.script | 6 | ||||
| -rw-r--r-- | test/text.zig | 9 | ||||
| -rw-r--r-- | vendor/fonts/Go-Bold.ttf | bin | 0 -> 151748 bytes | |||
| -rw-r--r-- | vendor/fonts/Go-Medium.ttf | bin | 0 -> 157176 bytes | |||
| -rw-r--r-- | vendor/fonts/Go-Mono-Bold.ttf | bin | 0 -> 178952 bytes | |||
| -rw-r--r-- | vendor/fonts/Go-Mono-Italic.ttf | bin | 0 -> 183024 bytes | |||
| -rw-r--r-- | vendor/fonts/Go-Regular.ttf | bin | 0 -> 148672 bytes | |||
| -rw-r--r-- | vendor/fonts/README.md | 7 | ||||
| -rw-r--r-- | vendor/text.c | 113 | ||||
| -rw-r--r-- | vendor/text.h | 6 |
16 files changed, 143 insertions, 51 deletions
diff --git a/src/abi.zig b/src/abi.zig index b30b7ae..327110d 100644 --- a/src/abi.zig +++ b/src/abi.zig @@ -57,6 +57,11 @@ pub const Color = extern struct { } }; +/// The face a text is set in: the Go family, bundled with the native host. +/// Mono is for data and code, 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 }; + /// How 2D drawing combines with what is already there (`Command.blend`). /// Both are fixed-function on both hosts and match: `alpha` is source /// over (canvas `source-over`, SDL `BLEND`), `add` adds the colour times @@ -341,8 +346,8 @@ 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, - /// Fixed 8px cells, `scale` times larger. - text: struct { x: f32, y: f32, scale: f32, color: Color, bytes: []const u8 }, + /// 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 }, clip: ?Rect, /// Logical canvas size; the host scales the window to fit. canvas: struct { w: u32, h: u32 }, @@ -583,7 +588,7 @@ pub const Host = struct { 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) [2]f32, + textSize: *const fn (*anyopaque, f32, []const u8, Face) [2]f32, /// `.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 @@ -637,7 +642,11 @@ pub const Host = struct { /// it is 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); + return h.vtable.textSize(h.ctx, scale, bytes, .mono); + } + /// `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); } pub fn loadSound(h: Host, path: []const u8) Audio { return h.vtable.loadSound(h.ctx, path); diff --git a/src/anim/root.zig b/src/anim/root.zig index 00e9362..a676f38 100644 --- a/src/anim/root.zig +++ b/src/anim/root.zig @@ -186,6 +186,18 @@ pub const Placed = struct { f.text(at[0], at[1], scale * p.scale, bytes, p.faded(ink)); } + /// `text` in another face (`sk.Face`: bold or italic mono, the sans). + pub fn textIn(p: Placed, f: *sk.Frame, face: sk.Face, x: f32, y: f32, scale: f32, bytes: []const u8, ink: sk.Color) void { + const at = p.xf.apply(.{ x, y }); + f.textIn(face, at[0], at[1], scale * p.scale, bytes, p.faded(ink)); + } + + /// The width and line height `textIn` covers, in the object's units. + pub fn textSizeIn(p: Placed, f: *sk.Frame, face: sk.Face, scale: f32, bytes: []const u8) [2]f32 { + const size = f.host.textSizeIn(face, scale * p.scale, bytes); + return .{ size[0] / p.scale, size[1] / p.scale }; + } + /// The width and line height `text` covers, in the object's units. pub fn textSize(p: Placed, f: *sk.Frame, scale: f32, bytes: []const u8) [2]f32 { const size = f.host.textSize(scale * p.scale, bytes); diff --git a/src/host/Host.zig b/src/host/Host.zig index 4b5f2a4..86e1873 100644 --- a/src/host/Host.zig +++ b/src/host/Host.zig @@ -834,8 +834,8 @@ fn apiSamples(ctx: *anyopaque, audio: abi.Audio) []const f32 { fn apiImageSize(ctx: *anyopaque, image: abi.Image) [2]u32 { return hostOf(ctx).images.size(image); } -fn apiTextSize(ctx: *anyopaque, scale: f32, bytes: []const u8) [2]f32 { - return gfx.textSize(hostOf(ctx).renderer, scale, bytes); +fn apiTextSize(ctx: *anyopaque, scale: f32, bytes: []const u8, face: abi.Face) [2]f32 { + return gfx.textSize(hostOf(ctx).renderer, scale, bytes, face); } fn apiLoadSound(ctx: *anyopaque, path: []const u8) abi.Audio { return hostOf(ctx).audio.load(path); diff --git a/src/host/gfx.zig b/src/host/gfx.zig index 0466f27..2aac380 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); + drawText(renderer, text.x, text.y, text.scale, text.bytes, text.color, text.face); }, .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) void { - if (@import("builtin").os.tag == .linux and c.sk_text_draw(renderer, 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) 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; 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]); @@ -558,9 +558,9 @@ pub fn drawText(renderer: *c.SDL_Renderer, x: f32, y: f32, scale: f32, bytes: [] /// 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) [2]f32 { +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(renderer, scale, bytes.ptr, bytes.len, &size[0], &size[1])) return size; + if (@import("builtin").os.tag == .linux and c.sk_text_measure_face(renderer, @intFromEnum(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 }; @@ -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 }); + drawText(renderer, dst.x + 4, dst.y + 4, 1, "missing image", .{ .r = 255, .g = 210, .b = 240 }, .mono); } fn setColor(renderer: *c.SDL_Renderer, color: abi.Color) void { diff --git a/src/sketch/root.zig b/src/sketch/root.zig index 33c9ba7..01aa977 100644 --- a/src/sketch/root.zig +++ b/src/sketch/root.zig @@ -57,6 +57,7 @@ pub const Buffer = abi.Buffer; pub const Cursor = abi.Cursor; pub const Voice = abi.Voice; pub const BlendMode = abi.BlendMode; +pub const Face = abi.Face; /// What `draw` receives: input, host services, memory by lifetime and the /// command list. Commands may be built from anything: the methods copy what @@ -146,6 +147,11 @@ pub const Frame = struct { pub fn text(f: *Frame, 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) } }); } + /// `text` in another face of the built-in family: bold or italic mono, + /// or the sans (`host.textSizeIn` measures it). + 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 } }); + } 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/src/sketch/web.zig b/src/sketch/web.zig index 4445e8d..db9d9a8 100644 --- a/src/sketch/web.zig +++ b/src/sketch/web.zig @@ -431,7 +431,7 @@ fn Driver(comptime App: type) type { 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) [2]f32 { + 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 }; } diff --git a/test/text.script b/test/text.script index 5bea6f5..4381f94 100644 --- a/test/text.script +++ b/test/text.script @@ -5,3 +5,9 @@ expect-pixel .zig-cache/text/native.png 84 11 32 64 96 255 expect-pixel .zig-cache/text/native.png 84 24 32 64 96 255 expect-pixel .zig-cache/text/native.png 97 11 16 16 16 255 expect-pixel .zig-cache/text/native.png 84 27 16 16 16 255 +# Another face is another width: ten sans `i` at scale 2 advance 60 px +# where the mono (bold here, the line below) advances 160. +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 diff --git a/test/text.zig b/test/text.zig index b570435..e47e788 100644 --- a/test/text.zig +++ b/test/text.zig @@ -5,11 +5,14 @@ const sk = @import("sketch"); pub const width = 640; pub const height = 360; -const lines = [_]struct { scale: f32, text: []const u8 }{ +const lines = [_]struct { scale: f32, text: []const u8, face: sk.Face = .mono }{ .{ .scale = 1, .text = "0123456789" }, .{ .scale = 1.5, .text = "mov rax, [rbp-0x18] ; 1.5" }, .{ .scale = 2.25, .text = "0x7ffed849acf0 i -> W" }, .{ .scale = 4, .text = "fn main() !void {}" }, + // 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 }, }; pub fn draw(f: *sk.Frame) void { @@ -18,9 +21,9 @@ 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.textSize(line.scale * k, line.text); + const size = f.host.textSizeIn(line.face, line.scale * k, line.text); f.fill(sk.Rect{ .x = 10 * k, .y = y, .w = size[0], .h = size[1] }, 0x204060); - f.text(10 * k, y, line.scale * k, line.text, 0xffffff); + f.textIn(line.face, 10 * k, y, line.scale * k, line.text, 0xffffff); y += size[1] + 6 * k; } } diff --git a/vendor/fonts/Go-Bold.ttf b/vendor/fonts/Go-Bold.ttf Binary files differnew file mode 100644 index 0000000..3ecc461 --- /dev/null +++ b/vendor/fonts/Go-Bold.ttf diff --git a/vendor/fonts/Go-Medium.ttf b/vendor/fonts/Go-Medium.ttf Binary files differnew file mode 100644 index 0000000..56cf4b7 --- /dev/null +++ b/vendor/fonts/Go-Medium.ttf diff --git a/vendor/fonts/Go-Mono-Bold.ttf b/vendor/fonts/Go-Mono-Bold.ttf Binary files differnew file mode 100644 index 0000000..d0bd544 --- /dev/null +++ b/vendor/fonts/Go-Mono-Bold.ttf diff --git a/vendor/fonts/Go-Mono-Italic.ttf b/vendor/fonts/Go-Mono-Italic.ttf Binary files differnew file mode 100644 index 0000000..0c636b6 --- /dev/null +++ b/vendor/fonts/Go-Mono-Italic.ttf diff --git a/vendor/fonts/Go-Regular.ttf b/vendor/fonts/Go-Regular.ttf Binary files differnew file mode 100644 index 0000000..3845655 --- /dev/null +++ b/vendor/fonts/Go-Regular.ttf diff --git a/vendor/fonts/README.md b/vendor/fonts/README.md index d1bf9d9..3c49784 100644 --- a/vendor/fonts/README.md +++ b/vendor/fonts/README.md @@ -10,6 +10,13 @@ https://github.com/golang/image/tree/master/font/gofont/ttfs SHA-256: `8bc66a0154bbf69cd24e5bde41a12ec9495c8a242c5def2255e4a164900f1ed7`. The complete Bigelow & Holmes / Go font license is in `LICENSE`. +Five more faces of the same family are bundled the same way, for +`f.textIn(face, ...)` (`abi.Face`, in this order after Go Mono): +`Go-Mono-Bold.ttf`, `Go-Mono-Italic.ttf`, `Go-Regular.ttf`, `Go-Medium.ttf`, +`Go-Bold.ttf`, as the `ttf-go` package installs them (the same upstream). +Each is a font description of its own (`sk_text_draw_face`); the sans +faces fall back to installed sans families instead of mono ones. + The font is embedded in the binary with C23 `#embed`. It is registered in a private Fontconfig config through a Linux memfd, so moving the executable does not require a font installation or the source checkout. Installed outline fonts diff --git a/vendor/text.c b/vendor/text.c index 172b61e..f8c04b3 100644 --- a/vendor/text.c +++ b/vendor/text.c @@ -17,25 +17,49 @@ #define CACHE_ENTRIES 512 #define CACHE_BYTES (16 * 1024 * 1024) #define CACHE_PROPERTY "sketchbook.native-text" +/* The Go family, bundled: one file a face, in `SkTextFace` order. */ static const unsigned char go_mono[] = { #embed "fonts/Go-Mono.ttf" }; +static const unsigned char go_mono_bold[] = { +#embed "fonts/Go-Mono-Bold.ttf" +}; +static const unsigned char go_mono_italic[] = { +#embed "fonts/Go-Mono-Italic.ttf" +}; +static const unsigned char go_regular[] = { +#embed "fonts/Go-Regular.ttf" +}; +static const unsigned char go_medium[] = { +#embed "fonts/Go-Medium.ttf" +}; +static const unsigned char go_bold[] = { +#embed "fonts/Go-Bold.ttf" +}; +static const struct { const unsigned char *bytes; size_t len; const char *family, *style; } faces[SK_TEXT_FACES] = { + {go_mono, sizeof(go_mono), "Go Mono", ""}, + {go_mono_bold, sizeof(go_mono_bold), "Go Mono", " Bold"}, + {go_mono_italic, sizeof(go_mono_italic), "Go Mono", " Italic"}, + {go_regular, sizeof(go_regular), "Go", ""}, + {go_medium, sizeof(go_medium), "Go Medium", ""}, + {go_bold, sizeof(go_bold), "Go", " Bold"}, +}; typedef struct { SDL_Texture *texture; char *text; size_t len, bytes; uint64_t hash, used; - int size, width, height, left, top; + int size, width, height, left, top, face; Uint32 color; } Text; typedef struct { PangoFontMap *map; PangoContext *context; PangoLayout *layout; - PangoFontDescription *font; + PangoFontDescription *font[SK_TEXT_FACES]; FcConfig *config; - int font_fd; + int font_fd[SK_TEXT_FACES]; Text entries[CACHE_ENTRIES]; size_t bytes; uint64_t frame; @@ -51,12 +75,12 @@ static void SDLCALL release_cache(void *userdata, void *value) { TextCache *c = value; /* SDL destroys its owned textures before renderer property cleanup. */ for (size_t i = 0; i < CACHE_ENTRIES; ++i) free(c->entries[i].text); - if (c->font) pango_font_description_free(c->font); + for (int i = 0; i < SK_TEXT_FACES; ++i) if (c->font[i]) pango_font_description_free(c->font[i]); if (c->layout) g_object_unref(c->layout); if (c->context) g_object_unref(c->context); if (c->map) g_object_unref(c->map); if (c->config) FcConfigDestroy(c->config); - if (c->font_fd >= 0) close(c->font_fd); + for (int i = 0; i < SK_TEXT_FACES; ++i) if (c->font_fd[i] >= 0) close(c->font_fd[i]); free(c); } static TextCache *cache_for(SDL_Renderer *renderer) { @@ -65,32 +89,41 @@ static TextCache *cache_for(SDL_Renderer *renderer) { if (c) return c; c = calloc(1, sizeof(*c)); if (!c) return NULL; - c->font_fd = memfd_create("sketchbook-go-mono", MFD_CLOEXEC); - if (c->font_fd < 0) { free(c); return NULL; } - size_t offset = 0; - while (offset < sizeof(go_mono)) { - ssize_t n = write(c->font_fd, go_mono + offset, sizeof(go_mono) - offset); - if (n < 0 && errno == EINTR) continue; - if (n <= 0) { release_cache(NULL, c); return NULL; } - offset += (size_t)n; - } - char path[64]; - snprintf(path, sizeof(path), "/proc/self/fd/%d", c->font_fd); + for (int i = 0; i < SK_TEXT_FACES; ++i) c->font_fd[i] = -1; c->config = FcInitLoadConfigAndFonts(); - if (!c->config || !FcConfigAppFontAddFile(c->config, (const FcChar8 *)path)) { - release_cache(NULL, c); - return NULL; + if (!c->config) { release_cache(NULL, c); return NULL; } + for (int i = 0; i < SK_TEXT_FACES; ++i) { + c->font_fd[i] = memfd_create("sketchbook-go", MFD_CLOEXEC); + if (c->font_fd[i] < 0) { release_cache(NULL, c); return NULL; } + size_t offset = 0; + while (offset < faces[i].len) { + ssize_t n = write(c->font_fd[i], faces[i].bytes + offset, faces[i].len - offset); + if (n < 0 && errno == EINTR) continue; + if (n <= 0) { release_cache(NULL, c); return NULL; } + offset += (size_t)n; + } + char path[64]; + snprintf(path, sizeof(path), "/proc/self/fd/%d", c->font_fd[i]); + if (!FcConfigAppFontAddFile(c->config, (const FcChar8 *)path)) { + release_cache(NULL, c); + return NULL; + } } c->map = pango_cairo_font_map_new_for_font_type(CAIRO_FONT_TYPE_FT); pango_fc_font_map_set_config(PANGO_FC_FONT_MAP(c->map), c->config); c->context = pango_font_map_create_context(c->map); c->layout = pango_layout_new(c->context); - /* SKETCHBOOK_FONT names an installed family to use before the built-in one. */ + /* SKETCHBOOK_FONT names an installed family to use before the built-in + mono one. The sans faces fall back the same way after their own. */ const char *first = getenv("SKETCHBOOK_FONT"); - char families[512]; - snprintf(families, sizeof(families), "%s%sGo Mono, Noto Sans Mono, DejaVu Sans Mono, Noto Sans Arabic, Noto Sans Hebrew, IBM Plex Sans JP, Noto Color Emoji, Noto Sans Symbols 2, DejaVu Sans, monospace", - first ? first : "", first ? ", " : ""); - c->font = pango_font_description_from_string(families); + for (int i = 0; i < SK_TEXT_FACES; ++i) { + const bool mono = i <= SK_TEXT_MONO_ITALIC; + char families[640]; + snprintf(families, sizeof(families), "%s%s%s, %sNoto Sans Arabic, Noto Sans Hebrew, IBM Plex Sans JP, Noto Color Emoji, Noto Sans Symbols 2, DejaVu Sans, %s%s", + mono && first ? first : "", mono && first ? ", " : "", faces[i].family, + mono ? "Noto Sans Mono, DejaVu Sans Mono, " : "Go, Noto Sans, ", mono ? "monospace" : "sans-serif", faces[i].style); + c->font[i] = pango_font_description_from_string(families); + } cairo_font_options_t *options = cairo_font_options_create(); cairo_font_options_set_antialias(options, CAIRO_ANTIALIAS_GRAY); cairo_font_options_set_hint_style(options, CAIRO_HINT_STYLE_SLIGHT); @@ -141,19 +174,19 @@ static int font_size(float scale, float dpi) { return (int)lroundf(fminf(512, 13.0f * scale * dpi) * 64); } /* Lays `bytes` out at `size`: its extents in physical pixels (`ink` may be NULL). */ -static bool lay_out(TextCache *c, const char *bytes, size_t len, int size, +static bool lay_out(TextCache *c, const char *bytes, size_t len, int size, int face, PangoRectangle *ink, PangoRectangle *logical) { if (len > INT_MAX || !g_utf8_validate(bytes, (gssize)len, NULL)) return false; - pango_font_description_set_absolute_size(c->font, (double)size * PANGO_SCALE / 64.0); - pango_layout_set_font_description(c->layout, c->font); + pango_font_description_set_absolute_size(c->font[face], (double)size * PANGO_SCALE / 64.0); + pango_layout_set_font_description(c->layout, c->font[face]); pango_layout_set_text(c->layout, bytes, (int)len); pango_layout_get_pixel_extents(c->layout, ink, logical); return true; } static bool rasterize(TextCache *c, SDL_Renderer *renderer, Text *out, - const char *bytes, size_t len, int size, Uint32 color) { + const char *bytes, size_t len, int size, int face, Uint32 color) { PangoRectangle ink, logical; - if (!lay_out(c, bytes, len, size, &ink, &logical)) return false; + if (!lay_out(c, bytes, len, size, face, &ink, &logical)) return false; const int left = MIN(0, ink.x) - 1; const int top = MIN(0, ink.y) - 1; const int width = MAX(logical.x + logical.width, ink.x + ink.width) - left + 1; @@ -184,18 +217,23 @@ static bool rasterize(TextCache *c, SDL_Renderer *renderer, Text *out, memcpy(key, bytes, len); key[len] = 0; *out = (Text){.texture=texture,.text=key,.len=len,.bytes=(size_t)width*height*4, .hash=hash_text(bytes,len),.used=c->frame,.size=size,.width=width,.height=height, - .left=left,.top=top,.color=color}; + .left=left,.top=top,.face=face,.color=color}; return true; } bool sk_text_measure(SDL_Renderer *renderer, float scale, const char *utf8, size_t len, float *width, float *height) { - if (!isfinite(scale) || scale <= 0) return false; + return sk_text_measure_face(renderer, SK_TEXT_MONO, scale, utf8, len, width, height); +} + +bool sk_text_measure_face(SDL_Renderer *renderer, int face, float scale, const char *utf8, size_t len, + float *width, float *height) { + if (!isfinite(scale) || scale <= 0 || face < 0 || face >= SK_TEXT_FACES) return false; TextCache *c = cache_for(renderer); if (!c) return false; const float dpi = density(renderer); PangoRectangle logical; - if (!lay_out(c, utf8, len, font_size(scale, dpi), NULL, &logical)) return false; + if (!lay_out(c, utf8, len, font_size(scale, dpi), face, NULL, &logical)) return false; *width = logical.width / dpi; *height = logical.height / dpi; return true; @@ -203,8 +241,13 @@ bool sk_text_measure(SDL_Renderer *renderer, float scale, const char *utf8, size bool sk_text_draw(SDL_Renderer *renderer, float x, float y, float scale, const char *utf8, size_t len, Uint8 r, Uint8 g, Uint8 b, Uint8 a) { + return sk_text_draw_face(renderer, SK_TEXT_MONO, x, y, scale, utf8, len, r, g, b, a); +} + +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) { if (!len || !a) return true; - if (!isfinite(scale) || scale <= 0) return false; + if (!isfinite(scale) || scale <= 0 || face < 0 || face >= SK_TEXT_FACES) return false; TextCache *c = cache_for(renderer); if (!c) return false; const float dpi = density(renderer); @@ -214,12 +257,12 @@ bool sk_text_draw(SDL_Renderer *renderer, float x, float y, float scale, Text *entry = NULL; for (size_t i = 0; i < CACHE_ENTRIES; ++i) { Text *t = &c->entries[i]; - if (t->texture && t->hash == hash && t->len == len && t->size == size && + if (t->texture && t->hash == hash && t->len == len && t->size == size && t->face == face && t->color == color && !memcmp(t->text, utf8, len)) { entry = t; break; } } Text made = {0}; if (!entry) { - if (!rasterize(c, renderer, &made, utf8, len, size, color)) return false; + if (!rasterize(c, renderer, &made, utf8, len, size, face, color)) return false; /* Only evict textures unused this epoch. SDL can still have queued draws referring to current-frame entries; those stay alive through present. */ while (!entry) { diff --git a/vendor/text.h b/vendor/text.h index 9cd445a..9611cb1 100644 --- a/vendor/text.h +++ b/vendor/text.h @@ -10,5 +10,11 @@ bool sk_text_draw(SDL_Renderer *, float x, float y, float scale, height of its line box, in logical units. False if it cannot be laid out. */ bool sk_text_measure(SDL_Renderer *, float scale, const char *utf8, size_t len, float *width, float *height); +/* The same in another face of the bundled Go family (`abi.Face`'s order). */ +enum { SK_TEXT_MONO, SK_TEXT_MONO_BOLD, SK_TEXT_MONO_ITALIC, SK_TEXT_SANS, SK_TEXT_SANS_MEDIUM, SK_TEXT_SANS_BOLD, SK_TEXT_FACES }; +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); void sk_text_begin_frame(SDL_Renderer *); #endif |
