From bee718f1affca7ccf6ce2dbe3bf69a2cc1882cce Mon Sep 17 00:00:00 2001 From: bruvzg <7645683+bruvzg@users.noreply.github.com> Date: Sat, 6 Feb 2021 16:34:06 +0200 Subject: [PATCH] [Text Server] Restores bitmap font dynamic construction functions. --- doc/classes/FontData.xml | 52 ++++++++++ doc/classes/TextServer.xml | 58 ++++++++++++ modules/gdnative/include/text/godot_text.h | 4 + .../gdnative/text/text_server_gdnative.cpp | 22 +++++ modules/gdnative/text/text_server_gdnative.h | 5 + modules/text_server_adv/bitmap_font_adv.cpp | 94 ++++++++++--------- modules/text_server_adv/bitmap_font_adv.h | 6 +- modules/text_server_adv/font_adv.h | 9 +- modules/text_server_adv/text_server_adv.cpp | 33 +++++++ modules/text_server_adv/text_server_adv.h | 5 + modules/text_server_fb/bitmap_font_fb.cpp | 89 +++++++++--------- modules/text_server_fb/bitmap_font_fb.h | 6 +- modules/text_server_fb/font_fb.h | 9 +- modules/text_server_fb/text_server_fb.cpp | 33 +++++++ modules/text_server_fb/text_server_fb.h | 5 + .../resources/default_theme/default_theme.cpp | 66 ++++++------- scene/resources/font.cpp | 33 +++++++ scene/resources/font.h | 6 ++ servers/text_server.cpp | 5 + servers/text_server.h | 17 +--- 20 files changed, 415 insertions(+), 142 deletions(-) diff --git a/doc/classes/FontData.xml b/doc/classes/FontData.xml index e2c35f9ce71..17f4351c23e 100644 --- a/doc/classes/FontData.xml +++ b/doc/classes/FontData.xml @@ -11,6 +11,45 @@ + + + + + + + + + + + + + + + Adds a character to the font, where [code]character[/code] is the Unicode value, [code]texture[/code] is the texture index, [code]rect[/code] is the region in the texture (in pixels!), [code]align[/code] is the (optional) alignment for the character and [code]advance[/code] is the (optional) advance. + + + + + + + + + + + + + Adds a kerning pair to the bitmap font as a difference. Kerning pairs are special cases where a typeface advance is determined by the next character. + + + + + + + + + Adds a texture to the bitmap font. + + @@ -256,6 +295,19 @@ Note: For non-scalable fonts [code]base_size[/code] is ignored, use [method get_base_size] to check actual font size. + + + + + + + + + + + Creates new, empty bitmap font. + + diff --git a/doc/classes/TextServer.xml b/doc/classes/TextServer.xml index 791646000b2..0dfdec81232 100644 --- a/doc/classes/TextServer.xml +++ b/doc/classes/TextServer.xml @@ -9,6 +9,19 @@ + + + + + + + + + + + Creates new, empty bitmap font. To free the resulting font, use [method free_rid] method. + + @@ -78,6 +91,51 @@ Draws box displaying character hexadecimal code. Used for replacing missing characters. + + + + + + + + + + + + + + + + + Adds a character to the font, where [code]character[/code] is the Unicode value, [code]texture[/code] is the texture index, [code]rect[/code] is the region in the texture (in pixels!), [code]align[/code] is the (optional) alignment for the character and [code]advance[/code] is the (optional) advance. + + + + + + + + + + + + + + + Adds a kerning pair to the bitmap font as a difference. Kerning pairs are special cases where a typeface advance is determined by the next character. + + + + + + + + + + + Adds a texture to the bitmap font. + + diff --git a/modules/gdnative/include/text/godot_text.h b/modules/gdnative/include/text/godot_text.h index 44fac3c1907..cdbd12a6a8e 100644 --- a/modules/gdnative/include/text/godot_text.h +++ b/modules/gdnative/include/text/godot_text.h @@ -74,6 +74,10 @@ typedef struct { godot_rid (*create_font_system)(void *, const godot_string *, int); godot_rid (*create_font_resource)(void *, const godot_string *, int); godot_rid (*create_font_memory)(void *, const uint8_t *, size_t, godot_string *, int); + godot_rid (*create_font_bitmap)(void *, float, float, int); + void (*font_bitmap_add_texture)(void *, godot_rid *, const godot_object *); + void (*font_bitmap_add_char)(void *, godot_rid *, char32_t, int, const godot_rect2 *, const godot_vector2 *, float); + void (*font_bitmap_add_kerning_pair)(void *, godot_rid *, char32_t, char32_t, int); float (*font_get_height)(void *, godot_rid *, int); float (*font_get_ascent)(void *, godot_rid *, int); float (*font_get_descent)(void *, godot_rid *, int); diff --git a/modules/gdnative/text/text_server_gdnative.cpp b/modules/gdnative/text/text_server_gdnative.cpp index f7a3cb8135d..34ff2ec3a20 100644 --- a/modules/gdnative/text/text_server_gdnative.cpp +++ b/modules/gdnative/text/text_server_gdnative.cpp @@ -113,6 +113,28 @@ RID TextServerGDNative::create_font_memory(const uint8_t *p_data, size_t p_size, return rid; } +RID TextServerGDNative::create_font_bitmap(float p_height, float p_ascent, int p_base_size) { + ERR_FAIL_COND_V(interface == nullptr, RID()); + godot_rid result = interface->create_font_bitmap(data, p_height, p_ascent, p_base_size); + RID rid = *(RID *)&result; + return rid; +} + +void TextServerGDNative::font_bitmap_add_texture(RID p_font, const Ref &p_texture) { + ERR_FAIL_COND(interface == nullptr); + interface->font_bitmap_add_texture(data, (godot_rid *)&p_font, (const godot_object *)p_texture.ptr()); +} + +void TextServerGDNative::font_bitmap_add_char(RID p_font, char32_t p_char, int p_texture_idx, const Rect2 &p_rect, const Size2 &p_align, float p_advance) { + ERR_FAIL_COND(interface == nullptr); + interface->font_bitmap_add_char(data, (godot_rid *)&p_font, p_char, p_texture_idx, (const godot_rect2 *)&p_rect, (const godot_vector2 *)&p_align, p_advance); +} + +void TextServerGDNative::font_bitmap_add_kerning_pair(RID p_font, char32_t p_A, char32_t p_B, int p_kerning) { + ERR_FAIL_COND(interface == nullptr); + interface->font_bitmap_add_kerning_pair(data, (godot_rid *)&p_font, p_A, p_B, p_kerning); +} + float TextServerGDNative::font_get_height(RID p_font, int p_size) const { ERR_FAIL_COND_V(interface == nullptr, 0.f); return interface->font_get_height(data, (godot_rid *)&p_font, p_size); diff --git a/modules/gdnative/text/text_server_gdnative.h b/modules/gdnative/text/text_server_gdnative.h index 9cbb94217e7..f90a60138f7 100644 --- a/modules/gdnative/text/text_server_gdnative.h +++ b/modules/gdnative/text/text_server_gdnative.h @@ -64,6 +64,11 @@ public: virtual RID create_font_system(const String &p_name, int p_base_size = 16) override; virtual RID create_font_resource(const String &p_filename, int p_base_size = 16) override; virtual RID create_font_memory(const uint8_t *p_data, size_t p_size, const String &p_type, int p_base_size = 16) override; + virtual RID create_font_bitmap(float p_height, float p_ascent, int p_base_size = 16) override; + + virtual void font_bitmap_add_texture(RID p_font, const Ref &p_texture) override; + virtual void font_bitmap_add_char(RID p_font, char32_t p_char, int p_texture_idx, const Rect2 &p_rect, const Size2 &p_align, float p_advance) override; + virtual void font_bitmap_add_kerning_pair(RID p_font, char32_t p_A, char32_t p_B, int p_kerning) override; virtual float font_get_height(RID p_font, int p_size) const override; virtual float font_get_ascent(RID p_font, int p_size) const override; diff --git a/modules/text_server_adv/bitmap_font_adv.cpp b/modules/text_server_adv/bitmap_font_adv.cpp index df771301e65..e33556b232c 100644 --- a/modules/text_server_adv/bitmap_font_adv.cpp +++ b/modules/text_server_adv/bitmap_font_adv.cpp @@ -367,63 +367,65 @@ Error BitmapFontDataAdvanced::load_from_file(const String &p_filename, int p_bas return OK; } -Error BitmapFontDataAdvanced::load_from_memory(const uint8_t *p_data, size_t p_size, int p_base_size) { - _THREAD_SAFE_METHOD_ - ERR_FAIL_COND_V(p_data == nullptr, ERR_CANT_CREATE); - ERR_FAIL_COND_V(p_size != sizeof(TextServer::BitmapFontData), ERR_CANT_CREATE); - - const TextServer::BitmapFontData *data = (const TextServer::BitmapFontData *)p_data; - - if (RenderingServer::get_singleton() != nullptr) { - Ref image = memnew(Image(data->img)); - Ref tex = memnew(ImageTexture); - tex->create_from_image(image); - - textures.push_back(tex); - } - - for (int i = 0; i < data->charcount; i++) { - const int *c = &data->char_rects[i * 8]; - - Character chr; - chr.rect.position.x = c[1]; - chr.rect.position.y = c[2]; - chr.rect.size.x = c[3]; - chr.rect.size.y = c[4]; - if (c[7] < 0) { - chr.advance.x = c[3]; - } else { - chr.advance.x = c[7]; - } - chr.align = Vector2(c[6], c[5]); - char_map[c[0]] = chr; - } - - for (int i = 0; i < data->kerning_count; i++) { - KerningPairKey kpk; - kpk.A = data->kernings[i * 3 + 0]; - kpk.B = data->kernings[i * 3 + 1]; - - if (data->kernings[i * 3 + 2] == 0 && kerning_map.has(kpk)) { - kerning_map.erase(kpk); - } else { - kerning_map[kpk] = data->kernings[i * 3 + 2]; - } - } - - height = data->height; - ascent = data->ascent; +Error BitmapFontDataAdvanced::bitmap_new(float p_height, float p_ascent, int p_base_size) { + height = p_height; + ascent = p_ascent; base_size = p_base_size; if (base_size == 0) { base_size = height; } + char_map.clear(); + textures.clear(); + kerning_map.clear(); + + for (Map::Element *E = cache.front(); E; E = E->next()) { + hb_font_destroy(E->get()); + } + cache.clear(); + valid = true; return OK; } +void BitmapFontDataAdvanced::bitmap_add_texture(const Ref &p_texture) { + ERR_FAIL_COND(!valid); + ERR_FAIL_COND_MSG(p_texture.is_null(), "It's not a reference to a valid Texture object."); + + textures.push_back(p_texture); +} + +void BitmapFontDataAdvanced::bitmap_add_char(char32_t p_char, int p_texture_idx, const Rect2 &p_rect, const Size2 &p_align, float p_advance) { + ERR_FAIL_COND(!valid); + + Character chr; + chr.rect = p_rect; + chr.texture_idx = p_texture_idx; + if (p_advance < 0) { + chr.advance.x = chr.rect.size.x; + } else { + chr.advance.x = p_advance; + } + chr.align = p_align; + char_map[p_char] = chr; +} + +void BitmapFontDataAdvanced::bitmap_add_kerning_pair(char32_t p_A, char32_t p_B, int p_kerning) { + ERR_FAIL_COND(!valid); + + KerningPairKey kpk; + kpk.A = p_A; + kpk.B = p_B; + + if (p_kerning == 0 && kerning_map.has(kpk)) { + kerning_map.erase(kpk); + } else { + kerning_map[kpk] = p_kerning; + } +} + float BitmapFontDataAdvanced::get_height(int p_size) const { ERR_FAIL_COND_V(!valid, 0.f); return height * (float(p_size) / float(base_size)); diff --git a/modules/text_server_adv/bitmap_font_adv.h b/modules/text_server_adv/bitmap_font_adv.h index c314f1b0877..da7c2b00acd 100644 --- a/modules/text_server_adv/bitmap_font_adv.h +++ b/modules/text_server_adv/bitmap_font_adv.h @@ -74,7 +74,11 @@ public: virtual void clear_cache() override{}; virtual Error load_from_file(const String &p_filename, int p_base_size) override; - virtual Error load_from_memory(const uint8_t *p_data, size_t p_size, int p_base_size) override; + virtual Error bitmap_new(float p_height, float p_ascent, int p_base_size) override; + + virtual void bitmap_add_texture(const Ref &p_texture) override; + virtual void bitmap_add_char(char32_t p_char, int p_texture_idx, const Rect2 &p_rect, const Size2 &p_align, float p_advance) override; + virtual void bitmap_add_kerning_pair(char32_t p_A, char32_t p_B, int p_kerning) override; virtual float get_height(int p_size) const override; virtual float get_ascent(int p_size) const override; diff --git a/modules/text_server_adv/font_adv.h b/modules/text_server_adv/font_adv.h index 4bbd2dd4bfd..80ede5ef2d0 100644 --- a/modules/text_server_adv/font_adv.h +++ b/modules/text_server_adv/font_adv.h @@ -42,8 +42,13 @@ struct FontDataAdvanced { virtual void clear_cache() = 0; - virtual Error load_from_file(const String &p_filename, int p_base_size) = 0; - virtual Error load_from_memory(const uint8_t *p_data, size_t p_size, int p_base_size) = 0; + virtual Error load_from_file(const String &p_filename, int p_base_size) { return ERR_CANT_CREATE; }; + virtual Error load_from_memory(const uint8_t *p_data, size_t p_size, int p_base_size) { return ERR_CANT_CREATE; }; + virtual Error bitmap_new(float p_height, float p_ascent, int p_base_size) { return ERR_CANT_CREATE; }; + + virtual void bitmap_add_texture(const Ref &p_texture) { ERR_FAIL_MSG("Not supported."); }; + virtual void bitmap_add_char(char32_t p_char, int p_texture_idx, const Rect2 &p_rect, const Size2 &p_align, float p_advance) { ERR_FAIL_MSG("Not supported."); }; + virtual void bitmap_add_kerning_pair(char32_t p_A, char32_t p_B, int p_kerning) { ERR_FAIL_MSG("Not supported."); }; virtual float get_height(int p_size) const = 0; virtual float get_ascent(int p_size) const = 0; diff --git a/modules/text_server_adv/text_server_adv.cpp b/modules/text_server_adv/text_server_adv.cpp index 8e4771685dc..3137f534ff4 100644 --- a/modules/text_server_adv/text_server_adv.cpp +++ b/modules/text_server_adv/text_server_adv.cpp @@ -566,6 +566,39 @@ RID TextServerAdvanced::create_font_memory(const uint8_t *p_data, size_t p_size, return font_owner.make_rid(fd); } +RID TextServerAdvanced::create_font_bitmap(float p_height, float p_ascent, int p_base_size) { + _THREAD_SAFE_METHOD_ + FontDataAdvanced *fd = memnew(BitmapFontDataAdvanced); + Error err = fd->bitmap_new(p_height, p_ascent, p_base_size); + if (err != OK) { + memdelete(fd); + return RID(); + } + + return font_owner.make_rid(fd); +} + +void TextServerAdvanced::font_bitmap_add_texture(RID p_font, const Ref &p_texture) { + _THREAD_SAFE_METHOD_ + FontDataAdvanced *fd = font_owner.getornull(p_font); + ERR_FAIL_COND(!fd); + fd->bitmap_add_texture(p_texture); +} + +void TextServerAdvanced::font_bitmap_add_char(RID p_font, char32_t p_char, int p_texture_idx, const Rect2 &p_rect, const Size2 &p_align, float p_advance) { + _THREAD_SAFE_METHOD_ + FontDataAdvanced *fd = font_owner.getornull(p_font); + ERR_FAIL_COND(!fd); + fd->bitmap_add_char(p_char, p_texture_idx, p_rect, p_align, p_advance); +} + +void TextServerAdvanced::font_bitmap_add_kerning_pair(RID p_font, char32_t p_A, char32_t p_B, int p_kerning) { + _THREAD_SAFE_METHOD_ + FontDataAdvanced *fd = font_owner.getornull(p_font); + ERR_FAIL_COND(!fd); + fd->bitmap_add_kerning_pair(p_A, p_B, p_kerning); +} + float TextServerAdvanced::font_get_height(RID p_font, int p_size) const { _THREAD_SAFE_METHOD_ const FontDataAdvanced *fd = font_owner.getornull(p_font); diff --git a/modules/text_server_adv/text_server_adv.h b/modules/text_server_adv/text_server_adv.h index 89fae477f98..5937b87b20f 100644 --- a/modules/text_server_adv/text_server_adv.h +++ b/modules/text_server_adv/text_server_adv.h @@ -126,6 +126,11 @@ public: virtual RID create_font_system(const String &p_name, int p_base_size = 16) override; virtual RID create_font_resource(const String &p_filename, int p_base_size = 16) override; virtual RID create_font_memory(const uint8_t *p_data, size_t p_size, const String &p_type, int p_base_size = 16) override; + virtual RID create_font_bitmap(float p_height, float p_ascent, int p_base_size = 16) override; + + virtual void font_bitmap_add_texture(RID p_font, const Ref &p_texture) override; + virtual void font_bitmap_add_char(RID p_font, char32_t p_char, int p_texture_idx, const Rect2 &p_rect, const Size2 &p_align, float p_advance) override; + virtual void font_bitmap_add_kerning_pair(RID p_font, char32_t p_A, char32_t p_B, int p_kerning) override; virtual float font_get_height(RID p_font, int p_size) const override; virtual float font_get_ascent(RID p_font, int p_size) const override; diff --git a/modules/text_server_fb/bitmap_font_fb.cpp b/modules/text_server_fb/bitmap_font_fb.cpp index c9a9cc6eba9..c58f8cbba18 100644 --- a/modules/text_server_fb/bitmap_font_fb.cpp +++ b/modules/text_server_fb/bitmap_font_fb.cpp @@ -175,63 +175,60 @@ Error BitmapFontDataFallback::load_from_file(const String &p_filename, int p_bas return OK; } -Error BitmapFontDataFallback::load_from_memory(const uint8_t *p_data, size_t p_size, int p_base_size) { - _THREAD_SAFE_METHOD_ - ERR_FAIL_COND_V(p_data == nullptr, ERR_CANT_CREATE); - ERR_FAIL_COND_V(p_size != sizeof(TextServer::BitmapFontData), ERR_CANT_CREATE); - - const TextServer::BitmapFontData *data = (const TextServer::BitmapFontData *)p_data; - - if (RenderingServer::get_singleton() != nullptr) { - Ref image = memnew(Image(data->img)); - Ref tex = memnew(ImageTexture); - tex->create_from_image(image); - - textures.push_back(tex); - } - - for (int i = 0; i < data->charcount; i++) { - const int *c = &data->char_rects[i * 8]; - - Character chr; - chr.rect.position.x = c[1]; - chr.rect.position.y = c[2]; - chr.rect.size.x = c[3]; - chr.rect.size.y = c[4]; - if (c[7] < 0) { - chr.advance.x = c[3]; - } else { - chr.advance.x = c[7]; - } - chr.align = Vector2(c[6], c[5]); - char_map[c[0]] = chr; - } - - for (int i = 0; i < data->kerning_count; i++) { - KerningPairKey kpk; - kpk.A = data->kernings[i * 3 + 0]; - kpk.B = data->kernings[i * 3 + 1]; - - if (data->kernings[i * 3 + 2] == 0 && kerning_map.has(kpk)) { - kerning_map.erase(kpk); - } else { - kerning_map[kpk] = data->kernings[i * 3 + 2]; - } - } - - height = data->height; - ascent = data->ascent; +Error BitmapFontDataFallback::bitmap_new(float p_height, float p_ascent, int p_base_size) { + height = p_height; + ascent = p_ascent; base_size = p_base_size; if (base_size == 0) { base_size = height; } + char_map.clear(); + textures.clear(); + kerning_map.clear(); + valid = true; return OK; } +void BitmapFontDataFallback::bitmap_add_texture(const Ref &p_texture) { + ERR_FAIL_COND(!valid); + ERR_FAIL_COND_MSG(p_texture.is_null(), "It's not a reference to a valid Texture object."); + + textures.push_back(p_texture); +} + +void BitmapFontDataFallback::bitmap_add_char(char32_t p_char, int p_texture_idx, const Rect2 &p_rect, const Size2 &p_align, float p_advance) { + ERR_FAIL_COND(!valid); + + Character chr; + chr.rect = p_rect; + chr.texture_idx = p_texture_idx; + if (p_advance < 0) { + chr.advance.x = chr.rect.size.x; + } else { + chr.advance.x = p_advance; + } + chr.align = p_align; + char_map[p_char] = chr; +} + +void BitmapFontDataFallback::bitmap_add_kerning_pair(char32_t p_A, char32_t p_B, int p_kerning) { + ERR_FAIL_COND(!valid); + + KerningPairKey kpk; + kpk.A = p_A; + kpk.B = p_B; + + if (p_kerning == 0 && kerning_map.has(kpk)) { + kerning_map.erase(kpk); + } else { + kerning_map[kpk] = p_kerning; + } +} + float BitmapFontDataFallback::get_height(int p_size) const { ERR_FAIL_COND_V(!valid, 0.f); return height * (float(p_size) / float(base_size)); diff --git a/modules/text_server_fb/bitmap_font_fb.h b/modules/text_server_fb/bitmap_font_fb.h index 33401b85fa8..7cd7507ebc7 100644 --- a/modules/text_server_fb/bitmap_font_fb.h +++ b/modules/text_server_fb/bitmap_font_fb.h @@ -70,7 +70,11 @@ public: virtual void clear_cache() override{}; virtual Error load_from_file(const String &p_filename, int p_base_size) override; - virtual Error load_from_memory(const uint8_t *p_data, size_t p_size, int p_base_size) override; + virtual Error bitmap_new(float p_height, float p_ascent, int p_base_size) override; + + virtual void bitmap_add_texture(const Ref &p_texture) override; + virtual void bitmap_add_char(char32_t p_char, int p_texture_idx, const Rect2 &p_rect, const Size2 &p_align, float p_advance) override; + virtual void bitmap_add_kerning_pair(char32_t p_A, char32_t p_B, int p_kerning) override; virtual float get_height(int p_size) const override; virtual float get_ascent(int p_size) const override; diff --git a/modules/text_server_fb/font_fb.h b/modules/text_server_fb/font_fb.h index cc72919542b..44b4d8507f5 100644 --- a/modules/text_server_fb/font_fb.h +++ b/modules/text_server_fb/font_fb.h @@ -40,8 +40,13 @@ struct FontDataFallback { virtual void clear_cache() = 0; - virtual Error load_from_file(const String &p_filename, int p_base_size) = 0; - virtual Error load_from_memory(const uint8_t *p_data, size_t p_size, int p_base_size) = 0; + virtual Error load_from_file(const String &p_filename, int p_base_size) { return ERR_CANT_CREATE; }; + virtual Error load_from_memory(const uint8_t *p_data, size_t p_size, int p_base_size) { return ERR_CANT_CREATE; }; + virtual Error bitmap_new(float p_height, float p_ascent, int p_base_size) { return ERR_CANT_CREATE; }; + + virtual void bitmap_add_texture(const Ref &p_texture) { ERR_FAIL_MSG("Not supported."); }; + virtual void bitmap_add_char(char32_t p_char, int p_texture_idx, const Rect2 &p_rect, const Size2 &p_align, float p_advance) { ERR_FAIL_MSG("Not supported."); }; + virtual void bitmap_add_kerning_pair(char32_t p_A, char32_t p_B, int p_kerning) { ERR_FAIL_MSG("Not supported."); }; virtual float get_height(int p_size) const = 0; virtual float get_ascent(int p_size) const = 0; diff --git a/modules/text_server_fb/text_server_fb.cpp b/modules/text_server_fb/text_server_fb.cpp index e60d269408b..94eb61ecb1c 100644 --- a/modules/text_server_fb/text_server_fb.cpp +++ b/modules/text_server_fb/text_server_fb.cpp @@ -144,6 +144,39 @@ RID TextServerFallback::create_font_memory(const uint8_t *p_data, size_t p_size, return font_owner.make_rid(fd); } +RID TextServerFallback::create_font_bitmap(float p_height, float p_ascent, int p_base_size) { + _THREAD_SAFE_METHOD_ + FontDataFallback *fd = memnew(BitmapFontDataFallback); + Error err = fd->bitmap_new(p_height, p_ascent, p_base_size); + if (err != OK) { + memdelete(fd); + return RID(); + } + + return font_owner.make_rid(fd); +} + +void TextServerFallback::font_bitmap_add_texture(RID p_font, const Ref &p_texture) { + _THREAD_SAFE_METHOD_ + FontDataFallback *fd = font_owner.getornull(p_font); + ERR_FAIL_COND(!fd); + fd->bitmap_add_texture(p_texture); +} + +void TextServerFallback::font_bitmap_add_char(RID p_font, char32_t p_char, int p_texture_idx, const Rect2 &p_rect, const Size2 &p_align, float p_advance) { + _THREAD_SAFE_METHOD_ + FontDataFallback *fd = font_owner.getornull(p_font); + ERR_FAIL_COND(!fd); + fd->bitmap_add_char(p_char, p_texture_idx, p_rect, p_align, p_advance); +} + +void TextServerFallback::font_bitmap_add_kerning_pair(RID p_font, char32_t p_A, char32_t p_B, int p_kerning) { + _THREAD_SAFE_METHOD_ + FontDataFallback *fd = font_owner.getornull(p_font); + ERR_FAIL_COND(!fd); + fd->bitmap_add_kerning_pair(p_A, p_B, p_kerning); +} + float TextServerFallback::font_get_height(RID p_font, int p_size) const { _THREAD_SAFE_METHOD_ const FontDataFallback *fd = font_owner.getornull(p_font); diff --git a/modules/text_server_fb/text_server_fb.h b/modules/text_server_fb/text_server_fb.h index d142b320e48..4727a65d6d5 100644 --- a/modules/text_server_fb/text_server_fb.h +++ b/modules/text_server_fb/text_server_fb.h @@ -81,6 +81,11 @@ public: virtual RID create_font_system(const String &p_name, int p_base_size = 16) override; virtual RID create_font_resource(const String &p_filename, int p_base_size = 16) override; virtual RID create_font_memory(const uint8_t *p_data, size_t p_size, const String &p_type, int p_base_size = 16) override; + virtual RID create_font_bitmap(float p_height, float p_ascent, int p_base_size = 16) override; + + virtual void font_bitmap_add_texture(RID p_font, const Ref &p_texture) override; + virtual void font_bitmap_add_char(RID p_font, char32_t p_char, int p_texture_idx, const Rect2 &p_rect, const Size2 &p_align, float p_advance) override; + virtual void font_bitmap_add_kerning_pair(RID p_font, char32_t p_A, char32_t p_B, int p_kerning) override; virtual float font_get_height(RID p_font, int p_size) const override; virtual float font_get_ascent(RID p_font, int p_size) const override; diff --git a/scene/resources/default_theme/default_theme.cpp b/scene/resources/default_theme/default_theme.cpp index e217bc94b0b..83ce243d3bc 100644 --- a/scene/resources/default_theme/default_theme.cpp +++ b/scene/resources/default_theme/default_theme.cpp @@ -128,6 +128,38 @@ static Ref flip_icon(Ref p_texture, bool p_flip_y = false, return texture; } +static Ref make_font(int p_height, int p_ascent, int p_charcount, const int *p_char_rects, int p_kerning_count, const int *p_kernings, int p_w, int p_h, const unsigned char *p_img) { + Ref font(memnew(FontData)); + font->new_bitmap(p_height, p_ascent, p_height); + + Ref image = memnew(Image(p_img)); + Ref tex = memnew(ImageTexture); + tex->create_from_image(image); + + font->bitmap_add_texture(tex); + + for (int i = 0; i < p_charcount; i++) { + const int *c = &p_char_rects[i * 8]; + + int chr = c[0]; + Rect2 frect; + frect.position.x = c[1]; + frect.position.y = c[2]; + frect.size.x = c[3]; + frect.size.y = c[4]; + Point2 align(c[6], c[5]); + int advance = c[7]; + + font->bitmap_add_char(chr, 0, frect, align, advance); + } + + for (int i = 0; i < p_kerning_count; i++) { + font->bitmap_add_kerning_pair(p_kernings[i * 3 + 0], p_kernings[i * 3 + 1], p_kernings[i * 3 + 2]); + } + + return font; +} + static Ref make_empty_stylebox(float p_margin_left = -1, float p_margin_top = -1, float p_margin_right = -1, float p_margin_bottom = -1) { Ref style(memnew(StyleBoxEmpty)); @@ -987,41 +1019,11 @@ void make_default_theme(bool p_hidpi, Ref p_font) { if (p_font.is_valid()) { default_font = p_font; } else if (p_hidpi) { - TextServer::BitmapFontData data; - data.height = _hidpi_font_height; - data.ascent = _hidpi_font_ascent; - data.charcount = _hidpi_font_charcount; - data.char_rects = &_hidpi_font_charrects[0][0]; - data.kerning_count = _hidpi_font_kerning_pair_count; - data.kernings = &_hidpi_font_kerning_pairs[0][0]; - data.w = _hidpi_font_img_width; - data.h = _hidpi_font_img_height; - data.img = _hidpi_font_img_data; - - Ref font_data; - font_data.instance(); - font_data->load_memory((const uint8_t *)&data, sizeof(data), "fnt"); - default_font_size = font_data->get_base_size(); - + Ref font_data = make_font(_hidpi_font_height, _hidpi_font_ascent, _hidpi_font_charcount, &_hidpi_font_charrects[0][0], _hidpi_font_kerning_pair_count, &_hidpi_font_kerning_pairs[0][0], _hidpi_font_img_width, _hidpi_font_img_height, _hidpi_font_img_data); default_font.instance(); default_font->add_data(font_data); } else { - TextServer::BitmapFontData data; - data.height = _lodpi_font_height; - data.ascent = _lodpi_font_ascent; - data.charcount = _lodpi_font_charcount; - data.char_rects = &_lodpi_font_charrects[0][0]; - data.kerning_count = _lodpi_font_kerning_pair_count; - data.kernings = &_lodpi_font_kerning_pairs[0][0]; - data.w = _lodpi_font_img_width; - data.h = _lodpi_font_img_height; - data.img = _lodpi_font_img_data; - - Ref font_data; - font_data.instance(); - font_data->load_memory((const uint8_t *)&data, sizeof(data), "fnt"); - default_font_size = font_data->get_base_size(); - + Ref font_data = make_font(_lodpi_font_height, _lodpi_font_ascent, _lodpi_font_charcount, &_lodpi_font_charrects[0][0], _lodpi_font_kerning_pair_count, &_lodpi_font_kerning_pairs[0][0], _lodpi_font_img_width, _lodpi_font_img_height, _lodpi_font_img_data); default_font.instance(); default_font->add_data(font_data); } diff --git a/scene/resources/font.cpp b/scene/resources/font.cpp index 06a15e36d7d..5acb75c3ce0 100644 --- a/scene/resources/font.cpp +++ b/scene/resources/font.cpp @@ -39,6 +39,11 @@ void FontData::_bind_methods() { ClassDB::bind_method(D_METHOD("load_resource", "filename", "base_size"), &FontData::load_resource, DEFVAL(16)); ClassDB::bind_method(D_METHOD("load_memory", "data", "type", "base_size"), &FontData::_load_memory, DEFVAL(16)); + ClassDB::bind_method(D_METHOD("new_bitmap", "height", "ascent", "base_size"), &FontData::new_bitmap); + + ClassDB::bind_method(D_METHOD("bitmap_add_texture", "texture"), &FontData::bitmap_add_texture); + ClassDB::bind_method(D_METHOD("bitmap_add_char", "char", "texture_idx", "rect", "align", "advance"), &FontData::bitmap_add_char); + ClassDB::bind_method(D_METHOD("bitmap_add_kerning_pair", "A", "B", "kerning"), &FontData::bitmap_add_kerning_pair); ClassDB::bind_method(D_METHOD("set_data_path", "path"), &FontData::set_data_path); ClassDB::bind_method(D_METHOD("get_data_path"), &FontData::get_data_path); @@ -219,6 +224,34 @@ void FontData::load_memory(const uint8_t *p_data, size_t p_size, const String &p emit_changed(); } +void FontData::new_bitmap(float p_height, float p_ascent, int p_base_size) { + if (rid != RID()) { + TS->free(rid); + } + rid = TS->create_font_bitmap(p_height, p_ascent, p_base_size); + path = TTR("(Bitmap: " + String::num_int64(rid.get_id(), 16, true) + ")"); + base_size = TS->font_get_base_size(rid); + emit_changed(); +} + +void FontData::bitmap_add_texture(const Ref &p_texture) { + if (rid != RID()) { + TS->font_bitmap_add_texture(rid, p_texture); + } +} + +void FontData::bitmap_add_char(char32_t p_char, int p_texture_idx, const Rect2 &p_rect, const Size2 &p_align, float p_advance) { + if (rid != RID()) { + TS->font_bitmap_add_char(rid, p_char, p_texture_idx, p_rect, p_align, p_advance); + } +} + +void FontData::bitmap_add_kerning_pair(char32_t p_A, char32_t p_B, int p_kerning) { + if (rid != RID()) { + TS->font_bitmap_add_kerning_pair(rid, p_A, p_B, p_kerning); + } +} + void FontData::set_data_path(const String &p_path) { load_resource(p_path, base_size); } diff --git a/scene/resources/font.h b/scene/resources/font.h index a91c9ec7a51..230ea26c7fc 100644 --- a/scene/resources/font.h +++ b/scene/resources/font.h @@ -62,6 +62,12 @@ public: void load_memory(const uint8_t *p_data, size_t p_size, const String &p_type, int p_base_size = 16); void _load_memory(const PackedByteArray &p_data, const String &p_type, int p_base_size = 16); + void new_bitmap(float p_height, float p_ascent, int p_base_size = 16); + + void bitmap_add_texture(const Ref &p_texture); + void bitmap_add_char(char32_t p_char, int p_texture_idx, const Rect2 &p_rect, const Size2 &p_align, float p_advance); + void bitmap_add_kerning_pair(char32_t p_A, char32_t p_B, int p_kerning); + void set_data_path(const String &p_path); String get_data_path() const; diff --git a/servers/text_server.cpp b/servers/text_server.cpp index da68ceb1282..091e230e6d2 100644 --- a/servers/text_server.cpp +++ b/servers/text_server.cpp @@ -219,6 +219,11 @@ void TextServer::_bind_methods() { ClassDB::bind_method(D_METHOD("create_font_system", "name", "base_size"), &TextServer::create_font_system, DEFVAL(16)); ClassDB::bind_method(D_METHOD("create_font_resource", "filename", "base_size"), &TextServer::create_font_resource, DEFVAL(16)); ClassDB::bind_method(D_METHOD("create_font_memory", "data", "type", "base_size"), &TextServer::_create_font_memory, DEFVAL(16)); + ClassDB::bind_method(D_METHOD("create_font_bitmap", "height", "ascent", "base_size"), &TextServer::create_font_bitmap); + + ClassDB::bind_method(D_METHOD("font_bitmap_add_texture", "font", "texture"), &TextServer::font_bitmap_add_texture); + ClassDB::bind_method(D_METHOD("font_bitmap_add_char", "font", "char", "texture_idx", "rect", "align", "advance"), &TextServer::font_bitmap_add_char); + ClassDB::bind_method(D_METHOD("font_bitmap_add_kerning_pair", "font", "A", "B", "kerning"), &TextServer::font_bitmap_add_kerning_pair); ClassDB::bind_method(D_METHOD("font_get_height", "font", "size"), &TextServer::font_get_height); ClassDB::bind_method(D_METHOD("font_get_ascent", "font", "size"), &TextServer::font_get_ascent); diff --git a/servers/text_server.h b/servers/text_server.h index 23367de4c83..116b25f9d5c 100644 --- a/servers/text_server.h +++ b/servers/text_server.h @@ -192,18 +192,6 @@ public: Vector glyphs_logical; }; - struct BitmapFontData { - int height = 0; - int ascent = 0; - int charcount = 0; - const int *char_rects = nullptr; - int kerning_count = 0; - const int *kernings = nullptr; - int w = 0; - int h = 0; - const unsigned char *img = nullptr; - }; - protected: static void _bind_methods(); @@ -236,6 +224,11 @@ public: virtual RID create_font_system(const String &p_name, int p_base_size = 16) = 0; virtual RID create_font_resource(const String &p_filename, int p_base_size = 16) = 0; virtual RID create_font_memory(const uint8_t *p_data, size_t p_size, const String &p_type, int p_base_size = 16) = 0; + virtual RID create_font_bitmap(float p_height, float p_ascent, int p_base_size = 16) = 0; + + virtual void font_bitmap_add_texture(RID p_font, const Ref &p_texture) = 0; + virtual void font_bitmap_add_char(RID p_font, char32_t p_char, int p_texture_idx, const Rect2 &p_rect, const Size2 &p_align, float p_advance) = 0; + virtual void font_bitmap_add_kerning_pair(RID p_font, char32_t p_A, char32_t p_B, int p_kerning) = 0; virtual float font_get_height(RID p_font, int p_size) const = 0; virtual float font_get_ascent(RID p_font, int p_size) const = 0;