summaryrefslogtreecommitdiff
path: root/vendor/text.h
diff options
context:
space:
mode:
authorGabriel Schneider <[email protected]>2026-11-17 04:02:44 -0300
committerGabriel Schneider <[email protected]>2026-11-17 12:15:01 -0300
commit1f9fa4fc66c68e66e7653498f04d17052833d6c3 (patch)
tree177d92af30f09207a2c9164a9b425c7178d5845d /vendor/text.h
parent1229dae433b0036547b1e28e76af645b1b57d7a0 (diff)
downloadsketchbook-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 'vendor/text.h')
-rw-r--r--vendor/text.h61
1 files changed, 40 insertions, 21 deletions
diff --git a/vendor/text.h b/vendor/text.h
index aa49db8..dd4eecf 100644
--- a/vendor/text.h
+++ b/vendor/text.h
@@ -1,26 +1,45 @@
+/* sketchbook's text: the faces (`abi.Face`'s ten, bundled, and a sketch's
+ own from bytes), shaped by HarfBuzz and rasterized by pardes's font.c.
+ One static library, `sketchbook-text`, linked by the host and by a
+ sketch's tests, so both measure with the same code. */
#ifndef SKETCHBOOK_TEXT_H
#define SKETCHBOOK_TEXT_H
-#include <SDL3/SDL.h>
+#include "font.h"
#include <stddef.h>
-/* x/y are the logical line-box top. Scale 1 is 13 px Go Mono (roughly
- eight-pixel ASCII advance and a 16-pixel line box); fallback uses Fontconfig. */
-bool sk_text_draw(SDL_Renderer *, float x, float y, float scale,
- const char *utf8, size_t len, Uint8 r, Uint8 g, Uint8 b, Uint8 a);
-/* What sk_text_draw gives the text at `scale`: its advance width and the
- 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). */
+#include <stdint.h>
+
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_CODE, SK_TEXT_CODE_BOLD, SK_TEXT_CODE_ITALIC, SK_TEXT_CODE_MEDIUM, 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);
-/* 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 *);
+#define SK_TEXT_MAX_FACES 64
+
+/* One glyph of a shaped run: `face` draws it (the run's, or its fallback),
+ `cluster` is the byte it starts at, `x`/`y` its pen position in whole
+ pixels from the run's start (y down). */
+typedef struct {
+ uint32_t glyph;
+ uint32_t cluster;
+ int32_t x;
+ int32_t y;
+ uint8_t face;
+} SkGlyph;
+
+/* `utf8` in `face` at `px` pixels, left to right: at most `max` glyphs into
+ `out` (NULL to measure only). Returns how many there are (more than `max`
+ when they did not fit), with the run's advance, its ascent and its line's
+ height, all in whole pixels: each glyph's advance is rounded, and the
+ line box is FreeType's at that size (font.h's cell metrics). Invalid
+ UTF-8 shapes as U+FFFD; an unknown face as the mono. */
+int sk_text_shape(int face, float px, const char *utf8, size_t len, SkGlyph *out, int max,
+ int32_t *advance, int32_t *ascent, int32_t *height);
+
+/* `glyph`'s own advance in `face` at `px`, in whole pixels (what the
+ renderer centres font.c's raster in, so the glyph lands on its origin). */
+int32_t sk_text_advance(int face, float px, uint32_t glyph);
+
+/* A face for a sketch's own font, its bytes copied: the same id for the
+ same bytes. -1 when FreeType refuses them or every face is taken. */
+int sk_text_font(const uint8_t *bytes, size_t len);
+
+/* What rasterizes `face` (font.h's ui_font_raster), NULL if none. */
+UIFont *sk_text_ui(int face);
+
#endif