test_text_server.h 41 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895
  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_Vazirmatn_Regular, _font_Vazirmatn_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_Vazirmatn_Regular, _font_Vazirmatn_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. if (ts->has_feature(TextServer::FEATURE_BREAK_ITERATORS)) {
  434. struct TestCase {
  435. String text;
  436. PackedInt32Array breaks;
  437. };
  438. TestCase cases[] = {
  439. { U" เมาส์ตัวนี้", { 0, 17, 17, 23 } },
  440. { U" กู้ไฟล์", { 0, 17, 17, 21 } },
  441. { U" ไม่มีคำ", { 0, 18, 18, 20 } },
  442. { U" ไม่มีคำพูด", { 0, 18, 18, 23 } },
  443. { U" ไม่มีคำ", { 0, 17, 17, 19 } },
  444. { U" มีอุปกรณ์\nนี้", { 0, 11, 11, 19, 19, 22 } },
  445. { U"الحمدا لحمدا لحمـــد", { 0, 13, 13, 20 } },
  446. { U" الحمد test", { 0, 15, 15, 19 } },
  447. { U"الحمـد الرياضي العربي", { 0, 7, 7, 15, 15, 21 } },
  448. };
  449. for (size_t j = 0; j < sizeof(cases) / sizeof(TestCase); j++) {
  450. RID ctx = ts->create_shaped_text();
  451. CHECK_FALSE_MESSAGE(ctx == RID(), "Creating text buffer failed.");
  452. bool ok = ts->shaped_text_add_string(ctx, cases[j].text, font, 16);
  453. CHECK_FALSE_MESSAGE(!ok, "Adding text to the buffer failed.");
  454. PackedInt32Array breaks = ts->shaped_text_get_line_breaks(ctx, 90.0);
  455. CHECK_FALSE_MESSAGE(breaks != cases[j].breaks, "Invalid break points.");
  456. ts->free_rid(ctx);
  457. }
  458. }
  459. for (int j = 0; j < font.size(); j++) {
  460. ts->free_rid(font[j]);
  461. }
  462. font.clear();
  463. }
  464. }
  465. SUBCASE("[TextServer] Text layout: Line breaking") {
  466. for (int i = 0; i < TextServerManager::get_singleton()->get_interface_count(); i++) {
  467. Ref<TextServer> ts = TextServerManager::get_singleton()->get_interface(i);
  468. CHECK_FALSE_MESSAGE(ts.is_null(), "Invalid TS interface.");
  469. if (!ts->has_feature(TextServer::FEATURE_FONT_DYNAMIC) || !ts->has_feature(TextServer::FEATURE_SIMPLE_LAYOUT)) {
  470. continue;
  471. }
  472. String test_1 = U"test test test";
  473. // 5^ 10^
  474. RID font1 = ts->create_font();
  475. ts->font_set_data_ptr(font1, _font_NotoSans_Regular, _font_NotoSans_Regular_size);
  476. RID font2 = ts->create_font();
  477. ts->font_set_data_ptr(font2, _font_NotoSansThai_Regular, _font_NotoSansThai_Regular_size);
  478. Array font;
  479. font.push_back(font1);
  480. font.push_back(font2);
  481. RID ctx = ts->create_shaped_text();
  482. CHECK_FALSE_MESSAGE(ctx == RID(), "Creating text buffer failed.");
  483. bool ok = ts->shaped_text_add_string(ctx, test_1, font, 16);
  484. CHECK_FALSE_MESSAGE(!ok, "Adding text to the buffer failed.");
  485. PackedInt32Array brks = ts->shaped_text_get_line_breaks(ctx, 1);
  486. CHECK_FALSE_MESSAGE(brks.size() != 6, "Invalid line breaks number.");
  487. if (brks.size() == 6) {
  488. CHECK_FALSE_MESSAGE(brks[0] != 0, "Invalid line break position.");
  489. CHECK_FALSE_MESSAGE(brks[1] != 5, "Invalid line break position.");
  490. CHECK_FALSE_MESSAGE(brks[2] != 5, "Invalid line break position.");
  491. CHECK_FALSE_MESSAGE(brks[3] != 10, "Invalid line break position.");
  492. CHECK_FALSE_MESSAGE(brks[4] != 10, "Invalid line break position.");
  493. CHECK_FALSE_MESSAGE(brks[5] != 14, "Invalid line break position.");
  494. }
  495. ts->free_rid(ctx);
  496. for (int j = 0; j < font.size(); j++) {
  497. ts->free_rid(font[j]);
  498. }
  499. font.clear();
  500. }
  501. }
  502. SUBCASE("[TextServer] Text layout: Justification") {
  503. for (int i = 0; i < TextServerManager::get_singleton()->get_interface_count(); i++) {
  504. Ref<TextServer> ts = TextServerManager::get_singleton()->get_interface(i);
  505. CHECK_FALSE_MESSAGE(ts.is_null(), "Invalid TS interface.");
  506. if (!ts->has_feature(TextServer::FEATURE_FONT_DYNAMIC) || !ts->has_feature(TextServer::FEATURE_SIMPLE_LAYOUT)) {
  507. continue;
  508. }
  509. RID font1 = ts->create_font();
  510. ts->font_set_data_ptr(font1, _font_NotoSans_Regular, _font_NotoSans_Regular_size);
  511. RID font2 = ts->create_font();
  512. ts->font_set_data_ptr(font2, _font_Vazirmatn_Regular, _font_Vazirmatn_Regular_size);
  513. Array font;
  514. font.push_back(font1);
  515. font.push_back(font2);
  516. String test_1 = U"الحمد";
  517. String test_2 = U"الحمد test";
  518. String test_3 = U"test test";
  519. // 7^ 26^
  520. RID ctx;
  521. bool ok;
  522. float width_old, width;
  523. if (ts->has_feature(TextServer::FEATURE_KASHIDA_JUSTIFICATION)) {
  524. ctx = ts->create_shaped_text();
  525. CHECK_FALSE_MESSAGE(ctx == RID(), "Creating text buffer failed.");
  526. ok = ts->shaped_text_add_string(ctx, test_1, font, 16);
  527. CHECK_FALSE_MESSAGE(!ok, "Adding text to the buffer failed.");
  528. width_old = ts->shaped_text_get_width(ctx);
  529. width = ts->shaped_text_fit_to_width(ctx, 100, TextServer::JUSTIFICATION_WORD_BOUND);
  530. CHECK_FALSE_MESSAGE((width != width_old), "Invalid fill width.");
  531. width = ts->shaped_text_fit_to_width(ctx, 100, TextServer::JUSTIFICATION_WORD_BOUND | TextServer::JUSTIFICATION_KASHIDA);
  532. CHECK_FALSE_MESSAGE((width <= width_old || width > 100), "Invalid fill width.");
  533. ts->free_rid(ctx);
  534. ctx = ts->create_shaped_text();
  535. CHECK_FALSE_MESSAGE(ctx == RID(), "Creating text buffer failed.");
  536. ok = ts->shaped_text_add_string(ctx, test_2, font, 16);
  537. CHECK_FALSE_MESSAGE(!ok, "Adding text to the buffer failed.");
  538. width_old = ts->shaped_text_get_width(ctx);
  539. width = ts->shaped_text_fit_to_width(ctx, 100, TextServer::JUSTIFICATION_WORD_BOUND);
  540. CHECK_FALSE_MESSAGE((width <= width_old || width > 100), "Invalid fill width.");
  541. width = ts->shaped_text_fit_to_width(ctx, 100, TextServer::JUSTIFICATION_WORD_BOUND | TextServer::JUSTIFICATION_KASHIDA);
  542. CHECK_FALSE_MESSAGE((width <= width_old || width > 100), "Invalid fill width.");
  543. ts->free_rid(ctx);
  544. }
  545. ctx = ts->create_shaped_text();
  546. CHECK_FALSE_MESSAGE(ctx == RID(), "Creating text buffer failed.");
  547. ok = ts->shaped_text_add_string(ctx, test_3, font, 16);
  548. CHECK_FALSE_MESSAGE(!ok, "Adding text to the buffer failed.");
  549. width_old = ts->shaped_text_get_width(ctx);
  550. width = ts->shaped_text_fit_to_width(ctx, 100, TextServer::JUSTIFICATION_WORD_BOUND);
  551. CHECK_FALSE_MESSAGE((width <= width_old || width > 100), "Invalid fill width.");
  552. ts->free_rid(ctx);
  553. for (int j = 0; j < font.size(); j++) {
  554. ts->free_rid(font[j]);
  555. }
  556. font.clear();
  557. }
  558. }
  559. SUBCASE("[TextServer] Unicode identifiers") {
  560. for (int i = 0; i < TextServerManager::get_singleton()->get_interface_count(); i++) {
  561. Ref<TextServer> ts = TextServerManager::get_singleton()->get_interface(i);
  562. CHECK_FALSE_MESSAGE(ts.is_null(), "Invalid TS interface.");
  563. 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"ÂÃÄÅĀĂĄÇĆĈĊ" };
  564. static bool isid[19] = { false, false, false, false, false, false, false, false, true, true, false, false, true, false, true, true, true, true, true };
  565. for (int j = 0; j < 19; j++) {
  566. String s = String(data[j]);
  567. CHECK(ts->is_valid_identifier(s) == isid[j]);
  568. }
  569. if (ts->has_feature(TextServer::FEATURE_UNICODE_IDENTIFIERS)) {
  570. // Test UAX 3.2 ZW(N)J usage.
  571. CHECK(ts->is_valid_identifier(U"\u0646\u0627\u0645\u0647\u200C\u0627\u06CC"));
  572. CHECK(ts->is_valid_identifier(U"\u0D26\u0D43\u0D15\u0D4D\u200C\u0D38\u0D3E\u0D15\u0D4D\u0D37\u0D3F"));
  573. CHECK(ts->is_valid_identifier(U"\u0DC1\u0DCA\u200D\u0DBB\u0DD3"));
  574. }
  575. }
  576. }
  577. SUBCASE("[TextServer] Unicode letters") {
  578. for (int i = 0; i < TextServerManager::get_singleton()->get_interface_count(); i++) {
  579. Ref<TextServer> ts = TextServerManager::get_singleton()->get_interface(i);
  580. CHECK_FALSE_MESSAGE(ts.is_null(), "Invalid TS interface.");
  581. struct ul_testcase {
  582. int fail_index = -1; // Expecting failure at given index.
  583. char32_t text[10]; // Using 0 as the terminator.
  584. };
  585. ul_testcase cases[14] = {
  586. {
  587. 0,
  588. { 0x2D, 0x33, 0x30, 0, 0, 0, 0, 0, 0, 0 }, // "-30"
  589. },
  590. {
  591. 1,
  592. { 0x61, 0x2E, 0x31, 0, 0, 0, 0, 0, 0, 0 }, // "a.1"
  593. },
  594. {
  595. 1,
  596. { 0x61, 0x2C, 0x31, 0, 0, 0, 0, 0, 0, 0 }, // "a,1"
  597. },
  598. {
  599. 0,
  600. { 0x31, 0x65, 0x2D, 0x32, 0, 0, 0, 0, 0, 0 }, // "1e-2"
  601. },
  602. {
  603. 0,
  604. { 0xAB, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, // "Left-Pointing Double Angle Quotation Mark"
  605. },
  606. {
  607. -1,
  608. { 0x41, 0x42, 0, 0, 0, 0, 0, 0, 0, 0 }, // "AB"
  609. },
  610. {
  611. 4,
  612. { 0x54, 0x65, 0x73, 0x74, 0x31, 0, 0, 0, 0, 0 }, // "Test1"
  613. },
  614. {
  615. 2,
  616. { 0x54, 0x65, 0x2A, 0x73, 0x74, 0, 0, 0, 0, 0 }, // "Te*st"
  617. },
  618. {
  619. 4,
  620. { 0x74, 0x65, 0x73, 0x74, 0x5F, 0x74, 0x65, 0x73, 0x74, 0x65 }, // "test_teste"
  621. },
  622. {
  623. 4,
  624. { 0x74, 0x65, 0x73, 0x74, 0x20, 0x74, 0x65, 0x73, 0x74, 0 }, // "test test"
  625. },
  626. {
  627. -1,
  628. { 0x643, 0x402, 0x716, 0xB05, 0, 0, 0, 0, 0, 0 }, // "كЂܖଅ" (arabic letters),
  629. },
  630. {
  631. -1,
  632. { 0x643, 0x402, 0x716, 0xB05, 0x54, 0x65, 0x73, 0x74, 0x30AA, 0x4E21 }, // 0-3 arabic letters, 4-7 latin letters, 8-9 CJK letters
  633. },
  634. {
  635. -1,
  636. { 0x4D2, 0x4D6, 0x4DA, 0x4DC, 0, 0, 0, 0, 0, 0 }, // "ӒӖӚӜ" cyrillic letters
  637. },
  638. {
  639. -1,
  640. { 0xC2, 0xC3, 0xC4, 0xC5, 0x100, 0x102, 0x104, 0xC7, 0x106, 0x108 }, // "ÂÃÄÅĀĂĄÇĆĈ" rarer latin letters
  641. },
  642. };
  643. for (int j = 0; j < 14; j++) {
  644. ul_testcase test = cases[j];
  645. int failed_on_index = -1;
  646. for (int k = 0; k < 10; k++) {
  647. char32_t character = test.text[k];
  648. if (character == 0) {
  649. break;
  650. }
  651. if (!ts->is_valid_letter(character)) {
  652. failed_on_index = k;
  653. break;
  654. }
  655. }
  656. if (test.fail_index == -1) {
  657. 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.");
  658. } else {
  659. 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);
  660. }
  661. }
  662. }
  663. }
  664. SUBCASE("[TextServer] Strip Diacritics") {
  665. for (int i = 0; i < TextServerManager::get_singleton()->get_interface_count(); i++) {
  666. Ref<TextServer> ts = TextServerManager::get_singleton()->get_interface(i);
  667. CHECK_FALSE_MESSAGE(ts.is_null(), "Invalid TS interface.");
  668. if (ts->has_feature(TextServer::FEATURE_SHAPING)) {
  669. CHECK(ts->strip_diacritics(U"ٱلسَّلَامُ عَلَيْكُمْ") == U"ٱلسلام عليكم");
  670. }
  671. CHECK(ts->strip_diacritics(U"pêches épinards tomates fraises") == U"peches epinards tomates fraises");
  672. CHECK(ts->strip_diacritics(U"ΆΈΉΊΌΎΏΪΫϓϔ") == U"ΑΕΗΙΟΥΩΙΥΥΥ");
  673. CHECK(ts->strip_diacritics(U"άέήίΐϊΰϋόύώ") == U"αεηιιιυυουω");
  674. CHECK(ts->strip_diacritics(U"ЀЁЃ ЇЌЍӢӤЙ ЎӮӰӲ ӐӒӖӚӜӞ ӦӪ Ӭ Ӵ Ӹ") == U"ЕЕГ ІКИИИИ УУУУ ААЕӘЖЗ ОӨ Э Ч Ы");
  675. CHECK(ts->strip_diacritics(U"ѐёѓ їќѝӣӥй ўӯӱӳ ӑӓӗӛӝӟ ӧӫ ӭ ӵ ӹ") == U"еег ікииии уууу ааеәжз оө э ч ы");
  676. CHECK(ts->strip_diacritics(U"ÀÁÂÃÄÅĀĂĄÇĆĈĊČĎÈÉÊËĒĔĖĘĚĜĞĠĢĤÌÍÎÏĨĪĬĮİĴĶĹĻĽÑŃŅŇŊÒÓÔÕÖØŌŎŐƠŔŖŘŚŜŞŠŢŤÙÚÛÜŨŪŬŮŰŲƯŴÝŶŹŻŽ") == U"AAAAAAAAACCCCCDEEEEEEEEEGGGGHIIIIIIIIIJKLLLNNNNŊOOOOOØOOOORRRSSSSTTUUUUUUUUUUUWYYZZZ");
  677. CHECK(ts->strip_diacritics(U"àáâãäåāăąçćĉċčďèéêëēĕėęěĝğġģĥìíîïĩīĭįĵķĺļľñńņňŋòóôõöøōŏőơŕŗřśŝşšţťùúûüũūŭůűųưŵýÿŷźżž") == U"aaaaaaaaacccccdeeeeeeeeegggghiiiiiiiijklllnnnnŋoooooøoooorrrssssttuuuuuuuuuuuwyyyzzz");
  678. 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");
  679. 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");
  680. CHECK(ts->strip_diacritics(U"ṼṾ ẀẂẄẆẈ ẊẌ Ẏ ẐẒẔ") == U"VV WWWWW XX Y ZZZ");
  681. CHECK(ts->strip_diacritics(U"ṽṿ ẁẃẅẇẉ ẋẍ ẏ ẑẓẕ ẖ ẗẘẙẛ") == U"vv wwwww xx y zzz h twys");
  682. }
  683. }
  684. SUBCASE("[TextServer] Word break") {
  685. for (int i = 0; i < TextServerManager::get_singleton()->get_interface_count(); i++) {
  686. Ref<TextServer> ts = TextServerManager::get_singleton()->get_interface(i);
  687. if (!ts->has_feature(TextServer::FEATURE_SIMPLE_LAYOUT)) {
  688. continue;
  689. }
  690. CHECK_FALSE_MESSAGE(ts.is_null(), "Invalid TS interface.");
  691. {
  692. String text1 = U"linguistically similar and effectively form";
  693. // 14^ 22^ 26^ 38^
  694. PackedInt32Array breaks = ts->string_get_word_breaks(text1, "en");
  695. CHECK(breaks.size() == 10);
  696. if (breaks.size() == 10) {
  697. CHECK(breaks[0] == 0);
  698. CHECK(breaks[1] == 14);
  699. CHECK(breaks[2] == 15);
  700. CHECK(breaks[3] == 22);
  701. CHECK(breaks[4] == 23);
  702. CHECK(breaks[5] == 26);
  703. CHECK(breaks[6] == 27);
  704. CHECK(breaks[7] == 38);
  705. CHECK(breaks[8] == 39);
  706. CHECK(breaks[9] == 43);
  707. }
  708. }
  709. if (ts->has_feature(TextServer::FEATURE_BREAK_ITERATORS)) {
  710. String text2 = U"เป็นภาษาราชการและภาษาประจำชาติของประเทศไทย";
  711. // เป็น ภาษา ราชการ และ ภาษา ประจำ ชาติ ของ ประเทศไทย
  712. // 3^ 7^ 13^ 16^ 20^ 25^ 29^ 32^
  713. PackedInt32Array breaks = ts->string_get_word_breaks(text2, "th");
  714. CHECK(breaks.size() == 18);
  715. if (breaks.size() == 18) {
  716. CHECK(breaks[0] == 0);
  717. CHECK(breaks[1] == 4);
  718. CHECK(breaks[2] == 4);
  719. CHECK(breaks[3] == 8);
  720. CHECK(breaks[4] == 8);
  721. CHECK(breaks[5] == 14);
  722. CHECK(breaks[6] == 14);
  723. CHECK(breaks[7] == 17);
  724. CHECK(breaks[8] == 17);
  725. CHECK(breaks[9] == 21);
  726. CHECK(breaks[10] == 21);
  727. CHECK(breaks[11] == 26);
  728. CHECK(breaks[12] == 26);
  729. CHECK(breaks[13] == 30);
  730. CHECK(breaks[14] == 30);
  731. CHECK(breaks[15] == 33);
  732. CHECK(breaks[16] == 33);
  733. CHECK(breaks[17] == 42);
  734. }
  735. }
  736. if (ts->has_feature(TextServer::FEATURE_BREAK_ITERATORS)) {
  737. String text2 = U"U+2764 U+FE0F U+200D U+1F525 ; 13.1 # ❤️‍🔥";
  738. PackedInt32Array breaks = ts->string_get_character_breaks(text2, "en");
  739. CHECK(breaks.size() == 39);
  740. if (breaks.size() == 39) {
  741. CHECK(breaks[0] == 1);
  742. CHECK(breaks[1] == 2);
  743. CHECK(breaks[2] == 3);
  744. CHECK(breaks[3] == 4);
  745. CHECK(breaks[4] == 5);
  746. CHECK(breaks[5] == 6);
  747. CHECK(breaks[6] == 7);
  748. CHECK(breaks[7] == 8);
  749. CHECK(breaks[8] == 9);
  750. CHECK(breaks[9] == 10);
  751. CHECK(breaks[10] == 11);
  752. CHECK(breaks[11] == 12);
  753. CHECK(breaks[12] == 13);
  754. CHECK(breaks[13] == 14);
  755. CHECK(breaks[14] == 15);
  756. CHECK(breaks[15] == 16);
  757. CHECK(breaks[16] == 17);
  758. CHECK(breaks[17] == 18);
  759. CHECK(breaks[18] == 19);
  760. CHECK(breaks[19] == 20);
  761. CHECK(breaks[20] == 21);
  762. CHECK(breaks[21] == 22);
  763. CHECK(breaks[22] == 23);
  764. CHECK(breaks[23] == 24);
  765. CHECK(breaks[24] == 25);
  766. CHECK(breaks[25] == 26);
  767. CHECK(breaks[26] == 27);
  768. CHECK(breaks[27] == 28);
  769. CHECK(breaks[28] == 29);
  770. CHECK(breaks[29] == 30);
  771. CHECK(breaks[30] == 31);
  772. CHECK(breaks[31] == 32);
  773. CHECK(breaks[32] == 33);
  774. CHECK(breaks[33] == 34);
  775. CHECK(breaks[34] == 35);
  776. CHECK(breaks[35] == 36);
  777. CHECK(breaks[36] == 37);
  778. CHECK(breaks[37] == 38);
  779. CHECK(breaks[38] == 42);
  780. }
  781. }
  782. }
  783. }
  784. }
  785. }
  786. }; // namespace TestTextServer
  787. #endif // TOOLS_ENABLED
  788. #endif // TEST_TEXT_SERVER_H