text_server.cpp 102 KB

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