aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorNathan Reiner <nathan@nathanreiner.xyz>2023-04-17 22:39:30 +0200
committerNathan Reiner <nathan@nathanreiner.xyz>2023-04-17 22:39:30 +0200
commitbcde9052c250bb8a684516930069379f1c1349ce (patch)
tree3ffb095d22d4108e2b6cc14cf52ef507f9bc7b3b
parent7659dfc2894518f124557ba1f2ae493c6c041782 (diff)
fix memory leak of draw_char
-rw-r--r--drw.c74
-rw-r--r--drw.h2
2 files changed, 18 insertions, 58 deletions
diff --git a/drw.c b/drw.c
index b906eb9..ff30811 100644
--- a/drw.c
+++ b/drw.c
@@ -241,6 +241,8 @@ init_font(Font *font)
error = FT_Select_Charmap(font->face, ft_encoding_unicode);
if (error)
die("cannot set unicode");
+
+ FT_Stroker_New(font->library, &font->stroke);
}
@@ -331,92 +333,50 @@ draw_char(Canvas *canvas, FontCache *fontcache, uint_least32_t charcode, unsigne
FT_UInt previous = 0;
FT_Bool has_kerning;
FT_Vector delta;
- Font *current_font;
- FT_Stroker stroker;
+ Font *font;
+ FT_BitmapGlyph bg;
+ FT_Glyph glyph;
- current_font = get_font_with_charcode(fontcache, charcode);
- FT_Set_Pixel_Sizes(current_font->face, 0, fontcache->fontsize);
+ font = get_font_with_charcode(fontcache, charcode);
+ FT_Set_Pixel_Sizes(font->face, 0, fontcache->fontsize);
if (boldstokemode && fontcache->fonttype == FONT_BOLD) {
- FT_Stroker_New(current_font->library, &stroker);
- FT_Stroker_Set(stroker, fontcache->fontsize * 2, FT_STROKER_LINECAP_ROUND, FT_STROKER_LINEJOIN_ROUND, fontcache->fontsize * 2);
+ FT_Stroker_Set(font->stroke, fontcache->fontsize * 2, FT_STROKER_LINECAP_ROUND, FT_STROKER_LINEJOIN_ROUND, fontcache->fontsize * 2);
y -= fontcache->fontsize * 2 / 64 + 1;
} else {
- FT_Stroker_New(current_font->library, &stroker);
- FT_Stroker_Set(stroker, 0, FT_STROKER_LINECAP_ROUND, FT_STROKER_LINEJOIN_ROUND, 0);
+ FT_Stroker_Set(font->stroke, 0, FT_STROKER_LINECAP_ROUND, FT_STROKER_LINEJOIN_ROUND, 0);
}
- slot = current_font->face->glyph;
- glyph_index = FT_Get_Char_Index(current_font->face, charcode);
+ slot = font->face->glyph;
+ glyph_index = FT_Get_Char_Index(font->face, charcode);
- has_kerning = FT_HAS_KERNING(current_font->face);
+ has_kerning = FT_HAS_KERNING(font->face);
if (has_kerning && previous && glyph_index) {
- FT_Get_Kerning(current_font->face, previous, glyph_index, FT_KERNING_DEFAULT, &delta);
+ FT_Get_Kerning(font->face, previous, glyph_index, FT_KERNING_DEFAULT, &delta);
x += delta.x >> 6;
}
- error = FT_Load_Glyph(current_font->face, glyph_index, FT_LOAD_DEFAULT);
+ error = FT_Load_Glyph(font->face, glyph_index, FT_LOAD_DEFAULT);
if (error) {
fprintf(stderr, "warning: char not loaded, skipping...\n");
return x;
}
- FT_Glyph glyph;
FT_Get_Glyph(slot, &glyph);
- FT_Glyph_StrokeBorder(&glyph, stroker, 0, 1);
+ FT_Glyph_StrokeBorder(&glyph, font->stroke, 0, 1);
if (error) {
fprintf(stderr, "warning: could not apply stroke\n");
return x;
}
FT_Glyph_To_Bitmap(&glyph, FT_RENDER_MODE_NORMAL, 0, 1);
-
- FT_BitmapGlyph bg = (FT_BitmapGlyph)glyph;
-
+ bg = (FT_BitmapGlyph)glyph;
draw_bitmap(canvas, &bg->bitmap, x + bg->left, y - bg->top, color);
+ FT_Done_Glyph(glyph);
x += fontcache->box.width >> 6;
return x;
}
-unsigned
-font_width(FontCache *fontcache, const char *text)
-{
- FT_GlyphSlot slot;
- FT_Error error;
- FT_UInt glyph_index;
- FT_UInt previous = 0;
- uint_least32_t charcode;
- FT_Bool has_kerning;
- FT_Vector delta;
- unsigned x = 0;
- Font *current_font;
-
- for (; *text;) {
- text += grapheme_decode_utf8(text, sizeof(uint_least32_t), &charcode);
-
- current_font = get_font_with_charcode(fontcache, charcode);
- FT_Set_Pixel_Sizes(current_font->face, 0, fontcache->fontsize);
-
- slot = current_font->face->glyph;
- glyph_index = FT_Get_Char_Index(current_font->face, charcode);
-
- has_kerning = FT_HAS_KERNING(current_font->face);
- if (has_kerning && previous && glyph_index) {
- FT_Get_Kerning(current_font->face, previous, glyph_index, FT_KERNING_DEFAULT, &delta);
- x += delta.x >> 6;
- }
-
- error = FT_Load_Glyph(current_font->face, glyph_index, FT_LOAD_RENDER);
- if (error) {
- fprintf(stderr, "warning: char not loaded, skipping...\n");
- continue;
- }
-
- x += slot->advance.x >> 6;
- }
-
- return x;
-}
diff --git a/drw.h b/drw.h
index af07d50..8417e15 100644
--- a/drw.h
+++ b/drw.h
@@ -24,6 +24,7 @@ typedef struct {
int loaded;
FT_Library library;
FT_Face face;
+ FT_Stroker stroke;
} Font;
enum FontType {
@@ -72,6 +73,5 @@ FontCache *create_font_cache(FontPath *fontpath, int size, unsigned fontsize);
void font_cache_generate_box(FontCache *fontcache);
void free_font_cache(FontCache *fontcache);
unsigned draw_char(Canvas *canvas, FontCache *fontcache, uint_least32_t charcode, unsigned x, unsigned y, uint32_t color);
-unsigned font_width(FontCache *fontcache, const char *text);
#endif