Improve bitmap font scaling. Fix default theme font size.

This commit is contained in:
bruvzg 2021-03-08 08:27:57 +02:00
parent 05248535f7
commit 43c7c279d5
No known key found for this signature in database
GPG Key ID: 009E1BFE42239B95
4 changed files with 24 additions and 21 deletions

View File

@ -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);
}
}

View File

@ -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;

View File

@ -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);
//}

View File

@ -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) {