text_server.cpp 86 KB

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