Implement text-to-speech support on Android, iOS, HTML5, Linux, macOS and Windows.

Implement TextServer word break method.
This commit is contained in:
bruvzg
2021-11-04 14:33:37 +02:00
parent 3e1b824c05
commit 6ab672d1ef
54 changed files with 3962 additions and 2 deletions

View File

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