text_server.cpp 54 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305
  1. /*************************************************************************/
  2. /* text_server.cpp */
  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. #include "servers/text_server.h"
  31. #include "scene/main/canvas_item.h"
  32. TextServerManager *TextServerManager::singleton = nullptr;
  33. TextServer *TextServerManager::server = nullptr;
  34. TextServerManager::TextServerCreate TextServerManager::server_create_functions[TextServerManager::MAX_SERVERS];
  35. int TextServerManager::server_create_count = 0;
  36. void TextServerManager::_bind_methods() {
  37. ClassDB::bind_method(D_METHOD("get_interface_count"), &TextServerManager::_get_interface_count);
  38. ClassDB::bind_method(D_METHOD("get_interface_name", "index"), &TextServerManager::_get_interface_name);
  39. ClassDB::bind_method(D_METHOD("get_interface_features", "index"), &TextServerManager::_get_interface_features);
  40. ClassDB::bind_method(D_METHOD("get_interface", "index"), &TextServerManager::_get_interface);
  41. ClassDB::bind_method(D_METHOD("get_interfaces"), &TextServerManager::_get_interfaces);
  42. ClassDB::bind_method(D_METHOD("find_interface", "name"), &TextServerManager::_find_interface);
  43. ClassDB::bind_method(D_METHOD("set_primary_interface", "index"), &TextServerManager::_set_primary_interface);
  44. ClassDB::bind_method(D_METHOD("get_primary_interface"), &TextServerManager::_get_primary_interface);
  45. }
  46. void TextServerManager::register_create_function(const String &p_name, uint32_t p_features, TextServerManager::CreateFunction p_function, void *p_user_data) {
  47. ERR_FAIL_COND(server_create_count == MAX_SERVERS);
  48. server_create_functions[server_create_count].name = p_name;
  49. server_create_functions[server_create_count].create_function = p_function;
  50. server_create_functions[server_create_count].user_data = p_user_data;
  51. server_create_functions[server_create_count].features = p_features;
  52. server_create_count++;
  53. }
  54. int TextServerManager::get_interface_count() {
  55. return server_create_count;
  56. }
  57. String TextServerManager::get_interface_name(int p_index) {
  58. ERR_FAIL_INDEX_V(p_index, server_create_count, String());
  59. return server_create_functions[p_index].name;
  60. }
  61. uint32_t TextServerManager::get_interface_features(int p_index) {
  62. ERR_FAIL_INDEX_V(p_index, server_create_count, 0);
  63. return server_create_functions[p_index].features;
  64. }
  65. TextServer *TextServerManager::initialize(int p_index, Error &r_error) {
  66. ERR_FAIL_INDEX_V(p_index, server_create_count, nullptr);
  67. if (server_create_functions[p_index].instance == nullptr) {
  68. server_create_functions[p_index].instance = server_create_functions[p_index].create_function(r_error, server_create_functions[p_index].user_data);
  69. if (server_create_functions[p_index].instance != nullptr) {
  70. server_create_functions[p_index].instance->load_support_data(""); // Try loading default data.
  71. }
  72. }
  73. if (server_create_functions[p_index].instance != nullptr) {
  74. server = server_create_functions[p_index].instance;
  75. if (OS::get_singleton()->get_main_loop()) {
  76. OS::get_singleton()->get_main_loop()->notification(MainLoop::NOTIFICATION_TEXT_SERVER_CHANGED);
  77. }
  78. }
  79. return server_create_functions[p_index].instance;
  80. }
  81. TextServer *TextServerManager::get_primary_interface() {
  82. return server;
  83. }
  84. int TextServerManager::_get_interface_count() const {
  85. return server_create_count;
  86. }
  87. String TextServerManager::_get_interface_name(int p_index) const {
  88. return get_interface_name(p_index);
  89. }
  90. uint32_t TextServerManager::_get_interface_features(int p_index) const {
  91. return get_interface_features(p_index);
  92. }
  93. TextServer *TextServerManager::_get_interface(int p_index) const {
  94. ERR_FAIL_INDEX_V(p_index, server_create_count, nullptr);
  95. if (server_create_functions[p_index].instance == nullptr) {
  96. Error error;
  97. server_create_functions[p_index].instance = server_create_functions[p_index].create_function(error, server_create_functions[p_index].user_data);
  98. if (server_create_functions[p_index].instance != nullptr) {
  99. server_create_functions[p_index].instance->load_support_data(""); // Try loading default data.
  100. }
  101. }
  102. return server_create_functions[p_index].instance;
  103. }
  104. TextServer *TextServerManager::_find_interface(const String &p_name) const {
  105. for (int i = 0; i < server_create_count; i++) {
  106. if (server_create_functions[i].name == p_name) {
  107. return _get_interface(i);
  108. }
  109. }
  110. return nullptr;
  111. }
  112. Array TextServerManager::_get_interfaces() const {
  113. Array ret;
  114. for (int i = 0; i < server_create_count; i++) {
  115. Dictionary iface_info;
  116. iface_info["id"] = i;
  117. iface_info["name"] = server_create_functions[i].name;
  118. ret.push_back(iface_info);
  119. };
  120. return ret;
  121. };
  122. bool TextServerManager::_set_primary_interface(int p_index) {
  123. Error error;
  124. TextServerManager::initialize(p_index, error);
  125. return (error == OK);
  126. }
  127. TextServer *TextServerManager::_get_primary_interface() const {
  128. return server;
  129. }
  130. TextServerManager::TextServerManager() {
  131. singleton = this;
  132. }
  133. TextServerManager::~TextServerManager() {
  134. singleton = nullptr;
  135. for (int i = 0; i < server_create_count; i++) {
  136. if (server_create_functions[i].instance != nullptr) {
  137. memdelete(server_create_functions[i].instance);
  138. server_create_functions[i].instance = nullptr;
  139. }
  140. }
  141. }
  142. /*************************************************************************/
  143. bool TextServer::Glyph::operator==(const Glyph &p_a) const {
  144. return (p_a.index == index) && (p_a.font_rid == font_rid) && (p_a.font_size == font_size) && (p_a.start == start);
  145. }
  146. bool TextServer::Glyph::operator!=(const Glyph &p_a) const {
  147. return (p_a.index != index) || (p_a.font_rid != font_rid) || (p_a.font_size != font_size) || (p_a.start != start);
  148. }
  149. bool TextServer::Glyph::operator<(const Glyph &p_a) const {
  150. if (p_a.start == start) {
  151. if (p_a.count == count) {
  152. if ((p_a.flags & GRAPHEME_IS_VIRTUAL) == GRAPHEME_IS_VIRTUAL) {
  153. return true;
  154. } else {
  155. return false;
  156. }
  157. }
  158. return p_a.count > count;
  159. }
  160. return p_a.start < start;
  161. }
  162. bool TextServer::Glyph::operator>(const Glyph &p_a) const {
  163. if (p_a.start == start) {
  164. if (p_a.count == count) {
  165. if ((p_a.flags & GRAPHEME_IS_VIRTUAL) == GRAPHEME_IS_VIRTUAL) {
  166. return false;
  167. } else {
  168. return true;
  169. }
  170. }
  171. return p_a.count < count;
  172. }
  173. return p_a.start > start;
  174. }
  175. void TextServer::_bind_methods() {
  176. ClassDB::bind_method(D_METHOD("has_feature", "feature"), &TextServer::has_feature);
  177. ClassDB::bind_method(D_METHOD("get_name"), &TextServer::get_name);
  178. ClassDB::bind_method(D_METHOD("load_support_data", "filename"), &TextServer::load_support_data);
  179. ClassDB::bind_method(D_METHOD("is_locale_right_to_left", "locale"), &TextServer::is_locale_right_to_left);
  180. ClassDB::bind_method(D_METHOD("name_to_tag", "name"), &TextServer::name_to_tag);
  181. ClassDB::bind_method(D_METHOD("tag_to_name", "tag"), &TextServer::tag_to_name);
  182. ClassDB::bind_method(D_METHOD("has", "rid"), &TextServer::has);
  183. ClassDB::bind_method(D_METHOD("free_rid", "rid"), &TextServer::free); // shouldn't conflict with Object::free()
  184. /* Font Interface */
  185. ClassDB::bind_method(D_METHOD("create_font_system", "name", "base_size"), &TextServer::create_font_system, DEFVAL(16));
  186. ClassDB::bind_method(D_METHOD("create_font_resource", "filename", "base_size"), &TextServer::create_font_resource, DEFVAL(16));
  187. ClassDB::bind_method(D_METHOD("create_font_memory", "data", "type", "base_size"), &TextServer::_create_font_memory, DEFVAL(16));
  188. ClassDB::bind_method(D_METHOD("font_get_height", "font", "size"), &TextServer::font_get_height);
  189. ClassDB::bind_method(D_METHOD("font_get_ascent", "font", "size"), &TextServer::font_get_ascent);
  190. ClassDB::bind_method(D_METHOD("font_get_descent", "font", "size"), &TextServer::font_get_descent);
  191. ClassDB::bind_method(D_METHOD("font_get_underline_position", "font", "size"), &TextServer::font_get_underline_position);
  192. ClassDB::bind_method(D_METHOD("font_get_underline_thickness", "font", "size"), &TextServer::font_get_underline_thickness);
  193. ClassDB::bind_method(D_METHOD("font_get_spacing_space", "font"), &TextServer::font_get_spacing_space);
  194. ClassDB::bind_method(D_METHOD("font_set_spacing_space", "font", "value"), &TextServer::font_set_spacing_space);
  195. ClassDB::bind_method(D_METHOD("font_get_spacing_glyph", "font"), &TextServer::font_get_spacing_glyph);
  196. ClassDB::bind_method(D_METHOD("font_set_spacing_glyph", "font", "value"), &TextServer::font_set_spacing_glyph);
  197. ClassDB::bind_method(D_METHOD("font_set_antialiased", "font", "antialiased"), &TextServer::font_set_antialiased);
  198. ClassDB::bind_method(D_METHOD("font_get_antialiased", "font"), &TextServer::font_get_antialiased);
  199. ClassDB::bind_method(D_METHOD("font_get_feature_list", "font"), &TextServer::font_get_feature_list);
  200. ClassDB::bind_method(D_METHOD("font_get_variation_list", "font"), &TextServer::font_get_variation_list);
  201. ClassDB::bind_method(D_METHOD("font_set_variation", "font", "tag", "value"), &TextServer::font_set_variation);
  202. ClassDB::bind_method(D_METHOD("font_get_variation", "font", "tag"), &TextServer::font_get_variation);
  203. ClassDB::bind_method(D_METHOD("font_set_hinting", "font", "hinting"), &TextServer::font_set_hinting);
  204. ClassDB::bind_method(D_METHOD("font_get_hinting", "font"), &TextServer::font_get_hinting);
  205. ClassDB::bind_method(D_METHOD("font_set_distance_field_hint", "font", "distance_field"), &TextServer::font_set_distance_field_hint);
  206. ClassDB::bind_method(D_METHOD("font_get_distance_field_hint", "font"), &TextServer::font_get_distance_field_hint);
  207. ClassDB::bind_method(D_METHOD("font_set_force_autohinter", "font", "enabeld"), &TextServer::font_set_force_autohinter);
  208. ClassDB::bind_method(D_METHOD("font_get_force_autohinter", "font"), &TextServer::font_get_force_autohinter);
  209. ClassDB::bind_method(D_METHOD("font_has_char", "font", "char"), &TextServer::font_has_char);
  210. ClassDB::bind_method(D_METHOD("font_get_supported_chars", "font"), &TextServer::font_get_supported_chars);
  211. ClassDB::bind_method(D_METHOD("font_has_outline", "font"), &TextServer::font_has_outline);
  212. ClassDB::bind_method(D_METHOD("font_get_base_size", "font"), &TextServer::font_get_base_size);
  213. ClassDB::bind_method(D_METHOD("font_is_language_supported", "font", "language"), &TextServer::font_is_language_supported);
  214. ClassDB::bind_method(D_METHOD("font_set_language_support_override", "font", "language", "supported"), &TextServer::font_set_language_support_override);
  215. ClassDB::bind_method(D_METHOD("font_get_language_support_override", "font", "language"), &TextServer::font_get_language_support_override);
  216. ClassDB::bind_method(D_METHOD("font_remove_language_support_override", "font", "language"), &TextServer::font_remove_language_support_override);
  217. ClassDB::bind_method(D_METHOD("font_get_language_support_overrides", "font"), &TextServer::font_get_language_support_overrides);
  218. ClassDB::bind_method(D_METHOD("font_is_script_supported", "font", "script"), &TextServer::font_is_script_supported);
  219. ClassDB::bind_method(D_METHOD("font_set_script_support_override", "font", "script", "supported"), &TextServer::font_set_script_support_override);
  220. ClassDB::bind_method(D_METHOD("font_get_script_support_override", "font", "script"), &TextServer::font_get_script_support_override);
  221. ClassDB::bind_method(D_METHOD("font_remove_script_support_override", "font", "script"), &TextServer::font_remove_script_support_override);
  222. ClassDB::bind_method(D_METHOD("font_get_script_support_overrides", "font"), &TextServer::font_get_script_support_overrides);
  223. ClassDB::bind_method(D_METHOD("font_get_glyph_index", "font", "char", "variation_selector"), &TextServer::font_get_glyph_index, DEFVAL(0x0000));
  224. ClassDB::bind_method(D_METHOD("font_get_glyph_advance", "font", "index", "size"), &TextServer::font_get_glyph_advance);
  225. ClassDB::bind_method(D_METHOD("font_get_glyph_kerning", "font", "index_a", "index_b", "size"), &TextServer::font_get_glyph_kerning);
  226. ClassDB::bind_method(D_METHOD("font_draw_glyph", "font", "canvas", "size", "pos", "index", "color"), &TextServer::font_draw_glyph, DEFVAL(Color(1, 1, 1)));
  227. ClassDB::bind_method(D_METHOD("font_draw_glyph_outline", "font", "canvas", "size", "outline_size", "pos", "index", "color"), &TextServer::font_draw_glyph_outline, DEFVAL(Color(1, 1, 1)));
  228. ClassDB::bind_method(D_METHOD("font_get_oversampling"), &TextServer::font_get_oversampling);
  229. ClassDB::bind_method(D_METHOD("font_set_oversampling", "oversampling"), &TextServer::font_set_oversampling);
  230. ClassDB::bind_method(D_METHOD("get_system_fonts"), &TextServer::get_system_fonts);
  231. ClassDB::bind_method(D_METHOD("get_hex_code_box_size", "size", "index"), &TextServer::get_hex_code_box_size);
  232. ClassDB::bind_method(D_METHOD("draw_hex_code_box", "canvas", "size", "pos", "index", "color"), &TextServer::draw_hex_code_box);
  233. /* Shaped text buffer interface */
  234. ClassDB::bind_method(D_METHOD("create_shaped_text", "direction", "orientation"), &TextServer::create_shaped_text, DEFVAL(DIRECTION_AUTO), DEFVAL(ORIENTATION_HORIZONTAL));
  235. ClassDB::bind_method(D_METHOD("shaped_text_clear", "rid"), &TextServer::shaped_text_clear);
  236. ClassDB::bind_method(D_METHOD("shaped_text_set_direction", "shaped", "direction"), &TextServer::shaped_text_set_direction, DEFVAL(DIRECTION_AUTO));
  237. ClassDB::bind_method(D_METHOD("shaped_text_get_direction", "shaped"), &TextServer::shaped_text_get_direction);
  238. ClassDB::bind_method(D_METHOD("shaped_text_set_bidi_override", "shaped", "override"), &TextServer::_shaped_text_set_bidi_override);
  239. ClassDB::bind_method(D_METHOD("shaped_text_set_orientation", "shaped", "orientation"), &TextServer::shaped_text_set_orientation, DEFVAL(ORIENTATION_HORIZONTAL));
  240. ClassDB::bind_method(D_METHOD("shaped_text_get_orientation", "shaped"), &TextServer::shaped_text_get_orientation);
  241. ClassDB::bind_method(D_METHOD("shaped_text_set_preserve_invalid", "shaped", "enabled"), &TextServer::shaped_text_set_preserve_invalid);
  242. ClassDB::bind_method(D_METHOD("shaped_text_get_preserve_invalid", "shaped"), &TextServer::shaped_text_get_preserve_invalid);
  243. ClassDB::bind_method(D_METHOD("shaped_text_set_preserve_control", "shaped", "enabled"), &TextServer::shaped_text_set_preserve_control);
  244. ClassDB::bind_method(D_METHOD("shaped_text_get_preserve_control", "shaped"), &TextServer::shaped_text_get_preserve_control);
  245. ClassDB::bind_method(D_METHOD("shaped_text_add_string", "shaped", "text", "fonts", "size", "opentype_features", "language"), &TextServer::shaped_text_add_string, DEFVAL(Dictionary()), DEFVAL(""));
  246. ClassDB::bind_method(D_METHOD("shaped_text_add_object", "shaped", "key", "size", "inline_align", "length"), &TextServer::shaped_text_add_object, DEFVAL(VALIGN_CENTER), DEFVAL(1));
  247. ClassDB::bind_method(D_METHOD("shaped_text_resize_object", "shaped", "key", "size", "inline_align"), &TextServer::shaped_text_resize_object, DEFVAL(VALIGN_CENTER));
  248. ClassDB::bind_method(D_METHOD("shaped_text_substr", "shaped", "start", "length"), &TextServer::shaped_text_substr);
  249. ClassDB::bind_method(D_METHOD("shaped_text_get_parent", "shaped"), &TextServer::shaped_text_get_parent);
  250. ClassDB::bind_method(D_METHOD("shaped_text_fit_to_width", "shaped", "width", "jst_flags"), &TextServer::shaped_text_fit_to_width, DEFVAL(JUSTIFICATION_WORD_BOUND | JUSTIFICATION_KASHIDA));
  251. ClassDB::bind_method(D_METHOD("shaped_text_tab_align", "shaped", "tab_stops"), &TextServer::shaped_text_tab_align);
  252. ClassDB::bind_method(D_METHOD("shaped_text_shape", "shaped"), &TextServer::shaped_text_shape);
  253. ClassDB::bind_method(D_METHOD("shaped_text_is_ready", "shaped"), &TextServer::shaped_text_is_ready);
  254. ClassDB::bind_method(D_METHOD("shaped_text_get_glyphs", "shaped"), &TextServer::_shaped_text_get_glyphs);
  255. ClassDB::bind_method(D_METHOD("shaped_text_get_range", "shaped"), &TextServer::shaped_text_get_range);
  256. ClassDB::bind_method(D_METHOD("shaped_text_get_line_breaks_adv", "shaped", "width", "start", "once", "break_flags"), &TextServer::_shaped_text_get_line_breaks_adv, DEFVAL(0), DEFVAL(true), DEFVAL(BREAK_MANDATORY | BREAK_WORD_BOUND));
  257. ClassDB::bind_method(D_METHOD("shaped_text_get_line_breaks", "shaped", "width", "start", "break_flags"), &TextServer::_shaped_text_get_line_breaks, DEFVAL(0), DEFVAL(BREAK_MANDATORY | BREAK_WORD_BOUND));
  258. ClassDB::bind_method(D_METHOD("shaped_text_get_word_breaks", "shaped"), &TextServer::_shaped_text_get_word_breaks);
  259. ClassDB::bind_method(D_METHOD("shaped_text_get_objects", "shaped"), &TextServer::shaped_text_get_objects);
  260. ClassDB::bind_method(D_METHOD("shaped_text_get_object_rect", "shaped", "key"), &TextServer::shaped_text_get_object_rect);
  261. ClassDB::bind_method(D_METHOD("shaped_text_get_size", "shaped"), &TextServer::shaped_text_get_size);
  262. ClassDB::bind_method(D_METHOD("shaped_text_get_ascent", "shaped"), &TextServer::shaped_text_get_ascent);
  263. ClassDB::bind_method(D_METHOD("shaped_text_get_descent", "shaped"), &TextServer::shaped_text_get_descent);
  264. ClassDB::bind_method(D_METHOD("shaped_text_get_width", "shaped"), &TextServer::shaped_text_get_width);
  265. ClassDB::bind_method(D_METHOD("shaped_text_get_underline_position", "shaped"), &TextServer::shaped_text_get_underline_position);
  266. ClassDB::bind_method(D_METHOD("shaped_text_get_underline_thickness", "shaped"), &TextServer::shaped_text_get_underline_thickness);
  267. ClassDB::bind_method(D_METHOD("shaped_text_get_carets", "shaped", "position"), &TextServer::_shaped_text_get_carets);
  268. ClassDB::bind_method(D_METHOD("shaped_text_get_selection", "shaped", "start", "end"), &TextServer::_shaped_text_get_selection);
  269. ClassDB::bind_method(D_METHOD("shaped_text_hit_test_grapheme", "shaped", "coords"), &TextServer::shaped_text_hit_test_grapheme);
  270. ClassDB::bind_method(D_METHOD("shaped_text_hit_test_position", "shaped", "coords"), &TextServer::shaped_text_hit_test_position);
  271. ClassDB::bind_method(D_METHOD("shaped_text_next_grapheme_pos", "shaped", "pos"), &TextServer::shaped_text_next_grapheme_pos);
  272. ClassDB::bind_method(D_METHOD("shaped_text_prev_grapheme_pos", "shaped", "pos"), &TextServer::shaped_text_prev_grapheme_pos);
  273. ClassDB::bind_method(D_METHOD("shaped_text_draw", "shaped", "canvas", "pos", "clip_l", "clip_r", "color"), &TextServer::shaped_text_draw, DEFVAL(-1), DEFVAL(-1), DEFVAL(Color(1, 1, 1)));
  274. ClassDB::bind_method(D_METHOD("shaped_text_draw_outline", "shaped", "canvas", "pos", "clip_l", "clip_r", "outline_size", "color"), &TextServer::shaped_text_draw_outline, DEFVAL(-1), DEFVAL(-1), DEFVAL(1), DEFVAL(Color(1, 1, 1)));
  275. ClassDB::bind_method(D_METHOD("shaped_text_get_dominant_direciton_in_range", "shaped", "start", "end"), &TextServer::shaped_text_get_dominant_direciton_in_range);
  276. ClassDB::bind_method(D_METHOD("format_number", "number", "language"), &TextServer::format_number, DEFVAL(""));
  277. ClassDB::bind_method(D_METHOD("parse_number", "number", "language"), &TextServer::parse_number, DEFVAL(""));
  278. ClassDB::bind_method(D_METHOD("percent_sign", "language"), &TextServer::percent_sign, DEFVAL(""));
  279. /* Direction */
  280. BIND_ENUM_CONSTANT(DIRECTION_AUTO);
  281. BIND_ENUM_CONSTANT(DIRECTION_LTR);
  282. BIND_ENUM_CONSTANT(DIRECTION_RTL);
  283. /* Orientation */
  284. BIND_ENUM_CONSTANT(ORIENTATION_HORIZONTAL);
  285. BIND_ENUM_CONSTANT(ORIENTATION_VERTICAL);
  286. /* JustificationFlag */
  287. BIND_ENUM_CONSTANT(JUSTIFICATION_NONE);
  288. BIND_ENUM_CONSTANT(JUSTIFICATION_KASHIDA);
  289. BIND_ENUM_CONSTANT(JUSTIFICATION_WORD_BOUND);
  290. BIND_ENUM_CONSTANT(JUSTIFICATION_TRIM_EDGE_SPACES);
  291. BIND_ENUM_CONSTANT(JUSTIFICATION_AFTER_LAST_TAB);
  292. /* LineBreakFlag */
  293. BIND_ENUM_CONSTANT(BREAK_NONE);
  294. BIND_ENUM_CONSTANT(BREAK_MANDATORY);
  295. BIND_ENUM_CONSTANT(BREAK_WORD_BOUND);
  296. BIND_ENUM_CONSTANT(BREAK_GRAPHEME_BOUND);
  297. /* GraphemeFlag */
  298. BIND_ENUM_CONSTANT(GRAPHEME_IS_RTL);
  299. BIND_ENUM_CONSTANT(GRAPHEME_IS_VIRTUAL);
  300. BIND_ENUM_CONSTANT(GRAPHEME_IS_SPACE);
  301. BIND_ENUM_CONSTANT(GRAPHEME_IS_BREAK_HARD);
  302. BIND_ENUM_CONSTANT(GRAPHEME_IS_BREAK_SOFT);
  303. BIND_ENUM_CONSTANT(GRAPHEME_IS_TAB);
  304. BIND_ENUM_CONSTANT(GRAPHEME_IS_ELONGATION);
  305. BIND_ENUM_CONSTANT(GRAPHEME_IS_PUNCTUATION);
  306. /* Hinting */
  307. BIND_ENUM_CONSTANT(HINTING_NONE);
  308. BIND_ENUM_CONSTANT(HINTING_LIGHT);
  309. BIND_ENUM_CONSTANT(HINTING_NORMAL);
  310. /* Feature */
  311. BIND_ENUM_CONSTANT(FEATURE_BIDI_LAYOUT);
  312. BIND_ENUM_CONSTANT(FEATURE_VERTICAL_LAYOUT);
  313. BIND_ENUM_CONSTANT(FEATURE_SHAPING);
  314. BIND_ENUM_CONSTANT(FEATURE_KASHIDA_JUSTIFICATION);
  315. BIND_ENUM_CONSTANT(FEATURE_BREAK_ITERATORS);
  316. BIND_ENUM_CONSTANT(FEATURE_FONT_SYSTEM);
  317. BIND_ENUM_CONSTANT(FEATURE_FONT_VARIABLE);
  318. BIND_ENUM_CONSTANT(FEATURE_USE_SUPPORT_DATA);
  319. }
  320. Vector3 TextServer::hex_code_box_font_size[2] = { Vector3(5, 5, 1), Vector3(10, 10, 2) };
  321. Ref<CanvasTexture> TextServer::hex_code_box_font_tex[2] = { nullptr, nullptr };
  322. void TextServer::initialize_hex_code_box_fonts() {
  323. static unsigned int tamsyn5x9_png_len = 175;
  324. static unsigned char tamsyn5x9_png[] = {
  325. 0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a, 0x00, 0x00, 0x00, 0x0d,
  326. 0x49, 0x48, 0x44, 0x52, 0x00, 0x00, 0x00, 0x50, 0x00, 0x00, 0x00, 0x05,
  327. 0x04, 0x03, 0x00, 0x00, 0x00, 0x20, 0x7c, 0x76, 0xda, 0x00, 0x00, 0x00,
  328. 0x0f, 0x50, 0x4c, 0x54, 0x45, 0xfd, 0x07, 0x00, 0x00, 0x00, 0x00, 0x06,
  329. 0x7e, 0x74, 0x00, 0x40, 0xc6, 0xff, 0xff, 0xff, 0x47, 0x9a, 0xd4, 0xc7,
  330. 0x00, 0x00, 0x00, 0x01, 0x74, 0x52, 0x4e, 0x53, 0x00, 0x40, 0xe6, 0xd8,
  331. 0x66, 0x00, 0x00, 0x00, 0x4e, 0x49, 0x44, 0x41, 0x54, 0x08, 0x1d, 0x05,
  332. 0xc1, 0x21, 0x01, 0x00, 0x00, 0x00, 0x83, 0x30, 0x04, 0xc1, 0x10, 0xef,
  333. 0x9f, 0xe9, 0x1b, 0x86, 0x2c, 0x17, 0xb9, 0xcc, 0x65, 0x0c, 0x73, 0x38,
  334. 0xc7, 0xe6, 0x22, 0x19, 0x88, 0x98, 0x10, 0x48, 0x4a, 0x29, 0x85, 0x14,
  335. 0x02, 0x89, 0x10, 0xa3, 0x1c, 0x0b, 0x31, 0xd6, 0xe6, 0x08, 0x69, 0x39,
  336. 0x48, 0x44, 0xa0, 0x0d, 0x4a, 0x22, 0xa1, 0x94, 0x42, 0x0a, 0x01, 0x63,
  337. 0x6d, 0x0e, 0x72, 0x18, 0x61, 0x8c, 0x74, 0x38, 0xc7, 0x26, 0x1c, 0xf3,
  338. 0x71, 0x16, 0x15, 0x27, 0x6a, 0xc2, 0x2f, 0x00, 0x00, 0x00, 0x00, 0x49,
  339. 0x45, 0x4e, 0x44, 0xae, 0x42, 0x60, 0x82
  340. };
  341. static unsigned int tamsyn10x20_png_len = 270;
  342. static unsigned char tamsyn10x20_png[] = {
  343. 0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a, 0x00, 0x00, 0x00, 0x0d,
  344. 0x49, 0x48, 0x44, 0x52, 0x00, 0x00, 0x00, 0xa0, 0x00, 0x00, 0x00, 0x0a,
  345. 0x04, 0x03, 0x00, 0x00, 0x00, 0xc1, 0x66, 0x48, 0x96, 0x00, 0x00, 0x00,
  346. 0x0f, 0x50, 0x4c, 0x54, 0x45, 0x00, 0x00, 0x00, 0xf9, 0x07, 0x00, 0x5d,
  347. 0x71, 0xa5, 0x00, 0x00, 0x00, 0xff, 0xff, 0xff, 0x49, 0xdb, 0xcb, 0x7f,
  348. 0x00, 0x00, 0x00, 0x01, 0x74, 0x52, 0x4e, 0x53, 0x00, 0x40, 0xe6, 0xd8,
  349. 0x66, 0x00, 0x00, 0x00, 0xad, 0x49, 0x44, 0x41, 0x54, 0x28, 0xcf, 0xa5,
  350. 0x92, 0x4b, 0x0e, 0x03, 0x31, 0x08, 0x43, 0xdf, 0x82, 0x83, 0x79, 0xe1,
  351. 0xfb, 0x9f, 0xa9, 0x0b, 0x3e, 0x61, 0xa6, 0x1f, 0x55, 0xad, 0x14, 0x31,
  352. 0x66, 0x42, 0x1c, 0x70, 0x0c, 0xb6, 0x00, 0x01, 0xb6, 0x08, 0xdb, 0x00,
  353. 0x8d, 0xc2, 0x14, 0xb2, 0x55, 0xa1, 0xfe, 0x09, 0xc2, 0x26, 0xdc, 0x25,
  354. 0x75, 0x22, 0x97, 0x1a, 0x25, 0x77, 0x28, 0x31, 0x02, 0x80, 0xc8, 0xdd,
  355. 0x2c, 0x11, 0x1a, 0x54, 0x9f, 0xc8, 0xa2, 0x8a, 0x06, 0xa9, 0x93, 0x22,
  356. 0xbd, 0xd4, 0xd0, 0x0c, 0xcf, 0x81, 0x2b, 0xca, 0xbb, 0x83, 0xe0, 0x10,
  357. 0xe6, 0xad, 0xff, 0x10, 0x2a, 0x66, 0x34, 0x41, 0x58, 0x35, 0x54, 0x49,
  358. 0x5a, 0x63, 0xa5, 0xc2, 0x87, 0xab, 0x52, 0x76, 0x9a, 0xba, 0xc6, 0xf4,
  359. 0x75, 0x7a, 0x9e, 0x3c, 0x46, 0x86, 0x5c, 0xa3, 0xfd, 0x87, 0x0e, 0x75,
  360. 0x08, 0x7b, 0xee, 0x7e, 0xea, 0x21, 0x5c, 0x4f, 0xf6, 0xc5, 0xc8, 0x4b,
  361. 0xb9, 0x11, 0xf2, 0xd6, 0xe1, 0x8f, 0x84, 0x62, 0x7b, 0x67, 0xf9, 0x24,
  362. 0xde, 0x6d, 0xbc, 0xb2, 0xcd, 0xb1, 0xf3, 0xf2, 0x2f, 0xe8, 0xe2, 0xe4,
  363. 0xae, 0x4b, 0x4f, 0xcf, 0x2b, 0xdc, 0x8d, 0x0d, 0xf0, 0x00, 0x8f, 0x22,
  364. 0x26, 0x65, 0x75, 0x8a, 0xe6, 0x84, 0x00, 0x00, 0x00, 0x00, 0x49, 0x45,
  365. 0x4e, 0x44, 0xae, 0x42, 0x60, 0x82
  366. };
  367. if (RenderingServer::get_singleton() != nullptr) {
  368. Vector<uint8_t> hex_box_data;
  369. Ref<Image> image;
  370. image.instance();
  371. Ref<ImageTexture> hex_code_image_tex[2];
  372. hex_box_data.resize(tamsyn5x9_png_len);
  373. memcpy(hex_box_data.ptrw(), tamsyn5x9_png, tamsyn5x9_png_len);
  374. image->load_png_from_buffer(hex_box_data);
  375. hex_code_image_tex[0].instance();
  376. hex_code_image_tex[0]->create_from_image(image);
  377. hex_code_box_font_tex[0].instance();
  378. hex_code_box_font_tex[0]->set_diffuse_texture(hex_code_image_tex[0]);
  379. hex_code_box_font_tex[0]->set_texture_filter(CanvasItem::TEXTURE_FILTER_NEAREST);
  380. hex_box_data.clear();
  381. hex_box_data.resize(tamsyn10x20_png_len);
  382. memcpy(hex_box_data.ptrw(), tamsyn10x20_png, tamsyn10x20_png_len);
  383. image->load_png_from_buffer(hex_box_data);
  384. hex_code_image_tex[1].instance();
  385. hex_code_image_tex[1]->create_from_image(image);
  386. hex_code_box_font_tex[1].instance();
  387. hex_code_box_font_tex[1]->set_diffuse_texture(hex_code_image_tex[1]);
  388. hex_code_box_font_tex[1]->set_texture_filter(CanvasItem::TEXTURE_FILTER_NEAREST);
  389. hex_box_data.clear();
  390. }
  391. }
  392. void TextServer::finish_hex_code_box_fonts() {
  393. if (hex_code_box_font_tex[0].is_valid()) {
  394. hex_code_box_font_tex[0].unref();
  395. }
  396. if (hex_code_box_font_tex[1].is_valid()) {
  397. hex_code_box_font_tex[1].unref();
  398. }
  399. }
  400. Vector2 TextServer::get_hex_code_box_size(int p_size, char32_t p_index) const {
  401. int fnt = (p_size < 20) ? 0 : 1;
  402. float w = ((p_index <= 0xFF) ? 1 : ((p_index <= 0xFFFF) ? 2 : 3)) * hex_code_box_font_size[fnt].x;
  403. float h = 2 * hex_code_box_font_size[fnt].y;
  404. return Vector2(w + 4, h + 3 + 2 * hex_code_box_font_size[fnt].z);
  405. }
  406. void TextServer::draw_hex_code_box(RID p_canvas, int p_size, const Vector2 &p_pos, char32_t p_index, const Color &p_color) const {
  407. int fnt = (p_size < 20) ? 0 : 1;
  408. ERR_FAIL_COND(hex_code_box_font_tex[fnt].is_null());
  409. uint8_t a = p_index & 0x0F;
  410. uint8_t b = (p_index >> 4) & 0x0F;
  411. uint8_t c = (p_index >> 8) & 0x0F;
  412. uint8_t d = (p_index >> 12) & 0x0F;
  413. uint8_t e = (p_index >> 16) & 0x0F;
  414. uint8_t f = (p_index >> 20) & 0x0F;
  415. Vector2 pos = p_pos;
  416. Rect2 dest = Rect2(Vector2(), Vector2(hex_code_box_font_size[fnt].x, hex_code_box_font_size[fnt].y));
  417. float w = ((p_index <= 0xFF) ? 1 : ((p_index <= 0xFFFF) ? 2 : 3)) * hex_code_box_font_size[fnt].x;
  418. float h = 2 * hex_code_box_font_size[fnt].y;
  419. pos.y -= Math::floor((h + 3 + hex_code_box_font_size[fnt].z) * 0.75);
  420. RenderingServer::get_singleton()->canvas_item_add_rect(p_canvas, Rect2(pos + Point2(0, 0), Size2(1, h + 2 + 2 * hex_code_box_font_size[fnt].z)), p_color);
  421. RenderingServer::get_singleton()->canvas_item_add_rect(p_canvas, Rect2(pos + Point2(w + 2, 0), Size2(1, h + 2 + 2 * hex_code_box_font_size[fnt].z)), p_color);
  422. RenderingServer::get_singleton()->canvas_item_add_rect(p_canvas, Rect2(pos + Point2(0, 0), Size2(w + 2, 1)), p_color);
  423. RenderingServer::get_singleton()->canvas_item_add_rect(p_canvas, Rect2(pos + Point2(0, h + 2 + 2 * hex_code_box_font_size[fnt].z), Size2(w + 2, 1)), p_color);
  424. pos += Point2(2, 2);
  425. if (p_index <= 0xFF) {
  426. dest.position = pos + Vector2(hex_code_box_font_size[fnt].x, hex_code_box_font_size[fnt].y) * Point2(0, 0);
  427. RenderingServer::get_singleton()->canvas_item_add_texture_rect_region(p_canvas, dest, hex_code_box_font_tex[fnt]->get_rid(), Rect2(Point2(b * hex_code_box_font_size[fnt].x, 0), dest.size), p_color, false, false);
  428. dest.position = pos + Vector2(hex_code_box_font_size[fnt].x, hex_code_box_font_size[fnt].y) * Point2(0, 1) + Point2(0, hex_code_box_font_size[fnt].z);
  429. RenderingServer::get_singleton()->canvas_item_add_texture_rect_region(p_canvas, dest, hex_code_box_font_tex[fnt]->get_rid(), Rect2(Point2(a * hex_code_box_font_size[fnt].x, 0), dest.size), p_color, false, false);
  430. } else if (p_index <= 0xFFFF) {
  431. dest.position = pos + Vector2(hex_code_box_font_size[fnt].x, hex_code_box_font_size[fnt].y) * Point2(0, 0);
  432. RenderingServer::get_singleton()->canvas_item_add_texture_rect_region(p_canvas, dest, hex_code_box_font_tex[fnt]->get_rid(), Rect2(Point2(d * hex_code_box_font_size[fnt].x, 0), dest.size), p_color, false, false);
  433. dest.position = pos + Vector2(hex_code_box_font_size[fnt].x, hex_code_box_font_size[fnt].y) * Point2(1, 0);
  434. RenderingServer::get_singleton()->canvas_item_add_texture_rect_region(p_canvas, dest, hex_code_box_font_tex[fnt]->get_rid(), Rect2(Point2(c * hex_code_box_font_size[fnt].x, 0), dest.size), p_color, false, false);
  435. dest.position = pos + Vector2(hex_code_box_font_size[fnt].x, hex_code_box_font_size[fnt].y) * Point2(0, 1) + Point2(0, hex_code_box_font_size[fnt].z);
  436. RenderingServer::get_singleton()->canvas_item_add_texture_rect_region(p_canvas, dest, hex_code_box_font_tex[fnt]->get_rid(), Rect2(Point2(b * hex_code_box_font_size[fnt].x, 0), dest.size), p_color, false, false);
  437. dest.position = pos + Vector2(hex_code_box_font_size[fnt].x, hex_code_box_font_size[fnt].y) * Point2(1, 1) + Point2(0, hex_code_box_font_size[fnt].z);
  438. RenderingServer::get_singleton()->canvas_item_add_texture_rect_region(p_canvas, dest, hex_code_box_font_tex[fnt]->get_rid(), Rect2(Point2(a * hex_code_box_font_size[fnt].x, 0), dest.size), p_color, false, false);
  439. } else {
  440. dest.position = pos + Vector2(hex_code_box_font_size[fnt].x, hex_code_box_font_size[fnt].y) * Point2(0, 0);
  441. RenderingServer::get_singleton()->canvas_item_add_texture_rect_region(p_canvas, dest, hex_code_box_font_tex[fnt]->get_rid(), Rect2(Point2(f * hex_code_box_font_size[fnt].x, 0), dest.size), p_color, false, false);
  442. dest.position = pos + Vector2(hex_code_box_font_size[fnt].x, hex_code_box_font_size[fnt].y) * Point2(1, 0);
  443. RenderingServer::get_singleton()->canvas_item_add_texture_rect_region(p_canvas, dest, hex_code_box_font_tex[fnt]->get_rid(), Rect2(Point2(e * hex_code_box_font_size[fnt].x, 0), dest.size), p_color, false, false);
  444. dest.position = pos + Vector2(hex_code_box_font_size[fnt].x, hex_code_box_font_size[fnt].y) * Point2(2, 0);
  445. RenderingServer::get_singleton()->canvas_item_add_texture_rect_region(p_canvas, dest, hex_code_box_font_tex[fnt]->get_rid(), Rect2(Point2(d * hex_code_box_font_size[fnt].x, 0), dest.size), p_color, false, false);
  446. dest.position = pos + Vector2(hex_code_box_font_size[fnt].x, hex_code_box_font_size[fnt].y) * Point2(0, 1) + Point2(0, hex_code_box_font_size[fnt].z);
  447. RenderingServer::get_singleton()->canvas_item_add_texture_rect_region(p_canvas, dest, hex_code_box_font_tex[fnt]->get_rid(), Rect2(Point2(c * hex_code_box_font_size[fnt].x, 0), dest.size), p_color, false, false);
  448. dest.position = pos + Vector2(hex_code_box_font_size[fnt].x, hex_code_box_font_size[fnt].y) * Point2(1, 1) + Point2(0, hex_code_box_font_size[fnt].z);
  449. RenderingServer::get_singleton()->canvas_item_add_texture_rect_region(p_canvas, dest, hex_code_box_font_tex[fnt]->get_rid(), Rect2(Point2(b * hex_code_box_font_size[fnt].x, 0), dest.size), p_color, false, false);
  450. dest.position = pos + Vector2(hex_code_box_font_size[fnt].x, hex_code_box_font_size[fnt].y) * Point2(2, 1) + Point2(0, hex_code_box_font_size[fnt].z);
  451. RenderingServer::get_singleton()->canvas_item_add_texture_rect_region(p_canvas, dest, hex_code_box_font_tex[fnt]->get_rid(), Rect2(Point2(a * hex_code_box_font_size[fnt].x, 0), dest.size), p_color, false, false);
  452. }
  453. }
  454. Vector<Vector2i> TextServer::shaped_text_get_line_breaks_adv(RID p_shaped, const Vector<float> &p_width, int p_start, bool p_once, uint8_t /*TextBreakFlag*/ p_break_flags) const {
  455. Vector<Vector2i> lines;
  456. ERR_FAIL_COND_V(p_width.is_empty(), lines);
  457. const_cast<TextServer *>(this)->shaped_text_update_breaks(p_shaped);
  458. const Vector<Glyph> &logical = const_cast<TextServer *>(this)->shaped_text_sort_logical(p_shaped);
  459. const Vector2i &range = shaped_text_get_range(p_shaped);
  460. float width = 0.f;
  461. int line_start = MAX(p_start, range.x);
  462. int last_safe_break = -1;
  463. int chunk = 0;
  464. int l_size = logical.size();
  465. const Glyph *l_gl = logical.ptr();
  466. for (int i = 0; i < l_size; i++) {
  467. if (l_gl[i].start < p_start) {
  468. continue;
  469. }
  470. if (l_gl[i].count > 0) {
  471. if ((p_width[chunk] > 0) && (width + l_gl[i].advance > p_width[chunk]) && (last_safe_break >= 0)) {
  472. lines.push_back(Vector2i(line_start, l_gl[last_safe_break].end));
  473. line_start = l_gl[last_safe_break].end;
  474. i = last_safe_break;
  475. last_safe_break = -1;
  476. width = 0;
  477. chunk++;
  478. if (chunk >= p_width.size()) {
  479. chunk = 0;
  480. if (p_once) {
  481. return lines;
  482. }
  483. }
  484. continue;
  485. }
  486. if ((p_break_flags & BREAK_MANDATORY) == BREAK_MANDATORY) {
  487. if ((l_gl[i].flags & GRAPHEME_IS_BREAK_HARD) == GRAPHEME_IS_BREAK_HARD) {
  488. lines.push_back(Vector2i(line_start, l_gl[i].end));
  489. line_start = l_gl[i].end;
  490. last_safe_break = -1;
  491. width = 0;
  492. chunk = 0;
  493. if (p_once) {
  494. return lines;
  495. }
  496. continue;
  497. }
  498. }
  499. if ((p_break_flags & BREAK_WORD_BOUND) == BREAK_WORD_BOUND) {
  500. if ((l_gl[i].flags & GRAPHEME_IS_BREAK_SOFT) == GRAPHEME_IS_BREAK_SOFT) {
  501. last_safe_break = i;
  502. }
  503. }
  504. if ((p_break_flags & BREAK_GRAPHEME_BOUND) == BREAK_GRAPHEME_BOUND) {
  505. last_safe_break = i;
  506. }
  507. }
  508. width += l_gl[i].advance;
  509. }
  510. if (l_size > 0) {
  511. lines.push_back(Vector2i(line_start, range.y));
  512. } else {
  513. lines.push_back(Vector2i(0, 0));
  514. }
  515. return lines;
  516. }
  517. Vector<Vector2i> TextServer::shaped_text_get_line_breaks(RID p_shaped, float p_width, int p_start, uint8_t /*TextBreakFlag*/ p_break_flags) const {
  518. Vector<Vector2i> lines;
  519. const_cast<TextServer *>(this)->shaped_text_update_breaks(p_shaped);
  520. const Vector<Glyph> &logical = const_cast<TextServer *>(this)->shaped_text_sort_logical(p_shaped);
  521. const Vector2i &range = shaped_text_get_range(p_shaped);
  522. float width = 0.f;
  523. int line_start = MAX(p_start, range.x);
  524. int last_safe_break = -1;
  525. int l_size = logical.size();
  526. const Glyph *l_gl = logical.ptr();
  527. for (int i = 0; i < l_size; i++) {
  528. if (l_gl[i].start < p_start) {
  529. continue;
  530. }
  531. if (l_gl[i].count > 0) {
  532. if ((p_width > 0) && (width + l_gl[i].advance > p_width) && (last_safe_break >= 0)) {
  533. lines.push_back(Vector2i(line_start, l_gl[last_safe_break].end));
  534. line_start = l_gl[last_safe_break].end;
  535. i = last_safe_break;
  536. last_safe_break = -1;
  537. width = 0;
  538. continue;
  539. }
  540. if ((p_break_flags & BREAK_MANDATORY) == BREAK_MANDATORY) {
  541. if ((l_gl[i].flags & GRAPHEME_IS_BREAK_HARD) == GRAPHEME_IS_BREAK_HARD) {
  542. lines.push_back(Vector2i(line_start, l_gl[i].end));
  543. line_start = l_gl[i].end;
  544. last_safe_break = -1;
  545. width = 0;
  546. continue;
  547. }
  548. }
  549. if ((p_break_flags & BREAK_WORD_BOUND) == BREAK_WORD_BOUND) {
  550. if ((l_gl[i].flags & GRAPHEME_IS_BREAK_SOFT) == GRAPHEME_IS_BREAK_SOFT) {
  551. last_safe_break = i;
  552. }
  553. }
  554. if ((p_break_flags & BREAK_GRAPHEME_BOUND) == BREAK_GRAPHEME_BOUND) {
  555. last_safe_break = i;
  556. }
  557. }
  558. width += l_gl[i].advance;
  559. }
  560. if (l_size > 0) {
  561. if (lines.size() == 0 || lines[lines.size() - 1].y < range.y) {
  562. lines.push_back(Vector2i(line_start, range.y));
  563. }
  564. } else {
  565. lines.push_back(Vector2i(0, 0));
  566. }
  567. return lines;
  568. }
  569. Vector<Vector2i> TextServer::shaped_text_get_word_breaks(RID p_shaped) const {
  570. Vector<Vector2i> words;
  571. const_cast<TextServer *>(this)->shaped_text_update_justification_ops(p_shaped);
  572. const Vector<Glyph> &logical = const_cast<TextServer *>(this)->shaped_text_sort_logical(p_shaped);
  573. const Vector2i &range = shaped_text_get_range(p_shaped);
  574. int word_start = range.x;
  575. int l_size = logical.size();
  576. const Glyph *l_gl = logical.ptr();
  577. for (int i = 0; i < l_size; i++) {
  578. if (l_gl[i].count > 0) {
  579. if (((l_gl[i].flags & GRAPHEME_IS_SPACE) == GRAPHEME_IS_SPACE) || ((l_gl[i].flags & GRAPHEME_IS_PUNCTUATION) == GRAPHEME_IS_PUNCTUATION)) {
  580. words.push_back(Vector2i(word_start, l_gl[i].start));
  581. word_start = l_gl[i].end;
  582. }
  583. }
  584. }
  585. if (l_size > 0) {
  586. words.push_back(Vector2i(word_start, range.y));
  587. }
  588. return words;
  589. }
  590. void TextServer::shaped_text_get_carets(RID p_shaped, int p_position, Rect2 &p_leading_caret, Direction &p_leading_dir, Rect2 &p_trailing_caret, Direction &p_trailing_dir) const {
  591. Vector<Rect2> carets;
  592. const Vector<TextServer::Glyph> visual = shaped_text_get_glyphs(p_shaped);
  593. TextServer::Orientation orientation = shaped_text_get_orientation(p_shaped);
  594. const Vector2 &range = shaped_text_get_range(p_shaped);
  595. float ascent = shaped_text_get_ascent(p_shaped);
  596. float descent = shaped_text_get_descent(p_shaped);
  597. float height = (ascent + descent) / 2;
  598. float off = 0.0f;
  599. p_leading_dir = DIRECTION_AUTO;
  600. p_trailing_dir = DIRECTION_AUTO;
  601. int v_size = visual.size();
  602. const Glyph *glyphs = visual.ptr();
  603. for (int i = 0; i < v_size; i++) {
  604. if (glyphs[i].count > 0) {
  605. // Caret before grapheme (top / left).
  606. if (p_position == glyphs[i].start && ((glyphs[i].flags & GRAPHEME_IS_VIRTUAL) != GRAPHEME_IS_VIRTUAL)) {
  607. Rect2 cr;
  608. if (orientation == ORIENTATION_HORIZONTAL) {
  609. if (glyphs[i].start == range.x) {
  610. cr.size.y = height * 2;
  611. } else {
  612. cr.size.y = height;
  613. }
  614. cr.position.y = -ascent;
  615. cr.position.x = off;
  616. if ((glyphs[i].flags & GRAPHEME_IS_RTL) == GRAPHEME_IS_RTL) {
  617. p_trailing_dir = DIRECTION_RTL;
  618. for (int j = 0; j < glyphs[i].count; j++) {
  619. cr.position.x += glyphs[i + j].advance * glyphs[i + j].repeat;
  620. cr.size.x -= glyphs[i + j].advance * glyphs[i + j].repeat;
  621. }
  622. } else {
  623. p_trailing_dir = DIRECTION_LTR;
  624. for (int j = 0; j < glyphs[i].count; j++) {
  625. cr.size.x += glyphs[i + j].advance * glyphs[i + j].repeat;
  626. }
  627. }
  628. } else {
  629. if (glyphs[i].start == range.x) {
  630. cr.size.x = height * 2;
  631. } else {
  632. cr.size.x = height;
  633. }
  634. cr.position.x = -ascent;
  635. cr.position.y = off;
  636. if ((glyphs[i].flags & GRAPHEME_IS_RTL) == GRAPHEME_IS_RTL) {
  637. p_trailing_dir = DIRECTION_RTL;
  638. for (int j = 0; j < glyphs[i].count; j++) {
  639. cr.position.y += glyphs[i + j].advance * glyphs[i + j].repeat;
  640. cr.size.y -= glyphs[i + j].advance * glyphs[i + j].repeat;
  641. }
  642. } else {
  643. p_trailing_dir = DIRECTION_LTR;
  644. for (int j = 0; j < glyphs[i].count; j++) {
  645. cr.size.y += glyphs[i + j].advance * glyphs[i + j].repeat;
  646. }
  647. }
  648. }
  649. p_trailing_caret = cr;
  650. }
  651. // Caret after grapheme (bottom / right).
  652. if (p_position == glyphs[i].end && ((glyphs[i].flags & GRAPHEME_IS_VIRTUAL) != GRAPHEME_IS_VIRTUAL)) {
  653. Rect2 cr;
  654. if (orientation == ORIENTATION_HORIZONTAL) {
  655. if (glyphs[i].end == range.y) {
  656. cr.size.y = height * 2;
  657. cr.position.y = -ascent;
  658. } else {
  659. cr.size.y = height;
  660. cr.position.y = -ascent + height;
  661. }
  662. cr.position.x = off;
  663. if ((glyphs[i].flags & GRAPHEME_IS_RTL) != GRAPHEME_IS_RTL) {
  664. p_leading_dir = DIRECTION_LTR;
  665. for (int j = 0; j < glyphs[i].count; j++) {
  666. cr.position.x += glyphs[i + j].advance * glyphs[i + j].repeat;
  667. cr.size.x -= glyphs[i + j].advance * glyphs[i + j].repeat;
  668. }
  669. } else {
  670. p_leading_dir = DIRECTION_RTL;
  671. for (int j = 0; j < glyphs[i].count; j++) {
  672. cr.size.x += glyphs[i + j].advance * glyphs[i + j].repeat;
  673. }
  674. }
  675. } else {
  676. cr.size.y = 1.0f;
  677. if (glyphs[i].end == range.y) {
  678. cr.size.x = height * 2;
  679. cr.position.x = -ascent;
  680. } else {
  681. cr.size.x = height;
  682. cr.position.x = -ascent + height;
  683. }
  684. cr.position.y = off;
  685. if ((glyphs[i].flags & GRAPHEME_IS_RTL) != GRAPHEME_IS_RTL) {
  686. p_leading_dir = DIRECTION_LTR;
  687. for (int j = 0; j < glyphs[i].count; j++) {
  688. cr.position.y += glyphs[i + j].advance * glyphs[i + j].repeat;
  689. cr.size.y -= glyphs[i + j].advance * glyphs[i + j].repeat;
  690. }
  691. } else {
  692. p_leading_dir = DIRECTION_RTL;
  693. for (int j = 0; j < glyphs[i].count; j++) {
  694. cr.size.y += glyphs[i + j].advance * glyphs[i + j].repeat;
  695. }
  696. }
  697. }
  698. p_leading_caret = cr;
  699. }
  700. // Caret inside grapheme (middle).
  701. if (p_position > glyphs[i].start && p_position < glyphs[i].end && (glyphs[i].flags & GRAPHEME_IS_VIRTUAL) != GRAPHEME_IS_VIRTUAL) {
  702. float advance = 0.f;
  703. for (int j = 0; j < glyphs[i].count; j++) {
  704. advance += glyphs[i + j].advance * glyphs[i + j].repeat;
  705. }
  706. float char_adv = advance / (float)(glyphs[i].end - glyphs[i].start);
  707. Rect2 cr;
  708. if (orientation == ORIENTATION_HORIZONTAL) {
  709. cr.size.x = 1.0f;
  710. cr.size.y = height * 2;
  711. cr.position.y = -ascent;
  712. if ((glyphs[i].flags & GRAPHEME_IS_RTL) == GRAPHEME_IS_RTL) {
  713. cr.position.x = off + char_adv * (glyphs[i].end - p_position);
  714. } else {
  715. cr.position.x = off + char_adv * (p_position - glyphs[i].start);
  716. }
  717. } else {
  718. cr.size.y = 1.0f;
  719. cr.size.x = height * 2;
  720. cr.position.x = -ascent;
  721. if ((glyphs[i].flags & GRAPHEME_IS_RTL) == GRAPHEME_IS_RTL) {
  722. cr.position.y = off + char_adv * (glyphs[i].end - p_position);
  723. } else {
  724. cr.position.y = off + char_adv * (p_position - glyphs[i].start);
  725. }
  726. }
  727. p_trailing_caret = cr;
  728. p_leading_caret = cr;
  729. }
  730. }
  731. off += glyphs[i].advance * glyphs[i].repeat;
  732. }
  733. }
  734. TextServer::Direction TextServer::shaped_text_get_dominant_direciton_in_range(RID p_shaped, int p_start, int p_end) const {
  735. const Vector<TextServer::Glyph> visual = shaped_text_get_glyphs(p_shaped);
  736. if (p_start == p_end) {
  737. return DIRECTION_AUTO;
  738. }
  739. int start = MIN(p_start, p_end);
  740. int end = MAX(p_start, p_end);
  741. int rtl = 0;
  742. int ltr = 0;
  743. int v_size = visual.size();
  744. const Glyph *glyphs = visual.ptr();
  745. for (int i = 0; i < v_size; i++) {
  746. if ((glyphs[i].end > start) && (glyphs[i].start < end)) {
  747. if (glyphs[i].count > 0) {
  748. if ((glyphs[i].flags & GRAPHEME_IS_RTL) == GRAPHEME_IS_RTL) {
  749. rtl++;
  750. } else {
  751. ltr++;
  752. }
  753. }
  754. }
  755. }
  756. if (ltr == rtl) {
  757. return DIRECTION_AUTO;
  758. } else if (ltr > rtl) {
  759. return DIRECTION_LTR;
  760. } else {
  761. return DIRECTION_RTL;
  762. }
  763. }
  764. Vector<Vector2> TextServer::shaped_text_get_selection(RID p_shaped, int p_start, int p_end) const {
  765. Vector<Vector2> ranges;
  766. const Vector<TextServer::Glyph> visual = shaped_text_get_glyphs(p_shaped);
  767. if (p_start == p_end) {
  768. return ranges;
  769. }
  770. int start = MIN(p_start, p_end);
  771. int end = MAX(p_start, p_end);
  772. int v_size = visual.size();
  773. const Glyph *glyphs = visual.ptr();
  774. float off = 0.0f;
  775. for (int i = 0; i < v_size; i++) {
  776. for (int k = 0; k < glyphs[i].repeat; k++) {
  777. if ((glyphs[i].count > 0) && ((glyphs[i].index != 0) || ((glyphs[i].flags & GRAPHEME_IS_SPACE) == GRAPHEME_IS_SPACE))) {
  778. if (glyphs[i].start < end && glyphs[i].end > start) {
  779. // Grapheme fully in selection range.
  780. if (glyphs[i].start >= start && glyphs[i].end <= end) {
  781. float advance = 0.f;
  782. for (int j = 0; j < glyphs[i].count; j++) {
  783. advance += glyphs[i + j].advance;
  784. }
  785. ranges.push_back(Vector2(off, off + advance));
  786. }
  787. // Only start of grapheme is in selection range.
  788. if (glyphs[i].start >= start && glyphs[i].end > end) {
  789. float advance = 0.f;
  790. for (int j = 0; j < glyphs[i].count; j++) {
  791. advance += glyphs[i + j].advance;
  792. }
  793. float char_adv = advance / (float)(glyphs[i].end - glyphs[i].start);
  794. if ((glyphs[i].flags & GRAPHEME_IS_RTL) == GRAPHEME_IS_RTL) {
  795. ranges.push_back(Vector2(off + char_adv * (glyphs[i].end - end), off + advance));
  796. } else {
  797. ranges.push_back(Vector2(off, off + char_adv * (end - glyphs[i].start)));
  798. }
  799. }
  800. // Only end of grapheme is in selection range.
  801. if (glyphs[i].start < start && glyphs[i].end <= end) {
  802. float advance = 0.f;
  803. for (int j = 0; j < glyphs[i].count; j++) {
  804. advance += glyphs[i + j].advance;
  805. }
  806. float char_adv = advance / (float)(glyphs[i].end - glyphs[i].start);
  807. if ((glyphs[i].flags & GRAPHEME_IS_RTL) == GRAPHEME_IS_RTL) {
  808. ranges.push_back(Vector2(off, off + char_adv * (start - glyphs[i].start)));
  809. } else {
  810. ranges.push_back(Vector2(off + char_adv * (glyphs[i].end - start), off + advance));
  811. }
  812. }
  813. // Selection range is within grapheme
  814. if (glyphs[i].start < start && glyphs[i].end > end) {
  815. float advance = 0.f;
  816. for (int j = 0; j < glyphs[i].count; j++) {
  817. advance += glyphs[i + j].advance;
  818. }
  819. float char_adv = advance / (float)(glyphs[i].end - glyphs[i].start);
  820. if ((glyphs[i].flags & GRAPHEME_IS_RTL) == GRAPHEME_IS_RTL) {
  821. ranges.push_back(Vector2(off + char_adv * (glyphs[i].end - end), off + char_adv * (glyphs[i].end - start)));
  822. } else {
  823. ranges.push_back(Vector2(off + char_adv * (start - glyphs[i].start), off + char_adv * (end - glyphs[i].start)));
  824. }
  825. }
  826. }
  827. }
  828. off += glyphs[i].advance;
  829. }
  830. }
  831. // Merge intersecting ranges.
  832. int i = 0;
  833. while (i < ranges.size()) {
  834. i++;
  835. }
  836. i = 0;
  837. while (i < ranges.size()) {
  838. int j = i + 1;
  839. while (j < ranges.size()) {
  840. if (Math::is_equal_approx(ranges[i].y, ranges[j].x, UNIT_EPSILON)) {
  841. ranges.write[i].y = ranges[j].y;
  842. ranges.remove(j);
  843. continue;
  844. }
  845. j++;
  846. }
  847. i++;
  848. }
  849. return ranges;
  850. }
  851. int TextServer::shaped_text_hit_test_grapheme(RID p_shaped, float p_coords) const {
  852. const Vector<TextServer::Glyph> visual = shaped_text_get_glyphs(p_shaped);
  853. // Exact grapheme hit test, return -1 if missed.
  854. float off = 0.0f;
  855. int v_size = visual.size();
  856. const Glyph *glyphs = visual.ptr();
  857. for (int i = 0; i < v_size; i++) {
  858. for (int j = 0; j < glyphs[i].repeat; j++) {
  859. if (p_coords >= off && p_coords < off + glyphs[i].advance) {
  860. return i;
  861. }
  862. off += glyphs[i].advance;
  863. }
  864. }
  865. return -1;
  866. }
  867. int TextServer::shaped_text_hit_test_position(RID p_shaped, float p_coords) const {
  868. const Vector<TextServer::Glyph> visual = shaped_text_get_glyphs(p_shaped);
  869. int v_size = visual.size();
  870. const Glyph *glyphs = visual.ptr();
  871. // Cursor placement hit test.
  872. // Place caret to the left of the leftmost grapheme, or to position 0 if string is empty.
  873. if (p_coords <= 0) {
  874. if (v_size > 0) {
  875. if ((glyphs[0].flags & GRAPHEME_IS_RTL) == GRAPHEME_IS_RTL) {
  876. return glyphs[0].end;
  877. } else {
  878. return glyphs[0].start;
  879. }
  880. } else {
  881. return 0;
  882. }
  883. }
  884. // Place caret to the right of the rightmost grapheme, or to position 0 if string is empty.
  885. if (p_coords >= shaped_text_get_width(p_shaped)) {
  886. if (v_size > 0) {
  887. if ((glyphs[v_size - 1].flags & GRAPHEME_IS_RTL) == GRAPHEME_IS_RTL) {
  888. return glyphs[v_size - 1].start;
  889. } else {
  890. return glyphs[v_size - 1].end;
  891. }
  892. } else {
  893. return 0;
  894. }
  895. }
  896. float off = 0.0f;
  897. for (int i = 0; i < v_size; i++) {
  898. if (glyphs[i].count > 0) {
  899. float advance = 0.f;
  900. for (int j = 0; j < glyphs[i].count; j++) {
  901. advance += glyphs[i + j].advance * glyphs[i + j].repeat;
  902. }
  903. if (((glyphs[i].flags & GRAPHEME_IS_VIRTUAL) == GRAPHEME_IS_VIRTUAL) && (p_coords >= off && p_coords < off + advance)) {
  904. if ((glyphs[i].flags & GRAPHEME_IS_RTL) == GRAPHEME_IS_RTL) {
  905. return glyphs[i].end;
  906. } else {
  907. return glyphs[i].start;
  908. }
  909. }
  910. // Place caret to the left of clicked grapheme.
  911. if (p_coords >= off && p_coords < off + advance / 2) {
  912. if ((glyphs[i].flags & GRAPHEME_IS_RTL) == GRAPHEME_IS_RTL) {
  913. return glyphs[i].end;
  914. } else {
  915. return glyphs[i].start;
  916. }
  917. }
  918. // Place caret to the right of clicked grapheme.
  919. if (p_coords >= off + advance / 2 && p_coords < off + advance) {
  920. if ((glyphs[i].flags & GRAPHEME_IS_RTL) == GRAPHEME_IS_RTL) {
  921. return glyphs[i].start;
  922. } else {
  923. return glyphs[i].end;
  924. }
  925. }
  926. }
  927. off += glyphs[i].advance * glyphs[i].repeat;
  928. }
  929. return 0;
  930. }
  931. int TextServer::shaped_text_next_grapheme_pos(RID p_shaped, int p_pos) {
  932. const Vector<TextServer::Glyph> visual = shaped_text_get_glyphs(p_shaped);
  933. int v_size = visual.size();
  934. const Glyph *glyphs = visual.ptr();
  935. for (int i = 0; i < v_size; i++) {
  936. if (p_pos >= glyphs[i].start && p_pos < glyphs[i].end) {
  937. return glyphs[i].end;
  938. }
  939. }
  940. return p_pos;
  941. }
  942. int TextServer::shaped_text_prev_grapheme_pos(RID p_shaped, int p_pos) {
  943. const Vector<TextServer::Glyph> visual = shaped_text_get_glyphs(p_shaped);
  944. int v_size = visual.size();
  945. const Glyph *glyphs = visual.ptr();
  946. for (int i = 0; i < v_size; i++) {
  947. if (p_pos > glyphs[i].start && p_pos <= glyphs[i].end) {
  948. return glyphs[i].start;
  949. }
  950. }
  951. return p_pos;
  952. }
  953. void TextServer::shaped_text_draw(RID p_shaped, RID p_canvas, const Vector2 &p_pos, float p_clip_l, float p_clip_r, const Color &p_color) const {
  954. const Vector<TextServer::Glyph> visual = shaped_text_get_glyphs(p_shaped);
  955. TextServer::Orientation orientation = shaped_text_get_orientation(p_shaped);
  956. bool hex_codes = shaped_text_get_preserve_control(p_shaped) || shaped_text_get_preserve_invalid(p_shaped);
  957. int v_size = visual.size();
  958. const Glyph *glyphs = visual.ptr();
  959. Vector2 ofs = p_pos;
  960. // Draw at the baseline.
  961. for (int i = 0; i < v_size; i++) {
  962. for (int j = 0; j < glyphs[i].repeat; j++) {
  963. if (p_clip_r > 0) {
  964. // Clip right / bottom.
  965. if (orientation == ORIENTATION_HORIZONTAL) {
  966. if (ofs.x - p_pos.x > p_clip_r) {
  967. return;
  968. }
  969. } else {
  970. if (ofs.y - p_pos.y > p_clip_r) {
  971. return;
  972. }
  973. }
  974. }
  975. if (p_clip_l > 0) {
  976. // Clip left / top.
  977. if (orientation == ORIENTATION_HORIZONTAL) {
  978. if (ofs.x - p_pos.x < p_clip_l) {
  979. ofs.x += glyphs[i].advance;
  980. continue;
  981. }
  982. } else {
  983. if (ofs.y - p_pos.y < p_clip_l) {
  984. ofs.y += glyphs[i].advance;
  985. continue;
  986. }
  987. }
  988. }
  989. if (glyphs[i].font_rid != RID()) {
  990. font_draw_glyph(glyphs[i].font_rid, p_canvas, glyphs[i].font_size, ofs + Vector2(glyphs[i].x_off, glyphs[i].y_off), glyphs[i].index, p_color);
  991. } else if (hex_codes && ((glyphs[i].flags & GRAPHEME_IS_VIRTUAL) != GRAPHEME_IS_VIRTUAL)) {
  992. TextServer::draw_hex_code_box(p_canvas, glyphs[i].font_size, ofs + Vector2(glyphs[i].x_off, glyphs[i].y_off), glyphs[i].index, p_color);
  993. }
  994. if (orientation == ORIENTATION_HORIZONTAL) {
  995. ofs.x += glyphs[i].advance;
  996. } else {
  997. ofs.y += glyphs[i].advance;
  998. }
  999. }
  1000. }
  1001. }
  1002. void TextServer::shaped_text_draw_outline(RID p_shaped, RID p_canvas, const Vector2 &p_pos, float p_clip_l, float p_clip_r, int p_outline_size, const Color &p_color) const {
  1003. const Vector<TextServer::Glyph> visual = shaped_text_get_glyphs(p_shaped);
  1004. TextServer::Orientation orientation = shaped_text_get_orientation(p_shaped);
  1005. int v_size = visual.size();
  1006. const Glyph *glyphs = visual.ptr();
  1007. Vector2 ofs = p_pos;
  1008. // Draw at the baseline.
  1009. for (int i = 0; i < v_size; i++) {
  1010. for (int j = 0; j < glyphs[i].repeat; j++) {
  1011. if (p_clip_r > 0) {
  1012. // Clip right / bottom.
  1013. if (orientation == ORIENTATION_HORIZONTAL) {
  1014. if (ofs.x - p_pos.x > p_clip_r) {
  1015. return;
  1016. }
  1017. } else {
  1018. if (ofs.y - p_pos.y > p_clip_r) {
  1019. return;
  1020. }
  1021. }
  1022. }
  1023. if (p_clip_l > 0) {
  1024. // Clip left / top.
  1025. if (orientation == ORIENTATION_HORIZONTAL) {
  1026. if (ofs.x - p_pos.x < p_clip_l) {
  1027. ofs.x += glyphs[i].advance;
  1028. continue;
  1029. }
  1030. } else {
  1031. if (ofs.y - p_pos.y < p_clip_l) {
  1032. ofs.y += glyphs[i].advance;
  1033. continue;
  1034. }
  1035. }
  1036. }
  1037. if (glyphs[i].font_rid != RID()) {
  1038. font_draw_glyph_outline(glyphs[i].font_rid, p_canvas, glyphs[i].font_size, p_outline_size, ofs + Vector2(glyphs[i].x_off, glyphs[i].y_off), glyphs[i].index, p_color);
  1039. }
  1040. if (orientation == ORIENTATION_HORIZONTAL) {
  1041. ofs.x += glyphs[i].advance;
  1042. } else {
  1043. ofs.y += glyphs[i].advance;
  1044. }
  1045. }
  1046. }
  1047. }
  1048. RID TextServer::_create_font_memory(const PackedByteArray &p_data, const String &p_type, int p_base_size) {
  1049. return create_font_memory(p_data.ptr(), p_data.size(), p_type, p_base_size);
  1050. }
  1051. void TextServer::_shaped_text_set_bidi_override(RID p_shaped, const Array &p_override) {
  1052. Vector<Vector2i> overrides;
  1053. for (int i = 0; i < p_override.size(); i++) {
  1054. overrides.push_back(p_override[i]);
  1055. }
  1056. shaped_text_set_bidi_override(p_shaped, overrides);
  1057. }
  1058. Array TextServer::_shaped_text_get_glyphs(RID p_shaped) const {
  1059. Array ret;
  1060. Vector<Glyph> glyphs = shaped_text_get_glyphs(p_shaped);
  1061. for (int i = 0; i < glyphs.size(); i++) {
  1062. Dictionary glyph;
  1063. glyph["start"] = glyphs[i].start;
  1064. glyph["end"] = glyphs[i].end;
  1065. glyph["repeat"] = glyphs[i].repeat;
  1066. glyph["count"] = glyphs[i].count;
  1067. glyph["flags"] = glyphs[i].flags;
  1068. glyph["offset"] = Vector2(glyphs[i].x_off, glyphs[i].y_off);
  1069. glyph["advance"] = glyphs[i].advance;
  1070. glyph["font_rid"] = glyphs[i].font_rid;
  1071. glyph["font_size"] = glyphs[i].font_size;
  1072. glyph["index"] = glyphs[i].index;
  1073. ret.push_back(glyph);
  1074. }
  1075. return ret;
  1076. }
  1077. Array TextServer::_shaped_text_get_line_breaks_adv(RID p_shaped, const PackedFloat32Array &p_width, int p_start, bool p_once, uint8_t p_break_flags) const {
  1078. Array ret;
  1079. Vector<Vector2i> lines = shaped_text_get_line_breaks_adv(p_shaped, p_width, p_start, p_once, p_break_flags);
  1080. for (int i = 0; i < lines.size(); i++) {
  1081. ret.push_back(lines[i]);
  1082. }
  1083. return ret;
  1084. }
  1085. Array TextServer::_shaped_text_get_line_breaks(RID p_shaped, float p_width, int p_start, uint8_t p_break_flags) const {
  1086. Array ret;
  1087. Vector<Vector2i> lines = shaped_text_get_line_breaks(p_shaped, p_width, p_start, p_break_flags);
  1088. for (int i = 0; i < lines.size(); i++) {
  1089. ret.push_back(lines[i]);
  1090. }
  1091. return ret;
  1092. }
  1093. Array TextServer::_shaped_text_get_word_breaks(RID p_shaped) const {
  1094. Array ret;
  1095. Vector<Vector2i> words = shaped_text_get_word_breaks(p_shaped);
  1096. for (int i = 0; i < words.size(); i++) {
  1097. ret.push_back(words[i]);
  1098. }
  1099. return ret;
  1100. }
  1101. Dictionary TextServer::_shaped_text_get_carets(RID p_shaped, int p_position) const {
  1102. Dictionary ret;
  1103. Rect2 l_caret, t_caret;
  1104. Direction l_dir, t_dir;
  1105. shaped_text_get_carets(p_shaped, p_position, l_caret, l_dir, t_caret, t_dir);
  1106. ret["leading_rect"] = l_caret;
  1107. ret["leading_direction"] = l_dir;
  1108. ret["trailing_rect"] = t_caret;
  1109. ret["trailing_direction"] = t_dir;
  1110. return ret;
  1111. }
  1112. Array TextServer::_shaped_text_get_selection(RID p_shaped, int p_start, int p_end) const {
  1113. Array ret;
  1114. Vector<Vector2> ranges = shaped_text_get_selection(p_shaped, p_start, p_end);
  1115. for (int i = 0; i < ranges.size(); i++) {
  1116. ret.push_back(ranges[i]);
  1117. }
  1118. return ret;
  1119. }
  1120. TextServer::TextServer() {
  1121. }
  1122. TextServer::~TextServer() {
  1123. }