From feb5adefd9d4909aa30d3a49e173a8954b755de2 Mon Sep 17 00:00:00 2001 From: Nathan Reiner Date: Mon, 24 Apr 2023 11:37:22 +0200 Subject: fix fontcache, free glyph after drawing if not cached --- drw.c | 29 ++++++++++++++++++++--------- 1 file changed, 20 insertions(+), 9 deletions(-) (limited to 'drw.c') diff --git a/drw.c b/drw.c index 307e942..3800de5 100644 --- a/drw.c +++ b/drw.c @@ -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); } -- cgit v1.2.3-70-g09d2