font.cpp 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684
  1. /*************************************************************************/
  2. /* font.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2020 Godot Engine contributors (cf. AUTHORS.md). */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /*************************************************************************/
  30. #include "font.h"
  31. #include "core/io/resource_loader.h"
  32. #include "core/method_bind_ext.gen.inc"
  33. #include "core/os/file_access.h"
  34. void Font::draw_halign(RID p_canvas_item, const Point2 &p_pos, HAlign p_align, float p_width, const String &p_text, const Color &p_modulate, const Color &p_outline_modulate) const {
  35. float length = get_string_size(p_text).width;
  36. if (length >= p_width) {
  37. draw(p_canvas_item, p_pos, p_text, p_modulate, p_width, p_outline_modulate);
  38. return;
  39. }
  40. float ofs = 0.f;
  41. switch (p_align) {
  42. case HALIGN_LEFT: {
  43. ofs = 0;
  44. } break;
  45. case HALIGN_CENTER: {
  46. ofs = Math::floor((p_width - length) / 2.0);
  47. } break;
  48. case HALIGN_RIGHT: {
  49. ofs = p_width - length;
  50. } break;
  51. default: {
  52. ERR_PRINT("Unknown halignment type");
  53. } break;
  54. }
  55. draw(p_canvas_item, p_pos + Point2(ofs, 0), p_text, p_modulate, p_width, p_outline_modulate);
  56. }
  57. void Font::draw(RID p_canvas_item, const Point2 &p_pos, const String &p_text, const Color &p_modulate, int p_clip_w, const Color &p_outline_modulate) const {
  58. Vector2 ofs;
  59. int chars_drawn = 0;
  60. bool with_outline = has_outline();
  61. for (int i = 0; i < p_text.length(); i++) {
  62. int width = get_char_size(p_text[i]).width;
  63. if (p_clip_w >= 0 && (ofs.x + width) > p_clip_w)
  64. break; //clip
  65. ofs.x += draw_char(p_canvas_item, p_pos + ofs, p_text[i], p_text[i + 1], with_outline ? p_outline_modulate : p_modulate, with_outline);
  66. ++chars_drawn;
  67. }
  68. if (has_outline()) {
  69. ofs = Vector2(0, 0);
  70. for (int i = 0; i < chars_drawn; i++) {
  71. ofs.x += draw_char(p_canvas_item, p_pos + ofs, p_text[i], p_text[i + 1], p_modulate, false);
  72. }
  73. }
  74. }
  75. void Font::update_changes() {
  76. emit_changed();
  77. }
  78. void Font::_bind_methods() {
  79. ClassDB::bind_method(D_METHOD("draw", "canvas_item", "position", "string", "modulate", "clip_w", "outline_modulate"), &Font::draw, DEFVAL(Color(1, 1, 1)), DEFVAL(-1), DEFVAL(Color(1, 1, 1)));
  80. ClassDB::bind_method(D_METHOD("get_ascent"), &Font::get_ascent);
  81. ClassDB::bind_method(D_METHOD("get_descent"), &Font::get_descent);
  82. ClassDB::bind_method(D_METHOD("get_height"), &Font::get_height);
  83. ClassDB::bind_method(D_METHOD("is_distance_field_hint"), &Font::is_distance_field_hint);
  84. ClassDB::bind_method(D_METHOD("get_string_size", "string"), &Font::get_string_size);
  85. ClassDB::bind_method(D_METHOD("get_wordwrap_string_size", "string", "width"), &Font::get_wordwrap_string_size);
  86. ClassDB::bind_method(D_METHOD("has_outline"), &Font::has_outline);
  87. ClassDB::bind_method(D_METHOD("draw_char", "canvas_item", "position", "char", "next", "modulate", "outline"), &Font::draw_char, DEFVAL(-1), DEFVAL(Color(1, 1, 1)), DEFVAL(false));
  88. ClassDB::bind_method(D_METHOD("update_changes"), &Font::update_changes);
  89. }
  90. Font::Font() {
  91. }
  92. /////////////////////////////////////////////////////////////////
  93. void BitmapFont::_set_chars(const PoolVector<int> &p_chars) {
  94. int len = p_chars.size();
  95. //char 1 charsize 1 texture, 4 rect, 2 align, advance 1
  96. ERR_FAIL_COND(len % 9);
  97. if (!len)
  98. return; //none to do
  99. int chars = len / 9;
  100. PoolVector<int>::Read r = p_chars.read();
  101. for (int i = 0; i < chars; i++) {
  102. const int *data = &r[i * 9];
  103. add_char(data[0], data[1], Rect2(data[2], data[3], data[4], data[5]), Size2(data[6], data[7]), data[8]);
  104. }
  105. }
  106. PoolVector<int> BitmapFont::_get_chars() const {
  107. PoolVector<int> chars;
  108. const CharType *key = NULL;
  109. while ((key = char_map.next(key))) {
  110. const Character *c = char_map.getptr(*key);
  111. ERR_FAIL_COND_V(!c, PoolVector<int>());
  112. chars.push_back(*key);
  113. chars.push_back(c->texture_idx);
  114. chars.push_back(c->rect.position.x);
  115. chars.push_back(c->rect.position.y);
  116. chars.push_back(c->rect.size.x);
  117. chars.push_back(c->rect.size.y);
  118. chars.push_back(c->h_align);
  119. chars.push_back(c->v_align);
  120. chars.push_back(c->advance);
  121. }
  122. return chars;
  123. }
  124. void BitmapFont::_set_kernings(const PoolVector<int> &p_kernings) {
  125. int len = p_kernings.size();
  126. ERR_FAIL_COND(len % 3);
  127. if (!len)
  128. return;
  129. PoolVector<int>::Read r = p_kernings.read();
  130. for (int i = 0; i < len / 3; i++) {
  131. const int *data = &r[i * 3];
  132. add_kerning_pair(data[0], data[1], data[2]);
  133. }
  134. }
  135. PoolVector<int> BitmapFont::_get_kernings() const {
  136. PoolVector<int> kernings;
  137. for (Map<KerningPairKey, int>::Element *E = kerning_map.front(); E; E = E->next()) {
  138. kernings.push_back(E->key().A);
  139. kernings.push_back(E->key().B);
  140. kernings.push_back(E->get());
  141. }
  142. return kernings;
  143. }
  144. void BitmapFont::_set_textures(const Vector<Variant> &p_textures) {
  145. textures.clear();
  146. for (int i = 0; i < p_textures.size(); i++) {
  147. Ref<Texture> tex = p_textures[i];
  148. ERR_CONTINUE(!tex.is_valid());
  149. add_texture(tex);
  150. }
  151. }
  152. Vector<Variant> BitmapFont::_get_textures() const {
  153. Vector<Variant> rtextures;
  154. for (int i = 0; i < textures.size(); i++)
  155. rtextures.push_back(textures[i].get_ref_ptr());
  156. return rtextures;
  157. }
  158. Error BitmapFont::create_from_fnt(const String &p_file) {
  159. //fnt format used by angelcode bmfont
  160. //http://www.angelcode.com/products/bmfont/
  161. FileAccess *f = FileAccess::open(p_file, FileAccess::READ);
  162. ERR_FAIL_COND_V_MSG(!f, ERR_FILE_NOT_FOUND, "Can't open font: " + p_file + ".");
  163. clear();
  164. while (true) {
  165. String line = f->get_line();
  166. int delimiter = line.find(" ");
  167. String type = line.substr(0, delimiter);
  168. int pos = delimiter + 1;
  169. Map<String, String> keys;
  170. while (pos < line.size() && line[pos] == ' ')
  171. pos++;
  172. while (pos < line.size()) {
  173. int eq = line.find("=", pos);
  174. if (eq == -1)
  175. break;
  176. String key = line.substr(pos, eq - pos);
  177. int end = -1;
  178. String value;
  179. if (line[eq + 1] == '"') {
  180. end = line.find("\"", eq + 2);
  181. if (end == -1)
  182. break;
  183. value = line.substr(eq + 2, end - 1 - eq - 1);
  184. pos = end + 1;
  185. } else {
  186. end = line.find(" ", eq + 1);
  187. if (end == -1)
  188. end = line.size();
  189. value = line.substr(eq + 1, end - eq);
  190. pos = end;
  191. }
  192. while (pos < line.size() && line[pos] == ' ')
  193. pos++;
  194. keys[key] = value;
  195. }
  196. if (type == "info") {
  197. if (keys.has("face"))
  198. set_name(keys["face"]);
  199. /*
  200. if (keys.has("size"))
  201. font->set_height(keys["size"].to_int());
  202. */
  203. } else if (type == "common") {
  204. if (keys.has("lineHeight"))
  205. set_height(keys["lineHeight"].to_int());
  206. if (keys.has("base"))
  207. set_ascent(keys["base"].to_int());
  208. } else if (type == "page") {
  209. if (keys.has("file")) {
  210. String base_dir = p_file.get_base_dir();
  211. String file = base_dir.plus_file(keys["file"]);
  212. Ref<Texture> tex = ResourceLoader::load(file);
  213. if (tex.is_null()) {
  214. ERR_PRINT("Can't load font texture!");
  215. } else {
  216. add_texture(tex);
  217. }
  218. }
  219. } else if (type == "char") {
  220. CharType idx = 0;
  221. if (keys.has("id"))
  222. idx = keys["id"].to_int();
  223. Rect2 rect;
  224. if (keys.has("x"))
  225. rect.position.x = keys["x"].to_int();
  226. if (keys.has("y"))
  227. rect.position.y = keys["y"].to_int();
  228. if (keys.has("width"))
  229. rect.size.width = keys["width"].to_int();
  230. if (keys.has("height"))
  231. rect.size.height = keys["height"].to_int();
  232. Point2 ofs;
  233. if (keys.has("xoffset"))
  234. ofs.x = keys["xoffset"].to_int();
  235. if (keys.has("yoffset"))
  236. ofs.y = keys["yoffset"].to_int();
  237. int texture = 0;
  238. if (keys.has("page"))
  239. texture = keys["page"].to_int();
  240. int advance = -1;
  241. if (keys.has("xadvance"))
  242. advance = keys["xadvance"].to_int();
  243. add_char(idx, texture, rect, ofs, advance);
  244. } else if (type == "kerning") {
  245. CharType first = 0, second = 0;
  246. int k = 0;
  247. if (keys.has("first"))
  248. first = keys["first"].to_int();
  249. if (keys.has("second"))
  250. second = keys["second"].to_int();
  251. if (keys.has("amount"))
  252. k = keys["amount"].to_int();
  253. add_kerning_pair(first, second, -k);
  254. }
  255. if (f->eof_reached())
  256. break;
  257. }
  258. memdelete(f);
  259. return OK;
  260. }
  261. void BitmapFont::set_height(float p_height) {
  262. height = p_height;
  263. }
  264. float BitmapFont::get_height() const {
  265. return height;
  266. }
  267. void BitmapFont::set_ascent(float p_ascent) {
  268. ascent = p_ascent;
  269. }
  270. float BitmapFont::get_ascent() const {
  271. return ascent;
  272. }
  273. float BitmapFont::get_descent() const {
  274. return height - ascent;
  275. }
  276. void BitmapFont::add_texture(const Ref<Texture> &p_texture) {
  277. ERR_FAIL_COND_MSG(p_texture.is_null(), "It's not a reference to a valid Texture object.");
  278. textures.push_back(p_texture);
  279. }
  280. int BitmapFont::get_texture_count() const {
  281. return textures.size();
  282. };
  283. Ref<Texture> BitmapFont::get_texture(int p_idx) const {
  284. ERR_FAIL_INDEX_V(p_idx, textures.size(), Ref<Texture>());
  285. return textures[p_idx];
  286. };
  287. int BitmapFont::get_character_count() const {
  288. return char_map.size();
  289. };
  290. Vector<CharType> BitmapFont::get_char_keys() const {
  291. Vector<CharType> chars;
  292. chars.resize(char_map.size());
  293. const CharType *ct = NULL;
  294. int count = 0;
  295. while ((ct = char_map.next(ct))) {
  296. chars.write[count++] = *ct;
  297. };
  298. return chars;
  299. };
  300. BitmapFont::Character BitmapFont::get_character(CharType p_char) const {
  301. if (!char_map.has(p_char)) {
  302. ERR_FAIL_V(Character());
  303. };
  304. return char_map[p_char];
  305. };
  306. void BitmapFont::add_char(CharType p_char, int p_texture_idx, const Rect2 &p_rect, const Size2 &p_align, float p_advance) {
  307. if (p_advance < 0)
  308. p_advance = p_rect.size.width;
  309. Character c;
  310. c.rect = p_rect;
  311. c.texture_idx = p_texture_idx;
  312. c.v_align = p_align.y;
  313. c.advance = p_advance;
  314. c.h_align = p_align.x;
  315. char_map[p_char] = c;
  316. }
  317. void BitmapFont::add_kerning_pair(CharType p_A, CharType p_B, int p_kerning) {
  318. KerningPairKey kpk;
  319. kpk.A = p_A;
  320. kpk.B = p_B;
  321. if (p_kerning == 0 && kerning_map.has(kpk)) {
  322. kerning_map.erase(kpk);
  323. } else {
  324. kerning_map[kpk] = p_kerning;
  325. }
  326. }
  327. Vector<BitmapFont::KerningPairKey> BitmapFont::get_kerning_pair_keys() const {
  328. Vector<BitmapFont::KerningPairKey> ret;
  329. ret.resize(kerning_map.size());
  330. int i = 0;
  331. for (Map<KerningPairKey, int>::Element *E = kerning_map.front(); E; E = E->next()) {
  332. ret.write[i++] = E->key();
  333. }
  334. return ret;
  335. }
  336. int BitmapFont::get_kerning_pair(CharType p_A, CharType p_B) const {
  337. KerningPairKey kpk;
  338. kpk.A = p_A;
  339. kpk.B = p_B;
  340. const Map<KerningPairKey, int>::Element *E = kerning_map.find(kpk);
  341. if (E)
  342. return E->get();
  343. return 0;
  344. }
  345. void BitmapFont::set_distance_field_hint(bool p_distance_field) {
  346. distance_field_hint = p_distance_field;
  347. emit_changed();
  348. }
  349. bool BitmapFont::is_distance_field_hint() const {
  350. return distance_field_hint;
  351. }
  352. void BitmapFont::clear() {
  353. height = 1;
  354. ascent = 0;
  355. char_map.clear();
  356. textures.clear();
  357. kerning_map.clear();
  358. distance_field_hint = false;
  359. }
  360. Size2 Font::get_string_size(const String &p_string) const {
  361. float w = 0;
  362. int l = p_string.length();
  363. if (l == 0)
  364. return Size2(0, get_height());
  365. const CharType *sptr = &p_string[0];
  366. for (int i = 0; i < l; i++) {
  367. w += get_char_size(sptr[i], sptr[i + 1]).width;
  368. }
  369. return Size2(w, get_height());
  370. }
  371. Size2 Font::get_wordwrap_string_size(const String &p_string, float p_width) const {
  372. ERR_FAIL_COND_V(p_width <= 0, Vector2(0, get_height()));
  373. int l = p_string.length();
  374. if (l == 0)
  375. return Size2(p_width, get_height());
  376. float line_w = 0;
  377. float h = 0;
  378. float space_w = get_char_size(' ').width;
  379. Vector<String> lines = p_string.split("\n");
  380. for (int i = 0; i < lines.size(); i++) {
  381. h += get_height();
  382. String t = lines[i];
  383. line_w = 0;
  384. Vector<String> words = t.split(" ");
  385. for (int j = 0; j < words.size(); j++) {
  386. line_w += get_string_size(words[j]).x;
  387. if (line_w > p_width) {
  388. h += get_height();
  389. line_w = get_string_size(words[j]).x;
  390. } else {
  391. line_w += space_w;
  392. }
  393. }
  394. }
  395. return Size2(p_width, h);
  396. }
  397. void BitmapFont::set_fallback(const Ref<BitmapFont> &p_fallback) {
  398. for (Ref<BitmapFont> fallback_child = p_fallback; fallback_child != NULL; fallback_child = fallback_child->get_fallback()) {
  399. ERR_FAIL_COND_MSG(fallback_child == this, "Can't set as fallback one of its parents to prevent crashes due to recursive loop.");
  400. }
  401. fallback = p_fallback;
  402. }
  403. Ref<BitmapFont> BitmapFont::get_fallback() const {
  404. return fallback;
  405. }
  406. float BitmapFont::draw_char(RID p_canvas_item, const Point2 &p_pos, CharType p_char, CharType p_next, const Color &p_modulate, bool p_outline) const {
  407. const Character *c = char_map.getptr(p_char);
  408. if (!c) {
  409. if (fallback.is_valid())
  410. return fallback->draw_char(p_canvas_item, p_pos, p_char, p_next, p_modulate, p_outline);
  411. return 0;
  412. }
  413. ERR_FAIL_COND_V(c->texture_idx < -1 || c->texture_idx >= textures.size(), 0);
  414. if (!p_outline && c->texture_idx != -1) {
  415. Point2 cpos = p_pos;
  416. cpos.x += c->h_align;
  417. cpos.y -= ascent;
  418. cpos.y += c->v_align;
  419. VisualServer::get_singleton()->canvas_item_add_texture_rect_region(p_canvas_item, Rect2(cpos, c->rect.size), textures[c->texture_idx]->get_rid(), c->rect, p_modulate, false, RID(), false);
  420. }
  421. return get_char_size(p_char, p_next).width;
  422. }
  423. Size2 BitmapFont::get_char_size(CharType p_char, CharType p_next) const {
  424. const Character *c = char_map.getptr(p_char);
  425. if (!c) {
  426. if (fallback.is_valid())
  427. return fallback->get_char_size(p_char, p_next);
  428. return Size2();
  429. }
  430. Size2 ret(c->advance, c->rect.size.y);
  431. if (p_next) {
  432. KerningPairKey kpk;
  433. kpk.A = p_char;
  434. kpk.B = p_next;
  435. const Map<KerningPairKey, int>::Element *E = kerning_map.find(kpk);
  436. if (E) {
  437. ret.width -= E->get();
  438. }
  439. }
  440. return ret;
  441. }
  442. void BitmapFont::_bind_methods() {
  443. ClassDB::bind_method(D_METHOD("create_from_fnt", "path"), &BitmapFont::create_from_fnt);
  444. ClassDB::bind_method(D_METHOD("set_height", "px"), &BitmapFont::set_height);
  445. ClassDB::bind_method(D_METHOD("set_ascent", "px"), &BitmapFont::set_ascent);
  446. ClassDB::bind_method(D_METHOD("add_kerning_pair", "char_a", "char_b", "kerning"), &BitmapFont::add_kerning_pair);
  447. ClassDB::bind_method(D_METHOD("get_kerning_pair", "char_a", "char_b"), &BitmapFont::get_kerning_pair);
  448. ClassDB::bind_method(D_METHOD("add_texture", "texture"), &BitmapFont::add_texture);
  449. ClassDB::bind_method(D_METHOD("add_char", "character", "texture", "rect", "align", "advance"), &BitmapFont::add_char, DEFVAL(Point2()), DEFVAL(-1));
  450. ClassDB::bind_method(D_METHOD("get_texture_count"), &BitmapFont::get_texture_count);
  451. ClassDB::bind_method(D_METHOD("get_texture", "idx"), &BitmapFont::get_texture);
  452. ClassDB::bind_method(D_METHOD("get_char_size", "char", "next"), &BitmapFont::get_char_size, DEFVAL(0));
  453. ClassDB::bind_method(D_METHOD("set_distance_field_hint", "enable"), &BitmapFont::set_distance_field_hint);
  454. ClassDB::bind_method(D_METHOD("clear"), &BitmapFont::clear);
  455. ClassDB::bind_method(D_METHOD("_set_chars"), &BitmapFont::_set_chars);
  456. ClassDB::bind_method(D_METHOD("_get_chars"), &BitmapFont::_get_chars);
  457. ClassDB::bind_method(D_METHOD("_set_kernings"), &BitmapFont::_set_kernings);
  458. ClassDB::bind_method(D_METHOD("_get_kernings"), &BitmapFont::_get_kernings);
  459. ClassDB::bind_method(D_METHOD("_set_textures"), &BitmapFont::_set_textures);
  460. ClassDB::bind_method(D_METHOD("_get_textures"), &BitmapFont::_get_textures);
  461. ClassDB::bind_method(D_METHOD("set_fallback", "fallback"), &BitmapFont::set_fallback);
  462. ClassDB::bind_method(D_METHOD("get_fallback"), &BitmapFont::get_fallback);
  463. ADD_PROPERTY(PropertyInfo(Variant::ARRAY, "textures", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NOEDITOR | PROPERTY_USAGE_INTERNAL), "_set_textures", "_get_textures");
  464. ADD_PROPERTY(PropertyInfo(Variant::POOL_INT_ARRAY, "chars", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NOEDITOR | PROPERTY_USAGE_INTERNAL), "_set_chars", "_get_chars");
  465. ADD_PROPERTY(PropertyInfo(Variant::POOL_INT_ARRAY, "kernings", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NOEDITOR | PROPERTY_USAGE_INTERNAL), "_set_kernings", "_get_kernings");
  466. ADD_PROPERTY(PropertyInfo(Variant::REAL, "height", PROPERTY_HINT_RANGE, "1,1024,1"), "set_height", "get_height");
  467. ADD_PROPERTY(PropertyInfo(Variant::REAL, "ascent", PROPERTY_HINT_RANGE, "0,1024,1"), "set_ascent", "get_ascent");
  468. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "distance_field"), "set_distance_field_hint", "is_distance_field_hint");
  469. ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "fallback", PROPERTY_HINT_RESOURCE_TYPE, "BitmapFont"), "set_fallback", "get_fallback");
  470. }
  471. BitmapFont::BitmapFont() {
  472. clear();
  473. }
  474. BitmapFont::~BitmapFont() {
  475. clear();
  476. }
  477. ////////////
  478. RES ResourceFormatLoaderBMFont::load(const String &p_path, const String &p_original_path, Error *r_error) {
  479. if (r_error)
  480. *r_error = ERR_FILE_CANT_OPEN;
  481. Ref<BitmapFont> font;
  482. font.instance();
  483. Error err = font->create_from_fnt(p_path);
  484. if (err) {
  485. if (r_error)
  486. *r_error = err;
  487. return RES();
  488. }
  489. return font;
  490. }
  491. void ResourceFormatLoaderBMFont::get_recognized_extensions(List<String> *p_extensions) const {
  492. p_extensions->push_back("fnt");
  493. }
  494. bool ResourceFormatLoaderBMFont::handles_type(const String &p_type) const {
  495. return (p_type == "BitmapFont");
  496. }
  497. String ResourceFormatLoaderBMFont::get_resource_type(const String &p_path) const {
  498. String el = p_path.get_extension().to_lower();
  499. if (el == "fnt")
  500. return "BitmapFont";
  501. return "";
  502. }