text_server.cpp 73 KB

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