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 /vendor/text.c | |
| 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)
Diffstat (limited to 'vendor/text.c')
| -rw-r--r-- | vendor/text.c | 113 |
1 files changed, 78 insertions, 35 deletions
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) { |
