test_text_server.h 45 KB

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