font.cpp 35 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063
  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. void FontData::_bind_methods() {
  37. ClassDB::bind_method(D_METHOD("load_resource", "filename", "base_size"), &FontData::load_resource, DEFVAL(16));
  38. ClassDB::bind_method(D_METHOD("load_memory", "data", "type", "base_size"), &FontData::_load_memory, DEFVAL(16));
  39. ClassDB::bind_method(D_METHOD("set_data_path", "path"), &FontData::set_data_path);
  40. ClassDB::bind_method(D_METHOD("get_data_path"), &FontData::get_data_path);
  41. ClassDB::bind_method(D_METHOD("get_height", "size"), &FontData::get_height);
  42. ClassDB::bind_method(D_METHOD("get_ascent", "size"), &FontData::get_ascent);
  43. ClassDB::bind_method(D_METHOD("get_descent", "size"), &FontData::get_descent);
  44. ClassDB::bind_method(D_METHOD("get_underline_position", "size"), &FontData::get_underline_position);
  45. ClassDB::bind_method(D_METHOD("get_underline_thickness", "size"), &FontData::get_underline_thickness);
  46. ClassDB::bind_method(D_METHOD("set_antialiased", "antialiased"), &FontData::set_antialiased);
  47. ClassDB::bind_method(D_METHOD("get_antialiased"), &FontData::get_antialiased);
  48. ClassDB::bind_method(D_METHOD("get_variation_list"), &FontData::get_variation_list);
  49. ClassDB::bind_method(D_METHOD("set_variation", "tag", "value"), &FontData::set_variation);
  50. ClassDB::bind_method(D_METHOD("get_variation", "tag"), &FontData::get_variation);
  51. ClassDB::bind_method(D_METHOD("set_hinting", "hinting"), &FontData::set_hinting);
  52. ClassDB::bind_method(D_METHOD("get_hinting"), &FontData::get_hinting);
  53. ClassDB::bind_method(D_METHOD("set_force_autohinter", "enabled"), &FontData::set_force_autohinter);
  54. ClassDB::bind_method(D_METHOD("get_force_autohinter"), &FontData::get_force_autohinter);
  55. ClassDB::bind_method(D_METHOD("set_distance_field_hint", "distance_field"), &FontData::set_distance_field_hint);
  56. ClassDB::bind_method(D_METHOD("get_distance_field_hint"), &FontData::get_distance_field_hint);
  57. ClassDB::bind_method(D_METHOD("has_char", "char"), &FontData::has_char);
  58. ClassDB::bind_method(D_METHOD("get_supported_chars"), &FontData::get_supported_chars);
  59. ClassDB::bind_method(D_METHOD("get_glyph_advance", "index", "size"), &FontData::get_glyph_advance);
  60. ClassDB::bind_method(D_METHOD("get_glyph_kerning", "index_a", "index_b", "size"), &FontData::get_glyph_kerning);
  61. ClassDB::bind_method(D_METHOD("get_base_size"), &FontData::get_base_size);
  62. ClassDB::bind_method(D_METHOD("has_outline"), &FontData::has_outline);
  63. ClassDB::bind_method(D_METHOD("is_language_supported", "language"), &FontData::is_language_supported);
  64. ClassDB::bind_method(D_METHOD("set_language_support_override", "language", "supported"), &FontData::set_language_support_override);
  65. ClassDB::bind_method(D_METHOD("get_language_support_override", "language"), &FontData::get_language_support_override);
  66. ClassDB::bind_method(D_METHOD("remove_language_support_override", "language"), &FontData::remove_language_support_override);
  67. ClassDB::bind_method(D_METHOD("get_language_support_overrides"), &FontData::get_language_support_overrides);
  68. ClassDB::bind_method(D_METHOD("is_script_supported", "script"), &FontData::is_script_supported);
  69. ClassDB::bind_method(D_METHOD("set_script_support_override", "script", "supported"), &FontData::set_script_support_override);
  70. ClassDB::bind_method(D_METHOD("get_script_support_override", "script"), &FontData::get_script_support_override);
  71. ClassDB::bind_method(D_METHOD("remove_script_support_override", "script"), &FontData::remove_script_support_override);
  72. ClassDB::bind_method(D_METHOD("get_script_support_overrides"), &FontData::get_script_support_overrides);
  73. ClassDB::bind_method(D_METHOD("get_glyph_index", "char", "variation_selector"), &FontData::get_glyph_index, DEFVAL(0x0000));
  74. ClassDB::bind_method(D_METHOD("draw_glyph", "canvas", "size", "pos", "index", "color"), &FontData::draw_glyph, DEFVAL(Color(1, 1, 1)));
  75. ClassDB::bind_method(D_METHOD("draw_glyph_outline", "canvas", "size", "outline_size", "pos", "index", "color"), &FontData::draw_glyph_outline, DEFVAL(Color(1, 1, 1)));
  76. ADD_PROPERTY(PropertyInfo(Variant::STRING, "data_path", PROPERTY_HINT_FILE, "*.ttf,*.otf,*.woff,*.fnt,*.font"), "set_data_path", "get_data_path");
  77. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "antialiased"), "set_antialiased", "get_antialiased");
  78. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "force_autohinter"), "set_force_autohinter", "get_force_autohinter");
  79. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "distance_field_hint"), "set_distance_field_hint", "get_distance_field_hint");
  80. ADD_PROPERTY(PropertyInfo(Variant::INT, "hinting", PROPERTY_HINT_ENUM, "None,Light,Normal"), "set_hinting", "get_hinting");
  81. }
  82. bool FontData::_set(const StringName &p_name, const Variant &p_value) {
  83. String str = p_name;
  84. if (str.begins_with("language_support_override/")) {
  85. String lang = str.get_slicec('/', 1);
  86. if (lang == "_new") {
  87. return false;
  88. }
  89. set_language_support_override(lang, p_value);
  90. return true;
  91. }
  92. if (str.begins_with("script_support_override/")) {
  93. String scr = str.get_slicec('/', 1);
  94. if (scr == "_new") {
  95. return false;
  96. }
  97. set_script_support_override(scr, p_value);
  98. return true;
  99. }
  100. if (str.begins_with("variation/")) {
  101. String name = str.get_slicec('/', 1);
  102. set_variation(name, p_value);
  103. return true;
  104. }
  105. return false;
  106. }
  107. bool FontData::_get(const StringName &p_name, Variant &r_ret) const {
  108. String str = p_name;
  109. if (str.begins_with("language_support_override/")) {
  110. String lang = str.get_slicec('/', 1);
  111. if (lang == "_new") {
  112. return true;
  113. }
  114. r_ret = get_language_support_override(lang);
  115. return true;
  116. }
  117. if (str.begins_with("script_support_override/")) {
  118. String scr = str.get_slicec('/', 1);
  119. if (scr == "_new") {
  120. return true;
  121. }
  122. r_ret = get_script_support_override(scr);
  123. return true;
  124. }
  125. if (str.begins_with("variation/")) {
  126. String name = str.get_slicec('/', 1);
  127. r_ret = get_variation(name);
  128. return true;
  129. }
  130. return false;
  131. }
  132. void FontData::_get_property_list(List<PropertyInfo> *p_list) const {
  133. Vector<String> lang_over = get_language_support_overrides();
  134. for (int i = 0; i < lang_over.size(); i++) {
  135. p_list->push_back(PropertyInfo(Variant::BOOL, "language_support_override/" + lang_over[i], PROPERTY_HINT_NONE, "", PROPERTY_USAGE_EDITOR | PROPERTY_USAGE_STORAGE));
  136. }
  137. p_list->push_back(PropertyInfo(Variant::NIL, "language_support_override/_new", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_EDITOR));
  138. Vector<String> scr_over = get_script_support_overrides();
  139. for (int i = 0; i < scr_over.size(); i++) {
  140. p_list->push_back(PropertyInfo(Variant::BOOL, "script_support_override/" + scr_over[i], PROPERTY_HINT_NONE, "", PROPERTY_USAGE_EDITOR | PROPERTY_USAGE_STORAGE));
  141. }
  142. p_list->push_back(PropertyInfo(Variant::NIL, "script_support_override/_new", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_EDITOR));
  143. Dictionary variations = get_variation_list();
  144. for (const Variant *ftr = variations.next(nullptr); ftr != nullptr; ftr = variations.next(ftr)) {
  145. Vector3i v = variations[*ftr];
  146. p_list->push_back(PropertyInfo(Variant::FLOAT, "variation/" + TS->tag_to_name(*ftr), PROPERTY_HINT_RANGE, itos(v.x) + "," + itos(v.y), PROPERTY_USAGE_EDITOR | PROPERTY_USAGE_STORAGE));
  147. }
  148. }
  149. void FontData::reset_state() {
  150. if (rid != RID()) {
  151. TS->free(rid);
  152. }
  153. base_size = 16;
  154. path = String();
  155. }
  156. RID FontData::get_rid() const {
  157. return rid;
  158. }
  159. void FontData::load_resource(const String &p_filename, int p_base_size) {
  160. if (rid != RID()) {
  161. TS->free(rid);
  162. }
  163. rid = TS->create_font_resource(p_filename, p_base_size);
  164. path = p_filename;
  165. base_size = TS->font_get_base_size(rid);
  166. emit_changed();
  167. }
  168. void FontData::_load_memory(const PackedByteArray &p_data, const String &p_type, int p_base_size) {
  169. if (rid != RID()) {
  170. TS->free(rid);
  171. }
  172. rid = TS->create_font_memory(p_data.ptr(), p_data.size(), p_type, p_base_size);
  173. path = TTR("(Memory: " + p_type.to_upper() + " @ 0x" + String::num_int64((uint64_t)p_data.ptr(), 16, true) + ")");
  174. base_size = TS->font_get_base_size(rid);
  175. emit_changed();
  176. }
  177. void FontData::load_memory(const uint8_t *p_data, size_t p_size, const String &p_type, int p_base_size) {
  178. if (rid != RID()) {
  179. TS->free(rid);
  180. }
  181. rid = TS->create_font_memory(p_data, p_size, p_type, p_base_size);
  182. path = TTR("(Memory: " + p_type.to_upper() + " @ 0x" + String::num_int64((uint64_t)p_data, 16, true) + ")");
  183. base_size = TS->font_get_base_size(rid);
  184. emit_changed();
  185. }
  186. void FontData::set_data_path(const String &p_path) {
  187. load_resource(p_path, base_size);
  188. }
  189. String FontData::get_data_path() const {
  190. return path;
  191. }
  192. float FontData::get_height(int p_size) const {
  193. if (rid == RID()) {
  194. return 0.f; // Do not raise errors in getters, to prevent editor from spamming errors on incomplete (without data_path set) fonts.
  195. }
  196. return TS->font_get_height(rid, (p_size < 0) ? base_size : p_size);
  197. }
  198. float FontData::get_ascent(int p_size) const {
  199. if (rid == RID()) {
  200. return 0.f;
  201. }
  202. return TS->font_get_ascent(rid, (p_size < 0) ? base_size : p_size);
  203. }
  204. float FontData::get_descent(int p_size) const {
  205. if (rid == RID()) {
  206. return 0.f;
  207. }
  208. return TS->font_get_descent(rid, (p_size < 0) ? base_size : p_size);
  209. }
  210. float FontData::get_underline_position(int p_size) const {
  211. if (rid == RID()) {
  212. return 0.f;
  213. }
  214. return TS->font_get_underline_position(rid, (p_size < 0) ? base_size : p_size);
  215. }
  216. Dictionary FontData::get_feature_list() const {
  217. if (rid == RID()) {
  218. return Dictionary();
  219. }
  220. return TS->font_get_feature_list(rid);
  221. }
  222. float FontData::get_underline_thickness(int p_size) const {
  223. if (rid == RID()) {
  224. return 0.f;
  225. }
  226. return TS->font_get_underline_thickness(rid, (p_size < 0) ? base_size : p_size);
  227. }
  228. Dictionary FontData::get_variation_list() const {
  229. if (rid == RID()) {
  230. return Dictionary();
  231. }
  232. return TS->font_get_variation_list(rid);
  233. }
  234. void FontData::set_variation(const String &p_name, double p_value) {
  235. ERR_FAIL_COND(rid == RID());
  236. TS->font_set_variation(rid, p_name, p_value);
  237. emit_changed();
  238. }
  239. double FontData::get_variation(const String &p_name) const {
  240. if (rid == RID()) {
  241. return 0;
  242. }
  243. return TS->font_get_variation(rid, p_name);
  244. }
  245. void FontData::set_antialiased(bool p_antialiased) {
  246. ERR_FAIL_COND(rid == RID());
  247. TS->font_set_antialiased(rid, p_antialiased);
  248. emit_changed();
  249. }
  250. bool FontData::get_antialiased() const {
  251. if (rid == RID()) {
  252. return false;
  253. }
  254. return TS->font_get_antialiased(rid);
  255. }
  256. void FontData::set_distance_field_hint(bool p_distance_field) {
  257. ERR_FAIL_COND(rid == RID());
  258. TS->font_set_distance_field_hint(rid, p_distance_field);
  259. emit_changed();
  260. }
  261. bool FontData::get_distance_field_hint() const {
  262. if (rid == RID()) {
  263. return false;
  264. }
  265. return TS->font_get_distance_field_hint(rid);
  266. }
  267. void FontData::set_hinting(TextServer::Hinting p_hinting) {
  268. ERR_FAIL_COND(rid == RID());
  269. TS->font_set_hinting(rid, p_hinting);
  270. emit_changed();
  271. }
  272. TextServer::Hinting FontData::get_hinting() const {
  273. if (rid == RID()) {
  274. return TextServer::HINTING_NONE;
  275. }
  276. return TS->font_get_hinting(rid);
  277. }
  278. void FontData::set_force_autohinter(bool p_enabeld) {
  279. ERR_FAIL_COND(rid == RID());
  280. TS->font_set_force_autohinter(rid, p_enabeld);
  281. emit_changed();
  282. }
  283. bool FontData::get_force_autohinter() const {
  284. if (rid == RID()) {
  285. return false;
  286. }
  287. return TS->font_get_force_autohinter(rid);
  288. }
  289. bool FontData::has_char(char32_t p_char) const {
  290. if (rid == RID()) {
  291. return false;
  292. }
  293. return TS->font_has_char(rid, p_char);
  294. }
  295. String FontData::get_supported_chars() const {
  296. ERR_FAIL_COND_V(rid == RID(), String());
  297. return TS->font_get_supported_chars(rid);
  298. }
  299. Vector2 FontData::get_glyph_advance(uint32_t p_index, int p_size) const {
  300. ERR_FAIL_COND_V(rid == RID(), Vector2());
  301. return TS->font_get_glyph_advance(rid, p_index, (p_size < 0) ? base_size : p_size);
  302. }
  303. Vector2 FontData::get_glyph_kerning(uint32_t p_index_a, uint32_t p_index_b, int p_size) const {
  304. ERR_FAIL_COND_V(rid == RID(), Vector2());
  305. return TS->font_get_glyph_kerning(rid, p_index_a, p_index_b, (p_size < 0) ? base_size : p_size);
  306. }
  307. bool FontData::has_outline() const {
  308. if (rid == RID()) {
  309. return false;
  310. }
  311. return TS->font_has_outline(rid);
  312. }
  313. float FontData::get_base_size() const {
  314. return base_size;
  315. }
  316. bool FontData::is_language_supported(const String &p_language) const {
  317. if (rid == RID()) {
  318. return false;
  319. }
  320. return TS->font_is_language_supported(rid, p_language);
  321. }
  322. void FontData::set_language_support_override(const String &p_language, bool p_supported) {
  323. ERR_FAIL_COND(rid == RID());
  324. TS->font_set_language_support_override(rid, p_language, p_supported);
  325. emit_changed();
  326. }
  327. bool FontData::get_language_support_override(const String &p_language) const {
  328. if (rid == RID()) {
  329. return false;
  330. }
  331. return TS->font_get_language_support_override(rid, p_language);
  332. }
  333. void FontData::remove_language_support_override(const String &p_language) {
  334. ERR_FAIL_COND(rid == RID());
  335. TS->font_remove_language_support_override(rid, p_language);
  336. emit_changed();
  337. }
  338. Vector<String> FontData::get_language_support_overrides() const {
  339. if (rid == RID()) {
  340. return Vector<String>();
  341. }
  342. return TS->font_get_language_support_overrides(rid);
  343. }
  344. bool FontData::is_script_supported(const String &p_script) const {
  345. if (rid == RID()) {
  346. return false;
  347. }
  348. return TS->font_is_script_supported(rid, p_script);
  349. }
  350. void FontData::set_script_support_override(const String &p_script, bool p_supported) {
  351. ERR_FAIL_COND(rid == RID());
  352. TS->font_set_script_support_override(rid, p_script, p_supported);
  353. emit_changed();
  354. }
  355. bool FontData::get_script_support_override(const String &p_script) const {
  356. if (rid == RID()) {
  357. return false;
  358. }
  359. return TS->font_get_script_support_override(rid, p_script);
  360. }
  361. void FontData::remove_script_support_override(const String &p_script) {
  362. ERR_FAIL_COND(rid == RID());
  363. TS->font_remove_script_support_override(rid, p_script);
  364. emit_changed();
  365. }
  366. Vector<String> FontData::get_script_support_overrides() const {
  367. if (rid == RID()) {
  368. return Vector<String>();
  369. }
  370. return TS->font_get_script_support_overrides(rid);
  371. }
  372. uint32_t FontData::get_glyph_index(char32_t p_char, char32_t p_variation_selector) const {
  373. ERR_FAIL_COND_V(rid == RID(), 0);
  374. return TS->font_get_glyph_index(rid, p_char, p_variation_selector);
  375. }
  376. Vector2 FontData::draw_glyph(RID p_canvas, int p_size, const Vector2 &p_pos, uint32_t p_index, const Color &p_color) const {
  377. ERR_FAIL_COND_V(rid == RID(), Vector2());
  378. return TS->font_draw_glyph(rid, p_canvas, (p_size <= 0) ? base_size : p_size, p_pos, p_index, p_color);
  379. }
  380. Vector2 FontData::draw_glyph_outline(RID p_canvas, int p_size, int p_outline_size, const Vector2 &p_pos, uint32_t p_index, const Color &p_color) const {
  381. ERR_FAIL_COND_V(rid == RID(), Vector2());
  382. return TS->font_draw_glyph_outline(rid, p_canvas, (p_size <= 0) ? base_size : p_size, p_outline_size, p_pos, p_index, p_color);
  383. }
  384. FontData::FontData() {}
  385. FontData::FontData(const String &p_filename, int p_base_size) {
  386. load_resource(p_filename, p_base_size);
  387. }
  388. FontData::FontData(const PackedByteArray &p_data, const String &p_type, int p_base_size) {
  389. _load_memory(p_data, p_type, p_base_size);
  390. }
  391. FontData::~FontData() {
  392. if (rid != RID()) {
  393. TS->free(rid);
  394. }
  395. }
  396. /*************************************************************************/
  397. void Font::_bind_methods() {
  398. ClassDB::bind_method(D_METHOD("add_data", "data"), &Font::add_data);
  399. ClassDB::bind_method(D_METHOD("set_data", "idx", "data"), &Font::set_data);
  400. ClassDB::bind_method(D_METHOD("get_data_count"), &Font::get_data_count);
  401. ClassDB::bind_method(D_METHOD("get_data", "idx"), &Font::get_data);
  402. ClassDB::bind_method(D_METHOD("remove_data", "idx"), &Font::remove_data);
  403. ClassDB::bind_method(D_METHOD("get_height", "size"), &Font::get_height, DEFVAL(-1));
  404. ClassDB::bind_method(D_METHOD("get_ascent", "size"), &Font::get_ascent, DEFVAL(-1));
  405. ClassDB::bind_method(D_METHOD("get_descent", "size"), &Font::get_descent, DEFVAL(-1));
  406. ClassDB::bind_method(D_METHOD("get_underline_position", "size"), &Font::get_underline_position, DEFVAL(-1));
  407. ClassDB::bind_method(D_METHOD("get_underline_thickness", "size"), &Font::get_underline_thickness, DEFVAL(-1));
  408. ClassDB::bind_method(D_METHOD("get_spacing", "type"), &Font::get_spacing);
  409. ClassDB::bind_method(D_METHOD("set_spacing", "type", "value"), &Font::set_spacing);
  410. ClassDB::bind_method(D_METHOD("get_string_size", "text", "size"), &Font::get_string_size, DEFVAL(-1));
  411. 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));
  412. 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));
  413. 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));
  414. ClassDB::bind_method(D_METHOD("has_char", "char"), &Font::has_char);
  415. ClassDB::bind_method(D_METHOD("get_supported_chars"), &Font::get_supported_chars);
  416. ClassDB::bind_method(D_METHOD("get_char_size", "char", "next", "size"), &Font::get_char_size, DEFVAL(0), DEFVAL(-1));
  417. 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)));
  418. ClassDB::bind_method(D_METHOD("update_changes"), &Font::update_changes);
  419. ADD_GROUP("Extra Spacing", "extra_spacing");
  420. ADD_PROPERTYI(PropertyInfo(Variant::INT, "extra_spacing_top"), "set_spacing", "get_spacing", SPACING_TOP);
  421. ADD_PROPERTYI(PropertyInfo(Variant::INT, "extra_spacing_bottom"), "set_spacing", "get_spacing", SPACING_BOTTOM);
  422. BIND_ENUM_CONSTANT(SPACING_TOP);
  423. BIND_ENUM_CONSTANT(SPACING_BOTTOM);
  424. }
  425. void Font::_data_changed() {
  426. cache.clear();
  427. cache_wrap.clear();
  428. emit_changed();
  429. notify_property_list_changed();
  430. }
  431. bool Font::_set(const StringName &p_name, const Variant &p_value) {
  432. String str = p_name;
  433. #ifndef DISABLE_DEPRECATED
  434. if (str == "font_data") { // Compatibility, DynamicFont main data
  435. Ref<FontData> fd = p_value;
  436. if (fd.is_valid()) {
  437. add_data(fd);
  438. return true;
  439. }
  440. return false;
  441. } else if (str.begins_with("fallback/")) { // Compatibility, DynamicFont fallback data
  442. Ref<FontData> fd = p_value;
  443. if (fd.is_valid()) {
  444. add_data(fd);
  445. return true;
  446. }
  447. return false;
  448. } else if (str == "fallback") { // Compatibility, BitmapFont fallback
  449. Ref<Font> f = p_value;
  450. if (f.is_valid()) {
  451. for (int i = 0; i < f->get_data_count(); i++) {
  452. add_data(f->get_data(i));
  453. }
  454. return true;
  455. }
  456. return false;
  457. }
  458. #endif /* DISABLE_DEPRECATED */
  459. if (str.begins_with("data/")) {
  460. int idx = str.get_slicec('/', 1).to_int();
  461. Ref<FontData> fd = p_value;
  462. if (fd.is_valid()) {
  463. if (idx == data.size()) {
  464. add_data(fd);
  465. return true;
  466. } else if (idx >= 0 && idx < data.size()) {
  467. set_data(idx, fd);
  468. return true;
  469. } else {
  470. return false;
  471. }
  472. } else if (idx >= 0 && idx < data.size()) {
  473. remove_data(idx);
  474. return true;
  475. }
  476. }
  477. return false;
  478. }
  479. bool Font::_get(const StringName &p_name, Variant &r_ret) const {
  480. String str = p_name;
  481. if (str.begins_with("data/")) {
  482. int idx = str.get_slicec('/', 1).to_int();
  483. if (idx == data.size()) {
  484. r_ret = Ref<FontData>();
  485. return true;
  486. } else if (idx >= 0 && idx < data.size()) {
  487. r_ret = get_data(idx);
  488. return true;
  489. }
  490. }
  491. return false;
  492. }
  493. void Font::_get_property_list(List<PropertyInfo> *p_list) const {
  494. for (int i = 0; i < data.size(); i++) {
  495. p_list->push_back(PropertyInfo(Variant::OBJECT, "data/" + itos(i), PROPERTY_HINT_RESOURCE_TYPE, "FontData"));
  496. }
  497. p_list->push_back(PropertyInfo(Variant::OBJECT, "data/" + itos(data.size()), PROPERTY_HINT_RESOURCE_TYPE, "FontData"));
  498. }
  499. void Font::reset_state() {
  500. spacing_top = 0;
  501. spacing_bottom = 0;
  502. cache.clear();
  503. cache_wrap.clear();
  504. data.clear();
  505. }
  506. void Font::add_data(const Ref<FontData> &p_data) {
  507. ERR_FAIL_COND(p_data.is_null());
  508. data.push_back(p_data);
  509. if (data[data.size() - 1].is_valid()) {
  510. data.write[data.size() - 1]->connect("changed", callable_mp(this, &Font::_data_changed), varray(), CONNECT_REFERENCE_COUNTED);
  511. }
  512. cache.clear();
  513. cache_wrap.clear();
  514. emit_changed();
  515. notify_property_list_changed();
  516. }
  517. void Font::set_data(int p_idx, const Ref<FontData> &p_data) {
  518. ERR_FAIL_COND(p_data.is_null());
  519. ERR_FAIL_INDEX(p_idx, data.size());
  520. if (data[p_idx].is_valid()) {
  521. data.write[p_idx]->disconnect("changed", callable_mp(this, &Font::_data_changed));
  522. }
  523. data.write[p_idx] = p_data;
  524. if (data[p_idx].is_valid()) {
  525. data.write[p_idx]->connect("changed", callable_mp(this, &Font::_data_changed), varray(), CONNECT_REFERENCE_COUNTED);
  526. }
  527. cache.clear();
  528. cache_wrap.clear();
  529. emit_changed();
  530. notify_property_list_changed();
  531. }
  532. int Font::get_data_count() const {
  533. return data.size();
  534. }
  535. Ref<FontData> Font::get_data(int p_idx) const {
  536. ERR_FAIL_INDEX_V(p_idx, data.size(), Ref<FontData>());
  537. return data[p_idx];
  538. }
  539. void Font::remove_data(int p_idx) {
  540. ERR_FAIL_INDEX(p_idx, data.size());
  541. if (data[p_idx].is_valid()) {
  542. data.write[p_idx]->disconnect("changed", callable_mp(this, &Font::_data_changed));
  543. }
  544. data.remove(p_idx);
  545. cache.clear();
  546. cache_wrap.clear();
  547. emit_changed();
  548. notify_property_list_changed();
  549. }
  550. Dictionary Font::get_feature_list() const {
  551. Dictionary out;
  552. for (int i = 0; i < data.size(); i++) {
  553. Dictionary data_ftrs = data[i]->get_feature_list();
  554. for (const Variant *ftr = data_ftrs.next(nullptr); ftr != nullptr; ftr = data_ftrs.next(ftr)) {
  555. out[*ftr] = data_ftrs[*ftr];
  556. }
  557. }
  558. return out;
  559. }
  560. float Font::get_height(int p_size) const {
  561. float ret = 0.f;
  562. for (int i = 0; i < data.size(); i++) {
  563. ret = MAX(ret, data[i]->get_height(p_size));
  564. }
  565. return ret + spacing_top + spacing_bottom;
  566. }
  567. float Font::get_ascent(int p_size) const {
  568. float ret = 0.f;
  569. for (int i = 0; i < data.size(); i++) {
  570. ret = MAX(ret, data[i]->get_ascent(p_size));
  571. }
  572. return ret + spacing_top;
  573. }
  574. float Font::get_descent(int p_size) const {
  575. float ret = 0.f;
  576. for (int i = 0; i < data.size(); i++) {
  577. ret = MAX(ret, data[i]->get_descent(p_size));
  578. }
  579. return ret + spacing_bottom;
  580. }
  581. float Font::get_underline_position(int p_size) const {
  582. float ret = 0.f;
  583. for (int i = 0; i < data.size(); i++) {
  584. ret = MAX(ret, data[i]->get_underline_position(p_size));
  585. }
  586. return ret;
  587. }
  588. float Font::get_underline_thickness(int p_size) const {
  589. float ret = 0.f;
  590. for (int i = 0; i < data.size(); i++) {
  591. ret = MAX(ret, data[i]->get_underline_thickness(p_size));
  592. }
  593. return ret;
  594. }
  595. int Font::get_spacing(int p_type) const {
  596. if (p_type == SPACING_TOP) {
  597. return spacing_top;
  598. } else if (p_type == SPACING_BOTTOM) {
  599. return spacing_bottom;
  600. }
  601. return 0;
  602. }
  603. void Font::set_spacing(int p_type, int p_value) {
  604. if (p_type == SPACING_TOP) {
  605. spacing_top = p_value;
  606. } else if (p_type == SPACING_BOTTOM) {
  607. spacing_bottom = p_value;
  608. }
  609. emit_changed();
  610. notify_property_list_changed();
  611. }
  612. // Drawing string and string sizes, cached.
  613. Size2 Font::get_string_size(const String &p_text, int p_size) const {
  614. ERR_FAIL_COND_V(data.is_empty(), Size2());
  615. uint64_t hash = p_text.hash64();
  616. hash = hash_djb2_one_64(p_size, hash);
  617. Ref<TextLine> buffer;
  618. if (cache.has(hash)) {
  619. buffer = cache.get(hash);
  620. } else {
  621. buffer.instance();
  622. int size = p_size <= 0 ? data[0]->get_base_size() : p_size;
  623. buffer->add_string(p_text, Ref<Font>(this), size, Dictionary(), TranslationServer::get_singleton()->get_tool_locale());
  624. cache.insert(hash, buffer);
  625. }
  626. if (buffer->get_orientation() == TextServer::ORIENTATION_HORIZONTAL) {
  627. return buffer->get_size() + Vector2(0, spacing_top + spacing_bottom);
  628. } else {
  629. return buffer->get_size() + Vector2(spacing_top + spacing_bottom, 0);
  630. }
  631. }
  632. Size2 Font::get_multiline_string_size(const String &p_text, float p_width, int p_size, uint8_t p_flags) const {
  633. ERR_FAIL_COND_V(data.is_empty(), Size2());
  634. uint64_t hash = p_text.hash64();
  635. hash = hash_djb2_one_64(p_size, hash);
  636. uint64_t wrp_hash = hash_djb2_one_64(hash_djb2_one_float(p_width), hash);
  637. wrp_hash = hash_djb2_one_64(p_flags, wrp_hash);
  638. Ref<TextParagraph> lines_buffer;
  639. if (cache_wrap.has(wrp_hash)) {
  640. lines_buffer = cache_wrap.get(wrp_hash);
  641. } else {
  642. lines_buffer.instance();
  643. int size = p_size <= 0 ? data[0]->get_base_size() : p_size;
  644. lines_buffer->add_string(p_text, Ref<Font>(this), size, Dictionary(), TranslationServer::get_singleton()->get_tool_locale());
  645. lines_buffer->set_width(p_width);
  646. lines_buffer->set_flags(p_flags);
  647. cache_wrap.insert(wrp_hash, lines_buffer);
  648. }
  649. Size2 ret;
  650. for (int i = 0; i < lines_buffer->get_line_count(); i++) {
  651. Size2 line_size = lines_buffer->get_line_size(i);
  652. if (lines_buffer->get_orientation() == TextServer::ORIENTATION_HORIZONTAL) {
  653. ret.x = MAX(ret.x, line_size.x);
  654. ret.y += line_size.y + spacing_top + spacing_bottom;
  655. } else {
  656. ret.y = MAX(ret.y, line_size.y);
  657. ret.x += line_size.x + spacing_top + spacing_bottom;
  658. }
  659. }
  660. return ret;
  661. }
  662. void Font::draw_string(RID p_canvas_item, const Point2 &p_pos, const String &p_text, HAlign p_align, float p_width, int p_size, const Color &p_modulate, int p_outline_size, const Color &p_outline_modulate, uint8_t p_flags) const {
  663. uint64_t hash = p_text.hash64();
  664. hash = hash_djb2_one_64(p_size, hash);
  665. Ref<TextLine> buffer;
  666. if (cache.has(hash)) {
  667. buffer = cache.get(hash);
  668. } else {
  669. buffer.instance();
  670. int size = p_size <= 0 ? data[0]->get_base_size() : p_size;
  671. buffer->add_string(p_text, Ref<Font>(this), size, Dictionary(), TranslationServer::get_singleton()->get_tool_locale());
  672. cache.insert(hash, buffer);
  673. }
  674. Vector2 ofs = p_pos;
  675. if (buffer->get_orientation() == TextServer::ORIENTATION_HORIZONTAL) {
  676. ofs.y += spacing_top - buffer->get_line_ascent();
  677. } else {
  678. ofs.x += spacing_top - buffer->get_line_ascent();
  679. }
  680. buffer->set_width(p_width);
  681. buffer->set_align(p_align);
  682. if (p_outline_size > 0 && p_outline_modulate.a != 0.0f) {
  683. buffer->draw_outline(p_canvas_item, ofs, p_outline_size, p_outline_modulate);
  684. }
  685. buffer->draw(p_canvas_item, ofs, p_modulate);
  686. }
  687. 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, uint8_t p_flags) const {
  688. uint64_t hash = p_text.hash64();
  689. hash = hash_djb2_one_64(p_size, hash);
  690. uint64_t wrp_hash = hash_djb2_one_64(hash_djb2_one_float(p_width), hash);
  691. wrp_hash = hash_djb2_one_64(p_flags, wrp_hash);
  692. Ref<TextParagraph> lines_buffer;
  693. if (cache_wrap.has(wrp_hash)) {
  694. lines_buffer = cache_wrap.get(wrp_hash);
  695. } else {
  696. lines_buffer.instance();
  697. int size = p_size <= 0 ? data[0]->get_base_size() : p_size;
  698. lines_buffer->add_string(p_text, Ref<Font>(this), size, Dictionary(), TranslationServer::get_singleton()->get_tool_locale());
  699. lines_buffer->set_width(p_width);
  700. lines_buffer->set_flags(p_flags);
  701. cache_wrap.insert(wrp_hash, lines_buffer);
  702. }
  703. lines_buffer->set_align(p_align);
  704. Vector2 lofs = p_pos;
  705. for (int i = 0; i < lines_buffer->get_line_count(); i++) {
  706. if (lines_buffer->get_orientation() == TextServer::ORIENTATION_HORIZONTAL) {
  707. lofs.y += spacing_top;
  708. if (i == 0) {
  709. lofs.y -= lines_buffer->get_line_ascent(0);
  710. }
  711. } else {
  712. lofs.x += spacing_top;
  713. if (i == 0) {
  714. lofs.x -= lines_buffer->get_line_ascent(0);
  715. }
  716. }
  717. if (p_width > 0) {
  718. lines_buffer->set_align(p_align);
  719. }
  720. if (p_outline_size > 0 && p_outline_modulate.a != 0.0f) {
  721. lines_buffer->draw_line_outline(p_canvas_item, lofs, i, p_outline_size, p_outline_modulate);
  722. }
  723. lines_buffer->draw_line(p_canvas_item, lofs, i, p_modulate);
  724. Size2 line_size = lines_buffer->get_line_size(i);
  725. if (lines_buffer->get_orientation() == TextServer::ORIENTATION_HORIZONTAL) {
  726. lofs.y += line_size.y + spacing_bottom;
  727. } else {
  728. lofs.x += line_size.x + spacing_bottom;
  729. }
  730. if ((p_max_lines > 0) && (i >= p_max_lines)) {
  731. return;
  732. }
  733. }
  734. }
  735. bool Font::has_char(char32_t p_char) const {
  736. for (int i = 0; i < data.size(); i++) {
  737. if (data[i]->has_char(p_char)) {
  738. return true;
  739. }
  740. }
  741. return false;
  742. }
  743. String Font::get_supported_chars() const {
  744. String chars;
  745. for (int i = 0; i < data.size(); i++) {
  746. String data_chars = data[i]->get_supported_chars();
  747. for (int j = 0; j < data_chars.length(); j++) {
  748. if (chars.find_char(data_chars[j]) == -1) {
  749. chars += data_chars[j];
  750. }
  751. }
  752. }
  753. return chars;
  754. }
  755. Size2 Font::get_char_size(char32_t p_char, char32_t p_next, int p_size) const {
  756. for (int i = 0; i < data.size(); i++) {
  757. if (data[i]->has_char(p_char)) {
  758. int size = p_size <= 0 ? data[i]->get_base_size() : p_size;
  759. uint32_t glyph_a = data[i]->get_glyph_index(p_char);
  760. Size2 ret = Size2(data[i]->get_glyph_advance(glyph_a, size).x, data[i]->get_height(size));
  761. if ((p_next != 0) && data[i]->has_char(p_next)) {
  762. uint32_t glyph_b = data[i]->get_glyph_index(p_next);
  763. ret.x -= data[i]->get_glyph_kerning(glyph_a, glyph_b, size).x;
  764. }
  765. return ret;
  766. }
  767. }
  768. return Size2();
  769. }
  770. float 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 {
  771. for (int i = 0; i < data.size(); i++) {
  772. if (data[i]->has_char(p_char)) {
  773. int size = p_size <= 0 ? data[i]->get_base_size() : p_size;
  774. uint32_t glyph_a = data[i]->get_glyph_index(p_char);
  775. float ret = data[i]->get_glyph_advance(glyph_a, size).x;
  776. if ((p_next != 0) && data[i]->has_char(p_next)) {
  777. uint32_t glyph_b = data[i]->get_glyph_index(p_next);
  778. ret -= data[i]->get_glyph_kerning(glyph_a, glyph_b, size).x;
  779. }
  780. if (p_outline_size > 0 && p_outline_modulate.a != 0.0f) {
  781. data[i]->draw_glyph_outline(p_canvas_item, size, p_outline_size, p_pos, glyph_a, p_outline_modulate);
  782. }
  783. data[i]->draw_glyph(p_canvas_item, size, p_pos, glyph_a, p_modulate);
  784. return ret;
  785. }
  786. }
  787. return 0;
  788. }
  789. Vector<RID> Font::get_rids() const {
  790. Vector<RID> ret;
  791. for (int i = 0; i < data.size(); i++) {
  792. RID rid = data[i]->get_rid();
  793. if (rid != RID()) {
  794. ret.push_back(rid);
  795. }
  796. }
  797. return ret;
  798. }
  799. void Font::update_changes() {
  800. emit_changed();
  801. }
  802. Font::Font() {
  803. cache.set_capacity(128);
  804. cache_wrap.set_capacity(32);
  805. }
  806. Font::~Font() {
  807. cache.clear();
  808. cache_wrap.clear();
  809. }
  810. /*************************************************************************/
  811. RES ResourceFormatLoaderFont::load(const String &p_path, const String &p_original_path, Error *r_error, bool p_use_sub_threads, float *r_progress, CacheMode p_cache_mode) {
  812. if (r_error) {
  813. *r_error = ERR_FILE_CANT_OPEN;
  814. }
  815. Ref<FontData> dfont;
  816. dfont.instance();
  817. dfont->load_resource(p_path);
  818. if (r_error) {
  819. *r_error = OK;
  820. }
  821. return dfont;
  822. }
  823. void ResourceFormatLoaderFont::get_recognized_extensions_for_type(const String &p_type, List<String> *p_extensions) const {
  824. #ifndef DISABLE_DEPRECATED
  825. if (p_type == "DynamicFontData") {
  826. p_extensions->push_back("ttf");
  827. p_extensions->push_back("otf");
  828. p_extensions->push_back("woff");
  829. return;
  830. }
  831. if (p_type == "BitmapFont") { // BitmapFont (*.font, *fnt) is handled by ResourceFormatLoaderCompatFont
  832. return;
  833. }
  834. #endif /* DISABLE_DEPRECATED */
  835. if (p_type == "" || handles_type(p_type)) {
  836. get_recognized_extensions(p_extensions);
  837. }
  838. }
  839. void ResourceFormatLoaderFont::get_recognized_extensions(List<String> *p_extensions) const {
  840. p_extensions->push_back("ttf");
  841. p_extensions->push_back("otf");
  842. p_extensions->push_back("woff");
  843. p_extensions->push_back("font");
  844. p_extensions->push_back("fnt");
  845. }
  846. bool ResourceFormatLoaderFont::handles_type(const String &p_type) const {
  847. return (p_type == "FontData");
  848. }
  849. String ResourceFormatLoaderFont::get_resource_type(const String &p_path) const {
  850. String el = p_path.get_extension().to_lower();
  851. if (el == "ttf" || el == "otf" || el == "woff" || el == "font" || el == "fnt") {
  852. return "FontData";
  853. }
  854. return "";
  855. }
  856. #ifndef DISABLE_DEPRECATED
  857. RES ResourceFormatLoaderCompatFont::load(const String &p_path, const String &p_original_path, Error *r_error, bool p_use_sub_threads, float *r_progress, CacheMode p_cache_mode) {
  858. if (r_error) {
  859. *r_error = ERR_FILE_CANT_OPEN;
  860. }
  861. Ref<FontData> dfont;
  862. dfont.instance();
  863. dfont->load_resource(p_path);
  864. Ref<Font> font;
  865. font.instance();
  866. font->add_data(dfont);
  867. if (r_error) {
  868. *r_error = OK;
  869. }
  870. return font;
  871. }
  872. void ResourceFormatLoaderCompatFont::get_recognized_extensions_for_type(const String &p_type, List<String> *p_extensions) const {
  873. if (p_type == "BitmapFont") {
  874. p_extensions->push_back("font");
  875. p_extensions->push_back("fnt");
  876. }
  877. }
  878. void ResourceFormatLoaderCompatFont::get_recognized_extensions(List<String> *p_extensions) const {
  879. }
  880. bool ResourceFormatLoaderCompatFont::handles_type(const String &p_type) const {
  881. return (p_type == "Font");
  882. }
  883. String ResourceFormatLoaderCompatFont::get_resource_type(const String &p_path) const {
  884. return "";
  885. }
  886. #endif /* DISABLE_DEPRECATED */