test_text_server.h 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647
  1. /**************************************************************************/
  2. /* test_text_server.h */
  3. /**************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /**************************************************************************/
  8. /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
  9. /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /**************************************************************************/
  30. #ifndef TEST_TEXT_SERVER_H
  31. #define TEST_TEXT_SERVER_H
  32. #ifdef TOOLS_ENABLED
  33. #include "editor/builtin_fonts.gen.h"
  34. #include "servers/text_server.h"
  35. #include "tests/test_macros.h"
  36. namespace TestTextServer {
  37. TEST_SUITE("[TextServer]") {
  38. TEST_CASE("[TextServer] Init, font loading and shaping") {
  39. SUBCASE("[TextServer] Loading fonts") {
  40. for (int i = 0; i < TextServerManager::get_singleton()->get_interface_count(); i++) {
  41. Ref<TextServer> ts = TextServerManager::get_singleton()->get_interface(i);
  42. CHECK_FALSE_MESSAGE(ts.is_null(), "Invalid TS interface.");
  43. if (!ts->has_feature(TextServer::FEATURE_FONT_DYNAMIC)) {
  44. continue;
  45. }
  46. RID font = ts->create_font();
  47. ts->font_set_data_ptr(font, _font_NotoSans_Regular, _font_NotoSans_Regular_size);
  48. CHECK_FALSE_MESSAGE(font == RID(), "Loading font failed.");
  49. ts->free_rid(font);
  50. }
  51. }
  52. SUBCASE("[TextServer] Text layout: Font fallback") {
  53. for (int i = 0; i < TextServerManager::get_singleton()->get_interface_count(); i++) {
  54. Ref<TextServer> ts = TextServerManager::get_singleton()->get_interface(i);
  55. CHECK_FALSE_MESSAGE(ts.is_null(), "Invalid TS interface.");
  56. if (!ts->has_feature(TextServer::FEATURE_FONT_DYNAMIC) || !ts->has_feature(TextServer::FEATURE_SIMPLE_LAYOUT)) {
  57. continue;
  58. }
  59. RID font1 = ts->create_font();
  60. ts->font_set_data_ptr(font1, _font_NotoSans_Regular, _font_NotoSans_Regular_size);
  61. ts->font_set_allow_system_fallback(font1, false);
  62. RID font2 = ts->create_font();
  63. ts->font_set_data_ptr(font2, _font_NotoSansThaiUI_Regular, _font_NotoSansThaiUI_Regular_size);
  64. ts->font_set_allow_system_fallback(font2, false);
  65. Array font;
  66. font.push_back(font1);
  67. font.push_back(font2);
  68. String test = U"คนอ้วน khon uan ראה";
  69. // 6^ 17^
  70. RID ctx = ts->create_shaped_text();
  71. CHECK_FALSE_MESSAGE(ctx == RID(), "Creating text buffer failed.");
  72. bool ok = ts->shaped_text_add_string(ctx, test, font, 16);
  73. CHECK_FALSE_MESSAGE(!ok, "Adding text to the buffer failed.");
  74. const Glyph *glyphs = ts->shaped_text_get_glyphs(ctx);
  75. int gl_size = ts->shaped_text_get_glyph_count(ctx);
  76. CHECK_FALSE_MESSAGE(gl_size == 0, "Shaping failed");
  77. for (int j = 0; j < gl_size; j++) {
  78. if (glyphs[j].start < 6) {
  79. CHECK_FALSE_MESSAGE(glyphs[j].font_rid != font[1], "Incorrect font selected.");
  80. }
  81. if ((glyphs[j].start > 6) && (glyphs[j].start < 16)) {
  82. CHECK_FALSE_MESSAGE(glyphs[j].font_rid != font[0], "Incorrect font selected.");
  83. }
  84. if (glyphs[j].start > 16) {
  85. CHECK_FALSE_MESSAGE(glyphs[j].font_rid != RID(), "Incorrect font selected.");
  86. CHECK_FALSE_MESSAGE(glyphs[j].index != test[glyphs[j].start], "Incorrect glyph index.");
  87. }
  88. CHECK_FALSE_MESSAGE((glyphs[j].start < 0 || glyphs[j].end > test.length()), "Incorrect glyph range.");
  89. CHECK_FALSE_MESSAGE(glyphs[j].font_size != 16, "Incorrect glyph font size.");
  90. }
  91. ts->free_rid(ctx);
  92. for (int j = 0; j < font.size(); j++) {
  93. ts->free_rid(font[j]);
  94. }
  95. font.clear();
  96. }
  97. }
  98. SUBCASE("[TextServer] Text layout: BiDi") {
  99. for (int i = 0; i < TextServerManager::get_singleton()->get_interface_count(); i++) {
  100. Ref<TextServer> ts = TextServerManager::get_singleton()->get_interface(i);
  101. CHECK_FALSE_MESSAGE(ts.is_null(), "Invalid TS interface.");
  102. if (!ts->has_feature(TextServer::FEATURE_FONT_DYNAMIC) || !ts->has_feature(TextServer::FEATURE_BIDI_LAYOUT)) {
  103. continue;
  104. }
  105. RID font1 = ts->create_font();
  106. ts->font_set_data_ptr(font1, _font_NotoSans_Regular, _font_NotoSans_Regular_size);
  107. RID font2 = ts->create_font();
  108. ts->font_set_data_ptr(font2, _font_NotoNaskhArabicUI_Regular, _font_NotoNaskhArabicUI_Regular_size);
  109. Array font;
  110. font.push_back(font1);
  111. font.push_back(font2);
  112. String test = U"Arabic (اَلْعَرَبِيَّةُ, al-ʿarabiyyah)";
  113. // 7^ 26^
  114. RID ctx = ts->create_shaped_text();
  115. CHECK_FALSE_MESSAGE(ctx == RID(), "Creating text buffer failed.");
  116. bool ok = ts->shaped_text_add_string(ctx, test, font, 16);
  117. CHECK_FALSE_MESSAGE(!ok, "Adding text to the buffer failed.");
  118. const Glyph *glyphs = ts->shaped_text_get_glyphs(ctx);
  119. int gl_size = ts->shaped_text_get_glyph_count(ctx);
  120. CHECK_FALSE_MESSAGE(gl_size == 0, "Shaping failed");
  121. for (int j = 0; j < gl_size; j++) {
  122. if (glyphs[j].count > 0) {
  123. if (glyphs[j].start < 7) {
  124. CHECK_FALSE_MESSAGE(((glyphs[j].flags & TextServer::GRAPHEME_IS_RTL) == TextServer::GRAPHEME_IS_RTL), "Incorrect direction.");
  125. }
  126. if ((glyphs[j].start > 8) && (glyphs[j].start < 23)) {
  127. CHECK_FALSE_MESSAGE(((glyphs[j].flags & TextServer::GRAPHEME_IS_RTL) != TextServer::GRAPHEME_IS_RTL), "Incorrect direction.");
  128. }
  129. if (glyphs[j].start > 26) {
  130. CHECK_FALSE_MESSAGE(((glyphs[j].flags & TextServer::GRAPHEME_IS_RTL) == TextServer::GRAPHEME_IS_RTL), "Incorrect direction.");
  131. }
  132. }
  133. }
  134. ts->free_rid(ctx);
  135. for (int j = 0; j < font.size(); j++) {
  136. ts->free_rid(font[j]);
  137. }
  138. font.clear();
  139. }
  140. }
  141. SUBCASE("[TextServer] Text layout: Line break and align points") {
  142. for (int i = 0; i < TextServerManager::get_singleton()->get_interface_count(); i++) {
  143. Ref<TextServer> ts = TextServerManager::get_singleton()->get_interface(i);
  144. CHECK_FALSE_MESSAGE(ts.is_null(), "Invalid TS interface.");
  145. if (!ts->has_feature(TextServer::FEATURE_FONT_DYNAMIC) || !ts->has_feature(TextServer::FEATURE_SIMPLE_LAYOUT)) {
  146. continue;
  147. }
  148. RID font1 = ts->create_font();
  149. ts->font_set_data_ptr(font1, _font_NotoSans_Regular, _font_NotoSans_Regular_size);
  150. RID font2 = ts->create_font();
  151. ts->font_set_data_ptr(font2, _font_NotoSansThaiUI_Regular, _font_NotoSansThaiUI_Regular_size);
  152. RID font3 = ts->create_font();
  153. ts->font_set_data_ptr(font3, _font_NotoNaskhArabicUI_Regular, _font_NotoNaskhArabicUI_Regular_size);
  154. Array font;
  155. font.push_back(font1);
  156. font.push_back(font2);
  157. font.push_back(font3);
  158. {
  159. String test = U"Test test long text long text\n";
  160. RID ctx = ts->create_shaped_text();
  161. CHECK_FALSE_MESSAGE(ctx == RID(), "Creating text buffer failed.");
  162. bool ok = ts->shaped_text_add_string(ctx, test, font, 16);
  163. CHECK_FALSE_MESSAGE(!ok, "Adding text to the buffer failed.");
  164. ts->shaped_text_update_breaks(ctx);
  165. ts->shaped_text_update_justification_ops(ctx);
  166. const Glyph *glyphs = ts->shaped_text_get_glyphs(ctx);
  167. int gl_size = ts->shaped_text_get_glyph_count(ctx);
  168. CHECK_FALSE_MESSAGE(gl_size != 30, "Invalid glyph count.");
  169. for (int j = 0; j < gl_size; j++) {
  170. bool hard = (glyphs[j].flags & TextServer::GRAPHEME_IS_BREAK_HARD) == TextServer::GRAPHEME_IS_BREAK_HARD;
  171. bool soft = (glyphs[j].flags & TextServer::GRAPHEME_IS_BREAK_SOFT) == TextServer::GRAPHEME_IS_BREAK_SOFT;
  172. bool space = (glyphs[j].flags & TextServer::GRAPHEME_IS_SPACE) == TextServer::GRAPHEME_IS_SPACE;
  173. bool virt = (glyphs[j].flags & TextServer::GRAPHEME_IS_VIRTUAL) == TextServer::GRAPHEME_IS_VIRTUAL;
  174. bool elo = (glyphs[j].flags & TextServer::GRAPHEME_IS_ELONGATION) == TextServer::GRAPHEME_IS_ELONGATION;
  175. if (j == 4 || j == 9 || j == 14 || j == 19 || j == 24) {
  176. CHECK_FALSE_MESSAGE((!soft || !space || hard || virt || elo), "Invalid glyph flags.");
  177. } else if (j == 29) {
  178. CHECK_FALSE_MESSAGE((soft || !space || !hard || virt || elo), "Invalid glyph flags.");
  179. } else {
  180. CHECK_FALSE_MESSAGE((soft || space || hard || virt || elo), "Invalid glyph flags.");
  181. }
  182. }
  183. ts->free_rid(ctx);
  184. }
  185. {
  186. String test = U"الحمـد";
  187. RID ctx = ts->create_shaped_text();
  188. CHECK_FALSE_MESSAGE(ctx == RID(), "Creating text buffer failed.");
  189. bool ok = ts->shaped_text_add_string(ctx, test, font, 16);
  190. CHECK_FALSE_MESSAGE(!ok, "Adding text to the buffer failed.");
  191. ts->shaped_text_update_breaks(ctx);
  192. const Glyph *glyphs = ts->shaped_text_get_glyphs(ctx);
  193. int gl_size = ts->shaped_text_get_glyph_count(ctx);
  194. CHECK_FALSE_MESSAGE(gl_size != 6, "Invalid glyph count.");
  195. for (int j = 0; j < gl_size; j++) {
  196. bool hard = (glyphs[j].flags & TextServer::GRAPHEME_IS_BREAK_HARD) == TextServer::GRAPHEME_IS_BREAK_HARD;
  197. bool soft = (glyphs[j].flags & TextServer::GRAPHEME_IS_BREAK_SOFT) == TextServer::GRAPHEME_IS_BREAK_SOFT;
  198. bool space = (glyphs[j].flags & TextServer::GRAPHEME_IS_SPACE) == TextServer::GRAPHEME_IS_SPACE;
  199. bool virt = (glyphs[j].flags & TextServer::GRAPHEME_IS_VIRTUAL) == TextServer::GRAPHEME_IS_VIRTUAL;
  200. bool elo = (glyphs[j].flags & TextServer::GRAPHEME_IS_ELONGATION) == TextServer::GRAPHEME_IS_ELONGATION;
  201. CHECK_FALSE_MESSAGE((soft || space || hard || virt || elo), "Invalid glyph flags.");
  202. }
  203. if (ts->has_feature(TextServer::FEATURE_KASHIDA_JUSTIFICATION)) {
  204. ts->shaped_text_update_justification_ops(ctx);
  205. glyphs = ts->shaped_text_get_glyphs(ctx);
  206. gl_size = ts->shaped_text_get_glyph_count(ctx);
  207. CHECK_FALSE_MESSAGE(gl_size != 6, "Invalid glyph count.");
  208. for (int j = 0; j < gl_size; j++) {
  209. bool hard = (glyphs[j].flags & TextServer::GRAPHEME_IS_BREAK_HARD) == TextServer::GRAPHEME_IS_BREAK_HARD;
  210. bool soft = (glyphs[j].flags & TextServer::GRAPHEME_IS_BREAK_SOFT) == TextServer::GRAPHEME_IS_BREAK_SOFT;
  211. bool space = (glyphs[j].flags & TextServer::GRAPHEME_IS_SPACE) == TextServer::GRAPHEME_IS_SPACE;
  212. bool virt = (glyphs[j].flags & TextServer::GRAPHEME_IS_VIRTUAL) == TextServer::GRAPHEME_IS_VIRTUAL;
  213. bool elo = (glyphs[j].flags & TextServer::GRAPHEME_IS_ELONGATION) == TextServer::GRAPHEME_IS_ELONGATION;
  214. if (j == 1) {
  215. CHECK_FALSE_MESSAGE((soft || space || hard || virt || !elo), "Invalid glyph flags.");
  216. } else {
  217. CHECK_FALSE_MESSAGE((soft || space || hard || virt || elo), "Invalid glyph flags.");
  218. }
  219. }
  220. }
  221. ts->free_rid(ctx);
  222. }
  223. {
  224. String test = U"الحمد";
  225. RID ctx = ts->create_shaped_text();
  226. CHECK_FALSE_MESSAGE(ctx == RID(), "Creating text buffer failed.");
  227. bool ok = ts->shaped_text_add_string(ctx, test, font, 16);
  228. CHECK_FALSE_MESSAGE(!ok, "Adding text to the buffer failed.");
  229. ts->shaped_text_update_breaks(ctx);
  230. const Glyph *glyphs = ts->shaped_text_get_glyphs(ctx);
  231. int gl_size = ts->shaped_text_get_glyph_count(ctx);
  232. CHECK_FALSE_MESSAGE(gl_size != 5, "Invalid glyph count.");
  233. for (int j = 0; j < gl_size; j++) {
  234. bool hard = (glyphs[j].flags & TextServer::GRAPHEME_IS_BREAK_HARD) == TextServer::GRAPHEME_IS_BREAK_HARD;
  235. bool soft = (glyphs[j].flags & TextServer::GRAPHEME_IS_BREAK_SOFT) == TextServer::GRAPHEME_IS_BREAK_SOFT;
  236. bool space = (glyphs[j].flags & TextServer::GRAPHEME_IS_SPACE) == TextServer::GRAPHEME_IS_SPACE;
  237. bool virt = (glyphs[j].flags & TextServer::GRAPHEME_IS_VIRTUAL) == TextServer::GRAPHEME_IS_VIRTUAL;
  238. bool elo = (glyphs[j].flags & TextServer::GRAPHEME_IS_ELONGATION) == TextServer::GRAPHEME_IS_ELONGATION;
  239. CHECK_FALSE_MESSAGE((soft || space || hard || virt || elo), "Invalid glyph flags.");
  240. }
  241. if (ts->has_feature(TextServer::FEATURE_KASHIDA_JUSTIFICATION)) {
  242. ts->shaped_text_update_justification_ops(ctx);
  243. glyphs = ts->shaped_text_get_glyphs(ctx);
  244. gl_size = ts->shaped_text_get_glyph_count(ctx);
  245. CHECK_FALSE_MESSAGE(gl_size != 6, "Invalid glyph count.");
  246. for (int j = 0; j < gl_size; j++) {
  247. bool hard = (glyphs[j].flags & TextServer::GRAPHEME_IS_BREAK_HARD) == TextServer::GRAPHEME_IS_BREAK_HARD;
  248. bool soft = (glyphs[j].flags & TextServer::GRAPHEME_IS_BREAK_SOFT) == TextServer::GRAPHEME_IS_BREAK_SOFT;
  249. bool space = (glyphs[j].flags & TextServer::GRAPHEME_IS_SPACE) == TextServer::GRAPHEME_IS_SPACE;
  250. bool virt = (glyphs[j].flags & TextServer::GRAPHEME_IS_VIRTUAL) == TextServer::GRAPHEME_IS_VIRTUAL;
  251. bool elo = (glyphs[j].flags & TextServer::GRAPHEME_IS_ELONGATION) == TextServer::GRAPHEME_IS_ELONGATION;
  252. if (j == 1) {
  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. }
  259. ts->free_rid(ctx);
  260. }
  261. {
  262. String test = U"الحمـد الرياضي العربي";
  263. RID ctx = ts->create_shaped_text();
  264. CHECK_FALSE_MESSAGE(ctx == RID(), "Creating text buffer failed.");
  265. bool ok = ts->shaped_text_add_string(ctx, test, font, 16);
  266. CHECK_FALSE_MESSAGE(!ok, "Adding text to the buffer failed.");
  267. ts->shaped_text_update_breaks(ctx);
  268. const Glyph *glyphs = ts->shaped_text_get_glyphs(ctx);
  269. int gl_size = ts->shaped_text_get_glyph_count(ctx);
  270. CHECK_FALSE_MESSAGE(gl_size != 21, "Invalid glyph count.");
  271. for (int j = 0; j < gl_size; j++) {
  272. bool hard = (glyphs[j].flags & TextServer::GRAPHEME_IS_BREAK_HARD) == TextServer::GRAPHEME_IS_BREAK_HARD;
  273. bool soft = (glyphs[j].flags & TextServer::GRAPHEME_IS_BREAK_SOFT) == TextServer::GRAPHEME_IS_BREAK_SOFT;
  274. bool space = (glyphs[j].flags & TextServer::GRAPHEME_IS_SPACE) == TextServer::GRAPHEME_IS_SPACE;
  275. bool virt = (glyphs[j].flags & TextServer::GRAPHEME_IS_VIRTUAL) == TextServer::GRAPHEME_IS_VIRTUAL;
  276. bool elo = (glyphs[j].flags & TextServer::GRAPHEME_IS_ELONGATION) == TextServer::GRAPHEME_IS_ELONGATION;
  277. if (j == 6 || j == 14) {
  278. CHECK_FALSE_MESSAGE((!soft || !space || hard || virt || elo), "Invalid glyph flags.");
  279. } else {
  280. CHECK_FALSE_MESSAGE((soft || space || hard || virt || elo), "Invalid glyph flags.");
  281. }
  282. }
  283. if (ts->has_feature(TextServer::FEATURE_KASHIDA_JUSTIFICATION)) {
  284. ts->shaped_text_update_justification_ops(ctx);
  285. glyphs = ts->shaped_text_get_glyphs(ctx);
  286. gl_size = ts->shaped_text_get_glyph_count(ctx);
  287. CHECK_FALSE_MESSAGE(gl_size != 23, "Invalid glyph count.");
  288. for (int j = 0; j < gl_size; j++) {
  289. bool hard = (glyphs[j].flags & TextServer::GRAPHEME_IS_BREAK_HARD) == TextServer::GRAPHEME_IS_BREAK_HARD;
  290. bool soft = (glyphs[j].flags & TextServer::GRAPHEME_IS_BREAK_SOFT) == TextServer::GRAPHEME_IS_BREAK_SOFT;
  291. bool space = (glyphs[j].flags & TextServer::GRAPHEME_IS_SPACE) == TextServer::GRAPHEME_IS_SPACE;
  292. bool virt = (glyphs[j].flags & TextServer::GRAPHEME_IS_VIRTUAL) == TextServer::GRAPHEME_IS_VIRTUAL;
  293. bool elo = (glyphs[j].flags & TextServer::GRAPHEME_IS_ELONGATION) == TextServer::GRAPHEME_IS_ELONGATION;
  294. if (j == 7 || j == 16) {
  295. CHECK_FALSE_MESSAGE((!soft || !space || hard || virt || elo), "Invalid glyph flags.");
  296. } else if (j == 3 || j == 9) {
  297. CHECK_FALSE_MESSAGE((soft || space || hard || !virt || !elo), "Invalid glyph flags.");
  298. } else if (j == 18) {
  299. CHECK_FALSE_MESSAGE((soft || space || hard || virt || !elo), "Invalid glyph flags.");
  300. } else {
  301. CHECK_FALSE_MESSAGE((soft || space || hard || virt || elo), "Invalid glyph flags.");
  302. }
  303. }
  304. }
  305. ts->free_rid(ctx);
  306. }
  307. {
  308. String test = U"เป็น ภาษา ราชการ และ ภาษา";
  309. RID ctx = ts->create_shaped_text();
  310. CHECK_FALSE_MESSAGE(ctx == RID(), "Creating text buffer failed.");
  311. bool ok = ts->shaped_text_add_string(ctx, test, font, 16);
  312. CHECK_FALSE_MESSAGE(!ok, "Adding text to the buffer failed.");
  313. ts->shaped_text_update_breaks(ctx);
  314. ts->shaped_text_update_justification_ops(ctx);
  315. const Glyph *glyphs = ts->shaped_text_get_glyphs(ctx);
  316. int gl_size = ts->shaped_text_get_glyph_count(ctx);
  317. CHECK_FALSE_MESSAGE(gl_size != 25, "Invalid glyph count.");
  318. for (int j = 0; j < gl_size; j++) {
  319. bool hard = (glyphs[j].flags & TextServer::GRAPHEME_IS_BREAK_HARD) == TextServer::GRAPHEME_IS_BREAK_HARD;
  320. bool soft = (glyphs[j].flags & TextServer::GRAPHEME_IS_BREAK_SOFT) == TextServer::GRAPHEME_IS_BREAK_SOFT;
  321. bool space = (glyphs[j].flags & TextServer::GRAPHEME_IS_SPACE) == TextServer::GRAPHEME_IS_SPACE;
  322. bool virt = (glyphs[j].flags & TextServer::GRAPHEME_IS_VIRTUAL) == TextServer::GRAPHEME_IS_VIRTUAL;
  323. bool elo = (glyphs[j].flags & TextServer::GRAPHEME_IS_ELONGATION) == TextServer::GRAPHEME_IS_ELONGATION;
  324. if (j == 4 || j == 9 || j == 16 || j == 20) {
  325. CHECK_FALSE_MESSAGE((!soft || !space || hard || virt || elo), "Invalid glyph flags.");
  326. } else {
  327. CHECK_FALSE_MESSAGE((soft || space || hard || virt || elo), "Invalid glyph flags.");
  328. }
  329. }
  330. ts->free_rid(ctx);
  331. }
  332. if (ts->has_feature(TextServer::FEATURE_BREAK_ITERATORS)) {
  333. String test = U"เป็นภาษาราชการและภาษา";
  334. RID ctx = ts->create_shaped_text();
  335. CHECK_FALSE_MESSAGE(ctx == RID(), "Creating text buffer failed.");
  336. bool ok = ts->shaped_text_add_string(ctx, test, font, 16);
  337. CHECK_FALSE_MESSAGE(!ok, "Adding text to the buffer failed.");
  338. ts->shaped_text_update_breaks(ctx);
  339. ts->shaped_text_update_justification_ops(ctx);
  340. const Glyph *glyphs = ts->shaped_text_get_glyphs(ctx);
  341. int gl_size = ts->shaped_text_get_glyph_count(ctx);
  342. CHECK_FALSE_MESSAGE(gl_size != 25, "Invalid glyph count.");
  343. for (int j = 0; j < gl_size; j++) {
  344. bool hard = (glyphs[j].flags & TextServer::GRAPHEME_IS_BREAK_HARD) == TextServer::GRAPHEME_IS_BREAK_HARD;
  345. bool soft = (glyphs[j].flags & TextServer::GRAPHEME_IS_BREAK_SOFT) == TextServer::GRAPHEME_IS_BREAK_SOFT;
  346. bool space = (glyphs[j].flags & TextServer::GRAPHEME_IS_SPACE) == TextServer::GRAPHEME_IS_SPACE;
  347. bool virt = (glyphs[j].flags & TextServer::GRAPHEME_IS_VIRTUAL) == TextServer::GRAPHEME_IS_VIRTUAL;
  348. bool elo = (glyphs[j].flags & TextServer::GRAPHEME_IS_ELONGATION) == TextServer::GRAPHEME_IS_ELONGATION;
  349. if (j == 4 || j == 9 || j == 16 || j == 20) {
  350. CHECK_FALSE_MESSAGE((!soft || !space || hard || !virt || elo), "Invalid glyph flags.");
  351. } else {
  352. CHECK_FALSE_MESSAGE((soft || space || hard || virt || elo), "Invalid glyph flags.");
  353. }
  354. }
  355. ts->free_rid(ctx);
  356. }
  357. for (int j = 0; j < font.size(); j++) {
  358. ts->free_rid(font[j]);
  359. }
  360. font.clear();
  361. }
  362. }
  363. SUBCASE("[TextServer] Text layout: Line breaking") {
  364. for (int i = 0; i < TextServerManager::get_singleton()->get_interface_count(); i++) {
  365. Ref<TextServer> ts = TextServerManager::get_singleton()->get_interface(i);
  366. CHECK_FALSE_MESSAGE(ts.is_null(), "Invalid TS interface.");
  367. if (!ts->has_feature(TextServer::FEATURE_FONT_DYNAMIC) || !ts->has_feature(TextServer::FEATURE_SIMPLE_LAYOUT)) {
  368. continue;
  369. }
  370. String test_1 = U"test test test";
  371. // 5^ 10^
  372. RID font1 = ts->create_font();
  373. ts->font_set_data_ptr(font1, _font_NotoSans_Regular, _font_NotoSans_Regular_size);
  374. RID font2 = ts->create_font();
  375. ts->font_set_data_ptr(font2, _font_NotoSansThaiUI_Regular, _font_NotoSansThaiUI_Regular_size);
  376. Array font;
  377. font.push_back(font1);
  378. font.push_back(font2);
  379. RID ctx = ts->create_shaped_text();
  380. CHECK_FALSE_MESSAGE(ctx == RID(), "Creating text buffer failed.");
  381. bool ok = ts->shaped_text_add_string(ctx, test_1, font, 16);
  382. CHECK_FALSE_MESSAGE(!ok, "Adding text to the buffer failed.");
  383. PackedInt32Array brks = ts->shaped_text_get_line_breaks(ctx, 1);
  384. CHECK_FALSE_MESSAGE(brks.size() != 6, "Invalid line breaks number.");
  385. if (brks.size() == 6) {
  386. CHECK_FALSE_MESSAGE(brks[0] != 0, "Invalid line break position.");
  387. CHECK_FALSE_MESSAGE(brks[1] != 5, "Invalid line break position.");
  388. CHECK_FALSE_MESSAGE(brks[2] != 5, "Invalid line break position.");
  389. CHECK_FALSE_MESSAGE(brks[3] != 10, "Invalid line break position.");
  390. CHECK_FALSE_MESSAGE(brks[4] != 10, "Invalid line break position.");
  391. CHECK_FALSE_MESSAGE(brks[5] != 14, "Invalid line break position.");
  392. }
  393. ts->free_rid(ctx);
  394. for (int j = 0; j < font.size(); j++) {
  395. ts->free_rid(font[j]);
  396. }
  397. font.clear();
  398. }
  399. }
  400. SUBCASE("[TextServer] Text layout: Justification") {
  401. for (int i = 0; i < TextServerManager::get_singleton()->get_interface_count(); i++) {
  402. Ref<TextServer> ts = TextServerManager::get_singleton()->get_interface(i);
  403. CHECK_FALSE_MESSAGE(ts.is_null(), "Invalid TS interface.");
  404. if (!ts->has_feature(TextServer::FEATURE_FONT_DYNAMIC) || !ts->has_feature(TextServer::FEATURE_SIMPLE_LAYOUT)) {
  405. continue;
  406. }
  407. RID font1 = ts->create_font();
  408. ts->font_set_data_ptr(font1, _font_NotoSans_Regular, _font_NotoSans_Regular_size);
  409. RID font2 = ts->create_font();
  410. ts->font_set_data_ptr(font2, _font_NotoNaskhArabicUI_Regular, _font_NotoNaskhArabicUI_Regular_size);
  411. Array font;
  412. font.push_back(font1);
  413. font.push_back(font2);
  414. String test_1 = U"الحمد";
  415. String test_2 = U"الحمد test";
  416. String test_3 = U"test test";
  417. // 7^ 26^
  418. RID ctx;
  419. bool ok;
  420. float width_old, width;
  421. if (ts->has_feature(TextServer::FEATURE_KASHIDA_JUSTIFICATION)) {
  422. ctx = ts->create_shaped_text();
  423. CHECK_FALSE_MESSAGE(ctx == RID(), "Creating text buffer failed.");
  424. ok = ts->shaped_text_add_string(ctx, test_1, font, 16);
  425. CHECK_FALSE_MESSAGE(!ok, "Adding text to the buffer failed.");
  426. width_old = ts->shaped_text_get_width(ctx);
  427. width = ts->shaped_text_fit_to_width(ctx, 100, TextServer::JUSTIFICATION_WORD_BOUND);
  428. CHECK_FALSE_MESSAGE((width != width_old), "Invalid fill width.");
  429. width = ts->shaped_text_fit_to_width(ctx, 100, TextServer::JUSTIFICATION_WORD_BOUND | TextServer::JUSTIFICATION_KASHIDA);
  430. CHECK_FALSE_MESSAGE((width <= width_old || width > 100), "Invalid fill width.");
  431. ts->free_rid(ctx);
  432. ctx = ts->create_shaped_text();
  433. CHECK_FALSE_MESSAGE(ctx == RID(), "Creating text buffer failed.");
  434. ok = ts->shaped_text_add_string(ctx, test_2, font, 16);
  435. CHECK_FALSE_MESSAGE(!ok, "Adding text to the buffer failed.");
  436. width_old = ts->shaped_text_get_width(ctx);
  437. width = ts->shaped_text_fit_to_width(ctx, 100, TextServer::JUSTIFICATION_WORD_BOUND);
  438. CHECK_FALSE_MESSAGE((width <= width_old || width > 100), "Invalid fill width.");
  439. width = ts->shaped_text_fit_to_width(ctx, 100, TextServer::JUSTIFICATION_WORD_BOUND | TextServer::JUSTIFICATION_KASHIDA);
  440. CHECK_FALSE_MESSAGE((width <= width_old || width > 100), "Invalid fill width.");
  441. ts->free_rid(ctx);
  442. }
  443. ctx = ts->create_shaped_text();
  444. CHECK_FALSE_MESSAGE(ctx == RID(), "Creating text buffer failed.");
  445. ok = ts->shaped_text_add_string(ctx, test_3, font, 16);
  446. CHECK_FALSE_MESSAGE(!ok, "Adding text to the buffer failed.");
  447. width_old = ts->shaped_text_get_width(ctx);
  448. width = ts->shaped_text_fit_to_width(ctx, 100, TextServer::JUSTIFICATION_WORD_BOUND);
  449. CHECK_FALSE_MESSAGE((width <= width_old || width > 100), "Invalid fill width.");
  450. ts->free_rid(ctx);
  451. for (int j = 0; j < font.size(); j++) {
  452. ts->free_rid(font[j]);
  453. }
  454. font.clear();
  455. }
  456. }
  457. SUBCASE("[TextServer] Unicode identifiers") {
  458. for (int i = 0; i < TextServerManager::get_singleton()->get_interface_count(); i++) {
  459. Ref<TextServer> ts = TextServerManager::get_singleton()->get_interface(i);
  460. CHECK_FALSE_MESSAGE(ts.is_null(), "Invalid TS interface.");
  461. 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"ÂÃÄÅĀĂĄÇĆĈĊ" };
  462. static bool isid[19] = { false, false, false, false, false, false, false, false, true, true, false, false, true, false, true, true, true, true, true };
  463. for (int j = 0; j < 19; j++) {
  464. String s = String(data[j]);
  465. CHECK(ts->is_valid_identifier(s) == isid[j]);
  466. }
  467. if (ts->has_feature(TextServer::FEATURE_UNICODE_IDENTIFIERS)) {
  468. // Test UAX 3.2 ZW(N)J usage.
  469. CHECK(ts->is_valid_identifier(U"\u0646\u0627\u0645\u0647\u200C\u0627\u06CC"));
  470. CHECK(ts->is_valid_identifier(U"\u0D26\u0D43\u0D15\u0D4D\u200C\u0D38\u0D3E\u0D15\u0D4D\u0D37\u0D3F"));
  471. CHECK(ts->is_valid_identifier(U"\u0DC1\u0DCA\u200D\u0DBB\u0DD3"));
  472. }
  473. }
  474. }
  475. SUBCASE("[TextServer] Strip Diacritics") {
  476. for (int i = 0; i < TextServerManager::get_singleton()->get_interface_count(); i++) {
  477. Ref<TextServer> ts = TextServerManager::get_singleton()->get_interface(i);
  478. CHECK_FALSE_MESSAGE(ts.is_null(), "Invalid TS interface.");
  479. if (ts->has_feature(TextServer::FEATURE_SHAPING)) {
  480. CHECK(ts->strip_diacritics(U"ٱلسَّلَامُ عَلَيْكُمْ") == U"ٱلسلام عليكم");
  481. }
  482. CHECK(ts->strip_diacritics(U"pêches épinards tomates fraises") == U"peches epinards tomates fraises");
  483. CHECK(ts->strip_diacritics(U"ΆΈΉΊΌΎΏΪΫϓϔ") == U"ΑΕΗΙΟΥΩΙΥΥΥ");
  484. CHECK(ts->strip_diacritics(U"άέήίΐϊΰϋόύώ") == U"αεηιιιυυουω");
  485. CHECK(ts->strip_diacritics(U"ЀЁЃ ЇЌЍӢӤЙ ЎӮӰӲ ӐӒӖӚӜӞ ӦӪ Ӭ Ӵ Ӹ") == U"ЕЕГ ІКИИИИ УУУУ ААЕӘЖЗ ОӨ Э Ч Ы");
  486. CHECK(ts->strip_diacritics(U"ѐёѓ їќѝӣӥй ўӯӱӳ ӑӓӗӛӝӟ ӧӫ ӭ ӵ ӹ") == U"еег ікииии уууу ааеәжз оө э ч ы");
  487. CHECK(ts->strip_diacritics(U"ÀÁÂÃÄÅĀĂĄÇĆĈĊČĎÈÉÊËĒĔĖĘĚĜĞĠĢĤÌÍÎÏĨĪĬĮİĴĶĹĻĽÑŃŅŇŊÒÓÔÕÖØŌŎŐƠŔŖŘŚŜŞŠŢŤÙÚÛÜŨŪŬŮŰŲƯŴÝŶŹŻŽ") == U"AAAAAAAAACCCCCDEEEEEEEEEGGGGHIIIIIIIIIJKLLLNNNNŊOOOOOØOOOORRRSSSSTTUUUUUUUUUUUWYYZZZ");
  488. CHECK(ts->strip_diacritics(U"àáâãäåāăąçćĉċčďèéêëēĕėęěĝğġģĥìíîïĩīĭįĵķĺļľñńņňŋòóôõöøōŏőơŕŗřśŝşšţťùúûüũūŭůűųưŵýÿŷźżž") == U"aaaaaaaaacccccdeeeeeeeeegggghiiiiiiiijklllnnnnŋoooooøoooorrrssssttuuuuuuuuuuuwyyyzzz");
  489. 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");
  490. 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");
  491. CHECK(ts->strip_diacritics(U"ṼṾ ẀẂẄẆẈ ẊẌ Ẏ ẐẒẔ") == U"VV WWWWW XX Y ZZZ");
  492. CHECK(ts->strip_diacritics(U"ṽṿ ẁẃẅẇẉ ẋẍ ẏ ẑẓẕ ẖ ẗẘẙẛ") == U"vv wwwww xx y zzz h twys");
  493. }
  494. }
  495. SUBCASE("[TextServer] Word break") {
  496. for (int i = 0; i < TextServerManager::get_singleton()->get_interface_count(); i++) {
  497. Ref<TextServer> ts = TextServerManager::get_singleton()->get_interface(i);
  498. if (!ts->has_feature(TextServer::FEATURE_SIMPLE_LAYOUT)) {
  499. continue;
  500. }
  501. CHECK_FALSE_MESSAGE(ts.is_null(), "Invalid TS interface.");
  502. {
  503. String text1 = U"linguistically similar and effectively form";
  504. // 14^ 22^ 26^ 38^
  505. PackedInt32Array breaks = ts->string_get_word_breaks(text1, "en");
  506. CHECK(breaks.size() == 10);
  507. if (breaks.size() == 10) {
  508. CHECK(breaks[0] == 0);
  509. CHECK(breaks[1] == 14);
  510. CHECK(breaks[2] == 15);
  511. CHECK(breaks[3] == 22);
  512. CHECK(breaks[4] == 23);
  513. CHECK(breaks[5] == 26);
  514. CHECK(breaks[6] == 27);
  515. CHECK(breaks[7] == 38);
  516. CHECK(breaks[8] == 39);
  517. CHECK(breaks[9] == 43);
  518. }
  519. }
  520. if (ts->has_feature(TextServer::FEATURE_BREAK_ITERATORS)) {
  521. String text2 = U"เป็นภาษาราชการและภาษาประจำชาติของประเทศไทย";
  522. // เป็น ภาษา ราชการ และ ภาษา ประจำ ชาติ ของ ประเทศไทย
  523. // 3^ 7^ 13^ 16^ 20^ 25^ 29^ 32^
  524. PackedInt32Array breaks = ts->string_get_word_breaks(text2, "th");
  525. CHECK(breaks.size() == 18);
  526. if (breaks.size() == 18) {
  527. CHECK(breaks[0] == 0);
  528. CHECK(breaks[1] == 4);
  529. CHECK(breaks[2] == 4);
  530. CHECK(breaks[3] == 8);
  531. CHECK(breaks[4] == 8);
  532. CHECK(breaks[5] == 14);
  533. CHECK(breaks[6] == 14);
  534. CHECK(breaks[7] == 17);
  535. CHECK(breaks[8] == 17);
  536. CHECK(breaks[9] == 21);
  537. CHECK(breaks[10] == 21);
  538. CHECK(breaks[11] == 26);
  539. CHECK(breaks[12] == 26);
  540. CHECK(breaks[13] == 30);
  541. CHECK(breaks[14] == 30);
  542. CHECK(breaks[15] == 33);
  543. CHECK(breaks[16] == 33);
  544. CHECK(breaks[17] == 42);
  545. }
  546. }
  547. }
  548. }
  549. }
  550. }
  551. }; // namespace TestTextServer
  552. #endif // TOOLS_ENABLED
  553. #endif // TEST_TEXT_SERVER_H