diff options
author | bruvzg <7645683+bruvzg@users.noreply.github.com> | 2021-03-08 08:27:57 +0200 |
---|---|---|
committer | bruvzg <7645683+bruvzg@users.noreply.github.com> | 2021-03-08 09:56:40 +0200 |
commit | 43c7c279d5e5523a25bec4f3fb835c8ed1de6a23 (patch) | |
tree | bcfa1095c3c804c3efce256f14f3a7d3a7a40816 /modules/text_server_adv | |
parent | 05248535f7bf0cb4e8aadb3a7ca4846bd0bf4da5 (diff) |
Improve bitmap font scaling. Fix default theme font size.
Diffstat (limited to 'modules/text_server_adv')
-rw-r--r-- | modules/text_server_adv/bitmap_font_adv.cpp | 31 | ||||
-rw-r--r-- | modules/text_server_adv/bitmap_font_adv.h | 5 |
2 files changed, 20 insertions, 16 deletions
diff --git a/modules/text_server_adv/bitmap_font_adv.cpp b/modules/text_server_adv/bitmap_font_adv.cpp index e33556b232..df7b42eac6 100644 --- a/modules/text_server_adv/bitmap_font_adv.cpp +++ b/modules/text_server_adv/bitmap_font_adv.cpp @@ -361,6 +361,10 @@ Error BitmapFontDataAdvanced::load_from_file(const String &p_filename, int p_bas base_size = height; } + if (hb_handle) { + hb_font_destroy(hb_handle); + } + hb_handle = hb_bmp_font_create(this, base_size, nullptr); valid = true; memdelete(f); @@ -379,12 +383,10 @@ Error BitmapFontDataAdvanced::bitmap_new(float p_height, float p_ascent, int p_b char_map.clear(); textures.clear(); kerning_map.clear(); - - for (Map<float, hb_font_t *>::Element *E = cache.front(); E; E = E->next()) { - hb_font_destroy(E->get()); + if (hb_handle) { + hb_font_destroy(hb_handle); } - cache.clear(); - + hb_handle = hb_bmp_font_create(this, base_size, nullptr); valid = true; return OK; @@ -466,10 +468,7 @@ float BitmapFontDataAdvanced::get_base_size() const { hb_font_t *BitmapFontDataAdvanced::get_hb_handle(int p_size) { _THREAD_SAFE_METHOD_ ERR_FAIL_COND_V(!valid, nullptr); - if (!cache.has(p_size)) { - cache[p_size] = hb_bmp_font_create(this, p_size, nullptr); - } - return cache[p_size]; + return hb_handle; } bool BitmapFontDataAdvanced::has_char(char32_t p_char) const { @@ -516,6 +515,10 @@ Vector2 BitmapFontDataAdvanced::get_size(uint32_t p_char, int p_size) const { return c->rect.size * (float(p_size) / float(base_size)); } +float BitmapFontDataAdvanced::get_font_scale(int p_size) const { + return float(p_size) / float(base_size); +} + Vector2 BitmapFontDataAdvanced::get_kerning(uint32_t p_char, uint32_t p_next, int p_size) const { _THREAD_SAFE_METHOD_ ERR_FAIL_COND_V(!valid, Vector2()); @@ -543,13 +546,13 @@ Vector2 BitmapFontDataAdvanced::draw_glyph(RID p_canvas, int p_size, const Vecto ERR_FAIL_COND_V(c->texture_idx < -1 || c->texture_idx >= textures.size(), Vector2()); if (c->texture_idx != -1) { Point2i cpos = p_pos; - cpos += c->align * (float(p_size) / float(base_size)); - cpos.y -= ascent * (float(p_size) / float(base_size)); + cpos += (c->align + Vector2(0, -ascent)) * (float(p_size) / float(base_size)); + Size2i csize = c->rect.size * (float(p_size) / float(base_size)); if (RenderingServer::get_singleton() != nullptr) { //if (distance_field_hint) { // Not implemented. // RenderingServer::get_singleton()->canvas_item_set_distance_field_mode(p_canvas, true); //} - RenderingServer::get_singleton()->canvas_item_add_texture_rect_region(p_canvas, Rect2(cpos, c->rect.size * (float(p_size) / float(base_size))), textures[c->texture_idx]->get_rid(), c->rect, p_color, false, false); + RenderingServer::get_singleton()->canvas_item_add_texture_rect_region(p_canvas, Rect2(cpos, csize), textures[c->texture_idx]->get_rid(), c->rect, p_color, false, false); //if (distance_field_hint) { // RenderingServer::get_singleton()->canvas_item_set_distance_field_mode(p_canvas, false); //} @@ -576,7 +579,7 @@ Vector2 BitmapFontDataAdvanced::draw_glyph_outline(RID p_canvas, int p_size, int } BitmapFontDataAdvanced::~BitmapFontDataAdvanced() { - for (Map<float, hb_font_t *>::Element *E = cache.front(); E; E = E->next()) { - hb_font_destroy(E->get()); + if (hb_handle) { + hb_font_destroy(hb_handle); } } diff --git a/modules/text_server_adv/bitmap_font_adv.h b/modules/text_server_adv/bitmap_font_adv.h index da7c2b00ac..7b620021e1 100644 --- a/modules/text_server_adv/bitmap_font_adv.h +++ b/modules/text_server_adv/bitmap_font_adv.h @@ -63,11 +63,11 @@ private: HashMap<uint32_t, Character> char_map; Map<KerningPairKey, int> kerning_map; - Map<float, hb_font_t *> cache; + hb_font_t *hb_handle = nullptr; float height = 0.f; float ascent = 0.f; - float base_size = 0.f; + int base_size = 0; bool distance_field_hint = false; public: @@ -101,6 +101,7 @@ public: virtual bool has_outline() const override { return false; }; virtual float get_base_size() const override; + virtual float get_font_scale(int p_size) const override; virtual hb_font_t *get_hb_handle(int p_size) override; |