diff options
| author | Daniel Eklöf <daniel@ekloef.se> | 2019-07-28 19:03:38 +0000 |
|---|---|---|
| committer | Daniel Eklöf <daniel@ekloef.se> | 2019-07-28 19:03:38 +0000 |
| commit | fe882bddbab2b3c75220976ba01cc998773fdfa5 (patch) | |
| tree | af15d9b85202230687a94769c59a6b517ac112f8 /font.c | |
| parent | font: add font_glyph_for_utf8() (diff) | |
font: populate glyph cache (ASCII characters only) when instantiating font
Diffstat (limited to 'font.c')
| -rw-r--r-- | font.c | 25 |
1 files changed, 25 insertions, 0 deletions
@@ -25,6 +25,14 @@ fini(void) FT_Done_FreeType(ft_lib); } +static void +font_populate_glyph_cache(struct font *font) +{ + memset(font->cache, 0, sizeof(font->cache)); + for (size_t i = 0; i < 256; i++) + font_glyph_for_utf8(font, &(char){i}, &font->cache[i]); +} + bool font_from_name(const char *name, struct font *font) { @@ -94,6 +102,7 @@ font_from_name(const char *name, struct font *font) FcPatternDestroy(final_pattern); font->face = ft_face; + font_populate_glyph_cache(font); return true; } @@ -114,6 +123,8 @@ font_glyph_for_utf8(const struct font *font, const char *utf8, if (err != 0) return false; + assert(font->face->glyph->format == FT_GLYPH_FORMAT_BITMAP); + FT_Bitmap *bitmap = &font->face->glyph->bitmap; assert(bitmap->pixel_mode == FT_PIXEL_MODE_GRAY || bitmap->pixel_mode == FT_PIXEL_MODE_MONO); @@ -172,3 +183,17 @@ font_glyph_for_utf8(const struct font *font, const char *utf8, }; return true; } + +void +font_destroy(struct font *font) +{ + if (font->face != NULL) + FT_Done_Face(font->face); + + for (size_t i = 0; i < 256; i++) { + if (font->cache[i].surf != NULL) + cairo_surface_destroy(font->cache[i].surf); + if (font->cache[i].data != NULL) + free(font->cache[i].data); + } +} |
