font.cpp 61 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657
  1. /*************************************************************************/
  2. /* font.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 "font.h"
  31. #include "core/io/resource_loader.h"
  32. #include "core/string/translation.h"
  33. #include "core/templates/hashfuncs.h"
  34. #include "scene/resources/text_line.h"
  35. #include "scene/resources/text_paragraph.h"
  36. _FORCE_INLINE_ void FontData::_clear_cache() {
  37. for (int i = 0; i < cache.size(); i++) {
  38. if (cache[i].is_valid()) {
  39. TS->free(cache[i]);
  40. cache.write[i] = RID();
  41. }
  42. }
  43. }
  44. _FORCE_INLINE_ void FontData::_ensure_rid(int p_cache_index) const {
  45. if (unlikely(p_cache_index >= cache.size())) {
  46. cache.resize(p_cache_index + 1);
  47. }
  48. if (unlikely(!cache[p_cache_index].is_valid())) {
  49. cache.write[p_cache_index] = TS->create_font();
  50. TS->font_set_data_ptr(cache[p_cache_index], data_ptr, data_size);
  51. TS->font_set_antialiased(cache[p_cache_index], antialiased);
  52. TS->font_set_multichannel_signed_distance_field(cache[p_cache_index], msdf);
  53. TS->font_set_msdf_pixel_range(cache[p_cache_index], msdf_pixel_range);
  54. TS->font_set_msdf_size(cache[p_cache_index], msdf_size);
  55. TS->font_set_fixed_size(cache[p_cache_index], fixed_size);
  56. TS->font_set_force_autohinter(cache[p_cache_index], force_autohinter);
  57. TS->font_set_hinting(cache[p_cache_index], hinting);
  58. TS->font_set_oversampling(cache[p_cache_index], oversampling);
  59. }
  60. }
  61. void FontData::_bind_methods() {
  62. ClassDB::bind_method(D_METHOD("set_data", "data"), &FontData::set_data);
  63. ClassDB::bind_method(D_METHOD("get_data"), &FontData::get_data);
  64. ClassDB::bind_method(D_METHOD("set_antialiased", "antialiased"), &FontData::set_antialiased);
  65. ClassDB::bind_method(D_METHOD("is_antialiased"), &FontData::is_antialiased);
  66. ClassDB::bind_method(D_METHOD("set_multichannel_signed_distance_field", "msdf"), &FontData::set_multichannel_signed_distance_field);
  67. ClassDB::bind_method(D_METHOD("is_multichannel_signed_distance_field"), &FontData::is_multichannel_signed_distance_field);
  68. ClassDB::bind_method(D_METHOD("set_msdf_pixel_range", "msdf_pixel_range"), &FontData::set_msdf_pixel_range);
  69. ClassDB::bind_method(D_METHOD("get_msdf_pixel_range"), &FontData::get_msdf_pixel_range);
  70. ClassDB::bind_method(D_METHOD("set_msdf_size", "msdf_size"), &FontData::set_msdf_size);
  71. ClassDB::bind_method(D_METHOD("get_msdf_size"), &FontData::get_msdf_size);
  72. ClassDB::bind_method(D_METHOD("set_force_autohinter", "force_autohinter"), &FontData::set_force_autohinter);
  73. ClassDB::bind_method(D_METHOD("is_force_autohinter"), &FontData::is_force_autohinter);
  74. ClassDB::bind_method(D_METHOD("set_hinting", "hinting"), &FontData::set_hinting);
  75. ClassDB::bind_method(D_METHOD("get_hinting"), &FontData::get_hinting);
  76. ClassDB::bind_method(D_METHOD("set_oversampling", "oversampling"), &FontData::set_oversampling);
  77. ClassDB::bind_method(D_METHOD("get_oversampling"), &FontData::get_oversampling);
  78. ClassDB::bind_method(D_METHOD("find_cache", "variation_coordinates"), &FontData::find_cache);
  79. ClassDB::bind_method(D_METHOD("get_cache_count"), &FontData::get_cache_count);
  80. ClassDB::bind_method(D_METHOD("clear_cache"), &FontData::clear_cache);
  81. ClassDB::bind_method(D_METHOD("remove_cache", "cache_index"), &FontData::remove_cache);
  82. ClassDB::bind_method(D_METHOD("get_size_cache_list", "cache_index"), &FontData::get_size_cache_list);
  83. ClassDB::bind_method(D_METHOD("clear_size_cache", "cache_index"), &FontData::clear_size_cache);
  84. ClassDB::bind_method(D_METHOD("remove_size_cache", "cache_index", "size"), &FontData::remove_size_cache);
  85. ClassDB::bind_method(D_METHOD("set_variation_coordinates", "cache_index", "variation_coordinates"), &FontData::set_variation_coordinates);
  86. ClassDB::bind_method(D_METHOD("get_variation_coordinates", "cache_index"), &FontData::get_variation_coordinates);
  87. ClassDB::bind_method(D_METHOD("set_ascent", "cache_index", "size", "ascent"), &FontData::set_ascent);
  88. ClassDB::bind_method(D_METHOD("get_ascent", "cache_index", "size"), &FontData::get_ascent);
  89. ClassDB::bind_method(D_METHOD("set_descent", "cache_index", "size", "descent"), &FontData::set_descent);
  90. ClassDB::bind_method(D_METHOD("get_descent", "cache_index", "size"), &FontData::get_descent);
  91. ClassDB::bind_method(D_METHOD("set_underline_position", "cache_index", "size", "underline_position"), &FontData::set_underline_position);
  92. ClassDB::bind_method(D_METHOD("get_underline_position", "cache_index", "size"), &FontData::get_underline_position);
  93. ClassDB::bind_method(D_METHOD("set_underline_thickness", "cache_index", "size", "underline_thickness"), &FontData::set_underline_thickness);
  94. ClassDB::bind_method(D_METHOD("get_underline_thickness", "cache_index", "size"), &FontData::get_underline_thickness);
  95. ClassDB::bind_method(D_METHOD("set_scale", "cache_index", "size", "scale"), &FontData::set_scale);
  96. ClassDB::bind_method(D_METHOD("get_scale", "cache_index", "size"), &FontData::get_scale);
  97. ClassDB::bind_method(D_METHOD("set_spacing", "cache_index", "size", "spacing"), &FontData::set_spacing);
  98. ClassDB::bind_method(D_METHOD("get_spacing", "cache_index", "size"), &FontData::get_spacing);
  99. ClassDB::bind_method(D_METHOD("get_texture_count", "cache_index", "size"), &FontData::get_texture_count);
  100. ClassDB::bind_method(D_METHOD("clear_textures", "cache_index", "size"), &FontData::clear_textures);
  101. ClassDB::bind_method(D_METHOD("remove_texture", "cache_index", "size", "texture_index"), &FontData::remove_texture);
  102. ClassDB::bind_method(D_METHOD("set_texture_image", "cache_index", "size", "texture_index", "image"), &FontData::set_texture_image);
  103. ClassDB::bind_method(D_METHOD("get_texture_image", "cache_index", "size", "texture_index"), &FontData::get_texture_image);
  104. ClassDB::bind_method(D_METHOD("set_texture_offsets", "cache_index", "size", "texture_index", "offset"), &FontData::set_texture_offsets);
  105. ClassDB::bind_method(D_METHOD("get_texture_offsets", "cache_index", "size", "texture_index"), &FontData::get_texture_offsets);
  106. ClassDB::bind_method(D_METHOD("get_glyph_list", "cache_index", "size"), &FontData::get_glyph_list);
  107. ClassDB::bind_method(D_METHOD("clear_glyphs", "cache_index", "size"), &FontData::clear_glyphs);
  108. ClassDB::bind_method(D_METHOD("remove_glyph", "cache_index", "size", "glyph"), &FontData::remove_glyph);
  109. ClassDB::bind_method(D_METHOD("set_glyph_advance", "cache_index", "size", "glyph", "advance"), &FontData::set_glyph_advance);
  110. ClassDB::bind_method(D_METHOD("get_glyph_advance", "cache_index", "size", "glyph"), &FontData::get_glyph_advance);
  111. ClassDB::bind_method(D_METHOD("set_glyph_offset", "cache_index", "size", "glyph", "offset"), &FontData::set_glyph_offset);
  112. ClassDB::bind_method(D_METHOD("get_glyph_offset", "cache_index", "size", "glyph"), &FontData::get_glyph_offset);
  113. ClassDB::bind_method(D_METHOD("set_glyph_size", "cache_index", "size", "glyph", "gl_size"), &FontData::set_glyph_size);
  114. ClassDB::bind_method(D_METHOD("get_glyph_size", "cache_index", "size", "glyph"), &FontData::get_glyph_size);
  115. ClassDB::bind_method(D_METHOD("set_glyph_uv_rect", "cache_index", "size", "glyph", "uv_rect"), &FontData::set_glyph_uv_rect);
  116. ClassDB::bind_method(D_METHOD("get_glyph_uv_rect", "cache_index", "size", "glyph"), &FontData::get_glyph_uv_rect);
  117. ClassDB::bind_method(D_METHOD("set_glyph_texture_idx", "cache_index", "size", "glyph", "texture_idx"), &FontData::set_glyph_texture_idx);
  118. ClassDB::bind_method(D_METHOD("get_glyph_texture_idx", "cache_index", "size", "glyph"), &FontData::get_glyph_texture_idx);
  119. ClassDB::bind_method(D_METHOD("get_kerning_list", "cache_index", "size"), &FontData::get_kerning_list);
  120. ClassDB::bind_method(D_METHOD("clear_kerning_map", "cache_index", "size"), &FontData::clear_kerning_map);
  121. ClassDB::bind_method(D_METHOD("remove_kerning", "cache_index", "size", "glyph_pair"), &FontData::remove_kerning);
  122. ClassDB::bind_method(D_METHOD("set_kerning", "cache_index", "size", "glyph_pair", "kerning"), &FontData::set_kerning);
  123. ClassDB::bind_method(D_METHOD("get_kerning", "cache_index", "size", "glyph_pair"), &FontData::get_kerning);
  124. ClassDB::bind_method(D_METHOD("render_range", "cache_index", "size", "start", "end"), &FontData::render_range);
  125. ClassDB::bind_method(D_METHOD("render_glyph", "cache_index", "size", "index"), &FontData::render_glyph);
  126. ClassDB::bind_method(D_METHOD("get_cache_rid", "cache_index"), &FontData::get_cache_rid);
  127. ClassDB::bind_method(D_METHOD("is_language_supported", "language"), &FontData::is_language_supported);
  128. ClassDB::bind_method(D_METHOD("set_language_support_override", "language", "supported"), &FontData::set_language_support_override);
  129. ClassDB::bind_method(D_METHOD("get_language_support_override", "language"), &FontData::get_language_support_override);
  130. ClassDB::bind_method(D_METHOD("remove_language_support_override", "language"), &FontData::remove_language_support_override);
  131. ClassDB::bind_method(D_METHOD("get_language_support_overrides"), &FontData::get_language_support_overrides);
  132. ClassDB::bind_method(D_METHOD("is_script_supported", "script"), &FontData::is_script_supported);
  133. ClassDB::bind_method(D_METHOD("set_script_support_override", "script", "supported"), &FontData::set_script_support_override);
  134. ClassDB::bind_method(D_METHOD("get_script_support_override", "script"), &FontData::get_script_support_override);
  135. ClassDB::bind_method(D_METHOD("remove_script_support_override", "script"), &FontData::remove_script_support_override);
  136. ClassDB::bind_method(D_METHOD("get_script_support_overrides"), &FontData::get_script_support_overrides);
  137. ClassDB::bind_method(D_METHOD("has_char", "char"), &FontData::has_char);
  138. ClassDB::bind_method(D_METHOD("get_supported_chars"), &FontData::get_supported_chars);
  139. ClassDB::bind_method(D_METHOD("get_glyph_index", "char", "variation_selector"), &FontData::get_glyph_index);
  140. ClassDB::bind_method(D_METHOD("get_supported_feature_list"), &FontData::get_supported_feature_list);
  141. ClassDB::bind_method(D_METHOD("get_supported_variation_list"), &FontData::get_supported_variation_list);
  142. }
  143. bool FontData::_set(const StringName &p_name, const Variant &p_value) {
  144. Vector<String> tokens = p_name.operator String().split("/");
  145. if (tokens.size() == 1) {
  146. if (tokens[0] == "data") {
  147. set_data(p_value);
  148. return true;
  149. } else if (tokens[0] == "antialiased") {
  150. set_antialiased(p_value);
  151. return true;
  152. } else if (tokens[0] == "multichannel_signed_distance_field") {
  153. set_multichannel_signed_distance_field(p_value);
  154. return true;
  155. } else if (tokens[0] == "msdf_pixel_range") {
  156. set_msdf_pixel_range(p_value);
  157. return true;
  158. } else if (tokens[0] == "msdf_size") {
  159. set_msdf_size(p_value);
  160. return true;
  161. } else if (tokens[0] == "fixed_size") {
  162. set_fixed_size(p_value);
  163. return true;
  164. } else if (tokens[0] == "hinting") {
  165. set_hinting((TextServer::Hinting)p_value.operator int());
  166. return true;
  167. } else if (tokens[0] == "force_autohinter") {
  168. set_force_autohinter(p_value);
  169. return true;
  170. } else if (tokens[0] == "oversampling") {
  171. set_oversampling(p_value);
  172. return true;
  173. }
  174. } else if (tokens.size() == 2 && tokens[0] == "language_support_override") {
  175. String lang = tokens[1];
  176. set_language_support_override(lang, p_value);
  177. return true;
  178. } else if (tokens.size() == 2 && tokens[0] == "script_support_override") {
  179. String script = tokens[1];
  180. set_script_support_override(script, p_value);
  181. return true;
  182. } else if (tokens.size() >= 3 && tokens[0] == "cache") {
  183. int cache_index = tokens[1].to_int();
  184. if (tokens.size() == 3 && tokens[2] == "variation_coordinates") {
  185. set_variation_coordinates(cache_index, p_value);
  186. return true;
  187. }
  188. if (tokens.size() >= 5) {
  189. Vector2i sz = Vector2i(tokens[2].to_int(), tokens[3].to_int());
  190. if (tokens[4] == "ascent") {
  191. set_ascent(cache_index, sz.x, p_value);
  192. return true;
  193. } else if (tokens[4] == "descent") {
  194. set_descent(cache_index, sz.x, p_value);
  195. return true;
  196. } else if (tokens[4] == "underline_position") {
  197. set_underline_position(cache_index, sz.x, p_value);
  198. return true;
  199. } else if (tokens[4] == "underline_thickness") {
  200. set_underline_thickness(cache_index, sz.x, p_value);
  201. return true;
  202. } else if (tokens[4] == "scale") {
  203. set_scale(cache_index, sz.x, p_value);
  204. return true;
  205. } else if (tokens[4] == "spacing_glyph") {
  206. set_spacing(cache_index, sz.x, TextServer::SPACING_GLYPH, p_value);
  207. return true;
  208. } else if (tokens[4] == "spacing_space") {
  209. set_spacing(cache_index, sz.x, TextServer::SPACING_SPACE, p_value);
  210. return true;
  211. } else if (tokens.size() == 7 && tokens[4] == "textures") {
  212. int texture_index = tokens[5].to_int();
  213. if (tokens[6] == "image") {
  214. set_texture_image(cache_index, sz, texture_index, p_value);
  215. return true;
  216. } else if (tokens[6] == "offsets") {
  217. set_texture_offsets(cache_index, sz, texture_index, p_value);
  218. return true;
  219. }
  220. } else if (tokens.size() == 7 && tokens[4] == "glyphs") {
  221. int32_t glyph_index = tokens[5].to_int();
  222. if (tokens[6] == "advance") {
  223. set_glyph_advance(cache_index, sz.x, glyph_index, p_value);
  224. return true;
  225. } else if (tokens[6] == "offset") {
  226. set_glyph_offset(cache_index, sz, glyph_index, p_value);
  227. return true;
  228. } else if (tokens[6] == "size") {
  229. set_glyph_size(cache_index, sz, glyph_index, p_value);
  230. return true;
  231. } else if (tokens[6] == "uv_rect") {
  232. set_glyph_uv_rect(cache_index, sz, glyph_index, p_value);
  233. return true;
  234. } else if (tokens[6] == "texture_idx") {
  235. set_glyph_texture_idx(cache_index, sz, glyph_index, p_value);
  236. return true;
  237. }
  238. } else if (tokens.size() == 7 && tokens[4] == "kerning_overrides") {
  239. Vector2i gp = Vector2i(tokens[5].to_int(), tokens[6].to_int());
  240. set_kerning(cache_index, sz.x, gp, p_value);
  241. return true;
  242. }
  243. }
  244. }
  245. return false;
  246. }
  247. bool FontData::_get(const StringName &p_name, Variant &r_ret) const {
  248. Vector<String> tokens = p_name.operator String().split("/");
  249. if (tokens.size() == 1) {
  250. if (tokens[0] == "data") {
  251. r_ret = get_data();
  252. return true;
  253. } else if (tokens[0] == "antialiased") {
  254. r_ret = is_antialiased();
  255. return true;
  256. } else if (tokens[0] == "multichannel_signed_distance_field") {
  257. r_ret = is_multichannel_signed_distance_field();
  258. return true;
  259. } else if (tokens[0] == "msdf_pixel_range") {
  260. r_ret = get_msdf_pixel_range();
  261. return true;
  262. } else if (tokens[0] == "msdf_size") {
  263. r_ret = get_msdf_size();
  264. return true;
  265. } else if (tokens[0] == "fixed_size") {
  266. r_ret = get_fixed_size();
  267. return true;
  268. } else if (tokens[0] == "hinting") {
  269. r_ret = get_hinting();
  270. return true;
  271. } else if (tokens[0] == "force_autohinter") {
  272. r_ret = is_force_autohinter();
  273. return true;
  274. } else if (tokens[0] == "oversampling") {
  275. r_ret = get_oversampling();
  276. return true;
  277. }
  278. } else if (tokens.size() == 2 && tokens[0] == "language_support_override") {
  279. String lang = tokens[1];
  280. r_ret = get_language_support_override(lang);
  281. return true;
  282. } else if (tokens.size() == 2 && tokens[0] == "script_support_override") {
  283. String script = tokens[1];
  284. r_ret = get_script_support_override(script);
  285. return true;
  286. } else if (tokens.size() >= 3 && tokens[0] == "cache") {
  287. int cache_index = tokens[1].to_int();
  288. if (tokens.size() == 3 && tokens[2] == "variation_coordinates") {
  289. r_ret = get_variation_coordinates(cache_index);
  290. return true;
  291. }
  292. if (tokens.size() >= 5) {
  293. Vector2i sz = Vector2i(tokens[2].to_int(), tokens[3].to_int());
  294. if (tokens[4] == "ascent") {
  295. r_ret = get_ascent(cache_index, sz.x);
  296. return true;
  297. } else if (tokens[4] == "descent") {
  298. r_ret = get_descent(cache_index, sz.x);
  299. return true;
  300. } else if (tokens[4] == "underline_position") {
  301. r_ret = get_underline_position(cache_index, sz.x);
  302. return true;
  303. } else if (tokens[4] == "underline_thickness") {
  304. r_ret = get_underline_thickness(cache_index, sz.x);
  305. return true;
  306. } else if (tokens[4] == "scale") {
  307. r_ret = get_scale(cache_index, sz.x);
  308. return true;
  309. } else if (tokens[4] == "spacing_glyph") {
  310. r_ret = get_spacing(cache_index, sz.x, TextServer::SPACING_GLYPH);
  311. return true;
  312. } else if (tokens[4] == "spacing_space") {
  313. r_ret = get_spacing(cache_index, sz.x, TextServer::SPACING_SPACE);
  314. return true;
  315. } else if (tokens.size() == 7 && tokens[4] == "textures") {
  316. int texture_index = tokens[5].to_int();
  317. if (tokens[6] == "image") {
  318. r_ret = get_texture_image(cache_index, sz, texture_index);
  319. return true;
  320. } else if (tokens[6] == "offsets") {
  321. r_ret = get_texture_offsets(cache_index, sz, texture_index);
  322. return true;
  323. }
  324. } else if (tokens.size() == 7 && tokens[4] == "glyphs") {
  325. int32_t glyph_index = tokens[5].to_int();
  326. if (tokens[6] == "advance") {
  327. r_ret = get_glyph_advance(cache_index, sz.x, glyph_index);
  328. return true;
  329. } else if (tokens[6] == "offset") {
  330. r_ret = get_glyph_offset(cache_index, sz, glyph_index);
  331. return true;
  332. } else if (tokens[6] == "size") {
  333. r_ret = get_glyph_size(cache_index, sz, glyph_index);
  334. return true;
  335. } else if (tokens[6] == "uv_rect") {
  336. r_ret = get_glyph_uv_rect(cache_index, sz, glyph_index);
  337. return true;
  338. } else if (tokens[6] == "texture_idx") {
  339. r_ret = get_glyph_texture_idx(cache_index, sz, glyph_index);
  340. return true;
  341. }
  342. } else if (tokens.size() == 7 && tokens[4] == "kerning_overrides") {
  343. Vector2i gp = Vector2i(tokens[5].to_int(), tokens[6].to_int());
  344. r_ret = get_kerning(cache_index, sz.x, gp);
  345. return true;
  346. }
  347. }
  348. }
  349. return false;
  350. }
  351. void FontData::_get_property_list(List<PropertyInfo> *p_list) const {
  352. p_list->push_back(PropertyInfo(Variant::PACKED_BYTE_ARRAY, "data", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_STORAGE));
  353. p_list->push_back(PropertyInfo(Variant::BOOL, "antialiased", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_STORAGE));
  354. p_list->push_back(PropertyInfo(Variant::BOOL, "multichannel_signed_distance_field", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_STORAGE));
  355. p_list->push_back(PropertyInfo(Variant::INT, "msdf_pixel_range", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_STORAGE));
  356. p_list->push_back(PropertyInfo(Variant::INT, "msdf_size", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_STORAGE));
  357. p_list->push_back(PropertyInfo(Variant::INT, "fixed_size", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_STORAGE));
  358. p_list->push_back(PropertyInfo(Variant::INT, "hinting", PROPERTY_HINT_ENUM, "None,Light,Normal", PROPERTY_USAGE_STORAGE));
  359. p_list->push_back(PropertyInfo(Variant::BOOL, "force_autohinter", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_STORAGE));
  360. p_list->push_back(PropertyInfo(Variant::FLOAT, "oversampling", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_STORAGE));
  361. Vector<String> lang_over = get_language_support_overrides();
  362. for (int i = 0; i < lang_over.size(); i++) {
  363. p_list->push_back(PropertyInfo(Variant::BOOL, "language_support_override/" + lang_over[i], PROPERTY_HINT_NONE, "", PROPERTY_USAGE_STORAGE));
  364. }
  365. Vector<String> scr_over = get_script_support_overrides();
  366. for (int i = 0; i < scr_over.size(); i++) {
  367. p_list->push_back(PropertyInfo(Variant::BOOL, "script_support_override/" + scr_over[i], PROPERTY_HINT_NONE, "", PROPERTY_USAGE_STORAGE));
  368. }
  369. for (int i = 0; i < cache.size(); i++) {
  370. String prefix = "cache/" + itos(i) + "/";
  371. Array sizes = get_size_cache_list(i);
  372. p_list->push_back(PropertyInfo(Variant::DICTIONARY, prefix + "variation_coordinates", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_STORAGE));
  373. for (int j = 0; j < sizes.size(); j++) {
  374. Vector2i sz = sizes[j];
  375. String prefix_sz = prefix + itos(sz.x) + "/" + itos(sz.y) + "/";
  376. if (sz.y == 0) {
  377. p_list->push_back(PropertyInfo(Variant::FLOAT, prefix_sz + "ascent", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_STORAGE));
  378. p_list->push_back(PropertyInfo(Variant::FLOAT, prefix_sz + "descent", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_STORAGE));
  379. p_list->push_back(PropertyInfo(Variant::FLOAT, prefix_sz + "underline_position", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_STORAGE));
  380. p_list->push_back(PropertyInfo(Variant::FLOAT, prefix_sz + "underline_thickness", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_STORAGE));
  381. p_list->push_back(PropertyInfo(Variant::FLOAT, prefix_sz + "scale", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_STORAGE));
  382. p_list->push_back(PropertyInfo(Variant::BOOL, prefix_sz + "spacing_glyph", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_STORAGE));
  383. p_list->push_back(PropertyInfo(Variant::BOOL, prefix_sz + "spacing_space", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_STORAGE));
  384. }
  385. int tx_cnt = get_texture_count(i, sz);
  386. for (int k = 0; k < tx_cnt; k++) {
  387. p_list->push_back(PropertyInfo(Variant::PACKED_INT32_ARRAY, prefix_sz + "textures/" + itos(k) + "/offsets", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_STORAGE));
  388. p_list->push_back(PropertyInfo(Variant::OBJECT, prefix_sz + "textures/" + itos(k) + "/image", PROPERTY_HINT_RESOURCE_TYPE, "Image", PROPERTY_USAGE_STORAGE | PROPERTY_USAGE_RESOURCE_NOT_PERSISTENT));
  389. }
  390. Array glyphs = get_glyph_list(i, sz);
  391. for (int k = 0; k < glyphs.size(); k++) {
  392. const int32_t &gl = glyphs[k];
  393. if (sz.y == 0) {
  394. p_list->push_back(PropertyInfo(Variant::VECTOR2, prefix_sz + "glyphs/" + itos(gl) + "/advance", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_STORAGE));
  395. }
  396. p_list->push_back(PropertyInfo(Variant::VECTOR2, prefix_sz + "glyphs/" + itos(gl) + "/offset", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_STORAGE));
  397. p_list->push_back(PropertyInfo(Variant::VECTOR2, prefix_sz + "glyphs/" + itos(gl) + "/size", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_STORAGE));
  398. p_list->push_back(PropertyInfo(Variant::RECT2, prefix_sz + "glyphs/" + itos(gl) + "/uv_rect", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_STORAGE));
  399. p_list->push_back(PropertyInfo(Variant::INT, prefix_sz + "glyphs/" + itos(gl) + "/texture_idx", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_STORAGE));
  400. }
  401. if (sz.y == 0) {
  402. Array kerning_map = get_kerning_list(i, sz.x);
  403. for (int k = 0; k < kerning_map.size(); k++) {
  404. const Vector2i &gl_pair = kerning_map[k];
  405. p_list->push_back(PropertyInfo(Variant::VECTOR2, prefix_sz + "kerning_overrides/" + itos(gl_pair.x) + "/" + itos(gl_pair.y), PROPERTY_HINT_NONE, "", PROPERTY_USAGE_STORAGE));
  406. }
  407. }
  408. }
  409. }
  410. }
  411. void FontData::reset_state() {
  412. _clear_cache();
  413. data.clear();
  414. data_ptr = nullptr;
  415. data_size = 0;
  416. cache.clear();
  417. antialiased = true;
  418. msdf = false;
  419. force_autohinter = false;
  420. hinting = TextServer::HINTING_LIGHT;
  421. msdf_pixel_range = 14;
  422. msdf_size = 128;
  423. oversampling = 0.f;
  424. }
  425. /*************************************************************************/
  426. void FontData::set_data_ptr(const uint8_t *p_data, size_t p_size) {
  427. data.clear();
  428. data_ptr = p_data;
  429. data_size = p_size;
  430. if (data_ptr != nullptr) {
  431. for (int i = 0; i < cache.size(); i++) {
  432. if (cache[i].is_valid()) {
  433. TS->font_set_data_ptr(cache[i], data_ptr, data_size);
  434. }
  435. }
  436. }
  437. }
  438. void FontData::set_data(const PackedByteArray &p_data) {
  439. data = p_data;
  440. data_ptr = data.ptr();
  441. data_size = data.size();
  442. if (data_ptr != nullptr) {
  443. for (int i = 0; i < cache.size(); i++) {
  444. if (cache[i].is_valid()) {
  445. TS->font_set_data_ptr(cache[i], data_ptr, data_size);
  446. }
  447. }
  448. }
  449. }
  450. PackedByteArray FontData::get_data() const {
  451. if (unlikely((size_t)data.size() != data_size)) {
  452. PackedByteArray *data_w = const_cast<PackedByteArray *>(&data);
  453. data_w->resize(data_size);
  454. memcpy(data_w->ptrw(), data_ptr, data_size);
  455. }
  456. return data;
  457. }
  458. void FontData::set_antialiased(bool p_antialiased) {
  459. if (antialiased != p_antialiased) {
  460. antialiased = p_antialiased;
  461. for (int i = 0; i < cache.size(); i++) {
  462. _ensure_rid(i);
  463. TS->font_set_antialiased(cache[i], antialiased);
  464. }
  465. emit_changed();
  466. }
  467. }
  468. bool FontData::is_antialiased() const {
  469. return antialiased;
  470. }
  471. void FontData::set_multichannel_signed_distance_field(bool p_msdf) {
  472. if (msdf != p_msdf) {
  473. msdf = p_msdf;
  474. for (int i = 0; i < cache.size(); i++) {
  475. _ensure_rid(i);
  476. TS->font_set_multichannel_signed_distance_field(cache[i], msdf);
  477. }
  478. emit_changed();
  479. }
  480. }
  481. bool FontData::is_multichannel_signed_distance_field() const {
  482. return msdf;
  483. }
  484. void FontData::set_msdf_pixel_range(int p_msdf_pixel_range) {
  485. if (msdf_pixel_range != p_msdf_pixel_range) {
  486. msdf_pixel_range = p_msdf_pixel_range;
  487. for (int i = 0; i < cache.size(); i++) {
  488. _ensure_rid(i);
  489. TS->font_set_msdf_pixel_range(cache[i], msdf_pixel_range);
  490. }
  491. emit_changed();
  492. }
  493. }
  494. int FontData::get_msdf_pixel_range() const {
  495. return msdf_pixel_range;
  496. }
  497. void FontData::set_msdf_size(int p_msdf_size) {
  498. if (msdf_size != p_msdf_size) {
  499. msdf_size = p_msdf_size;
  500. for (int i = 0; i < cache.size(); i++) {
  501. _ensure_rid(i);
  502. TS->font_set_msdf_size(cache[i], msdf_size);
  503. }
  504. emit_changed();
  505. }
  506. }
  507. int FontData::get_msdf_size() const {
  508. return msdf_size;
  509. }
  510. void FontData::set_fixed_size(int p_fixed_size) {
  511. if (fixed_size != p_fixed_size) {
  512. fixed_size = p_fixed_size;
  513. for (int i = 0; i < cache.size(); i++) {
  514. _ensure_rid(i);
  515. TS->font_set_fixed_size(cache[i], fixed_size);
  516. }
  517. emit_changed();
  518. }
  519. }
  520. int FontData::get_fixed_size() const {
  521. return fixed_size;
  522. }
  523. void FontData::set_force_autohinter(bool p_force_autohinter) {
  524. if (force_autohinter != p_force_autohinter) {
  525. force_autohinter = p_force_autohinter;
  526. for (int i = 0; i < cache.size(); i++) {
  527. _ensure_rid(i);
  528. TS->font_set_force_autohinter(cache[i], force_autohinter);
  529. }
  530. emit_changed();
  531. }
  532. }
  533. bool FontData::is_force_autohinter() const {
  534. return force_autohinter;
  535. }
  536. void FontData::set_hinting(TextServer::Hinting p_hinting) {
  537. if (hinting != p_hinting) {
  538. hinting = p_hinting;
  539. for (int i = 0; i < cache.size(); i++) {
  540. _ensure_rid(i);
  541. TS->font_set_hinting(cache[i], hinting);
  542. }
  543. emit_changed();
  544. }
  545. }
  546. TextServer::Hinting FontData::get_hinting() const {
  547. return hinting;
  548. }
  549. void FontData::set_oversampling(real_t p_oversampling) {
  550. if (oversampling != p_oversampling) {
  551. oversampling = p_oversampling;
  552. for (int i = 0; i < cache.size(); i++) {
  553. _ensure_rid(i);
  554. TS->font_set_oversampling(cache[i], oversampling);
  555. }
  556. emit_changed();
  557. }
  558. }
  559. real_t FontData::get_oversampling() const {
  560. return oversampling;
  561. }
  562. RID FontData::find_cache(const Dictionary &p_variation_coordinates) const {
  563. // Find existing variation cache.
  564. const Dictionary &supported_coords = get_supported_variation_list();
  565. for (int i = 0; i < cache.size(); i++) {
  566. if (cache[i].is_valid()) {
  567. const Dictionary &cache_var = TS->font_get_variation_coordinates(cache[i]);
  568. bool match = true;
  569. for (const Variant *V = supported_coords.next(nullptr); V && match; V = supported_coords.next(V)) {
  570. const Vector3 &def = supported_coords[*V];
  571. real_t c_v = def.z;
  572. if (cache_var.has(*V)) {
  573. real_t val = cache_var[*V];
  574. c_v = CLAMP(val, def.x, def.y);
  575. }
  576. if (cache_var.has(TS->tag_to_name(*V))) {
  577. real_t val = cache_var[TS->tag_to_name(*V)];
  578. c_v = CLAMP(val, def.x, def.y);
  579. }
  580. real_t s_v = def.z;
  581. if (p_variation_coordinates.has(*V)) {
  582. real_t val = p_variation_coordinates[*V];
  583. s_v = CLAMP(val, def.x, def.y);
  584. }
  585. if (p_variation_coordinates.has(TS->tag_to_name(*V))) {
  586. real_t val = p_variation_coordinates[TS->tag_to_name(*V)];
  587. s_v = CLAMP(val, def.x, def.y);
  588. }
  589. match = match && (c_v == s_v);
  590. }
  591. if (match) {
  592. return cache[i];
  593. }
  594. }
  595. }
  596. // Create new variation cache.
  597. int idx = cache.size();
  598. _ensure_rid(idx);
  599. TS->font_set_variation_coordinates(cache[idx], p_variation_coordinates);
  600. return cache[idx];
  601. }
  602. int FontData::get_cache_count() const {
  603. return cache.size();
  604. }
  605. void FontData::clear_cache() {
  606. _clear_cache();
  607. cache.clear();
  608. }
  609. void FontData::remove_cache(int p_cache_index) {
  610. ERR_FAIL_INDEX(p_cache_index, cache.size());
  611. if (cache[p_cache_index].is_valid()) {
  612. TS->free(cache.write[p_cache_index]);
  613. }
  614. cache.remove(p_cache_index);
  615. emit_changed();
  616. }
  617. Array FontData::get_size_cache_list(int p_cache_index) const {
  618. ERR_FAIL_COND_V(p_cache_index < 0, Array());
  619. _ensure_rid(p_cache_index);
  620. return TS->font_get_size_cache_list(cache[p_cache_index]);
  621. }
  622. void FontData::clear_size_cache(int p_cache_index) {
  623. ERR_FAIL_COND(p_cache_index < 0);
  624. _ensure_rid(p_cache_index);
  625. TS->font_clear_size_cache(cache[p_cache_index]);
  626. }
  627. void FontData::remove_size_cache(int p_cache_index, const Vector2i &p_size) {
  628. ERR_FAIL_COND(p_cache_index < 0);
  629. _ensure_rid(p_cache_index);
  630. TS->font_remove_size_cache(cache[p_cache_index], p_size);
  631. }
  632. void FontData::set_variation_coordinates(int p_cache_index, const Dictionary &p_variation_coordinates) {
  633. ERR_FAIL_COND(p_cache_index < 0);
  634. _ensure_rid(p_cache_index);
  635. TS->font_set_variation_coordinates(cache[p_cache_index], p_variation_coordinates);
  636. emit_changed();
  637. }
  638. Dictionary FontData::get_variation_coordinates(int p_cache_index) const {
  639. ERR_FAIL_COND_V(p_cache_index < 0, Dictionary());
  640. _ensure_rid(p_cache_index);
  641. return TS->font_get_variation_coordinates(cache[p_cache_index]);
  642. }
  643. void FontData::set_ascent(int p_cache_index, int p_size, real_t p_ascent) {
  644. ERR_FAIL_COND(p_cache_index < 0);
  645. _ensure_rid(p_cache_index);
  646. TS->font_set_ascent(cache[p_cache_index], p_size, p_ascent);
  647. }
  648. real_t FontData::get_ascent(int p_cache_index, int p_size) const {
  649. ERR_FAIL_COND_V(p_cache_index < 0, 0.f);
  650. _ensure_rid(p_cache_index);
  651. return TS->font_get_ascent(cache[p_cache_index], p_size);
  652. }
  653. void FontData::set_descent(int p_cache_index, int p_size, real_t p_descent) {
  654. ERR_FAIL_COND(p_cache_index < 0);
  655. _ensure_rid(p_cache_index);
  656. TS->font_set_descent(cache[p_cache_index], p_size, p_descent);
  657. }
  658. real_t FontData::get_descent(int p_cache_index, int p_size) const {
  659. ERR_FAIL_COND_V(p_cache_index < 0, 0.f);
  660. _ensure_rid(p_cache_index);
  661. return TS->font_get_descent(cache[p_cache_index], p_size);
  662. }
  663. void FontData::set_underline_position(int p_cache_index, int p_size, real_t p_underline_position) {
  664. ERR_FAIL_COND(p_cache_index < 0);
  665. _ensure_rid(p_cache_index);
  666. TS->font_set_underline_position(cache[p_cache_index], p_size, p_underline_position);
  667. }
  668. real_t FontData::get_underline_position(int p_cache_index, int p_size) const {
  669. ERR_FAIL_COND_V(p_cache_index < 0, 0.f);
  670. _ensure_rid(p_cache_index);
  671. return TS->font_get_underline_position(cache[p_cache_index], p_size);
  672. }
  673. void FontData::set_underline_thickness(int p_cache_index, int p_size, real_t p_underline_thickness) {
  674. ERR_FAIL_COND(p_cache_index < 0);
  675. _ensure_rid(p_cache_index);
  676. TS->font_set_underline_thickness(cache[p_cache_index], p_size, p_underline_thickness);
  677. }
  678. real_t FontData::get_underline_thickness(int p_cache_index, int p_size) const {
  679. ERR_FAIL_COND_V(p_cache_index < 0, 0.f);
  680. _ensure_rid(p_cache_index);
  681. return TS->font_get_underline_thickness(cache[p_cache_index], p_size);
  682. }
  683. void FontData::set_scale(int p_cache_index, int p_size, real_t p_scale) {
  684. ERR_FAIL_COND(p_cache_index < 0);
  685. _ensure_rid(p_cache_index);
  686. TS->font_set_scale(cache[p_cache_index], p_size, p_scale);
  687. }
  688. real_t FontData::get_scale(int p_cache_index, int p_size) const {
  689. ERR_FAIL_COND_V(p_cache_index < 0, 0.f);
  690. _ensure_rid(p_cache_index);
  691. return TS->font_get_scale(cache[p_cache_index], p_size);
  692. }
  693. void FontData::set_spacing(int p_cache_index, int p_size, TextServer::SpacingType p_spacing, int p_value) {
  694. ERR_FAIL_COND(p_cache_index < 0);
  695. _ensure_rid(p_cache_index);
  696. TS->font_set_spacing(cache[p_cache_index], p_size, p_spacing, p_value);
  697. }
  698. int FontData::get_spacing(int p_cache_index, int p_size, TextServer::SpacingType p_spacing) const {
  699. ERR_FAIL_COND_V(p_cache_index < 0, 0);
  700. _ensure_rid(p_cache_index);
  701. return TS->font_get_spacing(cache[p_cache_index], p_size, p_spacing);
  702. }
  703. int FontData::get_texture_count(int p_cache_index, const Vector2i &p_size) const {
  704. ERR_FAIL_COND_V(p_cache_index < 0, 0);
  705. _ensure_rid(p_cache_index);
  706. return TS->font_get_texture_count(cache[p_cache_index], p_size);
  707. }
  708. void FontData::clear_textures(int p_cache_index, const Vector2i &p_size) {
  709. ERR_FAIL_COND(p_cache_index < 0);
  710. _ensure_rid(p_cache_index);
  711. TS->font_clear_textures(cache[p_cache_index], p_size);
  712. }
  713. void FontData::remove_texture(int p_cache_index, const Vector2i &p_size, int p_texture_index) {
  714. ERR_FAIL_COND(p_cache_index < 0);
  715. _ensure_rid(p_cache_index);
  716. TS->font_remove_texture(cache[p_cache_index], p_size, p_texture_index);
  717. }
  718. void FontData::set_texture_image(int p_cache_index, const Vector2i &p_size, int p_texture_index, const Ref<Image> &p_image) {
  719. ERR_FAIL_COND(p_cache_index < 0);
  720. _ensure_rid(p_cache_index);
  721. TS->font_set_texture_image(cache[p_cache_index], p_size, p_texture_index, p_image);
  722. }
  723. Ref<Image> FontData::get_texture_image(int p_cache_index, const Vector2i &p_size, int p_texture_index) const {
  724. ERR_FAIL_COND_V(p_cache_index < 0, Ref<Image>());
  725. _ensure_rid(p_cache_index);
  726. return TS->font_get_texture_image(cache[p_cache_index], p_size, p_texture_index);
  727. }
  728. void FontData::set_texture_offsets(int p_cache_index, const Vector2i &p_size, int p_texture_index, const PackedInt32Array &p_offset) {
  729. ERR_FAIL_COND(p_cache_index < 0);
  730. _ensure_rid(p_cache_index);
  731. TS->font_set_texture_offsets(cache[p_cache_index], p_size, p_texture_index, p_offset);
  732. }
  733. PackedInt32Array FontData::get_texture_offsets(int p_cache_index, const Vector2i &p_size, int p_texture_index) const {
  734. ERR_FAIL_COND_V(p_cache_index < 0, PackedInt32Array());
  735. _ensure_rid(p_cache_index);
  736. return TS->font_get_texture_offsets(cache[p_cache_index], p_size, p_texture_index);
  737. }
  738. Array FontData::get_glyph_list(int p_cache_index, const Vector2i &p_size) const {
  739. ERR_FAIL_COND_V(p_cache_index < 0, Array());
  740. _ensure_rid(p_cache_index);
  741. return TS->font_get_glyph_list(cache[p_cache_index], p_size);
  742. }
  743. void FontData::clear_glyphs(int p_cache_index, const Vector2i &p_size) {
  744. ERR_FAIL_COND(p_cache_index < 0);
  745. _ensure_rid(p_cache_index);
  746. TS->font_clear_glyphs(cache[p_cache_index], p_size);
  747. }
  748. void FontData::remove_glyph(int p_cache_index, const Vector2i &p_size, int32_t p_glyph) {
  749. ERR_FAIL_COND(p_cache_index < 0);
  750. _ensure_rid(p_cache_index);
  751. TS->font_remove_glyph(cache[p_cache_index], p_size, p_glyph);
  752. }
  753. void FontData::set_glyph_advance(int p_cache_index, int p_size, int32_t p_glyph, const Vector2 &p_advance) {
  754. ERR_FAIL_COND(p_cache_index < 0);
  755. _ensure_rid(p_cache_index);
  756. TS->font_set_glyph_advance(cache[p_cache_index], p_size, p_glyph, p_advance);
  757. }
  758. Vector2 FontData::get_glyph_advance(int p_cache_index, int p_size, int32_t p_glyph) const {
  759. ERR_FAIL_COND_V(p_cache_index < 0, Vector2());
  760. _ensure_rid(p_cache_index);
  761. return TS->font_get_glyph_advance(cache[p_cache_index], p_size, p_glyph);
  762. }
  763. void FontData::set_glyph_offset(int p_cache_index, const Vector2i &p_size, int32_t p_glyph, const Vector2 &p_offset) {
  764. ERR_FAIL_COND(p_cache_index < 0);
  765. _ensure_rid(p_cache_index);
  766. TS->font_set_glyph_offset(cache[p_cache_index], p_size, p_glyph, p_offset);
  767. }
  768. Vector2 FontData::get_glyph_offset(int p_cache_index, const Vector2i &p_size, int32_t p_glyph) const {
  769. ERR_FAIL_COND_V(p_cache_index < 0, Vector2());
  770. _ensure_rid(p_cache_index);
  771. return TS->font_get_glyph_offset(cache[p_cache_index], p_size, p_glyph);
  772. }
  773. void FontData::set_glyph_size(int p_cache_index, const Vector2i &p_size, int32_t p_glyph, const Vector2 &p_gl_size) {
  774. ERR_FAIL_COND(p_cache_index < 0);
  775. _ensure_rid(p_cache_index);
  776. TS->font_set_glyph_size(cache[p_cache_index], p_size, p_glyph, p_gl_size);
  777. }
  778. Vector2 FontData::get_glyph_size(int p_cache_index, const Vector2i &p_size, int32_t p_glyph) const {
  779. ERR_FAIL_COND_V(p_cache_index < 0, Vector2());
  780. _ensure_rid(p_cache_index);
  781. return TS->font_get_glyph_size(cache[p_cache_index], p_size, p_glyph);
  782. }
  783. void FontData::set_glyph_uv_rect(int p_cache_index, const Vector2i &p_size, int32_t p_glyph, const Rect2 &p_uv_rect) {
  784. ERR_FAIL_COND(p_cache_index < 0);
  785. _ensure_rid(p_cache_index);
  786. TS->font_set_glyph_uv_rect(cache[p_cache_index], p_size, p_glyph, p_uv_rect);
  787. }
  788. Rect2 FontData::get_glyph_uv_rect(int p_cache_index, const Vector2i &p_size, int32_t p_glyph) const {
  789. ERR_FAIL_COND_V(p_cache_index < 0, Rect2());
  790. _ensure_rid(p_cache_index);
  791. return TS->font_get_glyph_uv_rect(cache[p_cache_index], p_size, p_glyph);
  792. }
  793. void FontData::set_glyph_texture_idx(int p_cache_index, const Vector2i &p_size, int32_t p_glyph, int p_texture_idx) {
  794. ERR_FAIL_COND(p_cache_index < 0);
  795. _ensure_rid(p_cache_index);
  796. TS->font_set_glyph_texture_idx(cache[p_cache_index], p_size, p_glyph, p_texture_idx);
  797. }
  798. int FontData::get_glyph_texture_idx(int p_cache_index, const Vector2i &p_size, int32_t p_glyph) const {
  799. ERR_FAIL_COND_V(p_cache_index < 0, 0);
  800. _ensure_rid(p_cache_index);
  801. return TS->font_get_glyph_texture_idx(cache[p_cache_index], p_size, p_glyph);
  802. }
  803. Array FontData::get_kerning_list(int p_cache_index, int p_size) const {
  804. ERR_FAIL_COND_V(p_cache_index < 0, Array());
  805. _ensure_rid(p_cache_index);
  806. return TS->font_get_kerning_list(cache[p_cache_index], p_size);
  807. }
  808. void FontData::clear_kerning_map(int p_cache_index, int p_size) {
  809. ERR_FAIL_COND(p_cache_index < 0);
  810. _ensure_rid(p_cache_index);
  811. TS->font_clear_kerning_map(cache[p_cache_index], p_size);
  812. }
  813. void FontData::remove_kerning(int p_cache_index, int p_size, const Vector2i &p_glyph_pair) {
  814. ERR_FAIL_COND(p_cache_index < 0);
  815. _ensure_rid(p_cache_index);
  816. TS->font_remove_kerning(cache[p_cache_index], p_size, p_glyph_pair);
  817. }
  818. void FontData::set_kerning(int p_cache_index, int p_size, const Vector2i &p_glyph_pair, const Vector2 &p_kerning) {
  819. ERR_FAIL_COND(p_cache_index < 0);
  820. _ensure_rid(p_cache_index);
  821. TS->font_set_kerning(cache[p_cache_index], p_size, p_glyph_pair, p_kerning);
  822. }
  823. Vector2 FontData::get_kerning(int p_cache_index, int p_size, const Vector2i &p_glyph_pair) const {
  824. ERR_FAIL_COND_V(p_cache_index < 0, Vector2());
  825. _ensure_rid(p_cache_index);
  826. return TS->font_get_kerning(cache[p_cache_index], p_size, p_glyph_pair);
  827. }
  828. void FontData::render_range(int p_cache_index, const Vector2i &p_size, char32_t p_start, char32_t p_end) {
  829. ERR_FAIL_COND(p_cache_index < 0);
  830. _ensure_rid(p_cache_index);
  831. TS->font_render_range(cache[p_cache_index], p_size, p_start, p_end);
  832. }
  833. void FontData::render_glyph(int p_cache_index, const Vector2i &p_size, int32_t p_index) {
  834. ERR_FAIL_COND(p_cache_index < 0);
  835. _ensure_rid(p_cache_index);
  836. TS->font_render_glyph(cache[p_cache_index], p_size, p_index);
  837. }
  838. RID FontData::get_cache_rid(int p_cache_index) const {
  839. ERR_FAIL_COND_V(p_cache_index < 0, RID());
  840. _ensure_rid(p_cache_index);
  841. return cache[p_cache_index];
  842. }
  843. bool FontData::is_language_supported(const String &p_language) const {
  844. _ensure_rid(0);
  845. return TS->font_is_language_supported(cache[0], p_language);
  846. }
  847. void FontData::set_language_support_override(const String &p_language, bool p_supported) {
  848. _ensure_rid(0);
  849. TS->font_set_language_support_override(cache[0], p_language, p_supported);
  850. }
  851. bool FontData::get_language_support_override(const String &p_language) const {
  852. _ensure_rid(0);
  853. return TS->font_get_language_support_override(cache[0], p_language);
  854. }
  855. void FontData::remove_language_support_override(const String &p_language) {
  856. _ensure_rid(0);
  857. TS->font_remove_language_support_override(cache[0], p_language);
  858. }
  859. Vector<String> FontData::get_language_support_overrides() const {
  860. _ensure_rid(0);
  861. return TS->font_get_language_support_overrides(cache[0]);
  862. }
  863. bool FontData::is_script_supported(const String &p_script) const {
  864. _ensure_rid(0);
  865. return TS->font_is_script_supported(cache[0], p_script);
  866. }
  867. void FontData::set_script_support_override(const String &p_script, bool p_supported) {
  868. _ensure_rid(0);
  869. TS->font_set_script_support_override(cache[0], p_script, p_supported);
  870. }
  871. bool FontData::get_script_support_override(const String &p_script) const {
  872. _ensure_rid(0);
  873. return TS->font_get_script_support_override(cache[0], p_script);
  874. }
  875. void FontData::remove_script_support_override(const String &p_script) {
  876. _ensure_rid(0);
  877. TS->font_remove_script_support_override(cache[0], p_script);
  878. }
  879. Vector<String> FontData::get_script_support_overrides() const {
  880. _ensure_rid(0);
  881. return TS->font_get_script_support_overrides(cache[0]);
  882. }
  883. bool FontData::has_char(char32_t p_char) const {
  884. _ensure_rid(0);
  885. return TS->font_has_char(cache[0], p_char);
  886. }
  887. String FontData::get_supported_chars() const {
  888. _ensure_rid(0);
  889. return TS->font_get_supported_chars(cache[0]);
  890. }
  891. int32_t FontData::get_glyph_index(int p_size, char32_t p_char, char32_t p_variation_selector) const {
  892. _ensure_rid(0);
  893. return TS->font_get_glyph_index(cache[0], p_size, p_char, p_variation_selector);
  894. }
  895. Dictionary FontData::get_supported_feature_list() const {
  896. _ensure_rid(0);
  897. return TS->font_supported_feature_list(cache[0]);
  898. }
  899. Dictionary FontData::get_supported_variation_list() const {
  900. _ensure_rid(0);
  901. return TS->font_supported_variation_list(cache[0]);
  902. }
  903. FontData::FontData() {
  904. /* NOP */
  905. }
  906. FontData::~FontData() {
  907. _clear_cache();
  908. }
  909. /*************************************************************************/
  910. void Font::_data_changed() {
  911. for (int i = 0; i < rids.size(); i++) {
  912. rids.write[i] = RID();
  913. }
  914. emit_changed();
  915. }
  916. void Font::_ensure_rid(int p_index) const {
  917. // Find or create cache record.
  918. if (!rids[p_index].is_valid() && data[p_index].is_valid()) {
  919. rids.write[p_index] = data[p_index]->find_cache(variation_coordinates);
  920. }
  921. }
  922. void Font::_bind_methods() {
  923. ClassDB::bind_method(D_METHOD("add_data", "data"), &Font::add_data);
  924. ClassDB::bind_method(D_METHOD("set_data", "idx", "data"), &Font::set_data);
  925. ClassDB::bind_method(D_METHOD("get_data_count"), &Font::get_data_count);
  926. ClassDB::bind_method(D_METHOD("get_data", "idx"), &Font::get_data);
  927. ClassDB::bind_method(D_METHOD("get_data_rid", "idx"), &Font::get_data_rid);
  928. ClassDB::bind_method(D_METHOD("clear_data"), &Font::clear_data);
  929. ClassDB::bind_method(D_METHOD("remove_data", "idx"), &Font::remove_data);
  930. ClassDB::bind_method(D_METHOD("set_base_size", "size"), &Font::set_base_size);
  931. ClassDB::bind_method(D_METHOD("get_base_size"), &Font::get_base_size);
  932. ADD_PROPERTY(PropertyInfo(Variant::INT, "base_size"), "set_base_size", "get_base_size");
  933. ClassDB::bind_method(D_METHOD("set_variation_coordinates", "variation_coordinates"), &Font::set_variation_coordinates);
  934. ClassDB::bind_method(D_METHOD("get_variation_coordinates"), &Font::get_variation_coordinates);
  935. ADD_PROPERTY(PropertyInfo(Variant::DICTIONARY, "variation_coordinates"), "set_variation_coordinates", "get_variation_coordinates");
  936. ClassDB::bind_method(D_METHOD("set_spacing", "spacing", "value"), &Font::set_spacing);
  937. ClassDB::bind_method(D_METHOD("get_spacing", "spacing"), &Font::get_spacing);
  938. ADD_GROUP("Extra Spacing", "spacing");
  939. ADD_PROPERTYI(PropertyInfo(Variant::INT, "spacing_top"), "set_spacing", "get_spacing", TextServer::SPACING_TOP);
  940. ADD_PROPERTYI(PropertyInfo(Variant::INT, "spacing_bottom"), "set_spacing", "get_spacing", TextServer::SPACING_BOTTOM);
  941. ClassDB::bind_method(D_METHOD("get_height", "size"), &Font::get_height, DEFVAL(-1));
  942. ClassDB::bind_method(D_METHOD("get_ascent", "size"), &Font::get_ascent, DEFVAL(-1));
  943. ClassDB::bind_method(D_METHOD("get_descent", "size"), &Font::get_descent, DEFVAL(-1));
  944. ClassDB::bind_method(D_METHOD("get_underline_position", "size"), &Font::get_underline_position, DEFVAL(-1));
  945. ClassDB::bind_method(D_METHOD("get_underline_thickness", "size"), &Font::get_underline_thickness, DEFVAL(-1));
  946. ClassDB::bind_method(D_METHOD("get_string_size", "text", "size", "align", "width", "flags"), &Font::get_string_size, DEFVAL(-1), DEFVAL(HALIGN_LEFT), DEFVAL(-1), DEFVAL(TextServer::JUSTIFICATION_KASHIDA | TextServer::JUSTIFICATION_WORD_BOUND));
  947. ClassDB::bind_method(D_METHOD("get_multiline_string_size", "text", "width", "size", "flags"), &Font::get_multiline_string_size, DEFVAL(-1), DEFVAL(-1), DEFVAL(TextServer::BREAK_MANDATORY | TextServer::BREAK_WORD_BOUND));
  948. ClassDB::bind_method(D_METHOD("draw_string", "canvas_item", "pos", "text", "align", "width", "size", "modulate", "outline_size", "outline_modulate", "flags"), &Font::draw_string, DEFVAL(HALIGN_LEFT), DEFVAL(-1), DEFVAL(-1), DEFVAL(Color(1, 1, 1)), DEFVAL(0), DEFVAL(Color(1, 1, 1, 0)), DEFVAL(TextServer::JUSTIFICATION_KASHIDA | TextServer::JUSTIFICATION_WORD_BOUND));
  949. ClassDB::bind_method(D_METHOD("draw_multiline_string", "canvas_item", "pos", "text", "align", "width", "max_lines", "size", "modulate", "outline_size", "outline_modulate", "flags"), &Font::draw_multiline_string, DEFVAL(HALIGN_LEFT), DEFVAL(-1), DEFVAL(-1), DEFVAL(-1), DEFVAL(Color(1, 1, 1)), DEFVAL(0), DEFVAL(Color(1, 1, 1, 0)), DEFVAL(TextServer::BREAK_MANDATORY | TextServer::BREAK_WORD_BOUND | TextServer::JUSTIFICATION_KASHIDA | TextServer::JUSTIFICATION_WORD_BOUND));
  950. ClassDB::bind_method(D_METHOD("get_char_size", "char", "next", "size"), &Font::get_char_size, DEFVAL(0), DEFVAL(-1));
  951. ClassDB::bind_method(D_METHOD("draw_char", "canvas_item", "pos", "char", "next", "size", "modulate", "outline_size", "outline_modulate"), &Font::draw_char, DEFVAL(0), DEFVAL(-1), DEFVAL(Color(1, 1, 1)), DEFVAL(0), DEFVAL(Color(1, 1, 1, 0)));
  952. ClassDB::bind_method(D_METHOD("has_char", "char"), &Font::has_char);
  953. ClassDB::bind_method(D_METHOD("get_supported_chars"), &Font::get_supported_chars);
  954. ClassDB::bind_method(D_METHOD("update_changes"), &Font::update_changes);
  955. }
  956. bool Font::_set(const StringName &p_name, const Variant &p_value) {
  957. Vector<String> tokens = p_name.operator String().split("/");
  958. #ifndef DISABLE_DEPRECATED
  959. if (tokens.size() == 1 && tokens[0] == "font_data") {
  960. // Compatibility, DynamicFont main data.
  961. Ref<FontData> fd = p_value;
  962. if (fd.is_valid()) {
  963. add_data(fd);
  964. return true;
  965. }
  966. return false;
  967. } else if (tokens.size() == 2 && tokens[0] == "fallback") {
  968. // Compatibility, DynamicFont fallback data.
  969. Ref<FontData> fd = p_value;
  970. if (fd.is_valid()) {
  971. add_data(fd);
  972. return true;
  973. }
  974. return false;
  975. } else if (tokens.size() == 1 && tokens[0] == "fallback") {
  976. // Compatibility, BitmapFont fallback data.
  977. Ref<Font> f = p_value;
  978. if (f.is_valid()) {
  979. for (int i = 0; i < f->get_data_count(); i++) {
  980. add_data(f->get_data(i));
  981. }
  982. return true;
  983. }
  984. return false;
  985. }
  986. #endif /* DISABLE_DEPRECATED */
  987. if (tokens.size() == 2 && tokens[0] == "data") {
  988. int idx = tokens[1].to_int();
  989. Ref<FontData> fd = p_value;
  990. if (fd.is_valid()) {
  991. if (idx == data.size()) {
  992. add_data(fd);
  993. return true;
  994. } else if (idx >= 0 && idx < data.size()) {
  995. set_data(idx, fd);
  996. return true;
  997. } else {
  998. return false;
  999. }
  1000. } else if (idx >= 0 && idx < data.size()) {
  1001. remove_data(idx);
  1002. return true;
  1003. }
  1004. }
  1005. return false;
  1006. }
  1007. bool Font::_get(const StringName &p_name, Variant &r_ret) const {
  1008. Vector<String> tokens = p_name.operator String().split("/");
  1009. if (tokens.size() == 2 && tokens[0] == "data") {
  1010. int idx = tokens[1].to_int();
  1011. if (idx == data.size()) {
  1012. r_ret = Ref<FontData>();
  1013. return true;
  1014. } else if (idx >= 0 && idx < data.size()) {
  1015. r_ret = get_data(idx);
  1016. return true;
  1017. }
  1018. }
  1019. return false;
  1020. }
  1021. void Font::_get_property_list(List<PropertyInfo> *p_list) const {
  1022. for (int i = 0; i < data.size(); i++) {
  1023. p_list->push_back(PropertyInfo(Variant::OBJECT, "data/" + itos(i), PROPERTY_HINT_RESOURCE_TYPE, "FontData"));
  1024. }
  1025. p_list->push_back(PropertyInfo(Variant::OBJECT, "data/" + itos(data.size()), PROPERTY_HINT_RESOURCE_TYPE, "FontData"));
  1026. }
  1027. void Font::reset_state() {
  1028. for (int i = 0; i < data.size(); i++) {
  1029. if (data[i].is_valid()) {
  1030. data.write[i]->connect(SNAME("changed"), callable_mp(this, &Font::_data_changed), varray(), CONNECT_REFERENCE_COUNTED);
  1031. }
  1032. }
  1033. cache.clear();
  1034. cache_wrap.clear();
  1035. data.clear();
  1036. rids.clear();
  1037. base_size = 16;
  1038. variation_coordinates.clear();
  1039. spacing_bottom = 0;
  1040. spacing_top = 0;
  1041. }
  1042. Dictionary Font::get_feature_list() const {
  1043. Dictionary out;
  1044. for (int i = 0; i < data.size(); i++) {
  1045. Dictionary data_ftrs = data[i]->get_supported_feature_list();
  1046. for (const Variant *ftr = data_ftrs.next(nullptr); ftr != nullptr; ftr = data_ftrs.next(ftr)) {
  1047. out[*ftr] = data_ftrs[*ftr];
  1048. }
  1049. }
  1050. return out;
  1051. }
  1052. void Font::add_data(const Ref<FontData> &p_data) {
  1053. ERR_FAIL_COND(p_data.is_null());
  1054. data.push_back(p_data);
  1055. rids.push_back(RID());
  1056. if (data[data.size() - 1].is_valid()) {
  1057. data.write[data.size() - 1]->connect(SNAME("changed"), callable_mp(this, &Font::_data_changed), varray(), CONNECT_REFERENCE_COUNTED);
  1058. Dictionary data_var_list = p_data->get_supported_variation_list();
  1059. for (int j = 0; j < data_var_list.size(); j++) {
  1060. int32_t tag = data_var_list.get_key_at_index(j);
  1061. Vector3i value = data_var_list.get_value_at_index(j);
  1062. if (!variation_coordinates.has(tag) && !variation_coordinates.has(TS->tag_to_name(tag))) {
  1063. variation_coordinates[TS->tag_to_name(tag)] = value.z;
  1064. }
  1065. }
  1066. }
  1067. cache.clear();
  1068. cache_wrap.clear();
  1069. emit_changed();
  1070. notify_property_list_changed();
  1071. }
  1072. void Font::set_data(int p_idx, const Ref<FontData> &p_data) {
  1073. ERR_FAIL_COND(p_data.is_null());
  1074. ERR_FAIL_INDEX(p_idx, data.size());
  1075. if (data[p_idx].is_valid()) {
  1076. data.write[p_idx]->disconnect(SNAME("changed"), callable_mp(this, &Font::_data_changed));
  1077. }
  1078. data.write[p_idx] = p_data;
  1079. rids.write[p_idx] = RID();
  1080. Dictionary data_var_list = p_data->get_supported_variation_list();
  1081. for (int j = 0; j < data_var_list.size(); j++) {
  1082. int32_t tag = data_var_list.get_key_at_index(j);
  1083. Vector3i value = data_var_list.get_value_at_index(j);
  1084. if (!variation_coordinates.has(tag) && !variation_coordinates.has(TS->tag_to_name(tag))) {
  1085. variation_coordinates[TS->tag_to_name(tag)] = value.z;
  1086. }
  1087. }
  1088. if (data[p_idx].is_valid()) {
  1089. data.write[p_idx]->connect(SNAME("changed"), callable_mp(this, &Font::_data_changed), varray(), CONNECT_REFERENCE_COUNTED);
  1090. }
  1091. cache.clear();
  1092. cache_wrap.clear();
  1093. emit_changed();
  1094. notify_property_list_changed();
  1095. }
  1096. int Font::get_data_count() const {
  1097. return data.size();
  1098. }
  1099. Ref<FontData> Font::get_data(int p_idx) const {
  1100. ERR_FAIL_INDEX_V(p_idx, data.size(), Ref<FontData>());
  1101. return data[p_idx];
  1102. }
  1103. RID Font::get_data_rid(int p_idx) const {
  1104. ERR_FAIL_INDEX_V(p_idx, data.size(), RID());
  1105. _ensure_rid(p_idx);
  1106. return rids[p_idx];
  1107. }
  1108. void Font::clear_data() {
  1109. for (int i = 0; i < data.size(); i++) {
  1110. if (data[i].is_valid()) {
  1111. data.write[i]->connect(SNAME("changed"), callable_mp(this, &Font::_data_changed), varray(), CONNECT_REFERENCE_COUNTED);
  1112. }
  1113. }
  1114. data.clear();
  1115. rids.clear();
  1116. }
  1117. void Font::remove_data(int p_idx) {
  1118. ERR_FAIL_INDEX(p_idx, data.size());
  1119. if (data[p_idx].is_valid()) {
  1120. data.write[p_idx]->disconnect(SNAME("changed"), callable_mp(this, &Font::_data_changed));
  1121. }
  1122. data.remove(p_idx);
  1123. rids.remove(p_idx);
  1124. cache.clear();
  1125. cache_wrap.clear();
  1126. emit_changed();
  1127. notify_property_list_changed();
  1128. }
  1129. void Font::set_base_size(int p_size) {
  1130. base_size = p_size;
  1131. }
  1132. int Font::get_base_size() const {
  1133. return base_size;
  1134. }
  1135. void Font::set_variation_coordinates(const Dictionary &p_variation_coordinates) {
  1136. _data_changed();
  1137. variation_coordinates = p_variation_coordinates;
  1138. }
  1139. Dictionary Font::get_variation_coordinates() const {
  1140. return variation_coordinates;
  1141. }
  1142. void Font::set_spacing(TextServer::SpacingType p_spacing, int p_value) {
  1143. _data_changed();
  1144. switch (p_spacing) {
  1145. case TextServer::SPACING_TOP: {
  1146. spacing_top = p_value;
  1147. } break;
  1148. case TextServer::SPACING_BOTTOM: {
  1149. spacing_bottom = p_value;
  1150. } break;
  1151. default: {
  1152. ERR_FAIL_MSG("Invalid spacing type: " + itos(p_spacing));
  1153. } break;
  1154. }
  1155. }
  1156. int Font::get_spacing(TextServer::SpacingType p_spacing) const {
  1157. switch (p_spacing) {
  1158. case TextServer::SPACING_TOP: {
  1159. return spacing_top;
  1160. } break;
  1161. case TextServer::SPACING_BOTTOM: {
  1162. return spacing_bottom;
  1163. } break;
  1164. default: {
  1165. ERR_FAIL_V_MSG(0, "Invalid spacing type: " + itos(p_spacing));
  1166. } break;
  1167. }
  1168. }
  1169. real_t Font::get_height(int p_size) const {
  1170. int size = (p_size <= 0) ? base_size : p_size;
  1171. real_t ret = 0.f;
  1172. for (int i = 0; i < data.size(); i++) {
  1173. _ensure_rid(i);
  1174. ret = MAX(ret, TS->font_get_ascent(rids[i], size) + TS->font_get_descent(rids[i], size));
  1175. }
  1176. return ret + spacing_bottom + spacing_top;
  1177. }
  1178. real_t Font::get_ascent(int p_size) const {
  1179. int size = (p_size <= 0) ? base_size : p_size;
  1180. real_t ret = 0.f;
  1181. for (int i = 0; i < data.size(); i++) {
  1182. _ensure_rid(i);
  1183. ret = MAX(ret, TS->font_get_ascent(rids[i], size));
  1184. }
  1185. return ret + spacing_top;
  1186. }
  1187. real_t Font::get_descent(int p_size) const {
  1188. int size = (p_size <= 0) ? base_size : p_size;
  1189. real_t ret = 0.f;
  1190. for (int i = 0; i < data.size(); i++) {
  1191. _ensure_rid(i);
  1192. ret = MAX(ret, TS->font_get_descent(rids[i], size));
  1193. }
  1194. return ret + spacing_bottom;
  1195. }
  1196. real_t Font::get_underline_position(int p_size) const {
  1197. int size = (p_size <= 0) ? base_size : p_size;
  1198. real_t ret = 0.f;
  1199. for (int i = 0; i < data.size(); i++) {
  1200. _ensure_rid(i);
  1201. ret = MAX(ret, TS->font_get_underline_position(rids[i], size));
  1202. }
  1203. return ret + spacing_top;
  1204. }
  1205. real_t Font::get_underline_thickness(int p_size) const {
  1206. int size = (p_size <= 0) ? base_size : p_size;
  1207. real_t ret = 0.f;
  1208. for (int i = 0; i < data.size(); i++) {
  1209. _ensure_rid(i);
  1210. ret = MAX(ret, TS->font_get_underline_thickness(rids[i], size));
  1211. }
  1212. return ret;
  1213. }
  1214. Size2 Font::get_string_size(const String &p_text, int p_size, HAlign p_align, real_t p_width, uint16_t p_flags) const {
  1215. ERR_FAIL_COND_V(data.is_empty(), Size2());
  1216. int size = (p_size <= 0) ? base_size : p_size;
  1217. for (int i = 0; i < data.size(); i++) {
  1218. _ensure_rid(i);
  1219. }
  1220. uint64_t hash = p_text.hash64();
  1221. if (p_align == HALIGN_FILL) {
  1222. hash = hash_djb2_one_64(hash_djb2_one_float(p_width), hash);
  1223. hash = hash_djb2_one_64(p_flags, hash);
  1224. }
  1225. hash = hash_djb2_one_64(size, hash);
  1226. Ref<TextLine> buffer;
  1227. if (cache.has(hash)) {
  1228. buffer = cache.get(hash);
  1229. } else {
  1230. buffer.instantiate();
  1231. buffer->add_string(p_text, Ref<Font>(this), size, Dictionary(), TranslationServer::get_singleton()->get_tool_locale());
  1232. cache.insert(hash, buffer);
  1233. }
  1234. return buffer->get_size();
  1235. }
  1236. Size2 Font::get_multiline_string_size(const String &p_text, real_t p_width, int p_size, uint16_t p_flags) const {
  1237. ERR_FAIL_COND_V(data.is_empty(), Size2());
  1238. int size = (p_size <= 0) ? base_size : p_size;
  1239. for (int i = 0; i < data.size(); i++) {
  1240. _ensure_rid(i);
  1241. }
  1242. uint64_t hash = p_text.hash64();
  1243. uint64_t wrp_hash = hash_djb2_one_64(hash_djb2_one_float(p_width), hash);
  1244. wrp_hash = hash_djb2_one_64(p_flags, wrp_hash);
  1245. wrp_hash = hash_djb2_one_64(size, wrp_hash);
  1246. Ref<TextParagraph> lines_buffer;
  1247. if (cache_wrap.has(wrp_hash)) {
  1248. lines_buffer = cache_wrap.get(wrp_hash);
  1249. } else {
  1250. lines_buffer.instantiate();
  1251. lines_buffer->add_string(p_text, Ref<Font>(this), size, Dictionary(), TranslationServer::get_singleton()->get_tool_locale());
  1252. lines_buffer->set_width(p_width);
  1253. lines_buffer->set_flags(p_flags);
  1254. cache_wrap.insert(wrp_hash, lines_buffer);
  1255. }
  1256. Size2 ret;
  1257. for (int i = 0; i < lines_buffer->get_line_count(); i++) {
  1258. Size2 line_size = lines_buffer->get_line_size(i);
  1259. if (lines_buffer->get_orientation() == TextServer::ORIENTATION_HORIZONTAL) {
  1260. ret.x = MAX(ret.x, line_size.x);
  1261. ret.y += line_size.y;
  1262. } else {
  1263. ret.y = MAX(ret.y, line_size.y);
  1264. ret.x += line_size.x;
  1265. }
  1266. }
  1267. return ret;
  1268. }
  1269. void Font::draw_string(RID p_canvas_item, const Point2 &p_pos, const String &p_text, HAlign p_align, real_t p_width, int p_size, const Color &p_modulate, int p_outline_size, const Color &p_outline_modulate, uint16_t p_flags) const {
  1270. ERR_FAIL_COND(data.is_empty());
  1271. int size = (p_size <= 0) ? base_size : p_size;
  1272. for (int i = 0; i < data.size(); i++) {
  1273. _ensure_rid(i);
  1274. }
  1275. uint64_t hash = p_text.hash64();
  1276. if (p_align == HALIGN_FILL) {
  1277. hash = hash_djb2_one_64(hash_djb2_one_float(p_width), hash);
  1278. hash = hash_djb2_one_64(p_flags, hash);
  1279. }
  1280. hash = hash_djb2_one_64(size, hash);
  1281. Ref<TextLine> buffer;
  1282. if (cache.has(hash)) {
  1283. buffer = cache.get(hash);
  1284. } else {
  1285. buffer.instantiate();
  1286. buffer->add_string(p_text, Ref<Font>(this), size, Dictionary(), TranslationServer::get_singleton()->get_tool_locale());
  1287. cache.insert(hash, buffer);
  1288. }
  1289. Vector2 ofs = p_pos;
  1290. if (buffer->get_orientation() == TextServer::ORIENTATION_HORIZONTAL) {
  1291. ofs.y -= buffer->get_line_ascent();
  1292. } else {
  1293. ofs.x -= buffer->get_line_ascent();
  1294. }
  1295. buffer->set_width(p_width);
  1296. buffer->set_align(p_align);
  1297. buffer->set_flags(p_flags);
  1298. if (p_outline_size > 0 && p_outline_modulate.a != 0.0f) {
  1299. buffer->draw_outline(p_canvas_item, ofs, p_outline_size, p_outline_modulate);
  1300. }
  1301. buffer->draw(p_canvas_item, ofs, p_modulate);
  1302. }
  1303. void Font::draw_multiline_string(RID p_canvas_item, const Point2 &p_pos, const String &p_text, HAlign p_align, float p_width, int p_max_lines, int p_size, const Color &p_modulate, int p_outline_size, const Color &p_outline_modulate, uint16_t p_flags) const {
  1304. ERR_FAIL_COND(data.is_empty());
  1305. int size = (p_size <= 0) ? base_size : p_size;
  1306. for (int i = 0; i < data.size(); i++) {
  1307. _ensure_rid(i);
  1308. }
  1309. uint64_t hash = p_text.hash64();
  1310. uint64_t wrp_hash = hash_djb2_one_64(hash_djb2_one_float(p_width), hash);
  1311. wrp_hash = hash_djb2_one_64(p_flags, wrp_hash);
  1312. wrp_hash = hash_djb2_one_64(size, wrp_hash);
  1313. Ref<TextParagraph> lines_buffer;
  1314. if (cache_wrap.has(wrp_hash)) {
  1315. lines_buffer = cache_wrap.get(wrp_hash);
  1316. } else {
  1317. lines_buffer.instantiate();
  1318. lines_buffer->add_string(p_text, Ref<Font>(this), size, Dictionary(), TranslationServer::get_singleton()->get_tool_locale());
  1319. lines_buffer->set_width(p_width);
  1320. lines_buffer->set_flags(p_flags);
  1321. cache_wrap.insert(wrp_hash, lines_buffer);
  1322. }
  1323. lines_buffer->set_align(p_align);
  1324. Vector2 lofs = p_pos;
  1325. for (int i = 0; i < lines_buffer->get_line_count(); i++) {
  1326. if (lines_buffer->get_orientation() == TextServer::ORIENTATION_HORIZONTAL) {
  1327. if (i == 0) {
  1328. lofs.y -= lines_buffer->get_line_ascent(0);
  1329. }
  1330. } else {
  1331. if (i == 0) {
  1332. lofs.x -= lines_buffer->get_line_ascent(0);
  1333. }
  1334. }
  1335. if (p_width > 0) {
  1336. lines_buffer->set_align(p_align);
  1337. }
  1338. if (p_outline_size > 0 && p_outline_modulate.a != 0.0f) {
  1339. lines_buffer->draw_line_outline(p_canvas_item, lofs, i, p_outline_size, p_outline_modulate);
  1340. }
  1341. lines_buffer->draw_line(p_canvas_item, lofs, i, p_modulate);
  1342. Size2 line_size = lines_buffer->get_line_size(i);
  1343. if (lines_buffer->get_orientation() == TextServer::ORIENTATION_HORIZONTAL) {
  1344. lofs.y += line_size.y;
  1345. } else {
  1346. lofs.x += line_size.x;
  1347. }
  1348. if ((p_max_lines > 0) && (i >= p_max_lines)) {
  1349. return;
  1350. }
  1351. }
  1352. }
  1353. Size2 Font::get_char_size(char32_t p_char, char32_t p_next, int p_size) const {
  1354. int size = (p_size <= 0) ? base_size : p_size;
  1355. for (int i = 0; i < data.size(); i++) {
  1356. _ensure_rid(i);
  1357. if (data[i]->has_char(p_char)) {
  1358. int32_t glyph_a = TS->font_get_glyph_index(rids[i], size, p_char, 0);
  1359. Size2 ret = Size2(TS->font_get_glyph_advance(rids[i], size, glyph_a).x, TS->font_get_ascent(rids[i], size) + TS->font_get_descent(rids[i], size));
  1360. if ((p_next != 0) && data[i]->has_char(p_next)) {
  1361. int32_t glyph_b = TS->font_get_glyph_index(rids[i], size, p_next, 0);
  1362. ret.x -= TS->font_get_kerning(rids[i], size, Vector2i(glyph_a, glyph_b)).x;
  1363. }
  1364. return ret;
  1365. }
  1366. }
  1367. return Size2();
  1368. }
  1369. real_t Font::draw_char(RID p_canvas_item, const Point2 &p_pos, char32_t p_char, char32_t p_next, int p_size, const Color &p_modulate, int p_outline_size, const Color &p_outline_modulate) const {
  1370. int size = (p_size <= 0) ? base_size : p_size;
  1371. for (int i = 0; i < data.size(); i++) {
  1372. _ensure_rid(i);
  1373. if (data[i]->has_char(p_char)) {
  1374. int32_t glyph_a = TS->font_get_glyph_index(rids[i], size, p_char, 0);
  1375. real_t ret = TS->font_get_glyph_advance(rids[i], size, glyph_a).x;
  1376. if ((p_next != 0) && data[i]->has_char(p_next)) {
  1377. int32_t glyph_b = TS->font_get_glyph_index(rids[i], size, p_next, 0);
  1378. ret -= TS->font_get_kerning(rids[i], size, Vector2i(glyph_a, glyph_b)).x;
  1379. }
  1380. if (p_outline_size > 0 && p_outline_modulate.a != 0.0f) {
  1381. TS->font_draw_glyph_outline(rids[i], p_canvas_item, size, p_outline_size, p_pos, glyph_a, p_outline_modulate);
  1382. }
  1383. TS->font_draw_glyph(rids[i], p_canvas_item, size, p_pos, glyph_a, p_modulate);
  1384. return ret;
  1385. }
  1386. }
  1387. return 0;
  1388. }
  1389. bool Font::has_char(char32_t p_char) const {
  1390. for (int i = 0; i < data.size(); i++) {
  1391. if (data[i]->has_char(p_char))
  1392. return true;
  1393. }
  1394. return false;
  1395. }
  1396. String Font::get_supported_chars() const {
  1397. String chars;
  1398. for (int i = 0; i < data.size(); i++) {
  1399. String data_chars = data[i]->get_supported_chars();
  1400. for (int j = 0; j < data_chars.length(); j++) {
  1401. if (chars.find_char(data_chars[j]) == -1) {
  1402. chars += data_chars[j];
  1403. }
  1404. }
  1405. }
  1406. return chars;
  1407. }
  1408. Vector<RID> Font::get_rids() const {
  1409. for (int i = 0; i < data.size(); i++) {
  1410. _ensure_rid(i);
  1411. }
  1412. return rids;
  1413. }
  1414. void Font::update_changes() {
  1415. emit_changed();
  1416. }
  1417. Font::Font() {
  1418. cache.set_capacity(128);
  1419. cache_wrap.set_capacity(32);
  1420. }
  1421. Font::~Font() {
  1422. clear_data();
  1423. cache.clear();
  1424. cache_wrap.clear();
  1425. }