diff options
author | bruvzg <7645683+bruvzg@users.noreply.github.com> | 2021-11-04 14:33:37 +0200 |
---|---|---|
committer | bruvzg <7645683+bruvzg@users.noreply.github.com> | 2022-04-28 14:35:41 +0300 |
commit | 6ab672d1ef7ece5c3019d46aeb98df3686f37e26 (patch) | |
tree | be10d088e90c6a9e60efef823f54f9aa0d70aa07 /modules | |
parent | 3e1b824c050b765095285c67b3e4c8092e1f88c6 (diff) |
Implement text-to-speech support on Android, iOS, HTML5, Linux, macOS and Windows.
Implement TextServer word break method.
Diffstat (limited to 'modules')
-rw-r--r-- | modules/text_server_adv/text_server_adv.cpp | 60 | ||||
-rw-r--r-- | modules/text_server_adv/text_server_adv.h | 4 | ||||
-rw-r--r-- | modules/text_server_fb/text_server_fb.cpp | 25 | ||||
-rw-r--r-- | modules/text_server_fb/text_server_fb.h | 2 |
4 files changed, 90 insertions, 1 deletions
diff --git a/modules/text_server_adv/text_server_adv.cpp b/modules/text_server_adv/text_server_adv.cpp index 0ae8219e23..437fbe76ab 100644 --- a/modules/text_server_adv/text_server_adv.cpp +++ b/modules/text_server_adv/text_server_adv.cpp @@ -3255,6 +3255,19 @@ void TextServerAdvanced::font_set_global_oversampling(double p_oversampling) { /* Shaped text buffer interface */ /*************************************************************************/ +int64_t TextServerAdvanced::_convert_pos(const String &p_utf32, const Char16String &p_utf16, int64_t p_pos) const { + int64_t limit = p_pos; + if (p_utf32.length() != p_utf16.length()) { + const UChar *data = p_utf16.ptr(); + for (int i = 0; i < p_pos; i++) { + if (U16_IS_LEAD(data[i])) { + limit--; + } + } + } + return limit; +} + int64_t TextServerAdvanced::_convert_pos(const ShapedTextDataAdvanced *p_sd, int64_t p_pos) const { int64_t limit = p_pos; if (p_sd->text.length() != p_sd->utf16.length()) { @@ -5555,6 +5568,53 @@ String TextServerAdvanced::string_to_lower(const String &p_string, const String return String::utf16(lower.ptr(), len); } +PackedInt32Array TextServerAdvanced::string_get_word_breaks(const String &p_string, const String &p_language) const { + // Convert to UTF-16. + Char16String utf16 = p_string.utf16(); + + Set<int> breaks; + UErrorCode err = U_ZERO_ERROR; + UBreakIterator *bi = ubrk_open(UBRK_LINE, p_language.ascii().get_data(), (const UChar *)utf16.ptr(), utf16.length(), &err); + if (U_FAILURE(err)) { + // No data loaded - use fallback. + for (int i = 0; i < p_string.length(); i++) { + char32_t c = p_string[i]; + if (is_whitespace(c) || is_linebreak(c)) { + breaks.insert(i); + } + } + } else { + while (ubrk_next(bi) != UBRK_DONE) { + int pos = _convert_pos(p_string, utf16, ubrk_current(bi)) - 1; + if (pos != p_string.length() - 1) { + breaks.insert(pos); + } + } + } + ubrk_close(bi); + + PackedInt32Array ret; + for (int i = 0; i < p_string.length(); i++) { + char32_t c = p_string[i]; + if (c == 0xfffc) { + continue; + } + if (u_ispunct(c) && c != 0x005F) { + ret.push_back(i); + continue; + } + if (is_underscore(c)) { + ret.push_back(i); + continue; + } + if (breaks.has(i)) { + ret.push_back(i); + continue; + } + } + return ret; +} + TextServerAdvanced::TextServerAdvanced() { _insert_num_systems_lang(); _insert_feature_sets(); diff --git a/modules/text_server_adv/text_server_adv.h b/modules/text_server_adv/text_server_adv.h index fa59566a94..1b4293aa72 100644 --- a/modules/text_server_adv/text_server_adv.h +++ b/modules/text_server_adv/text_server_adv.h @@ -393,11 +393,13 @@ class TextServerAdvanced : public TextServerExtension { mutable RID_PtrOwner<ShapedTextDataAdvanced> shaped_owner; void _realign(ShapedTextDataAdvanced *p_sd) const; + int64_t _convert_pos(const String &p_utf32, const Char16String &p_utf16, int64_t p_pos) const; int64_t _convert_pos(const ShapedTextDataAdvanced *p_sd, int64_t p_pos) const; int64_t _convert_pos_inv(const ShapedTextDataAdvanced *p_sd, int64_t p_pos) const; bool _shape_substr(ShapedTextDataAdvanced *p_new_sd, const ShapedTextDataAdvanced *p_sd, int64_t p_start, int64_t p_length) const; void _shape_run(ShapedTextDataAdvanced *p_sd, int64_t p_start, int64_t p_end, hb_script_t p_script, hb_direction_t p_direction, Array p_fonts, int64_t p_span, int64_t p_fb_index); Glyph _shape_single_glyph(ShapedTextDataAdvanced *p_sd, char32_t p_char, hb_script_t p_script, hb_direction_t p_direction, const RID &p_font, int64_t p_font_size); + _FORCE_INLINE_ void _add_featuers(const Dictionary &p_source, Vector<hb_feature_t> &r_ftrs); // HarfBuzz bitmap font interface. @@ -686,6 +688,8 @@ public: virtual String parse_number(const String &p_string, const String &p_language = "") const override; virtual String percent_sign(const String &p_language = "") const override; + virtual PackedInt32Array string_get_word_breaks(const String &p_string, const String &p_language = "") const override; + virtual String strip_diacritics(const String &p_string) const override; virtual String string_to_upper(const String &p_string, const String &p_language = "") const override; diff --git a/modules/text_server_fb/text_server_fb.cpp b/modules/text_server_fb/text_server_fb.cpp index 1251aaf2b9..d84e9e581a 100644 --- a/modules/text_server_fb/text_server_fb.cpp +++ b/modules/text_server_fb/text_server_fb.cpp @@ -3079,7 +3079,7 @@ bool TextServerFallback::shaped_text_update_breaks(const RID &p_shaped) { if (sd_glyphs[i].count > 0) { char32_t c = sd->text[sd_glyphs[i].start - sd->start]; if (c_punct_size == 0) { - if (is_punct(c)) { + if (is_punct(c) && c != 0x005F) { sd_glyphs[i].flags |= GRAPHEME_IS_PUNCTUATION; } } else { @@ -3623,6 +3623,29 @@ String TextServerFallback::string_to_lower(const String &p_string, const String return lower; } +PackedInt32Array TextServerFallback::string_get_word_breaks(const String &p_string, const String &p_language) const { + PackedInt32Array ret; + for (int i = 0; i < p_string.length(); i++) { + char32_t c = p_string[i]; + if (c == 0xfffc) { + continue; + } + if (is_punct(c) && c != 0x005F) { + ret.push_back(i); + continue; + } + if (is_underscore(c)) { + ret.push_back(i); + continue; + } + if (is_whitespace(c) || is_linebreak(c)) { + ret.push_back(i); + continue; + } + } + return ret; +} + TextServerFallback::TextServerFallback() { _insert_feature_sets(); }; diff --git a/modules/text_server_fb/text_server_fb.h b/modules/text_server_fb/text_server_fb.h index d6f61e02f8..c837029623 100644 --- a/modules/text_server_fb/text_server_fb.h +++ b/modules/text_server_fb/text_server_fb.h @@ -573,6 +573,8 @@ public: virtual double shaped_text_get_underline_position(const RID &p_shaped) const override; virtual double shaped_text_get_underline_thickness(const RID &p_shaped) const override; + virtual PackedInt32Array string_get_word_breaks(const String &p_string, const String &p_language = "") const override; + virtual String string_to_upper(const String &p_string, const String &p_language = "") const override; virtual String string_to_lower(const String &p_string, const String &p_language = "") const override; |