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 | |
parent | 05248535f7bf0cb4e8aadb3a7ca4846bd0bf4da5 (diff) |
Improve bitmap font scaling. Fix default theme font size.
-rw-r--r-- | modules/text_server_adv/bitmap_font_adv.cpp | 31 | ||||
-rw-r--r-- | modules/text_server_adv/bitmap_font_adv.h | 5 | ||||
-rw-r--r-- | modules/text_server_fb/bitmap_font_fb.cpp | 7 | ||||
-rw-r--r-- | scene/resources/default_theme/default_theme.cpp | 2 |
4 files changed, 24 insertions, 21 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; diff --git a/modules/text_server_fb/bitmap_font_fb.cpp b/modules/text_server_fb/bitmap_font_fb.cpp index c58f8cbba1..313f170f04 100644 --- a/modules/text_server_fb/bitmap_font_fb.cpp +++ b/modules/text_server_fb/bitmap_font_fb.cpp @@ -319,14 +319,13 @@ Vector2 BitmapFontDataFallback::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); //} diff --git a/scene/resources/default_theme/default_theme.cpp b/scene/resources/default_theme/default_theme.cpp index a94209c75f..ddf1fc3db4 100644 --- a/scene/resources/default_theme/default_theme.cpp +++ b/scene/resources/default_theme/default_theme.cpp @@ -1017,7 +1017,7 @@ void make_default_theme(bool p_hidpi, Ref<Font> p_font) { Ref<StyleBox> default_style; Ref<Texture2D> default_icon; Ref<Font> default_font; - int default_font_size = 16; + int default_font_size = 14; if (p_font.is_valid()) { default_font = p_font; } else if (p_hidpi) { |