text_server.cpp 64 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565
  1. /*************************************************************************/
  2. /* text_server.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2021 Godot Engine contributors (cf. AUTHORS.md). */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /*************************************************************************/
  30. #include "servers/text_server.h"
  31. #include "servers/rendering_server.h"
  32. TextServerManager *TextServerManager::singleton = nullptr;
  33. void TextServerManager::_bind_methods() {
  34. ClassDB::bind_method(D_METHOD("add_interface", "interface"), &TextServerManager::add_interface);
  35. ClassDB::bind_method(D_METHOD("get_interface_count"), &TextServerManager::get_interface_count);
  36. ClassDB::bind_method(D_METHOD("remove_interface", "interface"), &TextServerManager::remove_interface);
  37. ClassDB::bind_method(D_METHOD("get_interface", "idx"), &TextServerManager::get_interface);
  38. ClassDB::bind_method(D_METHOD("get_interfaces"), &TextServerManager::get_interfaces);
  39. ClassDB::bind_method(D_METHOD("find_interface", "name"), &TextServerManager::find_interface);
  40. ClassDB::bind_method(D_METHOD("set_primary_interface", "index"), &TextServerManager::set_primary_interface);
  41. ClassDB::bind_method(D_METHOD("get_primary_interface"), &TextServerManager::get_primary_interface);
  42. ADD_SIGNAL(MethodInfo("interface_added", PropertyInfo(Variant::STRING_NAME, "interface_name")));
  43. ADD_SIGNAL(MethodInfo("interface_removed", PropertyInfo(Variant::STRING_NAME, "interface_name")));
  44. }
  45. void TextServerManager::add_interface(const Ref<TextServer> &p_interface) {
  46. ERR_FAIL_COND(p_interface.is_null());
  47. for (int i = 0; i < interfaces.size(); i++) {
  48. if (interfaces[i] == p_interface) {
  49. ERR_PRINT("TextServer: Interface was already added.");
  50. return;
  51. };
  52. };
  53. interfaces.push_back(p_interface);
  54. print_verbose("TextServer: Added interface \"" + p_interface->get_name() + "\"");
  55. emit_signal(SNAME("interface_added"), p_interface->get_name());
  56. }
  57. void TextServerManager::remove_interface(const Ref<TextServer> &p_interface) {
  58. ERR_FAIL_COND(p_interface.is_null());
  59. ERR_FAIL_COND_MSG(p_interface == primary_interface, "TextServer: Can't remove primary interface.");
  60. int idx = -1;
  61. for (int i = 0; i < interfaces.size(); i++) {
  62. if (interfaces[i] == p_interface) {
  63. idx = i;
  64. break;
  65. };
  66. };
  67. ERR_FAIL_COND(idx == -1);
  68. print_verbose("TextServer: Removed interface \"" + p_interface->get_name() + "\"");
  69. emit_signal(SNAME("interface_removed"), p_interface->get_name());
  70. interfaces.remove_at(idx);
  71. }
  72. int TextServerManager::get_interface_count() const {
  73. return interfaces.size();
  74. }
  75. Ref<TextServer> TextServerManager::get_interface(int p_index) const {
  76. ERR_FAIL_INDEX_V(p_index, interfaces.size(), nullptr);
  77. return interfaces[p_index];
  78. }
  79. Ref<TextServer> TextServerManager::find_interface(const String &p_name) const {
  80. int idx = -1;
  81. for (int i = 0; i < interfaces.size(); i++) {
  82. if (interfaces[i]->get_name() == p_name) {
  83. idx = i;
  84. break;
  85. };
  86. };
  87. ERR_FAIL_COND_V(idx == -1, nullptr);
  88. return interfaces[idx];
  89. }
  90. Array TextServerManager::get_interfaces() const {
  91. Array ret;
  92. for (int i = 0; i < interfaces.size(); i++) {
  93. Dictionary iface_info;
  94. iface_info["id"] = i;
  95. iface_info["name"] = interfaces[i]->get_name();
  96. ret.push_back(iface_info);
  97. };
  98. return ret;
  99. }
  100. void TextServerManager::set_primary_interface(const Ref<TextServer> &p_primary_interface) {
  101. if (p_primary_interface.is_null()) {
  102. print_verbose("TextServer: Clearing primary interface");
  103. primary_interface.unref();
  104. } else {
  105. primary_interface = p_primary_interface;
  106. print_verbose("TextServer: Primary interface set to: \"" + primary_interface->get_name() + "\".");
  107. if (OS::get_singleton()->get_main_loop()) {
  108. OS::get_singleton()->get_main_loop()->notification(MainLoop::NOTIFICATION_TEXT_SERVER_CHANGED);
  109. }
  110. }
  111. }
  112. TextServerManager::TextServerManager() {
  113. singleton = this;
  114. }
  115. TextServerManager::~TextServerManager() {
  116. if (primary_interface.is_valid()) {
  117. primary_interface.unref();
  118. }
  119. while (interfaces.size() > 0) {
  120. interfaces.remove_at(0);
  121. }
  122. singleton = nullptr;
  123. }
  124. /*************************************************************************/
  125. bool Glyph::operator==(const Glyph &p_a) const {
  126. return (p_a.index == index) && (p_a.font_rid == font_rid) && (p_a.font_size == font_size) && (p_a.start == start);
  127. }
  128. bool Glyph::operator!=(const Glyph &p_a) const {
  129. return (p_a.index != index) || (p_a.font_rid != font_rid) || (p_a.font_size != font_size) || (p_a.start != start);
  130. }
  131. bool Glyph::operator<(const Glyph &p_a) const {
  132. if (p_a.start == start) {
  133. if (p_a.count == count) {
  134. if ((p_a.flags & TextServer::GRAPHEME_IS_VIRTUAL) == TextServer::GRAPHEME_IS_VIRTUAL) {
  135. return true;
  136. } else {
  137. return false;
  138. }
  139. }
  140. return p_a.count > count;
  141. }
  142. return p_a.start < start;
  143. }
  144. bool Glyph::operator>(const Glyph &p_a) const {
  145. if (p_a.start == start) {
  146. if (p_a.count == count) {
  147. if ((p_a.flags & TextServer::GRAPHEME_IS_VIRTUAL) == TextServer::GRAPHEME_IS_VIRTUAL) {
  148. return false;
  149. } else {
  150. return true;
  151. }
  152. }
  153. return p_a.count < count;
  154. }
  155. return p_a.start > start;
  156. }
  157. void TextServer::_bind_methods() {
  158. ClassDB::bind_method(D_METHOD("has_feature", "feature"), &TextServer::has_feature);
  159. ClassDB::bind_method(D_METHOD("get_name"), &TextServer::get_name);
  160. ClassDB::bind_method(D_METHOD("get_features"), &TextServer::get_features);
  161. ClassDB::bind_method(D_METHOD("load_support_data", "filename"), &TextServer::load_support_data);
  162. ClassDB::bind_method(D_METHOD("get_support_data_filename"), &TextServer::get_support_data_filename);
  163. ClassDB::bind_method(D_METHOD("get_support_data_info"), &TextServer::get_support_data_info);
  164. ClassDB::bind_method(D_METHOD("save_support_data", "filename"), &TextServer::save_support_data);
  165. ClassDB::bind_method(D_METHOD("is_locale_right_to_left", "locale"), &TextServer::is_locale_right_to_left);
  166. ClassDB::bind_method(D_METHOD("name_to_tag", "name"), &TextServer::name_to_tag);
  167. ClassDB::bind_method(D_METHOD("tag_to_name", "tag"), &TextServer::tag_to_name);
  168. ClassDB::bind_method(D_METHOD("has", "rid"), &TextServer::has);
  169. ClassDB::bind_method(D_METHOD("free_rid", "rid"), &TextServer::free); // shouldn't conflict with Object::free()
  170. /* Font Interface */
  171. ClassDB::bind_method(D_METHOD("create_font"), &TextServer::create_font);
  172. ClassDB::bind_method(D_METHOD("font_set_data", "font_rid", "data"), &TextServer::font_set_data);
  173. ClassDB::bind_method(D_METHOD("font_set_style", "font_rid", "style"), &TextServer::font_set_style);
  174. ClassDB::bind_method(D_METHOD("font_get_style", "font_rid"), &TextServer::font_get_style);
  175. ClassDB::bind_method(D_METHOD("font_set_name", "font_rid", "name"), &TextServer::font_set_name);
  176. ClassDB::bind_method(D_METHOD("font_get_name", "font_rid"), &TextServer::font_get_name);
  177. ClassDB::bind_method(D_METHOD("font_set_style_name", "font_rid", "name"), &TextServer::font_set_style_name);
  178. ClassDB::bind_method(D_METHOD("font_get_style_name", "font_rid"), &TextServer::font_get_style_name);
  179. ClassDB::bind_method(D_METHOD("font_set_antialiased", "font_rid", "antialiased"), &TextServer::font_set_antialiased);
  180. ClassDB::bind_method(D_METHOD("font_is_antialiased", "font_rid"), &TextServer::font_is_antialiased);
  181. ClassDB::bind_method(D_METHOD("font_set_multichannel_signed_distance_field", "font_rid", "msdf"), &TextServer::font_set_multichannel_signed_distance_field);
  182. ClassDB::bind_method(D_METHOD("font_is_multichannel_signed_distance_field", "font_rid"), &TextServer::font_is_multichannel_signed_distance_field);
  183. ClassDB::bind_method(D_METHOD("font_set_msdf_pixel_range", "font_rid", "msdf_pixel_range"), &TextServer::font_set_msdf_pixel_range);
  184. ClassDB::bind_method(D_METHOD("font_get_msdf_pixel_range", "font_rid"), &TextServer::font_get_msdf_pixel_range);
  185. ClassDB::bind_method(D_METHOD("font_set_msdf_size", "font_rid", "msdf_size"), &TextServer::font_set_msdf_size);
  186. ClassDB::bind_method(D_METHOD("font_get_msdf_size", "font_rid"), &TextServer::font_get_msdf_size);
  187. ClassDB::bind_method(D_METHOD("font_set_fixed_size", "font_rid", "fixed_size"), &TextServer::font_set_fixed_size);
  188. ClassDB::bind_method(D_METHOD("font_get_fixed_size", "font_rid"), &TextServer::font_get_fixed_size);
  189. ClassDB::bind_method(D_METHOD("font_set_force_autohinter", "font_rid", "force_autohinter"), &TextServer::font_set_force_autohinter);
  190. ClassDB::bind_method(D_METHOD("font_is_force_autohinter", "font_rid"), &TextServer::font_is_force_autohinter);
  191. ClassDB::bind_method(D_METHOD("font_set_hinting", "font_rid", "_hinting"), &TextServer::font_set_hinting);
  192. ClassDB::bind_method(D_METHOD("font_get_hinting", "font_rid"), &TextServer::font_get_hinting);
  193. ClassDB::bind_method(D_METHOD("font_set_variation_coordinates", "font_rid", "variation_coordinates"), &TextServer::font_set_variation_coordinates);
  194. ClassDB::bind_method(D_METHOD("font_get_variation_coordinates", "font_rid"), &TextServer::font_get_variation_coordinates);
  195. ClassDB::bind_method(D_METHOD("font_set_oversampling", "font_rid", "oversampling"), &TextServer::font_set_oversampling);
  196. ClassDB::bind_method(D_METHOD("font_get_oversampling", "font_rid"), &TextServer::font_get_oversampling);
  197. ClassDB::bind_method(D_METHOD("font_get_size_cache_list", "font_rid"), &TextServer::font_get_size_cache_list);
  198. ClassDB::bind_method(D_METHOD("font_clear_size_cache", "font_rid"), &TextServer::font_clear_size_cache);
  199. ClassDB::bind_method(D_METHOD("font_remove_size_cache", "font_rid", "size"), &TextServer::font_remove_size_cache);
  200. ClassDB::bind_method(D_METHOD("font_set_ascent", "font_rid", "size", "ascent"), &TextServer::font_set_ascent);
  201. ClassDB::bind_method(D_METHOD("font_get_ascent", "font_rid", "size"), &TextServer::font_get_ascent);
  202. ClassDB::bind_method(D_METHOD("font_set_descent", "font_rid", "size", "descent"), &TextServer::font_set_descent);
  203. ClassDB::bind_method(D_METHOD("font_get_descent", "font_rid", "size"), &TextServer::font_get_descent);
  204. ClassDB::bind_method(D_METHOD("font_set_underline_position", "font_rid", "size", "underline_position"), &TextServer::font_set_underline_position);
  205. ClassDB::bind_method(D_METHOD("font_get_underline_position", "font_rid", "size"), &TextServer::font_get_underline_position);
  206. ClassDB::bind_method(D_METHOD("font_set_underline_thickness", "font_rid", "size", "underline_thickness"), &TextServer::font_set_underline_thickness);
  207. ClassDB::bind_method(D_METHOD("font_get_underline_thickness", "font_rid", "size"), &TextServer::font_get_underline_thickness);
  208. ClassDB::bind_method(D_METHOD("font_set_scale", "font_rid", "size", "scale"), &TextServer::font_set_scale);
  209. ClassDB::bind_method(D_METHOD("font_get_scale", "font_rid", "size"), &TextServer::font_get_scale);
  210. ClassDB::bind_method(D_METHOD("font_set_spacing", "font_rid", "size", "spacing", "value"), &TextServer::font_set_spacing);
  211. ClassDB::bind_method(D_METHOD("font_get_spacing", "font_rid", "size", "spacing"), &TextServer::font_get_spacing);
  212. ClassDB::bind_method(D_METHOD("font_get_texture_count", "font_rid", "size"), &TextServer::font_get_texture_count);
  213. ClassDB::bind_method(D_METHOD("font_clear_textures", "font_rid", "size"), &TextServer::font_clear_textures);
  214. ClassDB::bind_method(D_METHOD("font_remove_texture", "font_rid", "size", "texture_index"), &TextServer::font_remove_texture);
  215. ClassDB::bind_method(D_METHOD("font_set_texture_image", "font_rid", "size", "texture_index", "image"), &TextServer::font_set_texture_image);
  216. ClassDB::bind_method(D_METHOD("font_get_texture_image", "font_rid", "size", "texture_index"), &TextServer::font_get_texture_image);
  217. ClassDB::bind_method(D_METHOD("font_set_texture_offsets", "font_rid", "size", "texture_index", "offset"), &TextServer::font_set_texture_offsets);
  218. ClassDB::bind_method(D_METHOD("font_get_texture_offsets", "font_rid", "size", "texture_index"), &TextServer::font_get_texture_offsets);
  219. ClassDB::bind_method(D_METHOD("font_get_glyph_list", "font_rid", "size"), &TextServer::font_get_glyph_list);
  220. ClassDB::bind_method(D_METHOD("font_clear_glyphs", "font_rid", "size"), &TextServer::font_clear_glyphs);
  221. ClassDB::bind_method(D_METHOD("font_remove_glyph", "font_rid", "size", "glyph"), &TextServer::font_remove_glyph);
  222. ClassDB::bind_method(D_METHOD("font_get_glyph_advance", "font_rid", "size", "glyph"), &TextServer::font_get_glyph_advance);
  223. ClassDB::bind_method(D_METHOD("font_set_glyph_advance", "font_rid", "size", "glyph", "advance"), &TextServer::font_set_glyph_advance);
  224. ClassDB::bind_method(D_METHOD("font_get_glyph_offset", "font_rid", "size", "glyph"), &TextServer::font_get_glyph_offset);
  225. ClassDB::bind_method(D_METHOD("font_set_glyph_offset", "font_rid", "size", "glyph", "offset"), &TextServer::font_set_glyph_offset);
  226. ClassDB::bind_method(D_METHOD("font_get_glyph_size", "font_rid", "size", "glyph"), &TextServer::font_get_glyph_size);
  227. ClassDB::bind_method(D_METHOD("font_set_glyph_size", "font_rid", "size", "glyph", "gl_size"), &TextServer::font_set_glyph_size);
  228. ClassDB::bind_method(D_METHOD("font_get_glyph_uv_rect", "font_rid", "size", "glyph"), &TextServer::font_get_glyph_uv_rect);
  229. ClassDB::bind_method(D_METHOD("font_set_glyph_uv_rect", "font_rid", "size", "glyph", "uv_rect"), &TextServer::font_set_glyph_uv_rect);
  230. ClassDB::bind_method(D_METHOD("font_get_glyph_texture_idx", "font_rid", "size", "glyph"), &TextServer::font_get_glyph_texture_idx);
  231. ClassDB::bind_method(D_METHOD("font_set_glyph_texture_idx", "font_rid", "size", "glyph", "texture_idx"), &TextServer::font_set_glyph_texture_idx);
  232. ClassDB::bind_method(D_METHOD("font_get_glyph_contours", "font", "size", "index"), &TextServer::font_get_glyph_contours);
  233. ClassDB::bind_method(D_METHOD("font_get_kerning_list", "font_rid", "size"), &TextServer::font_get_kerning_list);
  234. ClassDB::bind_method(D_METHOD("font_clear_kerning_map", "font_rid", "size"), &TextServer::font_clear_kerning_map);
  235. ClassDB::bind_method(D_METHOD("font_remove_kerning", "font_rid", "size", "glyph_pair"), &TextServer::font_remove_kerning);
  236. ClassDB::bind_method(D_METHOD("font_set_kerning", "font_rid", "size", "glyph_pair", "kerning"), &TextServer::font_set_kerning);
  237. ClassDB::bind_method(D_METHOD("font_get_kerning", "font_rid", "size", "glyph_pair"), &TextServer::font_get_kerning);
  238. ClassDB::bind_method(D_METHOD("font_get_glyph_index", "font_rid", "size", "char", "variation_selector"), &TextServer::font_get_glyph_index);
  239. ClassDB::bind_method(D_METHOD("font_has_char", "font_rid", "char"), &TextServer::font_has_char);
  240. ClassDB::bind_method(D_METHOD("font_get_supported_chars", "font_rid"), &TextServer::font_get_supported_chars);
  241. ClassDB::bind_method(D_METHOD("font_render_range", "font_rid", "size", "start", "end"), &TextServer::font_render_range);
  242. ClassDB::bind_method(D_METHOD("font_render_glyph", "font_rid", "size", "index"), &TextServer::font_render_glyph);
  243. ClassDB::bind_method(D_METHOD("font_draw_glyph", "font_rid", "canvas", "size", "pos", "index", "color"), &TextServer::font_draw_glyph, DEFVAL(Color(1, 1, 1)));
  244. ClassDB::bind_method(D_METHOD("font_draw_glyph_outline", "font_rid", "canvas", "size", "outline_size", "pos", "index", "color"), &TextServer::font_draw_glyph_outline, DEFVAL(Color(1, 1, 1)));
  245. ClassDB::bind_method(D_METHOD("font_is_language_supported", "font_rid", "language"), &TextServer::font_is_language_supported);
  246. ClassDB::bind_method(D_METHOD("font_set_language_support_override", "font_rid", "language", "supported"), &TextServer::font_set_language_support_override);
  247. ClassDB::bind_method(D_METHOD("font_get_language_support_override", "font_rid", "language"), &TextServer::font_get_language_support_override);
  248. ClassDB::bind_method(D_METHOD("font_remove_language_support_override", "font_rid", "language"), &TextServer::font_remove_language_support_override);
  249. ClassDB::bind_method(D_METHOD("font_get_language_support_overrides", "font_rid"), &TextServer::font_get_language_support_overrides);
  250. ClassDB::bind_method(D_METHOD("font_is_script_supported", "font_rid", "script"), &TextServer::font_is_script_supported);
  251. ClassDB::bind_method(D_METHOD("font_set_script_support_override", "font_rid", "script", "supported"), &TextServer::font_set_script_support_override);
  252. ClassDB::bind_method(D_METHOD("font_get_script_support_override", "font_rid", "script"), &TextServer::font_get_script_support_override);
  253. ClassDB::bind_method(D_METHOD("font_remove_script_support_override", "font_rid", "script"), &TextServer::font_remove_script_support_override);
  254. ClassDB::bind_method(D_METHOD("font_get_script_support_overrides", "font_rid"), &TextServer::font_get_script_support_overrides);
  255. ClassDB::bind_method(D_METHOD("font_supported_feature_list", "font_rid"), &TextServer::font_supported_feature_list);
  256. ClassDB::bind_method(D_METHOD("font_supported_variation_list", "font_rid"), &TextServer::font_supported_variation_list);
  257. ClassDB::bind_method(D_METHOD("font_get_global_oversampling"), &TextServer::font_get_global_oversampling);
  258. ClassDB::bind_method(D_METHOD("font_set_global_oversampling", "oversampling"), &TextServer::font_set_global_oversampling);
  259. ClassDB::bind_method(D_METHOD("get_hex_code_box_size", "size", "index"), &TextServer::get_hex_code_box_size);
  260. ClassDB::bind_method(D_METHOD("draw_hex_code_box", "canvas", "size", "pos", "index", "color"), &TextServer::draw_hex_code_box);
  261. /* Shaped text buffer interface */
  262. ClassDB::bind_method(D_METHOD("create_shaped_text", "direction", "orientation"), &TextServer::create_shaped_text, DEFVAL(DIRECTION_AUTO), DEFVAL(ORIENTATION_HORIZONTAL));
  263. ClassDB::bind_method(D_METHOD("shaped_text_clear", "rid"), &TextServer::shaped_text_clear);
  264. ClassDB::bind_method(D_METHOD("shaped_text_set_direction", "shaped", "direction"), &TextServer::shaped_text_set_direction, DEFVAL(DIRECTION_AUTO));
  265. ClassDB::bind_method(D_METHOD("shaped_text_get_direction", "shaped"), &TextServer::shaped_text_get_direction);
  266. ClassDB::bind_method(D_METHOD("shaped_text_set_bidi_override", "shaped", "override"), &TextServer::shaped_text_set_bidi_override);
  267. ClassDB::bind_method(D_METHOD("shaped_text_set_custom_punctuation", "shaped", "punct"), &TextServer::shaped_text_set_custom_punctuation);
  268. ClassDB::bind_method(D_METHOD("shaped_text_get_custom_punctuation", "shaped"), &TextServer::shaped_text_get_custom_punctuation);
  269. ClassDB::bind_method(D_METHOD("shaped_text_set_orientation", "shaped", "orientation"), &TextServer::shaped_text_set_orientation, DEFVAL(ORIENTATION_HORIZONTAL));
  270. ClassDB::bind_method(D_METHOD("shaped_text_get_orientation", "shaped"), &TextServer::shaped_text_get_orientation);
  271. ClassDB::bind_method(D_METHOD("shaped_text_set_preserve_invalid", "shaped", "enabled"), &TextServer::shaped_text_set_preserve_invalid);
  272. ClassDB::bind_method(D_METHOD("shaped_text_get_preserve_invalid", "shaped"), &TextServer::shaped_text_get_preserve_invalid);
  273. ClassDB::bind_method(D_METHOD("shaped_text_set_preserve_control", "shaped", "enabled"), &TextServer::shaped_text_set_preserve_control);
  274. ClassDB::bind_method(D_METHOD("shaped_text_get_preserve_control", "shaped"), &TextServer::shaped_text_get_preserve_control);
  275. ClassDB::bind_method(D_METHOD("shaped_text_add_string", "shaped", "text", "fonts", "size", "opentype_features", "language"), &TextServer::shaped_text_add_string, DEFVAL(Dictionary()), DEFVAL(""));
  276. ClassDB::bind_method(D_METHOD("shaped_text_add_object", "shaped", "key", "size", "inline_align", "length"), &TextServer::shaped_text_add_object, DEFVAL(INLINE_ALIGNMENT_CENTER), DEFVAL(1));
  277. ClassDB::bind_method(D_METHOD("shaped_text_resize_object", "shaped", "key", "size", "inline_align"), &TextServer::shaped_text_resize_object, DEFVAL(INLINE_ALIGNMENT_CENTER));
  278. ClassDB::bind_method(D_METHOD("shaped_text_substr", "shaped", "start", "length"), &TextServer::shaped_text_substr);
  279. ClassDB::bind_method(D_METHOD("shaped_text_get_parent", "shaped"), &TextServer::shaped_text_get_parent);
  280. ClassDB::bind_method(D_METHOD("shaped_text_fit_to_width", "shaped", "width", "jst_flags"), &TextServer::shaped_text_fit_to_width, DEFVAL(JUSTIFICATION_WORD_BOUND | JUSTIFICATION_KASHIDA));
  281. ClassDB::bind_method(D_METHOD("shaped_text_tab_align", "shaped", "tab_stops"), &TextServer::shaped_text_tab_align);
  282. ClassDB::bind_method(D_METHOD("shaped_text_shape", "shaped"), &TextServer::shaped_text_shape);
  283. ClassDB::bind_method(D_METHOD("shaped_text_is_ready", "shaped"), &TextServer::shaped_text_is_ready);
  284. ClassDB::bind_method(D_METHOD("shaped_text_get_glyphs", "shaped"), &TextServer::_shaped_text_get_glyphs_wrapper);
  285. ClassDB::bind_method(D_METHOD("shaped_text_sort_logical", "shaped"), &TextServer::_shaped_text_sort_logical_wrapper);
  286. ClassDB::bind_method(D_METHOD("shaped_text_get_glyph_count", "shaped"), &TextServer::shaped_text_get_glyph_count);
  287. ClassDB::bind_method(D_METHOD("shaped_text_get_range", "shaped"), &TextServer::shaped_text_get_range);
  288. 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));
  289. 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));
  290. ClassDB::bind_method(D_METHOD("shaped_text_get_word_breaks", "shaped", "grapheme_flags"), &TextServer::shaped_text_get_word_breaks);
  291. ClassDB::bind_method(D_METHOD("shaped_text_get_trim_pos", "shaped"), &TextServer::shaped_text_get_trim_pos);
  292. ClassDB::bind_method(D_METHOD("shaped_text_get_ellipsis_pos", "shaped"), &TextServer::shaped_text_get_ellipsis_pos);
  293. ClassDB::bind_method(D_METHOD("shaped_text_get_ellipsis_glyphs", "shaped"), &TextServer::_shaped_text_get_ellipsis_glyphs_wrapper);
  294. ClassDB::bind_method(D_METHOD("shaped_text_get_ellipsis_glyph_count", "shaped"), &TextServer::shaped_text_get_ellipsis_glyph_count);
  295. 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_TRIMMING));
  296. ClassDB::bind_method(D_METHOD("shaped_text_get_objects", "shaped"), &TextServer::shaped_text_get_objects);
  297. ClassDB::bind_method(D_METHOD("shaped_text_get_object_rect", "shaped", "key"), &TextServer::shaped_text_get_object_rect);
  298. ClassDB::bind_method(D_METHOD("shaped_text_get_size", "shaped"), &TextServer::shaped_text_get_size);
  299. ClassDB::bind_method(D_METHOD("shaped_text_get_ascent", "shaped"), &TextServer::shaped_text_get_ascent);
  300. ClassDB::bind_method(D_METHOD("shaped_text_get_descent", "shaped"), &TextServer::shaped_text_get_descent);
  301. ClassDB::bind_method(D_METHOD("shaped_text_get_width", "shaped"), &TextServer::shaped_text_get_width);
  302. ClassDB::bind_method(D_METHOD("shaped_text_get_underline_position", "shaped"), &TextServer::shaped_text_get_underline_position);
  303. ClassDB::bind_method(D_METHOD("shaped_text_get_underline_thickness", "shaped"), &TextServer::shaped_text_get_underline_thickness);
  304. ClassDB::bind_method(D_METHOD("shaped_text_get_carets", "shaped", "position"), &TextServer::_shaped_text_get_carets_wrapper);
  305. ClassDB::bind_method(D_METHOD("shaped_text_get_selection", "shaped", "start", "end"), &TextServer::shaped_text_get_selection);
  306. ClassDB::bind_method(D_METHOD("shaped_text_hit_test_grapheme", "shaped", "coords"), &TextServer::shaped_text_hit_test_grapheme);
  307. ClassDB::bind_method(D_METHOD("shaped_text_hit_test_position", "shaped", "coords"), &TextServer::shaped_text_hit_test_position);
  308. ClassDB::bind_method(D_METHOD("shaped_text_get_grapheme_bounds", "shaped", "pos"), &TextServer::shaped_text_get_grapheme_bounds);
  309. ClassDB::bind_method(D_METHOD("shaped_text_next_grapheme_pos", "shaped", "pos"), &TextServer::shaped_text_next_grapheme_pos);
  310. ClassDB::bind_method(D_METHOD("shaped_text_prev_grapheme_pos", "shaped", "pos"), &TextServer::shaped_text_prev_grapheme_pos);
  311. ClassDB::bind_method(D_METHOD("shaped_text_draw", "shaped", "canvas", "pos", "clip_l", "clip_r", "color"), &TextServer::shaped_text_draw, DEFVAL(-1), DEFVAL(-1), DEFVAL(Color(1, 1, 1)));
  312. ClassDB::bind_method(D_METHOD("shaped_text_draw_outline", "shaped", "canvas", "pos", "clip_l", "clip_r", "outline_size", "color"), &TextServer::shaped_text_draw_outline, DEFVAL(-1), DEFVAL(-1), DEFVAL(1), DEFVAL(Color(1, 1, 1)));
  313. ClassDB::bind_method(D_METHOD("shaped_text_get_dominant_direction_in_range", "shaped", "start", "end"), &TextServer::shaped_text_get_dominant_direction_in_range);
  314. ClassDB::bind_method(D_METHOD("format_number", "number", "language"), &TextServer::format_number, DEFVAL(""));
  315. ClassDB::bind_method(D_METHOD("parse_number", "number", "language"), &TextServer::parse_number, DEFVAL(""));
  316. ClassDB::bind_method(D_METHOD("percent_sign", "language"), &TextServer::percent_sign, DEFVAL(""));
  317. ClassDB::bind_method(D_METHOD("strip_diacritics", "string"), &TextServer::strip_diacritics);
  318. /* Direction */
  319. BIND_ENUM_CONSTANT(DIRECTION_AUTO);
  320. BIND_ENUM_CONSTANT(DIRECTION_LTR);
  321. BIND_ENUM_CONSTANT(DIRECTION_RTL);
  322. /* Orientation */
  323. BIND_ENUM_CONSTANT(ORIENTATION_HORIZONTAL);
  324. BIND_ENUM_CONSTANT(ORIENTATION_VERTICAL);
  325. /* JustificationFlag */
  326. BIND_ENUM_CONSTANT(JUSTIFICATION_NONE);
  327. BIND_ENUM_CONSTANT(JUSTIFICATION_KASHIDA);
  328. BIND_ENUM_CONSTANT(JUSTIFICATION_WORD_BOUND);
  329. BIND_ENUM_CONSTANT(JUSTIFICATION_TRIM_EDGE_SPACES);
  330. BIND_ENUM_CONSTANT(JUSTIFICATION_AFTER_LAST_TAB);
  331. BIND_ENUM_CONSTANT(JUSTIFICATION_CONSTRAIN_ELLIPSIS);
  332. /* LineBreakFlag */
  333. BIND_ENUM_CONSTANT(BREAK_NONE);
  334. BIND_ENUM_CONSTANT(BREAK_MANDATORY);
  335. BIND_ENUM_CONSTANT(BREAK_WORD_BOUND);
  336. BIND_ENUM_CONSTANT(BREAK_GRAPHEME_BOUND);
  337. BIND_ENUM_CONSTANT(BREAK_WORD_BOUND_ADAPTIVE);
  338. /* TextOverrunFlag */
  339. BIND_ENUM_CONSTANT(OVERRUN_NO_TRIMMING);
  340. BIND_ENUM_CONSTANT(OVERRUN_TRIM);
  341. BIND_ENUM_CONSTANT(OVERRUN_TRIM_WORD_ONLY);
  342. BIND_ENUM_CONSTANT(OVERRUN_ADD_ELLIPSIS);
  343. BIND_ENUM_CONSTANT(OVERRUN_ENFORCE_ELLIPSIS);
  344. BIND_ENUM_CONSTANT(OVERRUN_JUSTIFICATION_AWARE);
  345. /* GraphemeFlag */
  346. BIND_ENUM_CONSTANT(GRAPHEME_IS_VALID);
  347. BIND_ENUM_CONSTANT(GRAPHEME_IS_RTL);
  348. BIND_ENUM_CONSTANT(GRAPHEME_IS_VIRTUAL);
  349. BIND_ENUM_CONSTANT(GRAPHEME_IS_SPACE);
  350. BIND_ENUM_CONSTANT(GRAPHEME_IS_BREAK_HARD);
  351. BIND_ENUM_CONSTANT(GRAPHEME_IS_BREAK_SOFT);
  352. BIND_ENUM_CONSTANT(GRAPHEME_IS_TAB);
  353. BIND_ENUM_CONSTANT(GRAPHEME_IS_ELONGATION);
  354. BIND_ENUM_CONSTANT(GRAPHEME_IS_PUNCTUATION);
  355. BIND_ENUM_CONSTANT(GRAPHEME_IS_UNDERSCORE);
  356. BIND_ENUM_CONSTANT(GRAPHEME_IS_CONNECTED);
  357. /* Hinting */
  358. BIND_ENUM_CONSTANT(HINTING_NONE);
  359. BIND_ENUM_CONSTANT(HINTING_LIGHT);
  360. BIND_ENUM_CONSTANT(HINTING_NORMAL);
  361. /* Feature */
  362. BIND_ENUM_CONSTANT(FEATURE_BIDI_LAYOUT);
  363. BIND_ENUM_CONSTANT(FEATURE_VERTICAL_LAYOUT);
  364. BIND_ENUM_CONSTANT(FEATURE_SHAPING);
  365. BIND_ENUM_CONSTANT(FEATURE_KASHIDA_JUSTIFICATION);
  366. BIND_ENUM_CONSTANT(FEATURE_BREAK_ITERATORS);
  367. BIND_ENUM_CONSTANT(FEATURE_FONT_SYSTEM);
  368. BIND_ENUM_CONSTANT(FEATURE_FONT_VARIABLE);
  369. BIND_ENUM_CONSTANT(FEATURE_USE_SUPPORT_DATA);
  370. /* FT Contour Point Types */
  371. BIND_ENUM_CONSTANT(CONTOUR_CURVE_TAG_ON);
  372. BIND_ENUM_CONSTANT(CONTOUR_CURVE_TAG_OFF_CONIC);
  373. BIND_ENUM_CONSTANT(CONTOUR_CURVE_TAG_OFF_CUBIC);
  374. /* Font Spacing */
  375. BIND_ENUM_CONSTANT(SPACING_GLYPH);
  376. BIND_ENUM_CONSTANT(SPACING_SPACE);
  377. BIND_ENUM_CONSTANT(SPACING_TOP);
  378. BIND_ENUM_CONSTANT(SPACING_BOTTOM);
  379. /* Font Style */
  380. BIND_ENUM_CONSTANT(FONT_BOLD);
  381. BIND_ENUM_CONSTANT(FONT_ITALIC);
  382. BIND_ENUM_CONSTANT(FONT_FIXED_WIDTH);
  383. }
  384. Vector2 TextServer::get_hex_code_box_size(int p_size, char32_t p_index) const {
  385. int w = ((p_index <= 0xFF) ? 1 : ((p_index <= 0xFFFF) ? 2 : 3));
  386. int sp = MAX(0, w - 1);
  387. int sz = MAX(1, Math::round(p_size / 15.f));
  388. return Vector2(4 + 3 * w + sp + 1, 15) * sz;
  389. }
  390. void TextServer::_draw_hex_code_box_number(RID p_canvas, int p_size, const Vector2 &p_pos, uint8_t p_index, const Color &p_color) const {
  391. static uint8_t chars[] = { 0x7E, 0x30, 0x6D, 0x79, 0x33, 0x5B, 0x5F, 0x70, 0x7F, 0x7B, 0x77, 0x1F, 0x4E, 0x3D, 0x4F, 0x47, 0x00 };
  392. uint8_t x = chars[p_index];
  393. if (x & (1 << 6)) {
  394. RenderingServer::get_singleton()->canvas_item_add_rect(p_canvas, Rect2(p_pos, Size2(3, 1) * p_size), p_color);
  395. }
  396. if (x & (1 << 5)) {
  397. RenderingServer::get_singleton()->canvas_item_add_rect(p_canvas, Rect2(p_pos + Point2(2, 0) * p_size, Size2(1, 3) * p_size), p_color);
  398. }
  399. if (x & (1 << 4)) {
  400. RenderingServer::get_singleton()->canvas_item_add_rect(p_canvas, Rect2(p_pos + Point2(2, 2) * p_size, Size2(1, 3) * p_size), p_color);
  401. }
  402. if (x & (1 << 3)) {
  403. RenderingServer::get_singleton()->canvas_item_add_rect(p_canvas, Rect2(p_pos + Point2(0, 4) * p_size, Size2(3, 1) * p_size), p_color);
  404. }
  405. if (x & (1 << 2)) {
  406. RenderingServer::get_singleton()->canvas_item_add_rect(p_canvas, Rect2(p_pos + Point2(0, 2) * p_size, Size2(1, 3) * p_size), p_color);
  407. }
  408. if (x & (1 << 1)) {
  409. RenderingServer::get_singleton()->canvas_item_add_rect(p_canvas, Rect2(p_pos, Size2(1, 3) * p_size), p_color);
  410. }
  411. if (x & (1 << 0)) {
  412. RenderingServer::get_singleton()->canvas_item_add_rect(p_canvas, Rect2(p_pos + Point2(0, 2) * p_size, Size2(3, 1) * p_size), p_color);
  413. }
  414. }
  415. void TextServer::draw_hex_code_box(RID p_canvas, int p_size, const Vector2 &p_pos, char32_t p_index, const Color &p_color) const {
  416. if (p_index == 0) {
  417. return;
  418. }
  419. int w = ((p_index <= 0xFF) ? 1 : ((p_index <= 0xFFFF) ? 2 : 3));
  420. int sp = MAX(0, w - 1);
  421. int sz = MAX(1, Math::round(p_size / 15.f));
  422. Size2 size = Vector2(4 + 3 * w + sp, 15) * sz;
  423. Point2 pos = p_pos - Point2i(0, size.y * 0.85);
  424. // Draw frame.
  425. RenderingServer::get_singleton()->canvas_item_add_rect(p_canvas, Rect2(pos + Point2(0, 0), Size2(sz, size.y)), p_color);
  426. RenderingServer::get_singleton()->canvas_item_add_rect(p_canvas, Rect2(pos + Point2(size.x - sz, 0), Size2(sz, size.y)), p_color);
  427. RenderingServer::get_singleton()->canvas_item_add_rect(p_canvas, Rect2(pos + Point2(0, 0), Size2(size.x, sz)), p_color);
  428. RenderingServer::get_singleton()->canvas_item_add_rect(p_canvas, Rect2(pos + Point2(0, size.y - sz), Size2(size.x, sz)), p_color);
  429. uint8_t a = p_index & 0x0F;
  430. uint8_t b = (p_index >> 4) & 0x0F;
  431. uint8_t c = (p_index >> 8) & 0x0F;
  432. uint8_t d = (p_index >> 12) & 0x0F;
  433. uint8_t e = (p_index >> 16) & 0x0F;
  434. uint8_t f = (p_index >> 20) & 0x0F;
  435. // Draw hex code.
  436. if (p_index <= 0xFF) {
  437. _draw_hex_code_box_number(p_canvas, sz, pos + Point2(2, 2) * sz, b, p_color);
  438. _draw_hex_code_box_number(p_canvas, sz, pos + Point2(2, 8) * sz, a, p_color);
  439. } else if (p_index <= 0xFFFF) {
  440. _draw_hex_code_box_number(p_canvas, sz, pos + Point2(2, 2) * sz, d, p_color);
  441. _draw_hex_code_box_number(p_canvas, sz, pos + Point2(6, 2) * sz, c, p_color);
  442. _draw_hex_code_box_number(p_canvas, sz, pos + Point2(2, 8) * sz, b, p_color);
  443. _draw_hex_code_box_number(p_canvas, sz, pos + Point2(6, 8) * sz, a, p_color);
  444. } else {
  445. _draw_hex_code_box_number(p_canvas, sz, pos + Point2(2, 2) * sz, f, p_color);
  446. _draw_hex_code_box_number(p_canvas, sz, pos + Point2(6, 2) * sz, e, p_color);
  447. _draw_hex_code_box_number(p_canvas, sz, pos + Point2(10, 2) * sz, d, p_color);
  448. _draw_hex_code_box_number(p_canvas, sz, pos + Point2(2, 8) * sz, c, p_color);
  449. _draw_hex_code_box_number(p_canvas, sz, pos + Point2(6, 8) * sz, b, p_color);
  450. _draw_hex_code_box_number(p_canvas, sz, pos + Point2(10, 8) * sz, a, p_color);
  451. }
  452. }
  453. PackedInt32Array TextServer::shaped_text_get_line_breaks_adv(RID p_shaped, const PackedFloat32Array &p_width, int p_start, bool p_once, uint16_t /*TextBreakFlag*/ p_break_flags) const {
  454. PackedInt32Array lines;
  455. ERR_FAIL_COND_V(p_width.is_empty(), lines);
  456. const_cast<TextServer *>(this)->shaped_text_update_breaks(p_shaped);
  457. const Vector2i &range = shaped_text_get_range(p_shaped);
  458. real_t width = 0.f;
  459. int line_start = MAX(p_start, range.x);
  460. int last_safe_break = -1;
  461. int chunk = 0;
  462. int l_size = shaped_text_get_glyph_count(p_shaped);
  463. const Glyph *l_gl = const_cast<TextServer *>(this)->shaped_text_sort_logical(p_shaped);
  464. for (int i = 0; i < l_size; i++) {
  465. if (l_gl[i].start < p_start) {
  466. continue;
  467. }
  468. if (l_gl[i].count > 0) {
  469. if ((p_width[chunk] > 0) && (width + l_gl[i].advance > p_width[chunk]) && (last_safe_break >= 0)) {
  470. lines.push_back(line_start);
  471. lines.push_back(l_gl[last_safe_break].end);
  472. line_start = l_gl[last_safe_break].end;
  473. i = last_safe_break;
  474. last_safe_break = -1;
  475. width = 0;
  476. chunk++;
  477. if (chunk >= p_width.size()) {
  478. chunk = 0;
  479. if (p_once) {
  480. return lines;
  481. }
  482. }
  483. continue;
  484. }
  485. if ((p_break_flags & BREAK_MANDATORY) == BREAK_MANDATORY) {
  486. if ((l_gl[i].flags & GRAPHEME_IS_BREAK_HARD) == GRAPHEME_IS_BREAK_HARD) {
  487. lines.push_back(line_start);
  488. lines.push_back(l_gl[i].end);
  489. line_start = l_gl[i].end;
  490. last_safe_break = -1;
  491. width = 0;
  492. chunk = 0;
  493. if (p_once) {
  494. return lines;
  495. }
  496. continue;
  497. }
  498. }
  499. if ((p_break_flags & BREAK_WORD_BOUND) == BREAK_WORD_BOUND) {
  500. if ((l_gl[i].flags & GRAPHEME_IS_BREAK_SOFT) == GRAPHEME_IS_BREAK_SOFT) {
  501. last_safe_break = i;
  502. }
  503. }
  504. if ((p_break_flags & BREAK_GRAPHEME_BOUND) == BREAK_GRAPHEME_BOUND) {
  505. last_safe_break = i;
  506. }
  507. }
  508. width += l_gl[i].advance;
  509. }
  510. if (l_size > 0) {
  511. if (lines.size() == 0 || lines[lines.size() - 1] < range.y) {
  512. lines.push_back(line_start);
  513. lines.push_back(range.y);
  514. }
  515. } else {
  516. lines.push_back(0);
  517. lines.push_back(0);
  518. }
  519. return lines;
  520. }
  521. PackedInt32Array TextServer::shaped_text_get_line_breaks(RID p_shaped, float p_width, int p_start, uint16_t /*TextBreakFlag*/ p_break_flags) const {
  522. PackedInt32Array lines;
  523. const_cast<TextServer *>(this)->shaped_text_update_breaks(p_shaped);
  524. const Vector2i &range = shaped_text_get_range(p_shaped);
  525. float width = 0.f;
  526. int line_start = MAX(p_start, range.x);
  527. int last_safe_break = -1;
  528. int word_count = 0;
  529. int l_size = shaped_text_get_glyph_count(p_shaped);
  530. const Glyph *l_gl = const_cast<TextServer *>(this)->shaped_text_sort_logical(p_shaped);
  531. for (int i = 0; i < l_size; i++) {
  532. if (l_gl[i].start < p_start) {
  533. continue;
  534. }
  535. if (l_gl[i].count > 0) {
  536. if ((p_width > 0) && (width + l_gl[i].advance * l_gl[i].repeat > p_width) && (last_safe_break >= 0)) {
  537. lines.push_back(line_start);
  538. lines.push_back(l_gl[last_safe_break].end);
  539. line_start = l_gl[last_safe_break].end;
  540. i = last_safe_break;
  541. last_safe_break = -1;
  542. width = 0;
  543. word_count = 0;
  544. continue;
  545. }
  546. if ((p_break_flags & BREAK_MANDATORY) == BREAK_MANDATORY) {
  547. if ((l_gl[i].flags & GRAPHEME_IS_BREAK_HARD) == GRAPHEME_IS_BREAK_HARD) {
  548. lines.push_back(line_start);
  549. lines.push_back(l_gl[i].end);
  550. line_start = l_gl[i].end;
  551. last_safe_break = -1;
  552. width = 0;
  553. continue;
  554. }
  555. }
  556. if ((p_break_flags & BREAK_WORD_BOUND) == BREAK_WORD_BOUND) {
  557. if ((l_gl[i].flags & GRAPHEME_IS_BREAK_SOFT) == GRAPHEME_IS_BREAK_SOFT) {
  558. last_safe_break = i;
  559. word_count++;
  560. }
  561. }
  562. if (((p_break_flags & BREAK_WORD_BOUND_ADAPTIVE) == BREAK_WORD_BOUND_ADAPTIVE) && word_count == 0) {
  563. last_safe_break = i;
  564. }
  565. if ((p_break_flags & BREAK_GRAPHEME_BOUND) == BREAK_GRAPHEME_BOUND) {
  566. last_safe_break = i;
  567. }
  568. }
  569. width += l_gl[i].advance * l_gl[i].repeat;
  570. }
  571. if (l_size > 0) {
  572. if (lines.size() == 0 || lines[lines.size() - 1] < range.y) {
  573. lines.push_back(line_start);
  574. lines.push_back(range.y);
  575. }
  576. } else {
  577. lines.push_back(0);
  578. lines.push_back(0);
  579. }
  580. return lines;
  581. }
  582. PackedInt32Array TextServer::shaped_text_get_word_breaks(RID p_shaped, int p_grapheme_flags) const {
  583. PackedInt32Array words;
  584. const_cast<TextServer *>(this)->shaped_text_update_justification_ops(p_shaped);
  585. const Vector2i &range = shaped_text_get_range(p_shaped);
  586. int word_start = range.x;
  587. int l_size = shaped_text_get_glyph_count(p_shaped);
  588. const Glyph *l_gl = const_cast<TextServer *>(this)->shaped_text_sort_logical(p_shaped);
  589. for (int i = 0; i < l_size; i++) {
  590. if (l_gl[i].count > 0) {
  591. if ((l_gl[i].flags & p_grapheme_flags) != 0) {
  592. words.push_back(word_start);
  593. words.push_back(l_gl[i].start);
  594. word_start = l_gl[i].end;
  595. }
  596. }
  597. }
  598. if (l_size > 0) {
  599. words.push_back(word_start);
  600. words.push_back(range.y);
  601. }
  602. return words;
  603. }
  604. CaretInfo TextServer::shaped_text_get_carets(RID p_shaped, int p_position) const {
  605. Vector<Rect2> carets;
  606. TextServer::Orientation orientation = shaped_text_get_orientation(p_shaped);
  607. const Vector2 &range = shaped_text_get_range(p_shaped);
  608. real_t ascent = shaped_text_get_ascent(p_shaped);
  609. real_t descent = shaped_text_get_descent(p_shaped);
  610. real_t height = (ascent + descent) / 2;
  611. real_t off = 0.0f;
  612. CaretInfo caret;
  613. caret.l_dir = DIRECTION_AUTO;
  614. caret.t_dir = DIRECTION_AUTO;
  615. int v_size = shaped_text_get_glyph_count(p_shaped);
  616. const Glyph *glyphs = shaped_text_get_glyphs(p_shaped);
  617. for (int i = 0; i < v_size; i++) {
  618. if (glyphs[i].count > 0) {
  619. // Caret before grapheme (top / left).
  620. if (p_position == glyphs[i].start && ((glyphs[i].flags & GRAPHEME_IS_VIRTUAL) != GRAPHEME_IS_VIRTUAL)) {
  621. Rect2 cr;
  622. if (orientation == ORIENTATION_HORIZONTAL) {
  623. if (glyphs[i].start == range.x) {
  624. cr.size.y = height * 2;
  625. } else {
  626. cr.size.y = height;
  627. }
  628. cr.position.y = -ascent;
  629. cr.position.x = off;
  630. if ((glyphs[i].flags & GRAPHEME_IS_RTL) == GRAPHEME_IS_RTL) {
  631. caret.t_dir = DIRECTION_RTL;
  632. for (int j = 0; j < glyphs[i].count; j++) {
  633. cr.position.x += glyphs[i + j].advance * glyphs[i + j].repeat;
  634. cr.size.x -= glyphs[i + j].advance * glyphs[i + j].repeat;
  635. }
  636. } else {
  637. caret.t_dir = DIRECTION_LTR;
  638. for (int j = 0; j < glyphs[i].count; j++) {
  639. cr.size.x += glyphs[i + j].advance * glyphs[i + j].repeat;
  640. }
  641. }
  642. } else {
  643. if (glyphs[i].start == range.x) {
  644. cr.size.x = height * 2;
  645. } else {
  646. cr.size.x = height;
  647. }
  648. cr.position.x = -ascent;
  649. cr.position.y = off;
  650. if ((glyphs[i].flags & GRAPHEME_IS_RTL) == GRAPHEME_IS_RTL) {
  651. caret.t_dir = DIRECTION_RTL;
  652. for (int j = 0; j < glyphs[i].count; j++) {
  653. cr.position.y += glyphs[i + j].advance * glyphs[i + j].repeat;
  654. cr.size.y -= glyphs[i + j].advance * glyphs[i + j].repeat;
  655. }
  656. } else {
  657. caret.t_dir = DIRECTION_LTR;
  658. for (int j = 0; j < glyphs[i].count; j++) {
  659. cr.size.y += glyphs[i + j].advance * glyphs[i + j].repeat;
  660. }
  661. }
  662. }
  663. caret.t_caret = cr;
  664. }
  665. // Caret after grapheme (bottom / right).
  666. if (p_position == glyphs[i].end && ((glyphs[i].flags & GRAPHEME_IS_VIRTUAL) != GRAPHEME_IS_VIRTUAL)) {
  667. Rect2 cr;
  668. if (orientation == ORIENTATION_HORIZONTAL) {
  669. if (glyphs[i].end == range.y) {
  670. cr.size.y = height * 2;
  671. cr.position.y = -ascent;
  672. } else {
  673. cr.size.y = height;
  674. cr.position.y = -ascent + height;
  675. }
  676. cr.position.x = off;
  677. if ((glyphs[i].flags & GRAPHEME_IS_RTL) != GRAPHEME_IS_RTL) {
  678. caret.l_dir = DIRECTION_LTR;
  679. for (int j = 0; j < glyphs[i].count; j++) {
  680. cr.position.x += glyphs[i + j].advance * glyphs[i + j].repeat;
  681. cr.size.x -= glyphs[i + j].advance * glyphs[i + j].repeat;
  682. }
  683. } else {
  684. caret.l_dir = DIRECTION_RTL;
  685. for (int j = 0; j < glyphs[i].count; j++) {
  686. cr.size.x += glyphs[i + j].advance * glyphs[i + j].repeat;
  687. }
  688. }
  689. } else {
  690. cr.size.y = 1.0f;
  691. if (glyphs[i].end == range.y) {
  692. cr.size.x = height * 2;
  693. cr.position.x = -ascent;
  694. } else {
  695. cr.size.x = height;
  696. cr.position.x = -ascent + height;
  697. }
  698. cr.position.y = off;
  699. if ((glyphs[i].flags & GRAPHEME_IS_RTL) != GRAPHEME_IS_RTL) {
  700. caret.l_dir = DIRECTION_LTR;
  701. for (int j = 0; j < glyphs[i].count; j++) {
  702. cr.position.y += glyphs[i + j].advance * glyphs[i + j].repeat;
  703. cr.size.y -= glyphs[i + j].advance * glyphs[i + j].repeat;
  704. }
  705. } else {
  706. caret.l_dir = DIRECTION_RTL;
  707. for (int j = 0; j < glyphs[i].count; j++) {
  708. cr.size.y += glyphs[i + j].advance * glyphs[i + j].repeat;
  709. }
  710. }
  711. }
  712. caret.l_caret = cr;
  713. }
  714. // Caret inside grapheme (middle).
  715. if (p_position > glyphs[i].start && p_position < glyphs[i].end && (glyphs[i].flags & GRAPHEME_IS_VIRTUAL) != GRAPHEME_IS_VIRTUAL) {
  716. real_t advance = 0.f;
  717. for (int j = 0; j < glyphs[i].count; j++) {
  718. advance += glyphs[i + j].advance * glyphs[i + j].repeat;
  719. }
  720. real_t char_adv = advance / (real_t)(glyphs[i].end - glyphs[i].start);
  721. Rect2 cr;
  722. if (orientation == ORIENTATION_HORIZONTAL) {
  723. cr.size.x = 1.0f;
  724. cr.size.y = height * 2;
  725. cr.position.y = -ascent;
  726. if ((glyphs[i].flags & GRAPHEME_IS_RTL) == GRAPHEME_IS_RTL) {
  727. cr.position.x = off + char_adv * (glyphs[i].end - p_position);
  728. } else {
  729. cr.position.x = off + char_adv * (p_position - glyphs[i].start);
  730. }
  731. } else {
  732. cr.size.y = 1.0f;
  733. cr.size.x = height * 2;
  734. cr.position.x = -ascent;
  735. if ((glyphs[i].flags & GRAPHEME_IS_RTL) == GRAPHEME_IS_RTL) {
  736. cr.position.y = off + char_adv * (glyphs[i].end - p_position);
  737. } else {
  738. cr.position.y = off + char_adv * (p_position - glyphs[i].start);
  739. }
  740. }
  741. caret.t_caret = cr;
  742. caret.l_caret = cr;
  743. }
  744. }
  745. off += glyphs[i].advance * glyphs[i].repeat;
  746. }
  747. return caret;
  748. }
  749. Dictionary TextServer::_shaped_text_get_carets_wrapper(RID p_shaped, int p_position) const {
  750. Dictionary ret;
  751. CaretInfo caret = shaped_text_get_carets(p_shaped, p_position);
  752. ret["leading_rect"] = caret.l_caret;
  753. ret["leading_direction"] = caret.l_dir;
  754. ret["trailing_rect"] = caret.t_caret;
  755. ret["trailing_direction"] = caret.t_dir;
  756. return ret;
  757. }
  758. TextServer::Direction TextServer::shaped_text_get_dominant_direction_in_range(RID p_shaped, int p_start, int p_end) const {
  759. if (p_start == p_end) {
  760. return DIRECTION_AUTO;
  761. }
  762. int start = MIN(p_start, p_end);
  763. int end = MAX(p_start, p_end);
  764. int rtl = 0;
  765. int ltr = 0;
  766. int v_size = shaped_text_get_glyph_count(p_shaped);
  767. const Glyph *glyphs = shaped_text_get_glyphs(p_shaped);
  768. for (int i = 0; i < v_size; i++) {
  769. if ((glyphs[i].end > start) && (glyphs[i].start < end)) {
  770. if (glyphs[i].count > 0) {
  771. if ((glyphs[i].flags & GRAPHEME_IS_RTL) == GRAPHEME_IS_RTL) {
  772. rtl++;
  773. } else {
  774. ltr++;
  775. }
  776. }
  777. }
  778. }
  779. if (ltr == rtl) {
  780. return DIRECTION_AUTO;
  781. } else if (ltr > rtl) {
  782. return DIRECTION_LTR;
  783. } else {
  784. return DIRECTION_RTL;
  785. }
  786. }
  787. Vector<Vector2> TextServer::shaped_text_get_selection(RID p_shaped, int p_start, int p_end) const {
  788. Vector<Vector2> ranges;
  789. if (p_start == p_end) {
  790. return ranges;
  791. }
  792. int start = MIN(p_start, p_end);
  793. int end = MAX(p_start, p_end);
  794. int v_size = shaped_text_get_glyph_count(p_shaped);
  795. const Glyph *glyphs = shaped_text_get_glyphs(p_shaped);
  796. real_t off = 0.0f;
  797. for (int i = 0; i < v_size; i++) {
  798. for (int k = 0; k < glyphs[i].repeat; k++) {
  799. if ((glyphs[i].count > 0) && ((glyphs[i].index != 0) || ((glyphs[i].flags & GRAPHEME_IS_SPACE) == GRAPHEME_IS_SPACE))) {
  800. if (glyphs[i].start < end && glyphs[i].end > start) {
  801. // Grapheme fully in selection range.
  802. if (glyphs[i].start >= start && glyphs[i].end <= end) {
  803. real_t advance = 0.f;
  804. for (int j = 0; j < glyphs[i].count; j++) {
  805. advance += glyphs[i + j].advance;
  806. }
  807. ranges.push_back(Vector2(off, off + advance));
  808. }
  809. // Only start of grapheme is in selection range.
  810. if (glyphs[i].start >= start && glyphs[i].end > end) {
  811. real_t advance = 0.f;
  812. for (int j = 0; j < glyphs[i].count; j++) {
  813. advance += glyphs[i + j].advance;
  814. }
  815. real_t char_adv = advance / (real_t)(glyphs[i].end - glyphs[i].start);
  816. if ((glyphs[i].flags & GRAPHEME_IS_RTL) == GRAPHEME_IS_RTL) {
  817. ranges.push_back(Vector2(off + char_adv * (glyphs[i].end - end), off + advance));
  818. } else {
  819. ranges.push_back(Vector2(off, off + char_adv * (end - glyphs[i].start)));
  820. }
  821. }
  822. // Only end of grapheme is in selection range.
  823. if (glyphs[i].start < start && glyphs[i].end <= end) {
  824. real_t advance = 0.f;
  825. for (int j = 0; j < glyphs[i].count; j++) {
  826. advance += glyphs[i + j].advance;
  827. }
  828. real_t char_adv = advance / (real_t)(glyphs[i].end - glyphs[i].start);
  829. if ((glyphs[i].flags & GRAPHEME_IS_RTL) == GRAPHEME_IS_RTL) {
  830. ranges.push_back(Vector2(off, off + char_adv * (start - glyphs[i].start)));
  831. } else {
  832. ranges.push_back(Vector2(off + char_adv * (glyphs[i].end - start), off + advance));
  833. }
  834. }
  835. // Selection range is within grapheme.
  836. if (glyphs[i].start < start && glyphs[i].end > end) {
  837. real_t advance = 0.f;
  838. for (int j = 0; j < glyphs[i].count; j++) {
  839. advance += glyphs[i + j].advance;
  840. }
  841. real_t char_adv = advance / (real_t)(glyphs[i].end - glyphs[i].start);
  842. if ((glyphs[i].flags & GRAPHEME_IS_RTL) == GRAPHEME_IS_RTL) {
  843. ranges.push_back(Vector2(off + char_adv * (glyphs[i].end - end), off + char_adv * (glyphs[i].end - start)));
  844. } else {
  845. ranges.push_back(Vector2(off + char_adv * (start - glyphs[i].start), off + char_adv * (end - glyphs[i].start)));
  846. }
  847. }
  848. }
  849. }
  850. off += glyphs[i].advance;
  851. }
  852. }
  853. // Merge intersecting ranges.
  854. int i = 0;
  855. while (i < ranges.size()) {
  856. i++;
  857. }
  858. i = 0;
  859. while (i < ranges.size()) {
  860. int j = i + 1;
  861. while (j < ranges.size()) {
  862. if (Math::is_equal_approx(ranges[i].y, ranges[j].x, (real_t)UNIT_EPSILON)) {
  863. ranges.write[i].y = ranges[j].y;
  864. ranges.remove_at(j);
  865. continue;
  866. }
  867. j++;
  868. }
  869. i++;
  870. }
  871. return ranges;
  872. }
  873. int TextServer::shaped_text_hit_test_grapheme(RID p_shaped, float p_coords) const {
  874. // Exact grapheme hit test, return -1 if missed.
  875. float off = 0.0f;
  876. int v_size = shaped_text_get_glyph_count(p_shaped);
  877. const Glyph *glyphs = shaped_text_get_glyphs(p_shaped);
  878. for (int i = 0; i < v_size; i++) {
  879. for (int j = 0; j < glyphs[i].repeat; j++) {
  880. if (p_coords >= off && p_coords < off + glyphs[i].advance) {
  881. return i;
  882. }
  883. off += glyphs[i].advance;
  884. }
  885. }
  886. return -1;
  887. }
  888. int TextServer::shaped_text_hit_test_position(RID p_shaped, float p_coords) const {
  889. int v_size = shaped_text_get_glyph_count(p_shaped);
  890. const Glyph *glyphs = shaped_text_get_glyphs(p_shaped);
  891. // Cursor placement hit test.
  892. // Place caret to the left of the leftmost grapheme, or to position 0 if string is empty.
  893. if (p_coords <= 0) {
  894. if (v_size > 0) {
  895. if ((glyphs[0].flags & GRAPHEME_IS_RTL) == GRAPHEME_IS_RTL) {
  896. return glyphs[0].end;
  897. } else {
  898. return glyphs[0].start;
  899. }
  900. } else {
  901. return 0;
  902. }
  903. }
  904. // Place caret to the right of the rightmost grapheme, or to position 0 if string is empty.
  905. if (p_coords >= shaped_text_get_width(p_shaped)) {
  906. if (v_size > 0) {
  907. if ((glyphs[v_size - 1].flags & GRAPHEME_IS_RTL) == GRAPHEME_IS_RTL) {
  908. return glyphs[v_size - 1].start;
  909. } else {
  910. return glyphs[v_size - 1].end;
  911. }
  912. } else {
  913. return 0;
  914. }
  915. }
  916. real_t off = 0.0f;
  917. for (int i = 0; i < v_size; i++) {
  918. if (glyphs[i].count > 0) {
  919. real_t advance = 0.f;
  920. for (int j = 0; j < glyphs[i].count; j++) {
  921. advance += glyphs[i + j].advance * glyphs[i + j].repeat;
  922. }
  923. if (((glyphs[i].flags & GRAPHEME_IS_VIRTUAL) == GRAPHEME_IS_VIRTUAL) && (p_coords >= off && p_coords < off + advance)) {
  924. if ((glyphs[i].flags & GRAPHEME_IS_RTL) == GRAPHEME_IS_RTL) {
  925. return glyphs[i].end;
  926. } else {
  927. return glyphs[i].start;
  928. }
  929. }
  930. // Place caret to the left of clicked grapheme.
  931. if (p_coords >= off && p_coords < off + advance / 2) {
  932. if ((glyphs[i].flags & GRAPHEME_IS_RTL) == GRAPHEME_IS_RTL) {
  933. return glyphs[i].end;
  934. } else {
  935. return glyphs[i].start;
  936. }
  937. }
  938. // Place caret to the right of clicked grapheme.
  939. if (p_coords >= off + advance / 2 && p_coords < off + advance) {
  940. if ((glyphs[i].flags & GRAPHEME_IS_RTL) == GRAPHEME_IS_RTL) {
  941. return glyphs[i].start;
  942. } else {
  943. return glyphs[i].end;
  944. }
  945. }
  946. }
  947. off += glyphs[i].advance * glyphs[i].repeat;
  948. }
  949. return 0;
  950. }
  951. Vector2 TextServer::shaped_text_get_grapheme_bounds(RID p_shaped, int p_pos) const {
  952. int v_size = shaped_text_get_glyph_count(p_shaped);
  953. const Glyph *glyphs = shaped_text_get_glyphs(p_shaped);
  954. real_t off = 0.0f;
  955. for (int i = 0; i < v_size; i++) {
  956. if ((glyphs[i].count > 0) && ((glyphs[i].index != 0) || ((glyphs[i].flags & GRAPHEME_IS_SPACE) == GRAPHEME_IS_SPACE))) {
  957. if (glyphs[i].start <= p_pos && glyphs[i].end >= p_pos) {
  958. real_t advance = 0.f;
  959. for (int j = 0; j < glyphs[i].count; j++) {
  960. advance += glyphs[i + j].advance;
  961. }
  962. return Vector2(off, off + advance);
  963. }
  964. }
  965. off += glyphs[i].advance * glyphs[i].repeat;
  966. }
  967. return Vector2();
  968. }
  969. int TextServer::shaped_text_next_grapheme_pos(RID p_shaped, int p_pos) const {
  970. int v_size = shaped_text_get_glyph_count(p_shaped);
  971. const Glyph *glyphs = shaped_text_get_glyphs(p_shaped);
  972. for (int i = 0; i < v_size; i++) {
  973. if (p_pos >= glyphs[i].start && p_pos < glyphs[i].end) {
  974. return glyphs[i].end;
  975. }
  976. }
  977. return p_pos;
  978. }
  979. int TextServer::shaped_text_prev_grapheme_pos(RID p_shaped, int p_pos) const {
  980. int v_size = shaped_text_get_glyph_count(p_shaped);
  981. const Glyph *glyphs = shaped_text_get_glyphs(p_shaped);
  982. for (int i = 0; i < v_size; i++) {
  983. if (p_pos > glyphs[i].start && p_pos <= glyphs[i].end) {
  984. return glyphs[i].start;
  985. }
  986. }
  987. return p_pos;
  988. }
  989. void TextServer::shaped_text_draw(RID p_shaped, RID p_canvas, const Vector2 &p_pos, float p_clip_l, float p_clip_r, const Color &p_color) const {
  990. TextServer::Orientation orientation = shaped_text_get_orientation(p_shaped);
  991. bool hex_codes = shaped_text_get_preserve_control(p_shaped) || shaped_text_get_preserve_invalid(p_shaped);
  992. bool rtl = shaped_text_get_direction(p_shaped) == DIRECTION_RTL;
  993. int ellipsis_pos = shaped_text_get_ellipsis_pos(p_shaped);
  994. int trim_pos = shaped_text_get_trim_pos(p_shaped);
  995. const Glyph *ellipsis_glyphs = shaped_text_get_ellipsis_glyphs(p_shaped);
  996. int ellipsis_gl_size = shaped_text_get_ellipsis_glyph_count(p_shaped);
  997. int v_size = shaped_text_get_glyph_count(p_shaped);
  998. const Glyph *glyphs = shaped_text_get_glyphs(p_shaped);
  999. Vector2 ofs = p_pos;
  1000. // Draw RTL ellipsis string when needed.
  1001. if (rtl && ellipsis_pos >= 0) {
  1002. for (int i = ellipsis_gl_size - 1; i >= 0; i--) {
  1003. for (int j = 0; j < ellipsis_glyphs[i].repeat; j++) {
  1004. font_draw_glyph(ellipsis_glyphs[i].font_rid, p_canvas, ellipsis_glyphs[i].font_size, ofs + Vector2(ellipsis_glyphs[i].x_off, ellipsis_glyphs[i].y_off), ellipsis_glyphs[i].index, p_color);
  1005. if (orientation == ORIENTATION_HORIZONTAL) {
  1006. ofs.x += ellipsis_glyphs[i].advance;
  1007. } else {
  1008. ofs.y += ellipsis_glyphs[i].advance;
  1009. }
  1010. }
  1011. }
  1012. }
  1013. // Draw at the baseline.
  1014. for (int i = 0; i < v_size; i++) {
  1015. for (int j = 0; j < glyphs[i].repeat; j++) {
  1016. if (p_clip_r > 0) {
  1017. // Clip right / bottom.
  1018. if (orientation == ORIENTATION_HORIZONTAL) {
  1019. if (ofs.x - p_pos.x > p_clip_r) {
  1020. return;
  1021. }
  1022. } else {
  1023. if (ofs.y - p_pos.y > p_clip_r) {
  1024. return;
  1025. }
  1026. }
  1027. }
  1028. if (p_clip_l > 0) {
  1029. // Clip left / top.
  1030. if (orientation == ORIENTATION_HORIZONTAL) {
  1031. if (ofs.x - p_pos.x < p_clip_l) {
  1032. ofs.x += glyphs[i].advance;
  1033. continue;
  1034. }
  1035. } else {
  1036. if (ofs.y - p_pos.y < p_clip_l) {
  1037. ofs.y += glyphs[i].advance;
  1038. continue;
  1039. }
  1040. }
  1041. }
  1042. if (trim_pos >= 0) {
  1043. if (rtl) {
  1044. if (i < trim_pos && (glyphs[j].flags & TextServer::GRAPHEME_IS_VIRTUAL) != TextServer::GRAPHEME_IS_VIRTUAL) {
  1045. continue;
  1046. }
  1047. } else {
  1048. if (i >= trim_pos && (glyphs[j].flags & TextServer::GRAPHEME_IS_VIRTUAL) != TextServer::GRAPHEME_IS_VIRTUAL) {
  1049. break;
  1050. }
  1051. }
  1052. }
  1053. if (glyphs[i].font_rid != RID()) {
  1054. font_draw_glyph(glyphs[i].font_rid, p_canvas, glyphs[i].font_size, ofs + Vector2(glyphs[i].x_off, glyphs[i].y_off), glyphs[i].index, p_color);
  1055. } else if (hex_codes && ((glyphs[i].flags & GRAPHEME_IS_VIRTUAL) != GRAPHEME_IS_VIRTUAL)) {
  1056. TextServer::draw_hex_code_box(p_canvas, glyphs[i].font_size, ofs + Vector2(glyphs[i].x_off, glyphs[i].y_off), glyphs[i].index, p_color);
  1057. }
  1058. if (orientation == ORIENTATION_HORIZONTAL) {
  1059. ofs.x += glyphs[i].advance;
  1060. } else {
  1061. ofs.y += glyphs[i].advance;
  1062. }
  1063. }
  1064. }
  1065. // Draw LTR ellipsis string when needed.
  1066. if (!rtl && ellipsis_pos >= 0) {
  1067. for (int i = 0; i < ellipsis_gl_size; i++) {
  1068. for (int j = 0; j < ellipsis_glyphs[i].repeat; j++) {
  1069. font_draw_glyph(ellipsis_glyphs[i].font_rid, p_canvas, ellipsis_glyphs[i].font_size, ofs + Vector2(ellipsis_glyphs[i].x_off, ellipsis_glyphs[i].y_off), ellipsis_glyphs[i].index, p_color);
  1070. if (orientation == ORIENTATION_HORIZONTAL) {
  1071. ofs.x += ellipsis_glyphs[i].advance;
  1072. } else {
  1073. ofs.y += ellipsis_glyphs[i].advance;
  1074. }
  1075. }
  1076. }
  1077. }
  1078. }
  1079. void TextServer::shaped_text_draw_outline(RID p_shaped, RID p_canvas, const Vector2 &p_pos, float p_clip_l, float p_clip_r, int p_outline_size, const Color &p_color) const {
  1080. TextServer::Orientation orientation = shaped_text_get_orientation(p_shaped);
  1081. bool rtl = (shaped_text_get_direction(p_shaped) == DIRECTION_RTL);
  1082. int ellipsis_pos = shaped_text_get_ellipsis_pos(p_shaped);
  1083. int trim_pos = shaped_text_get_trim_pos(p_shaped);
  1084. const Glyph *ellipsis_glyphs = shaped_text_get_ellipsis_glyphs(p_shaped);
  1085. int ellipsis_gl_size = shaped_text_get_ellipsis_glyph_count(p_shaped);
  1086. int v_size = shaped_text_get_glyph_count(p_shaped);
  1087. const Glyph *glyphs = shaped_text_get_glyphs(p_shaped);
  1088. Vector2 ofs = p_pos;
  1089. // Draw RTL ellipsis string when needed.
  1090. if (rtl && ellipsis_pos >= 0) {
  1091. for (int i = ellipsis_gl_size - 1; i >= 0; i--) {
  1092. for (int j = 0; j < ellipsis_glyphs[i].repeat; j++) {
  1093. font_draw_glyph(ellipsis_glyphs[i].font_rid, p_canvas, ellipsis_glyphs[i].font_size, ofs + Vector2(ellipsis_glyphs[i].x_off, ellipsis_glyphs[i].y_off), ellipsis_glyphs[i].index, p_color);
  1094. if (orientation == ORIENTATION_HORIZONTAL) {
  1095. ofs.x += ellipsis_glyphs[i].advance;
  1096. } else {
  1097. ofs.y += ellipsis_glyphs[i].advance;
  1098. }
  1099. }
  1100. }
  1101. }
  1102. // Draw at the baseline.
  1103. for (int i = 0; i < v_size; i++) {
  1104. for (int j = 0; j < glyphs[i].repeat; j++) {
  1105. if (p_clip_r > 0) {
  1106. // Clip right / bottom.
  1107. if (orientation == ORIENTATION_HORIZONTAL) {
  1108. if (ofs.x - p_pos.x > p_clip_r) {
  1109. return;
  1110. }
  1111. } else {
  1112. if (ofs.y - p_pos.y > p_clip_r) {
  1113. return;
  1114. }
  1115. }
  1116. }
  1117. if (p_clip_l > 0) {
  1118. // Clip left / top.
  1119. if (orientation == ORIENTATION_HORIZONTAL) {
  1120. if (ofs.x - p_pos.x < p_clip_l) {
  1121. ofs.x += glyphs[i].advance;
  1122. continue;
  1123. }
  1124. } else {
  1125. if (ofs.y - p_pos.y < p_clip_l) {
  1126. ofs.y += glyphs[i].advance;
  1127. continue;
  1128. }
  1129. }
  1130. }
  1131. if (trim_pos >= 0) {
  1132. if (rtl) {
  1133. if (i < trim_pos) {
  1134. continue;
  1135. }
  1136. } else {
  1137. if (i >= trim_pos && (glyphs[j].flags & TextServer::GRAPHEME_IS_VIRTUAL) != TextServer::GRAPHEME_IS_VIRTUAL) {
  1138. break;
  1139. }
  1140. }
  1141. }
  1142. if (glyphs[i].font_rid != RID()) {
  1143. font_draw_glyph_outline(glyphs[i].font_rid, p_canvas, glyphs[i].font_size, p_outline_size, ofs + Vector2(glyphs[i].x_off, glyphs[i].y_off), glyphs[i].index, p_color);
  1144. }
  1145. if (orientation == ORIENTATION_HORIZONTAL) {
  1146. ofs.x += glyphs[i].advance;
  1147. } else {
  1148. ofs.y += glyphs[i].advance;
  1149. }
  1150. }
  1151. }
  1152. // Draw LTR ellipsis string when needed.
  1153. if (!rtl && ellipsis_pos >= 0) {
  1154. for (int i = 0; i < ellipsis_gl_size; i++) {
  1155. for (int j = 0; j < ellipsis_glyphs[i].repeat; j++) {
  1156. font_draw_glyph(ellipsis_glyphs[i].font_rid, p_canvas, ellipsis_glyphs[i].font_size, ofs + Vector2(ellipsis_glyphs[i].x_off, ellipsis_glyphs[i].y_off), ellipsis_glyphs[i].index, p_color);
  1157. if (orientation == ORIENTATION_HORIZONTAL) {
  1158. ofs.x += ellipsis_glyphs[i].advance;
  1159. } else {
  1160. ofs.y += ellipsis_glyphs[i].advance;
  1161. }
  1162. }
  1163. }
  1164. }
  1165. }
  1166. void TextServer::_diacritics_map_add(const String &p_from, char32_t p_to) {
  1167. for (int i = 0; i < p_from.size(); i++) {
  1168. diacritics_map[p_from[i]] = p_to;
  1169. }
  1170. }
  1171. void TextServer::_init_diacritics_map() {
  1172. diacritics_map.clear();
  1173. // Latin.
  1174. _diacritics_map_add(U"ÀÁÂÃÄÅĀĂĄǍǞǠǺȀȂȦḀẠẢẤẦẨẪẬẮẰẲẴẶ", U'A');
  1175. _diacritics_map_add(U"àáâãäåāăąǎǟǡǻȁȃȧḁẚạảấầẩẫậắằẳẵặ", U'a');
  1176. _diacritics_map_add(U"ǢǼ", U'Æ');
  1177. _diacritics_map_add(U"ǣǽ", U'æ');
  1178. _diacritics_map_add(U"ḂḄḆ", U'B');
  1179. _diacritics_map_add(U"ḃḅḇ", U'b');
  1180. _diacritics_map_add(U"ÇĆĈĊČḈ", U'C');
  1181. _diacritics_map_add(U"çćĉċčḉ", U'c');
  1182. _diacritics_map_add(U"ĎḊḌḎḐḒ", U'D');
  1183. _diacritics_map_add(U"ďḋḍḏḑḓ", U'd');
  1184. _diacritics_map_add(U"ÈÉÊËĒĔĖĘĚȆȨḔḖḘḚḜẸẺẼẾỀỂỄỆ", U'E');
  1185. _diacritics_map_add(U"èéêëēĕėęěȇȩḕḗḙḛḝẹẻẽếềểễệ", U'e');
  1186. _diacritics_map_add(U"Ḟ", U'F');
  1187. _diacritics_map_add(U"ḟ", U'f');
  1188. _diacritics_map_add(U"ĜĞĠĢǦǴḠ", U'G');
  1189. _diacritics_map_add(U"ĝğġģǧǵḡ", U'g');
  1190. _diacritics_map_add(U"ĤȞḢḤḦḨḪ", U'H');
  1191. _diacritics_map_add(U"ĥȟḣḥḧḩḫẖ", U'h');
  1192. _diacritics_map_add(U"ÌÍÎÏĨĪĬĮİǏȈȊḬḮỈỊ", U'I');
  1193. _diacritics_map_add(U"ìíîïĩīĭįıǐȉȋḭḯỉị", U'i');
  1194. _diacritics_map_add(U"Ĵ", U'J');
  1195. _diacritics_map_add(U"ĵ", U'j');
  1196. _diacritics_map_add(U"ĶǨḰḲḴ", U'K');
  1197. _diacritics_map_add(U"ķĸǩḱḳḵ", U'k');
  1198. _diacritics_map_add(U"ĹĻĽĿḶḸḺḼ", U'L');
  1199. _diacritics_map_add(U"ĺļľŀḷḹḻḽ", U'l');
  1200. _diacritics_map_add(U"ḾṀṂ", U'M');
  1201. _diacritics_map_add(U"ḿṁṃ", U'm');
  1202. _diacritics_map_add(U"ÑŃŅŇǸṄṆṈṊ", U'N');
  1203. _diacritics_map_add(U"ñńņňʼnǹṅṇṉṋ", U'n');
  1204. _diacritics_map_add(U"ÒÓÔÕÖŌŎŐƠǑǪǬȌȎȪȬȮȰṌṎṐṒỌỎỐỒỔỖỘỚỜỞỠỢ", U'O');
  1205. _diacritics_map_add(U"òóôõöōŏőơǒǫǭȍȏȫȭȯȱṍṏṑṓọỏốồổỗộớờởỡợ", U'o');
  1206. _diacritics_map_add(U"ṔṖ", U'P');
  1207. _diacritics_map_add(U"ṗṕ", U'p');
  1208. _diacritics_map_add(U"ŔŖŘȐȒṘṚṜṞ", U'R');
  1209. _diacritics_map_add(U"ŕŗřȑȓṙṛṝṟ", U'r');
  1210. _diacritics_map_add(U"ŚŜŞŠȘṠṢṤṦṨ", U'S');
  1211. _diacritics_map_add(U"śŝşšſșṡṣṥṧṩẛẜẝ", U's');
  1212. _diacritics_map_add(U"ŢŤȚṪṬṮṰ", U'T');
  1213. _diacritics_map_add(U"ţťțṫṭṯṱẗ", U't');
  1214. _diacritics_map_add(U"ÙÚÛÜŨŪŬŮŰŲƯǓǕǗǙǛȔȖṲṴṶṸṺỤỦỨỪỬỮỰ", U'U');
  1215. _diacritics_map_add(U"ùúûüũūŭůűųưǔǖǘǚǜȕȗṳṵṷṹṻụủứừửữự", U'u');
  1216. _diacritics_map_add(U"ṼṾ", U'V');
  1217. _diacritics_map_add(U"ṽṿ", U'v');
  1218. _diacritics_map_add(U"ŴẀẂẄẆẈ", U'W');
  1219. _diacritics_map_add(U"ŵẁẃẅẇẉẘ", U'w');
  1220. _diacritics_map_add(U"ẊẌ", U'X');
  1221. _diacritics_map_add(U"ẋẍ", U'x');
  1222. _diacritics_map_add(U"ÝŶẎỲỴỶỸỾ", U'Y');
  1223. _diacritics_map_add(U"ýÿŷẏẙỳỵỷỹỿ", U'y');
  1224. _diacritics_map_add(U"ŹŻŽẐẒẔ", U'Z');
  1225. _diacritics_map_add(U"źżžẑẓẕ", U'z');
  1226. // Greek.
  1227. _diacritics_map_add(U"ΆἈἉἊἋἌἍἎἏᾈᾉᾊᾋᾌᾍᾎᾏᾸᾹᾺΆᾼ", U'Α');
  1228. _diacritics_map_add(U"άἀἁἂἃἄἅἆἇὰάᾀᾁᾂᾃᾄᾅᾆᾇᾰᾱᾲᾳᾴᾶᾷ", U'α');
  1229. _diacritics_map_add(U"ΈἘἙἚἛἜἝῈΈ", U'Ε');
  1230. _diacritics_map_add(U"έἐἑἒἓἔἕὲέ", U'ε');
  1231. _diacritics_map_add(U"ΉἨἩἪἫἬἭἮἯᾘᾙᾚᾛᾜᾝᾞᾟῊΉῌ", U'Η');
  1232. _diacritics_map_add(U"ήἠἡἢἣἤἥἦἧὴήᾐᾑᾒᾓᾔᾕᾖᾗῂῃῄῆῇ", U'η');
  1233. _diacritics_map_add(U"ΊΪἸἹἺἻἼἽἾἿῘῙῚΊ", U'Ι');
  1234. _diacritics_map_add(U"ίΐϊἰἱἲἳἴἵἶἷὶίῐῑῒΐῖῗ", U'ι');
  1235. _diacritics_map_add(U"ΌὈὉὊὋὌὍῸΌ", U'Ο');
  1236. _diacritics_map_add(U"όὀὁὂὃὄὅὸό", U'ο');
  1237. _diacritics_map_add(U"Ῥ", U'Ρ');
  1238. _diacritics_map_add(U"ῤῥ", U'ρ');
  1239. _diacritics_map_add(U"ΎΫϓϔὙὛὝὟῨῩῪΎ", U'Υ');
  1240. _diacritics_map_add(U"ΰϋύὐὑὒὓὔὕὖὗὺύῠῡῢΰῦῧ", U'υ');
  1241. _diacritics_map_add(U"ΏὨὩὪὫὬὭὮὯᾨᾩᾪᾫᾬᾭᾮᾯῺΏῼ", U'Ω');
  1242. _diacritics_map_add(U"ώὠὡὢὣὤὥὦὧὼώᾠᾡᾢᾣᾤᾥᾦᾧῲῳῴῶῷ", U'ω');
  1243. // Cyrillic.
  1244. _diacritics_map_add(U"ӐӒ", U'А');
  1245. _diacritics_map_add(U"ӑӓ", U'а');
  1246. _diacritics_map_add(U"ЀЁӖ", U'Е');
  1247. _diacritics_map_add(U"ѐёӗ", U'е');
  1248. _diacritics_map_add(U"Ӛ", U'Ә');
  1249. _diacritics_map_add(U"ӛ", U'ә');
  1250. _diacritics_map_add(U"Ӝ", U'Ж');
  1251. _diacritics_map_add(U"ӝ", U'ж');
  1252. _diacritics_map_add(U"Ӟ", U'З');
  1253. _diacritics_map_add(U"ӟ", U'з');
  1254. _diacritics_map_add(U"Ѓ", U'Г');
  1255. _diacritics_map_add(U"ѓ", U'г');
  1256. _diacritics_map_add(U"Ї", U'І');
  1257. _diacritics_map_add(U"ї", U'і');
  1258. _diacritics_map_add(U"ЍӢӤЙ", U'И');
  1259. _diacritics_map_add(U"ѝӣӥй", U'и');
  1260. _diacritics_map_add(U"Ќ", U'К');
  1261. _diacritics_map_add(U"ќ", U'к');
  1262. _diacritics_map_add(U"Ӧ", U'О');
  1263. _diacritics_map_add(U"ӧ", U'о');
  1264. _diacritics_map_add(U"Ӫ", U'Ө');
  1265. _diacritics_map_add(U"ӫ", U'ө');
  1266. _diacritics_map_add(U"Ӭ", U'Э');
  1267. _diacritics_map_add(U"ӭ", U'э');
  1268. _diacritics_map_add(U"ЎӮӰӲ", U'У');
  1269. _diacritics_map_add(U"ўӯӱӳ", U'у');
  1270. _diacritics_map_add(U"Ӵ", U'Ч');
  1271. _diacritics_map_add(U"ӵ", U'ч');
  1272. _diacritics_map_add(U"Ӹ", U'Ы');
  1273. _diacritics_map_add(U"ӹ", U'ы');
  1274. }
  1275. String TextServer::strip_diacritics(const String &p_string) const {
  1276. String result;
  1277. for (int i = 0; i < p_string.length(); i++) {
  1278. if (p_string[i] < 0x02B0 || p_string[i] > 0x036F) { // Skip combining diacritics.
  1279. if (diacritics_map.has(p_string[i])) {
  1280. result += diacritics_map[p_string[i]];
  1281. } else {
  1282. result += p_string[i];
  1283. }
  1284. }
  1285. }
  1286. return result;
  1287. }
  1288. Array TextServer::_shaped_text_get_glyphs_wrapper(RID p_shaped) const {
  1289. Array ret;
  1290. const Glyph *glyphs = shaped_text_get_glyphs(p_shaped);
  1291. int gl_size = shaped_text_get_glyph_count(p_shaped);
  1292. for (int i = 0; i < gl_size; i++) {
  1293. Dictionary glyph;
  1294. glyph["start"] = glyphs[i].start;
  1295. glyph["end"] = glyphs[i].end;
  1296. glyph["repeat"] = glyphs[i].repeat;
  1297. glyph["count"] = glyphs[i].count;
  1298. glyph["flags"] = glyphs[i].flags;
  1299. glyph["offset"] = Vector2(glyphs[i].x_off, glyphs[i].y_off);
  1300. glyph["advance"] = glyphs[i].advance;
  1301. glyph["font_rid"] = glyphs[i].font_rid;
  1302. glyph["font_size"] = glyphs[i].font_size;
  1303. glyph["index"] = glyphs[i].index;
  1304. ret.push_back(glyph);
  1305. }
  1306. return ret;
  1307. }
  1308. Array TextServer::_shaped_text_sort_logical_wrapper(RID p_shaped) {
  1309. Array ret;
  1310. const Glyph *glyphs = shaped_text_sort_logical(p_shaped);
  1311. int gl_size = shaped_text_get_glyph_count(p_shaped);
  1312. for (int i = 0; i < gl_size; i++) {
  1313. Dictionary glyph;
  1314. glyph["start"] = glyphs[i].start;
  1315. glyph["end"] = glyphs[i].end;
  1316. glyph["repeat"] = glyphs[i].repeat;
  1317. glyph["count"] = glyphs[i].count;
  1318. glyph["flags"] = glyphs[i].flags;
  1319. glyph["offset"] = Vector2(glyphs[i].x_off, glyphs[i].y_off);
  1320. glyph["advance"] = glyphs[i].advance;
  1321. glyph["font_rid"] = glyphs[i].font_rid;
  1322. glyph["font_size"] = glyphs[i].font_size;
  1323. glyph["index"] = glyphs[i].index;
  1324. ret.push_back(glyph);
  1325. }
  1326. return ret;
  1327. }
  1328. Array TextServer::_shaped_text_get_ellipsis_glyphs_wrapper(RID p_shaped) const {
  1329. Array ret;
  1330. const Glyph *glyphs = shaped_text_get_ellipsis_glyphs(p_shaped);
  1331. int gl_size = shaped_text_get_ellipsis_glyph_count(p_shaped);
  1332. for (int i = 0; i < gl_size; i++) {
  1333. Dictionary glyph;
  1334. glyph["start"] = glyphs[i].start;
  1335. glyph["end"] = glyphs[i].end;
  1336. glyph["repeat"] = glyphs[i].repeat;
  1337. glyph["count"] = glyphs[i].count;
  1338. glyph["flags"] = glyphs[i].flags;
  1339. glyph["offset"] = Vector2(glyphs[i].x_off, glyphs[i].y_off);
  1340. glyph["advance"] = glyphs[i].advance;
  1341. glyph["font_rid"] = glyphs[i].font_rid;
  1342. glyph["font_size"] = glyphs[i].font_size;
  1343. glyph["index"] = glyphs[i].index;
  1344. ret.push_back(glyph);
  1345. }
  1346. return ret;
  1347. }
  1348. TextServer::TextServer() {
  1349. _init_diacritics_map();
  1350. }
  1351. TextServer::~TextServer() {
  1352. }