text_server.cpp 86 KB

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