Fix memleak, properly init variables, added unload() method, and small cleanups to Font.

This commit is contained in:
Relintai 2024-01-04 15:48:21 +01:00
parent 6dd8f13545
commit 8f6288e674
2 changed files with 62 additions and 7 deletions

View File

@ -63,12 +63,12 @@ void Font::font_face_from_mem(const void *ttf_data, uint32_t ttf_len, float font
return; return;
} }
unload();
if (!(flags & FONT_ASCII)) { if (!(flags & FONT_ASCII)) {
flags |= FONT_ASCII; // ensure this minimal range [0020-00FF] is almost always in flags |= FONT_ASCII; // ensure this minimal range [0020-00FF] is almost always in
} }
_initialized = 1;
// load .ttf into a bitmap using stb_truetype.h // load .ttf into a bitmap using stb_truetype.h
int dim = flags & FONT_4096 ? 4096 : flags & FONT_2048 ? 2048 int dim = flags & FONT_4096 ? 4096 : flags & FONT_2048 ? 2048
: flags & FONT_1024 ? 1024 : flags & FONT_1024 ? 1024
@ -304,6 +304,9 @@ void Font::font_face_from_mem(const void *ttf_data, uint32_t ttf_len, float font
memdelete_arr(cdata); memdelete_arr(cdata);
_initialized = true; _initialized = true;
#undef MERGE_TABLE
#undef MERGE_PACKED_TABLE
} }
void Font::font_face(const char *filename_ttf, float font_size, unsigned flags) { void Font::font_face(const char *filename_ttf, float font_size, unsigned flags) {
@ -442,6 +445,55 @@ Ref<Texture> Font::get_texture() {
return _texture; return _texture;
} }
Font::Font() { void Font::unload() {
if (!_initialized) {
return;
}
_num_glyphs = 0;
_cp2iter = NULL;
_iter2cp = NULL;
_begin = 0;
_initialized = false; _initialized = false;
_height = 0;
_width = 0;
_font_size = 0;
_factor = 0;
_scale = 0;
_ascent = 0;
_descent = 0;
_linegap = 0;
_linedist = 0;
_image.unref();
_texture.unref();
_texture_offsets.clear();
}
Font::Font() {
_num_glyphs = 0;
_cp2iter = NULL;
_iter2cp = NULL;
_begin = 0;
_initialized = false;
_height = 0;
_width = 0;
_font_size = 0;
_factor = 0;
_scale = 0;
_ascent = 0;
_descent = 0;
_linegap = 0;
_linedist = 0;
}
Font::~Font() {
unload();
} }

View File

@ -79,15 +79,18 @@ public:
Ref<Image> get_image(); Ref<Image> get_image();
Ref<Texture> get_texture(); Ref<Texture> get_texture();
void unload();
Font(); Font();
~Font();
protected: protected:
// character info // character info
// filled up by stb_truetype.h // filled up by stb_truetype.h
unsigned _num_glyphs; uint32_t _num_glyphs;
unsigned *_cp2iter; uint32_t *_cp2iter;
unsigned *_iter2cp; uint32_t *_iter2cp;
unsigned _begin; // first glyph. used in cp2iter table to clamp into a lesser range uint32_t _begin; // first glyph. used in cp2iter table to clamp into a lesser range
// font info and data // font info and data
bool _initialized; bool _initialized;