text_server.cpp 97 KB

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