diff options
Diffstat (limited to 'modules/text_server_fb')
-rw-r--r-- | modules/text_server_fb/bitmap_font_fb.cpp | 85 | ||||
-rw-r--r-- | modules/text_server_fb/bitmap_font_fb.h | 6 | ||||
-rw-r--r-- | modules/text_server_fb/config.py | 2 | ||||
-rw-r--r-- | modules/text_server_fb/dynamic_font_fb.cpp | 9 | ||||
-rw-r--r-- | modules/text_server_fb/dynamic_font_fb.h | 6 | ||||
-rw-r--r-- | modules/text_server_fb/font_fb.h | 23 | ||||
-rw-r--r-- | modules/text_server_fb/text_server_fb.cpp | 82 | ||||
-rw-r--r-- | modules/text_server_fb/text_server_fb.h | 11 |
8 files changed, 168 insertions, 56 deletions
diff --git a/modules/text_server_fb/bitmap_font_fb.cpp b/modules/text_server_fb/bitmap_font_fb.cpp index c9a9cc6eba..c58f8cbba1 100644 --- a/modules/text_server_fb/bitmap_font_fb.cpp +++ b/modules/text_server_fb/bitmap_font_fb.cpp @@ -175,61 +175,58 @@ 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); +Error BitmapFontDataFallback::bitmap_new(float p_height, float p_ascent, int p_base_size) { + height = p_height; + ascent = p_ascent; - const TextServer::BitmapFontData *data = (const TextServer::BitmapFontData *)p_data; + base_size = p_base_size; + if (base_size == 0) { + base_size = height; + } - if (RenderingServer::get_singleton() != nullptr) { - Ref<Image> image = memnew(Image(data->img)); - Ref<ImageTexture> tex = memnew(ImageTexture); - tex->create_from_image(image); + char_map.clear(); + textures.clear(); + kerning_map.clear(); - textures.push_back(tex); - } + valid = true; - 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; - } + return OK; +} - 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]; +void BitmapFontDataFallback::bitmap_add_texture(const Ref<Texture> &p_texture) { + ERR_FAIL_COND(!valid); + ERR_FAIL_COND_MSG(p_texture.is_null(), "It's not a reference to a valid Texture object."); - if (data->kernings[i * 3 + 2] == 0 && kerning_map.has(kpk)) { - kerning_map.erase(kpk); - } else { - kerning_map[kpk] = data->kernings[i * 3 + 2]; - } - } + textures.push_back(p_texture); +} - height = data->height; - ascent = data->ascent; +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); - base_size = p_base_size; - if (base_size == 0) { - base_size = height; + 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; +} - valid = true; +void BitmapFontDataFallback::bitmap_add_kerning_pair(char32_t p_A, char32_t p_B, int p_kerning) { + ERR_FAIL_COND(!valid); - return OK; + 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 { diff --git a/modules/text_server_fb/bitmap_font_fb.h b/modules/text_server_fb/bitmap_font_fb.h index 33401b85fa..7cd7507ebc 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<Texture> &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/config.py b/modules/text_server_fb/config.py index 491377a369..7a73080ae9 100644 --- a/modules/text_server_fb/config.py +++ b/modules/text_server_fb/config.py @@ -1,5 +1,5 @@ def can_build(env, platform): - return env.module_check_dependencies("text_server_fb", ["freetype"]) + return True def configure(env): diff --git a/modules/text_server_fb/dynamic_font_fb.cpp b/modules/text_server_fb/dynamic_font_fb.cpp index df7756cbd0..66d36bc885 100644 --- a/modules/text_server_fb/dynamic_font_fb.cpp +++ b/modules/text_server_fb/dynamic_font_fb.cpp @@ -30,6 +30,8 @@ #include "dynamic_font_fb.h" +#ifdef MODULE_FREETYPE_ENABLED + #include FT_STROKER_H #include FT_ADVANCES_H @@ -124,8 +126,9 @@ DynamicFontDataFallback::DataAtSize *DynamicFontDataFallback::get_data_for_size( fds->size = p_size; fds->ascent = (fds->face->size->metrics.ascender / 64.0) / oversampling * fds->scale_color_font; fds->descent = (-fds->face->size->metrics.descender / 64.0) / oversampling * fds->scale_color_font; - fds->underline_position = -fds->face->underline_position / 64.0 / oversampling * fds->scale_color_font; - fds->underline_thickness = fds->face->underline_thickness / 64.0 / oversampling * fds->scale_color_font; + fds->underline_position = (-FT_MulFix(fds->face->underline_position, fds->face->size->metrics.y_scale) / 64.0) / oversampling * fds->scale_color_font; + fds->underline_thickness = (FT_MulFix(fds->face->underline_thickness, fds->face->size->metrics.y_scale) / 64.0) / oversampling * fds->scale_color_font; + if (p_outline_size != 0) { size_cache_outline[id] = fds; } else { @@ -683,3 +686,5 @@ DynamicFontDataFallback::~DynamicFontDataFallback() { FT_Done_FreeType(library); } } + +#endif // MODULE_FREETYPE_ENABLED diff --git a/modules/text_server_fb/dynamic_font_fb.h b/modules/text_server_fb/dynamic_font_fb.h index 81b18f6af3..eb70f46666 100644 --- a/modules/text_server_fb/dynamic_font_fb.h +++ b/modules/text_server_fb/dynamic_font_fb.h @@ -33,6 +33,10 @@ #include "font_fb.h" +#include "modules/modules_enabled.gen.h" + +#ifdef MODULE_FREETYPE_ENABLED + #include <ft2build.h> #include FT_FREETYPE_H @@ -163,4 +167,6 @@ public: virtual ~DynamicFontDataFallback() override; }; +#endif // MODULE_FREETYPE_ENABLED + #endif // DYNAMIC_FONT_FALLBACK_H diff --git a/modules/text_server_fb/font_fb.h b/modules/text_server_fb/font_fb.h index cc72919542..218f3df03a 100644 --- a/modules/text_server_fb/font_fb.h +++ b/modules/text_server_fb/font_fb.h @@ -37,11 +37,18 @@ struct FontDataFallback { Map<String, bool> lang_support_overrides; Map<String, bool> script_support_overrides; bool valid = false; + int spacing_space = 0; + int spacing_glyph = 0; 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<Texture> &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; @@ -50,6 +57,18 @@ struct FontDataFallback { virtual float get_underline_position(int p_size) const = 0; virtual float get_underline_thickness(int p_size) const = 0; + virtual int get_spacing_space() const { return spacing_space; }; + virtual void set_spacing_space(int p_value) { + spacing_space = p_value; + clear_cache(); + }; + + virtual int get_spacing_glyph() const { return spacing_glyph; }; + virtual void set_spacing_glyph(int p_value) { + spacing_glyph = p_value; + clear_cache(); + }; + virtual void set_antialiased(bool p_antialiased) = 0; virtual bool get_antialiased() const = 0; diff --git a/modules/text_server_fb/text_server_fb.cpp b/modules/text_server_fb/text_server_fb.cpp index e60d269408..60ab14738a 100644 --- a/modules/text_server_fb/text_server_fb.cpp +++ b/modules/text_server_fb/text_server_fb.cpp @@ -107,10 +107,12 @@ RID TextServerFallback::create_font_system(const String &p_name, int p_base_size RID TextServerFallback::create_font_resource(const String &p_filename, int p_base_size) { _THREAD_SAFE_METHOD_ FontDataFallback *fd = nullptr; - if (p_filename.get_extension() == "ttf" || p_filename.get_extension() == "otf" || p_filename.get_extension() == "woff") { - fd = memnew(DynamicFontDataFallback); - } else if (p_filename.get_extension() == "fnt" || p_filename.get_extension() == "font") { + if (p_filename.get_extension() == "fnt" || p_filename.get_extension() == "font") { fd = memnew(BitmapFontDataFallback); +#ifdef MODULE_FREETYPE_ENABLED + } else if (p_filename.get_extension() == "ttf" || p_filename.get_extension() == "otf" || p_filename.get_extension() == "woff") { + fd = memnew(DynamicFontDataFallback); +#endif } else { return RID(); } @@ -127,10 +129,12 @@ RID TextServerFallback::create_font_resource(const String &p_filename, int p_bas RID TextServerFallback::create_font_memory(const uint8_t *p_data, size_t p_size, const String &p_type, int p_base_size) { _THREAD_SAFE_METHOD_ FontDataFallback *fd = nullptr; - if (p_type == "ttf" || p_type == "otf" || p_type == "woff") { - fd = memnew(DynamicFontDataFallback); - } else if (p_type == "fnt" || p_type == "font") { + if (p_type == "fnt" || p_type == "font") { fd = memnew(BitmapFontDataFallback); +#ifdef MODULE_FREETYPE_ENABLED + } else if (p_type == "ttf" || p_type == "otf" || p_type == "woff") { + fd = memnew(DynamicFontDataFallback); +#endif } else { return RID(); } @@ -144,6 +148,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<Texture> &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); @@ -179,6 +216,34 @@ float TextServerFallback::font_get_underline_thickness(RID p_font, int p_size) c return fd->get_underline_thickness(p_size); } +int TextServerFallback::font_get_spacing_space(RID p_font) const { + _THREAD_SAFE_METHOD_ + const FontDataFallback *fd = font_owner.getornull(p_font); + ERR_FAIL_COND_V(!fd, 0); + return fd->get_spacing_space(); +} + +void TextServerFallback::font_set_spacing_space(RID p_font, int p_value) { + _THREAD_SAFE_METHOD_ + FontDataFallback *fd = font_owner.getornull(p_font); + ERR_FAIL_COND(!fd); + fd->set_spacing_space(p_value); +} + +int TextServerFallback::font_get_spacing_glyph(RID p_font) const { + _THREAD_SAFE_METHOD_ + const FontDataFallback *fd = font_owner.getornull(p_font); + ERR_FAIL_COND_V(!fd, 0); + return fd->get_spacing_glyph(); +} + +void TextServerFallback::font_set_spacing_glyph(RID p_font, int p_value) { + _THREAD_SAFE_METHOD_ + FontDataFallback *fd = font_owner.getornull(p_font); + ERR_FAIL_COND(!fd); + fd->set_spacing_glyph(p_value); +} + void TextServerFallback::font_set_antialiased(RID p_font, bool p_antialiased) { _THREAD_SAFE_METHOD_ FontDataFallback *fd = font_owner.getornull(p_font); @@ -1184,6 +1249,11 @@ bool TextServerFallback::shaped_text_shape(RID p_shaped) { sd->descent = MAX(sd->descent, Math::round(fd->get_advance(gl.index, gl.font_size).x * 0.5)); } } + if (fd->get_spacing_space() && is_whitespace(sd->text[j])) { + gl.advance += fd->get_spacing_space(); + } else { + gl.advance += fd->get_spacing_glyph(); + } sd->upos = MAX(sd->upos, fd->get_underline_position(gl.font_size)); sd->uthk = MAX(sd->uthk, fd->get_underline_thickness(gl.font_size)); diff --git a/modules/text_server_fb/text_server_fb.h b/modules/text_server_fb/text_server_fb.h index d142b320e4..b10369d172 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<Texture> &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; @@ -89,6 +94,12 @@ public: virtual float font_get_underline_position(RID p_font, int p_size) const override; virtual float font_get_underline_thickness(RID p_font, int p_size) const override; + virtual int font_get_spacing_space(RID p_font) const override; + virtual void font_set_spacing_space(RID p_font, int p_value) override; + + virtual int font_get_spacing_glyph(RID p_font) const override; + virtual void font_set_spacing_glyph(RID p_font, int p_value) override; + virtual void font_set_antialiased(RID p_font, bool p_antialiased) override; virtual bool font_get_antialiased(RID p_font) const override; |