Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
godotengine
GitHub Repository: godotengine/godot
Path: blob/master/tests/servers/test_text_server.cpp
23449 views
1
/**************************************************************************/
2
/* test_text_server.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 "tests/test_macros.h"
32
33
TEST_FORCE_LINK(test_text_server)
34
35
#ifdef TOOLS_ENABLED
36
37
#include "core/variant/typed_array.h"
38
#include "editor/themes/builtin_fonts.gen.h"
39
#include "servers/text/text_server.h"
40
41
namespace TestTextServer {
42
43
TEST_SUITE("[TextServer]") {
44
TEST_CASE("[TextServer] Init, font loading and shaping") {
45
SUBCASE("[TextServer] Loading fonts") {
46
for (int i = 0; i < TextServerManager::get_singleton()->get_interface_count(); i++) {
47
Ref<TextServer> ts = TextServerManager::get_singleton()->get_interface(i);
48
CHECK_FALSE_MESSAGE(ts.is_null(), "Invalid TS interface.");
49
50
if (!ts->has_feature(TextServer::FEATURE_FONT_DYNAMIC)) {
51
continue;
52
}
53
54
RID font = ts->create_font();
55
ts->font_set_data_ptr(font, _font_Inter_Regular, _font_Inter_Regular_size);
56
CHECK_FALSE_MESSAGE(font == RID(), "Loading font failed.");
57
ts->free_rid(font);
58
}
59
}
60
61
SUBCASE("[TextServer] Text layout: Font fallback") {
62
for (int i = 0; i < TextServerManager::get_singleton()->get_interface_count(); i++) {
63
Ref<TextServer> ts = TextServerManager::get_singleton()->get_interface(i);
64
CHECK_FALSE_MESSAGE(ts.is_null(), "Invalid TS interface.");
65
66
if (!ts->has_feature(TextServer::FEATURE_FONT_DYNAMIC) || !ts->has_feature(TextServer::FEATURE_SIMPLE_LAYOUT)) {
67
continue;
68
}
69
70
RID font1 = ts->create_font();
71
ts->font_set_data_ptr(font1, _font_Inter_Regular, _font_Inter_Regular_size);
72
ts->font_set_allow_system_fallback(font1, false);
73
RID font2 = ts->create_font();
74
ts->font_set_data_ptr(font2, _font_NotoSansThai_Regular, _font_NotoSansThai_Regular_size);
75
ts->font_set_allow_system_fallback(font2, false);
76
77
Array font = { font1, font2 };
78
String test = U"คนอ้วน khon uan ראה";
79
// 6^ 17^
80
81
RID ctx = ts->create_shaped_text();
82
CHECK_FALSE_MESSAGE(ctx == RID(), "Creating text buffer failed.");
83
bool ok = ts->shaped_text_add_string(ctx, test, font, 16);
84
CHECK_FALSE_MESSAGE(!ok, "Adding text to the buffer failed.");
85
86
const Glyph *glyphs = ts->shaped_text_get_glyphs(ctx);
87
int gl_size = ts->shaped_text_get_glyph_count(ctx);
88
CHECK_FALSE_MESSAGE(gl_size == 0, "Shaping failed");
89
for (int j = 0; j < gl_size; j++) {
90
if (glyphs[j].start < 6) {
91
CHECK_FALSE_MESSAGE(glyphs[j].font_rid != font[1], "Incorrect font selected.");
92
}
93
if ((glyphs[j].start > 6) && (glyphs[j].start < 16)) {
94
CHECK_FALSE_MESSAGE(glyphs[j].font_rid != font[0], "Incorrect font selected.");
95
}
96
if (glyphs[j].start > 16) {
97
CHECK_FALSE_MESSAGE(glyphs[j].font_rid != RID(), "Incorrect font selected.");
98
CHECK_FALSE_MESSAGE(glyphs[j].index != test[glyphs[j].start], "Incorrect glyph index.");
99
}
100
CHECK_FALSE_MESSAGE((glyphs[j].start < 0 || glyphs[j].end > test.length()), "Incorrect glyph range.");
101
CHECK_FALSE_MESSAGE(glyphs[j].font_size != 16, "Incorrect glyph font size.");
102
}
103
104
ts->free_rid(ctx);
105
106
for (int j = 0; j < font.size(); j++) {
107
ts->free_rid(font[j]);
108
}
109
font.clear();
110
}
111
}
112
113
SUBCASE("[TextServer] Text layout: BiDi") {
114
for (int i = 0; i < TextServerManager::get_singleton()->get_interface_count(); i++) {
115
Ref<TextServer> ts = TextServerManager::get_singleton()->get_interface(i);
116
CHECK_FALSE_MESSAGE(ts.is_null(), "Invalid TS interface.");
117
118
if (!ts->has_feature(TextServer::FEATURE_FONT_DYNAMIC) || !ts->has_feature(TextServer::FEATURE_BIDI_LAYOUT)) {
119
continue;
120
}
121
122
RID font1 = ts->create_font();
123
ts->font_set_data_ptr(font1, _font_Inter_Regular, _font_Inter_Regular_size);
124
RID font2 = ts->create_font();
125
ts->font_set_data_ptr(font2, _font_Vazirmatn_Regular, _font_Vazirmatn_Regular_size);
126
127
Array font = { font1, font2 };
128
String test = U"Arabic (اَلْعَرَبِيَّةُ, al-ʿarabiyyah)";
129
// 7^ 26^
130
131
RID ctx = ts->create_shaped_text();
132
CHECK_FALSE_MESSAGE(ctx == RID(), "Creating text buffer failed.");
133
bool ok = ts->shaped_text_add_string(ctx, test, font, 16);
134
CHECK_FALSE_MESSAGE(!ok, "Adding text to the buffer failed.");
135
136
const Glyph *glyphs = ts->shaped_text_get_glyphs(ctx);
137
int gl_size = ts->shaped_text_get_glyph_count(ctx);
138
CHECK_FALSE_MESSAGE(gl_size == 0, "Shaping failed");
139
for (int j = 0; j < gl_size; j++) {
140
if (glyphs[j].count > 0) {
141
if (glyphs[j].start < 7) {
142
CHECK_FALSE_MESSAGE(((glyphs[j].flags & TextServer::GRAPHEME_IS_RTL) == TextServer::GRAPHEME_IS_RTL), "Incorrect direction.");
143
}
144
if ((glyphs[j].start > 8) && (glyphs[j].start < 23)) {
145
CHECK_FALSE_MESSAGE(((glyphs[j].flags & TextServer::GRAPHEME_IS_RTL) != TextServer::GRAPHEME_IS_RTL), "Incorrect direction.");
146
}
147
if (glyphs[j].start > 26) {
148
CHECK_FALSE_MESSAGE(((glyphs[j].flags & TextServer::GRAPHEME_IS_RTL) == TextServer::GRAPHEME_IS_RTL), "Incorrect direction.");
149
}
150
}
151
}
152
153
ts->free_rid(ctx);
154
155
for (int j = 0; j < font.size(); j++) {
156
ts->free_rid(font[j]);
157
}
158
font.clear();
159
}
160
}
161
162
SUBCASE("[TextServer] Text layout: Line break and align points") {
163
for (int i = 0; i < TextServerManager::get_singleton()->get_interface_count(); i++) {
164
Ref<TextServer> ts = TextServerManager::get_singleton()->get_interface(i);
165
CHECK_FALSE_MESSAGE(ts.is_null(), "Invalid TS interface.");
166
167
if (!ts->has_feature(TextServer::FEATURE_FONT_DYNAMIC) || !ts->has_feature(TextServer::FEATURE_SIMPLE_LAYOUT)) {
168
continue;
169
}
170
171
RID font1 = ts->create_font();
172
ts->font_set_data_ptr(font1, _font_Inter_Regular, _font_Inter_Regular_size);
173
ts->font_set_allow_system_fallback(font1, false);
174
RID font2 = ts->create_font();
175
ts->font_set_data_ptr(font2, _font_NotoSansThai_Regular, _font_NotoSansThai_Regular_size);
176
ts->font_set_allow_system_fallback(font2, false);
177
RID font3 = ts->create_font();
178
ts->font_set_data_ptr(font3, _font_Vazirmatn_Regular, _font_Vazirmatn_Regular_size);
179
ts->font_set_allow_system_fallback(font3, false);
180
181
Array font = { font1, font2, font3 };
182
{
183
RID ctx = ts->create_shaped_text();
184
CHECK_FALSE_MESSAGE(ctx == RID(), "Creating text buffer failed.");
185
ts->shaped_text_add_string(ctx, U"Xtest", font, 10);
186
ts->shaped_text_add_string(ctx, U"xs", font, 10);
187
RID sctx = ts->shaped_text_substr(ctx, 1, 5);
188
CHECK_FALSE_MESSAGE(sctx == RID(), "Creating substring text buffer failed.");
189
PackedInt32Array sbrk = ts->shaped_text_get_character_breaks(sctx);
190
CHECK_FALSE_MESSAGE(sbrk.size() != 5, "Invalid substring char breaks number.");
191
if (sbrk.size() == 5) {
192
CHECK_FALSE_MESSAGE(sbrk[0] != 2, "Invalid substring char break position.");
193
CHECK_FALSE_MESSAGE(sbrk[1] != 3, "Invalid substring char break position.");
194
CHECK_FALSE_MESSAGE(sbrk[2] != 4, "Invalid substring char break position.");
195
CHECK_FALSE_MESSAGE(sbrk[3] != 5, "Invalid substring char break position.");
196
CHECK_FALSE_MESSAGE(sbrk[4] != 6, "Invalid substring char break position.");
197
}
198
PackedInt32Array fbrk = ts->shaped_text_get_character_breaks(ctx);
199
CHECK_FALSE_MESSAGE(fbrk.size() != 7, "Invalid char breaks number.");
200
if (fbrk.size() == 7) {
201
CHECK_FALSE_MESSAGE(fbrk[0] != 1, "Invalid char break position.");
202
CHECK_FALSE_MESSAGE(fbrk[1] != 2, "Invalid char break position.");
203
CHECK_FALSE_MESSAGE(fbrk[2] != 3, "Invalid char break position.");
204
CHECK_FALSE_MESSAGE(fbrk[3] != 4, "Invalid char break position.");
205
CHECK_FALSE_MESSAGE(fbrk[4] != 5, "Invalid char break position.");
206
CHECK_FALSE_MESSAGE(fbrk[5] != 6, "Invalid char break position.");
207
CHECK_FALSE_MESSAGE(fbrk[6] != 7, "Invalid char break position.");
208
}
209
PackedInt32Array rbrk = ts->string_get_character_breaks(U"Xtestxs");
210
CHECK_FALSE_MESSAGE(rbrk.size() != 7, "Invalid char breaks number.");
211
if (rbrk.size() == 7) {
212
CHECK_FALSE_MESSAGE(rbrk[0] != 1, "Invalid char break position.");
213
CHECK_FALSE_MESSAGE(rbrk[1] != 2, "Invalid char break position.");
214
CHECK_FALSE_MESSAGE(rbrk[2] != 3, "Invalid char break position.");
215
CHECK_FALSE_MESSAGE(rbrk[3] != 4, "Invalid char break position.");
216
CHECK_FALSE_MESSAGE(rbrk[4] != 5, "Invalid char break position.");
217
CHECK_FALSE_MESSAGE(rbrk[5] != 6, "Invalid char break position.");
218
CHECK_FALSE_MESSAGE(rbrk[6] != 7, "Invalid char break position.");
219
}
220
221
ts->free_rid(sctx);
222
ts->free_rid(ctx);
223
}
224
225
if (ts->has_feature(TextServer::FEATURE_BREAK_ITERATORS)) {
226
RID ctx = ts->create_shaped_text();
227
CHECK_FALSE_MESSAGE(ctx == RID(), "Creating text buffer failed.");
228
ts->shaped_text_add_string(ctx, U"X❤️‍🔥", font, 10);
229
ts->shaped_text_add_string(ctx, U"xs", font, 10);
230
RID sctx = ts->shaped_text_substr(ctx, 1, 5);
231
CHECK_FALSE_MESSAGE(sctx == RID(), "Creating substring text buffer failed.");
232
PackedInt32Array sbrk = ts->shaped_text_get_character_breaks(sctx);
233
CHECK_FALSE_MESSAGE(sbrk.size() != 2, "Invalid substring char breaks number.");
234
if (sbrk.size() == 2) {
235
CHECK_FALSE_MESSAGE(sbrk[0] != 5, "Invalid substring char break position.");
236
CHECK_FALSE_MESSAGE(sbrk[1] != 6, "Invalid substring char break position.");
237
}
238
PackedInt32Array fbrk = ts->shaped_text_get_character_breaks(ctx);
239
CHECK_FALSE_MESSAGE(fbrk.size() != 4, "Invalid char breaks number.");
240
if (fbrk.size() == 4) {
241
CHECK_FALSE_MESSAGE(fbrk[0] != 1, "Invalid char break position.");
242
CHECK_FALSE_MESSAGE(fbrk[1] != 5, "Invalid char break position.");
243
CHECK_FALSE_MESSAGE(fbrk[2] != 6, "Invalid char break position.");
244
CHECK_FALSE_MESSAGE(fbrk[3] != 7, "Invalid char break position.");
245
}
246
PackedInt32Array rbrk = ts->string_get_character_breaks(U"X❤️‍🔥xs");
247
CHECK_FALSE_MESSAGE(rbrk.size() != 4, "Invalid char breaks number.");
248
if (rbrk.size() == 4) {
249
CHECK_FALSE_MESSAGE(rbrk[0] != 1, "Invalid char break position.");
250
CHECK_FALSE_MESSAGE(rbrk[1] != 5, "Invalid char break position.");
251
CHECK_FALSE_MESSAGE(rbrk[2] != 6, "Invalid char break position.");
252
CHECK_FALSE_MESSAGE(rbrk[3] != 7, "Invalid char break position.");
253
}
254
255
ts->free_rid(sctx);
256
ts->free_rid(ctx);
257
}
258
259
{
260
String test = U"Test test long text long text\n";
261
RID ctx = ts->create_shaped_text();
262
CHECK_FALSE_MESSAGE(ctx == RID(), "Creating text buffer failed.");
263
bool ok = ts->shaped_text_add_string(ctx, test, font, 16);
264
CHECK_FALSE_MESSAGE(!ok, "Adding text to the buffer failed.");
265
ts->shaped_text_update_breaks(ctx);
266
ts->shaped_text_update_justification_ops(ctx);
267
268
const Glyph *glyphs = ts->shaped_text_get_glyphs(ctx);
269
int gl_size = ts->shaped_text_get_glyph_count(ctx);
270
271
CHECK_FALSE_MESSAGE(gl_size != 30, "Invalid glyph count.");
272
for (int j = 0; j < gl_size; j++) {
273
bool hard = (glyphs[j].flags & TextServer::GRAPHEME_IS_BREAK_HARD) == TextServer::GRAPHEME_IS_BREAK_HARD;
274
bool soft = (glyphs[j].flags & TextServer::GRAPHEME_IS_BREAK_SOFT) == TextServer::GRAPHEME_IS_BREAK_SOFT;
275
bool space = (glyphs[j].flags & TextServer::GRAPHEME_IS_SPACE) == TextServer::GRAPHEME_IS_SPACE;
276
bool virt = (glyphs[j].flags & TextServer::GRAPHEME_IS_VIRTUAL) == TextServer::GRAPHEME_IS_VIRTUAL;
277
bool elo = (glyphs[j].flags & TextServer::GRAPHEME_IS_ELONGATION) == TextServer::GRAPHEME_IS_ELONGATION;
278
if (j == 4 || j == 9 || j == 14 || j == 19 || j == 24) {
279
CHECK_FALSE_MESSAGE((!soft || !space || hard || virt || elo), "Invalid glyph flags.");
280
} else if (j == 29) {
281
CHECK_FALSE_MESSAGE((soft || !space || !hard || virt || elo), "Invalid glyph flags.");
282
} else {
283
CHECK_FALSE_MESSAGE((soft || space || hard || virt || elo), "Invalid glyph flags.");
284
}
285
}
286
ts->free_rid(ctx);
287
}
288
289
{
290
String test = U"الحمـد";
291
RID ctx = ts->create_shaped_text();
292
CHECK_FALSE_MESSAGE(ctx == RID(), "Creating text buffer failed.");
293
bool ok = ts->shaped_text_add_string(ctx, test, font, 16);
294
CHECK_FALSE_MESSAGE(!ok, "Adding text to the buffer failed.");
295
ts->shaped_text_update_breaks(ctx);
296
297
const Glyph *glyphs = ts->shaped_text_get_glyphs(ctx);
298
int gl_size = ts->shaped_text_get_glyph_count(ctx);
299
CHECK_FALSE_MESSAGE(gl_size != 6, "Invalid glyph count.");
300
for (int j = 0; j < gl_size; j++) {
301
bool hard = (glyphs[j].flags & TextServer::GRAPHEME_IS_BREAK_HARD) == TextServer::GRAPHEME_IS_BREAK_HARD;
302
bool soft = (glyphs[j].flags & TextServer::GRAPHEME_IS_BREAK_SOFT) == TextServer::GRAPHEME_IS_BREAK_SOFT;
303
bool space = (glyphs[j].flags & TextServer::GRAPHEME_IS_SPACE) == TextServer::GRAPHEME_IS_SPACE;
304
bool virt = (glyphs[j].flags & TextServer::GRAPHEME_IS_VIRTUAL) == TextServer::GRAPHEME_IS_VIRTUAL;
305
bool elo = (glyphs[j].flags & TextServer::GRAPHEME_IS_ELONGATION) == TextServer::GRAPHEME_IS_ELONGATION;
306
CHECK_FALSE_MESSAGE((soft || space || hard || virt || elo), "Invalid glyph flags.");
307
}
308
if (ts->has_feature(TextServer::FEATURE_KASHIDA_JUSTIFICATION)) {
309
ts->shaped_text_update_justification_ops(ctx);
310
311
glyphs = ts->shaped_text_get_glyphs(ctx);
312
gl_size = ts->shaped_text_get_glyph_count(ctx);
313
314
CHECK_FALSE_MESSAGE(gl_size != 6, "Invalid glyph count.");
315
for (int j = 0; j < gl_size; j++) {
316
bool hard = (glyphs[j].flags & TextServer::GRAPHEME_IS_BREAK_HARD) == TextServer::GRAPHEME_IS_BREAK_HARD;
317
bool soft = (glyphs[j].flags & TextServer::GRAPHEME_IS_BREAK_SOFT) == TextServer::GRAPHEME_IS_BREAK_SOFT;
318
bool space = (glyphs[j].flags & TextServer::GRAPHEME_IS_SPACE) == TextServer::GRAPHEME_IS_SPACE;
319
bool virt = (glyphs[j].flags & TextServer::GRAPHEME_IS_VIRTUAL) == TextServer::GRAPHEME_IS_VIRTUAL;
320
bool elo = (glyphs[j].flags & TextServer::GRAPHEME_IS_ELONGATION) == TextServer::GRAPHEME_IS_ELONGATION;
321
if (j == 1) {
322
CHECK_FALSE_MESSAGE((soft || space || hard || virt || !elo), "Invalid glyph flags.");
323
} else {
324
CHECK_FALSE_MESSAGE((soft || space || hard || virt || elo), "Invalid glyph flags.");
325
}
326
}
327
}
328
ts->free_rid(ctx);
329
}
330
331
{
332
String test = U"الحمد";
333
RID ctx = ts->create_shaped_text();
334
CHECK_FALSE_MESSAGE(ctx == RID(), "Creating text buffer failed.");
335
bool ok = ts->shaped_text_add_string(ctx, test, font, 16);
336
CHECK_FALSE_MESSAGE(!ok, "Adding text to the buffer failed.");
337
ts->shaped_text_update_breaks(ctx);
338
339
const Glyph *glyphs = ts->shaped_text_get_glyphs(ctx);
340
int gl_size = ts->shaped_text_get_glyph_count(ctx);
341
CHECK_FALSE_MESSAGE(gl_size != 5, "Invalid glyph count.");
342
for (int j = 0; j < gl_size; j++) {
343
bool hard = (glyphs[j].flags & TextServer::GRAPHEME_IS_BREAK_HARD) == TextServer::GRAPHEME_IS_BREAK_HARD;
344
bool soft = (glyphs[j].flags & TextServer::GRAPHEME_IS_BREAK_SOFT) == TextServer::GRAPHEME_IS_BREAK_SOFT;
345
bool space = (glyphs[j].flags & TextServer::GRAPHEME_IS_SPACE) == TextServer::GRAPHEME_IS_SPACE;
346
bool virt = (glyphs[j].flags & TextServer::GRAPHEME_IS_VIRTUAL) == TextServer::GRAPHEME_IS_VIRTUAL;
347
bool elo = (glyphs[j].flags & TextServer::GRAPHEME_IS_ELONGATION) == TextServer::GRAPHEME_IS_ELONGATION;
348
CHECK_FALSE_MESSAGE((soft || space || hard || virt || elo), "Invalid glyph flags.");
349
}
350
351
if (ts->has_feature(TextServer::FEATURE_KASHIDA_JUSTIFICATION)) {
352
ts->shaped_text_update_justification_ops(ctx);
353
354
glyphs = ts->shaped_text_get_glyphs(ctx);
355
gl_size = ts->shaped_text_get_glyph_count(ctx);
356
357
CHECK_FALSE_MESSAGE(gl_size != 6, "Invalid glyph count.");
358
for (int j = 0; j < gl_size; j++) {
359
bool hard = (glyphs[j].flags & TextServer::GRAPHEME_IS_BREAK_HARD) == TextServer::GRAPHEME_IS_BREAK_HARD;
360
bool soft = (glyphs[j].flags & TextServer::GRAPHEME_IS_BREAK_SOFT) == TextServer::GRAPHEME_IS_BREAK_SOFT;
361
bool space = (glyphs[j].flags & TextServer::GRAPHEME_IS_SPACE) == TextServer::GRAPHEME_IS_SPACE;
362
bool virt = (glyphs[j].flags & TextServer::GRAPHEME_IS_VIRTUAL) == TextServer::GRAPHEME_IS_VIRTUAL;
363
bool elo = (glyphs[j].flags & TextServer::GRAPHEME_IS_ELONGATION) == TextServer::GRAPHEME_IS_ELONGATION;
364
if (j == 1) {
365
CHECK_FALSE_MESSAGE((soft || space || hard || !virt || !elo), "Invalid glyph flags.");
366
} else {
367
CHECK_FALSE_MESSAGE((soft || space || hard || virt || elo), "Invalid glyph flags.");
368
}
369
}
370
}
371
ts->free_rid(ctx);
372
}
373
374
{
375
String test = U"الحمـد الرياضي العربي";
376
RID ctx = ts->create_shaped_text();
377
CHECK_FALSE_MESSAGE(ctx == RID(), "Creating text buffer failed.");
378
bool ok = ts->shaped_text_add_string(ctx, test, font, 16);
379
CHECK_FALSE_MESSAGE(!ok, "Adding text to the buffer failed.");
380
ts->shaped_text_update_breaks(ctx);
381
382
const Glyph *glyphs = ts->shaped_text_get_glyphs(ctx);
383
int gl_size = ts->shaped_text_get_glyph_count(ctx);
384
385
CHECK_FALSE_MESSAGE(gl_size != 21, "Invalid glyph count.");
386
for (int j = 0; j < gl_size; j++) {
387
bool hard = (glyphs[j].flags & TextServer::GRAPHEME_IS_BREAK_HARD) == TextServer::GRAPHEME_IS_BREAK_HARD;
388
bool soft = (glyphs[j].flags & TextServer::GRAPHEME_IS_BREAK_SOFT) == TextServer::GRAPHEME_IS_BREAK_SOFT;
389
bool space = (glyphs[j].flags & TextServer::GRAPHEME_IS_SPACE) == TextServer::GRAPHEME_IS_SPACE;
390
bool virt = (glyphs[j].flags & TextServer::GRAPHEME_IS_VIRTUAL) == TextServer::GRAPHEME_IS_VIRTUAL;
391
bool elo = (glyphs[j].flags & TextServer::GRAPHEME_IS_ELONGATION) == TextServer::GRAPHEME_IS_ELONGATION;
392
if (j == 6 || j == 14) {
393
CHECK_FALSE_MESSAGE((!soft || !space || hard || virt || elo), "Invalid glyph flags.");
394
} else {
395
CHECK_FALSE_MESSAGE((soft || space || hard || virt || elo), "Invalid glyph flags.");
396
}
397
}
398
399
if (ts->has_feature(TextServer::FEATURE_KASHIDA_JUSTIFICATION)) {
400
ts->shaped_text_update_justification_ops(ctx);
401
402
glyphs = ts->shaped_text_get_glyphs(ctx);
403
gl_size = ts->shaped_text_get_glyph_count(ctx);
404
405
CHECK_FALSE_MESSAGE(gl_size != 23, "Invalid glyph count.");
406
for (int j = 0; j < gl_size; j++) {
407
bool hard = (glyphs[j].flags & TextServer::GRAPHEME_IS_BREAK_HARD) == TextServer::GRAPHEME_IS_BREAK_HARD;
408
bool soft = (glyphs[j].flags & TextServer::GRAPHEME_IS_BREAK_SOFT) == TextServer::GRAPHEME_IS_BREAK_SOFT;
409
bool space = (glyphs[j].flags & TextServer::GRAPHEME_IS_SPACE) == TextServer::GRAPHEME_IS_SPACE;
410
bool virt = (glyphs[j].flags & TextServer::GRAPHEME_IS_VIRTUAL) == TextServer::GRAPHEME_IS_VIRTUAL;
411
bool elo = (glyphs[j].flags & TextServer::GRAPHEME_IS_ELONGATION) == TextServer::GRAPHEME_IS_ELONGATION;
412
if (j == 7 || j == 16) {
413
CHECK_FALSE_MESSAGE((!soft || !space || hard || virt || elo), "Invalid glyph flags.");
414
} else if (j == 3 || j == 9) {
415
CHECK_FALSE_MESSAGE((soft || space || hard || !virt || !elo), "Invalid glyph flags.");
416
} else if (j == 18) {
417
CHECK_FALSE_MESSAGE((soft || space || hard || virt || !elo), "Invalid glyph flags.");
418
} else {
419
CHECK_FALSE_MESSAGE((soft || space || hard || virt || elo), "Invalid glyph flags.");
420
}
421
}
422
}
423
424
ts->free_rid(ctx);
425
}
426
427
{
428
String test = U"เป็น ภาษา ราชการ และ ภาษา";
429
RID ctx = ts->create_shaped_text();
430
CHECK_FALSE_MESSAGE(ctx == RID(), "Creating text buffer failed.");
431
bool ok = ts->shaped_text_add_string(ctx, test, font, 16);
432
CHECK_FALSE_MESSAGE(!ok, "Adding text to the buffer failed.");
433
ts->shaped_text_update_breaks(ctx);
434
ts->shaped_text_update_justification_ops(ctx);
435
436
const Glyph *glyphs = ts->shaped_text_get_glyphs(ctx);
437
int gl_size = ts->shaped_text_get_glyph_count(ctx);
438
439
CHECK_FALSE_MESSAGE(gl_size != 25, "Invalid glyph count.");
440
for (int j = 0; j < gl_size; j++) {
441
bool hard = (glyphs[j].flags & TextServer::GRAPHEME_IS_BREAK_HARD) == TextServer::GRAPHEME_IS_BREAK_HARD;
442
bool soft = (glyphs[j].flags & TextServer::GRAPHEME_IS_BREAK_SOFT) == TextServer::GRAPHEME_IS_BREAK_SOFT;
443
bool space = (glyphs[j].flags & TextServer::GRAPHEME_IS_SPACE) == TextServer::GRAPHEME_IS_SPACE;
444
bool virt = (glyphs[j].flags & TextServer::GRAPHEME_IS_VIRTUAL) == TextServer::GRAPHEME_IS_VIRTUAL;
445
bool elo = (glyphs[j].flags & TextServer::GRAPHEME_IS_ELONGATION) == TextServer::GRAPHEME_IS_ELONGATION;
446
if (j == 4 || j == 9 || j == 16 || j == 20) {
447
CHECK_FALSE_MESSAGE((!soft || !space || hard || virt || elo), "Invalid glyph flags.");
448
} else {
449
CHECK_FALSE_MESSAGE((soft || space || hard || virt || elo), "Invalid glyph flags.");
450
}
451
}
452
ts->free_rid(ctx);
453
}
454
455
if (ts->has_feature(TextServer::FEATURE_BREAK_ITERATORS)) { // Line breaking opportunities.
456
String test = U"เป็นภาษาราชการและภาษา";
457
RID ctx = ts->create_shaped_text();
458
CHECK_FALSE_MESSAGE(ctx == RID(), "Creating text buffer failed.");
459
bool ok = ts->shaped_text_add_string(ctx, test, font, 16);
460
CHECK_FALSE_MESSAGE(!ok, "Adding text to the buffer failed.");
461
ts->shaped_text_update_breaks(ctx);
462
ts->shaped_text_update_justification_ops(ctx);
463
464
const Glyph *glyphs = ts->shaped_text_get_glyphs(ctx);
465
int gl_size = ts->shaped_text_get_glyph_count(ctx);
466
467
CHECK_FALSE_MESSAGE(gl_size != 25, "Invalid glyph count.");
468
for (int j = 0; j < gl_size; j++) {
469
bool hard = (glyphs[j].flags & TextServer::GRAPHEME_IS_BREAK_HARD) == TextServer::GRAPHEME_IS_BREAK_HARD;
470
bool soft = (glyphs[j].flags & TextServer::GRAPHEME_IS_BREAK_SOFT) == TextServer::GRAPHEME_IS_BREAK_SOFT;
471
bool space = (glyphs[j].flags & TextServer::GRAPHEME_IS_SPACE) == TextServer::GRAPHEME_IS_SPACE;
472
bool virt = (glyphs[j].flags & TextServer::GRAPHEME_IS_VIRTUAL) == TextServer::GRAPHEME_IS_VIRTUAL;
473
bool elo = (glyphs[j].flags & TextServer::GRAPHEME_IS_ELONGATION) == TextServer::GRAPHEME_IS_ELONGATION;
474
if (j == 4 || j == 9 || j == 16 || j == 20) {
475
CHECK_FALSE_MESSAGE((!soft || !space || hard || !virt || elo), "Invalid glyph flags.");
476
} else {
477
CHECK_FALSE_MESSAGE((soft || space || hard || virt || elo), "Invalid glyph flags.");
478
}
479
}
480
ts->free_rid(ctx);
481
}
482
483
if (ts->has_feature(TextServer::FEATURE_BREAK_ITERATORS)) { // Break line.
484
struct TestCase {
485
String text;
486
PackedInt32Array breaks;
487
};
488
TestCase cases[] = {
489
{ U" เมาส์ตัวนี้", { 0, 17, 17, 23 } },
490
{ U" กู้ไฟล์", { 0, 17, 17, 21 } },
491
{ U" ไม่มีคำ", { 0, 18, 18, 20 } },
492
{ U" ไม่มีคำพูด", { 0, 18, 18, 23 } },
493
{ U" ไม่มีคำ", { 0, 17, 17, 19 } },
494
{ U" มีอุปกรณ์\nนี้", { 0, 11, 11, 19, 19, 22 } },
495
{ U"الحمدا لحمدا لحمـــد", { 0, 13, 13, 20 } },
496
{ U" الحمد test", { 0, 15, 15, 19 } },
497
{ U"الحمـد الرياضي العربي", { 0, 7, 7, 15, 15, 21 } },
498
{ U"test \rtest", { 0, 6, 6, 10 } },
499
{ U"test\r test", { 0, 5, 5, 10 } },
500
{ U"test\r test \r test", { 0, 5, 5, 12, 12, 17 } },
501
};
502
for (size_t j = 0; j < std_size(cases); j++) {
503
RID ctx = ts->create_shaped_text();
504
CHECK_FALSE_MESSAGE(ctx == RID(), "Creating text buffer failed.");
505
bool ok = ts->shaped_text_add_string(ctx, cases[j].text, font, 16);
506
CHECK_FALSE_MESSAGE(!ok, "Adding text to the buffer failed.");
507
508
PackedInt32Array breaks = ts->shaped_text_get_line_breaks(ctx, 90.0);
509
CHECK_FALSE_MESSAGE(breaks != cases[j].breaks, "Invalid break points.");
510
511
breaks = ts->shaped_text_get_line_breaks_adv(ctx, { 90.0 }, 0, false);
512
CHECK_FALSE_MESSAGE(breaks != cases[j].breaks, "Invalid break points.");
513
514
ts->free_rid(ctx);
515
}
516
}
517
518
if (ts->has_feature(TextServer::FEATURE_BREAK_ITERATORS)) { // Break line and trim spaces.
519
struct TestCase {
520
String text;
521
PackedInt32Array breaks;
522
BitField<TextServer::LineBreakFlag> flags = TextServer::BREAK_NONE;
523
};
524
TestCase cases[] = {
525
{ U"test \rtest", { 0, 4, 6, 10 }, TextServer::BREAK_MANDATORY | TextServer::BREAK_WORD_BOUND | TextServer::BREAK_TRIM_START_EDGE_SPACES | TextServer::BREAK_TRIM_END_EDGE_SPACES },
526
{ U"test \rtest", { 0, 6, 6, 10 }, TextServer::BREAK_MANDATORY | TextServer::BREAK_WORD_BOUND | TextServer::BREAK_TRIM_START_EDGE_SPACES },
527
{ U"test\r test", { 0, 4, 6, 10 }, TextServer::BREAK_MANDATORY | TextServer::BREAK_WORD_BOUND | TextServer::BREAK_TRIM_START_EDGE_SPACES | TextServer::BREAK_TRIM_END_EDGE_SPACES },
528
{ U"test\r test", { 0, 4, 5, 10 }, TextServer::BREAK_MANDATORY | TextServer::BREAK_WORD_BOUND | TextServer::BREAK_TRIM_END_EDGE_SPACES },
529
{ U"test\r test \r test", { 0, 4, 6, 10, 13, 17 }, TextServer::BREAK_MANDATORY | TextServer::BREAK_WORD_BOUND | TextServer::BREAK_TRIM_START_EDGE_SPACES | TextServer::BREAK_TRIM_END_EDGE_SPACES },
530
{ U"test\r test \r test", { 0, 5, 6, 12, 13, 17 }, TextServer::BREAK_MANDATORY | TextServer::BREAK_WORD_BOUND | TextServer::BREAK_TRIM_START_EDGE_SPACES },
531
{ U"test\r test \r test", { 0, 4, 5, 10, 12, 17 }, TextServer::BREAK_MANDATORY | TextServer::BREAK_WORD_BOUND | TextServer::BREAK_TRIM_END_EDGE_SPACES },
532
{ U"test\r test \r test", { 0, 5, 5, 12, 12, 17 }, TextServer::BREAK_MANDATORY | TextServer::BREAK_WORD_BOUND },
533
};
534
for (size_t j = 0; j < sizeof(cases) / sizeof(TestCase); j++) {
535
RID ctx = ts->create_shaped_text();
536
CHECK_FALSE_MESSAGE(ctx == RID(), "Creating text buffer failed.");
537
bool ok = ts->shaped_text_add_string(ctx, cases[j].text, font, 16);
538
CHECK_FALSE_MESSAGE(!ok, "Adding text to the buffer failed.");
539
540
PackedInt32Array breaks = ts->shaped_text_get_line_breaks(ctx, 90.0, 0, cases[j].flags);
541
CHECK_FALSE_MESSAGE(breaks != cases[j].breaks, "Invalid break points.");
542
543
breaks = ts->shaped_text_get_line_breaks_adv(ctx, { 90.0 }, 0, false, cases[j].flags);
544
CHECK_FALSE_MESSAGE(breaks != cases[j].breaks, "Invalid break points.");
545
546
ts->free_rid(ctx);
547
}
548
}
549
550
for (int j = 0; j < font.size(); j++) {
551
ts->free_rid(font[j]);
552
}
553
font.clear();
554
}
555
}
556
557
SUBCASE("[TextServer] Text layout: Line breaking") {
558
for (int i = 0; i < TextServerManager::get_singleton()->get_interface_count(); i++) {
559
Ref<TextServer> ts = TextServerManager::get_singleton()->get_interface(i);
560
CHECK_FALSE_MESSAGE(ts.is_null(), "Invalid TS interface.");
561
562
if (!ts->has_feature(TextServer::FEATURE_FONT_DYNAMIC) || !ts->has_feature(TextServer::FEATURE_SIMPLE_LAYOUT)) {
563
continue;
564
}
565
566
String test_1 = U"test test test";
567
// 5^ 10^
568
569
RID font1 = ts->create_font();
570
ts->font_set_data_ptr(font1, _font_Inter_Regular, _font_Inter_Regular_size);
571
RID font2 = ts->create_font();
572
ts->font_set_data_ptr(font2, _font_NotoSansThai_Regular, _font_NotoSansThai_Regular_size);
573
574
Array font = { font1, font2 };
575
RID ctx = ts->create_shaped_text();
576
CHECK_FALSE_MESSAGE(ctx == RID(), "Creating text buffer failed.");
577
bool ok = ts->shaped_text_add_string(ctx, test_1, font, 16);
578
CHECK_FALSE_MESSAGE(!ok, "Adding text to the buffer failed.");
579
580
PackedInt32Array brks = ts->shaped_text_get_line_breaks(ctx, 1);
581
CHECK_FALSE_MESSAGE(brks.size() != 6, "Invalid line breaks number.");
582
if (brks.size() == 6) {
583
CHECK_FALSE_MESSAGE(brks[0] != 0, "Invalid line break position.");
584
CHECK_FALSE_MESSAGE(brks[1] != 5, "Invalid line break position.");
585
586
CHECK_FALSE_MESSAGE(brks[2] != 5, "Invalid line break position.");
587
CHECK_FALSE_MESSAGE(brks[3] != 10, "Invalid line break position.");
588
589
CHECK_FALSE_MESSAGE(brks[4] != 10, "Invalid line break position.");
590
CHECK_FALSE_MESSAGE(brks[5] != 14, "Invalid line break position.");
591
}
592
593
brks = ts->shaped_text_get_line_breaks(ctx, 35.0, 0, TextServer::BREAK_WORD_BOUND | TextServer::BREAK_MANDATORY | TextServer::BREAK_TRIM_START_EDGE_SPACES | TextServer::BREAK_TRIM_END_EDGE_SPACES);
594
CHECK_FALSE_MESSAGE(brks.size() != 6, "Invalid line breaks number.");
595
if (brks.size() == 6) {
596
CHECK_FALSE_MESSAGE(brks[0] != 0, "Invalid line break position.");
597
CHECK_FALSE_MESSAGE(brks[1] != 4, "Invalid line break position.");
598
599
CHECK_FALSE_MESSAGE(brks[2] != 5, "Invalid line break position.");
600
CHECK_FALSE_MESSAGE(brks[3] != 9, "Invalid line break position.");
601
602
CHECK_FALSE_MESSAGE(brks[4] != 10, "Invalid line break position.");
603
CHECK_FALSE_MESSAGE(brks[5] != 14, "Invalid line break position.");
604
}
605
606
ts->free_rid(ctx);
607
608
String test_2 = U"Word Wrap";
609
// 5^
610
611
ctx = ts->create_shaped_text();
612
CHECK_FALSE_MESSAGE(ctx == RID(), "Creating text buffer failed.");
613
ok = ts->shaped_text_add_string(ctx, test_2, font, 16);
614
CHECK_FALSE_MESSAGE(!ok, "Adding text to the buffer failed.");
615
616
brks = ts->shaped_text_get_line_breaks(ctx, 43);
617
CHECK_FALSE_MESSAGE(brks.size() != 4, "Invalid line breaks number.");
618
if (brks.size() == 4) {
619
CHECK_FALSE_MESSAGE(brks[0] != 0, "Invalid line break position.");
620
CHECK_FALSE_MESSAGE(brks[1] != 5, "Invalid line break position.");
621
622
CHECK_FALSE_MESSAGE(brks[2] != 5, "Invalid line break position.");
623
CHECK_FALSE_MESSAGE(brks[3] != 9, "Invalid line break position.");
624
}
625
626
brks = ts->shaped_text_get_line_breaks(ctx, 43.0, 0, TextServer::BREAK_WORD_BOUND | TextServer::BREAK_MANDATORY | TextServer::BREAK_TRIM_START_EDGE_SPACES | TextServer::BREAK_TRIM_END_EDGE_SPACES);
627
CHECK_FALSE_MESSAGE(brks.size() != 4, "Invalid line breaks number.");
628
if (brks.size() == 4) {
629
CHECK_FALSE_MESSAGE(brks[0] != 0, "Invalid line break position.");
630
CHECK_FALSE_MESSAGE(brks[1] != 4, "Invalid line break position.");
631
632
CHECK_FALSE_MESSAGE(brks[2] != 5, "Invalid line break position.");
633
CHECK_FALSE_MESSAGE(brks[3] != 9, "Invalid line break position.");
634
}
635
636
brks = ts->shaped_text_get_line_breaks(ctx, 43.0, 0, TextServer::BREAK_WORD_BOUND | TextServer::BREAK_ADAPTIVE | TextServer::BREAK_MANDATORY | TextServer::BREAK_TRIM_START_EDGE_SPACES | TextServer::BREAK_TRIM_END_EDGE_SPACES);
637
CHECK_FALSE_MESSAGE(brks.size() != 4, "Invalid line breaks number.");
638
if (brks.size() == 4) {
639
CHECK_FALSE_MESSAGE(brks[0] != 0, "Invalid line break position.");
640
CHECK_FALSE_MESSAGE(brks[1] != 4, "Invalid line break position.");
641
642
CHECK_FALSE_MESSAGE(brks[2] != 5, "Invalid line break position.");
643
CHECK_FALSE_MESSAGE(brks[3] != 9, "Invalid line break position.");
644
}
645
646
brks = ts->shaped_text_get_line_breaks(ctx, 43.0, 0, TextServer::BREAK_WORD_BOUND | TextServer::BREAK_ADAPTIVE | TextServer::BREAK_MANDATORY);
647
CHECK_FALSE_MESSAGE(brks.size() != 6, "Invalid line breaks number.");
648
if (brks.size() == 6) {
649
CHECK_FALSE_MESSAGE(brks[0] != 0, "Invalid line break position.");
650
CHECK_FALSE_MESSAGE(brks[1] != 4, "Invalid line break position.");
651
652
CHECK_FALSE_MESSAGE(brks[2] != 4, "Invalid line break position.");
653
CHECK_FALSE_MESSAGE(brks[3] != 5, "Invalid line break position.");
654
655
CHECK_FALSE_MESSAGE(brks[4] != 5, "Invalid line break position.");
656
CHECK_FALSE_MESSAGE(brks[5] != 9, "Invalid line break position.");
657
}
658
659
ts->free_rid(ctx);
660
661
for (int j = 0; j < font.size(); j++) {
662
ts->free_rid(font[j]);
663
}
664
font.clear();
665
}
666
}
667
668
SUBCASE("[TextServer] Text layout: Justification") {
669
for (int i = 0; i < TextServerManager::get_singleton()->get_interface_count(); i++) {
670
Ref<TextServer> ts = TextServerManager::get_singleton()->get_interface(i);
671
CHECK_FALSE_MESSAGE(ts.is_null(), "Invalid TS interface.");
672
673
if (!ts->has_feature(TextServer::FEATURE_FONT_DYNAMIC) || !ts->has_feature(TextServer::FEATURE_SIMPLE_LAYOUT)) {
674
continue;
675
}
676
677
RID font1 = ts->create_font();
678
ts->font_set_data_ptr(font1, _font_Inter_Regular, _font_Inter_Regular_size);
679
RID font2 = ts->create_font();
680
ts->font_set_data_ptr(font2, _font_Vazirmatn_Regular, _font_Vazirmatn_Regular_size);
681
682
Array font = { font1, font2 };
683
String test_1 = U"الحمد";
684
String test_2 = U"الحمد test";
685
String test_3 = U"test test";
686
// 7^ 26^
687
688
RID ctx;
689
bool ok;
690
float width_old, width;
691
if (ts->has_feature(TextServer::FEATURE_KASHIDA_JUSTIFICATION)) {
692
ctx = ts->create_shaped_text();
693
CHECK_FALSE_MESSAGE(ctx == RID(), "Creating text buffer failed.");
694
ok = ts->shaped_text_add_string(ctx, test_1, font, 16);
695
CHECK_FALSE_MESSAGE(!ok, "Adding text to the buffer failed.");
696
697
width_old = ts->shaped_text_get_width(ctx);
698
width = ts->shaped_text_fit_to_width(ctx, 100, TextServer::JUSTIFICATION_WORD_BOUND);
699
CHECK_FALSE_MESSAGE((width != width_old), "Invalid fill width.");
700
width = ts->shaped_text_fit_to_width(ctx, 100, TextServer::JUSTIFICATION_WORD_BOUND | TextServer::JUSTIFICATION_KASHIDA);
701
CHECK_FALSE_MESSAGE((width <= width_old || width > 100), "Invalid fill width.");
702
703
ts->free_rid(ctx);
704
705
ctx = ts->create_shaped_text();
706
CHECK_FALSE_MESSAGE(ctx == RID(), "Creating text buffer failed.");
707
ok = ts->shaped_text_add_string(ctx, test_2, font, 16);
708
CHECK_FALSE_MESSAGE(!ok, "Adding text to the buffer failed.");
709
710
width_old = ts->shaped_text_get_width(ctx);
711
width = ts->shaped_text_fit_to_width(ctx, 100, TextServer::JUSTIFICATION_WORD_BOUND);
712
CHECK_FALSE_MESSAGE((width <= width_old || width > 100), "Invalid fill width.");
713
width = ts->shaped_text_fit_to_width(ctx, 100, TextServer::JUSTIFICATION_WORD_BOUND | TextServer::JUSTIFICATION_KASHIDA);
714
CHECK_FALSE_MESSAGE((width <= width_old || width > 100), "Invalid fill width.");
715
716
ts->free_rid(ctx);
717
}
718
719
ctx = ts->create_shaped_text();
720
CHECK_FALSE_MESSAGE(ctx == RID(), "Creating text buffer failed.");
721
ok = ts->shaped_text_add_string(ctx, test_3, font, 16);
722
CHECK_FALSE_MESSAGE(!ok, "Adding text to the buffer failed.");
723
724
width_old = ts->shaped_text_get_width(ctx);
725
width = ts->shaped_text_fit_to_width(ctx, 100, TextServer::JUSTIFICATION_WORD_BOUND);
726
CHECK_FALSE_MESSAGE((width <= width_old || width > 100), "Invalid fill width.");
727
728
ts->free_rid(ctx);
729
730
for (int j = 0; j < font.size(); j++) {
731
ts->free_rid(font[j]);
732
}
733
font.clear();
734
}
735
}
736
737
SUBCASE("[TextServer] Unicode identifiers") {
738
for (int i = 0; i < TextServerManager::get_singleton()->get_interface_count(); i++) {
739
Ref<TextServer> ts = TextServerManager::get_singleton()->get_interface(i);
740
CHECK_FALSE_MESSAGE(ts.is_null(), "Invalid TS interface.");
741
742
static const char32_t *data[19] = { U"-30", U"100", U"10.1", U"10,1", U"1e2", U"1e-2", U"1e2e3", U"0xAB", U"AB", U"Test1", U"1Test", U"Test*1", U"test_testeT", U"test_tes teT", U"عَلَيْكُمْ", U"عَلَيْكُمْTest", U"ӒӖӚӜ", U"_test", U"ÂÃÄÅĀĂĄÇĆĈĊ" };
743
static bool isid[19] = { false, false, false, false, false, false, false, false, true, true, false, false, true, false, true, true, true, true, true };
744
for (int j = 0; j < 19; j++) {
745
String s = String(data[j]);
746
CHECK(ts->is_valid_identifier(s) == isid[j]);
747
}
748
749
if (ts->has_feature(TextServer::FEATURE_UNICODE_IDENTIFIERS)) {
750
// Test UAX 3.2 ZW(N)J usage.
751
CHECK(ts->is_valid_identifier(U"\u0646\u0627\u0645\u0647\u200C\u0627\u06CC"));
752
CHECK(ts->is_valid_identifier(U"\u0D26\u0D43\u0D15\u0D4D\u200C\u0D38\u0D3E\u0D15\u0D4D\u0D37\u0D3F"));
753
CHECK(ts->is_valid_identifier(U"\u0DC1\u0DCA\u200D\u0DBB\u0DD3"));
754
}
755
}
756
}
757
758
SUBCASE("[TextServer] Unicode letters") {
759
for (int i = 0; i < TextServerManager::get_singleton()->get_interface_count(); i++) {
760
Ref<TextServer> ts = TextServerManager::get_singleton()->get_interface(i);
761
CHECK_FALSE_MESSAGE(ts.is_null(), "Invalid TS interface.");
762
763
struct ul_testcase {
764
int fail_index = -1; // Expecting failure at given index.
765
char32_t text[10]; // Using 0 as the terminator.
766
};
767
ul_testcase cases[14] = {
768
{
769
0,
770
{ 0x2D, 0x33, 0x30, 0, 0, 0, 0, 0, 0, 0 }, // "-30"
771
},
772
{
773
1,
774
{ 0x61, 0x2E, 0x31, 0, 0, 0, 0, 0, 0, 0 }, // "a.1"
775
},
776
{
777
1,
778
{ 0x61, 0x2C, 0x31, 0, 0, 0, 0, 0, 0, 0 }, // "a,1"
779
},
780
{
781
0,
782
{ 0x31, 0x65, 0x2D, 0x32, 0, 0, 0, 0, 0, 0 }, // "1e-2"
783
},
784
{
785
0,
786
{ 0xAB, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, // "Left-Pointing Double Angle Quotation Mark"
787
},
788
{
789
-1,
790
{ 0x41, 0x42, 0, 0, 0, 0, 0, 0, 0, 0 }, // "AB"
791
},
792
{
793
4,
794
{ 0x54, 0x65, 0x73, 0x74, 0x31, 0, 0, 0, 0, 0 }, // "Test1"
795
},
796
{
797
2,
798
{ 0x54, 0x65, 0x2A, 0x73, 0x74, 0, 0, 0, 0, 0 }, // "Te*st"
799
},
800
{
801
4,
802
{ 0x74, 0x65, 0x73, 0x74, 0x5F, 0x74, 0x65, 0x73, 0x74, 0x65 }, // "test_teste"
803
},
804
{
805
4,
806
{ 0x74, 0x65, 0x73, 0x74, 0x20, 0x74, 0x65, 0x73, 0x74, 0 }, // "test test"
807
},
808
{
809
-1,
810
{ 0x643, 0x402, 0x716, 0xB05, 0, 0, 0, 0, 0, 0 }, // "كЂܖଅ" (arabic letters),
811
},
812
{
813
-1,
814
{ 0x643, 0x402, 0x716, 0xB05, 0x54, 0x65, 0x73, 0x74, 0x30AA, 0x4E21 }, // 0-3 arabic letters, 4-7 latin letters, 8-9 CJK letters
815
},
816
{
817
-1,
818
{ 0x4D2, 0x4D6, 0x4DA, 0x4DC, 0, 0, 0, 0, 0, 0 }, // "ӒӖӚӜ" cyrillic letters
819
},
820
{
821
-1,
822
{ 0xC2, 0xC3, 0xC4, 0xC5, 0x100, 0x102, 0x104, 0xC7, 0x106, 0x108 }, // "ÂÃÄÅĀĂĄÇĆĈ" rarer latin letters
823
},
824
};
825
826
for (int j = 0; j < 14; j++) {
827
ul_testcase test = cases[j];
828
int failed_on_index = -1;
829
for (int k = 0; k < 10; k++) {
830
char32_t character = test.text[k];
831
if (character == 0) {
832
break;
833
}
834
if (!ts->is_valid_letter(character)) {
835
failed_on_index = k;
836
break;
837
}
838
}
839
840
if (test.fail_index == -1) {
841
CHECK_MESSAGE(test.fail_index == failed_on_index, "In interface ", ts->get_name() + ": In test case ", j, ", the character at index ", failed_on_index, " should have been a letter.");
842
} else {
843
CHECK_MESSAGE(test.fail_index == failed_on_index, "In interface ", ts->get_name() + ": In test case ", j, ", expected first non-letter at index ", test.fail_index, ", but found at index ", failed_on_index);
844
}
845
}
846
}
847
}
848
849
SUBCASE("[TextServer] Strip Diacritics") {
850
for (int i = 0; i < TextServerManager::get_singleton()->get_interface_count(); i++) {
851
Ref<TextServer> ts = TextServerManager::get_singleton()->get_interface(i);
852
CHECK_FALSE_MESSAGE(ts.is_null(), "Invalid TS interface.");
853
854
if (ts->has_feature(TextServer::FEATURE_SHAPING)) {
855
CHECK(ts->strip_diacritics(U"ٱلسَّلَامُ عَلَيْكُمْ") == U"ٱلسلام عليكم");
856
}
857
858
CHECK(ts->strip_diacritics(U"pêches épinards tomates fraises") == U"peches epinards tomates fraises");
859
CHECK(ts->strip_diacritics(U"ΆΈΉΊΌΎΏΪΫϓϔ") == U"ΑΕΗΙΟΥΩΙΥΥΥ");
860
CHECK(ts->strip_diacritics(U"άέήίΐϊΰϋόύώ") == U"αεηιιιυυουω");
861
CHECK(ts->strip_diacritics(U"ЀЁЃ ЇЌЍӢӤЙ ЎӮӰӲ ӐӒӖӚӜӞ ӦӪ Ӭ Ӵ Ӹ") == U"ЕЕГ ІКИИИИ УУУУ ААЕӘЖЗ ОӨ Э Ч Ы");
862
CHECK(ts->strip_diacritics(U"ѐёѓ їќѝӣӥй ўӯӱӳ ӑӓӗӛӝӟ ӧӫ ӭ ӵ ӹ") == U"еег ікииии уууу ааеәжз оө э ч ы");
863
CHECK(ts->strip_diacritics(U"ÀÁÂÃÄÅĀĂĄÇĆĈĊČĎÈÉÊËĒĔĖĘĚĜĞĠĢĤÌÍÎÏĨĪĬĮİĴĶĹĻĽÑŃŅŇŊÒÓÔÕÖØŌŎŐƠŔŖŘŚŜŞŠŢŤÙÚÛÜŨŪŬŮŰŲƯŴÝŶŹŻŽ") == U"AAAAAAAAACCCCCDEEEEEEEEEGGGGHIIIIIIIIIJKLLLNNNNŊOOOOOØOOOORRRSSSSTTUUUUUUUUUUUWYYZZZ");
864
CHECK(ts->strip_diacritics(U"àáâãäåāăąçćĉċčďèéêëēĕėęěĝğġģĥìíîïĩīĭįĵķĺļľñńņňŋòóôõöøōŏőơŕŗřśŝşšţťùúûüũūŭůűųưŵýÿŷźżž") == U"aaaaaaaaacccccdeeeeeeeeegggghiiiiiiiijklllnnnnŋoooooøoooorrrssssttuuuuuuuuuuuwyyyzzz");
865
CHECK(ts->strip_diacritics(U"ǍǏȈǑǪǬȌȎȪȬȮȰǓǕǗǙǛȔȖǞǠǺȀȂȦǢǼǦǴǨǸȆȐȒȘȚȞȨ Ḁ ḂḄḆ Ḉ ḊḌḎḐḒ ḔḖḘḚḜ Ḟ Ḡ ḢḤḦḨḪ ḬḮ ḰḲḴ ḶḸḺḼ ḾṀṂ ṄṆṈṊ ṌṎṐṒ ṔṖ ṘṚṜṞ ṠṢṤṦṨ ṪṬṮṰ ṲṴṶṸṺ") == U"AIIOOOOOOOOOUUUUUUUAAAAAAÆÆGGKNERRSTHE A BBB C DDDDD EEEEE F G HHHHH II KKK LLLL MMM NNNN OOOO PP RRRR SSSSS TTTT UUUUU");
866
CHECK(ts->strip_diacritics(U"ǎǐȉȋǒǫǭȍȏȫȭȯȱǔǖǘǚǜȕȗǟǡǻȁȃȧǣǽǧǵǩǹȇȑȓșțȟȩ ḁ ḃḅḇ ḉ ḋḍḏḑḓ ḟ ḡ ḭḯ ḱḳḵ ḷḹḻḽ ḿṁṃ ṅṇṉṋ ṍṏṑṓ ṗṕ ṙṛṝṟ ṡṣṥṧṩ ṫṭṯṱ ṳṵṷṹṻ") == U"aiiiooooooooouuuuuuuaaaaaaææggknerrsthe a bbb c ddddd f g ii kkk llll mmm nnnn oooo pp rrrr sssss tttt uuuuu");
867
CHECK(ts->strip_diacritics(U"ṼṾ ẀẂẄẆẈ ẊẌ Ẏ ẐẒẔ") == U"VV WWWWW XX Y ZZZ");
868
CHECK(ts->strip_diacritics(U"ṽṿ ẁẃẅẇẉ ẋẍ ẏ ẑẓẕ ẖ ẗẘẙẛ") == U"vv wwwww xx y zzz h twys");
869
}
870
}
871
872
SUBCASE("[TextServer] Word break") {
873
for (int i = 0; i < TextServerManager::get_singleton()->get_interface_count(); i++) {
874
Ref<TextServer> ts = TextServerManager::get_singleton()->get_interface(i);
875
CHECK_FALSE_MESSAGE(ts.is_null(), "Invalid TS interface.");
876
877
if (!ts->has_feature(TextServer::FEATURE_SIMPLE_LAYOUT)) {
878
continue;
879
}
880
881
{
882
String text1 = U"linguistically similar and effectively form";
883
// 14^ 22^ 26^ 38^
884
PackedInt32Array breaks = ts->string_get_word_breaks(text1, "en");
885
CHECK(breaks.size() == 10);
886
if (breaks.size() == 10) {
887
CHECK(breaks[0] == 0);
888
CHECK(breaks[1] == 14);
889
CHECK(breaks[2] == 15);
890
CHECK(breaks[3] == 22);
891
CHECK(breaks[4] == 23);
892
CHECK(breaks[5] == 26);
893
CHECK(breaks[6] == 27);
894
CHECK(breaks[7] == 38);
895
CHECK(breaks[8] == 39);
896
CHECK(breaks[9] == 43);
897
}
898
}
899
900
if (ts->has_feature(TextServer::FEATURE_BREAK_ITERATORS)) {
901
String text2 = U"เป็นภาษาราชการและภาษาประจำชาติของประเทศไทย";
902
// เป็น ภาษา ราชการ และ ภาษา ประจำ ชาติ ของ ประเทศไทย
903
// 3^ 7^ 13^ 16^ 20^ 25^ 29^ 32^
904
905
PackedInt32Array breaks = ts->string_get_word_breaks(text2, "th");
906
CHECK(breaks.size() == 18);
907
if (breaks.size() == 18) {
908
CHECK(breaks[0] == 0);
909
CHECK(breaks[1] == 4);
910
CHECK(breaks[2] == 4);
911
CHECK(breaks[3] == 8);
912
CHECK(breaks[4] == 8);
913
CHECK(breaks[5] == 14);
914
CHECK(breaks[6] == 14);
915
CHECK(breaks[7] == 17);
916
CHECK(breaks[8] == 17);
917
CHECK(breaks[9] == 21);
918
CHECK(breaks[10] == 21);
919
CHECK(breaks[11] == 26);
920
CHECK(breaks[12] == 26);
921
CHECK(breaks[13] == 30);
922
CHECK(breaks[14] == 30);
923
CHECK(breaks[15] == 33);
924
CHECK(breaks[16] == 33);
925
CHECK(breaks[17] == 42);
926
}
927
}
928
929
if (ts->has_feature(TextServer::FEATURE_BREAK_ITERATORS)) {
930
String text2 = U"U+2764 U+FE0F U+200D U+1F525 ; 13.1 # ❤️‍🔥";
931
932
PackedInt32Array breaks = ts->string_get_character_breaks(text2, "en");
933
CHECK(breaks.size() == 39);
934
if (breaks.size() == 39) {
935
CHECK(breaks[0] == 1);
936
CHECK(breaks[1] == 2);
937
CHECK(breaks[2] == 3);
938
CHECK(breaks[3] == 4);
939
CHECK(breaks[4] == 5);
940
CHECK(breaks[5] == 6);
941
CHECK(breaks[6] == 7);
942
CHECK(breaks[7] == 8);
943
CHECK(breaks[8] == 9);
944
CHECK(breaks[9] == 10);
945
CHECK(breaks[10] == 11);
946
CHECK(breaks[11] == 12);
947
CHECK(breaks[12] == 13);
948
CHECK(breaks[13] == 14);
949
CHECK(breaks[14] == 15);
950
CHECK(breaks[15] == 16);
951
CHECK(breaks[16] == 17);
952
CHECK(breaks[17] == 18);
953
CHECK(breaks[18] == 19);
954
CHECK(breaks[19] == 20);
955
CHECK(breaks[20] == 21);
956
CHECK(breaks[21] == 22);
957
CHECK(breaks[22] == 23);
958
CHECK(breaks[23] == 24);
959
CHECK(breaks[24] == 25);
960
CHECK(breaks[25] == 26);
961
CHECK(breaks[26] == 27);
962
CHECK(breaks[27] == 28);
963
CHECK(breaks[28] == 29);
964
CHECK(breaks[29] == 30);
965
CHECK(breaks[30] == 31);
966
CHECK(breaks[31] == 32);
967
CHECK(breaks[32] == 33);
968
CHECK(breaks[33] == 34);
969
CHECK(breaks[34] == 35);
970
CHECK(breaks[35] == 36);
971
CHECK(breaks[36] == 37);
972
CHECK(breaks[37] == 38);
973
CHECK(breaks[38] == 42);
974
}
975
}
976
}
977
}
978
979
SUBCASE("[TextServer] Buffer invalidation") {
980
for (int i = 0; i < TextServerManager::get_singleton()->get_interface_count(); i++) {
981
Ref<TextServer> ts = TextServerManager::get_singleton()->get_interface(i);
982
CHECK_FALSE_MESSAGE(ts.is_null(), "Invalid TS interface.");
983
984
if (!ts->has_feature(TextServer::FEATURE_SIMPLE_LAYOUT)) {
985
continue;
986
}
987
988
RID font1 = ts->create_font();
989
ts->font_set_data_ptr(font1, _font_Inter_Regular, _font_Inter_Regular_size);
990
991
Array font = { font1 };
992
RID ctx = ts->create_shaped_text();
993
CHECK_FALSE_MESSAGE(ctx == RID(), "Creating text buffer failed.");
994
bool ok = ts->shaped_text_add_string(ctx, "T", font, 16);
995
CHECK_FALSE_MESSAGE(!ok, "Adding text to the buffer failed.");
996
int gl_size = ts->shaped_text_get_glyph_count(ctx);
997
CHECK_MESSAGE(gl_size == 1, "Shaping failed, invalid glyph count");
998
999
ok = ts->shaped_text_add_object(ctx, "key", Size2(20, 20), INLINE_ALIGNMENT_CENTER, 1, 0.0);
1000
CHECK_FALSE_MESSAGE(!ok, "Adding text to the buffer failed.");
1001
gl_size = ts->shaped_text_get_glyph_count(ctx);
1002
CHECK_MESSAGE(gl_size == 2, "Shaping failed, invalid glyph count");
1003
1004
ok = ts->shaped_text_add_string(ctx, "B", font, 16);
1005
CHECK_FALSE_MESSAGE(!ok, "Adding text to the buffer failed.");
1006
gl_size = ts->shaped_text_get_glyph_count(ctx);
1007
CHECK_MESSAGE(gl_size == 3, "Shaping failed, invalid glyph count");
1008
1009
ts->free_rid(ctx);
1010
1011
for (int j = 0; j < font.size(); j++) {
1012
ts->free_rid(font[j]);
1013
}
1014
font.clear();
1015
}
1016
}
1017
}
1018
}
1019
1020
} // namespace TestTextServer
1021
1022
#endif // TOOLS_ENABLED
1023
1024