test_text_server.h 39 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867
  1. /**************************************************************************/
  2. /* test_text_server.h */
  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. #ifndef TEST_TEXT_SERVER_H
  31. #define TEST_TEXT_SERVER_H
  32. #ifdef TOOLS_ENABLED
  33. #include "editor/themes/builtin_fonts.gen.h"
  34. #include "servers/text_server.h"
  35. #include "tests/test_macros.h"
  36. namespace TestTextServer {
  37. TEST_SUITE("[TextServer]") {
  38. TEST_CASE("[TextServer] Init, font loading and shaping") {
  39. SUBCASE("[TextServer] Loading fonts") {
  40. for (int i = 0; i < TextServerManager::get_singleton()->get_interface_count(); i++) {
  41. Ref<TextServer> ts = TextServerManager::get_singleton()->get_interface(i);
  42. CHECK_FALSE_MESSAGE(ts.is_null(), "Invalid TS interface.");
  43. if (!ts->has_feature(TextServer::FEATURE_FONT_DYNAMIC)) {
  44. continue;
  45. }
  46. RID font = ts->create_font();
  47. ts->font_set_data_ptr(font, _font_NotoSans_Regular, _font_NotoSans_Regular_size);
  48. CHECK_FALSE_MESSAGE(font == RID(), "Loading font failed.");
  49. ts->free_rid(font);
  50. }
  51. }
  52. SUBCASE("[TextServer] Text layout: Font fallback") {
  53. for (int i = 0; i < TextServerManager::get_singleton()->get_interface_count(); i++) {
  54. Ref<TextServer> ts = TextServerManager::get_singleton()->get_interface(i);
  55. CHECK_FALSE_MESSAGE(ts.is_null(), "Invalid TS interface.");
  56. if (!ts->has_feature(TextServer::FEATURE_FONT_DYNAMIC) || !ts->has_feature(TextServer::FEATURE_SIMPLE_LAYOUT)) {
  57. continue;
  58. }
  59. RID font1 = ts->create_font();
  60. ts->font_set_data_ptr(font1, _font_NotoSans_Regular, _font_NotoSans_Regular_size);
  61. ts->font_set_allow_system_fallback(font1, false);
  62. RID font2 = ts->create_font();
  63. ts->font_set_data_ptr(font2, _font_NotoSansThai_Regular, _font_NotoSansThai_Regular_size);
  64. ts->font_set_allow_system_fallback(font2, false);
  65. Array font;
  66. font.push_back(font1);
  67. font.push_back(font2);
  68. String test = U"คนอ้วน khon uan ראה";
  69. // 6^ 17^
  70. RID ctx = ts->create_shaped_text();
  71. CHECK_FALSE_MESSAGE(ctx == RID(), "Creating text buffer failed.");
  72. bool ok = ts->shaped_text_add_string(ctx, test, font, 16);
  73. CHECK_FALSE_MESSAGE(!ok, "Adding text to the buffer failed.");
  74. const Glyph *glyphs = ts->shaped_text_get_glyphs(ctx);
  75. int gl_size = ts->shaped_text_get_glyph_count(ctx);
  76. CHECK_FALSE_MESSAGE(gl_size == 0, "Shaping failed");
  77. for (int j = 0; j < gl_size; j++) {
  78. if (glyphs[j].start < 6) {
  79. CHECK_FALSE_MESSAGE(glyphs[j].font_rid != font[1], "Incorrect font selected.");
  80. }
  81. if ((glyphs[j].start > 6) && (glyphs[j].start < 16)) {
  82. CHECK_FALSE_MESSAGE(glyphs[j].font_rid != font[0], "Incorrect font selected.");
  83. }
  84. if (glyphs[j].start > 16) {
  85. CHECK_FALSE_MESSAGE(glyphs[j].font_rid != RID(), "Incorrect font selected.");
  86. CHECK_FALSE_MESSAGE(glyphs[j].index != test[glyphs[j].start], "Incorrect glyph index.");
  87. }
  88. CHECK_FALSE_MESSAGE((glyphs[j].start < 0 || glyphs[j].end > test.length()), "Incorrect glyph range.");
  89. CHECK_FALSE_MESSAGE(glyphs[j].font_size != 16, "Incorrect glyph font size.");
  90. }
  91. ts->free_rid(ctx);
  92. for (int j = 0; j < font.size(); j++) {
  93. ts->free_rid(font[j]);
  94. }
  95. font.clear();
  96. }
  97. }
  98. SUBCASE("[TextServer] Text layout: BiDi") {
  99. for (int i = 0; i < TextServerManager::get_singleton()->get_interface_count(); i++) {
  100. Ref<TextServer> ts = TextServerManager::get_singleton()->get_interface(i);
  101. CHECK_FALSE_MESSAGE(ts.is_null(), "Invalid TS interface.");
  102. if (!ts->has_feature(TextServer::FEATURE_FONT_DYNAMIC) || !ts->has_feature(TextServer::FEATURE_BIDI_LAYOUT)) {
  103. continue;
  104. }
  105. RID font1 = ts->create_font();
  106. ts->font_set_data_ptr(font1, _font_NotoSans_Regular, _font_NotoSans_Regular_size);
  107. RID font2 = ts->create_font();
  108. ts->font_set_data_ptr(font2, _font_NotoNaskhArabicUI_Regular, _font_NotoNaskhArabicUI_Regular_size);
  109. Array font;
  110. font.push_back(font1);
  111. font.push_back(font2);
  112. String test = U"Arabic (اَلْعَرَبِيَّةُ, al-ʿarabiyyah)";
  113. // 7^ 26^
  114. RID ctx = ts->create_shaped_text();
  115. CHECK_FALSE_MESSAGE(ctx == RID(), "Creating text buffer failed.");
  116. bool ok = ts->shaped_text_add_string(ctx, test, font, 16);
  117. CHECK_FALSE_MESSAGE(!ok, "Adding text to the buffer failed.");
  118. const Glyph *glyphs = ts->shaped_text_get_glyphs(ctx);
  119. int gl_size = ts->shaped_text_get_glyph_count(ctx);
  120. CHECK_FALSE_MESSAGE(gl_size == 0, "Shaping failed");
  121. for (int j = 0; j < gl_size; j++) {
  122. if (glyphs[j].count > 0) {
  123. if (glyphs[j].start < 7) {
  124. CHECK_FALSE_MESSAGE(((glyphs[j].flags & TextServer::GRAPHEME_IS_RTL) == TextServer::GRAPHEME_IS_RTL), "Incorrect direction.");
  125. }
  126. if ((glyphs[j].start > 8) && (glyphs[j].start < 23)) {
  127. CHECK_FALSE_MESSAGE(((glyphs[j].flags & TextServer::GRAPHEME_IS_RTL) != TextServer::GRAPHEME_IS_RTL), "Incorrect direction.");
  128. }
  129. if (glyphs[j].start > 26) {
  130. CHECK_FALSE_MESSAGE(((glyphs[j].flags & TextServer::GRAPHEME_IS_RTL) == TextServer::GRAPHEME_IS_RTL), "Incorrect direction.");
  131. }
  132. }
  133. }
  134. ts->free_rid(ctx);
  135. for (int j = 0; j < font.size(); j++) {
  136. ts->free_rid(font[j]);
  137. }
  138. font.clear();
  139. }
  140. }
  141. SUBCASE("[TextServer] Text layout: Line break and align points") {
  142. for (int i = 0; i < TextServerManager::get_singleton()->get_interface_count(); i++) {
  143. Ref<TextServer> ts = TextServerManager::get_singleton()->get_interface(i);
  144. CHECK_FALSE_MESSAGE(ts.is_null(), "Invalid TS interface.");
  145. if (!ts->has_feature(TextServer::FEATURE_FONT_DYNAMIC) || !ts->has_feature(TextServer::FEATURE_SIMPLE_LAYOUT)) {
  146. continue;
  147. }
  148. RID font1 = ts->create_font();
  149. ts->font_set_data_ptr(font1, _font_NotoSans_Regular, _font_NotoSans_Regular_size);
  150. ts->font_set_allow_system_fallback(font1, false);
  151. RID font2 = ts->create_font();
  152. ts->font_set_data_ptr(font2, _font_NotoSansThai_Regular, _font_NotoSansThai_Regular_size);
  153. ts->font_set_allow_system_fallback(font2, false);
  154. RID font3 = ts->create_font();
  155. ts->font_set_data_ptr(font3, _font_NotoNaskhArabicUI_Regular, _font_NotoNaskhArabicUI_Regular_size);
  156. ts->font_set_allow_system_fallback(font3, false);
  157. Array font;
  158. font.push_back(font1);
  159. font.push_back(font2);
  160. font.push_back(font3);
  161. {
  162. RID ctx = ts->create_shaped_text();
  163. CHECK_FALSE_MESSAGE(ctx == RID(), "Creating text buffer failed.");
  164. ts->shaped_text_add_string(ctx, U"Xtest", font, 10);
  165. ts->shaped_text_add_string(ctx, U"xs", font, 10);
  166. RID sctx = ts->shaped_text_substr(ctx, 1, 5);
  167. CHECK_FALSE_MESSAGE(sctx == RID(), "Creating substring text buffer failed.");
  168. PackedInt32Array sbrk = ts->shaped_text_get_character_breaks(sctx);
  169. CHECK_FALSE_MESSAGE(sbrk.size() != 5, "Invalid substring char breaks number.");
  170. if (sbrk.size() == 5) {
  171. CHECK_FALSE_MESSAGE(sbrk[0] != 2, "Invalid substring char break position.");
  172. CHECK_FALSE_MESSAGE(sbrk[1] != 3, "Invalid substring char break position.");
  173. CHECK_FALSE_MESSAGE(sbrk[2] != 4, "Invalid substring char break position.");
  174. CHECK_FALSE_MESSAGE(sbrk[3] != 5, "Invalid substring char break position.");
  175. CHECK_FALSE_MESSAGE(sbrk[4] != 6, "Invalid substring char break position.");
  176. }
  177. PackedInt32Array fbrk = ts->shaped_text_get_character_breaks(ctx);
  178. CHECK_FALSE_MESSAGE(fbrk.size() != 7, "Invalid char breaks number.");
  179. if (fbrk.size() == 7) {
  180. CHECK_FALSE_MESSAGE(fbrk[0] != 1, "Invalid char break position.");
  181. CHECK_FALSE_MESSAGE(fbrk[1] != 2, "Invalid char break position.");
  182. CHECK_FALSE_MESSAGE(fbrk[2] != 3, "Invalid char break position.");
  183. CHECK_FALSE_MESSAGE(fbrk[3] != 4, "Invalid char break position.");
  184. CHECK_FALSE_MESSAGE(fbrk[4] != 5, "Invalid char break position.");
  185. CHECK_FALSE_MESSAGE(fbrk[5] != 6, "Invalid char break position.");
  186. CHECK_FALSE_MESSAGE(fbrk[6] != 7, "Invalid char break position.");
  187. }
  188. PackedInt32Array rbrk = ts->string_get_character_breaks(U"Xtestxs");
  189. CHECK_FALSE_MESSAGE(rbrk.size() != 7, "Invalid char breaks number.");
  190. if (rbrk.size() == 7) {
  191. CHECK_FALSE_MESSAGE(rbrk[0] != 1, "Invalid char break position.");
  192. CHECK_FALSE_MESSAGE(rbrk[1] != 2, "Invalid char break position.");
  193. CHECK_FALSE_MESSAGE(rbrk[2] != 3, "Invalid char break position.");
  194. CHECK_FALSE_MESSAGE(rbrk[3] != 4, "Invalid char break position.");
  195. CHECK_FALSE_MESSAGE(rbrk[4] != 5, "Invalid char break position.");
  196. CHECK_FALSE_MESSAGE(rbrk[5] != 6, "Invalid char break position.");
  197. CHECK_FALSE_MESSAGE(rbrk[6] != 7, "Invalid char break position.");
  198. }
  199. ts->free_rid(sctx);
  200. ts->free_rid(ctx);
  201. }
  202. if (ts->has_feature(TextServer::FEATURE_BREAK_ITERATORS)) {
  203. RID ctx = ts->create_shaped_text();
  204. CHECK_FALSE_MESSAGE(ctx == RID(), "Creating text buffer failed.");
  205. ts->shaped_text_add_string(ctx, U"X❤️‍🔥", font, 10);
  206. ts->shaped_text_add_string(ctx, U"xs", font, 10);
  207. RID sctx = ts->shaped_text_substr(ctx, 1, 5);
  208. CHECK_FALSE_MESSAGE(sctx == RID(), "Creating substring text buffer failed.");
  209. PackedInt32Array sbrk = ts->shaped_text_get_character_breaks(sctx);
  210. CHECK_FALSE_MESSAGE(sbrk.size() != 2, "Invalid substring char breaks number.");
  211. if (sbrk.size() == 2) {
  212. CHECK_FALSE_MESSAGE(sbrk[0] != 5, "Invalid substring char break position.");
  213. CHECK_FALSE_MESSAGE(sbrk[1] != 6, "Invalid substring char break position.");
  214. }
  215. PackedInt32Array fbrk = ts->shaped_text_get_character_breaks(ctx);
  216. CHECK_FALSE_MESSAGE(fbrk.size() != 4, "Invalid char breaks number.");
  217. if (fbrk.size() == 4) {
  218. CHECK_FALSE_MESSAGE(fbrk[0] != 1, "Invalid char break position.");
  219. CHECK_FALSE_MESSAGE(fbrk[1] != 5, "Invalid char break position.");
  220. CHECK_FALSE_MESSAGE(fbrk[2] != 6, "Invalid char break position.");
  221. CHECK_FALSE_MESSAGE(fbrk[3] != 7, "Invalid char break position.");
  222. }
  223. PackedInt32Array rbrk = ts->string_get_character_breaks(U"X❤️‍🔥xs");
  224. CHECK_FALSE_MESSAGE(rbrk.size() != 4, "Invalid char breaks number.");
  225. if (rbrk.size() == 4) {
  226. CHECK_FALSE_MESSAGE(rbrk[0] != 1, "Invalid char break position.");
  227. CHECK_FALSE_MESSAGE(rbrk[1] != 5, "Invalid char break position.");
  228. CHECK_FALSE_MESSAGE(rbrk[2] != 6, "Invalid char break position.");
  229. CHECK_FALSE_MESSAGE(rbrk[3] != 7, "Invalid char break position.");
  230. }
  231. ts->free_rid(sctx);
  232. ts->free_rid(ctx);
  233. }
  234. {
  235. String test = U"Test test long text long text\n";
  236. RID ctx = ts->create_shaped_text();
  237. CHECK_FALSE_MESSAGE(ctx == RID(), "Creating text buffer failed.");
  238. bool ok = ts->shaped_text_add_string(ctx, test, font, 16);
  239. CHECK_FALSE_MESSAGE(!ok, "Adding text to the buffer failed.");
  240. ts->shaped_text_update_breaks(ctx);
  241. ts->shaped_text_update_justification_ops(ctx);
  242. const Glyph *glyphs = ts->shaped_text_get_glyphs(ctx);
  243. int gl_size = ts->shaped_text_get_glyph_count(ctx);
  244. CHECK_FALSE_MESSAGE(gl_size != 30, "Invalid glyph count.");
  245. for (int j = 0; j < gl_size; j++) {
  246. bool hard = (glyphs[j].flags & TextServer::GRAPHEME_IS_BREAK_HARD) == TextServer::GRAPHEME_IS_BREAK_HARD;
  247. bool soft = (glyphs[j].flags & TextServer::GRAPHEME_IS_BREAK_SOFT) == TextServer::GRAPHEME_IS_BREAK_SOFT;
  248. bool space = (glyphs[j].flags & TextServer::GRAPHEME_IS_SPACE) == TextServer::GRAPHEME_IS_SPACE;
  249. bool virt = (glyphs[j].flags & TextServer::GRAPHEME_IS_VIRTUAL) == TextServer::GRAPHEME_IS_VIRTUAL;
  250. bool elo = (glyphs[j].flags & TextServer::GRAPHEME_IS_ELONGATION) == TextServer::GRAPHEME_IS_ELONGATION;
  251. if (j == 4 || j == 9 || j == 14 || j == 19 || j == 24) {
  252. CHECK_FALSE_MESSAGE((!soft || !space || hard || virt || elo), "Invalid glyph flags.");
  253. } else if (j == 29) {
  254. CHECK_FALSE_MESSAGE((soft || !space || !hard || virt || elo), "Invalid glyph flags.");
  255. } else {
  256. CHECK_FALSE_MESSAGE((soft || space || hard || virt || elo), "Invalid glyph flags.");
  257. }
  258. }
  259. ts->free_rid(ctx);
  260. }
  261. {
  262. String test = U"الحمـد";
  263. RID ctx = ts->create_shaped_text();
  264. CHECK_FALSE_MESSAGE(ctx == RID(), "Creating text buffer failed.");
  265. bool ok = ts->shaped_text_add_string(ctx, test, font, 16);
  266. CHECK_FALSE_MESSAGE(!ok, "Adding text to the buffer failed.");
  267. ts->shaped_text_update_breaks(ctx);
  268. const Glyph *glyphs = ts->shaped_text_get_glyphs(ctx);
  269. int gl_size = ts->shaped_text_get_glyph_count(ctx);
  270. CHECK_FALSE_MESSAGE(gl_size != 6, "Invalid glyph count.");
  271. for (int j = 0; j < gl_size; j++) {
  272. bool hard = (glyphs[j].flags & TextServer::GRAPHEME_IS_BREAK_HARD) == TextServer::GRAPHEME_IS_BREAK_HARD;
  273. bool soft = (glyphs[j].flags & TextServer::GRAPHEME_IS_BREAK_SOFT) == TextServer::GRAPHEME_IS_BREAK_SOFT;
  274. bool space = (glyphs[j].flags & TextServer::GRAPHEME_IS_SPACE) == TextServer::GRAPHEME_IS_SPACE;
  275. bool virt = (glyphs[j].flags & TextServer::GRAPHEME_IS_VIRTUAL) == TextServer::GRAPHEME_IS_VIRTUAL;
  276. bool elo = (glyphs[j].flags & TextServer::GRAPHEME_IS_ELONGATION) == TextServer::GRAPHEME_IS_ELONGATION;
  277. CHECK_FALSE_MESSAGE((soft || space || hard || virt || elo), "Invalid glyph flags.");
  278. }
  279. if (ts->has_feature(TextServer::FEATURE_KASHIDA_JUSTIFICATION)) {
  280. ts->shaped_text_update_justification_ops(ctx);
  281. glyphs = ts->shaped_text_get_glyphs(ctx);
  282. gl_size = ts->shaped_text_get_glyph_count(ctx);
  283. CHECK_FALSE_MESSAGE(gl_size != 6, "Invalid glyph count.");
  284. for (int j = 0; j < gl_size; j++) {
  285. bool hard = (glyphs[j].flags & TextServer::GRAPHEME_IS_BREAK_HARD) == TextServer::GRAPHEME_IS_BREAK_HARD;
  286. bool soft = (glyphs[j].flags & TextServer::GRAPHEME_IS_BREAK_SOFT) == TextServer::GRAPHEME_IS_BREAK_SOFT;
  287. bool space = (glyphs[j].flags & TextServer::GRAPHEME_IS_SPACE) == TextServer::GRAPHEME_IS_SPACE;
  288. bool virt = (glyphs[j].flags & TextServer::GRAPHEME_IS_VIRTUAL) == TextServer::GRAPHEME_IS_VIRTUAL;
  289. bool elo = (glyphs[j].flags & TextServer::GRAPHEME_IS_ELONGATION) == TextServer::GRAPHEME_IS_ELONGATION;
  290. if (j == 1) {
  291. CHECK_FALSE_MESSAGE((soft || space || hard || virt || !elo), "Invalid glyph flags.");
  292. } else {
  293. CHECK_FALSE_MESSAGE((soft || space || hard || virt || elo), "Invalid glyph flags.");
  294. }
  295. }
  296. }
  297. ts->free_rid(ctx);
  298. }
  299. {
  300. String test = U"الحمد";
  301. RID ctx = ts->create_shaped_text();
  302. CHECK_FALSE_MESSAGE(ctx == RID(), "Creating text buffer failed.");
  303. bool ok = ts->shaped_text_add_string(ctx, test, font, 16);
  304. CHECK_FALSE_MESSAGE(!ok, "Adding text to the buffer failed.");
  305. ts->shaped_text_update_breaks(ctx);
  306. const Glyph *glyphs = ts->shaped_text_get_glyphs(ctx);
  307. int gl_size = ts->shaped_text_get_glyph_count(ctx);
  308. CHECK_FALSE_MESSAGE(gl_size != 5, "Invalid glyph count.");
  309. for (int j = 0; j < gl_size; j++) {
  310. bool hard = (glyphs[j].flags & TextServer::GRAPHEME_IS_BREAK_HARD) == TextServer::GRAPHEME_IS_BREAK_HARD;
  311. bool soft = (glyphs[j].flags & TextServer::GRAPHEME_IS_BREAK_SOFT) == TextServer::GRAPHEME_IS_BREAK_SOFT;
  312. bool space = (glyphs[j].flags & TextServer::GRAPHEME_IS_SPACE) == TextServer::GRAPHEME_IS_SPACE;
  313. bool virt = (glyphs[j].flags & TextServer::GRAPHEME_IS_VIRTUAL) == TextServer::GRAPHEME_IS_VIRTUAL;
  314. bool elo = (glyphs[j].flags & TextServer::GRAPHEME_IS_ELONGATION) == TextServer::GRAPHEME_IS_ELONGATION;
  315. CHECK_FALSE_MESSAGE((soft || space || hard || virt || elo), "Invalid glyph flags.");
  316. }
  317. if (ts->has_feature(TextServer::FEATURE_KASHIDA_JUSTIFICATION)) {
  318. ts->shaped_text_update_justification_ops(ctx);
  319. glyphs = ts->shaped_text_get_glyphs(ctx);
  320. gl_size = ts->shaped_text_get_glyph_count(ctx);
  321. CHECK_FALSE_MESSAGE(gl_size != 6, "Invalid glyph count.");
  322. for (int j = 0; j < gl_size; j++) {
  323. bool hard = (glyphs[j].flags & TextServer::GRAPHEME_IS_BREAK_HARD) == TextServer::GRAPHEME_IS_BREAK_HARD;
  324. bool soft = (glyphs[j].flags & TextServer::GRAPHEME_IS_BREAK_SOFT) == TextServer::GRAPHEME_IS_BREAK_SOFT;
  325. bool space = (glyphs[j].flags & TextServer::GRAPHEME_IS_SPACE) == TextServer::GRAPHEME_IS_SPACE;
  326. bool virt = (glyphs[j].flags & TextServer::GRAPHEME_IS_VIRTUAL) == TextServer::GRAPHEME_IS_VIRTUAL;
  327. bool elo = (glyphs[j].flags & TextServer::GRAPHEME_IS_ELONGATION) == TextServer::GRAPHEME_IS_ELONGATION;
  328. if (j == 1) {
  329. CHECK_FALSE_MESSAGE((soft || space || hard || !virt || !elo), "Invalid glyph flags.");
  330. } else {
  331. CHECK_FALSE_MESSAGE((soft || space || hard || virt || elo), "Invalid glyph flags.");
  332. }
  333. }
  334. }
  335. ts->free_rid(ctx);
  336. }
  337. {
  338. String test = U"الحمـد الرياضي العربي";
  339. RID ctx = ts->create_shaped_text();
  340. CHECK_FALSE_MESSAGE(ctx == RID(), "Creating text buffer failed.");
  341. bool ok = ts->shaped_text_add_string(ctx, test, font, 16);
  342. CHECK_FALSE_MESSAGE(!ok, "Adding text to the buffer failed.");
  343. ts->shaped_text_update_breaks(ctx);
  344. const Glyph *glyphs = ts->shaped_text_get_glyphs(ctx);
  345. int gl_size = ts->shaped_text_get_glyph_count(ctx);
  346. CHECK_FALSE_MESSAGE(gl_size != 21, "Invalid glyph count.");
  347. for (int j = 0; j < gl_size; j++) {
  348. bool hard = (glyphs[j].flags & TextServer::GRAPHEME_IS_BREAK_HARD) == TextServer::GRAPHEME_IS_BREAK_HARD;
  349. bool soft = (glyphs[j].flags & TextServer::GRAPHEME_IS_BREAK_SOFT) == TextServer::GRAPHEME_IS_BREAK_SOFT;
  350. bool space = (glyphs[j].flags & TextServer::GRAPHEME_IS_SPACE) == TextServer::GRAPHEME_IS_SPACE;
  351. bool virt = (glyphs[j].flags & TextServer::GRAPHEME_IS_VIRTUAL) == TextServer::GRAPHEME_IS_VIRTUAL;
  352. bool elo = (glyphs[j].flags & TextServer::GRAPHEME_IS_ELONGATION) == TextServer::GRAPHEME_IS_ELONGATION;
  353. if (j == 6 || j == 14) {
  354. CHECK_FALSE_MESSAGE((!soft || !space || hard || virt || elo), "Invalid glyph flags.");
  355. } else {
  356. CHECK_FALSE_MESSAGE((soft || space || hard || virt || elo), "Invalid glyph flags.");
  357. }
  358. }
  359. if (ts->has_feature(TextServer::FEATURE_KASHIDA_JUSTIFICATION)) {
  360. ts->shaped_text_update_justification_ops(ctx);
  361. glyphs = ts->shaped_text_get_glyphs(ctx);
  362. gl_size = ts->shaped_text_get_glyph_count(ctx);
  363. CHECK_FALSE_MESSAGE(gl_size != 23, "Invalid glyph count.");
  364. for (int j = 0; j < gl_size; j++) {
  365. bool hard = (glyphs[j].flags & TextServer::GRAPHEME_IS_BREAK_HARD) == TextServer::GRAPHEME_IS_BREAK_HARD;
  366. bool soft = (glyphs[j].flags & TextServer::GRAPHEME_IS_BREAK_SOFT) == TextServer::GRAPHEME_IS_BREAK_SOFT;
  367. bool space = (glyphs[j].flags & TextServer::GRAPHEME_IS_SPACE) == TextServer::GRAPHEME_IS_SPACE;
  368. bool virt = (glyphs[j].flags & TextServer::GRAPHEME_IS_VIRTUAL) == TextServer::GRAPHEME_IS_VIRTUAL;
  369. bool elo = (glyphs[j].flags & TextServer::GRAPHEME_IS_ELONGATION) == TextServer::GRAPHEME_IS_ELONGATION;
  370. if (j == 7 || j == 16) {
  371. CHECK_FALSE_MESSAGE((!soft || !space || hard || virt || elo), "Invalid glyph flags.");
  372. } else if (j == 3 || j == 9) {
  373. CHECK_FALSE_MESSAGE((soft || space || hard || !virt || !elo), "Invalid glyph flags.");
  374. } else if (j == 18) {
  375. CHECK_FALSE_MESSAGE((soft || space || hard || virt || !elo), "Invalid glyph flags.");
  376. } else {
  377. CHECK_FALSE_MESSAGE((soft || space || hard || virt || elo), "Invalid glyph flags.");
  378. }
  379. }
  380. }
  381. ts->free_rid(ctx);
  382. }
  383. {
  384. String test = U"เป็น ภาษา ราชการ และ ภาษา";
  385. RID ctx = ts->create_shaped_text();
  386. CHECK_FALSE_MESSAGE(ctx == RID(), "Creating text buffer failed.");
  387. bool ok = ts->shaped_text_add_string(ctx, test, font, 16);
  388. CHECK_FALSE_MESSAGE(!ok, "Adding text to the buffer failed.");
  389. ts->shaped_text_update_breaks(ctx);
  390. ts->shaped_text_update_justification_ops(ctx);
  391. const Glyph *glyphs = ts->shaped_text_get_glyphs(ctx);
  392. int gl_size = ts->shaped_text_get_glyph_count(ctx);
  393. CHECK_FALSE_MESSAGE(gl_size != 25, "Invalid glyph count.");
  394. for (int j = 0; j < gl_size; j++) {
  395. bool hard = (glyphs[j].flags & TextServer::GRAPHEME_IS_BREAK_HARD) == TextServer::GRAPHEME_IS_BREAK_HARD;
  396. bool soft = (glyphs[j].flags & TextServer::GRAPHEME_IS_BREAK_SOFT) == TextServer::GRAPHEME_IS_BREAK_SOFT;
  397. bool space = (glyphs[j].flags & TextServer::GRAPHEME_IS_SPACE) == TextServer::GRAPHEME_IS_SPACE;
  398. bool virt = (glyphs[j].flags & TextServer::GRAPHEME_IS_VIRTUAL) == TextServer::GRAPHEME_IS_VIRTUAL;
  399. bool elo = (glyphs[j].flags & TextServer::GRAPHEME_IS_ELONGATION) == TextServer::GRAPHEME_IS_ELONGATION;
  400. if (j == 4 || j == 9 || j == 16 || j == 20) {
  401. CHECK_FALSE_MESSAGE((!soft || !space || hard || virt || elo), "Invalid glyph flags.");
  402. } else {
  403. CHECK_FALSE_MESSAGE((soft || space || hard || virt || elo), "Invalid glyph flags.");
  404. }
  405. }
  406. ts->free_rid(ctx);
  407. }
  408. if (ts->has_feature(TextServer::FEATURE_BREAK_ITERATORS)) {
  409. String test = U"เป็นภาษาราชการและภาษา";
  410. RID ctx = ts->create_shaped_text();
  411. CHECK_FALSE_MESSAGE(ctx == RID(), "Creating text buffer failed.");
  412. bool ok = ts->shaped_text_add_string(ctx, test, font, 16);
  413. CHECK_FALSE_MESSAGE(!ok, "Adding text to the buffer failed.");
  414. ts->shaped_text_update_breaks(ctx);
  415. ts->shaped_text_update_justification_ops(ctx);
  416. const Glyph *glyphs = ts->shaped_text_get_glyphs(ctx);
  417. int gl_size = ts->shaped_text_get_glyph_count(ctx);
  418. CHECK_FALSE_MESSAGE(gl_size != 25, "Invalid glyph count.");
  419. for (int j = 0; j < gl_size; j++) {
  420. bool hard = (glyphs[j].flags & TextServer::GRAPHEME_IS_BREAK_HARD) == TextServer::GRAPHEME_IS_BREAK_HARD;
  421. bool soft = (glyphs[j].flags & TextServer::GRAPHEME_IS_BREAK_SOFT) == TextServer::GRAPHEME_IS_BREAK_SOFT;
  422. bool space = (glyphs[j].flags & TextServer::GRAPHEME_IS_SPACE) == TextServer::GRAPHEME_IS_SPACE;
  423. bool virt = (glyphs[j].flags & TextServer::GRAPHEME_IS_VIRTUAL) == TextServer::GRAPHEME_IS_VIRTUAL;
  424. bool elo = (glyphs[j].flags & TextServer::GRAPHEME_IS_ELONGATION) == TextServer::GRAPHEME_IS_ELONGATION;
  425. if (j == 4 || j == 9 || j == 16 || j == 20) {
  426. CHECK_FALSE_MESSAGE((!soft || !space || hard || !virt || elo), "Invalid glyph flags.");
  427. } else {
  428. CHECK_FALSE_MESSAGE((soft || space || hard || virt || elo), "Invalid glyph flags.");
  429. }
  430. }
  431. ts->free_rid(ctx);
  432. }
  433. for (int j = 0; j < font.size(); j++) {
  434. ts->free_rid(font[j]);
  435. }
  436. font.clear();
  437. }
  438. }
  439. SUBCASE("[TextServer] Text layout: Line breaking") {
  440. for (int i = 0; i < TextServerManager::get_singleton()->get_interface_count(); i++) {
  441. Ref<TextServer> ts = TextServerManager::get_singleton()->get_interface(i);
  442. CHECK_FALSE_MESSAGE(ts.is_null(), "Invalid TS interface.");
  443. if (!ts->has_feature(TextServer::FEATURE_FONT_DYNAMIC) || !ts->has_feature(TextServer::FEATURE_SIMPLE_LAYOUT)) {
  444. continue;
  445. }
  446. String test_1 = U"test test test";
  447. // 5^ 10^
  448. RID font1 = ts->create_font();
  449. ts->font_set_data_ptr(font1, _font_NotoSans_Regular, _font_NotoSans_Regular_size);
  450. RID font2 = ts->create_font();
  451. ts->font_set_data_ptr(font2, _font_NotoSansThai_Regular, _font_NotoSansThai_Regular_size);
  452. Array font;
  453. font.push_back(font1);
  454. font.push_back(font2);
  455. RID ctx = ts->create_shaped_text();
  456. CHECK_FALSE_MESSAGE(ctx == RID(), "Creating text buffer failed.");
  457. bool ok = ts->shaped_text_add_string(ctx, test_1, font, 16);
  458. CHECK_FALSE_MESSAGE(!ok, "Adding text to the buffer failed.");
  459. PackedInt32Array brks = ts->shaped_text_get_line_breaks(ctx, 1);
  460. CHECK_FALSE_MESSAGE(brks.size() != 6, "Invalid line breaks number.");
  461. if (brks.size() == 6) {
  462. CHECK_FALSE_MESSAGE(brks[0] != 0, "Invalid line break position.");
  463. CHECK_FALSE_MESSAGE(brks[1] != 5, "Invalid line break position.");
  464. CHECK_FALSE_MESSAGE(brks[2] != 5, "Invalid line break position.");
  465. CHECK_FALSE_MESSAGE(brks[3] != 10, "Invalid line break position.");
  466. CHECK_FALSE_MESSAGE(brks[4] != 10, "Invalid line break position.");
  467. CHECK_FALSE_MESSAGE(brks[5] != 14, "Invalid line break position.");
  468. }
  469. ts->free_rid(ctx);
  470. for (int j = 0; j < font.size(); j++) {
  471. ts->free_rid(font[j]);
  472. }
  473. font.clear();
  474. }
  475. }
  476. SUBCASE("[TextServer] Text layout: Justification") {
  477. for (int i = 0; i < TextServerManager::get_singleton()->get_interface_count(); i++) {
  478. Ref<TextServer> ts = TextServerManager::get_singleton()->get_interface(i);
  479. CHECK_FALSE_MESSAGE(ts.is_null(), "Invalid TS interface.");
  480. if (!ts->has_feature(TextServer::FEATURE_FONT_DYNAMIC) || !ts->has_feature(TextServer::FEATURE_SIMPLE_LAYOUT)) {
  481. continue;
  482. }
  483. RID font1 = ts->create_font();
  484. ts->font_set_data_ptr(font1, _font_NotoSans_Regular, _font_NotoSans_Regular_size);
  485. RID font2 = ts->create_font();
  486. ts->font_set_data_ptr(font2, _font_NotoNaskhArabicUI_Regular, _font_NotoNaskhArabicUI_Regular_size);
  487. Array font;
  488. font.push_back(font1);
  489. font.push_back(font2);
  490. String test_1 = U"الحمد";
  491. String test_2 = U"الحمد test";
  492. String test_3 = U"test test";
  493. // 7^ 26^
  494. RID ctx;
  495. bool ok;
  496. float width_old, width;
  497. if (ts->has_feature(TextServer::FEATURE_KASHIDA_JUSTIFICATION)) {
  498. ctx = ts->create_shaped_text();
  499. CHECK_FALSE_MESSAGE(ctx == RID(), "Creating text buffer failed.");
  500. ok = ts->shaped_text_add_string(ctx, test_1, font, 16);
  501. CHECK_FALSE_MESSAGE(!ok, "Adding text to the buffer failed.");
  502. width_old = ts->shaped_text_get_width(ctx);
  503. width = ts->shaped_text_fit_to_width(ctx, 100, TextServer::JUSTIFICATION_WORD_BOUND);
  504. CHECK_FALSE_MESSAGE((width != width_old), "Invalid fill width.");
  505. width = ts->shaped_text_fit_to_width(ctx, 100, TextServer::JUSTIFICATION_WORD_BOUND | TextServer::JUSTIFICATION_KASHIDA);
  506. CHECK_FALSE_MESSAGE((width <= width_old || width > 100), "Invalid fill width.");
  507. ts->free_rid(ctx);
  508. ctx = ts->create_shaped_text();
  509. CHECK_FALSE_MESSAGE(ctx == RID(), "Creating text buffer failed.");
  510. ok = ts->shaped_text_add_string(ctx, test_2, font, 16);
  511. CHECK_FALSE_MESSAGE(!ok, "Adding text to the buffer failed.");
  512. width_old = ts->shaped_text_get_width(ctx);
  513. width = ts->shaped_text_fit_to_width(ctx, 100, TextServer::JUSTIFICATION_WORD_BOUND);
  514. CHECK_FALSE_MESSAGE((width <= width_old || width > 100), "Invalid fill width.");
  515. width = ts->shaped_text_fit_to_width(ctx, 100, TextServer::JUSTIFICATION_WORD_BOUND | TextServer::JUSTIFICATION_KASHIDA);
  516. CHECK_FALSE_MESSAGE((width <= width_old || width > 100), "Invalid fill width.");
  517. ts->free_rid(ctx);
  518. }
  519. ctx = ts->create_shaped_text();
  520. CHECK_FALSE_MESSAGE(ctx == RID(), "Creating text buffer failed.");
  521. ok = ts->shaped_text_add_string(ctx, test_3, font, 16);
  522. CHECK_FALSE_MESSAGE(!ok, "Adding text to the buffer failed.");
  523. width_old = ts->shaped_text_get_width(ctx);
  524. width = ts->shaped_text_fit_to_width(ctx, 100, TextServer::JUSTIFICATION_WORD_BOUND);
  525. CHECK_FALSE_MESSAGE((width <= width_old || width > 100), "Invalid fill width.");
  526. ts->free_rid(ctx);
  527. for (int j = 0; j < font.size(); j++) {
  528. ts->free_rid(font[j]);
  529. }
  530. font.clear();
  531. }
  532. }
  533. SUBCASE("[TextServer] Unicode identifiers") {
  534. for (int i = 0; i < TextServerManager::get_singleton()->get_interface_count(); i++) {
  535. Ref<TextServer> ts = TextServerManager::get_singleton()->get_interface(i);
  536. CHECK_FALSE_MESSAGE(ts.is_null(), "Invalid TS interface.");
  537. 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"ÂÃÄÅĀĂĄÇĆĈĊ" };
  538. static bool isid[19] = { false, false, false, false, false, false, false, false, true, true, false, false, true, false, true, true, true, true, true };
  539. for (int j = 0; j < 19; j++) {
  540. String s = String(data[j]);
  541. CHECK(ts->is_valid_identifier(s) == isid[j]);
  542. }
  543. if (ts->has_feature(TextServer::FEATURE_UNICODE_IDENTIFIERS)) {
  544. // Test UAX 3.2 ZW(N)J usage.
  545. CHECK(ts->is_valid_identifier(U"\u0646\u0627\u0645\u0647\u200C\u0627\u06CC"));
  546. CHECK(ts->is_valid_identifier(U"\u0D26\u0D43\u0D15\u0D4D\u200C\u0D38\u0D3E\u0D15\u0D4D\u0D37\u0D3F"));
  547. CHECK(ts->is_valid_identifier(U"\u0DC1\u0DCA\u200D\u0DBB\u0DD3"));
  548. }
  549. }
  550. }
  551. SUBCASE("[TextServer] Unicode letters") {
  552. for (int i = 0; i < TextServerManager::get_singleton()->get_interface_count(); i++) {
  553. Ref<TextServer> ts = TextServerManager::get_singleton()->get_interface(i);
  554. CHECK_FALSE_MESSAGE(ts.is_null(), "Invalid TS interface.");
  555. struct ul_testcase {
  556. int fail_index = -1; // Expecting failure at given index.
  557. char32_t text[10]; // Using 0 as the terminator.
  558. };
  559. ul_testcase cases[14] = {
  560. {
  561. 0,
  562. { 0x2D, 0x33, 0x30, 0, 0, 0, 0, 0, 0, 0 }, // "-30"
  563. },
  564. {
  565. 1,
  566. { 0x61, 0x2E, 0x31, 0, 0, 0, 0, 0, 0, 0 }, // "a.1"
  567. },
  568. {
  569. 1,
  570. { 0x61, 0x2C, 0x31, 0, 0, 0, 0, 0, 0, 0 }, // "a,1"
  571. },
  572. {
  573. 0,
  574. { 0x31, 0x65, 0x2D, 0x32, 0, 0, 0, 0, 0, 0 }, // "1e-2"
  575. },
  576. {
  577. 0,
  578. { 0xAB, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, // "Left-Pointing Double Angle Quotation Mark"
  579. },
  580. {
  581. -1,
  582. { 0x41, 0x42, 0, 0, 0, 0, 0, 0, 0, 0 }, // "AB"
  583. },
  584. {
  585. 4,
  586. { 0x54, 0x65, 0x73, 0x74, 0x31, 0, 0, 0, 0, 0 }, // "Test1"
  587. },
  588. {
  589. 2,
  590. { 0x54, 0x65, 0x2A, 0x73, 0x74, 0, 0, 0, 0, 0 }, // "Te*st"
  591. },
  592. {
  593. 4,
  594. { 0x74, 0x65, 0x73, 0x74, 0x5F, 0x74, 0x65, 0x73, 0x74, 0x65 }, // "test_teste"
  595. },
  596. {
  597. 4,
  598. { 0x74, 0x65, 0x73, 0x74, 0x20, 0x74, 0x65, 0x73, 0x74, 0 }, // "test test"
  599. },
  600. {
  601. -1,
  602. { 0x643, 0x402, 0x716, 0xB05, 0, 0, 0, 0, 0, 0 }, // "كЂܖଅ" (arabic letters),
  603. },
  604. {
  605. -1,
  606. { 0x643, 0x402, 0x716, 0xB05, 0x54, 0x65, 0x73, 0x74, 0x30AA, 0x4E21 }, // 0-3 arabic letters, 4-7 latin letters, 8-9 CJK letters
  607. },
  608. {
  609. -1,
  610. { 0x4D2, 0x4D6, 0x4DA, 0x4DC, 0, 0, 0, 0, 0, 0 }, // "ӒӖӚӜ" cyrillic letters
  611. },
  612. {
  613. -1,
  614. { 0xC2, 0xC3, 0xC4, 0xC5, 0x100, 0x102, 0x104, 0xC7, 0x106, 0x108 }, // "ÂÃÄÅĀĂĄÇĆĈ" rarer latin letters
  615. },
  616. };
  617. for (int j = 0; j < 14; j++) {
  618. ul_testcase test = cases[j];
  619. int failed_on_index = -1;
  620. for (int k = 0; k < 10; k++) {
  621. char32_t character = test.text[k];
  622. if (character == 0) {
  623. break;
  624. }
  625. if (!ts->is_valid_letter(character)) {
  626. failed_on_index = k;
  627. break;
  628. }
  629. }
  630. if (test.fail_index == -1) {
  631. 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.");
  632. } else {
  633. 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);
  634. }
  635. }
  636. }
  637. }
  638. SUBCASE("[TextServer] Strip Diacritics") {
  639. for (int i = 0; i < TextServerManager::get_singleton()->get_interface_count(); i++) {
  640. Ref<TextServer> ts = TextServerManager::get_singleton()->get_interface(i);
  641. CHECK_FALSE_MESSAGE(ts.is_null(), "Invalid TS interface.");
  642. if (ts->has_feature(TextServer::FEATURE_SHAPING)) {
  643. CHECK(ts->strip_diacritics(U"ٱلسَّلَامُ عَلَيْكُمْ") == U"ٱلسلام عليكم");
  644. }
  645. CHECK(ts->strip_diacritics(U"pêches épinards tomates fraises") == U"peches epinards tomates fraises");
  646. CHECK(ts->strip_diacritics(U"ΆΈΉΊΌΎΏΪΫϓϔ") == U"ΑΕΗΙΟΥΩΙΥΥΥ");
  647. CHECK(ts->strip_diacritics(U"άέήίΐϊΰϋόύώ") == U"αεηιιιυυουω");
  648. CHECK(ts->strip_diacritics(U"ЀЁЃ ЇЌЍӢӤЙ ЎӮӰӲ ӐӒӖӚӜӞ ӦӪ Ӭ Ӵ Ӹ") == U"ЕЕГ ІКИИИИ УУУУ ААЕӘЖЗ ОӨ Э Ч Ы");
  649. CHECK(ts->strip_diacritics(U"ѐёѓ їќѝӣӥй ўӯӱӳ ӑӓӗӛӝӟ ӧӫ ӭ ӵ ӹ") == U"еег ікииии уууу ааеәжз оө э ч ы");
  650. CHECK(ts->strip_diacritics(U"ÀÁÂÃÄÅĀĂĄÇĆĈĊČĎÈÉÊËĒĔĖĘĚĜĞĠĢĤÌÍÎÏĨĪĬĮİĴĶĹĻĽÑŃŅŇŊÒÓÔÕÖØŌŎŐƠŔŖŘŚŜŞŠŢŤÙÚÛÜŨŪŬŮŰŲƯŴÝŶŹŻŽ") == U"AAAAAAAAACCCCCDEEEEEEEEEGGGGHIIIIIIIIIJKLLLNNNNŊOOOOOØOOOORRRSSSSTTUUUUUUUUUUUWYYZZZ");
  651. CHECK(ts->strip_diacritics(U"àáâãäåāăąçćĉċčďèéêëēĕėęěĝğġģĥìíîïĩīĭįĵķĺļľñńņňŋòóôõöøōŏőơŕŗřśŝşšţťùúûüũūŭůűųưŵýÿŷźżž") == U"aaaaaaaaacccccdeeeeeeeeegggghiiiiiiiijklllnnnnŋoooooøoooorrrssssttuuuuuuuuuuuwyyyzzz");
  652. 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");
  653. 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");
  654. CHECK(ts->strip_diacritics(U"ṼṾ ẀẂẄẆẈ ẊẌ Ẏ ẐẒẔ") == U"VV WWWWW XX Y ZZZ");
  655. CHECK(ts->strip_diacritics(U"ṽṿ ẁẃẅẇẉ ẋẍ ẏ ẑẓẕ ẖ ẗẘẙẛ") == U"vv wwwww xx y zzz h twys");
  656. }
  657. }
  658. SUBCASE("[TextServer] Word break") {
  659. for (int i = 0; i < TextServerManager::get_singleton()->get_interface_count(); i++) {
  660. Ref<TextServer> ts = TextServerManager::get_singleton()->get_interface(i);
  661. if (!ts->has_feature(TextServer::FEATURE_SIMPLE_LAYOUT)) {
  662. continue;
  663. }
  664. CHECK_FALSE_MESSAGE(ts.is_null(), "Invalid TS interface.");
  665. {
  666. String text1 = U"linguistically similar and effectively form";
  667. // 14^ 22^ 26^ 38^
  668. PackedInt32Array breaks = ts->string_get_word_breaks(text1, "en");
  669. CHECK(breaks.size() == 10);
  670. if (breaks.size() == 10) {
  671. CHECK(breaks[0] == 0);
  672. CHECK(breaks[1] == 14);
  673. CHECK(breaks[2] == 15);
  674. CHECK(breaks[3] == 22);
  675. CHECK(breaks[4] == 23);
  676. CHECK(breaks[5] == 26);
  677. CHECK(breaks[6] == 27);
  678. CHECK(breaks[7] == 38);
  679. CHECK(breaks[8] == 39);
  680. CHECK(breaks[9] == 43);
  681. }
  682. }
  683. if (ts->has_feature(TextServer::FEATURE_BREAK_ITERATORS)) {
  684. String text2 = U"เป็นภาษาราชการและภาษาประจำชาติของประเทศไทย";
  685. // เป็น ภาษา ราชการ และ ภาษา ประจำ ชาติ ของ ประเทศไทย
  686. // 3^ 7^ 13^ 16^ 20^ 25^ 29^ 32^
  687. PackedInt32Array breaks = ts->string_get_word_breaks(text2, "th");
  688. CHECK(breaks.size() == 18);
  689. if (breaks.size() == 18) {
  690. CHECK(breaks[0] == 0);
  691. CHECK(breaks[1] == 4);
  692. CHECK(breaks[2] == 4);
  693. CHECK(breaks[3] == 8);
  694. CHECK(breaks[4] == 8);
  695. CHECK(breaks[5] == 14);
  696. CHECK(breaks[6] == 14);
  697. CHECK(breaks[7] == 17);
  698. CHECK(breaks[8] == 17);
  699. CHECK(breaks[9] == 21);
  700. CHECK(breaks[10] == 21);
  701. CHECK(breaks[11] == 26);
  702. CHECK(breaks[12] == 26);
  703. CHECK(breaks[13] == 30);
  704. CHECK(breaks[14] == 30);
  705. CHECK(breaks[15] == 33);
  706. CHECK(breaks[16] == 33);
  707. CHECK(breaks[17] == 42);
  708. }
  709. }
  710. if (ts->has_feature(TextServer::FEATURE_BREAK_ITERATORS)) {
  711. String text2 = U"U+2764 U+FE0F U+200D U+1F525 ; 13.1 # ❤️‍🔥";
  712. PackedInt32Array breaks = ts->string_get_character_breaks(text2, "en");
  713. CHECK(breaks.size() == 39);
  714. if (breaks.size() == 39) {
  715. CHECK(breaks[0] == 1);
  716. CHECK(breaks[1] == 2);
  717. CHECK(breaks[2] == 3);
  718. CHECK(breaks[3] == 4);
  719. CHECK(breaks[4] == 5);
  720. CHECK(breaks[5] == 6);
  721. CHECK(breaks[6] == 7);
  722. CHECK(breaks[7] == 8);
  723. CHECK(breaks[8] == 9);
  724. CHECK(breaks[9] == 10);
  725. CHECK(breaks[10] == 11);
  726. CHECK(breaks[11] == 12);
  727. CHECK(breaks[12] == 13);
  728. CHECK(breaks[13] == 14);
  729. CHECK(breaks[14] == 15);
  730. CHECK(breaks[15] == 16);
  731. CHECK(breaks[16] == 17);
  732. CHECK(breaks[17] == 18);
  733. CHECK(breaks[18] == 19);
  734. CHECK(breaks[19] == 20);
  735. CHECK(breaks[20] == 21);
  736. CHECK(breaks[21] == 22);
  737. CHECK(breaks[22] == 23);
  738. CHECK(breaks[23] == 24);
  739. CHECK(breaks[24] == 25);
  740. CHECK(breaks[25] == 26);
  741. CHECK(breaks[26] == 27);
  742. CHECK(breaks[27] == 28);
  743. CHECK(breaks[28] == 29);
  744. CHECK(breaks[29] == 30);
  745. CHECK(breaks[30] == 31);
  746. CHECK(breaks[31] == 32);
  747. CHECK(breaks[32] == 33);
  748. CHECK(breaks[33] == 34);
  749. CHECK(breaks[34] == 35);
  750. CHECK(breaks[35] == 36);
  751. CHECK(breaks[36] == 37);
  752. CHECK(breaks[37] == 38);
  753. CHECK(breaks[38] == 42);
  754. }
  755. }
  756. }
  757. }
  758. }
  759. }
  760. }; // namespace TestTextServer
  761. #endif // TOOLS_ENABLED
  762. #endif // TEST_TEXT_SERVER_H