test_text_server.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  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. #ifndef TEST_TEXT_SERVER_H
  31. #define TEST_TEXT_SERVER_H
  32. #include "editor/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. TextServerManager *tsman = memnew(TextServerManager);
  39. Error err = OK;
  40. SUBCASE("[TextServer] Init") {
  41. for (int i = 0; i < TextServerManager::get_interface_count(); i++) {
  42. TextServer *ts = TextServerManager::initialize(i, err);
  43. TEST_FAIL_COND((err != OK || ts == nullptr), "Text server ", TextServerManager::get_interface_name(i), " init failed.");
  44. }
  45. }
  46. SUBCASE("[TextServer] Loading fonts") {
  47. for (int i = 0; i < TextServerManager::get_interface_count(); i++) {
  48. TextServer *ts = TextServerManager::initialize(i, err);
  49. RID font = ts->create_font_memory(_font_NotoSansUI_Regular, _font_NotoSansUI_Regular_size, "ttf");
  50. TEST_FAIL_COND(font == RID(), "Loading font failed.");
  51. ts->free(font);
  52. }
  53. }
  54. SUBCASE("[TextServer] Text layout: Font fallback") {
  55. for (int i = 0; i < TextServerManager::get_interface_count(); i++) {
  56. TextServer *ts = TextServerManager::initialize(i, err);
  57. Vector<RID> font;
  58. font.push_back(ts->create_font_memory(_font_NotoSansUI_Regular, _font_NotoSansUI_Regular_size, "ttf"));
  59. font.push_back(ts->create_font_memory(_font_NotoSansThaiUI_Regular, _font_NotoSansThaiUI_Regular_size, "ttf"));
  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. Vector<TextServer::Glyph> glyphs = ts->shaped_text_get_glyphs(ctx);
  67. TEST_FAIL_COND(glyphs.size() == 0, "Shaping failed");
  68. for (int j = 0; j < glyphs.size(); j++) {
  69. if (glyphs[j].start < 6) {
  70. TEST_FAIL_COND(glyphs[j].font_rid != font[1], "Incorrect font selected.");
  71. }
  72. if ((glyphs[j].start > 6) && (glyphs[j].start < 16)) {
  73. TEST_FAIL_COND(glyphs[j].font_rid != font[0], "Incorrect font selected.");
  74. }
  75. if (glyphs[j].start > 16) {
  76. TEST_FAIL_COND(glyphs[j].font_rid != RID(), "Incorrect font selected.");
  77. TEST_FAIL_COND(glyphs[j].index != test[glyphs[j].start], "Incorrect glyph index.");
  78. }
  79. TEST_FAIL_COND((glyphs[j].start < 0 || glyphs[j].end > test.length()), "Incorrect glyph range.");
  80. TEST_FAIL_COND(glyphs[j].font_size != 16, "Incorrect glyph font size.");
  81. }
  82. ts->free(ctx);
  83. for (int j = 0; j < font.size(); j++) {
  84. ts->free(font[j]);
  85. }
  86. font.clear();
  87. }
  88. }
  89. SUBCASE("[TextServer] Text layout: BiDi") {
  90. for (int i = 0; i < TextServerManager::get_interface_count(); i++) {
  91. TextServer *ts = TextServerManager::initialize(i, err);
  92. if (!ts->has_feature(TextServer::FEATURE_BIDI_LAYOUT)) {
  93. continue;
  94. }
  95. Vector<RID> font;
  96. font.push_back(ts->create_font_memory(_font_NotoSansUI_Regular, _font_NotoSansUI_Regular_size, "ttf"));
  97. font.push_back(ts->create_font_memory(_font_NotoNaskhArabicUI_Regular, _font_NotoNaskhArabicUI_Regular_size, "ttf"));
  98. String test = U"Arabic (اَلْعَرَبِيَّةُ, al-ʿarabiyyah)";
  99. // 7^ 26^
  100. RID ctx = ts->create_shaped_text();
  101. TEST_FAIL_COND(ctx == RID(), "Creating text buffer failed.");
  102. bool ok = ts->shaped_text_add_string(ctx, test, font, 16);
  103. TEST_FAIL_COND(!ok, "Adding text to the buffer failed.");
  104. Vector<TextServer::Glyph> glyphs = ts->shaped_text_get_glyphs(ctx);
  105. TEST_FAIL_COND(glyphs.size() == 0, "Shaping failed");
  106. for (int j = 0; j < glyphs.size(); j++) {
  107. if (glyphs[j].count > 0) {
  108. if (glyphs[j].start < 7) {
  109. TEST_FAIL_COND(((glyphs[j].flags & TextServer::GRAPHEME_IS_RTL) == TextServer::GRAPHEME_IS_RTL), "Incorrect direction.");
  110. }
  111. if ((glyphs[j].start > 8) && (glyphs[j].start < 23)) {
  112. TEST_FAIL_COND(((glyphs[j].flags & TextServer::GRAPHEME_IS_RTL) != TextServer::GRAPHEME_IS_RTL), "Incorrect direction.");
  113. }
  114. if (glyphs[j].start > 26) {
  115. TEST_FAIL_COND(((glyphs[j].flags & TextServer::GRAPHEME_IS_RTL) == TextServer::GRAPHEME_IS_RTL), "Incorrect direction.");
  116. }
  117. }
  118. }
  119. ts->free(ctx);
  120. for (int j = 0; j < font.size(); j++) {
  121. ts->free(font[j]);
  122. }
  123. font.clear();
  124. }
  125. }
  126. SUBCASE("[TextServer] Text layout: Line breaking") {
  127. for (int i = 0; i < TextServerManager::get_interface_count(); i++) {
  128. TextServer *ts = TextServerManager::initialize(i, err);
  129. String test_1 = U"test test test";
  130. // 5^ 10^
  131. Vector<RID> font;
  132. font.push_back(ts->create_font_memory(_font_NotoSansUI_Regular, _font_NotoSansUI_Regular_size, "ttf"));
  133. font.push_back(ts->create_font_memory(_font_NotoSansThaiUI_Regular, _font_NotoSansThaiUI_Regular_size, "ttf"));
  134. RID ctx = ts->create_shaped_text();
  135. TEST_FAIL_COND(ctx == RID(), "Creating text buffer failed.");
  136. bool ok = ts->shaped_text_add_string(ctx, test_1, font, 16);
  137. TEST_FAIL_COND(!ok, "Adding text to the buffer failed.");
  138. Vector<Vector2i> brks = ts->shaped_text_get_line_breaks(ctx, 1);
  139. TEST_FAIL_COND(brks.size() != 3, "Invalid line breaks number.");
  140. if (brks.size() == 3) {
  141. TEST_FAIL_COND(brks[0] != Vector2i(0, 5), "Invalid line break position.");
  142. TEST_FAIL_COND(brks[1] != Vector2i(5, 10), "Invalid line break position.");
  143. TEST_FAIL_COND(brks[2] != Vector2i(10, 14), "Invalid line break position.");
  144. }
  145. ts->free(ctx);
  146. for (int j = 0; j < font.size(); j++) {
  147. ts->free(font[j]);
  148. }
  149. font.clear();
  150. }
  151. }
  152. SUBCASE("[TextServer] Text layout: Justification") {
  153. for (int i = 0; i < TextServerManager::get_interface_count(); i++) {
  154. TextServer *ts = TextServerManager::initialize(i, err);
  155. Vector<RID> font;
  156. font.push_back(ts->create_font_memory(_font_NotoSansUI_Regular, _font_NotoSansUI_Regular_size, "ttf"));
  157. font.push_back(ts->create_font_memory(_font_NotoNaskhArabicUI_Regular, _font_NotoNaskhArabicUI_Regular_size, "ttf"));
  158. String test_1 = U"الحمد";
  159. String test_2 = U"الحمد test";
  160. String test_3 = U"test test";
  161. // 7^ 26^
  162. RID ctx;
  163. bool ok;
  164. float width_old, width;
  165. if (ts->has_feature(TextServer::FEATURE_KASHIDA_JUSTIFICATION)) {
  166. ctx = ts->create_shaped_text();
  167. TEST_FAIL_COND(ctx == RID(), "Creating text buffer failed.");
  168. ok = ts->shaped_text_add_string(ctx, test_1, font, 16);
  169. TEST_FAIL_COND(!ok, "Adding text to the buffer failed.");
  170. width_old = ts->shaped_text_get_width(ctx);
  171. width = ts->shaped_text_fit_to_width(ctx, 100, TextServer::JUSTIFICATION_WORD_BOUND);
  172. TEST_FAIL_COND((width != width_old), "Invalid fill width.");
  173. width = ts->shaped_text_fit_to_width(ctx, 100, TextServer::JUSTIFICATION_WORD_BOUND | TextServer::JUSTIFICATION_KASHIDA);
  174. TEST_FAIL_COND((width <= width_old || width > 100), "Invalid fill width.");
  175. ts->free(ctx);
  176. ctx = ts->create_shaped_text();
  177. TEST_FAIL_COND(ctx == RID(), "Creating text buffer failed.");
  178. ok = ts->shaped_text_add_string(ctx, test_2, font, 16);
  179. TEST_FAIL_COND(!ok, "Adding text to the buffer failed.");
  180. width_old = ts->shaped_text_get_width(ctx);
  181. width = ts->shaped_text_fit_to_width(ctx, 100, TextServer::JUSTIFICATION_WORD_BOUND);
  182. TEST_FAIL_COND((width <= width_old || width > 100), "Invalid fill width.");
  183. width = ts->shaped_text_fit_to_width(ctx, 100, TextServer::JUSTIFICATION_WORD_BOUND | TextServer::JUSTIFICATION_KASHIDA);
  184. TEST_FAIL_COND((width <= width_old || width > 100), "Invalid fill width.");
  185. ts->free(ctx);
  186. }
  187. ctx = ts->create_shaped_text();
  188. TEST_FAIL_COND(ctx == RID(), "Creating text buffer failed.");
  189. ok = ts->shaped_text_add_string(ctx, test_3, font, 16);
  190. TEST_FAIL_COND(!ok, "Adding text to the buffer failed.");
  191. width_old = ts->shaped_text_get_width(ctx);
  192. width = ts->shaped_text_fit_to_width(ctx, 100, TextServer::JUSTIFICATION_WORD_BOUND);
  193. TEST_FAIL_COND((width <= width_old || width > 100), "Invalid fill width.");
  194. ts->free(ctx);
  195. for (int j = 0; j < font.size(); j++) {
  196. ts->free(font[j]);
  197. }
  198. font.clear();
  199. }
  200. }
  201. memdelete(tsman);
  202. }
  203. }
  204. }; // namespace TestTextServer
  205. #endif // TEST_TEXT_SERVER_H