text_server.cpp 95 KB

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