summaryrefslogtreecommitdiff
path: root/vendor
diff options
context:
space:
mode:
authorGabriel Schneider <[email protected]>2026-11-10 11:13:20 -0300
committerGabriel Schneider <[email protected]>2026-11-12 13:22:02 -0300
commit86ffca0024326110962f0880727f4eb84e6a48d1 (patch)
tree055f489341053c1db258606bb63b282bc7345a89 /vendor
parenta62a6aa896adda901d335ef38814a13b8839545c (diff)
downloadsketchbook-86ffca0024326110962f0880727f4eb84e6a48d1.tar.gz
sketchbook-86ffca0024326110962f0880727f4eb84e6a48d1.zip
sketchbook: frames for ffmpeg (--export, on a clock of its own), text sizes, a font from the environment
Co-Authored-By: Claude Fable 5.2 <[email protected]>
Diffstat (limited to 'vendor')
-rw-r--r--vendor/text.c38
-rw-r--r--vendor/text.h4
2 files changed, 37 insertions, 5 deletions
diff --git a/vendor/text.c b/vendor/text.c
index 9a81558..172b61e 100644
--- a/vendor/text.c
+++ b/vendor/text.c
@@ -85,7 +85,12 @@ static TextCache *cache_for(SDL_Renderer *renderer) {
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);
- c->font = pango_font_description_from_string("Go 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");
+ /* SKETCHBOOK_FONT names an installed family to use before the built-in one. */
+ 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);
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);
@@ -131,14 +136,24 @@ static uint64_t hash_text(const char *bytes, size_t len) {
for (size_t i = 0; i < len; ++i) hash = (hash ^ (unsigned char)bytes[i]) * UINT64_C(1099511628211);
return hash;
}
-static bool rasterize(TextCache *c, SDL_Renderer *renderer, Text *out,
- const char *bytes, size_t len, int size, Uint32 color) {
+/* The font size, in 64ths of a physical pixel, that `scale` asks for. */
+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,
+ 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_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) {
PangoRectangle ink, logical;
- pango_layout_get_pixel_extents(c->layout, &ink, &logical);
+ if (!lay_out(c, bytes, len, size, &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;
@@ -173,6 +188,19 @@ static bool rasterize(TextCache *c, SDL_Renderer *renderer, Text *out,
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;
+ 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;
+ *width = logical.width / dpi;
+ *height = logical.height / dpi;
+ return true;
+}
+
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) {
if (!len || !a) return true;
@@ -180,7 +208,7 @@ bool sk_text_draw(SDL_Renderer *renderer, float x, float y, float scale,
TextCache *c = cache_for(renderer);
if (!c) return false;
const float dpi = density(renderer);
- const int size = (int)lroundf(fminf(512, 13.0f * scale * dpi) * 64);
+ const int size = font_size(scale, dpi);
const Uint32 color = ((Uint32)r << 16) | ((Uint32)g << 8) | b;
const uint64_t hash = hash_text(utf8, len);
Text *entry = NULL;
diff --git a/vendor/text.h b/vendor/text.h
index bf4f363..9cd445a 100644
--- a/vendor/text.h
+++ b/vendor/text.h
@@ -6,5 +6,9 @@
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);
void sk_text_begin_frame(SDL_Renderer *);
#endif