diff options
| author | Nathan Reiner <nathan@nathanreiner.xyz> | 2023-04-24 11:37:22 +0200 |
|---|---|---|
| committer | Nathan Reiner <nathan@nathanreiner.xyz> | 2023-04-24 11:37:22 +0200 |
| commit | feb5adefd9d4909aa30d3a49e173a8954b755de2 (patch) | |
| tree | 8e457891824d08386f24eee6e3915f3a8b28d0ee /drw.c | |
| parent | 67893dc14d10f49a76652ff384ab2bf4a945ca30 (diff) | |
fix fontcache, free glyph after drawing if not cached
Diffstat (limited to 'drw.c')
| -rw-r--r-- | drw.c | 29 |
1 files changed, 20 insertions, 9 deletions
@@ -13,6 +13,11 @@ extern int boldstokemode; +typedef struct { + FT_Glyph glyph; + FT_BitmapGlyph bg; +} CacheGlyph; + /* static function declarations */ static Color to_color(uint32_t color); static uint32_t to_uint32_t(Color color); @@ -380,9 +385,10 @@ get_font_with_charcode(FontCache *fontcache, uint_least32_t charcode) } -FT_BitmapGlyph +CacheGlyph load_bitmap(Font *font, uint_least32_t charcode) { + CacheGlyph cglyph = { .bg = 0, .glyph = 0 }; FT_Bool has_kerning; FT_Vector delta; FT_UInt glyph_index; @@ -403,21 +409,21 @@ load_bitmap(Font *font, uint_least32_t charcode) error = FT_Load_Glyph(font->face, glyph_index, FT_LOAD_DEFAULT); if (error) { fprintf(stderr, "warning: char not loaded, skipping...\n"); - return 0; + return cglyph; } FT_Get_Glyph(slot, &glyph); FT_Glyph_StrokeBorder(&glyph, font->stroke, 0, 1); if (error) { fprintf(stderr, "warning: could not apply stroke\n"); - return 0; + return cglyph; } FT_Glyph_To_Bitmap(&glyph, FT_RENDER_MODE_NORMAL, 0, 1); bg = (FT_BitmapGlyph)glyph; if (glyph_index >= CACHE_SIZE) { - FT_Done_Glyph(glyph); + cglyph.glyph = glyph; } else { font->cache[glyph_index] = bg; } @@ -425,14 +431,16 @@ load_bitmap(Font *font, uint_least32_t charcode) bg = font->cache[glyph_index]; } - return bg; + cglyph.bg = bg; + + return cglyph; } void draw_char(Canvas *canvas, FontCache *fontcache, uint_least32_t charcode, unsigned x, unsigned y, uint32_t color) { Font *font; - FT_BitmapGlyph bg; + CacheGlyph cg; font = get_font_with_charcode(fontcache, charcode); FT_Set_Pixel_Sizes(font->face, 0, fontcache->fontsize); @@ -444,11 +452,14 @@ draw_char(Canvas *canvas, FontCache *fontcache, uint_least32_t charcode, unsigne FT_Stroker_Set(font->stroke, 0, FT_STROKER_LINECAP_BUTT, FT_STROKER_LINEJOIN_ROUND, 0); } - bg = load_bitmap(font, charcode); + cg = load_bitmap(font, charcode); - if (bg == 0) + if (cg.bg == 0) return; - draw_bitmap(canvas, &bg->bitmap, x + bg->left, y - bg->top, color); + draw_bitmap(canvas, &cg.bg->bitmap, x + cg.bg->left, y - cg.bg->top, color); + + if (cg.glyph) + FT_Done_Glyph(cg.glyph); } |