Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
godotengine
GitHub Repository: godotengine/godot
Path: blob/master/modules/text_server_adv/script_iterator.cpp
11351 views
1
/**************************************************************************/
2
/* script_iterator.cpp */
3
/**************************************************************************/
4
/* This file is part of: */
5
/* GODOT ENGINE */
6
/* https://godotengine.org */
7
/**************************************************************************/
8
/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
9
/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
10
/* */
11
/* Permission is hereby granted, free of charge, to any person obtaining */
12
/* a copy of this software and associated documentation files (the */
13
/* "Software"), to deal in the Software without restriction, including */
14
/* without limitation the rights to use, copy, modify, merge, publish, */
15
/* distribute, sublicense, and/or sell copies of the Software, and to */
16
/* permit persons to whom the Software is furnished to do so, subject to */
17
/* the following conditions: */
18
/* */
19
/* The above copyright notice and this permission notice shall be */
20
/* included in all copies or substantial portions of the Software. */
21
/* */
22
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
23
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
24
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
25
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
26
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
27
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
28
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
29
/**************************************************************************/
30
31
#include "script_iterator.h"
32
33
// This implementation is derived from ICU: icu4c/source/extra/scrptrun/scrptrun.cpp
34
35
inline bool ScriptIterator::same_script(int32_t p_script_one, int32_t p_script_two) {
36
return p_script_one <= USCRIPT_INHERITED || p_script_two <= USCRIPT_INHERITED || p_script_one == p_script_two;
37
}
38
39
inline bool ScriptIterator::is_emoji(UChar32 p_c, UChar32 p_next) {
40
if (p_next == 0xFE0E) { // Variation Selector-15
41
return false;
42
} else if (p_next == 0xFE0F) { // Variation Selector-16
43
return true;
44
} else {
45
return u_hasBinaryProperty(p_c, UCHAR_EMOJI) || u_hasBinaryProperty(p_c, UCHAR_EMOJI_PRESENTATION) || u_hasBinaryProperty(p_c, UCHAR_EMOJI_MODIFIER) || u_hasBinaryProperty(p_c, UCHAR_REGIONAL_INDICATOR) || u_hasBinaryProperty(p_c, UCHAR_EXTENDED_PICTOGRAPHIC);
46
}
47
}
48
49
ScriptIterator::ScriptIterator(const String &p_string, int p_start, int p_length) {
50
struct ParenStackEntry {
51
int pair_index;
52
UScriptCode script_code;
53
};
54
55
if (p_start >= p_length) {
56
p_start = p_length - 1;
57
}
58
59
if (p_start < 0) {
60
p_start = 0;
61
}
62
63
int paren_size = PAREN_STACK_DEPTH;
64
ParenStackEntry *paren_stack = static_cast<ParenStackEntry *>(memalloc(paren_size * sizeof(ParenStackEntry)));
65
66
int script_start;
67
int script_end = p_start;
68
UScriptCode script_code;
69
int paren_sp = -1;
70
int start_sp = paren_sp;
71
UErrorCode err = U_ZERO_ERROR;
72
const char32_t *str = p_string.ptr();
73
74
do {
75
script_code = USCRIPT_COMMON;
76
for (script_start = script_end; script_end < p_length; script_end++) {
77
UChar32 ch = str[script_end];
78
UChar32 n = (script_end + 1 < p_length) ? str[script_end + 1] : 0;
79
UScriptCode sc = uscript_getScript(ch, &err);
80
if (U_FAILURE(err)) {
81
memfree(paren_stack);
82
ERR_FAIL_MSG(u_errorName(err));
83
}
84
if (is_emoji(ch, n)) {
85
sc = USCRIPT_SYMBOLS_EMOJI;
86
}
87
88
if (u_getIntPropertyValue(ch, UCHAR_BIDI_PAIRED_BRACKET_TYPE) != U_BPT_NONE) {
89
if (u_getIntPropertyValue(ch, UCHAR_BIDI_PAIRED_BRACKET_TYPE) == U_BPT_OPEN) {
90
// If it's an open character, push it onto the stack.
91
paren_sp++;
92
if (unlikely(paren_sp >= paren_size)) {
93
// If the stack is full, allocate more space to handle deeply nested parentheses. This is unlikely to happen with any real text.
94
paren_size += PAREN_STACK_DEPTH;
95
paren_stack = static_cast<ParenStackEntry *>(memrealloc(paren_stack, paren_size * sizeof(ParenStackEntry)));
96
}
97
paren_stack[paren_sp].pair_index = ch;
98
paren_stack[paren_sp].script_code = script_code;
99
} else if (paren_sp >= 0) {
100
// If it's a close character, find the matching open on the stack, and use that script code. Any non-matching open characters above it on the stack will be popped.
101
UChar32 paired_ch = u_getBidiPairedBracket(ch);
102
while (paren_sp >= 0 && paren_stack[paren_sp].pair_index != paired_ch) {
103
paren_sp -= 1;
104
}
105
if (paren_sp < start_sp) {
106
start_sp = paren_sp;
107
}
108
if (paren_sp >= 0) {
109
sc = paren_stack[paren_sp].script_code;
110
}
111
}
112
}
113
114
if (script_code == USCRIPT_SYMBOLS_EMOJI && script_code != sc) {
115
UCharCategory cat = (UCharCategory)u_charType(ch);
116
if ((cat >= U_SPACE_SEPARATOR && cat <= U_CONTROL_CHAR) || (cat >= U_DASH_PUNCTUATION && cat <= U_OTHER_PUNCTUATION) || (cat >= U_INITIAL_PUNCTUATION && cat <= U_FINAL_PUNCTUATION)) {
117
break;
118
}
119
} else if (same_script(script_code, sc)) {
120
if (script_code <= USCRIPT_INHERITED && sc > USCRIPT_INHERITED) {
121
script_code = sc;
122
// Now that we have a final script code, fix any open characters we pushed before we knew the script code.
123
while (start_sp < paren_sp) {
124
paren_stack[++start_sp].script_code = script_code;
125
}
126
}
127
if ((u_getIntPropertyValue(ch, UCHAR_BIDI_PAIRED_BRACKET_TYPE) == U_BPT_CLOSE) && paren_sp >= 0) {
128
// If this character is a close paired character pop the matching open character from the stack.
129
paren_sp -= 1;
130
if (start_sp >= 0) {
131
start_sp -= 1;
132
}
133
}
134
} else {
135
break;
136
}
137
}
138
139
ScriptRange rng;
140
rng.script = hb_icu_script_to_script(script_code);
141
rng.start = script_start;
142
rng.end = script_end;
143
144
script_ranges.push_back(rng);
145
} while (script_end < p_length);
146
147
memfree(paren_stack);
148
}
149
150