test_text_server.h 45 KB

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