text_server.cpp 91 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181
  1. /**************************************************************************/
  2. /* text_server.cpp */
  3. /**************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /**************************************************************************/
  8. /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
  9. /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /**************************************************************************/
  30. #include "servers/text_server.h"
  31. #include "text_server.compat.inc"
  32. #include "core/variant/typed_array.h"
  33. #include "servers/rendering_server.h"
  34. TextServerManager *TextServerManager::singleton = nullptr;
  35. void TextServerManager::_bind_methods() {
  36. ClassDB::bind_method(D_METHOD("add_interface", "interface"), &TextServerManager::add_interface);
  37. ClassDB::bind_method(D_METHOD("get_interface_count"), &TextServerManager::get_interface_count);
  38. ClassDB::bind_method(D_METHOD("remove_interface", "interface"), &TextServerManager::remove_interface);
  39. ClassDB::bind_method(D_METHOD("get_interface", "idx"), &TextServerManager::get_interface);
  40. ClassDB::bind_method(D_METHOD("get_interfaces"), &TextServerManager::get_interfaces);
  41. ClassDB::bind_method(D_METHOD("find_interface", "name"), &TextServerManager::find_interface);
  42. ClassDB::bind_method(D_METHOD("set_primary_interface", "index"), &TextServerManager::set_primary_interface);
  43. ClassDB::bind_method(D_METHOD("get_primary_interface"), &TextServerManager::get_primary_interface);
  44. ADD_SIGNAL(MethodInfo("interface_added", PropertyInfo(Variant::STRING_NAME, "interface_name")));
  45. ADD_SIGNAL(MethodInfo("interface_removed", PropertyInfo(Variant::STRING_NAME, "interface_name")));
  46. }
  47. void TextServerManager::add_interface(const Ref<TextServer> &p_interface) {
  48. ERR_FAIL_COND(p_interface.is_null());
  49. for (int i = 0; i < interfaces.size(); i++) {
  50. if (interfaces[i] == p_interface) {
  51. ERR_PRINT("TextServer: Interface was already added.");
  52. return;
  53. };
  54. };
  55. interfaces.push_back(p_interface);
  56. print_verbose("TextServer: Added interface \"" + p_interface->get_name() + "\"");
  57. emit_signal(SNAME("interface_added"), p_interface->get_name());
  58. }
  59. void TextServerManager::remove_interface(const Ref<TextServer> &p_interface) {
  60. ERR_FAIL_COND(p_interface.is_null());
  61. ERR_FAIL_COND_MSG(p_interface == primary_interface, "TextServer: Can't remove primary interface.");
  62. int idx = -1;
  63. for (int i = 0; i < interfaces.size(); i++) {
  64. if (interfaces[i] == p_interface) {
  65. idx = i;
  66. break;
  67. };
  68. };
  69. ERR_FAIL_COND_MSG(idx == -1, "Interface not found.");
  70. print_verbose("TextServer: Removed interface \"" + p_interface->get_name() + "\"");
  71. emit_signal(SNAME("interface_removed"), p_interface->get_name());
  72. interfaces.remove_at(idx);
  73. }
  74. int TextServerManager::get_interface_count() const {
  75. return interfaces.size();
  76. }
  77. Ref<TextServer> TextServerManager::get_interface(int p_index) const {
  78. ERR_FAIL_INDEX_V(p_index, interfaces.size(), nullptr);
  79. return interfaces[p_index];
  80. }
  81. Ref<TextServer> TextServerManager::find_interface(const String &p_name) const {
  82. int idx = -1;
  83. for (int i = 0; i < interfaces.size(); i++) {
  84. if (interfaces[i]->get_name() == p_name) {
  85. idx = i;
  86. break;
  87. };
  88. };
  89. ERR_FAIL_COND_V_MSG(idx == -1, nullptr, "Interface not found.");
  90. return interfaces[idx];
  91. }
  92. TypedArray<Dictionary> TextServerManager::get_interfaces() const {
  93. TypedArray<Dictionary> ret;
  94. for (int i = 0; i < interfaces.size(); i++) {
  95. Dictionary iface_info;
  96. iface_info["id"] = i;
  97. iface_info["name"] = interfaces[i]->get_name();
  98. ret.push_back(iface_info);
  99. };
  100. return ret;
  101. }
  102. void TextServerManager::set_primary_interface(const Ref<TextServer> &p_primary_interface) {
  103. if (p_primary_interface.is_null()) {
  104. print_verbose("TextServer: Clearing primary interface");
  105. primary_interface.unref();
  106. } else {
  107. primary_interface = p_primary_interface;
  108. print_verbose("TextServer: Primary interface set to: \"" + primary_interface->get_name() + "\".");
  109. if (OS::get_singleton()->get_main_loop()) {
  110. OS::get_singleton()->get_main_loop()->notification(MainLoop::NOTIFICATION_TEXT_SERVER_CHANGED);
  111. }
  112. }
  113. }
  114. TextServerManager::TextServerManager() {
  115. singleton = this;
  116. }
  117. TextServerManager::~TextServerManager() {
  118. if (primary_interface.is_valid()) {
  119. primary_interface.unref();
  120. }
  121. while (interfaces.size() > 0) {
  122. interfaces.remove_at(0);
  123. }
  124. singleton = nullptr;
  125. }
  126. /*************************************************************************/
  127. bool Glyph::operator==(const Glyph &p_a) const {
  128. return (p_a.index == index) && (p_a.font_rid == font_rid) && (p_a.font_size == font_size) && (p_a.start == start);
  129. }
  130. bool Glyph::operator!=(const Glyph &p_a) const {
  131. return (p_a.index != index) || (p_a.font_rid != font_rid) || (p_a.font_size != font_size) || (p_a.start != start);
  132. }
  133. bool Glyph::operator<(const Glyph &p_a) const {
  134. if (p_a.start == start) {
  135. if (p_a.count == count) {
  136. if ((p_a.flags & TextServer::GRAPHEME_IS_VIRTUAL) == TextServer::GRAPHEME_IS_VIRTUAL) {
  137. return true;
  138. } else {
  139. return false;
  140. }
  141. }
  142. return p_a.count > count;
  143. }
  144. return p_a.start < start;
  145. }
  146. bool Glyph::operator>(const Glyph &p_a) const {
  147. if (p_a.start == start) {
  148. if (p_a.count == count) {
  149. if ((p_a.flags & TextServer::GRAPHEME_IS_VIRTUAL) == TextServer::GRAPHEME_IS_VIRTUAL) {
  150. return false;
  151. } else {
  152. return true;
  153. }
  154. }
  155. return p_a.count < count;
  156. }
  157. return p_a.start > start;
  158. }
  159. void TextServer::_bind_methods() {
  160. ClassDB::bind_method(D_METHOD("has_feature", "feature"), &TextServer::has_feature);
  161. ClassDB::bind_method(D_METHOD("get_name"), &TextServer::get_name);
  162. ClassDB::bind_method(D_METHOD("get_features"), &TextServer::get_features);
  163. ClassDB::bind_method(D_METHOD("load_support_data", "filename"), &TextServer::load_support_data);
  164. ClassDB::bind_method(D_METHOD("get_support_data_filename"), &TextServer::get_support_data_filename);
  165. ClassDB::bind_method(D_METHOD("get_support_data_info"), &TextServer::get_support_data_info);
  166. ClassDB::bind_method(D_METHOD("save_support_data", "filename"), &TextServer::save_support_data);
  167. ClassDB::bind_method(D_METHOD("get_support_data"), &TextServer::get_support_data);
  168. ClassDB::bind_method(D_METHOD("is_locale_right_to_left", "locale"), &TextServer::is_locale_right_to_left);
  169. ClassDB::bind_method(D_METHOD("name_to_tag", "name"), &TextServer::name_to_tag);
  170. ClassDB::bind_method(D_METHOD("tag_to_name", "tag"), &TextServer::tag_to_name);
  171. ClassDB::bind_method(D_METHOD("has", "rid"), &TextServer::has);
  172. ClassDB::bind_method(D_METHOD("free_rid", "rid"), &TextServer::free_rid);
  173. /* Font Interface */
  174. ClassDB::bind_method(D_METHOD("create_font"), &TextServer::create_font);
  175. ClassDB::bind_method(D_METHOD("create_font_linked_variation", "font_rid"), &TextServer::create_font_linked_variation);
  176. ClassDB::bind_method(D_METHOD("font_set_data", "font_rid", "data"), &TextServer::font_set_data);
  177. ClassDB::bind_method(D_METHOD("font_set_face_index", "font_rid", "face_index"), &TextServer::font_set_face_index);
  178. ClassDB::bind_method(D_METHOD("font_get_face_index", "font_rid"), &TextServer::font_get_face_index);
  179. ClassDB::bind_method(D_METHOD("font_get_face_count", "font_rid"), &TextServer::font_get_face_count);
  180. ClassDB::bind_method(D_METHOD("font_set_style", "font_rid", "style"), &TextServer::font_set_style);
  181. ClassDB::bind_method(D_METHOD("font_get_style", "font_rid"), &TextServer::font_get_style);
  182. ClassDB::bind_method(D_METHOD("font_set_name", "font_rid", "name"), &TextServer::font_set_name);
  183. ClassDB::bind_method(D_METHOD("font_get_name", "font_rid"), &TextServer::font_get_name);
  184. ClassDB::bind_method(D_METHOD("font_get_ot_name_strings", "font_rid"), &TextServer::font_get_ot_name_strings);
  185. ClassDB::bind_method(D_METHOD("font_set_style_name", "font_rid", "name"), &TextServer::font_set_style_name);
  186. ClassDB::bind_method(D_METHOD("font_get_style_name", "font_rid"), &TextServer::font_get_style_name);
  187. ClassDB::bind_method(D_METHOD("font_set_weight", "font_rid", "weight"), &TextServer::font_set_weight);
  188. ClassDB::bind_method(D_METHOD("font_get_weight", "font_rid"), &TextServer::font_get_weight);
  189. ClassDB::bind_method(D_METHOD("font_set_stretch", "font_rid", "weight"), &TextServer::font_set_stretch);
  190. ClassDB::bind_method(D_METHOD("font_get_stretch", "font_rid"), &TextServer::font_get_stretch);
  191. ClassDB::bind_method(D_METHOD("font_set_antialiasing", "font_rid", "antialiasing"), &TextServer::font_set_antialiasing);
  192. ClassDB::bind_method(D_METHOD("font_get_antialiasing", "font_rid"), &TextServer::font_get_antialiasing);
  193. ClassDB::bind_method(D_METHOD("font_set_disable_embedded_bitmaps", "font_rid", "disable_embedded_bitmaps"), &TextServer::font_set_disable_embedded_bitmaps);
  194. ClassDB::bind_method(D_METHOD("font_get_disable_embedded_bitmaps", "font_rid"), &TextServer::font_get_disable_embedded_bitmaps);
  195. ClassDB::bind_method(D_METHOD("font_set_generate_mipmaps", "font_rid", "generate_mipmaps"), &TextServer::font_set_generate_mipmaps);
  196. ClassDB::bind_method(D_METHOD("font_get_generate_mipmaps", "font_rid"), &TextServer::font_get_generate_mipmaps);
  197. ClassDB::bind_method(D_METHOD("font_set_multichannel_signed_distance_field", "font_rid", "msdf"), &TextServer::font_set_multichannel_signed_distance_field);
  198. ClassDB::bind_method(D_METHOD("font_is_multichannel_signed_distance_field", "font_rid"), &TextServer::font_is_multichannel_signed_distance_field);
  199. ClassDB::bind_method(D_METHOD("font_set_msdf_pixel_range", "font_rid", "msdf_pixel_range"), &TextServer::font_set_msdf_pixel_range);
  200. ClassDB::bind_method(D_METHOD("font_get_msdf_pixel_range", "font_rid"), &TextServer::font_get_msdf_pixel_range);
  201. ClassDB::bind_method(D_METHOD("font_set_msdf_size", "font_rid", "msdf_size"), &TextServer::font_set_msdf_size);
  202. ClassDB::bind_method(D_METHOD("font_get_msdf_size", "font_rid"), &TextServer::font_get_msdf_size);
  203. ClassDB::bind_method(D_METHOD("font_set_fixed_size", "font_rid", "fixed_size"), &TextServer::font_set_fixed_size);
  204. ClassDB::bind_method(D_METHOD("font_get_fixed_size", "font_rid"), &TextServer::font_get_fixed_size);
  205. ClassDB::bind_method(D_METHOD("font_set_fixed_size_scale_mode", "font_rid", "fixed_size_scale_mode"), &TextServer::font_set_fixed_size_scale_mode);
  206. ClassDB::bind_method(D_METHOD("font_get_fixed_size_scale_mode", "font_rid"), &TextServer::font_get_fixed_size_scale_mode);
  207. ClassDB::bind_method(D_METHOD("font_set_allow_system_fallback", "font_rid", "allow_system_fallback"), &TextServer::font_set_allow_system_fallback);
  208. ClassDB::bind_method(D_METHOD("font_is_allow_system_fallback", "font_rid"), &TextServer::font_is_allow_system_fallback);
  209. ClassDB::bind_method(D_METHOD("font_set_force_autohinter", "font_rid", "force_autohinter"), &TextServer::font_set_force_autohinter);
  210. ClassDB::bind_method(D_METHOD("font_is_force_autohinter", "font_rid"), &TextServer::font_is_force_autohinter);
  211. ClassDB::bind_method(D_METHOD("font_set_hinting", "font_rid", "hinting"), &TextServer::font_set_hinting);
  212. ClassDB::bind_method(D_METHOD("font_get_hinting", "font_rid"), &TextServer::font_get_hinting);
  213. ClassDB::bind_method(D_METHOD("font_set_subpixel_positioning", "font_rid", "subpixel_positioning"), &TextServer::font_set_subpixel_positioning);
  214. ClassDB::bind_method(D_METHOD("font_get_subpixel_positioning", "font_rid"), &TextServer::font_get_subpixel_positioning);
  215. ClassDB::bind_method(D_METHOD("font_set_embolden", "font_rid", "strength"), &TextServer::font_set_embolden);
  216. ClassDB::bind_method(D_METHOD("font_get_embolden", "font_rid"), &TextServer::font_get_embolden);
  217. ClassDB::bind_method(D_METHOD("font_set_spacing", "font_rid", "spacing", "value"), &TextServer::font_set_spacing);
  218. ClassDB::bind_method(D_METHOD("font_get_spacing", "font_rid", "spacing"), &TextServer::font_get_spacing);
  219. ClassDB::bind_method(D_METHOD("font_set_baseline_offset", "font_rid", "baseline_offset"), &TextServer::font_set_baseline_offset);
  220. ClassDB::bind_method(D_METHOD("font_get_baseline_offset", "font_rid"), &TextServer::font_get_baseline_offset);
  221. ClassDB::bind_method(D_METHOD("font_set_transform", "font_rid", "transform"), &TextServer::font_set_transform);
  222. ClassDB::bind_method(D_METHOD("font_get_transform", "font_rid"), &TextServer::font_get_transform);
  223. ClassDB::bind_method(D_METHOD("font_set_variation_coordinates", "font_rid", "variation_coordinates"), &TextServer::font_set_variation_coordinates);
  224. ClassDB::bind_method(D_METHOD("font_get_variation_coordinates", "font_rid"), &TextServer::font_get_variation_coordinates);
  225. ClassDB::bind_method(D_METHOD("font_set_oversampling", "font_rid", "oversampling"), &TextServer::font_set_oversampling);
  226. ClassDB::bind_method(D_METHOD("font_get_oversampling", "font_rid"), &TextServer::font_get_oversampling);
  227. ClassDB::bind_method(D_METHOD("font_get_size_cache_list", "font_rid"), &TextServer::font_get_size_cache_list);
  228. ClassDB::bind_method(D_METHOD("font_clear_size_cache", "font_rid"), &TextServer::font_clear_size_cache);
  229. ClassDB::bind_method(D_METHOD("font_remove_size_cache", "font_rid", "size"), &TextServer::font_remove_size_cache);
  230. ClassDB::bind_method(D_METHOD("font_set_ascent", "font_rid", "size", "ascent"), &TextServer::font_set_ascent);
  231. ClassDB::bind_method(D_METHOD("font_get_ascent", "font_rid", "size"), &TextServer::font_get_ascent);
  232. ClassDB::bind_method(D_METHOD("font_set_descent", "font_rid", "size", "descent"), &TextServer::font_set_descent);
  233. ClassDB::bind_method(D_METHOD("font_get_descent", "font_rid", "size"), &TextServer::font_get_descent);
  234. ClassDB::bind_method(D_METHOD("font_set_underline_position", "font_rid", "size", "underline_position"), &TextServer::font_set_underline_position);
  235. ClassDB::bind_method(D_METHOD("font_get_underline_position", "font_rid", "size"), &TextServer::font_get_underline_position);
  236. ClassDB::bind_method(D_METHOD("font_set_underline_thickness", "font_rid", "size", "underline_thickness"), &TextServer::font_set_underline_thickness);
  237. ClassDB::bind_method(D_METHOD("font_get_underline_thickness", "font_rid", "size"), &TextServer::font_get_underline_thickness);
  238. ClassDB::bind_method(D_METHOD("font_set_scale", "font_rid", "size", "scale"), &TextServer::font_set_scale);
  239. ClassDB::bind_method(D_METHOD("font_get_scale", "font_rid", "size"), &TextServer::font_get_scale);
  240. ClassDB::bind_method(D_METHOD("font_get_texture_count", "font_rid", "size"), &TextServer::font_get_texture_count);
  241. ClassDB::bind_method(D_METHOD("font_clear_textures", "font_rid", "size"), &TextServer::font_clear_textures);
  242. ClassDB::bind_method(D_METHOD("font_remove_texture", "font_rid", "size", "texture_index"), &TextServer::font_remove_texture);
  243. ClassDB::bind_method(D_METHOD("font_set_texture_image", "font_rid", "size", "texture_index", "image"), &TextServer::font_set_texture_image);
  244. ClassDB::bind_method(D_METHOD("font_get_texture_image", "font_rid", "size", "texture_index"), &TextServer::font_get_texture_image);
  245. ClassDB::bind_method(D_METHOD("font_set_texture_offsets", "font_rid", "size", "texture_index", "offset"), &TextServer::font_set_texture_offsets);
  246. ClassDB::bind_method(D_METHOD("font_get_texture_offsets", "font_rid", "size", "texture_index"), &TextServer::font_get_texture_offsets);
  247. ClassDB::bind_method(D_METHOD("font_get_glyph_list", "font_rid", "size"), &TextServer::font_get_glyph_list);
  248. ClassDB::bind_method(D_METHOD("font_clear_glyphs", "font_rid", "size"), &TextServer::font_clear_glyphs);
  249. ClassDB::bind_method(D_METHOD("font_remove_glyph", "font_rid", "size", "glyph"), &TextServer::font_remove_glyph);
  250. ClassDB::bind_method(D_METHOD("font_get_glyph_advance", "font_rid", "size", "glyph"), &TextServer::font_get_glyph_advance);
  251. ClassDB::bind_method(D_METHOD("font_set_glyph_advance", "font_rid", "size", "glyph", "advance"), &TextServer::font_set_glyph_advance);
  252. ClassDB::bind_method(D_METHOD("font_get_glyph_offset", "font_rid", "size", "glyph"), &TextServer::font_get_glyph_offset);
  253. ClassDB::bind_method(D_METHOD("font_set_glyph_offset", "font_rid", "size", "glyph", "offset"), &TextServer::font_set_glyph_offset);
  254. ClassDB::bind_method(D_METHOD("font_get_glyph_size", "font_rid", "size", "glyph"), &TextServer::font_get_glyph_size);
  255. ClassDB::bind_method(D_METHOD("font_set_glyph_size", "font_rid", "size", "glyph", "gl_size"), &TextServer::font_set_glyph_size);
  256. ClassDB::bind_method(D_METHOD("font_get_glyph_uv_rect", "font_rid", "size", "glyph"), &TextServer::font_get_glyph_uv_rect);
  257. ClassDB::bind_method(D_METHOD("font_set_glyph_uv_rect", "font_rid", "size", "glyph", "uv_rect"), &TextServer::font_set_glyph_uv_rect);
  258. ClassDB::bind_method(D_METHOD("font_get_glyph_texture_idx", "font_rid", "size", "glyph"), &TextServer::font_get_glyph_texture_idx);
  259. ClassDB::bind_method(D_METHOD("font_set_glyph_texture_idx", "font_rid", "size", "glyph", "texture_idx"), &TextServer::font_set_glyph_texture_idx);
  260. ClassDB::bind_method(D_METHOD("font_get_glyph_texture_rid", "font_rid", "size", "glyph"), &TextServer::font_get_glyph_texture_rid);
  261. ClassDB::bind_method(D_METHOD("font_get_glyph_texture_size", "font_rid", "size", "glyph"), &TextServer::font_get_glyph_texture_size);
  262. ClassDB::bind_method(D_METHOD("font_get_glyph_contours", "font", "size", "index"), &TextServer::font_get_glyph_contours);
  263. ClassDB::bind_method(D_METHOD("font_get_kerning_list", "font_rid", "size"), &TextServer::font_get_kerning_list);
  264. ClassDB::bind_method(D_METHOD("font_clear_kerning_map", "font_rid", "size"), &TextServer::font_clear_kerning_map);
  265. ClassDB::bind_method(D_METHOD("font_remove_kerning", "font_rid", "size", "glyph_pair"), &TextServer::font_remove_kerning);
  266. ClassDB::bind_method(D_METHOD("font_set_kerning", "font_rid", "size", "glyph_pair", "kerning"), &TextServer::font_set_kerning);
  267. ClassDB::bind_method(D_METHOD("font_get_kerning", "font_rid", "size", "glyph_pair"), &TextServer::font_get_kerning);
  268. ClassDB::bind_method(D_METHOD("font_get_glyph_index", "font_rid", "size", "char", "variation_selector"), &TextServer::font_get_glyph_index);
  269. ClassDB::bind_method(D_METHOD("font_get_char_from_glyph_index", "font_rid", "size", "glyph_index"), &TextServer::font_get_char_from_glyph_index);
  270. ClassDB::bind_method(D_METHOD("font_has_char", "font_rid", "char"), &TextServer::font_has_char);
  271. ClassDB::bind_method(D_METHOD("font_get_supported_chars", "font_rid"), &TextServer::font_get_supported_chars);
  272. ClassDB::bind_method(D_METHOD("font_get_supported_glyphs", "font_rid"), &TextServer::font_get_supported_glyphs);
  273. ClassDB::bind_method(D_METHOD("font_render_range", "font_rid", "size", "start", "end"), &TextServer::font_render_range);
  274. ClassDB::bind_method(D_METHOD("font_render_glyph", "font_rid", "size", "index"), &TextServer::font_render_glyph);
  275. ClassDB::bind_method(D_METHOD("font_draw_glyph", "font_rid", "canvas", "size", "pos", "index", "color"), &TextServer::font_draw_glyph, DEFVAL(Color(1, 1, 1)));
  276. ClassDB::bind_method(D_METHOD("font_draw_glyph_outline", "font_rid", "canvas", "size", "outline_size", "pos", "index", "color"), &TextServer::font_draw_glyph_outline, DEFVAL(Color(1, 1, 1)));
  277. ClassDB::bind_method(D_METHOD("font_is_language_supported", "font_rid", "language"), &TextServer::font_is_language_supported);
  278. ClassDB::bind_method(D_METHOD("font_set_language_support_override", "font_rid", "language", "supported"), &TextServer::font_set_language_support_override);
  279. ClassDB::bind_method(D_METHOD("font_get_language_support_override", "font_rid", "language"), &TextServer::font_get_language_support_override);
  280. ClassDB::bind_method(D_METHOD("font_remove_language_support_override", "font_rid", "language"), &TextServer::font_remove_language_support_override);
  281. ClassDB::bind_method(D_METHOD("font_get_language_support_overrides", "font_rid"), &TextServer::font_get_language_support_overrides);
  282. ClassDB::bind_method(D_METHOD("font_is_script_supported", "font_rid", "script"), &TextServer::font_is_script_supported);
  283. ClassDB::bind_method(D_METHOD("font_set_script_support_override", "font_rid", "script", "supported"), &TextServer::font_set_script_support_override);
  284. ClassDB::bind_method(D_METHOD("font_get_script_support_override", "font_rid", "script"), &TextServer::font_get_script_support_override);
  285. ClassDB::bind_method(D_METHOD("font_remove_script_support_override", "font_rid", "script"), &TextServer::font_remove_script_support_override);
  286. ClassDB::bind_method(D_METHOD("font_get_script_support_overrides", "font_rid"), &TextServer::font_get_script_support_overrides);
  287. ClassDB::bind_method(D_METHOD("font_set_opentype_feature_overrides", "font_rid", "overrides"), &TextServer::font_set_opentype_feature_overrides);
  288. ClassDB::bind_method(D_METHOD("font_get_opentype_feature_overrides", "font_rid"), &TextServer::font_get_opentype_feature_overrides);
  289. ClassDB::bind_method(D_METHOD("font_supported_feature_list", "font_rid"), &TextServer::font_supported_feature_list);
  290. ClassDB::bind_method(D_METHOD("font_supported_variation_list", "font_rid"), &TextServer::font_supported_variation_list);
  291. ClassDB::bind_method(D_METHOD("font_get_global_oversampling"), &TextServer::font_get_global_oversampling);
  292. ClassDB::bind_method(D_METHOD("font_set_global_oversampling", "oversampling"), &TextServer::font_set_global_oversampling);
  293. ClassDB::bind_method(D_METHOD("get_hex_code_box_size", "size", "index"), &TextServer::get_hex_code_box_size);
  294. ClassDB::bind_method(D_METHOD("draw_hex_code_box", "canvas", "size", "pos", "index", "color"), &TextServer::draw_hex_code_box);
  295. /* Shaped text buffer interface */
  296. ClassDB::bind_method(D_METHOD("create_shaped_text", "direction", "orientation"), &TextServer::create_shaped_text, DEFVAL(DIRECTION_AUTO), DEFVAL(ORIENTATION_HORIZONTAL));
  297. ClassDB::bind_method(D_METHOD("shaped_text_clear", "rid"), &TextServer::shaped_text_clear);
  298. ClassDB::bind_method(D_METHOD("shaped_text_set_direction", "shaped", "direction"), &TextServer::shaped_text_set_direction, DEFVAL(DIRECTION_AUTO));
  299. ClassDB::bind_method(D_METHOD("shaped_text_get_direction", "shaped"), &TextServer::shaped_text_get_direction);
  300. ClassDB::bind_method(D_METHOD("shaped_text_get_inferred_direction", "shaped"), &TextServer::shaped_text_get_inferred_direction);
  301. ClassDB::bind_method(D_METHOD("shaped_text_set_bidi_override", "shaped", "override"), &TextServer::shaped_text_set_bidi_override);
  302. ClassDB::bind_method(D_METHOD("shaped_text_set_custom_punctuation", "shaped", "punct"), &TextServer::shaped_text_set_custom_punctuation);
  303. ClassDB::bind_method(D_METHOD("shaped_text_get_custom_punctuation", "shaped"), &TextServer::shaped_text_get_custom_punctuation);
  304. ClassDB::bind_method(D_METHOD("shaped_text_set_custom_ellipsis", "shaped", "char"), &TextServer::shaped_text_set_custom_ellipsis);
  305. ClassDB::bind_method(D_METHOD("shaped_text_get_custom_ellipsis", "shaped"), &TextServer::shaped_text_get_custom_ellipsis);
  306. ClassDB::bind_method(D_METHOD("shaped_text_set_orientation", "shaped", "orientation"), &TextServer::shaped_text_set_orientation, DEFVAL(ORIENTATION_HORIZONTAL));
  307. ClassDB::bind_method(D_METHOD("shaped_text_get_orientation", "shaped"), &TextServer::shaped_text_get_orientation);
  308. ClassDB::bind_method(D_METHOD("shaped_text_set_preserve_invalid", "shaped", "enabled"), &TextServer::shaped_text_set_preserve_invalid);
  309. ClassDB::bind_method(D_METHOD("shaped_text_get_preserve_invalid", "shaped"), &TextServer::shaped_text_get_preserve_invalid);
  310. ClassDB::bind_method(D_METHOD("shaped_text_set_preserve_control", "shaped", "enabled"), &TextServer::shaped_text_set_preserve_control);
  311. ClassDB::bind_method(D_METHOD("shaped_text_get_preserve_control", "shaped"), &TextServer::shaped_text_get_preserve_control);
  312. ClassDB::bind_method(D_METHOD("shaped_text_set_spacing", "shaped", "spacing", "value"), &TextServer::shaped_text_set_spacing);
  313. ClassDB::bind_method(D_METHOD("shaped_text_get_spacing", "shaped", "spacing"), &TextServer::shaped_text_get_spacing);
  314. ClassDB::bind_method(D_METHOD("shaped_text_add_string", "shaped", "text", "fonts", "size", "opentype_features", "language", "meta"), &TextServer::shaped_text_add_string, DEFVAL(Dictionary()), DEFVAL(""), DEFVAL(Variant()));
  315. ClassDB::bind_method(D_METHOD("shaped_text_add_object", "shaped", "key", "size", "inline_align", "length", "baseline"), &TextServer::shaped_text_add_object, DEFVAL(INLINE_ALIGNMENT_CENTER), DEFVAL(1), DEFVAL(0.0));
  316. ClassDB::bind_method(D_METHOD("shaped_text_resize_object", "shaped", "key", "size", "inline_align", "baseline"), &TextServer::shaped_text_resize_object, DEFVAL(INLINE_ALIGNMENT_CENTER), DEFVAL(0.0));
  317. ClassDB::bind_method(D_METHOD("shaped_get_span_count", "shaped"), &TextServer::shaped_get_span_count);
  318. ClassDB::bind_method(D_METHOD("shaped_get_span_meta", "shaped", "index"), &TextServer::shaped_get_span_meta);
  319. ClassDB::bind_method(D_METHOD("shaped_set_span_update_font", "shaped", "index", "fonts", "size", "opentype_features"), &TextServer::shaped_set_span_update_font, DEFVAL(Dictionary()));
  320. ClassDB::bind_method(D_METHOD("shaped_text_substr", "shaped", "start", "length"), &TextServer::shaped_text_substr);
  321. ClassDB::bind_method(D_METHOD("shaped_text_get_parent", "shaped"), &TextServer::shaped_text_get_parent);
  322. ClassDB::bind_method(D_METHOD("shaped_text_fit_to_width", "shaped", "width", "justification_flags"), &TextServer::shaped_text_fit_to_width, DEFVAL(JUSTIFICATION_WORD_BOUND | JUSTIFICATION_KASHIDA));
  323. ClassDB::bind_method(D_METHOD("shaped_text_tab_align", "shaped", "tab_stops"), &TextServer::shaped_text_tab_align);
  324. ClassDB::bind_method(D_METHOD("shaped_text_shape", "shaped"), &TextServer::shaped_text_shape);
  325. ClassDB::bind_method(D_METHOD("shaped_text_is_ready", "shaped"), &TextServer::shaped_text_is_ready);
  326. ClassDB::bind_method(D_METHOD("shaped_text_has_visible_chars", "shaped"), &TextServer::shaped_text_has_visible_chars);
  327. ClassDB::bind_method(D_METHOD("shaped_text_get_glyphs", "shaped"), &TextServer::_shaped_text_get_glyphs_wrapper);
  328. ClassDB::bind_method(D_METHOD("shaped_text_sort_logical", "shaped"), &TextServer::_shaped_text_sort_logical_wrapper);
  329. ClassDB::bind_method(D_METHOD("shaped_text_get_glyph_count", "shaped"), &TextServer::shaped_text_get_glyph_count);
  330. ClassDB::bind_method(D_METHOD("shaped_text_get_range", "shaped"), &TextServer::shaped_text_get_range);
  331. 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));
  332. 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));
  333. ClassDB::bind_method(D_METHOD("shaped_text_get_word_breaks", "shaped", "grapheme_flags", "skip_grapheme_flags"), &TextServer::shaped_text_get_word_breaks, DEFVAL(GRAPHEME_IS_SPACE | GRAPHEME_IS_PUNCTUATION), DEFVAL(GRAPHEME_IS_VIRTUAL));
  334. ClassDB::bind_method(D_METHOD("shaped_text_get_trim_pos", "shaped"), &TextServer::shaped_text_get_trim_pos);
  335. ClassDB::bind_method(D_METHOD("shaped_text_get_ellipsis_pos", "shaped"), &TextServer::shaped_text_get_ellipsis_pos);
  336. ClassDB::bind_method(D_METHOD("shaped_text_get_ellipsis_glyphs", "shaped"), &TextServer::_shaped_text_get_ellipsis_glyphs_wrapper);
  337. ClassDB::bind_method(D_METHOD("shaped_text_get_ellipsis_glyph_count", "shaped"), &TextServer::shaped_text_get_ellipsis_glyph_count);
  338. ClassDB::bind_method(D_METHOD("shaped_text_overrun_trim_to_width", "shaped", "width", "overrun_trim_flags"), &TextServer::shaped_text_overrun_trim_to_width, DEFVAL(0), DEFVAL(OVERRUN_NO_TRIM));
  339. ClassDB::bind_method(D_METHOD("shaped_text_get_objects", "shaped"), &TextServer::shaped_text_get_objects);
  340. ClassDB::bind_method(D_METHOD("shaped_text_get_object_rect", "shaped", "key"), &TextServer::shaped_text_get_object_rect);
  341. ClassDB::bind_method(D_METHOD("shaped_text_get_object_range", "shaped", "key"), &TextServer::shaped_text_get_object_range);
  342. ClassDB::bind_method(D_METHOD("shaped_text_get_object_glyph", "shaped", "key"), &TextServer::shaped_text_get_object_glyph);
  343. ClassDB::bind_method(D_METHOD("shaped_text_get_size", "shaped"), &TextServer::shaped_text_get_size);
  344. ClassDB::bind_method(D_METHOD("shaped_text_get_ascent", "shaped"), &TextServer::shaped_text_get_ascent);
  345. ClassDB::bind_method(D_METHOD("shaped_text_get_descent", "shaped"), &TextServer::shaped_text_get_descent);
  346. ClassDB::bind_method(D_METHOD("shaped_text_get_width", "shaped"), &TextServer::shaped_text_get_width);
  347. ClassDB::bind_method(D_METHOD("shaped_text_get_underline_position", "shaped"), &TextServer::shaped_text_get_underline_position);
  348. ClassDB::bind_method(D_METHOD("shaped_text_get_underline_thickness", "shaped"), &TextServer::shaped_text_get_underline_thickness);
  349. ClassDB::bind_method(D_METHOD("shaped_text_get_carets", "shaped", "position"), &TextServer::_shaped_text_get_carets_wrapper);
  350. ClassDB::bind_method(D_METHOD("shaped_text_get_selection", "shaped", "start", "end"), &TextServer::shaped_text_get_selection);
  351. ClassDB::bind_method(D_METHOD("shaped_text_hit_test_grapheme", "shaped", "coords"), &TextServer::shaped_text_hit_test_grapheme);
  352. ClassDB::bind_method(D_METHOD("shaped_text_hit_test_position", "shaped", "coords"), &TextServer::shaped_text_hit_test_position);
  353. ClassDB::bind_method(D_METHOD("shaped_text_get_grapheme_bounds", "shaped", "pos"), &TextServer::shaped_text_get_grapheme_bounds);
  354. ClassDB::bind_method(D_METHOD("shaped_text_next_grapheme_pos", "shaped", "pos"), &TextServer::shaped_text_next_grapheme_pos);
  355. ClassDB::bind_method(D_METHOD("shaped_text_prev_grapheme_pos", "shaped", "pos"), &TextServer::shaped_text_prev_grapheme_pos);
  356. ClassDB::bind_method(D_METHOD("shaped_text_get_character_breaks", "shaped"), &TextServer::shaped_text_get_character_breaks);
  357. ClassDB::bind_method(D_METHOD("shaped_text_next_character_pos", "shaped", "pos"), &TextServer::shaped_text_next_character_pos);
  358. ClassDB::bind_method(D_METHOD("shaped_text_prev_character_pos", "shaped", "pos"), &TextServer::shaped_text_prev_character_pos);
  359. ClassDB::bind_method(D_METHOD("shaped_text_closest_character_pos", "shaped", "pos"), &TextServer::shaped_text_closest_character_pos);
  360. 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)));
  361. 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)));
  362. ClassDB::bind_method(D_METHOD("shaped_text_get_dominant_direction_in_range", "shaped", "start", "end"), &TextServer::shaped_text_get_dominant_direction_in_range);
  363. ClassDB::bind_method(D_METHOD("format_number", "number", "language"), &TextServer::format_number, DEFVAL(""));
  364. ClassDB::bind_method(D_METHOD("parse_number", "number", "language"), &TextServer::parse_number, DEFVAL(""));
  365. ClassDB::bind_method(D_METHOD("percent_sign", "language"), &TextServer::percent_sign, DEFVAL(""));
  366. ClassDB::bind_method(D_METHOD("string_get_word_breaks", "string", "language", "chars_per_line"), &TextServer::string_get_word_breaks, DEFVAL(""), DEFVAL(0));
  367. ClassDB::bind_method(D_METHOD("string_get_character_breaks", "string", "language"), &TextServer::string_get_character_breaks, DEFVAL(""));
  368. ClassDB::bind_method(D_METHOD("is_confusable", "string", "dict"), &TextServer::is_confusable);
  369. ClassDB::bind_method(D_METHOD("spoof_check", "string"), &TextServer::spoof_check);
  370. ClassDB::bind_method(D_METHOD("strip_diacritics", "string"), &TextServer::strip_diacritics);
  371. ClassDB::bind_method(D_METHOD("is_valid_identifier", "string"), &TextServer::is_valid_identifier);
  372. ClassDB::bind_method(D_METHOD("is_valid_letter", "unicode"), &TextServer::is_valid_letter);
  373. ClassDB::bind_method(D_METHOD("string_to_upper", "string", "language"), &TextServer::string_to_upper, DEFVAL(""));
  374. ClassDB::bind_method(D_METHOD("string_to_lower", "string", "language"), &TextServer::string_to_lower, DEFVAL(""));
  375. ClassDB::bind_method(D_METHOD("string_to_title", "string", "language"), &TextServer::string_to_title, DEFVAL(""));
  376. ClassDB::bind_method(D_METHOD("parse_structured_text", "parser_type", "args", "text"), &TextServer::parse_structured_text);
  377. /* Font AA */
  378. BIND_ENUM_CONSTANT(FONT_ANTIALIASING_NONE);
  379. BIND_ENUM_CONSTANT(FONT_ANTIALIASING_GRAY);
  380. BIND_ENUM_CONSTANT(FONT_ANTIALIASING_LCD);
  381. BIND_ENUM_CONSTANT(FONT_LCD_SUBPIXEL_LAYOUT_NONE);
  382. BIND_ENUM_CONSTANT(FONT_LCD_SUBPIXEL_LAYOUT_HRGB);
  383. BIND_ENUM_CONSTANT(FONT_LCD_SUBPIXEL_LAYOUT_HBGR);
  384. BIND_ENUM_CONSTANT(FONT_LCD_SUBPIXEL_LAYOUT_VRGB);
  385. BIND_ENUM_CONSTANT(FONT_LCD_SUBPIXEL_LAYOUT_VBGR);
  386. BIND_ENUM_CONSTANT(FONT_LCD_SUBPIXEL_LAYOUT_MAX);
  387. /* Direction */
  388. BIND_ENUM_CONSTANT(DIRECTION_AUTO);
  389. BIND_ENUM_CONSTANT(DIRECTION_LTR);
  390. BIND_ENUM_CONSTANT(DIRECTION_RTL);
  391. BIND_ENUM_CONSTANT(DIRECTION_INHERITED);
  392. /* Orientation */
  393. BIND_ENUM_CONSTANT(ORIENTATION_HORIZONTAL);
  394. BIND_ENUM_CONSTANT(ORIENTATION_VERTICAL);
  395. /* JustificationFlag */
  396. BIND_BITFIELD_FLAG(JUSTIFICATION_NONE);
  397. BIND_BITFIELD_FLAG(JUSTIFICATION_KASHIDA);
  398. BIND_BITFIELD_FLAG(JUSTIFICATION_WORD_BOUND);
  399. BIND_BITFIELD_FLAG(JUSTIFICATION_TRIM_EDGE_SPACES);
  400. BIND_BITFIELD_FLAG(JUSTIFICATION_AFTER_LAST_TAB);
  401. BIND_BITFIELD_FLAG(JUSTIFICATION_CONSTRAIN_ELLIPSIS);
  402. BIND_BITFIELD_FLAG(JUSTIFICATION_SKIP_LAST_LINE);
  403. BIND_BITFIELD_FLAG(JUSTIFICATION_SKIP_LAST_LINE_WITH_VISIBLE_CHARS);
  404. BIND_BITFIELD_FLAG(JUSTIFICATION_DO_NOT_SKIP_SINGLE_LINE);
  405. /* AutowrapMode */
  406. BIND_ENUM_CONSTANT(AUTOWRAP_OFF);
  407. BIND_ENUM_CONSTANT(AUTOWRAP_ARBITRARY);
  408. BIND_ENUM_CONSTANT(AUTOWRAP_WORD);
  409. BIND_ENUM_CONSTANT(AUTOWRAP_WORD_SMART);
  410. /* LineBreakFlag */
  411. BIND_BITFIELD_FLAG(BREAK_NONE);
  412. BIND_BITFIELD_FLAG(BREAK_MANDATORY);
  413. BIND_BITFIELD_FLAG(BREAK_WORD_BOUND);
  414. BIND_BITFIELD_FLAG(BREAK_GRAPHEME_BOUND);
  415. BIND_BITFIELD_FLAG(BREAK_ADAPTIVE);
  416. BIND_BITFIELD_FLAG(BREAK_TRIM_EDGE_SPACES);
  417. BIND_BITFIELD_FLAG(BREAK_TRIM_INDENT);
  418. /* VisibleCharactersBehavior */
  419. BIND_ENUM_CONSTANT(VC_CHARS_BEFORE_SHAPING);
  420. BIND_ENUM_CONSTANT(VC_CHARS_AFTER_SHAPING);
  421. BIND_ENUM_CONSTANT(VC_GLYPHS_AUTO);
  422. BIND_ENUM_CONSTANT(VC_GLYPHS_LTR);
  423. BIND_ENUM_CONSTANT(VC_GLYPHS_RTL);
  424. /* OverrunBehavior */
  425. BIND_ENUM_CONSTANT(OVERRUN_NO_TRIMMING);
  426. BIND_ENUM_CONSTANT(OVERRUN_TRIM_CHAR);
  427. BIND_ENUM_CONSTANT(OVERRUN_TRIM_WORD);
  428. BIND_ENUM_CONSTANT(OVERRUN_TRIM_ELLIPSIS);
  429. BIND_ENUM_CONSTANT(OVERRUN_TRIM_WORD_ELLIPSIS);
  430. /* TextOverrunFlag */
  431. BIND_BITFIELD_FLAG(OVERRUN_NO_TRIM);
  432. BIND_BITFIELD_FLAG(OVERRUN_TRIM);
  433. BIND_BITFIELD_FLAG(OVERRUN_TRIM_WORD_ONLY);
  434. BIND_BITFIELD_FLAG(OVERRUN_ADD_ELLIPSIS);
  435. BIND_BITFIELD_FLAG(OVERRUN_ENFORCE_ELLIPSIS);
  436. BIND_BITFIELD_FLAG(OVERRUN_JUSTIFICATION_AWARE);
  437. /* GraphemeFlag */
  438. BIND_BITFIELD_FLAG(GRAPHEME_IS_VALID);
  439. BIND_BITFIELD_FLAG(GRAPHEME_IS_RTL);
  440. BIND_BITFIELD_FLAG(GRAPHEME_IS_VIRTUAL);
  441. BIND_BITFIELD_FLAG(GRAPHEME_IS_SPACE);
  442. BIND_BITFIELD_FLAG(GRAPHEME_IS_BREAK_HARD);
  443. BIND_BITFIELD_FLAG(GRAPHEME_IS_BREAK_SOFT);
  444. BIND_BITFIELD_FLAG(GRAPHEME_IS_TAB);
  445. BIND_BITFIELD_FLAG(GRAPHEME_IS_ELONGATION);
  446. BIND_BITFIELD_FLAG(GRAPHEME_IS_PUNCTUATION);
  447. BIND_BITFIELD_FLAG(GRAPHEME_IS_UNDERSCORE);
  448. BIND_BITFIELD_FLAG(GRAPHEME_IS_CONNECTED);
  449. BIND_BITFIELD_FLAG(GRAPHEME_IS_SAFE_TO_INSERT_TATWEEL);
  450. BIND_BITFIELD_FLAG(GRAPHEME_IS_EMBEDDED_OBJECT);
  451. BIND_BITFIELD_FLAG(GRAPHEME_IS_SOFT_HYPHEN);
  452. /* Hinting */
  453. BIND_ENUM_CONSTANT(HINTING_NONE);
  454. BIND_ENUM_CONSTANT(HINTING_LIGHT);
  455. BIND_ENUM_CONSTANT(HINTING_NORMAL);
  456. /* SubpixelPositioning */
  457. BIND_ENUM_CONSTANT(SUBPIXEL_POSITIONING_DISABLED);
  458. BIND_ENUM_CONSTANT(SUBPIXEL_POSITIONING_AUTO);
  459. BIND_ENUM_CONSTANT(SUBPIXEL_POSITIONING_ONE_HALF);
  460. BIND_ENUM_CONSTANT(SUBPIXEL_POSITIONING_ONE_QUARTER);
  461. BIND_ENUM_CONSTANT(SUBPIXEL_POSITIONING_ONE_HALF_MAX_SIZE);
  462. BIND_ENUM_CONSTANT(SUBPIXEL_POSITIONING_ONE_QUARTER_MAX_SIZE);
  463. /* Feature */
  464. BIND_ENUM_CONSTANT(FEATURE_SIMPLE_LAYOUT);
  465. BIND_ENUM_CONSTANT(FEATURE_BIDI_LAYOUT);
  466. BIND_ENUM_CONSTANT(FEATURE_VERTICAL_LAYOUT);
  467. BIND_ENUM_CONSTANT(FEATURE_SHAPING);
  468. BIND_ENUM_CONSTANT(FEATURE_KASHIDA_JUSTIFICATION);
  469. BIND_ENUM_CONSTANT(FEATURE_BREAK_ITERATORS);
  470. BIND_ENUM_CONSTANT(FEATURE_FONT_BITMAP);
  471. BIND_ENUM_CONSTANT(FEATURE_FONT_DYNAMIC);
  472. BIND_ENUM_CONSTANT(FEATURE_FONT_MSDF);
  473. BIND_ENUM_CONSTANT(FEATURE_FONT_SYSTEM);
  474. BIND_ENUM_CONSTANT(FEATURE_FONT_VARIABLE);
  475. BIND_ENUM_CONSTANT(FEATURE_CONTEXT_SENSITIVE_CASE_CONVERSION);
  476. BIND_ENUM_CONSTANT(FEATURE_USE_SUPPORT_DATA);
  477. BIND_ENUM_CONSTANT(FEATURE_UNICODE_IDENTIFIERS);
  478. BIND_ENUM_CONSTANT(FEATURE_UNICODE_SECURITY);
  479. /* FT Contour Point Types */
  480. BIND_ENUM_CONSTANT(CONTOUR_CURVE_TAG_ON);
  481. BIND_ENUM_CONSTANT(CONTOUR_CURVE_TAG_OFF_CONIC);
  482. BIND_ENUM_CONSTANT(CONTOUR_CURVE_TAG_OFF_CUBIC);
  483. /* Font Spacing */
  484. BIND_ENUM_CONSTANT(SPACING_GLYPH);
  485. BIND_ENUM_CONSTANT(SPACING_SPACE);
  486. BIND_ENUM_CONSTANT(SPACING_TOP);
  487. BIND_ENUM_CONSTANT(SPACING_BOTTOM);
  488. BIND_ENUM_CONSTANT(SPACING_MAX);
  489. /* Font Style */
  490. BIND_BITFIELD_FLAG(FONT_BOLD);
  491. BIND_BITFIELD_FLAG(FONT_ITALIC);
  492. BIND_BITFIELD_FLAG(FONT_FIXED_WIDTH);
  493. /* Structured text parser */
  494. BIND_ENUM_CONSTANT(STRUCTURED_TEXT_DEFAULT);
  495. BIND_ENUM_CONSTANT(STRUCTURED_TEXT_URI);
  496. BIND_ENUM_CONSTANT(STRUCTURED_TEXT_FILE);
  497. BIND_ENUM_CONSTANT(STRUCTURED_TEXT_EMAIL);
  498. BIND_ENUM_CONSTANT(STRUCTURED_TEXT_LIST);
  499. BIND_ENUM_CONSTANT(STRUCTURED_TEXT_GDSCRIPT);
  500. BIND_ENUM_CONSTANT(STRUCTURED_TEXT_CUSTOM);
  501. /* Fixed size scale mode */
  502. BIND_ENUM_CONSTANT(FIXED_SIZE_SCALE_DISABLE);
  503. BIND_ENUM_CONSTANT(FIXED_SIZE_SCALE_INTEGER_ONLY);
  504. BIND_ENUM_CONSTANT(FIXED_SIZE_SCALE_ENABLED);
  505. }
  506. _FORCE_INLINE_ int32_t ot_tag_from_string(const char *p_str, int p_len) {
  507. char tag[4];
  508. uint32_t i;
  509. if (!p_str || !p_len || !*p_str) {
  510. return OT_TAG(0, 0, 0, 0);
  511. }
  512. if (p_len < 0 || p_len > 4) {
  513. p_len = 4;
  514. }
  515. for (i = 0; i < (uint32_t)p_len && p_str[i]; i++) {
  516. tag[i] = p_str[i];
  517. }
  518. for (; i < 4; i++) {
  519. tag[i] = ' ';
  520. }
  521. return OT_TAG(tag[0], tag[1], tag[2], tag[3]);
  522. }
  523. int64_t TextServer::name_to_tag(const String &p_name) const {
  524. // No readable name, use tag string.
  525. return ot_tag_from_string(p_name.replace("custom_", "").ascii().get_data(), -1);
  526. }
  527. _FORCE_INLINE_ void ot_tag_to_string(int32_t p_tag, char *p_buf) {
  528. p_buf[0] = (char)(uint8_t)(p_tag >> 24);
  529. p_buf[1] = (char)(uint8_t)(p_tag >> 16);
  530. p_buf[2] = (char)(uint8_t)(p_tag >> 8);
  531. p_buf[3] = (char)(uint8_t)(p_tag >> 0);
  532. }
  533. String TextServer::tag_to_name(int64_t p_tag) const {
  534. // No readable name, use tag string.
  535. char name[5];
  536. memset(name, 0, 5);
  537. ot_tag_to_string(p_tag, name);
  538. return String("custom_") + String(name);
  539. }
  540. Vector2 TextServer::get_hex_code_box_size(int64_t p_size, int64_t p_index) const {
  541. int w = ((p_index <= 0xFF) ? 1 : ((p_index <= 0xFFFF) ? 2 : 3));
  542. int sp = MAX(0, w - 1);
  543. int sz = MAX(1, Math::round(p_size / 15.f));
  544. return Vector2(4 + 3 * w + sp + 1, 15) * sz;
  545. }
  546. void TextServer::_draw_hex_code_box_number(const RID &p_canvas, int64_t p_size, const Vector2 &p_pos, uint8_t p_index, const Color &p_color) const {
  547. static uint8_t chars[] = { 0x7E, 0x30, 0x6D, 0x79, 0x33, 0x5B, 0x5F, 0x70, 0x7F, 0x7B, 0x77, 0x1F, 0x4E, 0x3D, 0x4F, 0x47, 0x00 };
  548. uint8_t x = chars[p_index];
  549. if (x & (1 << 6)) {
  550. RenderingServer::get_singleton()->canvas_item_add_rect(p_canvas, Rect2(p_pos, Size2(3, 1) * p_size), p_color);
  551. }
  552. if (x & (1 << 5)) {
  553. RenderingServer::get_singleton()->canvas_item_add_rect(p_canvas, Rect2(p_pos + Point2(2, 0) * p_size, Size2(1, 3) * p_size), p_color);
  554. }
  555. if (x & (1 << 4)) {
  556. RenderingServer::get_singleton()->canvas_item_add_rect(p_canvas, Rect2(p_pos + Point2(2, 2) * p_size, Size2(1, 3) * p_size), p_color);
  557. }
  558. if (x & (1 << 3)) {
  559. RenderingServer::get_singleton()->canvas_item_add_rect(p_canvas, Rect2(p_pos + Point2(0, 4) * p_size, Size2(3, 1) * p_size), p_color);
  560. }
  561. if (x & (1 << 2)) {
  562. RenderingServer::get_singleton()->canvas_item_add_rect(p_canvas, Rect2(p_pos + Point2(0, 2) * p_size, Size2(1, 3) * p_size), p_color);
  563. }
  564. if (x & (1 << 1)) {
  565. RenderingServer::get_singleton()->canvas_item_add_rect(p_canvas, Rect2(p_pos, Size2(1, 3) * p_size), p_color);
  566. }
  567. if (x & (1 << 0)) {
  568. RenderingServer::get_singleton()->canvas_item_add_rect(p_canvas, Rect2(p_pos + Point2(0, 2) * p_size, Size2(3, 1) * p_size), p_color);
  569. }
  570. }
  571. void TextServer::draw_hex_code_box(const RID &p_canvas, int64_t p_size, const Vector2 &p_pos, int64_t p_index, const Color &p_color) const {
  572. if (p_index == 0) {
  573. return;
  574. }
  575. int w = ((p_index <= 0xFF) ? 1 : ((p_index <= 0xFFFF) ? 2 : 3));
  576. int sp = MAX(0, w - 1);
  577. int sz = MAX(1, Math::round(p_size / 15.f));
  578. Size2 size = Vector2(4 + 3 * w + sp, 15) * sz;
  579. Point2 pos = p_pos - Point2i(0, size.y * 0.85);
  580. // Draw frame.
  581. RenderingServer::get_singleton()->canvas_item_add_rect(p_canvas, Rect2(pos + Point2(0, 0), Size2(sz, size.y)), p_color);
  582. RenderingServer::get_singleton()->canvas_item_add_rect(p_canvas, Rect2(pos + Point2(size.x - sz, 0), Size2(sz, size.y)), p_color);
  583. RenderingServer::get_singleton()->canvas_item_add_rect(p_canvas, Rect2(pos + Point2(0, 0), Size2(size.x, sz)), p_color);
  584. RenderingServer::get_singleton()->canvas_item_add_rect(p_canvas, Rect2(pos + Point2(0, size.y - sz), Size2(size.x, sz)), p_color);
  585. uint8_t a = p_index & 0x0F;
  586. uint8_t b = (p_index >> 4) & 0x0F;
  587. uint8_t c = (p_index >> 8) & 0x0F;
  588. uint8_t d = (p_index >> 12) & 0x0F;
  589. uint8_t e = (p_index >> 16) & 0x0F;
  590. uint8_t f = (p_index >> 20) & 0x0F;
  591. // Draw hex code.
  592. if (p_index <= 0xFF) {
  593. _draw_hex_code_box_number(p_canvas, sz, pos + Point2(2, 2) * sz, b, p_color);
  594. _draw_hex_code_box_number(p_canvas, sz, pos + Point2(2, 8) * sz, a, p_color);
  595. } else if (p_index <= 0xFFFF) {
  596. _draw_hex_code_box_number(p_canvas, sz, pos + Point2(2, 2) * sz, d, p_color);
  597. _draw_hex_code_box_number(p_canvas, sz, pos + Point2(6, 2) * sz, c, p_color);
  598. _draw_hex_code_box_number(p_canvas, sz, pos + Point2(2, 8) * sz, b, p_color);
  599. _draw_hex_code_box_number(p_canvas, sz, pos + Point2(6, 8) * sz, a, p_color);
  600. } else {
  601. _draw_hex_code_box_number(p_canvas, sz, pos + Point2(2, 2) * sz, f, p_color);
  602. _draw_hex_code_box_number(p_canvas, sz, pos + Point2(6, 2) * sz, e, p_color);
  603. _draw_hex_code_box_number(p_canvas, sz, pos + Point2(10, 2) * sz, d, p_color);
  604. _draw_hex_code_box_number(p_canvas, sz, pos + Point2(2, 8) * sz, c, p_color);
  605. _draw_hex_code_box_number(p_canvas, sz, pos + Point2(6, 8) * sz, b, p_color);
  606. _draw_hex_code_box_number(p_canvas, sz, pos + Point2(10, 8) * sz, a, p_color);
  607. }
  608. }
  609. bool TextServer::shaped_text_has_visible_chars(const RID &p_shaped) const {
  610. int v_size = shaped_text_get_glyph_count(p_shaped);
  611. if (v_size == 0) {
  612. return false;
  613. }
  614. const Glyph *glyphs = shaped_text_get_glyphs(p_shaped);
  615. for (int i = 0; i < v_size; i++) {
  616. if (glyphs[i].index != 0 && (glyphs[i].flags & GRAPHEME_IS_VIRTUAL) != GRAPHEME_IS_VIRTUAL) {
  617. return true;
  618. }
  619. }
  620. return false;
  621. }
  622. PackedInt32Array TextServer::shaped_text_get_line_breaks_adv(const RID &p_shaped, const PackedFloat32Array &p_width, int64_t p_start, bool p_once, BitField<TextServer::LineBreakFlag> p_break_flags) const {
  623. PackedInt32Array lines;
  624. ERR_FAIL_COND_V(p_width.is_empty(), lines);
  625. TextServer::Orientation orientation = shaped_text_get_orientation(p_shaped);
  626. const_cast<TextServer *>(this)->shaped_text_update_breaks(p_shaped);
  627. const Vector2i &range = shaped_text_get_range(p_shaped);
  628. real_t width = 0.f;
  629. int line_start = MAX(p_start, range.x);
  630. int last_end = line_start;
  631. int prev_safe_break = 0;
  632. int last_safe_break = -1;
  633. int word_count = 0;
  634. int chunk = 0;
  635. bool trim_next = false;
  636. int l_size = shaped_text_get_glyph_count(p_shaped);
  637. const Glyph *l_gl = const_cast<TextServer *>(this)->shaped_text_sort_logical(p_shaped);
  638. double indent = 0.0;
  639. if (p_break_flags.has_flag(BREAK_TRIM_INDENT)) {
  640. for (int i = 0; i < l_size; i++) {
  641. if ((l_gl[i].flags & GRAPHEME_IS_TAB) == GRAPHEME_IS_TAB || (l_gl[i].flags & GRAPHEME_IS_SPACE) == GRAPHEME_IS_SPACE) {
  642. indent += l_gl[i].advance * l_gl[i].repeat;
  643. } else {
  644. break;
  645. }
  646. }
  647. }
  648. for (int i = 0; i < l_size; i++) {
  649. double l_width = p_width[chunk];
  650. if (l_width > indent) {
  651. l_width -= indent;
  652. }
  653. if (l_gl[i].start < p_start) {
  654. prev_safe_break = i + 1;
  655. continue;
  656. }
  657. if (l_gl[i].count > 0) {
  658. if ((l_width > 0) && (width + l_gl[i].advance > l_width) && (last_safe_break >= 0)) {
  659. if (p_break_flags.has_flag(BREAK_TRIM_EDGE_SPACES)) {
  660. int start_pos = prev_safe_break;
  661. int end_pos = last_safe_break;
  662. while (trim_next && (start_pos < end_pos) && ((l_gl[start_pos].flags & GRAPHEME_IS_SOFT_HYPHEN) != GRAPHEME_IS_SOFT_HYPHEN) && ((l_gl[start_pos].flags & GRAPHEME_IS_SPACE) == GRAPHEME_IS_SPACE || (l_gl[start_pos].flags & GRAPHEME_IS_BREAK_HARD) == GRAPHEME_IS_BREAK_HARD || (l_gl[start_pos].flags & GRAPHEME_IS_BREAK_SOFT) == GRAPHEME_IS_BREAK_SOFT)) {
  663. start_pos += l_gl[start_pos].count;
  664. }
  665. while ((start_pos < end_pos) && ((l_gl[end_pos].flags & GRAPHEME_IS_SOFT_HYPHEN) != GRAPHEME_IS_SOFT_HYPHEN) && ((l_gl[end_pos].flags & GRAPHEME_IS_SPACE) == GRAPHEME_IS_SPACE || (l_gl[end_pos].flags & GRAPHEME_IS_BREAK_HARD) == GRAPHEME_IS_BREAK_HARD || (l_gl[end_pos].flags & GRAPHEME_IS_BREAK_SOFT) == GRAPHEME_IS_BREAK_SOFT)) {
  666. end_pos -= l_gl[end_pos].count;
  667. }
  668. if (last_end <= l_gl[start_pos].start) {
  669. lines.push_back(l_gl[start_pos].start);
  670. lines.push_back(l_gl[end_pos].end);
  671. last_end = l_gl[end_pos].end;
  672. }
  673. trim_next = true;
  674. } else {
  675. if (last_end <= line_start) {
  676. lines.push_back(line_start);
  677. lines.push_back(l_gl[last_safe_break].end);
  678. last_end = l_gl[last_safe_break].end;
  679. }
  680. }
  681. line_start = l_gl[last_safe_break].end;
  682. prev_safe_break = last_safe_break + 1;
  683. i = last_safe_break;
  684. last_safe_break = -1;
  685. width = 0;
  686. word_count = 0;
  687. chunk++;
  688. if (chunk >= p_width.size()) {
  689. chunk = 0;
  690. if (p_once) {
  691. return lines;
  692. }
  693. }
  694. continue;
  695. }
  696. if (p_break_flags.has_flag(BREAK_MANDATORY)) {
  697. if ((l_gl[i].flags & GRAPHEME_IS_BREAK_HARD) == GRAPHEME_IS_BREAK_HARD) {
  698. if (p_break_flags.has_flag(BREAK_TRIM_EDGE_SPACES)) {
  699. int start_pos = prev_safe_break;
  700. int end_pos = i;
  701. while (trim_next && (start_pos < end_pos) && ((l_gl[start_pos].flags & GRAPHEME_IS_SPACE) == GRAPHEME_IS_SPACE || (l_gl[start_pos].flags & GRAPHEME_IS_BREAK_HARD) == GRAPHEME_IS_BREAK_HARD || (l_gl[start_pos].flags & GRAPHEME_IS_BREAK_SOFT) == GRAPHEME_IS_BREAK_SOFT)) {
  702. start_pos += l_gl[start_pos].count;
  703. }
  704. while ((start_pos < end_pos) && ((l_gl[end_pos].flags & GRAPHEME_IS_SPACE) == GRAPHEME_IS_SPACE || (l_gl[end_pos].flags & GRAPHEME_IS_BREAK_HARD) == GRAPHEME_IS_BREAK_HARD || (l_gl[end_pos].flags & GRAPHEME_IS_BREAK_SOFT) == GRAPHEME_IS_BREAK_SOFT)) {
  705. end_pos -= l_gl[end_pos].count;
  706. }
  707. if (last_end <= l_gl[start_pos].start) {
  708. lines.push_back(l_gl[start_pos].start);
  709. lines.push_back(l_gl[end_pos].end);
  710. last_end = l_gl[end_pos].end;
  711. }
  712. trim_next = false;
  713. } else {
  714. if (last_end <= line_start) {
  715. lines.push_back(line_start);
  716. lines.push_back(l_gl[i].end);
  717. last_end = l_gl[i].end;
  718. }
  719. }
  720. line_start = l_gl[i].end;
  721. prev_safe_break = i + 1;
  722. last_safe_break = -1;
  723. width = 0;
  724. chunk = 0;
  725. if (p_once) {
  726. return lines;
  727. }
  728. continue;
  729. }
  730. }
  731. if (p_break_flags.has_flag(BREAK_WORD_BOUND)) {
  732. if ((l_gl[i].flags & GRAPHEME_IS_BREAK_SOFT) == GRAPHEME_IS_BREAK_SOFT) {
  733. if ((l_gl[i].flags & GRAPHEME_IS_SOFT_HYPHEN) == GRAPHEME_IS_SOFT_HYPHEN) {
  734. uint32_t gl = font_get_glyph_index(l_gl[i].font_rid, l_gl[i].font_size, 0x00ad, 0);
  735. float w = font_get_glyph_advance(l_gl[i].font_rid, l_gl[i].font_size, gl)[(orientation == ORIENTATION_HORIZONTAL) ? 0 : 1];
  736. if (width + l_gl[i].advance + w <= p_width[chunk]) {
  737. last_safe_break = i;
  738. word_count++;
  739. }
  740. } else {
  741. last_safe_break = i;
  742. word_count++;
  743. }
  744. }
  745. }
  746. if (p_break_flags.has_flag(BREAK_GRAPHEME_BOUND) && word_count == 0) {
  747. last_safe_break = i;
  748. }
  749. }
  750. width += l_gl[i].advance;
  751. }
  752. if (l_size > 0) {
  753. if (lines.size() == 0 || (lines[lines.size() - 1] < range.y && prev_safe_break < l_size)) {
  754. if (p_break_flags.has_flag(BREAK_TRIM_EDGE_SPACES)) {
  755. int start_pos = (prev_safe_break < l_size) ? prev_safe_break : l_size - 1;
  756. int end_pos = l_size - 1;
  757. while (trim_next && (start_pos < end_pos) && ((l_gl[start_pos].flags & GRAPHEME_IS_SPACE) == GRAPHEME_IS_SPACE || (l_gl[start_pos].flags & GRAPHEME_IS_BREAK_HARD) == GRAPHEME_IS_BREAK_HARD || (l_gl[start_pos].flags & GRAPHEME_IS_BREAK_SOFT) == GRAPHEME_IS_BREAK_SOFT)) {
  758. start_pos += l_gl[start_pos].count;
  759. }
  760. lines.push_back(l_gl[start_pos].start);
  761. } else {
  762. lines.push_back(line_start);
  763. }
  764. lines.push_back(range.y);
  765. }
  766. } else {
  767. lines.push_back(0);
  768. lines.push_back(0);
  769. }
  770. return lines;
  771. }
  772. PackedInt32Array TextServer::shaped_text_get_line_breaks(const RID &p_shaped, double p_width, int64_t p_start, BitField<TextServer::LineBreakFlag> p_break_flags) const {
  773. PackedInt32Array lines;
  774. const_cast<TextServer *>(this)->shaped_text_update_breaks(p_shaped);
  775. const Vector2i &range = shaped_text_get_range(p_shaped);
  776. double width = 0.f;
  777. int line_start = MAX(p_start, range.x);
  778. int last_end = line_start;
  779. int prev_safe_break = 0;
  780. int last_safe_break = -1;
  781. int word_count = 0;
  782. bool trim_next = false;
  783. TextServer::Orientation orientation = shaped_text_get_orientation(p_shaped);
  784. int l_size = shaped_text_get_glyph_count(p_shaped);
  785. const Glyph *l_gl = const_cast<TextServer *>(this)->shaped_text_sort_logical(p_shaped);
  786. double indent = 0.0;
  787. if (p_break_flags.has_flag(BREAK_TRIM_INDENT)) {
  788. for (int i = 0; i < l_size; i++) {
  789. if ((l_gl[i].flags & GRAPHEME_IS_TAB) == GRAPHEME_IS_TAB || (l_gl[i].flags & GRAPHEME_IS_SPACE) == GRAPHEME_IS_SPACE) {
  790. indent += l_gl[i].advance * l_gl[i].repeat;
  791. } else {
  792. break;
  793. }
  794. }
  795. }
  796. double l_width = p_width;
  797. for (int i = 0; i < l_size; i++) {
  798. if (l_gl[i].start < p_start) {
  799. prev_safe_break = i + 1;
  800. continue;
  801. }
  802. if (l_gl[i].count > 0) {
  803. if ((l_width > 0) && (width + l_gl[i].advance * l_gl[i].repeat > l_width) && (last_safe_break >= 0)) {
  804. if (p_break_flags.has_flag(BREAK_TRIM_EDGE_SPACES)) {
  805. int start_pos = prev_safe_break;
  806. int end_pos = last_safe_break;
  807. while (trim_next && (start_pos < end_pos) && ((l_gl[start_pos].flags & GRAPHEME_IS_SOFT_HYPHEN) != GRAPHEME_IS_SOFT_HYPHEN) && ((l_gl[start_pos].flags & GRAPHEME_IS_SPACE) == GRAPHEME_IS_SPACE || (l_gl[start_pos].flags & GRAPHEME_IS_BREAK_HARD) == GRAPHEME_IS_BREAK_HARD || (l_gl[start_pos].flags & GRAPHEME_IS_BREAK_SOFT) == GRAPHEME_IS_BREAK_SOFT)) {
  808. start_pos += l_gl[start_pos].count;
  809. }
  810. while ((start_pos < end_pos) && ((l_gl[end_pos].flags & GRAPHEME_IS_SOFT_HYPHEN) != GRAPHEME_IS_SOFT_HYPHEN) && ((l_gl[end_pos].flags & GRAPHEME_IS_SPACE) == GRAPHEME_IS_SPACE || (l_gl[end_pos].flags & GRAPHEME_IS_BREAK_HARD) == GRAPHEME_IS_BREAK_HARD || (l_gl[end_pos].flags & GRAPHEME_IS_BREAK_SOFT) == GRAPHEME_IS_BREAK_SOFT)) {
  811. end_pos -= l_gl[end_pos].count;
  812. }
  813. if (last_end <= l_gl[start_pos].start) {
  814. lines.push_back(l_gl[start_pos].start);
  815. lines.push_back(l_gl[end_pos].end);
  816. if (p_width > indent) {
  817. l_width = p_width - indent;
  818. }
  819. last_end = l_gl[end_pos].end;
  820. }
  821. trim_next = true;
  822. } else {
  823. if (last_end <= line_start) {
  824. lines.push_back(line_start);
  825. lines.push_back(l_gl[last_safe_break].end);
  826. if (p_width > indent) {
  827. l_width = p_width - indent;
  828. }
  829. last_end = l_gl[last_safe_break].end;
  830. }
  831. }
  832. line_start = l_gl[last_safe_break].end;
  833. prev_safe_break = last_safe_break + 1;
  834. i = last_safe_break;
  835. last_safe_break = -1;
  836. width = 0;
  837. word_count = 0;
  838. continue;
  839. }
  840. if (p_break_flags.has_flag(BREAK_MANDATORY)) {
  841. if ((l_gl[i].flags & GRAPHEME_IS_BREAK_HARD) == GRAPHEME_IS_BREAK_HARD) {
  842. if (p_break_flags.has_flag(BREAK_TRIM_EDGE_SPACES)) {
  843. int start_pos = prev_safe_break;
  844. int end_pos = i;
  845. while (trim_next && (start_pos < end_pos) && ((l_gl[start_pos].flags & GRAPHEME_IS_SPACE) == GRAPHEME_IS_SPACE || (l_gl[start_pos].flags & GRAPHEME_IS_BREAK_HARD) == GRAPHEME_IS_BREAK_HARD || (l_gl[start_pos].flags & GRAPHEME_IS_BREAK_SOFT) == GRAPHEME_IS_BREAK_SOFT)) {
  846. start_pos += l_gl[start_pos].count;
  847. }
  848. while ((start_pos < end_pos) && ((l_gl[end_pos].flags & GRAPHEME_IS_SPACE) == GRAPHEME_IS_SPACE || (l_gl[end_pos].flags & GRAPHEME_IS_BREAK_HARD) == GRAPHEME_IS_BREAK_HARD || (l_gl[end_pos].flags & GRAPHEME_IS_BREAK_SOFT) == GRAPHEME_IS_BREAK_SOFT)) {
  849. end_pos -= l_gl[end_pos].count;
  850. }
  851. trim_next = false;
  852. if (last_end <= l_gl[start_pos].start) {
  853. lines.push_back(l_gl[start_pos].start);
  854. lines.push_back(l_gl[end_pos].end);
  855. if (p_width > indent) {
  856. l_width = p_width - indent;
  857. }
  858. last_end = l_gl[end_pos].end;
  859. }
  860. } else {
  861. if (last_end <= line_start) {
  862. lines.push_back(line_start);
  863. lines.push_back(l_gl[i].end);
  864. if (p_width > indent) {
  865. l_width = p_width - indent;
  866. }
  867. last_end = l_gl[i].end;
  868. }
  869. }
  870. line_start = l_gl[i].end;
  871. prev_safe_break = i + 1;
  872. last_safe_break = -1;
  873. width = 0;
  874. continue;
  875. }
  876. }
  877. if (p_break_flags.has_flag(BREAK_WORD_BOUND)) {
  878. if ((l_gl[i].flags & GRAPHEME_IS_BREAK_SOFT) == GRAPHEME_IS_BREAK_SOFT) {
  879. if ((l_gl[i].flags & GRAPHEME_IS_SOFT_HYPHEN) == GRAPHEME_IS_SOFT_HYPHEN) {
  880. uint32_t gl = font_get_glyph_index(l_gl[i].font_rid, l_gl[i].font_size, 0x00AD, 0);
  881. float w = font_get_glyph_advance(l_gl[i].font_rid, l_gl[i].font_size, gl)[(orientation == ORIENTATION_HORIZONTAL) ? 0 : 1];
  882. if (width + l_gl[i].advance + w <= p_width) {
  883. last_safe_break = i;
  884. word_count++;
  885. }
  886. } else {
  887. last_safe_break = i;
  888. word_count++;
  889. }
  890. }
  891. if (p_break_flags.has_flag(BREAK_ADAPTIVE) && word_count == 0) {
  892. last_safe_break = i;
  893. }
  894. }
  895. if (p_break_flags.has_flag(BREAK_GRAPHEME_BOUND)) {
  896. last_safe_break = i;
  897. }
  898. }
  899. width += l_gl[i].advance * l_gl[i].repeat;
  900. }
  901. if (l_size > 0) {
  902. if (lines.size() == 0 || (lines[lines.size() - 1] < range.y && prev_safe_break < l_size)) {
  903. if (p_break_flags.has_flag(BREAK_TRIM_EDGE_SPACES)) {
  904. int start_pos = (prev_safe_break < l_size) ? prev_safe_break : l_size - 1;
  905. int end_pos = l_size - 1;
  906. while (trim_next && (start_pos < end_pos) && ((l_gl[start_pos].flags & GRAPHEME_IS_SPACE) == GRAPHEME_IS_SPACE || (l_gl[start_pos].flags & GRAPHEME_IS_BREAK_HARD) == GRAPHEME_IS_BREAK_HARD || (l_gl[start_pos].flags & GRAPHEME_IS_BREAK_SOFT) == GRAPHEME_IS_BREAK_SOFT)) {
  907. start_pos += l_gl[start_pos].count;
  908. }
  909. lines.push_back(l_gl[start_pos].start);
  910. } else {
  911. lines.push_back(line_start);
  912. }
  913. lines.push_back(range.y);
  914. }
  915. } else {
  916. lines.push_back(0);
  917. lines.push_back(0);
  918. }
  919. return lines;
  920. }
  921. PackedInt32Array TextServer::shaped_text_get_word_breaks(const RID &p_shaped, BitField<TextServer::GraphemeFlag> p_grapheme_flags, BitField<TextServer::GraphemeFlag> p_skip_grapheme_flags) const {
  922. PackedInt32Array words;
  923. const_cast<TextServer *>(this)->shaped_text_update_justification_ops(p_shaped);
  924. const Vector2i &range = shaped_text_get_range(p_shaped);
  925. int word_start = range.x;
  926. const int l_size = shaped_text_get_glyph_count(p_shaped);
  927. const Glyph *l_gl = const_cast<TextServer *>(this)->shaped_text_sort_logical(p_shaped);
  928. for (int i = 0; i < l_size; i++) {
  929. if (l_gl[i].count > 0) {
  930. if ((l_gl[i].flags & p_grapheme_flags) != 0 && (l_gl[i].flags & p_skip_grapheme_flags) == 0) {
  931. int next = (i == 0) ? l_gl[i].start : l_gl[i - 1].end;
  932. if (word_start < next) {
  933. words.push_back(word_start);
  934. words.push_back(next);
  935. }
  936. word_start = l_gl[i].end;
  937. }
  938. }
  939. }
  940. if (l_size > 0) {
  941. if (word_start != range.y) {
  942. words.push_back(word_start);
  943. words.push_back(range.y);
  944. }
  945. }
  946. return words;
  947. }
  948. CaretInfo TextServer::shaped_text_get_carets(const RID &p_shaped, int64_t p_position) const {
  949. Vector<Rect2> carets;
  950. TextServer::Orientation orientation = shaped_text_get_orientation(p_shaped);
  951. const Vector2 &range = shaped_text_get_range(p_shaped);
  952. real_t ascent = shaped_text_get_ascent(p_shaped);
  953. real_t descent = shaped_text_get_descent(p_shaped);
  954. real_t height = (ascent + descent) / 2;
  955. real_t off = 0.0f;
  956. CaretInfo caret;
  957. caret.l_dir = DIRECTION_AUTO;
  958. caret.t_dir = DIRECTION_AUTO;
  959. int v_size = shaped_text_get_glyph_count(p_shaped);
  960. const Glyph *glyphs = shaped_text_get_glyphs(p_shaped);
  961. for (int i = 0; i < v_size; i++) {
  962. if (glyphs[i].count > 0) {
  963. // Caret before grapheme (top / left).
  964. if (p_position == glyphs[i].start && ((glyphs[i].flags & GRAPHEME_IS_VIRTUAL) != GRAPHEME_IS_VIRTUAL)) {
  965. real_t advance = 0.f;
  966. for (int j = 0; j < glyphs[i].count; j++) {
  967. advance += glyphs[i + j].advance * glyphs[i + j].repeat;
  968. }
  969. real_t char_adv = advance / (real_t)(glyphs[i].end - glyphs[i].start);
  970. Rect2 cr;
  971. if (orientation == ORIENTATION_HORIZONTAL) {
  972. if (glyphs[i].start == range.x) {
  973. cr.size.y = height * 2;
  974. } else {
  975. cr.size.y = height;
  976. }
  977. cr.position.y = -ascent;
  978. cr.position.x = off;
  979. if ((glyphs[i].flags & GRAPHEME_IS_RTL) == GRAPHEME_IS_RTL) {
  980. caret.t_dir = DIRECTION_RTL;
  981. cr.position.x += advance;
  982. cr.size.x = -char_adv;
  983. } else {
  984. caret.t_dir = DIRECTION_LTR;
  985. cr.size.x = char_adv;
  986. }
  987. } else {
  988. if (glyphs[i].start == range.x) {
  989. cr.size.x = height * 2;
  990. } else {
  991. cr.size.x = height;
  992. }
  993. cr.position.x = -ascent;
  994. cr.position.y = off;
  995. if ((glyphs[i].flags & GRAPHEME_IS_RTL) == GRAPHEME_IS_RTL) {
  996. caret.t_dir = DIRECTION_RTL;
  997. cr.position.y += advance;
  998. cr.size.y = -char_adv;
  999. } else {
  1000. caret.t_dir = DIRECTION_LTR;
  1001. cr.size.y = char_adv;
  1002. }
  1003. }
  1004. caret.t_caret = cr;
  1005. }
  1006. // Caret after grapheme (bottom / right).
  1007. if (p_position == glyphs[i].end && ((glyphs[i].flags & GRAPHEME_IS_VIRTUAL) != GRAPHEME_IS_VIRTUAL)) {
  1008. real_t advance = 0.f;
  1009. for (int j = 0; j < glyphs[i].count; j++) {
  1010. advance += glyphs[i + j].advance * glyphs[i + j].repeat;
  1011. }
  1012. real_t char_adv = advance / (real_t)(glyphs[i].end - glyphs[i].start);
  1013. Rect2 cr;
  1014. if (orientation == ORIENTATION_HORIZONTAL) {
  1015. if (glyphs[i].end == range.y) {
  1016. cr.size.y = height * 2;
  1017. cr.position.y = -ascent;
  1018. } else {
  1019. cr.size.y = height;
  1020. cr.position.y = -ascent + height;
  1021. }
  1022. cr.position.x = off;
  1023. if ((glyphs[i].flags & GRAPHEME_IS_RTL) != GRAPHEME_IS_RTL) {
  1024. caret.l_dir = DIRECTION_LTR;
  1025. cr.position.x += advance;
  1026. cr.size.x = -char_adv;
  1027. } else {
  1028. caret.l_dir = DIRECTION_RTL;
  1029. cr.size.x = char_adv;
  1030. }
  1031. } else {
  1032. cr.size.y = 1.0f;
  1033. if (glyphs[i].end == range.y) {
  1034. cr.size.x = height * 2;
  1035. cr.position.x = -ascent;
  1036. } else {
  1037. cr.size.x = height;
  1038. cr.position.x = -ascent + height;
  1039. }
  1040. cr.position.y = off;
  1041. if ((glyphs[i].flags & GRAPHEME_IS_RTL) != GRAPHEME_IS_RTL) {
  1042. caret.l_dir = DIRECTION_LTR;
  1043. cr.position.y += advance;
  1044. cr.size.y = -char_adv;
  1045. } else {
  1046. caret.l_dir = DIRECTION_RTL;
  1047. cr.position.x += advance;
  1048. cr.size.y = char_adv;
  1049. }
  1050. }
  1051. caret.l_caret = cr;
  1052. }
  1053. // Caret inside grapheme (middle).
  1054. if (p_position > glyphs[i].start && p_position < glyphs[i].end && (glyphs[i].flags & GRAPHEME_IS_VIRTUAL) != GRAPHEME_IS_VIRTUAL) {
  1055. real_t advance = 0.f;
  1056. for (int j = 0; j < glyphs[i].count; j++) {
  1057. advance += glyphs[i + j].advance * glyphs[i + j].repeat;
  1058. }
  1059. real_t char_adv = advance / (real_t)(glyphs[i].end - glyphs[i].start);
  1060. Rect2 cr;
  1061. if (orientation == ORIENTATION_HORIZONTAL) {
  1062. cr.size.y = height * 2;
  1063. cr.position.y = -ascent;
  1064. if ((glyphs[i].flags & GRAPHEME_IS_RTL) == GRAPHEME_IS_RTL) {
  1065. cr.position.x = off + char_adv * (glyphs[i].end - p_position);
  1066. cr.size.x = -char_adv;
  1067. } else {
  1068. cr.position.x = off + char_adv * (p_position - glyphs[i].start);
  1069. cr.size.x = char_adv;
  1070. }
  1071. } else {
  1072. cr.size.x = height * 2;
  1073. cr.position.x = -ascent;
  1074. if ((glyphs[i].flags & GRAPHEME_IS_RTL) == GRAPHEME_IS_RTL) {
  1075. cr.position.y = off + char_adv * (glyphs[i].end - p_position);
  1076. cr.size.y = -char_adv;
  1077. } else {
  1078. cr.position.y = off + char_adv * (p_position - glyphs[i].start);
  1079. cr.size.y = char_adv;
  1080. }
  1081. }
  1082. caret.t_caret = cr;
  1083. caret.l_caret = cr;
  1084. }
  1085. }
  1086. off += glyphs[i].advance * glyphs[i].repeat;
  1087. }
  1088. return caret;
  1089. }
  1090. Dictionary TextServer::_shaped_text_get_carets_wrapper(const RID &p_shaped, int64_t p_position) const {
  1091. Dictionary ret;
  1092. CaretInfo caret = shaped_text_get_carets(p_shaped, p_position);
  1093. ret["leading_rect"] = caret.l_caret;
  1094. ret["leading_direction"] = caret.l_dir;
  1095. ret["trailing_rect"] = caret.t_caret;
  1096. ret["trailing_direction"] = caret.t_dir;
  1097. return ret;
  1098. }
  1099. TextServer::Direction TextServer::shaped_text_get_dominant_direction_in_range(const RID &p_shaped, int64_t p_start, int64_t p_end) const {
  1100. if (p_start == p_end) {
  1101. return DIRECTION_AUTO;
  1102. }
  1103. int start = MIN(p_start, p_end);
  1104. int end = MAX(p_start, p_end);
  1105. int rtl = 0;
  1106. int ltr = 0;
  1107. int v_size = shaped_text_get_glyph_count(p_shaped);
  1108. const Glyph *glyphs = shaped_text_get_glyphs(p_shaped);
  1109. for (int i = 0; i < v_size; i++) {
  1110. if ((glyphs[i].end > start) && (glyphs[i].start < end)) {
  1111. if (glyphs[i].count > 0) {
  1112. if ((glyphs[i].flags & GRAPHEME_IS_RTL) == GRAPHEME_IS_RTL) {
  1113. rtl++;
  1114. } else {
  1115. ltr++;
  1116. }
  1117. }
  1118. }
  1119. }
  1120. if (ltr == rtl) {
  1121. return DIRECTION_AUTO;
  1122. } else if (ltr > rtl) {
  1123. return DIRECTION_LTR;
  1124. } else {
  1125. return DIRECTION_RTL;
  1126. }
  1127. }
  1128. _FORCE_INLINE_ void _push_range(Vector<Vector2> &r_vector, real_t p_start, real_t p_end) {
  1129. if (!r_vector.is_empty() && Math::is_equal_approx(r_vector[r_vector.size() - 1].y, p_start, (real_t)UNIT_EPSILON)) {
  1130. r_vector.write[r_vector.size() - 1].y = p_end;
  1131. } else {
  1132. r_vector.push_back(Vector2(p_start, p_end));
  1133. }
  1134. }
  1135. Vector<Vector2> TextServer::shaped_text_get_selection(const RID &p_shaped, int64_t p_start, int64_t p_end) const {
  1136. Vector<Vector2> ranges;
  1137. if (p_start == p_end) {
  1138. return ranges;
  1139. }
  1140. int start = MIN(p_start, p_end);
  1141. int end = MAX(p_start, p_end);
  1142. int v_size = shaped_text_get_glyph_count(p_shaped);
  1143. const Glyph *glyphs = shaped_text_get_glyphs(p_shaped);
  1144. real_t off = 0.0f;
  1145. for (int i = 0; i < v_size; i++) {
  1146. for (int k = 0; k < glyphs[i].repeat; k++) {
  1147. if ((glyphs[i].count > 0) && ((glyphs[i].index != 0) || ((glyphs[i].flags & GRAPHEME_IS_SPACE) == GRAPHEME_IS_SPACE))) {
  1148. if (glyphs[i].start < end && glyphs[i].end > start) {
  1149. // Grapheme fully in selection range.
  1150. if (glyphs[i].start >= start && glyphs[i].end <= end) {
  1151. real_t advance = 0.f;
  1152. for (int j = 0; j < glyphs[i].count; j++) {
  1153. advance += glyphs[i + j].advance;
  1154. }
  1155. _push_range(ranges, off, off + advance);
  1156. }
  1157. // Only start of grapheme is in selection range.
  1158. if (glyphs[i].start >= start && glyphs[i].end > end) {
  1159. real_t advance = 0.f;
  1160. for (int j = 0; j < glyphs[i].count; j++) {
  1161. advance += glyphs[i + j].advance;
  1162. }
  1163. real_t char_adv = advance / (real_t)(glyphs[i].end - glyphs[i].start);
  1164. if ((glyphs[i].flags & GRAPHEME_IS_RTL) == GRAPHEME_IS_RTL) {
  1165. _push_range(ranges, off + char_adv * (glyphs[i].end - end), off + advance);
  1166. } else {
  1167. _push_range(ranges, off, off + char_adv * (end - glyphs[i].start));
  1168. }
  1169. }
  1170. // Only end of grapheme is in selection range.
  1171. if (glyphs[i].start < start && glyphs[i].end <= end) {
  1172. real_t advance = 0.f;
  1173. for (int j = 0; j < glyphs[i].count; j++) {
  1174. advance += glyphs[i + j].advance;
  1175. }
  1176. real_t char_adv = advance / (real_t)(glyphs[i].end - glyphs[i].start);
  1177. if ((glyphs[i].flags & GRAPHEME_IS_RTL) == GRAPHEME_IS_RTL) {
  1178. _push_range(ranges, off, off + char_adv * (glyphs[i].end - start));
  1179. } else {
  1180. _push_range(ranges, off + char_adv * (start - glyphs[i].start), off + advance);
  1181. }
  1182. }
  1183. // Selection range is within grapheme.
  1184. if (glyphs[i].start < start && glyphs[i].end > end) {
  1185. real_t advance = 0.f;
  1186. for (int j = 0; j < glyphs[i].count; j++) {
  1187. advance += glyphs[i + j].advance;
  1188. }
  1189. real_t char_adv = advance / (real_t)(glyphs[i].end - glyphs[i].start);
  1190. if ((glyphs[i].flags & GRAPHEME_IS_RTL) == GRAPHEME_IS_RTL) {
  1191. _push_range(ranges, off + char_adv * (glyphs[i].end - end), off + char_adv * (glyphs[i].end - start));
  1192. } else {
  1193. _push_range(ranges, off + char_adv * (start - glyphs[i].start), off + char_adv * (end - glyphs[i].start));
  1194. }
  1195. }
  1196. }
  1197. }
  1198. off += glyphs[i].advance;
  1199. }
  1200. }
  1201. return ranges;
  1202. }
  1203. int64_t TextServer::shaped_text_hit_test_grapheme(const RID &p_shaped, double p_coords) const {
  1204. // Exact grapheme hit test, return -1 if missed.
  1205. double off = 0.0f;
  1206. int v_size = shaped_text_get_glyph_count(p_shaped);
  1207. const Glyph *glyphs = shaped_text_get_glyphs(p_shaped);
  1208. for (int i = 0; i < v_size; i++) {
  1209. for (int j = 0; j < glyphs[i].repeat; j++) {
  1210. if (p_coords >= off && p_coords < off + glyphs[i].advance) {
  1211. return i;
  1212. }
  1213. off += glyphs[i].advance;
  1214. }
  1215. }
  1216. return -1;
  1217. }
  1218. int64_t TextServer::shaped_text_hit_test_position(const RID &p_shaped, double p_coords) const {
  1219. int v_size = shaped_text_get_glyph_count(p_shaped);
  1220. const Glyph *glyphs = shaped_text_get_glyphs(p_shaped);
  1221. // Cursor placement hit test.
  1222. // Place caret to the left of the leftmost grapheme, or to position 0 if string is empty.
  1223. if (p_coords <= 0) {
  1224. if (v_size > 0) {
  1225. if ((glyphs[0].flags & GRAPHEME_IS_RTL) == GRAPHEME_IS_RTL) {
  1226. return glyphs[0].end;
  1227. } else {
  1228. return glyphs[0].start;
  1229. }
  1230. } else {
  1231. return 0;
  1232. }
  1233. }
  1234. // Place caret to the right of the rightmost grapheme, or to position 0 if string is empty.
  1235. if (p_coords >= shaped_text_get_width(p_shaped)) {
  1236. if (v_size > 0) {
  1237. if ((glyphs[v_size - 1].flags & GRAPHEME_IS_RTL) == GRAPHEME_IS_RTL) {
  1238. return glyphs[v_size - 1].start;
  1239. } else {
  1240. return glyphs[v_size - 1].end;
  1241. }
  1242. } else {
  1243. return 0;
  1244. }
  1245. }
  1246. real_t off = 0.0f;
  1247. for (int i = 0; i < v_size; i++) {
  1248. if (glyphs[i].count > 0) {
  1249. real_t advance = 0.f;
  1250. for (int j = 0; j < glyphs[i].count; j++) {
  1251. advance += glyphs[i + j].advance * glyphs[i + j].repeat;
  1252. }
  1253. if (((glyphs[i].flags & GRAPHEME_IS_VIRTUAL) == GRAPHEME_IS_VIRTUAL) && (p_coords >= off && p_coords < off + advance)) {
  1254. if ((glyphs[i].flags & GRAPHEME_IS_RTL) == GRAPHEME_IS_RTL) {
  1255. return glyphs[i].end;
  1256. } else {
  1257. return glyphs[i].start;
  1258. }
  1259. }
  1260. // Ligature, handle mid-grapheme hit.
  1261. if (p_coords >= off && p_coords < off + advance && glyphs[i].end > glyphs[i].start + 1) {
  1262. int cnt = glyphs[i].end - glyphs[i].start;
  1263. real_t char_adv = advance / (real_t)(cnt);
  1264. real_t sub_off = off;
  1265. for (int j = 0; j < cnt; j++) {
  1266. // Place caret to the left of clicked sub-grapheme.
  1267. if (p_coords >= sub_off && p_coords < sub_off + char_adv / 2) {
  1268. if ((glyphs[i].flags & GRAPHEME_IS_RTL) == GRAPHEME_IS_RTL) {
  1269. return glyphs[i].end - j;
  1270. } else {
  1271. return glyphs[i].start + j;
  1272. }
  1273. }
  1274. // Place caret to the right of clicked sub-grapheme.
  1275. if (p_coords >= sub_off + char_adv / 2 && p_coords < sub_off + char_adv) {
  1276. if ((glyphs[i].flags & GRAPHEME_IS_RTL) == GRAPHEME_IS_RTL) {
  1277. return glyphs[i].start + (cnt - 1) - j;
  1278. } else {
  1279. return glyphs[i].end - (cnt - 1) + j;
  1280. }
  1281. }
  1282. sub_off += char_adv;
  1283. }
  1284. }
  1285. // Place caret to the left of clicked grapheme.
  1286. if (p_coords >= off && p_coords < off + advance / 2) {
  1287. if ((glyphs[i].flags & GRAPHEME_IS_RTL) == GRAPHEME_IS_RTL) {
  1288. return glyphs[i].end;
  1289. } else {
  1290. return glyphs[i].start;
  1291. }
  1292. }
  1293. // Place caret to the right of clicked grapheme.
  1294. if (p_coords >= off + advance / 2 && p_coords < off + advance) {
  1295. if ((glyphs[i].flags & GRAPHEME_IS_RTL) == GRAPHEME_IS_RTL) {
  1296. return glyphs[i].start;
  1297. } else {
  1298. return glyphs[i].end;
  1299. }
  1300. }
  1301. }
  1302. off += glyphs[i].advance * glyphs[i].repeat;
  1303. }
  1304. return 0;
  1305. }
  1306. Vector2 TextServer::shaped_text_get_grapheme_bounds(const RID &p_shaped, int64_t p_pos) const {
  1307. int v_size = shaped_text_get_glyph_count(p_shaped);
  1308. const Glyph *glyphs = shaped_text_get_glyphs(p_shaped);
  1309. real_t off = 0.0f;
  1310. for (int i = 0; i < v_size; i++) {
  1311. if ((glyphs[i].count > 0) && ((glyphs[i].index != 0) || ((glyphs[i].flags & GRAPHEME_IS_SPACE) == GRAPHEME_IS_SPACE))) {
  1312. if (glyphs[i].start <= p_pos && glyphs[i].end >= p_pos) {
  1313. real_t advance = 0.f;
  1314. for (int j = 0; j < glyphs[i].count; j++) {
  1315. advance += glyphs[i + j].advance;
  1316. }
  1317. return Vector2(off, off + advance);
  1318. }
  1319. }
  1320. off += glyphs[i].advance * glyphs[i].repeat;
  1321. }
  1322. return Vector2();
  1323. }
  1324. int64_t TextServer::shaped_text_next_grapheme_pos(const RID &p_shaped, int64_t p_pos) const {
  1325. int v_size = shaped_text_get_glyph_count(p_shaped);
  1326. const Glyph *glyphs = shaped_text_get_glyphs(p_shaped);
  1327. for (int i = 0; i < v_size; i++) {
  1328. if (p_pos >= glyphs[i].start && p_pos < glyphs[i].end) {
  1329. return glyphs[i].end;
  1330. }
  1331. }
  1332. return p_pos;
  1333. }
  1334. int64_t TextServer::shaped_text_prev_grapheme_pos(const RID &p_shaped, int64_t p_pos) const {
  1335. int v_size = shaped_text_get_glyph_count(p_shaped);
  1336. const Glyph *glyphs = shaped_text_get_glyphs(p_shaped);
  1337. for (int i = 0; i < v_size; i++) {
  1338. if (p_pos > glyphs[i].start && p_pos <= glyphs[i].end) {
  1339. return glyphs[i].start;
  1340. }
  1341. }
  1342. return p_pos;
  1343. }
  1344. int64_t TextServer::shaped_text_prev_character_pos(const RID &p_shaped, int64_t p_pos) const {
  1345. const PackedInt32Array &chars = shaped_text_get_character_breaks(p_shaped);
  1346. int64_t prev = shaped_text_get_range(p_shaped).x;
  1347. for (const int32_t &E : chars) {
  1348. if (E >= p_pos) {
  1349. return prev;
  1350. }
  1351. prev = E;
  1352. }
  1353. return prev;
  1354. }
  1355. int64_t TextServer::shaped_text_next_character_pos(const RID &p_shaped, int64_t p_pos) const {
  1356. const PackedInt32Array &chars = shaped_text_get_character_breaks(p_shaped);
  1357. int64_t prev = shaped_text_get_range(p_shaped).x;
  1358. for (const int32_t &E : chars) {
  1359. if (E > p_pos) {
  1360. return E;
  1361. }
  1362. prev = E;
  1363. }
  1364. return prev;
  1365. }
  1366. int64_t TextServer::shaped_text_closest_character_pos(const RID &p_shaped, int64_t p_pos) const {
  1367. const PackedInt32Array &chars = shaped_text_get_character_breaks(p_shaped);
  1368. int64_t prev = shaped_text_get_range(p_shaped).x;
  1369. for (const int32_t &E : chars) {
  1370. if (E == p_pos) {
  1371. return E;
  1372. } else if (E > p_pos) {
  1373. if ((E - p_pos) < (p_pos - prev)) {
  1374. return E;
  1375. } else {
  1376. return prev;
  1377. }
  1378. }
  1379. prev = E;
  1380. }
  1381. return prev;
  1382. }
  1383. PackedInt32Array TextServer::string_get_character_breaks(const String &p_string, const String &p_language) const {
  1384. PackedInt32Array ret;
  1385. if (!p_string.is_empty()) {
  1386. ret.resize(p_string.size() - 1);
  1387. for (int i = 0; i < p_string.size() - 1; i++) {
  1388. ret.write[i] = i + 1;
  1389. }
  1390. }
  1391. return ret;
  1392. }
  1393. void TextServer::shaped_text_draw(const RID &p_shaped, const RID &p_canvas, const Vector2 &p_pos, double p_clip_l, double p_clip_r, const Color &p_color) const {
  1394. TextServer::Orientation orientation = shaped_text_get_orientation(p_shaped);
  1395. bool hex_codes = shaped_text_get_preserve_control(p_shaped) || shaped_text_get_preserve_invalid(p_shaped);
  1396. bool rtl = shaped_text_get_direction(p_shaped) == DIRECTION_RTL;
  1397. int ellipsis_pos = shaped_text_get_ellipsis_pos(p_shaped);
  1398. int trim_pos = shaped_text_get_trim_pos(p_shaped);
  1399. const Glyph *ellipsis_glyphs = shaped_text_get_ellipsis_glyphs(p_shaped);
  1400. int ellipsis_gl_size = shaped_text_get_ellipsis_glyph_count(p_shaped);
  1401. int v_size = shaped_text_get_glyph_count(p_shaped);
  1402. const Glyph *glyphs = shaped_text_get_glyphs(p_shaped);
  1403. Vector2 ofs;
  1404. // Draw RTL ellipsis string when needed.
  1405. if (rtl && ellipsis_pos >= 0) {
  1406. for (int i = ellipsis_gl_size - 1; i >= 0; i--) {
  1407. for (int j = 0; j < ellipsis_glyphs[i].repeat; j++) {
  1408. font_draw_glyph(ellipsis_glyphs[i].font_rid, p_canvas, ellipsis_glyphs[i].font_size, ofs + p_pos + Vector2(ellipsis_glyphs[i].x_off, ellipsis_glyphs[i].y_off), ellipsis_glyphs[i].index, p_color);
  1409. if (orientation == ORIENTATION_HORIZONTAL) {
  1410. ofs.x += ellipsis_glyphs[i].advance;
  1411. } else {
  1412. ofs.y += ellipsis_glyphs[i].advance;
  1413. }
  1414. }
  1415. }
  1416. }
  1417. // Draw at the baseline.
  1418. for (int i = 0; i < v_size; i++) {
  1419. if (trim_pos >= 0) {
  1420. if (rtl) {
  1421. if (i < trim_pos) {
  1422. continue;
  1423. }
  1424. } else {
  1425. if (i >= trim_pos) {
  1426. break;
  1427. }
  1428. }
  1429. }
  1430. for (int j = 0; j < glyphs[i].repeat; j++) {
  1431. if (p_clip_r > 0) {
  1432. // Clip right / bottom.
  1433. if (orientation == ORIENTATION_HORIZONTAL) {
  1434. if (ofs.x + glyphs[i].advance > p_clip_r) {
  1435. return;
  1436. }
  1437. } else {
  1438. if (ofs.y + glyphs[i].advance > p_clip_r) {
  1439. return;
  1440. }
  1441. }
  1442. }
  1443. if (p_clip_l > 0) {
  1444. // Clip left / top.
  1445. if (orientation == ORIENTATION_HORIZONTAL) {
  1446. if (ofs.x < p_clip_l) {
  1447. ofs.x += glyphs[i].advance;
  1448. continue;
  1449. }
  1450. } else {
  1451. if (ofs.y < p_clip_l) {
  1452. ofs.y += glyphs[i].advance;
  1453. continue;
  1454. }
  1455. }
  1456. }
  1457. if (glyphs[i].font_rid != RID()) {
  1458. font_draw_glyph(glyphs[i].font_rid, p_canvas, glyphs[i].font_size, ofs + p_pos + Vector2(glyphs[i].x_off, glyphs[i].y_off), glyphs[i].index, p_color);
  1459. } else if (hex_codes && ((glyphs[i].flags & GRAPHEME_IS_VIRTUAL) != GRAPHEME_IS_VIRTUAL) && ((glyphs[i].flags & GRAPHEME_IS_EMBEDDED_OBJECT) != GRAPHEME_IS_EMBEDDED_OBJECT)) {
  1460. TextServer::draw_hex_code_box(p_canvas, glyphs[i].font_size, ofs + p_pos + Vector2(glyphs[i].x_off, glyphs[i].y_off), glyphs[i].index, p_color);
  1461. }
  1462. if (orientation == ORIENTATION_HORIZONTAL) {
  1463. ofs.x += glyphs[i].advance;
  1464. } else {
  1465. ofs.y += glyphs[i].advance;
  1466. }
  1467. }
  1468. }
  1469. // Draw LTR ellipsis string when needed.
  1470. if (!rtl && ellipsis_pos >= 0) {
  1471. for (int i = 0; i < ellipsis_gl_size; i++) {
  1472. for (int j = 0; j < ellipsis_glyphs[i].repeat; j++) {
  1473. font_draw_glyph(ellipsis_glyphs[i].font_rid, p_canvas, ellipsis_glyphs[i].font_size, ofs + p_pos + Vector2(ellipsis_glyphs[i].x_off, ellipsis_glyphs[i].y_off), ellipsis_glyphs[i].index, p_color);
  1474. if (orientation == ORIENTATION_HORIZONTAL) {
  1475. ofs.x += ellipsis_glyphs[i].advance;
  1476. } else {
  1477. ofs.y += ellipsis_glyphs[i].advance;
  1478. }
  1479. }
  1480. }
  1481. }
  1482. }
  1483. void TextServer::shaped_text_draw_outline(const RID &p_shaped, const RID &p_canvas, const Vector2 &p_pos, double p_clip_l, double p_clip_r, int64_t p_outline_size, const Color &p_color) const {
  1484. TextServer::Orientation orientation = shaped_text_get_orientation(p_shaped);
  1485. bool rtl = (shaped_text_get_inferred_direction(p_shaped) == DIRECTION_RTL);
  1486. int ellipsis_pos = shaped_text_get_ellipsis_pos(p_shaped);
  1487. int trim_pos = shaped_text_get_trim_pos(p_shaped);
  1488. const Glyph *ellipsis_glyphs = shaped_text_get_ellipsis_glyphs(p_shaped);
  1489. int ellipsis_gl_size = shaped_text_get_ellipsis_glyph_count(p_shaped);
  1490. int v_size = shaped_text_get_glyph_count(p_shaped);
  1491. const Glyph *glyphs = shaped_text_get_glyphs(p_shaped);
  1492. Vector2 ofs;
  1493. // Draw RTL ellipsis string when needed.
  1494. if (rtl && ellipsis_pos >= 0) {
  1495. for (int i = ellipsis_gl_size - 1; i >= 0; i--) {
  1496. for (int j = 0; j < ellipsis_glyphs[i].repeat; j++) {
  1497. font_draw_glyph_outline(ellipsis_glyphs[i].font_rid, p_canvas, ellipsis_glyphs[i].font_size, p_outline_size, ofs + p_pos + Vector2(ellipsis_glyphs[i].x_off, ellipsis_glyphs[i].y_off), ellipsis_glyphs[i].index, p_color);
  1498. if (orientation == ORIENTATION_HORIZONTAL) {
  1499. ofs.x += ellipsis_glyphs[i].advance;
  1500. } else {
  1501. ofs.y += ellipsis_glyphs[i].advance;
  1502. }
  1503. }
  1504. }
  1505. }
  1506. // Draw at the baseline.
  1507. for (int i = 0; i < v_size; i++) {
  1508. if (trim_pos >= 0) {
  1509. if (rtl) {
  1510. if (i < trim_pos) {
  1511. continue;
  1512. }
  1513. } else {
  1514. if (i >= trim_pos) {
  1515. break;
  1516. }
  1517. }
  1518. }
  1519. for (int j = 0; j < glyphs[i].repeat; j++) {
  1520. if (p_clip_r > 0) {
  1521. // Clip right / bottom.
  1522. if (orientation == ORIENTATION_HORIZONTAL) {
  1523. if (ofs.x + glyphs[i].advance > p_clip_r) {
  1524. return;
  1525. }
  1526. } else {
  1527. if (ofs.y + glyphs[i].advance > p_clip_r) {
  1528. return;
  1529. }
  1530. }
  1531. }
  1532. if (p_clip_l > 0) {
  1533. // Clip left / top.
  1534. if (orientation == ORIENTATION_HORIZONTAL) {
  1535. if (ofs.x < p_clip_l) {
  1536. ofs.x += glyphs[i].advance;
  1537. continue;
  1538. }
  1539. } else {
  1540. if (ofs.y < p_clip_l) {
  1541. ofs.y += glyphs[i].advance;
  1542. continue;
  1543. }
  1544. }
  1545. }
  1546. if (glyphs[i].font_rid != RID()) {
  1547. font_draw_glyph_outline(glyphs[i].font_rid, p_canvas, glyphs[i].font_size, p_outline_size, ofs + p_pos + Vector2(glyphs[i].x_off, glyphs[i].y_off), glyphs[i].index, p_color);
  1548. }
  1549. if (orientation == ORIENTATION_HORIZONTAL) {
  1550. ofs.x += glyphs[i].advance;
  1551. } else {
  1552. ofs.y += glyphs[i].advance;
  1553. }
  1554. }
  1555. }
  1556. // Draw LTR ellipsis string when needed.
  1557. if (!rtl && ellipsis_pos >= 0) {
  1558. for (int i = 0; i < ellipsis_gl_size; i++) {
  1559. for (int j = 0; j < ellipsis_glyphs[i].repeat; j++) {
  1560. font_draw_glyph_outline(ellipsis_glyphs[i].font_rid, p_canvas, ellipsis_glyphs[i].font_size, p_outline_size, ofs + p_pos + Vector2(ellipsis_glyphs[i].x_off, ellipsis_glyphs[i].y_off), ellipsis_glyphs[i].index, p_color);
  1561. if (orientation == ORIENTATION_HORIZONTAL) {
  1562. ofs.x += ellipsis_glyphs[i].advance;
  1563. } else {
  1564. ofs.y += ellipsis_glyphs[i].advance;
  1565. }
  1566. }
  1567. }
  1568. }
  1569. }
  1570. void TextServer::_diacritics_map_add(const String &p_from, char32_t p_to) {
  1571. for (int i = 0; i < p_from.size(); i++) {
  1572. diacritics_map[p_from[i]] = p_to;
  1573. }
  1574. }
  1575. void TextServer::_init_diacritics_map() {
  1576. diacritics_map.clear();
  1577. // Latin.
  1578. _diacritics_map_add(U"ÀÁÂÃÄÅĀĂĄǍǞǠǺȀȂȦḀẠẢẤẦẨẪẬẮẰẲẴẶ", U'A');
  1579. _diacritics_map_add(U"àáâãäåāăąǎǟǡǻȁȃȧḁẚạảấầẩẫậắằẳẵặ", U'a');
  1580. _diacritics_map_add(U"ǢǼ", U'Æ');
  1581. _diacritics_map_add(U"ǣǽ", U'æ');
  1582. _diacritics_map_add(U"ḂḄḆ", U'B');
  1583. _diacritics_map_add(U"ḃḅḇ", U'b');
  1584. _diacritics_map_add(U"ÇĆĈĊČḈ", U'C');
  1585. _diacritics_map_add(U"çćĉċčḉ", U'c');
  1586. _diacritics_map_add(U"ĎḊḌḎḐḒ", U'D');
  1587. _diacritics_map_add(U"ďḋḍḏḑḓ", U'd');
  1588. _diacritics_map_add(U"ÈÉÊËĒĔĖĘĚȆȨḔḖḘḚḜẸẺẼẾỀỂỄỆ", U'E');
  1589. _diacritics_map_add(U"èéêëēĕėęěȇȩḕḗḙḛḝẹẻẽếềểễệ", U'e');
  1590. _diacritics_map_add(U"Ḟ", U'F');
  1591. _diacritics_map_add(U"ḟ", U'f');
  1592. _diacritics_map_add(U"ĜĞĠĢǦǴḠ", U'G');
  1593. _diacritics_map_add(U"ĝğġģǧǵḡ", U'g');
  1594. _diacritics_map_add(U"ĤȞḢḤḦḨḪ", U'H');
  1595. _diacritics_map_add(U"ĥȟḣḥḧḩḫẖ", U'h');
  1596. _diacritics_map_add(U"ÌÍÎÏĨĪĬĮİǏȈȊḬḮỈỊ", U'I');
  1597. _diacritics_map_add(U"ìíîïĩīĭįıǐȉȋḭḯỉị", U'i');
  1598. _diacritics_map_add(U"Ĵ", U'J');
  1599. _diacritics_map_add(U"ĵ", U'j');
  1600. _diacritics_map_add(U"ĶǨḰḲḴ", U'K');
  1601. _diacritics_map_add(U"ķĸǩḱḳḵ", U'k');
  1602. _diacritics_map_add(U"ĹĻĽĿḶḸḺḼ", U'L');
  1603. _diacritics_map_add(U"ĺļľŀḷḹḻḽ", U'l');
  1604. _diacritics_map_add(U"ḾṀṂ", U'M');
  1605. _diacritics_map_add(U"ḿṁṃ", U'm');
  1606. _diacritics_map_add(U"ÑŃŅŇǸṄṆṈṊ", U'N');
  1607. _diacritics_map_add(U"ñńņňʼnǹṅṇṉṋ", U'n');
  1608. _diacritics_map_add(U"ÒÓÔÕÖŌŎŐƠǑǪǬȌȎȪȬȮȰṌṎṐṒỌỎỐỒỔỖỘỚỜỞỠỢ", U'O');
  1609. _diacritics_map_add(U"òóôõöōŏőơǒǫǭȍȏȫȭȯȱṍṏṑṓọỏốồổỗộớờởỡợ", U'o');
  1610. _diacritics_map_add(U"ṔṖ", U'P');
  1611. _diacritics_map_add(U"ṗṕ", U'p');
  1612. _diacritics_map_add(U"ŔŖŘȐȒṘṚṜṞ", U'R');
  1613. _diacritics_map_add(U"ŕŗřȑȓṙṛṝṟ", U'r');
  1614. _diacritics_map_add(U"ŚŜŞŠȘṠṢṤṦṨ", U'S');
  1615. _diacritics_map_add(U"śŝşšſșṡṣṥṧṩẛẜẝ", U's');
  1616. _diacritics_map_add(U"ŢŤȚṪṬṮṰ", U'T');
  1617. _diacritics_map_add(U"ţťțṫṭṯṱẗ", U't');
  1618. _diacritics_map_add(U"ÙÚÛÜŨŪŬŮŰŲƯǓǕǗǙǛȔȖṲṴṶṸṺỤỦỨỪỬỮỰ", U'U');
  1619. _diacritics_map_add(U"ùúûüũūŭůűųưǔǖǘǚǜȕȗṳṵṷṹṻụủứừửữự", U'u');
  1620. _diacritics_map_add(U"ṼṾ", U'V');
  1621. _diacritics_map_add(U"ṽṿ", U'v');
  1622. _diacritics_map_add(U"ŴẀẂẄẆẈ", U'W');
  1623. _diacritics_map_add(U"ŵẁẃẅẇẉẘ", U'w');
  1624. _diacritics_map_add(U"ẊẌ", U'X');
  1625. _diacritics_map_add(U"ẋẍ", U'x');
  1626. _diacritics_map_add(U"ÝŶẎỲỴỶỸỾ", U'Y');
  1627. _diacritics_map_add(U"ýÿŷẏẙỳỵỷỹỿ", U'y');
  1628. _diacritics_map_add(U"ŹŻŽẐẒẔ", U'Z');
  1629. _diacritics_map_add(U"źżžẑẓẕ", U'z');
  1630. // Greek.
  1631. _diacritics_map_add(U"ΆἈἉἊἋἌἍἎἏᾈᾉᾊᾋᾌᾍᾎᾏᾸᾹᾺΆᾼ", U'Α');
  1632. _diacritics_map_add(U"άἀἁἂἃἄἅἆἇὰάᾀᾁᾂᾃᾄᾅᾆᾇᾰᾱᾲᾳᾴᾶᾷ", U'α');
  1633. _diacritics_map_add(U"ΈἘἙἚἛἜἝῈΈ", U'Ε');
  1634. _diacritics_map_add(U"έἐἑἒἓἔἕὲέ", U'ε');
  1635. _diacritics_map_add(U"ΉἨἩἪἫἬἭἮἯᾘᾙᾚᾛᾜᾝᾞᾟῊΉῌ", U'Η');
  1636. _diacritics_map_add(U"ήἠἡἢἣἤἥἦἧὴήᾐᾑᾒᾓᾔᾕᾖᾗῂῃῄῆῇ", U'η');
  1637. _diacritics_map_add(U"ΊΪἸἹἺἻἼἽἾἿῘῙῚΊ", U'Ι');
  1638. _diacritics_map_add(U"ίΐϊἰἱἲἳἴἵἶἷὶίῐῑῒΐῖῗ", U'ι');
  1639. _diacritics_map_add(U"ΌὈὉὊὋὌὍῸΌ", U'Ο');
  1640. _diacritics_map_add(U"όὀὁὂὃὄὅὸό", U'ο');
  1641. _diacritics_map_add(U"Ῥ", U'Ρ');
  1642. _diacritics_map_add(U"ῤῥ", U'ρ');
  1643. _diacritics_map_add(U"ΎΫϓϔὙὛὝὟῨῩῪΎ", U'Υ');
  1644. _diacritics_map_add(U"ΰϋύὐὑὒὓὔὕὖὗὺύῠῡῢΰῦῧ", U'υ');
  1645. _diacritics_map_add(U"ΏὨὩὪὫὬὭὮὯᾨᾩᾪᾫᾬᾭᾮᾯῺΏῼ", U'Ω');
  1646. _diacritics_map_add(U"ώὠὡὢὣὤὥὦὧὼώᾠᾡᾢᾣᾤᾥᾦᾧῲῳῴῶῷ", U'ω');
  1647. // Cyrillic.
  1648. _diacritics_map_add(U"ӐӒ", U'А');
  1649. _diacritics_map_add(U"ӑӓ", U'а');
  1650. _diacritics_map_add(U"ЀЁӖ", U'Е');
  1651. _diacritics_map_add(U"ѐёӗ", U'е');
  1652. _diacritics_map_add(U"Ӛ", U'Ә');
  1653. _diacritics_map_add(U"ӛ", U'ә');
  1654. _diacritics_map_add(U"Ӝ", U'Ж');
  1655. _diacritics_map_add(U"ӝ", U'ж');
  1656. _diacritics_map_add(U"Ӟ", U'З');
  1657. _diacritics_map_add(U"ӟ", U'з');
  1658. _diacritics_map_add(U"Ѓ", U'Г');
  1659. _diacritics_map_add(U"ѓ", U'г');
  1660. _diacritics_map_add(U"Ї", U'І');
  1661. _diacritics_map_add(U"ї", U'і');
  1662. _diacritics_map_add(U"ЍӢӤЙ", U'И');
  1663. _diacritics_map_add(U"ѝӣӥй", U'и');
  1664. _diacritics_map_add(U"Ќ", U'К');
  1665. _diacritics_map_add(U"ќ", U'к');
  1666. _diacritics_map_add(U"Ӧ", U'О');
  1667. _diacritics_map_add(U"ӧ", U'о');
  1668. _diacritics_map_add(U"Ӫ", U'Ө');
  1669. _diacritics_map_add(U"ӫ", U'ө');
  1670. _diacritics_map_add(U"Ӭ", U'Э');
  1671. _diacritics_map_add(U"ӭ", U'э');
  1672. _diacritics_map_add(U"ЎӮӰӲ", U'У');
  1673. _diacritics_map_add(U"ўӯӱӳ", U'у');
  1674. _diacritics_map_add(U"Ӵ", U'Ч');
  1675. _diacritics_map_add(U"ӵ", U'ч');
  1676. _diacritics_map_add(U"Ӹ", U'Ы');
  1677. _diacritics_map_add(U"ӹ", U'ы');
  1678. }
  1679. String TextServer::strip_diacritics(const String &p_string) const {
  1680. String result;
  1681. for (int i = 0; i < p_string.length(); i++) {
  1682. if (p_string[i] < 0x02B0 || p_string[i] > 0x036F) { // Skip combining diacritics.
  1683. if (diacritics_map.has(p_string[i])) {
  1684. result += diacritics_map[p_string[i]];
  1685. } else {
  1686. result += p_string[i];
  1687. }
  1688. }
  1689. }
  1690. return result;
  1691. }
  1692. TypedArray<Vector3i> TextServer::parse_structured_text(StructuredTextParser p_parser_type, const Array &p_args, const String &p_text) const {
  1693. TypedArray<Vector3i> ret;
  1694. switch (p_parser_type) {
  1695. case STRUCTURED_TEXT_URI: {
  1696. int prev = 0;
  1697. for (int i = 0; i < p_text.length(); i++) {
  1698. if ((p_text[i] == '\\') || (p_text[i] == '/') || (p_text[i] == '.') || (p_text[i] == ':') || (p_text[i] == '&') || (p_text[i] == '=') || (p_text[i] == '@') || (p_text[i] == '?') || (p_text[i] == '#')) {
  1699. if (prev != i) {
  1700. ret.push_back(Vector3i(prev, i, TextServer::DIRECTION_AUTO));
  1701. }
  1702. ret.push_back(Vector3i(i, i + 1, TextServer::DIRECTION_LTR));
  1703. prev = i + 1;
  1704. }
  1705. }
  1706. if (prev != p_text.length()) {
  1707. ret.push_back(Vector3i(prev, p_text.length(), TextServer::DIRECTION_AUTO));
  1708. }
  1709. } break;
  1710. case STRUCTURED_TEXT_FILE: {
  1711. int prev = 0;
  1712. for (int i = 0; i < p_text.length(); i++) {
  1713. if ((p_text[i] == '\\') || (p_text[i] == '/') || (p_text[i] == ':')) {
  1714. if (prev != i) {
  1715. ret.push_back(Vector3i(prev, i, TextServer::DIRECTION_AUTO));
  1716. }
  1717. ret.push_back(Vector3i(i, i + 1, TextServer::DIRECTION_LTR));
  1718. prev = i + 1;
  1719. }
  1720. }
  1721. if (prev != p_text.length()) {
  1722. ret.push_back(Vector3i(prev, p_text.length(), TextServer::DIRECTION_AUTO));
  1723. }
  1724. } break;
  1725. case STRUCTURED_TEXT_EMAIL: {
  1726. bool local = true;
  1727. int prev = 0;
  1728. for (int i = 0; i < p_text.length(); i++) {
  1729. if ((p_text[i] == '@') && local) { // Add full "local" as single context.
  1730. local = false;
  1731. ret.push_back(Vector3i(prev, i, TextServer::DIRECTION_AUTO));
  1732. ret.push_back(Vector3i(i, i + 1, TextServer::DIRECTION_LTR));
  1733. prev = i + 1;
  1734. } else if (!local && (p_text[i] == '.')) { // Add each dot separated "domain" part as context.
  1735. if (prev != i) {
  1736. ret.push_back(Vector3i(prev, i, TextServer::DIRECTION_AUTO));
  1737. }
  1738. ret.push_back(Vector3i(i, i + 1, TextServer::DIRECTION_LTR));
  1739. prev = i + 1;
  1740. }
  1741. }
  1742. if (prev != p_text.length()) {
  1743. ret.push_back(Vector3i(prev, p_text.length(), TextServer::DIRECTION_AUTO));
  1744. }
  1745. } break;
  1746. case STRUCTURED_TEXT_LIST: {
  1747. if (p_args.size() == 1 && p_args[0].is_string()) {
  1748. Vector<String> tags = p_text.split(String(p_args[0]));
  1749. int prev = 0;
  1750. for (int i = 0; i < tags.size(); i++) {
  1751. if (prev != i) {
  1752. ret.push_back(Vector3i(prev, prev + tags[i].length(), TextServer::DIRECTION_INHERITED));
  1753. }
  1754. ret.push_back(Vector3i(prev + tags[i].length(), prev + tags[i].length() + 1, TextServer::DIRECTION_INHERITED));
  1755. prev = prev + tags[i].length() + 1;
  1756. }
  1757. }
  1758. } break;
  1759. case STRUCTURED_TEXT_GDSCRIPT: {
  1760. bool in_string_literal = false;
  1761. bool in_string_literal_single = false;
  1762. bool in_id = false;
  1763. int prev = 0;
  1764. for (int i = 0; i < p_text.length(); i++) {
  1765. char32_t c = p_text[i];
  1766. if (in_string_literal) {
  1767. if (c == '\\') {
  1768. i++;
  1769. continue; // Skip escaped chars.
  1770. } else if (c == '\"') {
  1771. // String literal end, push string and ".
  1772. if (prev != i) {
  1773. ret.push_back(Vector3i(prev, i, TextServer::DIRECTION_AUTO));
  1774. }
  1775. prev = i + 1;
  1776. ret.push_back(Vector3i(i, i + 1, TextServer::DIRECTION_LTR));
  1777. in_string_literal = false;
  1778. }
  1779. } else if (in_string_literal_single) {
  1780. if (c == '\\') {
  1781. i++;
  1782. continue; // Skip escaped chars.
  1783. } else if (c == '\'') {
  1784. // String literal end, push string and '.
  1785. if (prev != i) {
  1786. ret.push_back(Vector3i(prev, i, TextServer::DIRECTION_AUTO));
  1787. }
  1788. prev = i + 1;
  1789. ret.push_back(Vector3i(i, i + 1, TextServer::DIRECTION_LTR));
  1790. in_string_literal_single = false;
  1791. }
  1792. } else if (in_id) {
  1793. if (!is_unicode_identifier_continue(c)) {
  1794. // End of id, push id.
  1795. if (prev != i) {
  1796. ret.push_back(Vector3i(prev, i, TextServer::DIRECTION_AUTO));
  1797. }
  1798. prev = i;
  1799. in_id = false;
  1800. }
  1801. } else if (is_unicode_identifier_start(c)) {
  1802. // Start of new id, push prev element.
  1803. if (prev != i) {
  1804. ret.push_back(Vector3i(prev, i, TextServer::DIRECTION_AUTO));
  1805. }
  1806. prev = i;
  1807. in_id = true;
  1808. } else if (c == '\"') {
  1809. // String literal start, push prev element and ".
  1810. if (prev != i) {
  1811. ret.push_back(Vector3i(prev, i, TextServer::DIRECTION_AUTO));
  1812. }
  1813. prev = i + 1;
  1814. ret.push_back(Vector3i(i, i + 1, TextServer::DIRECTION_LTR));
  1815. in_string_literal = true;
  1816. } else if (c == '\'') {
  1817. // String literal start, push prev element and '.
  1818. if (prev != i) {
  1819. ret.push_back(Vector3i(prev, i, TextServer::DIRECTION_AUTO));
  1820. }
  1821. prev = i + 1;
  1822. ret.push_back(Vector3i(i, i + 1, TextServer::DIRECTION_LTR));
  1823. in_string_literal_single = true;
  1824. } else if (c == '#') {
  1825. // Start of comment, push prev element and #, skip the rest of the text.
  1826. if (prev != i) {
  1827. ret.push_back(Vector3i(prev, i, TextServer::DIRECTION_AUTO));
  1828. }
  1829. prev = p_text.length();
  1830. ret.push_back(Vector3i(i, p_text.length(), TextServer::DIRECTION_AUTO));
  1831. break;
  1832. }
  1833. }
  1834. if (prev < p_text.length()) {
  1835. ret.push_back(Vector3i(prev, p_text.length(), TextServer::DIRECTION_AUTO));
  1836. }
  1837. } break;
  1838. case STRUCTURED_TEXT_CUSTOM:
  1839. case STRUCTURED_TEXT_DEFAULT:
  1840. default: {
  1841. ret.push_back(Vector3i(0, p_text.length(), TextServer::DIRECTION_INHERITED));
  1842. }
  1843. }
  1844. return ret;
  1845. }
  1846. TypedArray<Dictionary> TextServer::_shaped_text_get_glyphs_wrapper(const RID &p_shaped) const {
  1847. TypedArray<Dictionary> ret;
  1848. const Glyph *glyphs = shaped_text_get_glyphs(p_shaped);
  1849. int gl_size = shaped_text_get_glyph_count(p_shaped);
  1850. for (int i = 0; i < gl_size; i++) {
  1851. Dictionary glyph;
  1852. glyph["start"] = glyphs[i].start;
  1853. glyph["end"] = glyphs[i].end;
  1854. glyph["repeat"] = glyphs[i].repeat;
  1855. glyph["count"] = glyphs[i].count;
  1856. glyph["flags"] = glyphs[i].flags;
  1857. glyph["offset"] = Vector2(glyphs[i].x_off, glyphs[i].y_off);
  1858. glyph["advance"] = glyphs[i].advance;
  1859. glyph["font_rid"] = glyphs[i].font_rid;
  1860. glyph["font_size"] = glyphs[i].font_size;
  1861. glyph["index"] = glyphs[i].index;
  1862. ret.push_back(glyph);
  1863. }
  1864. return ret;
  1865. }
  1866. TypedArray<Dictionary> TextServer::_shaped_text_sort_logical_wrapper(const RID &p_shaped) {
  1867. Array ret;
  1868. const Glyph *glyphs = shaped_text_sort_logical(p_shaped);
  1869. int gl_size = shaped_text_get_glyph_count(p_shaped);
  1870. for (int i = 0; i < gl_size; i++) {
  1871. Dictionary glyph;
  1872. glyph["start"] = glyphs[i].start;
  1873. glyph["end"] = glyphs[i].end;
  1874. glyph["repeat"] = glyphs[i].repeat;
  1875. glyph["count"] = glyphs[i].count;
  1876. glyph["flags"] = glyphs[i].flags;
  1877. glyph["offset"] = Vector2(glyphs[i].x_off, glyphs[i].y_off);
  1878. glyph["advance"] = glyphs[i].advance;
  1879. glyph["font_rid"] = glyphs[i].font_rid;
  1880. glyph["font_size"] = glyphs[i].font_size;
  1881. glyph["index"] = glyphs[i].index;
  1882. ret.push_back(glyph);
  1883. }
  1884. return ret;
  1885. }
  1886. TypedArray<Dictionary> TextServer::_shaped_text_get_ellipsis_glyphs_wrapper(const RID &p_shaped) const {
  1887. TypedArray<Dictionary> ret;
  1888. const Glyph *glyphs = shaped_text_get_ellipsis_glyphs(p_shaped);
  1889. int gl_size = shaped_text_get_ellipsis_glyph_count(p_shaped);
  1890. for (int i = 0; i < gl_size; i++) {
  1891. Dictionary glyph;
  1892. glyph["start"] = glyphs[i].start;
  1893. glyph["end"] = glyphs[i].end;
  1894. glyph["repeat"] = glyphs[i].repeat;
  1895. glyph["count"] = glyphs[i].count;
  1896. glyph["flags"] = glyphs[i].flags;
  1897. glyph["offset"] = Vector2(glyphs[i].x_off, glyphs[i].y_off);
  1898. glyph["advance"] = glyphs[i].advance;
  1899. glyph["font_rid"] = glyphs[i].font_rid;
  1900. glyph["font_size"] = glyphs[i].font_size;
  1901. glyph["index"] = glyphs[i].index;
  1902. ret.push_back(glyph);
  1903. }
  1904. return ret;
  1905. }
  1906. bool TextServer::is_valid_identifier(const String &p_string) const {
  1907. return p_string.is_valid_unicode_identifier();
  1908. }
  1909. bool TextServer::is_valid_letter(uint64_t p_unicode) const {
  1910. return is_unicode_letter(p_unicode);
  1911. }
  1912. TextServer::TextServer() {
  1913. _init_diacritics_map();
  1914. }
  1915. TextServer::~TextServer() {
  1916. }