text_server.cpp 53 KB

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