test_text_server.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  1. /*************************************************************************/
  2. /* test_text_server.h */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2021 Godot Engine contributors (cf. AUTHORS.md). */
  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. #ifdef TOOLS_ENABLED
  31. #ifndef TEST_TEXT_SERVER_H
  32. #define TEST_TEXT_SERVER_H
  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. TEST_FAIL_COND(ts.is_null(), "Invalid TS interface.");
  43. RID font = ts->create_font();
  44. ts->font_set_data_ptr(font, _font_NotoSans_Regular, _font_NotoSans_Regular_size);
  45. TEST_FAIL_COND(font == RID(), "Loading font failed.");
  46. ts->free(font);
  47. }
  48. }
  49. SUBCASE("[TextServer] Text layout: Font fallback") {
  50. for (int i = 0; i < TextServerManager::get_singleton()->get_interface_count(); i++) {
  51. Ref<TextServer> ts = TextServerManager::get_singleton()->get_interface(i);
  52. TEST_FAIL_COND(ts.is_null(), "Invalid TS interface.");
  53. RID font1 = ts->create_font();
  54. ts->font_set_data_ptr(font1, _font_NotoSans_Regular, _font_NotoSans_Regular_size);
  55. RID font2 = ts->create_font();
  56. ts->font_set_data_ptr(font2, _font_NotoSansThaiUI_Regular, _font_NotoSansThaiUI_Regular_size);
  57. Vector<RID> font;
  58. font.push_back(font1);
  59. font.push_back(font2);
  60. String test = U"คนอ้วน khon uan ראה";
  61. // 6^ 17^
  62. RID ctx = ts->create_shaped_text();
  63. TEST_FAIL_COND(ctx == RID(), "Creating text buffer failed.");
  64. bool ok = ts->shaped_text_add_string(ctx, test, font, 16);
  65. TEST_FAIL_COND(!ok, "Adding text to the buffer failed.");
  66. const Glyph *glyphs = ts->shaped_text_get_glyphs(ctx);
  67. int gl_size = ts->shaped_text_get_glyph_count(ctx);
  68. TEST_FAIL_COND(gl_size == 0, "Shaping failed");
  69. for (int j = 0; j < gl_size; j++) {
  70. if (glyphs[j].start < 6) {
  71. TEST_FAIL_COND(glyphs[j].font_rid != font[1], "Incorrect font selected.");
  72. }
  73. if ((glyphs[j].start > 6) && (glyphs[j].start < 16)) {
  74. TEST_FAIL_COND(glyphs[j].font_rid != font[0], "Incorrect font selected.");
  75. }
  76. if (glyphs[j].start > 16) {
  77. TEST_FAIL_COND(glyphs[j].font_rid != RID(), "Incorrect font selected.");
  78. TEST_FAIL_COND(glyphs[j].index != test[glyphs[j].start], "Incorrect glyph index.");
  79. }
  80. TEST_FAIL_COND((glyphs[j].start < 0 || glyphs[j].end > test.length()), "Incorrect glyph range.");
  81. TEST_FAIL_COND(glyphs[j].font_size != 16, "Incorrect glyph font size.");
  82. }
  83. ts->free(ctx);
  84. for (int j = 0; j < font.size(); j++) {
  85. ts->free(font[j]);
  86. }
  87. font.clear();
  88. }
  89. }
  90. SUBCASE("[TextServer] Text layout: BiDi") {
  91. for (int i = 0; i < TextServerManager::get_singleton()->get_interface_count(); i++) {
  92. Ref<TextServer> ts = TextServerManager::get_singleton()->get_interface(i);
  93. TEST_FAIL_COND(ts.is_null(), "Invalid TS interface.");
  94. if (!ts->has_feature(TextServer::FEATURE_BIDI_LAYOUT)) {
  95. continue;
  96. }
  97. RID font1 = ts->create_font();
  98. ts->font_set_data_ptr(font1, _font_NotoSans_Regular, _font_NotoSans_Regular_size);
  99. RID font2 = ts->create_font();
  100. ts->font_set_data_ptr(font2, _font_NotoNaskhArabicUI_Regular, _font_NotoNaskhArabicUI_Regular_size);
  101. Vector<RID> font;
  102. font.push_back(font1);
  103. font.push_back(font2);
  104. String test = U"Arabic (اَلْعَرَبِيَّةُ, al-ʿarabiyyah)";
  105. // 7^ 26^
  106. RID ctx = ts->create_shaped_text();
  107. TEST_FAIL_COND(ctx == RID(), "Creating text buffer failed.");
  108. bool ok = ts->shaped_text_add_string(ctx, test, font, 16);
  109. TEST_FAIL_COND(!ok, "Adding text to the buffer failed.");
  110. const Glyph *glyphs = ts->shaped_text_get_glyphs(ctx);
  111. int gl_size = ts->shaped_text_get_glyph_count(ctx);
  112. TEST_FAIL_COND(gl_size == 0, "Shaping failed");
  113. for (int j = 0; j < gl_size; j++) {
  114. if (glyphs[j].count > 0) {
  115. if (glyphs[j].start < 7) {
  116. TEST_FAIL_COND(((glyphs[j].flags & TextServer::GRAPHEME_IS_RTL) == TextServer::GRAPHEME_IS_RTL), "Incorrect direction.");
  117. }
  118. if ((glyphs[j].start > 8) && (glyphs[j].start < 23)) {
  119. TEST_FAIL_COND(((glyphs[j].flags & TextServer::GRAPHEME_IS_RTL) != TextServer::GRAPHEME_IS_RTL), "Incorrect direction.");
  120. }
  121. if (glyphs[j].start > 26) {
  122. TEST_FAIL_COND(((glyphs[j].flags & TextServer::GRAPHEME_IS_RTL) == TextServer::GRAPHEME_IS_RTL), "Incorrect direction.");
  123. }
  124. }
  125. }
  126. ts->free(ctx);
  127. for (int j = 0; j < font.size(); j++) {
  128. ts->free(font[j]);
  129. }
  130. font.clear();
  131. }
  132. }
  133. SUBCASE("[TextServer] Text layout: Line breaking") {
  134. for (int i = 0; i < TextServerManager::get_singleton()->get_interface_count(); i++) {
  135. Ref<TextServer> ts = TextServerManager::get_singleton()->get_interface(i);
  136. TEST_FAIL_COND(ts.is_null(), "Invalid TS interface.");
  137. String test_1 = U"test test test";
  138. // 5^ 10^
  139. RID font1 = ts->create_font();
  140. ts->font_set_data_ptr(font1, _font_NotoSans_Regular, _font_NotoSans_Regular_size);
  141. RID font2 = ts->create_font();
  142. ts->font_set_data_ptr(font2, _font_NotoSansThaiUI_Regular, _font_NotoSansThaiUI_Regular_size);
  143. Vector<RID> font;
  144. font.push_back(font1);
  145. font.push_back(font2);
  146. RID ctx = ts->create_shaped_text();
  147. TEST_FAIL_COND(ctx == RID(), "Creating text buffer failed.");
  148. bool ok = ts->shaped_text_add_string(ctx, test_1, font, 16);
  149. TEST_FAIL_COND(!ok, "Adding text to the buffer failed.");
  150. PackedInt32Array brks = ts->shaped_text_get_line_breaks(ctx, 1);
  151. TEST_FAIL_COND(brks.size() != 6, "Invalid line breaks number.");
  152. if (brks.size() == 6) {
  153. TEST_FAIL_COND(brks[0] != 0, "Invalid line break position.");
  154. TEST_FAIL_COND(brks[1] != 5, "Invalid line break position.");
  155. TEST_FAIL_COND(brks[2] != 5, "Invalid line break position.");
  156. TEST_FAIL_COND(brks[3] != 10, "Invalid line break position.");
  157. TEST_FAIL_COND(brks[4] != 10, "Invalid line break position.");
  158. TEST_FAIL_COND(brks[5] != 14, "Invalid line break position.");
  159. }
  160. ts->free(ctx);
  161. for (int j = 0; j < font.size(); j++) {
  162. ts->free(font[j]);
  163. }
  164. font.clear();
  165. }
  166. }
  167. SUBCASE("[TextServer] Text layout: Justification") {
  168. for (int i = 0; i < TextServerManager::get_singleton()->get_interface_count(); i++) {
  169. Ref<TextServer> ts = TextServerManager::get_singleton()->get_interface(i);
  170. TEST_FAIL_COND(ts.is_null(), "Invalid TS interface.");
  171. RID font1 = ts->create_font();
  172. ts->font_set_data_ptr(font1, _font_NotoSans_Regular, _font_NotoSans_Regular_size);
  173. RID font2 = ts->create_font();
  174. ts->font_set_data_ptr(font2, _font_NotoNaskhArabicUI_Regular, _font_NotoNaskhArabicUI_Regular_size);
  175. Vector<RID> font;
  176. font.push_back(font1);
  177. font.push_back(font2);
  178. String test_1 = U"الحمد";
  179. String test_2 = U"الحمد test";
  180. String test_3 = U"test test";
  181. // 7^ 26^
  182. RID ctx;
  183. bool ok;
  184. float width_old, width;
  185. if (ts->has_feature(TextServer::FEATURE_KASHIDA_JUSTIFICATION)) {
  186. ctx = ts->create_shaped_text();
  187. TEST_FAIL_COND(ctx == RID(), "Creating text buffer failed.");
  188. ok = ts->shaped_text_add_string(ctx, test_1, font, 16);
  189. TEST_FAIL_COND(!ok, "Adding text to the buffer failed.");
  190. width_old = ts->shaped_text_get_width(ctx);
  191. width = ts->shaped_text_fit_to_width(ctx, 100, TextServer::JUSTIFICATION_WORD_BOUND);
  192. TEST_FAIL_COND((width != width_old), "Invalid fill width.");
  193. width = ts->shaped_text_fit_to_width(ctx, 100, TextServer::JUSTIFICATION_WORD_BOUND | TextServer::JUSTIFICATION_KASHIDA);
  194. TEST_FAIL_COND((width <= width_old || width > 100), "Invalid fill width.");
  195. ts->free(ctx);
  196. ctx = ts->create_shaped_text();
  197. TEST_FAIL_COND(ctx == RID(), "Creating text buffer failed.");
  198. ok = ts->shaped_text_add_string(ctx, test_2, font, 16);
  199. TEST_FAIL_COND(!ok, "Adding text to the buffer failed.");
  200. width_old = ts->shaped_text_get_width(ctx);
  201. width = ts->shaped_text_fit_to_width(ctx, 100, TextServer::JUSTIFICATION_WORD_BOUND);
  202. TEST_FAIL_COND((width <= width_old || width > 100), "Invalid fill width.");
  203. width = ts->shaped_text_fit_to_width(ctx, 100, TextServer::JUSTIFICATION_WORD_BOUND | TextServer::JUSTIFICATION_KASHIDA);
  204. TEST_FAIL_COND((width <= width_old || width > 100), "Invalid fill width.");
  205. ts->free(ctx);
  206. }
  207. ctx = ts->create_shaped_text();
  208. TEST_FAIL_COND(ctx == RID(), "Creating text buffer failed.");
  209. ok = ts->shaped_text_add_string(ctx, test_3, font, 16);
  210. TEST_FAIL_COND(!ok, "Adding text to the buffer failed.");
  211. width_old = ts->shaped_text_get_width(ctx);
  212. width = ts->shaped_text_fit_to_width(ctx, 100, TextServer::JUSTIFICATION_WORD_BOUND);
  213. TEST_FAIL_COND((width <= width_old || width > 100), "Invalid fill width.");
  214. ts->free(ctx);
  215. for (int j = 0; j < font.size(); j++) {
  216. ts->free(font[j]);
  217. }
  218. font.clear();
  219. }
  220. }
  221. SUBCASE("[TextServer] Strip Diacritics") {
  222. for (int i = 0; i < TextServerManager::get_singleton()->get_interface_count(); i++) {
  223. Ref<TextServer> ts = TextServerManager::get_singleton()->get_interface(i);
  224. TEST_FAIL_COND(ts.is_null(), "Invalid TS interface.");
  225. if (ts->has_feature(TextServer::FEATURE_SHAPING)) {
  226. CHECK(ts->strip_diacritics(U"ٱلسَّلَامُ عَلَيْكُمْ") == U"ٱلسلام عليكم");
  227. }
  228. CHECK(ts->strip_diacritics(U"pêches épinards tomates fraises") == U"peches epinards tomates fraises");
  229. CHECK(ts->strip_diacritics(U"ΆΈΉΊΌΎΏΪΫϓϔ") == U"ΑΕΗΙΟΥΩΙΥΥΥ");
  230. CHECK(ts->strip_diacritics(U"άέήίΐϊΰϋόύώ") == U"αεηιιιυυουω");
  231. CHECK(ts->strip_diacritics(U"ЀЁЃ ЇЌЍӢӤЙ ЎӮӰӲ ӐӒӖӚӜӞ ӦӪ Ӭ Ӵ Ӹ") == U"ЕЕГ ІКИИИИ УУУУ ААЕӘЖЗ ОӨ Э Ч Ы");
  232. CHECK(ts->strip_diacritics(U"ѐёѓ їќѝӣӥй ўӯӱӳ ӑӓӗӛӝӟ ӧӫ ӭ ӵ ӹ") == U"еег ікииии уууу ааеәжз оө э ч ы");
  233. CHECK(ts->strip_diacritics(U"ÀÁÂÃÄÅĀĂĄÇĆĈĊČĎÈÉÊËĒĔĖĘĚĜĞĠĢĤÌÍÎÏĨĪĬĮİĴĶĹĻĽÑŃŅŇŊÒÓÔÕÖØŌŎŐƠŔŖŘŚŜŞŠŢŤÙÚÛÜŨŪŬŮŰŲƯŴÝŶŹŻŽ") == U"AAAAAAAAACCCCCDEEEEEEEEEGGGGHIIIIIIIIIJKLLLNNNNŊOOOOOØOOOORRRSSSSTTUUUUUUUUUUUWYYZZZ");
  234. CHECK(ts->strip_diacritics(U"àáâãäåāăąçćĉċčďèéêëēĕėęěĝğġģĥìíîïĩīĭįĵķĺļľñńņňŋòóôõöøōŏőơŕŗřśŝşšţťùúûüũūŭůűųưŵýÿŷźżž") == U"aaaaaaaaacccccdeeeeeeeeegggghiiiiiiiijklllnnnnŋoooooøoooorrrssssttuuuuuuuuuuuwyyyzzz");
  235. 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");
  236. 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");
  237. CHECK(ts->strip_diacritics(U"ṼṾ ẀẂẄẆẈ ẊẌ Ẏ ẐẒẔ") == U"VV WWWWW XX Y ZZZ");
  238. CHECK(ts->strip_diacritics(U"ṽṿ ẁẃẅẇẉ ẋẍ ẏ ẑẓẕ ẖ ẗẘẙẛ") == U"vv wwwww xx y zzz h twys");
  239. }
  240. }
  241. }
  242. }
  243. }; // namespace TestTextServer
  244. #endif // TEST_TEXT_SERVER_H
  245. #endif // TOOLS_ENABLED