font: populate glyph cache (ASCII characters only) when instantiating font

This commit is contained in:
Daniel Eklöf 2019-07-28 21:03:38 +02:00
parent 9e57ba2108
commit fe882bddba
No known key found for this signature in database
GPG key ID: 5BBD4992C116573F
5 changed files with 44 additions and 82 deletions

25
font.c
View file

@ -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);
}
}