From fe882bddbab2b3c75220976ba01cc998773fdfa5 Mon Sep 17 00:00:00 2001 From: Daniel Eklöf Date: Sun, 28 Jul 2019 21:03:38 +0200 Subject: font: populate glyph cache (ASCII characters only) when instantiating font --- font.c | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) (limited to 'font.c') diff --git a/font.c b/font.c index b22e4457..49fd8130 100644 --- a/font.c +++ b/font.c @@ -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); + } +} -- cgit v1.3