diff options
author | RĂ©mi Verschelde <remi@verschelde.fr> | 2022-06-13 11:13:05 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-06-13 11:13:05 +0200 |
commit | 136f84fc3549fa22b700d80f86216c484174ffb6 (patch) | |
tree | c162d7045779cd4d03a50607e8a24cc265dcda75 /modules | |
parent | e95f4eb5a7d9bf644cb0aea218683d522297ae83 (diff) | |
parent | 6e4cdad3ac5b0eac89a381575ec297912c330bec (diff) |
Merge pull request #61772 from bruvzg/ft_ot_collect
Diffstat (limited to 'modules')
-rw-r--r-- | modules/text_server_adv/text_server_adv.cpp | 74 | ||||
-rw-r--r-- | modules/text_server_adv/text_server_adv.h | 6 | ||||
-rw-r--r-- | modules/text_server_fb/text_server_fb.cpp | 74 | ||||
-rw-r--r-- | modules/text_server_fb/text_server_fb.h | 6 |
4 files changed, 158 insertions, 2 deletions
diff --git a/modules/text_server_adv/text_server_adv.cpp b/modules/text_server_adv/text_server_adv.cpp index 3573e200e8..f7d6ce87dc 100644 --- a/modules/text_server_adv/text_server_adv.cpp +++ b/modules/text_server_adv/text_server_adv.cpp @@ -1288,7 +1288,16 @@ _FORCE_INLINE_ bool TextServerAdvanced::_ensure_cache_for_size(FontDataAdvanced fargs.memory_size = p_font_data->data_size; fargs.flags = FT_OPEN_MEMORY; fargs.stream = &fd->stream; - error = FT_Open_Face(ft_library, &fargs, 0, &fd->face); + + int max_index = 0; + FT_Face tmp_face; + error = FT_Open_Face(ft_library, &fargs, -1, &tmp_face); + if (error == 0) { + max_index = tmp_face->num_faces - 1; + } + FT_Done_Face(tmp_face); + + error = FT_Open_Face(ft_library, &fargs, CLAMP(p_font_data->face_index, 0, max_index), &fd->face); if (error) { FT_Done_Face(fd->face); fd->face = nullptr; @@ -1720,6 +1729,69 @@ void TextServerAdvanced::font_set_data_ptr(const RID &p_font_rid, const uint8_t fd->data_size = p_data_size; } +void TextServerAdvanced::font_set_face_index(const RID &p_font_rid, int64_t p_face_index) { + ERR_FAIL_COND(p_face_index < 0); + ERR_FAIL_COND(p_face_index >= 0x7FFF); + + FontDataAdvanced *fd = font_owner.get_or_null(p_font_rid); + ERR_FAIL_COND(!fd); + + MutexLock lock(fd->mutex); + if (fd->face_index != p_face_index) { + fd->face_index = p_face_index; + _font_clear_cache(fd); + } +} + +int64_t TextServerAdvanced::font_get_face_index(const RID &p_font_rid) const { + FontDataAdvanced *fd = font_owner.get_or_null(p_font_rid); + ERR_FAIL_COND_V(!fd, 0); + + MutexLock lock(fd->mutex); + return fd->face_index; +} + +int64_t TextServerAdvanced::font_get_face_count(const RID &p_font_rid) const { + FontDataAdvanced *fd = font_owner.get_or_null(p_font_rid); + ERR_FAIL_COND_V(!fd, 0); + + MutexLock lock(fd->mutex); + int face_count = 0; + + if (fd->data_ptr && (fd->data_size > 0)) { + // Init dynamic font. +#ifdef MODULE_FREETYPE_ENABLED + int error = 0; + if (!ft_library) { + error = FT_Init_FreeType(&ft_library); + ERR_FAIL_COND_V_MSG(error != 0, false, "FreeType: Error initializing library: '" + String(FT_Error_String(error)) + "'."); + } + + FT_StreamRec stream; + memset(&stream, 0, sizeof(FT_StreamRec)); + stream.base = (unsigned char *)fd->data_ptr; + stream.size = fd->data_size; + stream.pos = 0; + + FT_Open_Args fargs; + memset(&fargs, 0, sizeof(FT_Open_Args)); + fargs.memory_base = (unsigned char *)fd->data_ptr; + fargs.memory_size = fd->data_size; + fargs.flags = FT_OPEN_MEMORY; + fargs.stream = &stream; + + FT_Face tmp_face; + error = FT_Open_Face(ft_library, &fargs, -1, &tmp_face); + if (error == 0) { + face_count = tmp_face->num_faces; + } + FT_Done_Face(tmp_face); +#endif + } + + return face_count; +} + void TextServerAdvanced::font_set_style(const RID &p_font_rid, int64_t /*FontStyle*/ p_style) { FontDataAdvanced *fd = font_owner.get_or_null(p_font_rid); ERR_FAIL_COND(!fd); diff --git a/modules/text_server_adv/text_server_adv.h b/modules/text_server_adv/text_server_adv.h index 62f94472b5..c72454d0bb 100644 --- a/modules/text_server_adv/text_server_adv.h +++ b/modules/text_server_adv/text_server_adv.h @@ -247,6 +247,7 @@ class TextServerAdvanced : public TextServerExtension { PackedByteArray data; const uint8_t *data_ptr; size_t data_size; + int face_index = 0; mutable ThreadWorkPool work_pool; ~FontDataAdvanced() { @@ -473,6 +474,11 @@ public: virtual void font_set_data(const RID &p_font_rid, const PackedByteArray &p_data) override; virtual void font_set_data_ptr(const RID &p_font_rid, const uint8_t *p_data_ptr, int64_t p_data_size) override; + virtual void font_set_face_index(const RID &p_font_rid, int64_t p_index) override; + virtual int64_t font_get_face_index(const RID &p_font_rid) const override; + + virtual int64_t font_get_face_count(const RID &p_font_rid) const override; + virtual void font_set_style(const RID &p_font_rid, int64_t /*FontStyle*/ p_style) override; virtual int64_t /*FontStyle*/ font_get_style(const RID &p_font_rid) const override; diff --git a/modules/text_server_fb/text_server_fb.cpp b/modules/text_server_fb/text_server_fb.cpp index 498b58175e..1863baf769 100644 --- a/modules/text_server_fb/text_server_fb.cpp +++ b/modules/text_server_fb/text_server_fb.cpp @@ -733,7 +733,16 @@ _FORCE_INLINE_ bool TextServerFallback::_ensure_cache_for_size(FontDataFallback fargs.memory_size = p_font_data->data_size; fargs.flags = FT_OPEN_MEMORY; fargs.stream = &fd->stream; - error = FT_Open_Face(ft_library, &fargs, 0, &fd->face); + + int max_index = 0; + FT_Face tmp_face; + error = FT_Open_Face(ft_library, &fargs, -1, &tmp_face); + if (error == 0) { + max_index = tmp_face->num_faces - 1; + } + FT_Done_Face(tmp_face); + + error = FT_Open_Face(ft_library, &fargs, CLAMP(p_font_data->face_index, 0, max_index), &fd->face); if (error) { FT_Done_Face(fd->face); fd->face = nullptr; @@ -892,6 +901,69 @@ void TextServerFallback::font_set_style(const RID &p_font_rid, int64_t /*FontSty fd->style_flags = p_style; } +void TextServerFallback::font_set_face_index(const RID &p_font_rid, int64_t p_face_index) { + ERR_FAIL_COND(p_face_index < 0); + ERR_FAIL_COND(p_face_index >= 0x7FFF); + + FontDataFallback *fd = font_owner.get_or_null(p_font_rid); + ERR_FAIL_COND(!fd); + + MutexLock lock(fd->mutex); + if (fd->face_index != p_face_index) { + fd->face_index = p_face_index; + _font_clear_cache(fd); + } +} + +int64_t TextServerFallback::font_get_face_index(const RID &p_font_rid) const { + FontDataFallback *fd = font_owner.get_or_null(p_font_rid); + ERR_FAIL_COND_V(!fd, 0); + + MutexLock lock(fd->mutex); + return fd->face_index; +} + +int64_t TextServerFallback::font_get_face_count(const RID &p_font_rid) const { + FontDataFallback *fd = font_owner.get_or_null(p_font_rid); + ERR_FAIL_COND_V(!fd, 0); + + MutexLock lock(fd->mutex); + int face_count = 0; + + if (fd->data_ptr && (fd->data_size > 0)) { + // Init dynamic font. +#ifdef MODULE_FREETYPE_ENABLED + int error = 0; + if (!ft_library) { + error = FT_Init_FreeType(&ft_library); + ERR_FAIL_COND_V_MSG(error != 0, false, "FreeType: Error initializing library: '" + String(FT_Error_String(error)) + "'."); + } + + FT_StreamRec stream; + memset(&stream, 0, sizeof(FT_StreamRec)); + stream.base = (unsigned char *)fd->data_ptr; + stream.size = fd->data_size; + stream.pos = 0; + + FT_Open_Args fargs; + memset(&fargs, 0, sizeof(FT_Open_Args)); + fargs.memory_base = (unsigned char *)fd->data_ptr; + fargs.memory_size = fd->data_size; + fargs.flags = FT_OPEN_MEMORY; + fargs.stream = &stream; + + FT_Face tmp_face; + error = FT_Open_Face(ft_library, &fargs, -1, &tmp_face); + if (error == 0) { + face_count = tmp_face->num_faces; + } + FT_Done_Face(tmp_face); +#endif + } + + return face_count; +} + int64_t /*FontStyle*/ TextServerFallback::font_get_style(const RID &p_font_rid) const { FontDataFallback *fd = font_owner.get_or_null(p_font_rid); ERR_FAIL_COND_V(!fd, 0); diff --git a/modules/text_server_fb/text_server_fb.h b/modules/text_server_fb/text_server_fb.h index f3d516edea..c085b58898 100644 --- a/modules/text_server_fb/text_server_fb.h +++ b/modules/text_server_fb/text_server_fb.h @@ -209,6 +209,7 @@ class TextServerFallback : public TextServerExtension { PackedByteArray data; const uint8_t *data_ptr; size_t data_size; + int face_index = 0; mutable ThreadWorkPool work_pool; @@ -364,6 +365,11 @@ public: virtual void font_set_data(const RID &p_font_rid, const PackedByteArray &p_data) override; virtual void font_set_data_ptr(const RID &p_font_rid, const uint8_t *p_data_ptr, int64_t p_data_size) override; + virtual void font_set_face_index(const RID &p_font_rid, int64_t p_index) override; + virtual int64_t font_get_face_index(const RID &p_font_rid) const override; + + virtual int64_t font_get_face_count(const RID &p_font_rid) const override; + virtual void font_set_style(const RID &p_font_rid, int64_t /*FontStyle*/ p_style) override; virtual int64_t /*FontStyle*/ font_get_style(const RID &p_font_rid) const override; |