diff options
Diffstat (limited to 'vendor/text.c')
| -rw-r--r-- | vendor/text.c | 38 |
1 files changed, 33 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; |
