text_server.cpp 103 KB

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