text_server.cpp 54 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289
  1. /*************************************************************************/
  2. /* text_server.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2020 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 "scene/main/canvas_item.h"
  32. TextServerManager *TextServerManager::singleton = nullptr;
  33. TextServer *TextServerManager::server = nullptr;
  34. TextServerManager::TextServerCreate TextServerManager::server_create_functions[TextServerManager::MAX_SERVERS];
  35. int TextServerManager::server_create_count = 0;
  36. void TextServerManager::_bind_methods() {
  37. ClassDB::bind_method(D_METHOD("get_interface_count"), &TextServerManager::_get_interface_count);
  38. ClassDB::bind_method(D_METHOD("get_interface_name", "index"), &TextServerManager::_get_interface_name);
  39. ClassDB::bind_method(D_METHOD("get_interface_features", "index"), &TextServerManager::_get_interface_features);
  40. ClassDB::bind_method(D_METHOD("get_interface", "index"), &TextServerManager::_get_interface);
  41. ClassDB::bind_method(D_METHOD("get_interfaces"), &TextServerManager::_get_interfaces);
  42. ClassDB::bind_method(D_METHOD("find_interface", "name"), &TextServerManager::_find_interface);
  43. ClassDB::bind_method(D_METHOD("set_primary_interface", "index"), &TextServerManager::_set_primary_interface);
  44. ClassDB::bind_method(D_METHOD("get_primary_interface"), &TextServerManager::_get_primary_interface);
  45. }
  46. void TextServerManager::register_create_function(const String &p_name, uint32_t p_features, TextServerManager::CreateFunction p_function, void *p_user_data) {
  47. ERR_FAIL_COND(server_create_count == MAX_SERVERS);
  48. server_create_functions[server_create_count].name = p_name;
  49. server_create_functions[server_create_count].create_function = p_function;
  50. server_create_functions[server_create_count].user_data = p_user_data;
  51. server_create_functions[server_create_count].features = p_features;
  52. server_create_count++;
  53. }
  54. int TextServerManager::get_interface_count() {
  55. return server_create_count;
  56. }
  57. String TextServerManager::get_interface_name(int p_index) {
  58. ERR_FAIL_INDEX_V(p_index, server_create_count, String());
  59. return server_create_functions[p_index].name;
  60. }
  61. uint32_t TextServerManager::get_interface_features(int p_index) {
  62. ERR_FAIL_INDEX_V(p_index, server_create_count, 0);
  63. return server_create_functions[p_index].features;
  64. }
  65. TextServer *TextServerManager::initialize(int p_index, Error &r_error) {
  66. ERR_FAIL_INDEX_V(p_index, server_create_count, nullptr);
  67. if (server_create_functions[p_index].instance == nullptr) {
  68. server_create_functions[p_index].instance = server_create_functions[p_index].create_function(r_error, server_create_functions[p_index].user_data);
  69. if (server_create_functions[p_index].instance != nullptr) {
  70. server_create_functions[p_index].instance->load_support_data(""); // Try loading default data.
  71. }
  72. }
  73. if (server_create_functions[p_index].instance != nullptr) {
  74. server = server_create_functions[p_index].instance;
  75. if (OS::get_singleton()->get_main_loop()) {
  76. OS::get_singleton()->get_main_loop()->notification(MainLoop::NOTIFICATION_TEXT_SERVER_CHANGED);
  77. }
  78. }
  79. return server_create_functions[p_index].instance;
  80. }
  81. TextServer *TextServerManager::get_primary_interface() {
  82. return server;
  83. }
  84. int TextServerManager::_get_interface_count() const {
  85. return server_create_count;
  86. }
  87. String TextServerManager::_get_interface_name(int p_index) const {
  88. return get_interface_name(p_index);
  89. }
  90. uint32_t TextServerManager::_get_interface_features(int p_index) const {
  91. return get_interface_features(p_index);
  92. }
  93. TextServer *TextServerManager::_get_interface(int p_index) const {
  94. ERR_FAIL_INDEX_V(p_index, server_create_count, nullptr);
  95. if (server_create_functions[p_index].instance == nullptr) {
  96. Error error;
  97. server_create_functions[p_index].instance = server_create_functions[p_index].create_function(error, server_create_functions[p_index].user_data);
  98. if (server_create_functions[p_index].instance != nullptr) {
  99. server_create_functions[p_index].instance->load_support_data(""); // Try loading default data.
  100. }
  101. }
  102. return server_create_functions[p_index].instance;
  103. }
  104. TextServer *TextServerManager::_find_interface(const String &p_name) const {
  105. for (int i = 0; i < server_create_count; i++) {
  106. if (server_create_functions[i].name == p_name) {
  107. return _get_interface(i);
  108. }
  109. }
  110. return nullptr;
  111. }
  112. Array TextServerManager::_get_interfaces() const {
  113. Array ret;
  114. for (int i = 0; i < server_create_count; i++) {
  115. Dictionary iface_info;
  116. iface_info["id"] = i;
  117. iface_info["name"] = server_create_functions[i].name;
  118. ret.push_back(iface_info);
  119. };
  120. return ret;
  121. };
  122. bool TextServerManager::_set_primary_interface(int p_index) {
  123. Error error;
  124. TextServerManager::initialize(p_index, error);
  125. return (error == OK);
  126. }
  127. TextServer *TextServerManager::_get_primary_interface() const {
  128. return server;
  129. }
  130. TextServerManager::TextServerManager() {
  131. singleton = this;
  132. }
  133. TextServerManager::~TextServerManager() {
  134. singleton = nullptr;
  135. for (int i = 0; i < server_create_count; i++) {
  136. if (server_create_functions[i].instance != nullptr) {
  137. memdelete(server_create_functions[i].instance);
  138. server_create_functions[i].instance = nullptr;
  139. }
  140. }
  141. }
  142. /*************************************************************************/
  143. bool TextServer::Glyph::operator==(const Glyph &p_a) const {
  144. return (p_a.index == index) && (p_a.font_rid == font_rid) && (p_a.font_size == font_size) && (p_a.start == start);
  145. }
  146. bool TextServer::Glyph::operator!=(const Glyph &p_a) const {
  147. return (p_a.index != index) || (p_a.font_rid != font_rid) || (p_a.font_size != font_size) || (p_a.start != start);
  148. }
  149. bool TextServer::Glyph::operator<(const Glyph &p_a) const {
  150. if (p_a.start == start) {
  151. if (p_a.count == count) {
  152. if ((p_a.flags & GRAPHEME_IS_VIRTUAL) == GRAPHEME_IS_VIRTUAL) {
  153. return true;
  154. } else {
  155. return false;
  156. }
  157. }
  158. return p_a.count > count;
  159. }
  160. return p_a.start < start;
  161. }
  162. bool TextServer::Glyph::operator>(const Glyph &p_a) const {
  163. if (p_a.start == start) {
  164. if (p_a.count == count) {
  165. if ((p_a.flags & GRAPHEME_IS_VIRTUAL) == GRAPHEME_IS_VIRTUAL) {
  166. return false;
  167. } else {
  168. return true;
  169. }
  170. }
  171. return p_a.count < count;
  172. }
  173. return p_a.start > start;
  174. }
  175. void TextServer::_bind_methods() {
  176. ClassDB::bind_method(D_METHOD("has_feature", "feature"), &TextServer::has_feature);
  177. ClassDB::bind_method(D_METHOD("get_name"), &TextServer::get_name);
  178. ClassDB::bind_method(D_METHOD("load_support_data", "filename"), &TextServer::load_support_data);
  179. ClassDB::bind_method(D_METHOD("is_locale_right_to_left", "locale"), &TextServer::is_locale_right_to_left);
  180. ClassDB::bind_method(D_METHOD("name_to_tag", "name"), &TextServer::name_to_tag);
  181. ClassDB::bind_method(D_METHOD("tag_to_name", "tag"), &TextServer::tag_to_name);
  182. ClassDB::bind_method(D_METHOD("has", "rid"), &TextServer::has);
  183. ClassDB::bind_method(D_METHOD("free_rid", "rid"), &TextServer::free); // shouldn't conflict with Object::free()
  184. /* Font Interface */
  185. ClassDB::bind_method(D_METHOD("create_font_system", "name", "base_size"), &TextServer::create_font_system, DEFVAL(16));
  186. ClassDB::bind_method(D_METHOD("create_font_resource", "filename", "base_size"), &TextServer::create_font_resource, DEFVAL(16));
  187. ClassDB::bind_method(D_METHOD("create_font_memory", "data", "type", "base_size"), &TextServer::_create_font_memory, DEFVAL(16));
  188. ClassDB::bind_method(D_METHOD("font_get_height", "font", "size"), &TextServer::font_get_height);
  189. ClassDB::bind_method(D_METHOD("font_get_ascent", "font", "size"), &TextServer::font_get_ascent);
  190. ClassDB::bind_method(D_METHOD("font_get_descent", "font", "size"), &TextServer::font_get_descent);
  191. ClassDB::bind_method(D_METHOD("font_get_underline_position", "font", "size"), &TextServer::font_get_underline_position);
  192. ClassDB::bind_method(D_METHOD("font_get_underline_thickness", "font", "size"), &TextServer::font_get_underline_thickness);
  193. ClassDB::bind_method(D_METHOD("font_set_antialiased", "font", "antialiased"), &TextServer::font_set_antialiased);
  194. ClassDB::bind_method(D_METHOD("font_get_antialiased", "font"), &TextServer::font_get_antialiased);
  195. ClassDB::bind_method(D_METHOD("font_get_feature_list", "font"), &TextServer::font_get_feature_list);
  196. ClassDB::bind_method(D_METHOD("font_get_variation_list", "font"), &TextServer::font_get_variation_list);
  197. ClassDB::bind_method(D_METHOD("font_set_variation", "font", "tag", "value"), &TextServer::font_set_variation);
  198. ClassDB::bind_method(D_METHOD("font_get_variation", "font", "tag"), &TextServer::font_get_variation);
  199. ClassDB::bind_method(D_METHOD("font_set_hinting", "font", "hinting"), &TextServer::font_set_hinting);
  200. ClassDB::bind_method(D_METHOD("font_get_hinting", "font"), &TextServer::font_get_hinting);
  201. ClassDB::bind_method(D_METHOD("font_set_distance_field_hint", "font", "distance_field"), &TextServer::font_set_distance_field_hint);
  202. ClassDB::bind_method(D_METHOD("font_get_distance_field_hint", "font"), &TextServer::font_get_distance_field_hint);
  203. ClassDB::bind_method(D_METHOD("font_set_force_autohinter", "font", "enabeld"), &TextServer::font_set_force_autohinter);
  204. ClassDB::bind_method(D_METHOD("font_get_force_autohinter", "font"), &TextServer::font_get_force_autohinter);
  205. ClassDB::bind_method(D_METHOD("font_has_char", "font", "char"), &TextServer::font_has_char);
  206. ClassDB::bind_method(D_METHOD("font_get_supported_chars", "font"), &TextServer::font_get_supported_chars);
  207. ClassDB::bind_method(D_METHOD("font_has_outline", "font"), &TextServer::font_has_outline);
  208. ClassDB::bind_method(D_METHOD("font_get_base_size", "font"), &TextServer::font_get_base_size);
  209. ClassDB::bind_method(D_METHOD("font_is_language_supported", "font", "language"), &TextServer::font_is_language_supported);
  210. ClassDB::bind_method(D_METHOD("font_set_language_support_override", "font", "language", "supported"), &TextServer::font_set_language_support_override);
  211. ClassDB::bind_method(D_METHOD("font_get_language_support_override", "font", "language"), &TextServer::font_get_language_support_override);
  212. ClassDB::bind_method(D_METHOD("font_remove_language_support_override", "font", "language"), &TextServer::font_remove_language_support_override);
  213. ClassDB::bind_method(D_METHOD("font_get_language_support_overrides", "font"), &TextServer::font_get_language_support_overrides);
  214. ClassDB::bind_method(D_METHOD("font_is_script_supported", "font", "script"), &TextServer::font_is_script_supported);
  215. ClassDB::bind_method(D_METHOD("font_set_script_support_override", "font", "script", "supported"), &TextServer::font_set_script_support_override);
  216. ClassDB::bind_method(D_METHOD("font_get_script_support_override", "font", "script"), &TextServer::font_get_script_support_override);
  217. ClassDB::bind_method(D_METHOD("font_remove_script_support_override", "font", "script"), &TextServer::font_remove_script_support_override);
  218. ClassDB::bind_method(D_METHOD("font_get_script_support_overrides", "font"), &TextServer::font_get_script_support_overrides);
  219. ClassDB::bind_method(D_METHOD("font_get_glyph_index", "font", "char", "variation_selector"), &TextServer::font_get_glyph_index, DEFVAL(0x0000));
  220. ClassDB::bind_method(D_METHOD("font_get_glyph_advance", "font", "index", "size"), &TextServer::font_get_glyph_advance);
  221. ClassDB::bind_method(D_METHOD("font_get_glyph_kerning", "font", "index_a", "index_b", "size"), &TextServer::font_get_glyph_kerning);
  222. ClassDB::bind_method(D_METHOD("font_draw_glyph", "font", "canvas", "size", "pos", "index", "color"), &TextServer::font_draw_glyph, DEFVAL(Color(1, 1, 1)));
  223. ClassDB::bind_method(D_METHOD("font_draw_glyph_outline", "font", "canvas", "size", "outline_size", "pos", "index", "color"), &TextServer::font_draw_glyph_outline, DEFVAL(Color(1, 1, 1)));
  224. ClassDB::bind_method(D_METHOD("font_get_oversampling"), &TextServer::font_get_oversampling);
  225. ClassDB::bind_method(D_METHOD("font_set_oversampling", "oversampling"), &TextServer::font_set_oversampling);
  226. ClassDB::bind_method(D_METHOD("get_system_fonts"), &TextServer::get_system_fonts);
  227. ClassDB::bind_method(D_METHOD("get_hex_code_box_size", "size", "index"), &TextServer::get_hex_code_box_size);
  228. ClassDB::bind_method(D_METHOD("draw_hex_code_box", "canvas", "size", "pos", "index", "color"), &TextServer::draw_hex_code_box);
  229. /* Shaped text buffer interface */
  230. ClassDB::bind_method(D_METHOD("create_shaped_text", "direction", "orientation"), &TextServer::create_shaped_text, DEFVAL(DIRECTION_AUTO), DEFVAL(ORIENTATION_HORIZONTAL));
  231. ClassDB::bind_method(D_METHOD("shaped_text_clear"), &TextServer::shaped_text_clear);
  232. ClassDB::bind_method(D_METHOD("shaped_text_set_direction", "shaped", "direction"), &TextServer::shaped_text_set_direction, DEFVAL(DIRECTION_AUTO));
  233. ClassDB::bind_method(D_METHOD("shaped_text_get_direction", "shaped"), &TextServer::shaped_text_get_direction);
  234. ClassDB::bind_method(D_METHOD("shaped_text_set_bidi_override", "shaped", "override"), &TextServer::_shaped_text_set_bidi_override);
  235. ClassDB::bind_method(D_METHOD("shaped_text_set_orientation", "shaped", "orientation"), &TextServer::shaped_text_set_orientation, DEFVAL(ORIENTATION_HORIZONTAL));
  236. ClassDB::bind_method(D_METHOD("shaped_text_get_orientation", "shaped"), &TextServer::shaped_text_get_orientation);
  237. ClassDB::bind_method(D_METHOD("shaped_text_set_preserve_invalid", "shaped", "enabled"), &TextServer::shaped_text_set_preserve_invalid);
  238. ClassDB::bind_method(D_METHOD("shaped_text_get_preserve_invalid", "shaped"), &TextServer::shaped_text_get_preserve_invalid);
  239. ClassDB::bind_method(D_METHOD("shaped_text_set_preserve_control", "shaped", "enabled"), &TextServer::shaped_text_set_preserve_control);
  240. ClassDB::bind_method(D_METHOD("shaped_text_get_preserve_control", "shaped"), &TextServer::shaped_text_get_preserve_control);
  241. ClassDB::bind_method(D_METHOD("shaped_text_add_string", "shaped", "text", "fonts", "size", "opentype_features", "language"), &TextServer::shaped_text_add_string, DEFVAL(Dictionary()), DEFVAL(""));
  242. ClassDB::bind_method(D_METHOD("shaped_text_add_object", "shaped", "key", "size", "inline_align", "length"), &TextServer::shaped_text_add_object, DEFVAL(VALIGN_CENTER), DEFVAL(1));
  243. ClassDB::bind_method(D_METHOD("shaped_text_resize_object", "shaped", "key", "size", "inline_align"), &TextServer::shaped_text_resize_object, DEFVAL(VALIGN_CENTER));
  244. ClassDB::bind_method(D_METHOD("shaped_text_substr", "shaped", "start", "length"), &TextServer::shaped_text_substr);
  245. ClassDB::bind_method(D_METHOD("shaped_text_get_parent", "shaped"), &TextServer::shaped_text_get_parent);
  246. 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));
  247. ClassDB::bind_method(D_METHOD("shaped_text_tab_align", "shaped", "tab_stops"), &TextServer::shaped_text_tab_align);
  248. ClassDB::bind_method(D_METHOD("shaped_text_shape", "shaped"), &TextServer::shaped_text_shape);
  249. ClassDB::bind_method(D_METHOD("shaped_text_is_ready", "shaped"), &TextServer::shaped_text_is_ready);
  250. ClassDB::bind_method(D_METHOD("shaped_text_get_glyphs", "shaped"), &TextServer::_shaped_text_get_glyphs);
  251. ClassDB::bind_method(D_METHOD("shaped_text_get_range", "shaped"), &TextServer::shaped_text_get_range);
  252. 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));
  253. 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));
  254. ClassDB::bind_method(D_METHOD("shaped_text_get_word_breaks", "shaped"), &TextServer::_shaped_text_get_word_breaks);
  255. ClassDB::bind_method(D_METHOD("shaped_text_get_objects", "shaped"), &TextServer::shaped_text_get_objects);
  256. ClassDB::bind_method(D_METHOD("shaped_text_get_object_rect", "shaped", "key"), &TextServer::shaped_text_get_object_rect);
  257. ClassDB::bind_method(D_METHOD("shaped_text_get_size", "shaped"), &TextServer::shaped_text_get_size);
  258. ClassDB::bind_method(D_METHOD("shaped_text_get_ascent", "shaped"), &TextServer::shaped_text_get_ascent);
  259. ClassDB::bind_method(D_METHOD("shaped_text_get_descent", "shaped"), &TextServer::shaped_text_get_descent);
  260. ClassDB::bind_method(D_METHOD("shaped_text_get_width", "shaped"), &TextServer::shaped_text_get_width);
  261. ClassDB::bind_method(D_METHOD("shaped_text_get_underline_position", "shaped"), &TextServer::shaped_text_get_underline_position);
  262. ClassDB::bind_method(D_METHOD("shaped_text_get_underline_thickness", "shaped"), &TextServer::shaped_text_get_underline_thickness);
  263. ClassDB::bind_method(D_METHOD("shaped_text_get_carets", "shaped", "position"), &TextServer::_shaped_text_get_carets);
  264. ClassDB::bind_method(D_METHOD("shaped_text_get_selection", "shaped", "start", "end"), &TextServer::_shaped_text_get_selection);
  265. ClassDB::bind_method(D_METHOD("shaped_text_hit_test_grapheme", "shaped", "coords"), &TextServer::shaped_text_hit_test_grapheme);
  266. ClassDB::bind_method(D_METHOD("shaped_text_hit_test_position", "shaped", "coords"), &TextServer::shaped_text_hit_test_position);
  267. ClassDB::bind_method(D_METHOD("shaped_text_next_grapheme_pos", "shaped", "pos"), &TextServer::shaped_text_next_grapheme_pos);
  268. ClassDB::bind_method(D_METHOD("shaped_text_prev_grapheme_pos", "shaped", "pos"), &TextServer::shaped_text_prev_grapheme_pos);
  269. 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)));
  270. 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)));
  271. ClassDB::bind_method(D_METHOD("shaped_text_get_dominant_direciton_in_range", "shaped", "start", "end"), &TextServer::shaped_text_get_dominant_direciton_in_range);
  272. ClassDB::bind_method(D_METHOD("format_number", "number", "language"), &TextServer::format_number, DEFVAL(""));
  273. ClassDB::bind_method(D_METHOD("parse_number", "number", "language"), &TextServer::parse_number, DEFVAL(""));
  274. ClassDB::bind_method(D_METHOD("percent_sign", "language"), &TextServer::percent_sign, DEFVAL(""));
  275. /* Direction */
  276. BIND_ENUM_CONSTANT(DIRECTION_AUTO);
  277. BIND_ENUM_CONSTANT(DIRECTION_LTR);
  278. BIND_ENUM_CONSTANT(DIRECTION_RTL);
  279. /* Orientation */
  280. BIND_ENUM_CONSTANT(ORIENTATION_HORIZONTAL);
  281. BIND_ENUM_CONSTANT(ORIENTATION_VERTICAL);
  282. /* JustificationFlag */
  283. BIND_ENUM_CONSTANT(JUSTIFICATION_NONE);
  284. BIND_ENUM_CONSTANT(JUSTIFICATION_KASHIDA);
  285. BIND_ENUM_CONSTANT(JUSTIFICATION_WORD_BOUND);
  286. BIND_ENUM_CONSTANT(JUSTIFICATION_TRIM_EDGE_SPACES);
  287. BIND_ENUM_CONSTANT(JUSTIFICATION_AFTER_LAST_TAB);
  288. /* LineBreakFlag */
  289. BIND_ENUM_CONSTANT(BREAK_NONE);
  290. BIND_ENUM_CONSTANT(BREAK_MANDATORY);
  291. BIND_ENUM_CONSTANT(BREAK_WORD_BOUND);
  292. BIND_ENUM_CONSTANT(BREAK_GRAPHEME_BOUND);
  293. /* GraphemeFlag */
  294. BIND_ENUM_CONSTANT(GRAPHEME_IS_RTL);
  295. BIND_ENUM_CONSTANT(GRAPHEME_IS_VIRTUAL);
  296. BIND_ENUM_CONSTANT(GRAPHEME_IS_SPACE);
  297. BIND_ENUM_CONSTANT(GRAPHEME_IS_BREAK_HARD);
  298. BIND_ENUM_CONSTANT(GRAPHEME_IS_BREAK_SOFT);
  299. BIND_ENUM_CONSTANT(GRAPHEME_IS_TAB);
  300. BIND_ENUM_CONSTANT(GRAPHEME_IS_ELONGATION);
  301. /* Hinting */
  302. BIND_ENUM_CONSTANT(HINTING_NONE);
  303. BIND_ENUM_CONSTANT(HINTING_LIGHT);
  304. BIND_ENUM_CONSTANT(HINTING_NORMAL);
  305. /* Feature */
  306. BIND_ENUM_CONSTANT(FEATURE_BIDI_LAYOUT);
  307. BIND_ENUM_CONSTANT(FEATURE_VERTICAL_LAYOUT);
  308. BIND_ENUM_CONSTANT(FEATURE_SHAPING);
  309. BIND_ENUM_CONSTANT(FEATURE_KASHIDA_JUSTIFICATION);
  310. BIND_ENUM_CONSTANT(FEATURE_BREAK_ITERATORS);
  311. BIND_ENUM_CONSTANT(FEATURE_FONT_SYSTEM);
  312. BIND_ENUM_CONSTANT(FEATURE_FONT_VARIABLE);
  313. BIND_ENUM_CONSTANT(FEATURE_USE_SUPPORT_DATA);
  314. }
  315. Vector3 TextServer::hex_code_box_font_size[2] = { Vector3(5, 5, 1), Vector3(10, 10, 2) };
  316. Ref<CanvasTexture> TextServer::hex_code_box_font_tex[2] = { nullptr, nullptr };
  317. void TextServer::initialize_hex_code_box_fonts() {
  318. static unsigned int tamsyn5x9_png_len = 175;
  319. static unsigned char tamsyn5x9_png[] = {
  320. 0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a, 0x00, 0x00, 0x00, 0x0d,
  321. 0x49, 0x48, 0x44, 0x52, 0x00, 0x00, 0x00, 0x50, 0x00, 0x00, 0x00, 0x05,
  322. 0x04, 0x03, 0x00, 0x00, 0x00, 0x20, 0x7c, 0x76, 0xda, 0x00, 0x00, 0x00,
  323. 0x0f, 0x50, 0x4c, 0x54, 0x45, 0xfd, 0x07, 0x00, 0x00, 0x00, 0x00, 0x06,
  324. 0x7e, 0x74, 0x00, 0x40, 0xc6, 0xff, 0xff, 0xff, 0x47, 0x9a, 0xd4, 0xc7,
  325. 0x00, 0x00, 0x00, 0x01, 0x74, 0x52, 0x4e, 0x53, 0x00, 0x40, 0xe6, 0xd8,
  326. 0x66, 0x00, 0x00, 0x00, 0x4e, 0x49, 0x44, 0x41, 0x54, 0x08, 0x1d, 0x05,
  327. 0xc1, 0x21, 0x01, 0x00, 0x00, 0x00, 0x83, 0x30, 0x04, 0xc1, 0x10, 0xef,
  328. 0x9f, 0xe9, 0x1b, 0x86, 0x2c, 0x17, 0xb9, 0xcc, 0x65, 0x0c, 0x73, 0x38,
  329. 0xc7, 0xe6, 0x22, 0x19, 0x88, 0x98, 0x10, 0x48, 0x4a, 0x29, 0x85, 0x14,
  330. 0x02, 0x89, 0x10, 0xa3, 0x1c, 0x0b, 0x31, 0xd6, 0xe6, 0x08, 0x69, 0x39,
  331. 0x48, 0x44, 0xa0, 0x0d, 0x4a, 0x22, 0xa1, 0x94, 0x42, 0x0a, 0x01, 0x63,
  332. 0x6d, 0x0e, 0x72, 0x18, 0x61, 0x8c, 0x74, 0x38, 0xc7, 0x26, 0x1c, 0xf3,
  333. 0x71, 0x16, 0x15, 0x27, 0x6a, 0xc2, 0x2f, 0x00, 0x00, 0x00, 0x00, 0x49,
  334. 0x45, 0x4e, 0x44, 0xae, 0x42, 0x60, 0x82
  335. };
  336. static unsigned int tamsyn10x20_png_len = 270;
  337. static unsigned char tamsyn10x20_png[] = {
  338. 0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a, 0x00, 0x00, 0x00, 0x0d,
  339. 0x49, 0x48, 0x44, 0x52, 0x00, 0x00, 0x00, 0xa0, 0x00, 0x00, 0x00, 0x0a,
  340. 0x04, 0x03, 0x00, 0x00, 0x00, 0xc1, 0x66, 0x48, 0x96, 0x00, 0x00, 0x00,
  341. 0x0f, 0x50, 0x4c, 0x54, 0x45, 0x00, 0x00, 0x00, 0xf9, 0x07, 0x00, 0x5d,
  342. 0x71, 0xa5, 0x00, 0x00, 0x00, 0xff, 0xff, 0xff, 0x49, 0xdb, 0xcb, 0x7f,
  343. 0x00, 0x00, 0x00, 0x01, 0x74, 0x52, 0x4e, 0x53, 0x00, 0x40, 0xe6, 0xd8,
  344. 0x66, 0x00, 0x00, 0x00, 0xad, 0x49, 0x44, 0x41, 0x54, 0x28, 0xcf, 0xa5,
  345. 0x92, 0x4b, 0x0e, 0x03, 0x31, 0x08, 0x43, 0xdf, 0x82, 0x83, 0x79, 0xe1,
  346. 0xfb, 0x9f, 0xa9, 0x0b, 0x3e, 0x61, 0xa6, 0x1f, 0x55, 0xad, 0x14, 0x31,
  347. 0x66, 0x42, 0x1c, 0x70, 0x0c, 0xb6, 0x00, 0x01, 0xb6, 0x08, 0xdb, 0x00,
  348. 0x8d, 0xc2, 0x14, 0xb2, 0x55, 0xa1, 0xfe, 0x09, 0xc2, 0x26, 0xdc, 0x25,
  349. 0x75, 0x22, 0x97, 0x1a, 0x25, 0x77, 0x28, 0x31, 0x02, 0x80, 0xc8, 0xdd,
  350. 0x2c, 0x11, 0x1a, 0x54, 0x9f, 0xc8, 0xa2, 0x8a, 0x06, 0xa9, 0x93, 0x22,
  351. 0xbd, 0xd4, 0xd0, 0x0c, 0xcf, 0x81, 0x2b, 0xca, 0xbb, 0x83, 0xe0, 0x10,
  352. 0xe6, 0xad, 0xff, 0x10, 0x2a, 0x66, 0x34, 0x41, 0x58, 0x35, 0x54, 0x49,
  353. 0x5a, 0x63, 0xa5, 0xc2, 0x87, 0xab, 0x52, 0x76, 0x9a, 0xba, 0xc6, 0xf4,
  354. 0x75, 0x7a, 0x9e, 0x3c, 0x46, 0x86, 0x5c, 0xa3, 0xfd, 0x87, 0x0e, 0x75,
  355. 0x08, 0x7b, 0xee, 0x7e, 0xea, 0x21, 0x5c, 0x4f, 0xf6, 0xc5, 0xc8, 0x4b,
  356. 0xb9, 0x11, 0xf2, 0xd6, 0xe1, 0x8f, 0x84, 0x62, 0x7b, 0x67, 0xf9, 0x24,
  357. 0xde, 0x6d, 0xbc, 0xb2, 0xcd, 0xb1, 0xf3, 0xf2, 0x2f, 0xe8, 0xe2, 0xe4,
  358. 0xae, 0x4b, 0x4f, 0xcf, 0x2b, 0xdc, 0x8d, 0x0d, 0xf0, 0x00, 0x8f, 0x22,
  359. 0x26, 0x65, 0x75, 0x8a, 0xe6, 0x84, 0x00, 0x00, 0x00, 0x00, 0x49, 0x45,
  360. 0x4e, 0x44, 0xae, 0x42, 0x60, 0x82
  361. };
  362. if (RenderingServer::get_singleton() != nullptr) {
  363. Vector<uint8_t> hex_box_data;
  364. Ref<Image> image;
  365. image.instance();
  366. Ref<ImageTexture> hex_code_image_tex[2];
  367. hex_box_data.resize(tamsyn5x9_png_len);
  368. memcpy(hex_box_data.ptrw(), tamsyn5x9_png, tamsyn5x9_png_len);
  369. image->load_png_from_buffer(hex_box_data);
  370. hex_code_image_tex[0].instance();
  371. hex_code_image_tex[0]->create_from_image(image);
  372. hex_code_box_font_tex[0].instance();
  373. hex_code_box_font_tex[0]->set_diffuse_texture(hex_code_image_tex[0]);
  374. hex_code_box_font_tex[0]->set_texture_filter(CanvasItem::TEXTURE_FILTER_NEAREST);
  375. hex_box_data.clear();
  376. hex_box_data.resize(tamsyn10x20_png_len);
  377. memcpy(hex_box_data.ptrw(), tamsyn10x20_png, tamsyn10x20_png_len);
  378. image->load_png_from_buffer(hex_box_data);
  379. hex_code_image_tex[1].instance();
  380. hex_code_image_tex[1]->create_from_image(image);
  381. hex_code_box_font_tex[1].instance();
  382. hex_code_box_font_tex[1]->set_diffuse_texture(hex_code_image_tex[1]);
  383. hex_code_box_font_tex[1]->set_texture_filter(CanvasItem::TEXTURE_FILTER_NEAREST);
  384. hex_box_data.clear();
  385. }
  386. }
  387. void TextServer::finish_hex_code_box_fonts() {
  388. if (hex_code_box_font_tex[0].is_valid()) {
  389. hex_code_box_font_tex[0].unref();
  390. }
  391. if (hex_code_box_font_tex[1].is_valid()) {
  392. hex_code_box_font_tex[1].unref();
  393. }
  394. }
  395. Vector2 TextServer::get_hex_code_box_size(int p_size, char32_t p_index) const {
  396. int fnt = (p_size < 20) ? 0 : 1;
  397. float w = ((p_index <= 0xFF) ? 1 : ((p_index <= 0xFFFF) ? 2 : 3)) * hex_code_box_font_size[fnt].x;
  398. float h = 2 * hex_code_box_font_size[fnt].y;
  399. return Vector2(w + 4, h + 3 + 2 * hex_code_box_font_size[fnt].z);
  400. }
  401. 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 {
  402. int fnt = (p_size < 20) ? 0 : 1;
  403. ERR_FAIL_COND(hex_code_box_font_tex[fnt].is_null());
  404. uint8_t a = p_index & 0x0F;
  405. uint8_t b = (p_index >> 4) & 0x0F;
  406. uint8_t c = (p_index >> 8) & 0x0F;
  407. uint8_t d = (p_index >> 12) & 0x0F;
  408. uint8_t e = (p_index >> 16) & 0x0F;
  409. uint8_t f = (p_index >> 20) & 0x0F;
  410. Vector2 pos = p_pos;
  411. Rect2 dest = Rect2(Vector2(), Vector2(hex_code_box_font_size[fnt].x, hex_code_box_font_size[fnt].y));
  412. float w = ((p_index <= 0xFF) ? 1 : ((p_index <= 0xFFFF) ? 2 : 3)) * hex_code_box_font_size[fnt].x;
  413. float h = 2 * hex_code_box_font_size[fnt].y;
  414. pos.y -= Math::floor((h + 3 + hex_code_box_font_size[fnt].z) * 0.75);
  415. RenderingServer::get_singleton()->canvas_item_add_rect(p_canvas, Rect2(pos + Point2(0, 0), Size2(1, h + 2 + 2 * hex_code_box_font_size[fnt].z)), p_color);
  416. RenderingServer::get_singleton()->canvas_item_add_rect(p_canvas, Rect2(pos + Point2(w + 2, 0), Size2(1, h + 2 + 2 * hex_code_box_font_size[fnt].z)), p_color);
  417. RenderingServer::get_singleton()->canvas_item_add_rect(p_canvas, Rect2(pos + Point2(0, 0), Size2(w + 2, 1)), p_color);
  418. RenderingServer::get_singleton()->canvas_item_add_rect(p_canvas, Rect2(pos + Point2(0, h + 2 + 2 * hex_code_box_font_size[fnt].z), Size2(w + 2, 1)), p_color);
  419. pos += Point2(2, 2);
  420. if (p_index <= 0xFF) {
  421. dest.position = pos + Vector2(hex_code_box_font_size[fnt].x, hex_code_box_font_size[fnt].y) * Point2(0, 0);
  422. RenderingServer::get_singleton()->canvas_item_add_texture_rect_region(p_canvas, dest, hex_code_box_font_tex[fnt]->get_rid(), Rect2(Point2(b * hex_code_box_font_size[fnt].x, 0), dest.size), p_color, false, false);
  423. dest.position = pos + Vector2(hex_code_box_font_size[fnt].x, hex_code_box_font_size[fnt].y) * Point2(0, 1) + Point2(0, hex_code_box_font_size[fnt].z);
  424. RenderingServer::get_singleton()->canvas_item_add_texture_rect_region(p_canvas, dest, hex_code_box_font_tex[fnt]->get_rid(), Rect2(Point2(a * hex_code_box_font_size[fnt].x, 0), dest.size), p_color, false, false);
  425. } else if (p_index <= 0xFFFF) {
  426. dest.position = pos + Vector2(hex_code_box_font_size[fnt].x, hex_code_box_font_size[fnt].y) * Point2(0, 0);
  427. RenderingServer::get_singleton()->canvas_item_add_texture_rect_region(p_canvas, dest, hex_code_box_font_tex[fnt]->get_rid(), Rect2(Point2(d * hex_code_box_font_size[fnt].x, 0), dest.size), p_color, false, false);
  428. dest.position = pos + Vector2(hex_code_box_font_size[fnt].x, hex_code_box_font_size[fnt].y) * Point2(1, 0);
  429. RenderingServer::get_singleton()->canvas_item_add_texture_rect_region(p_canvas, dest, hex_code_box_font_tex[fnt]->get_rid(), Rect2(Point2(c * hex_code_box_font_size[fnt].x, 0), dest.size), p_color, false, false);
  430. dest.position = pos + Vector2(hex_code_box_font_size[fnt].x, hex_code_box_font_size[fnt].y) * Point2(0, 1) + Point2(0, hex_code_box_font_size[fnt].z);
  431. RenderingServer::get_singleton()->canvas_item_add_texture_rect_region(p_canvas, dest, hex_code_box_font_tex[fnt]->get_rid(), Rect2(Point2(b * hex_code_box_font_size[fnt].x, 0), dest.size), p_color, false, false);
  432. dest.position = pos + Vector2(hex_code_box_font_size[fnt].x, hex_code_box_font_size[fnt].y) * Point2(1, 1) + Point2(0, hex_code_box_font_size[fnt].z);
  433. RenderingServer::get_singleton()->canvas_item_add_texture_rect_region(p_canvas, dest, hex_code_box_font_tex[fnt]->get_rid(), Rect2(Point2(a * hex_code_box_font_size[fnt].x, 0), dest.size), p_color, false, false);
  434. } else {
  435. dest.position = pos + Vector2(hex_code_box_font_size[fnt].x, hex_code_box_font_size[fnt].y) * Point2(0, 0);
  436. RenderingServer::get_singleton()->canvas_item_add_texture_rect_region(p_canvas, dest, hex_code_box_font_tex[fnt]->get_rid(), Rect2(Point2(f * hex_code_box_font_size[fnt].x, 0), dest.size), p_color, false, false);
  437. dest.position = pos + Vector2(hex_code_box_font_size[fnt].x, hex_code_box_font_size[fnt].y) * Point2(1, 0);
  438. RenderingServer::get_singleton()->canvas_item_add_texture_rect_region(p_canvas, dest, hex_code_box_font_tex[fnt]->get_rid(), Rect2(Point2(e * hex_code_box_font_size[fnt].x, 0), dest.size), p_color, false, false);
  439. dest.position = pos + Vector2(hex_code_box_font_size[fnt].x, hex_code_box_font_size[fnt].y) * Point2(2, 0);
  440. RenderingServer::get_singleton()->canvas_item_add_texture_rect_region(p_canvas, dest, hex_code_box_font_tex[fnt]->get_rid(), Rect2(Point2(d * hex_code_box_font_size[fnt].x, 0), dest.size), p_color, false, false);
  441. dest.position = pos + Vector2(hex_code_box_font_size[fnt].x, hex_code_box_font_size[fnt].y) * Point2(0, 1) + Point2(0, hex_code_box_font_size[fnt].z);
  442. RenderingServer::get_singleton()->canvas_item_add_texture_rect_region(p_canvas, dest, hex_code_box_font_tex[fnt]->get_rid(), Rect2(Point2(c * hex_code_box_font_size[fnt].x, 0), dest.size), p_color, false, false);
  443. dest.position = pos + Vector2(hex_code_box_font_size[fnt].x, hex_code_box_font_size[fnt].y) * Point2(1, 1) + Point2(0, hex_code_box_font_size[fnt].z);
  444. RenderingServer::get_singleton()->canvas_item_add_texture_rect_region(p_canvas, dest, hex_code_box_font_tex[fnt]->get_rid(), Rect2(Point2(b * hex_code_box_font_size[fnt].x, 0), dest.size), p_color, false, false);
  445. dest.position = pos + Vector2(hex_code_box_font_size[fnt].x, hex_code_box_font_size[fnt].y) * Point2(2, 1) + Point2(0, hex_code_box_font_size[fnt].z);
  446. RenderingServer::get_singleton()->canvas_item_add_texture_rect_region(p_canvas, dest, hex_code_box_font_tex[fnt]->get_rid(), Rect2(Point2(a * hex_code_box_font_size[fnt].x, 0), dest.size), p_color, false, false);
  447. }
  448. }
  449. Vector<Vector2i> TextServer::shaped_text_get_line_breaks_adv(RID p_shaped, const Vector<float> &p_width, int p_start, bool p_once, uint8_t /*TextBreakFlag*/ p_break_flags) const {
  450. Vector<Vector2i> lines;
  451. ERR_FAIL_COND_V(p_width.empty(), lines);
  452. const_cast<TextServer *>(this)->shaped_text_update_breaks(p_shaped);
  453. const Vector<Glyph> &logical = const_cast<TextServer *>(this)->shaped_text_sort_logical(p_shaped);
  454. const Vector2i &range = shaped_text_get_range(p_shaped);
  455. float width = 0.f;
  456. int line_start = MAX(p_start, range.x);
  457. int last_safe_break = -1;
  458. int chunk = 0;
  459. int l_size = logical.size();
  460. const Glyph *l_gl = logical.ptr();
  461. for (int i = 0; i < l_size; i++) {
  462. if (l_gl[i].start < p_start) {
  463. continue;
  464. }
  465. if (l_gl[i].count > 0) {
  466. if ((p_width[chunk] > 0) && (width + l_gl[i].advance > p_width[chunk]) && (last_safe_break >= 0)) {
  467. lines.push_back(Vector2i(line_start, l_gl[last_safe_break].end));
  468. line_start = l_gl[last_safe_break].end;
  469. i = last_safe_break;
  470. last_safe_break = -1;
  471. width = 0;
  472. chunk++;
  473. if (chunk >= p_width.size()) {
  474. chunk = 0;
  475. if (p_once) {
  476. return lines;
  477. }
  478. }
  479. continue;
  480. }
  481. if ((p_break_flags & BREAK_MANDATORY) == BREAK_MANDATORY) {
  482. if ((l_gl[i].flags & GRAPHEME_IS_BREAK_HARD) == GRAPHEME_IS_BREAK_HARD) {
  483. lines.push_back(Vector2i(line_start, l_gl[i].end));
  484. line_start = l_gl[i].end;
  485. last_safe_break = -1;
  486. width = 0;
  487. chunk = 0;
  488. if (p_once) {
  489. return lines;
  490. }
  491. continue;
  492. }
  493. }
  494. if ((p_break_flags & BREAK_WORD_BOUND) == BREAK_WORD_BOUND) {
  495. if ((l_gl[i].flags & GRAPHEME_IS_BREAK_SOFT) == GRAPHEME_IS_BREAK_SOFT) {
  496. last_safe_break = i;
  497. }
  498. }
  499. if ((p_break_flags & BREAK_GRAPHEME_BOUND) == BREAK_GRAPHEME_BOUND) {
  500. last_safe_break = i;
  501. }
  502. }
  503. width += l_gl[i].advance;
  504. }
  505. if (l_size > 0) {
  506. lines.push_back(Vector2i(line_start, range.y));
  507. } else {
  508. lines.push_back(Vector2i(0, 0));
  509. }
  510. return lines;
  511. }
  512. Vector<Vector2i> TextServer::shaped_text_get_line_breaks(RID p_shaped, float p_width, int p_start, uint8_t /*TextBreakFlag*/ p_break_flags) const {
  513. Vector<Vector2i> lines;
  514. const_cast<TextServer *>(this)->shaped_text_update_breaks(p_shaped);
  515. const Vector<Glyph> &logical = const_cast<TextServer *>(this)->shaped_text_sort_logical(p_shaped);
  516. const Vector2i &range = shaped_text_get_range(p_shaped);
  517. float width = 0.f;
  518. int line_start = MAX(p_start, range.x);
  519. int last_safe_break = -1;
  520. int l_size = logical.size();
  521. const Glyph *l_gl = logical.ptr();
  522. for (int i = 0; i < l_size; i++) {
  523. if (l_gl[i].start < p_start) {
  524. continue;
  525. }
  526. if (l_gl[i].count > 0) {
  527. if ((p_width > 0) && (width + l_gl[i].advance > p_width) && (last_safe_break >= 0)) {
  528. lines.push_back(Vector2i(line_start, l_gl[last_safe_break].end));
  529. line_start = l_gl[last_safe_break].end;
  530. i = last_safe_break;
  531. last_safe_break = -1;
  532. width = 0;
  533. continue;
  534. }
  535. if ((p_break_flags & BREAK_MANDATORY) == BREAK_MANDATORY) {
  536. if ((l_gl[i].flags & GRAPHEME_IS_BREAK_HARD) == GRAPHEME_IS_BREAK_HARD) {
  537. lines.push_back(Vector2i(line_start, l_gl[i].end));
  538. line_start = l_gl[i].end;
  539. last_safe_break = -1;
  540. width = 0;
  541. continue;
  542. }
  543. }
  544. if ((p_break_flags & BREAK_WORD_BOUND) == BREAK_WORD_BOUND) {
  545. if ((l_gl[i].flags & GRAPHEME_IS_BREAK_SOFT) == GRAPHEME_IS_BREAK_SOFT) {
  546. last_safe_break = i;
  547. }
  548. }
  549. if ((p_break_flags & BREAK_GRAPHEME_BOUND) == BREAK_GRAPHEME_BOUND) {
  550. last_safe_break = i;
  551. }
  552. }
  553. width += l_gl[i].advance;
  554. }
  555. if (l_size > 0) {
  556. if (lines.size() == 0 || lines[lines.size() - 1].y < range.y) {
  557. lines.push_back(Vector2i(line_start, range.y));
  558. }
  559. } else {
  560. lines.push_back(Vector2i(0, 0));
  561. }
  562. return lines;
  563. }
  564. Vector<Vector2i> TextServer::shaped_text_get_word_breaks(RID p_shaped) const {
  565. Vector<Vector2i> words;
  566. const_cast<TextServer *>(this)->shaped_text_update_breaks(p_shaped);
  567. const Vector<Glyph> &logical = const_cast<TextServer *>(this)->shaped_text_sort_logical(p_shaped);
  568. const Vector2i &range = shaped_text_get_range(p_shaped);
  569. int word_start = range.x;
  570. int l_size = logical.size();
  571. const Glyph *l_gl = logical.ptr();
  572. for (int i = 0; i < l_size; i++) {
  573. if (l_gl[i].count > 0) {
  574. if ((l_gl[i].flags & GRAPHEME_IS_SPACE) == GRAPHEME_IS_SPACE) {
  575. words.push_back(Vector2i(word_start, l_gl[i].end - 1));
  576. word_start = l_gl[i].end;
  577. }
  578. }
  579. }
  580. if (l_size > 0) {
  581. words.push_back(Vector2i(word_start, range.y));
  582. }
  583. return words;
  584. }
  585. void TextServer::shaped_text_get_carets(RID p_shaped, int p_position, Rect2 &p_leading_caret, Direction &p_leading_dir, Rect2 &p_trailing_caret, Direction &p_trailing_dir) const {
  586. Vector<Rect2> carets;
  587. const Vector<TextServer::Glyph> visual = shaped_text_get_glyphs(p_shaped);
  588. TextServer::Orientation orientation = shaped_text_get_orientation(p_shaped);
  589. const Vector2 &range = shaped_text_get_range(p_shaped);
  590. float ascent = shaped_text_get_ascent(p_shaped);
  591. float descent = shaped_text_get_descent(p_shaped);
  592. float height = (ascent + descent) / 2;
  593. float off = 0.0f;
  594. p_leading_dir = DIRECTION_AUTO;
  595. p_trailing_dir = DIRECTION_AUTO;
  596. int v_size = visual.size();
  597. const Glyph *glyphs = visual.ptr();
  598. for (int i = 0; i < v_size; i++) {
  599. if (glyphs[i].count > 0) {
  600. // Caret before grapheme (top / left).
  601. if (p_position == glyphs[i].start && ((glyphs[i].flags & GRAPHEME_IS_VIRTUAL) != GRAPHEME_IS_VIRTUAL)) {
  602. Rect2 cr;
  603. if (orientation == ORIENTATION_HORIZONTAL) {
  604. if (glyphs[i].start == range.x) {
  605. cr.size.y = height * 2;
  606. } else {
  607. cr.size.y = height;
  608. }
  609. cr.position.y = -ascent;
  610. cr.position.x = off;
  611. if ((glyphs[i].flags & GRAPHEME_IS_RTL) == GRAPHEME_IS_RTL) {
  612. p_trailing_dir = DIRECTION_RTL;
  613. for (int j = 0; j < glyphs[i].count; j++) {
  614. cr.position.x += glyphs[i + j].advance * glyphs[i + j].repeat;
  615. cr.size.x -= glyphs[i + j].advance * glyphs[i + j].repeat;
  616. }
  617. } else {
  618. p_trailing_dir = DIRECTION_LTR;
  619. for (int j = 0; j < glyphs[i].count; j++) {
  620. cr.size.x += glyphs[i + j].advance * glyphs[i + j].repeat;
  621. }
  622. }
  623. } else {
  624. if (glyphs[i].start == range.x) {
  625. cr.size.x = height * 2;
  626. } else {
  627. cr.size.x = height;
  628. }
  629. cr.position.x = -ascent;
  630. cr.position.y = off;
  631. if ((glyphs[i].flags & GRAPHEME_IS_RTL) == GRAPHEME_IS_RTL) {
  632. p_trailing_dir = DIRECTION_RTL;
  633. for (int j = 0; j < glyphs[i].count; j++) {
  634. cr.position.y += glyphs[i + j].advance * glyphs[i + j].repeat;
  635. cr.size.y -= glyphs[i + j].advance * glyphs[i + j].repeat;
  636. }
  637. } else {
  638. p_trailing_dir = DIRECTION_LTR;
  639. for (int j = 0; j < glyphs[i].count; j++) {
  640. cr.size.y += glyphs[i + j].advance * glyphs[i + j].repeat;
  641. }
  642. }
  643. }
  644. p_trailing_caret = cr;
  645. }
  646. // Caret after grapheme (bottom / right).
  647. if (p_position == glyphs[i].end && ((glyphs[i].flags & GRAPHEME_IS_VIRTUAL) != GRAPHEME_IS_VIRTUAL)) {
  648. Rect2 cr;
  649. if (orientation == ORIENTATION_HORIZONTAL) {
  650. if (glyphs[i].end == range.y) {
  651. cr.size.y = height * 2;
  652. cr.position.y = -ascent;
  653. } else {
  654. cr.size.y = height;
  655. cr.position.y = -ascent + height;
  656. }
  657. cr.position.x = off;
  658. if ((glyphs[i].flags & GRAPHEME_IS_RTL) != GRAPHEME_IS_RTL) {
  659. p_leading_dir = DIRECTION_LTR;
  660. for (int j = 0; j < glyphs[i].count; j++) {
  661. cr.position.x += glyphs[i + j].advance * glyphs[i + j].repeat;
  662. cr.size.x -= glyphs[i + j].advance * glyphs[i + j].repeat;
  663. }
  664. } else {
  665. p_leading_dir = DIRECTION_RTL;
  666. for (int j = 0; j < glyphs[i].count; j++) {
  667. cr.size.x += glyphs[i + j].advance * glyphs[i + j].repeat;
  668. }
  669. }
  670. } else {
  671. cr.size.y = 1.0f;
  672. if (glyphs[i].end == range.y) {
  673. cr.size.x = height * 2;
  674. cr.position.x = -ascent;
  675. } else {
  676. cr.size.x = height;
  677. cr.position.x = -ascent + height;
  678. }
  679. cr.position.y = off;
  680. if ((glyphs[i].flags & GRAPHEME_IS_RTL) != GRAPHEME_IS_RTL) {
  681. p_leading_dir = DIRECTION_LTR;
  682. for (int j = 0; j < glyphs[i].count; j++) {
  683. cr.position.y += glyphs[i + j].advance * glyphs[i + j].repeat;
  684. cr.size.y -= glyphs[i + j].advance * glyphs[i + j].repeat;
  685. }
  686. } else {
  687. p_leading_dir = DIRECTION_RTL;
  688. for (int j = 0; j < glyphs[i].count; j++) {
  689. cr.size.y += glyphs[i + j].advance * glyphs[i + j].repeat;
  690. }
  691. }
  692. }
  693. p_leading_caret = cr;
  694. }
  695. // Caret inside grapheme (middle).
  696. if (p_position > glyphs[i].start && p_position < glyphs[i].end && (glyphs[i].flags & GRAPHEME_IS_VIRTUAL) != GRAPHEME_IS_VIRTUAL) {
  697. float advance = 0.f;
  698. for (int j = 0; j < glyphs[i].count; j++) {
  699. advance += glyphs[i + j].advance * glyphs[i + j].repeat;
  700. }
  701. float char_adv = advance / (float)(glyphs[i].end - glyphs[i].start);
  702. Rect2 cr;
  703. if (orientation == ORIENTATION_HORIZONTAL) {
  704. cr.size.x = 1.0f;
  705. cr.size.y = height * 2;
  706. cr.position.y = -ascent;
  707. if ((glyphs[i].flags & GRAPHEME_IS_RTL) == GRAPHEME_IS_RTL) {
  708. cr.position.x = off + char_adv * (glyphs[i].end - p_position);
  709. } else {
  710. cr.position.x = off + char_adv * (p_position - glyphs[i].start);
  711. }
  712. } else {
  713. cr.size.y = 1.0f;
  714. cr.size.x = height * 2;
  715. cr.position.x = -ascent;
  716. if ((glyphs[i].flags & GRAPHEME_IS_RTL) == GRAPHEME_IS_RTL) {
  717. cr.position.y = off + char_adv * (glyphs[i].end - p_position);
  718. } else {
  719. cr.position.y = off + char_adv * (p_position - glyphs[i].start);
  720. }
  721. }
  722. p_trailing_caret = cr;
  723. p_leading_caret = cr;
  724. }
  725. }
  726. off += glyphs[i].advance * glyphs[i].repeat;
  727. }
  728. }
  729. TextServer::Direction TextServer::shaped_text_get_dominant_direciton_in_range(RID p_shaped, int p_start, int p_end) const {
  730. const Vector<TextServer::Glyph> visual = shaped_text_get_glyphs(p_shaped);
  731. if (p_start == p_end) {
  732. return DIRECTION_AUTO;
  733. }
  734. int start = MIN(p_start, p_end);
  735. int end = MAX(p_start, p_end);
  736. int rtl = 0;
  737. int ltr = 0;
  738. int v_size = visual.size();
  739. const Glyph *glyphs = visual.ptr();
  740. for (int i = 0; i < v_size; i++) {
  741. if ((glyphs[i].end > start) && (glyphs[i].start < end)) {
  742. if (glyphs[i].count > 0) {
  743. if ((glyphs[i].flags & GRAPHEME_IS_RTL) == GRAPHEME_IS_RTL) {
  744. rtl++;
  745. } else {
  746. ltr++;
  747. }
  748. }
  749. }
  750. }
  751. if (ltr == rtl) {
  752. return DIRECTION_AUTO;
  753. } else if (ltr > rtl) {
  754. return DIRECTION_LTR;
  755. } else {
  756. return DIRECTION_RTL;
  757. }
  758. }
  759. Vector<Vector2> TextServer::shaped_text_get_selection(RID p_shaped, int p_start, int p_end) const {
  760. Vector<Vector2> ranges;
  761. const Vector<TextServer::Glyph> visual = shaped_text_get_glyphs(p_shaped);
  762. if (p_start == p_end) {
  763. return ranges;
  764. }
  765. int start = MIN(p_start, p_end);
  766. int end = MAX(p_start, p_end);
  767. int v_size = visual.size();
  768. const Glyph *glyphs = visual.ptr();
  769. float off = 0.0f;
  770. for (int i = 0; i < v_size; i++) {
  771. for (int k = 0; k < glyphs[i].repeat; k++) {
  772. if (glyphs[i].count > 0 && glyphs[i].index != 0) {
  773. if (glyphs[i].start < end && glyphs[i].end > start) {
  774. // Grapheme fully in selection range.
  775. if (glyphs[i].start >= start && glyphs[i].end <= end) {
  776. float advance = 0.f;
  777. for (int j = 0; j < glyphs[i].count; j++) {
  778. advance += glyphs[i + j].advance;
  779. }
  780. ranges.push_back(Vector2(off, off + advance));
  781. }
  782. // Only start of grapheme is in selection range.
  783. if (glyphs[i].start >= start && glyphs[i].end > end) {
  784. float advance = 0.f;
  785. for (int j = 0; j < glyphs[i].count; j++) {
  786. advance += glyphs[i + j].advance;
  787. }
  788. float char_adv = advance / (float)(glyphs[i].end - glyphs[i].start);
  789. if ((glyphs[i].flags & GRAPHEME_IS_RTL) == GRAPHEME_IS_RTL) {
  790. ranges.push_back(Vector2(off + char_adv * (glyphs[i].end - end), off + advance));
  791. } else {
  792. ranges.push_back(Vector2(off, off + char_adv * (end - glyphs[i].start)));
  793. }
  794. }
  795. // Only end of grapheme is in selection range.
  796. if (glyphs[i].start < start && glyphs[i].end <= end) {
  797. float advance = 0.f;
  798. for (int j = 0; j < glyphs[i].count; j++) {
  799. advance += glyphs[i + j].advance;
  800. }
  801. float char_adv = advance / (float)(glyphs[i].end - glyphs[i].start);
  802. if ((glyphs[i].flags & GRAPHEME_IS_RTL) == GRAPHEME_IS_RTL) {
  803. ranges.push_back(Vector2(off, off + char_adv * (start - glyphs[i].start)));
  804. } else {
  805. ranges.push_back(Vector2(off + char_adv * (glyphs[i].end - start), off + advance));
  806. }
  807. }
  808. // Selection range is within grapheme
  809. if (glyphs[i].start < start && glyphs[i].end > end) {
  810. float advance = 0.f;
  811. for (int j = 0; j < glyphs[i].count; j++) {
  812. advance += glyphs[i + j].advance;
  813. }
  814. float char_adv = advance / (float)(glyphs[i].end - glyphs[i].start);
  815. if ((glyphs[i].flags & GRAPHEME_IS_RTL) == GRAPHEME_IS_RTL) {
  816. ranges.push_back(Vector2(off + char_adv * (glyphs[i].end - end), off + char_adv * (glyphs[i].end - start)));
  817. } else {
  818. ranges.push_back(Vector2(off + char_adv * (start - glyphs[i].start), off + char_adv * (end - glyphs[i].start)));
  819. }
  820. }
  821. }
  822. }
  823. off += glyphs[i].advance;
  824. }
  825. }
  826. // Merge intersecting ranges.
  827. int i = 0;
  828. while (i < ranges.size()) {
  829. int j = i + 1;
  830. while (j < ranges.size()) {
  831. if (Math::is_equal_approx(ranges[i].y, ranges[j].x, UNIT_EPSILON)) {
  832. ranges.write[i].y = ranges[j].y;
  833. ranges.remove(j);
  834. continue;
  835. }
  836. j++;
  837. }
  838. i++;
  839. }
  840. return ranges;
  841. }
  842. int TextServer::shaped_text_hit_test_grapheme(RID p_shaped, float p_coords) const {
  843. const Vector<TextServer::Glyph> visual = shaped_text_get_glyphs(p_shaped);
  844. // Exact grapheme hit test, return -1 if missed.
  845. float off = 0.0f;
  846. int v_size = visual.size();
  847. const Glyph *glyphs = visual.ptr();
  848. for (int i = 0; i < v_size; i++) {
  849. for (int j = 0; j < glyphs[i].repeat; j++) {
  850. if (p_coords >= off && p_coords < off + glyphs[i].advance) {
  851. return i;
  852. }
  853. off += glyphs[i].advance;
  854. }
  855. }
  856. return -1;
  857. }
  858. int TextServer::shaped_text_hit_test_position(RID p_shaped, float p_coords) const {
  859. const Vector<TextServer::Glyph> visual = shaped_text_get_glyphs(p_shaped);
  860. int v_size = visual.size();
  861. const Glyph *glyphs = visual.ptr();
  862. // Cursor placement hit test.
  863. // Place caret to the left of the leftmost grapheme, or to position 0 if string is empty.
  864. if (p_coords <= 0) {
  865. if (v_size > 0) {
  866. if ((glyphs[0].flags & GRAPHEME_IS_RTL) == GRAPHEME_IS_RTL) {
  867. return glyphs[0].end;
  868. } else {
  869. return glyphs[0].start;
  870. }
  871. } else {
  872. return 0;
  873. }
  874. }
  875. // Place caret to the right of the rightmost grapheme, or to position 0 if string is empty.
  876. if (p_coords >= shaped_text_get_width(p_shaped)) {
  877. if (v_size > 0) {
  878. if ((glyphs[v_size - 1].flags & GRAPHEME_IS_RTL) == GRAPHEME_IS_RTL) {
  879. return glyphs[v_size - 1].start;
  880. } else {
  881. return glyphs[v_size - 1].end;
  882. }
  883. } else {
  884. return 0;
  885. }
  886. }
  887. float off = 0.0f;
  888. for (int i = 0; i < v_size; i++) {
  889. for (int k = 0; k < glyphs[i].repeat; k++) {
  890. if (glyphs[i].count > 0) {
  891. float advance = 0.f;
  892. for (int j = 0; j < glyphs[i].count; j++) {
  893. advance += glyphs[i + j].advance;
  894. }
  895. // Place caret to the left of clicked grapheme.
  896. if (p_coords >= off && p_coords < off + advance / 2) {
  897. if ((glyphs[i].flags & GRAPHEME_IS_RTL) == GRAPHEME_IS_RTL) {
  898. return glyphs[i].end;
  899. } else {
  900. return glyphs[i].start;
  901. }
  902. }
  903. // Place caret to the right of clicked grapheme.
  904. if (p_coords >= off + advance / 2 && p_coords < off + advance) {
  905. if ((glyphs[i].flags & GRAPHEME_IS_RTL) == GRAPHEME_IS_RTL) {
  906. return glyphs[i].start;
  907. } else {
  908. return glyphs[i].end;
  909. }
  910. }
  911. }
  912. off += glyphs[i].advance;
  913. }
  914. }
  915. return 0;
  916. }
  917. int TextServer::shaped_text_next_grapheme_pos(RID p_shaped, int p_pos) {
  918. const Vector<TextServer::Glyph> visual = shaped_text_get_glyphs(p_shaped);
  919. int v_size = visual.size();
  920. const Glyph *glyphs = visual.ptr();
  921. for (int i = 0; i < v_size; i++) {
  922. if (p_pos >= glyphs[i].start && p_pos < glyphs[i].end) {
  923. return glyphs[i].end;
  924. }
  925. }
  926. return p_pos;
  927. }
  928. int TextServer::shaped_text_prev_grapheme_pos(RID p_shaped, int p_pos) {
  929. const Vector<TextServer::Glyph> visual = shaped_text_get_glyphs(p_shaped);
  930. int v_size = visual.size();
  931. const Glyph *glyphs = visual.ptr();
  932. for (int i = 0; i < v_size; i++) {
  933. if (p_pos > glyphs[i].start && p_pos <= glyphs[i].end) {
  934. return glyphs[i].start;
  935. }
  936. }
  937. return p_pos;
  938. }
  939. 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 {
  940. const Vector<TextServer::Glyph> visual = shaped_text_get_glyphs(p_shaped);
  941. TextServer::Orientation orientation = shaped_text_get_orientation(p_shaped);
  942. bool hex_codes = shaped_text_get_preserve_control(p_shaped) || shaped_text_get_preserve_invalid(p_shaped);
  943. int v_size = visual.size();
  944. const Glyph *glyphs = visual.ptr();
  945. Vector2 ofs = p_pos;
  946. // Draw at the baseline.
  947. for (int i = 0; i < v_size; i++) {
  948. for (int j = 0; j < glyphs[i].repeat; j++) {
  949. if (p_clip_r > 0) {
  950. // Clip right / bottom.
  951. if (orientation == ORIENTATION_HORIZONTAL) {
  952. if (ofs.x - p_pos.x > p_clip_r) {
  953. return;
  954. }
  955. } else {
  956. if (ofs.y - p_pos.y > p_clip_r) {
  957. return;
  958. }
  959. }
  960. }
  961. if (p_clip_l > 0) {
  962. // Clip left / top.
  963. if (orientation == ORIENTATION_HORIZONTAL) {
  964. if (ofs.x - p_pos.x < p_clip_l) {
  965. ofs.x += glyphs[i].advance;
  966. continue;
  967. }
  968. } else {
  969. if (ofs.y - p_pos.y < p_clip_l) {
  970. ofs.y += glyphs[i].advance;
  971. continue;
  972. }
  973. }
  974. }
  975. if (glyphs[i].font_rid != RID()) {
  976. 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);
  977. } else if (hex_codes && ((glyphs[i].flags & GRAPHEME_IS_VIRTUAL) != GRAPHEME_IS_VIRTUAL)) {
  978. 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);
  979. }
  980. if (orientation == ORIENTATION_HORIZONTAL) {
  981. ofs.x += glyphs[i].advance;
  982. } else {
  983. ofs.y += glyphs[i].advance;
  984. }
  985. }
  986. }
  987. }
  988. 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 {
  989. const Vector<TextServer::Glyph> visual = shaped_text_get_glyphs(p_shaped);
  990. TextServer::Orientation orientation = shaped_text_get_orientation(p_shaped);
  991. int v_size = visual.size();
  992. const Glyph *glyphs = visual.ptr();
  993. Vector2 ofs = p_pos;
  994. // Draw at the baseline.
  995. for (int i = 0; i < v_size; i++) {
  996. for (int j = 0; j < glyphs[i].repeat; j++) {
  997. if (p_clip_r > 0) {
  998. // Clip right / bottom.
  999. if (orientation == ORIENTATION_HORIZONTAL) {
  1000. if (ofs.x - p_pos.x > p_clip_r) {
  1001. return;
  1002. }
  1003. } else {
  1004. if (ofs.y - p_pos.y > p_clip_r) {
  1005. return;
  1006. }
  1007. }
  1008. }
  1009. if (p_clip_l > 0) {
  1010. // Clip left / top.
  1011. if (orientation == ORIENTATION_HORIZONTAL) {
  1012. if (ofs.x - p_pos.x < p_clip_l) {
  1013. ofs.x += glyphs[i].advance;
  1014. continue;
  1015. }
  1016. } else {
  1017. if (ofs.y - p_pos.y < p_clip_l) {
  1018. ofs.y += glyphs[i].advance;
  1019. continue;
  1020. }
  1021. }
  1022. }
  1023. if (glyphs[i].font_rid != RID()) {
  1024. 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);
  1025. }
  1026. if (orientation == ORIENTATION_HORIZONTAL) {
  1027. ofs.x += glyphs[i].advance;
  1028. } else {
  1029. ofs.y += glyphs[i].advance;
  1030. }
  1031. }
  1032. }
  1033. }
  1034. RID TextServer::_create_font_memory(const PackedByteArray &p_data, const String &p_type, int p_base_size) {
  1035. return create_font_memory(p_data.ptr(), p_data.size(), p_type, p_base_size);
  1036. }
  1037. void TextServer::_shaped_text_set_bidi_override(RID p_shaped, const Array &p_override) {
  1038. Vector<Vector2i> overrides;
  1039. for (int i = 0; i < p_override.size(); i++) {
  1040. overrides.push_back(p_override[i]);
  1041. }
  1042. shaped_text_set_bidi_override(p_shaped, overrides);
  1043. }
  1044. Array TextServer::_shaped_text_get_glyphs(RID p_shaped) const {
  1045. Array ret;
  1046. Vector<Glyph> glyphs = shaped_text_get_glyphs(p_shaped);
  1047. for (int i = 0; i < glyphs.size(); i++) {
  1048. Dictionary glyph;
  1049. glyph["start"] = glyphs[i].start;
  1050. glyph["end"] = glyphs[i].end;
  1051. glyph["repeat"] = glyphs[i].repeat;
  1052. glyph["count"] = glyphs[i].count;
  1053. glyph["flags"] = glyphs[i].flags;
  1054. glyph["offset"] = Vector2(glyphs[i].x_off, glyphs[i].y_off);
  1055. glyph["advance"] = glyphs[i].advance;
  1056. glyph["font_rid"] = glyphs[i].font_rid;
  1057. glyph["font_size"] = glyphs[i].font_size;
  1058. glyph["index"] = glyphs[i].index;
  1059. ret.push_back(glyph);
  1060. }
  1061. return ret;
  1062. }
  1063. Array TextServer::_shaped_text_get_line_breaks_adv(RID p_shaped, const PackedFloat32Array &p_width, int p_start, bool p_once, uint8_t p_break_flags) const {
  1064. Array ret;
  1065. Vector<Vector2i> lines = shaped_text_get_line_breaks_adv(p_shaped, p_width, p_start, p_once, p_break_flags);
  1066. for (int i = 0; i < lines.size(); i++) {
  1067. ret.push_back(lines[i]);
  1068. }
  1069. return ret;
  1070. }
  1071. Array TextServer::_shaped_text_get_line_breaks(RID p_shaped, float p_width, int p_start, uint8_t p_break_flags) const {
  1072. Array ret;
  1073. Vector<Vector2i> lines = shaped_text_get_line_breaks(p_shaped, p_width, p_start, p_break_flags);
  1074. for (int i = 0; i < lines.size(); i++) {
  1075. ret.push_back(lines[i]);
  1076. }
  1077. return ret;
  1078. }
  1079. Array TextServer::_shaped_text_get_word_breaks(RID p_shaped) const {
  1080. Array ret;
  1081. Vector<Vector2i> words = shaped_text_get_word_breaks(p_shaped);
  1082. for (int i = 0; i < words.size(); i++) {
  1083. ret.push_back(words[i]);
  1084. }
  1085. return ret;
  1086. }
  1087. Dictionary TextServer::_shaped_text_get_carets(RID p_shaped, int p_position) const {
  1088. Dictionary ret;
  1089. Rect2 l_caret, t_caret;
  1090. Direction l_dir, t_dir;
  1091. shaped_text_get_carets(p_shaped, p_position, l_caret, l_dir, t_caret, t_dir);
  1092. ret["leading_rect"] = l_caret;
  1093. ret["leading_direction"] = l_dir;
  1094. ret["trailing_rect"] = t_caret;
  1095. ret["trailing_direction"] = t_dir;
  1096. return ret;
  1097. }
  1098. Array TextServer::_shaped_text_get_selection(RID p_shaped, int p_start, int p_end) const {
  1099. Array ret;
  1100. Vector<Vector2> ranges = shaped_text_get_selection(p_shaped, p_start, p_end);
  1101. for (int i = 0; i < ranges.size(); i++) {
  1102. ret.push_back(ranges[i]);
  1103. }
  1104. return ret;
  1105. }
  1106. TextServer::TextServer() {
  1107. }
  1108. TextServer::~TextServer() {
  1109. }