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_char_size", "char", "next"), &Font::get_char_size, DEFVAL(0));
  85. ClassDB::bind_method(D_METHOD("get_string_size", "string"), &Font::get_string_size);
  86. ClassDB::bind_method(D_METHOD("get_wordwrap_string_size", "string", "width"), &Font::get_wordwrap_string_size);
  87. ClassDB::bind_method(D_METHOD("has_outline"), &Font::has_outline);
  88. 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));
  89. ClassDB::bind_method(D_METHOD("update_changes"), &Font::update_changes);
  90. }
  91. Font::Font() {
  92. }
  93. /////////////////////////////////////////////////////////////////
  94. void BitmapFont::_set_chars(const PoolVector<int> &p_chars) {
  95. int len = p_chars.size();
  96. //char 1 charsize 1 texture, 4 rect, 2 align, advance 1
  97. ERR_FAIL_COND(len % 9);
  98. if (!len)
  99. return; //none to do
  100. int chars = len / 9;
  101. PoolVector<int>::Read r = p_chars.read();
  102. for (int i = 0; i < chars; i++) {
  103. const int *data = &r[i * 9];
  104. add_char(data[0], data[1], Rect2(data[2], data[3], data[4], data[5]), Size2(data[6], data[7]), data[8]);
  105. }
  106. }
  107. PoolVector<int> BitmapFont::_get_chars() const {
  108. PoolVector<int> chars;
  109. const CharType *key = NULL;
  110. while ((key = char_map.next(key))) {
  111. const Character *c = char_map.getptr(*key);
  112. ERR_FAIL_COND_V(!c, PoolVector<int>());
  113. chars.push_back(*key);
  114. chars.push_back(c->texture_idx);
  115. chars.push_back(c->rect.position.x);
  116. chars.push_back(c->rect.position.y);
  117. chars.push_back(c->rect.size.x);
  118. chars.push_back(c->rect.size.y);
  119. chars.push_back(c->h_align);
  120. chars.push_back(c->v_align);
  121. chars.push_back(c->advance);
  122. }
  123. return chars;
  124. }
  125. void BitmapFont::_set_kernings(const PoolVector<int> &p_kernings) {
  126. int len = p_kernings.size();
  127. ERR_FAIL_COND(len % 3);
  128. if (!len)
  129. return;
  130. PoolVector<int>::Read r = p_kernings.read();
  131. for (int i = 0; i < len / 3; i++) {
  132. const int *data = &r[i * 3];
  133. add_kerning_pair(data[0], data[1], data[2]);
  134. }
  135. }
  136. PoolVector<int> BitmapFont::_get_kernings() const {
  137. PoolVector<int> kernings;
  138. for (Map<KerningPairKey, int>::Element *E = kerning_map.front(); E; E = E->next()) {
  139. kernings.push_back(E->key().A);
  140. kernings.push_back(E->key().B);
  141. kernings.push_back(E->get());
  142. }
  143. return kernings;
  144. }
  145. void BitmapFont::_set_textures(const Vector<Variant> &p_textures) {
  146. textures.clear();
  147. for (int i = 0; i < p_textures.size(); i++) {
  148. Ref<Texture> tex = p_textures[i];
  149. ERR_CONTINUE(!tex.is_valid());
  150. add_texture(tex);
  151. }
  152. }
  153. Vector<Variant> BitmapFont::_get_textures() const {
  154. Vector<Variant> rtextures;
  155. for (int i = 0; i < textures.size(); i++)
  156. rtextures.push_back(textures[i].get_ref_ptr());
  157. return rtextures;
  158. }
  159. Error BitmapFont::create_from_fnt(const String &p_file) {
  160. //fnt format used by angelcode bmfont
  161. //http://www.angelcode.com/products/bmfont/
  162. FileAccess *f = FileAccess::open(p_file, FileAccess::READ);
  163. ERR_FAIL_COND_V_MSG(!f, ERR_FILE_NOT_FOUND, "Can't open font: " + p_file + ".");
  164. clear();
  165. while (true) {
  166. String line = f->get_line();
  167. int delimiter = line.find(" ");
  168. String type = line.substr(0, delimiter);
  169. int pos = delimiter + 1;
  170. Map<String, String> keys;
  171. while (pos < line.size() && line[pos] == ' ')
  172. pos++;
  173. while (pos < line.size()) {
  174. int eq = line.find("=", pos);
  175. if (eq == -1)
  176. break;
  177. String key = line.substr(pos, eq - pos);
  178. int end = -1;
  179. String value;
  180. if (line[eq + 1] == '"') {
  181. end = line.find("\"", eq + 2);
  182. if (end == -1)
  183. break;
  184. value = line.substr(eq + 2, end - 1 - eq - 1);
  185. pos = end + 1;
  186. } else {
  187. end = line.find(" ", eq + 1);
  188. if (end == -1)
  189. end = line.size();
  190. value = line.substr(eq + 1, end - eq);
  191. pos = end;
  192. }
  193. while (pos < line.size() && line[pos] == ' ')
  194. pos++;
  195. keys[key] = value;
  196. }
  197. if (type == "info") {
  198. if (keys.has("face"))
  199. set_name(keys["face"]);
  200. /*
  201. if (keys.has("size"))
  202. font->set_height(keys["size"].to_int());
  203. */
  204. } else if (type == "common") {
  205. if (keys.has("lineHeight"))
  206. set_height(keys["lineHeight"].to_int());
  207. if (keys.has("base"))
  208. set_ascent(keys["base"].to_int());
  209. } else if (type == "page") {
  210. if (keys.has("file")) {
  211. String base_dir = p_file.get_base_dir();
  212. String file = base_dir.plus_file(keys["file"]);
  213. Ref<Texture> tex = ResourceLoader::load(file);
  214. if (tex.is_null()) {
  215. ERR_PRINT("Can't load font texture!");
  216. } else {
  217. add_texture(tex);
  218. }
  219. }
  220. } else if (type == "char") {
  221. CharType idx = 0;
  222. if (keys.has("id"))
  223. idx = keys["id"].to_int();
  224. Rect2 rect;
  225. if (keys.has("x"))
  226. rect.position.x = keys["x"].to_int();
  227. if (keys.has("y"))
  228. rect.position.y = keys["y"].to_int();
  229. if (keys.has("width"))
  230. rect.size.width = keys["width"].to_int();
  231. if (keys.has("height"))
  232. rect.size.height = keys["height"].to_int();
  233. Point2 ofs;
  234. if (keys.has("xoffset"))
  235. ofs.x = keys["xoffset"].to_int();
  236. if (keys.has("yoffset"))
  237. ofs.y = keys["yoffset"].to_int();
  238. int texture = 0;
  239. if (keys.has("page"))
  240. texture = keys["page"].to_int();
  241. int advance = -1;
  242. if (keys.has("xadvance"))
  243. advance = keys["xadvance"].to_int();
  244. add_char(idx, texture, rect, ofs, advance);
  245. } else if (type == "kerning") {
  246. CharType first = 0, second = 0;
  247. int k = 0;
  248. if (keys.has("first"))
  249. first = keys["first"].to_int();
  250. if (keys.has("second"))
  251. second = keys["second"].to_int();
  252. if (keys.has("amount"))
  253. k = keys["amount"].to_int();
  254. add_kerning_pair(first, second, -k);
  255. }
  256. if (f->eof_reached())
  257. break;
  258. }
  259. memdelete(f);
  260. return OK;
  261. }
  262. void BitmapFont::set_height(float p_height) {
  263. height = p_height;
  264. }
  265. float BitmapFont::get_height() const {
  266. return height;
  267. }
  268. void BitmapFont::set_ascent(float p_ascent) {
  269. ascent = p_ascent;
  270. }
  271. float BitmapFont::get_ascent() const {
  272. return ascent;
  273. }
  274. float BitmapFont::get_descent() const {
  275. return height - ascent;
  276. }
  277. void BitmapFont::add_texture(const Ref<Texture> &p_texture) {
  278. ERR_FAIL_COND_MSG(p_texture.is_null(), "It's not a reference to a valid Texture object.");
  279. textures.push_back(p_texture);
  280. }
  281. int BitmapFont::get_texture_count() const {
  282. return textures.size();
  283. };
  284. Ref<Texture> BitmapFont::get_texture(int p_idx) const {
  285. ERR_FAIL_INDEX_V(p_idx, textures.size(), Ref<Texture>());
  286. return textures[p_idx];
  287. };
  288. int BitmapFont::get_character_count() const {
  289. return char_map.size();
  290. };
  291. Vector<CharType> BitmapFont::get_char_keys() const {
  292. Vector<CharType> chars;
  293. chars.resize(char_map.size());
  294. const CharType *ct = NULL;
  295. int count = 0;
  296. while ((ct = char_map.next(ct))) {
  297. chars.write[count++] = *ct;
  298. };
  299. return chars;
  300. };
  301. BitmapFont::Character BitmapFont::get_character(CharType p_char) const {
  302. if (!char_map.has(p_char)) {
  303. ERR_FAIL_V(Character());
  304. };
  305. return char_map[p_char];
  306. };
  307. void BitmapFont::add_char(CharType p_char, int p_texture_idx, const Rect2 &p_rect, const Size2 &p_align, float p_advance) {
  308. if (p_advance < 0)
  309. p_advance = p_rect.size.width;
  310. Character c;
  311. c.rect = p_rect;
  312. c.texture_idx = p_texture_idx;
  313. c.v_align = p_align.y;
  314. c.advance = p_advance;
  315. c.h_align = p_align.x;
  316. char_map[p_char] = c;
  317. }
  318. void BitmapFont::add_kerning_pair(CharType p_A, CharType p_B, int p_kerning) {
  319. KerningPairKey kpk;
  320. kpk.A = p_A;
  321. kpk.B = p_B;
  322. if (p_kerning == 0 && kerning_map.has(kpk)) {
  323. kerning_map.erase(kpk);
  324. } else {
  325. kerning_map[kpk] = p_kerning;
  326. }
  327. }
  328. Vector<BitmapFont::KerningPairKey> BitmapFont::get_kerning_pair_keys() const {
  329. Vector<BitmapFont::KerningPairKey> ret;
  330. ret.resize(kerning_map.size());
  331. int i = 0;
  332. for (Map<KerningPairKey, int>::Element *E = kerning_map.front(); E; E = E->next()) {
  333. ret.write[i++] = E->key();
  334. }
  335. return ret;
  336. }
  337. int BitmapFont::get_kerning_pair(CharType p_A, CharType p_B) const {
  338. KerningPairKey kpk;
  339. kpk.A = p_A;
  340. kpk.B = p_B;
  341. const Map<KerningPairKey, int>::Element *E = kerning_map.find(kpk);
  342. if (E)
  343. return E->get();
  344. return 0;
  345. }
  346. void BitmapFont::set_distance_field_hint(bool p_distance_field) {
  347. distance_field_hint = p_distance_field;
  348. emit_changed();
  349. }
  350. bool BitmapFont::is_distance_field_hint() const {
  351. return distance_field_hint;
  352. }
  353. void BitmapFont::clear() {
  354. height = 1;
  355. ascent = 0;
  356. char_map.clear();
  357. textures.clear();
  358. kerning_map.clear();
  359. distance_field_hint = false;
  360. }
  361. Size2 Font::get_string_size(const String &p_string) const {
  362. float w = 0;
  363. int l = p_string.length();
  364. if (l == 0)
  365. return Size2(0, get_height());
  366. const CharType *sptr = &p_string[0];
  367. for (int i = 0; i < l; i++) {
  368. w += get_char_size(sptr[i], sptr[i + 1]).width;
  369. }
  370. return Size2(w, get_height());
  371. }
  372. Size2 Font::get_wordwrap_string_size(const String &p_string, float p_width) const {
  373. ERR_FAIL_COND_V(p_width <= 0, Vector2(0, get_height()));
  374. int l = p_string.length();
  375. if (l == 0)
  376. return Size2(p_width, get_height());
  377. float line_w = 0;
  378. float h = 0;
  379. float space_w = get_char_size(' ').width;
  380. Vector<String> lines = p_string.split("\n");
  381. for (int i = 0; i < lines.size(); i++) {
  382. h += get_height();
  383. String t = lines[i];
  384. line_w = 0;
  385. Vector<String> words = t.split(" ");
  386. for (int j = 0; j < words.size(); j++) {
  387. line_w += get_string_size(words[j]).x;
  388. if (line_w > p_width) {
  389. h += get_height();
  390. line_w = get_string_size(words[j]).x;
  391. } else {
  392. line_w += space_w;
  393. }
  394. }
  395. }
  396. return Size2(p_width, h);
  397. }
  398. void BitmapFont::set_fallback(const Ref<BitmapFont> &p_fallback) {
  399. for (Ref<BitmapFont> fallback_child = p_fallback; fallback_child != NULL; fallback_child = fallback_child->get_fallback()) {
  400. ERR_FAIL_COND_MSG(fallback_child == this, "Can't set as fallback one of its parents to prevent crashes due to recursive loop.");
  401. }
  402. fallback = p_fallback;
  403. }
  404. Ref<BitmapFont> BitmapFont::get_fallback() const {
  405. return fallback;
  406. }
  407. 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 {
  408. const Character *c = char_map.getptr(p_char);
  409. if (!c) {
  410. if (fallback.is_valid())
  411. return fallback->draw_char(p_canvas_item, p_pos, p_char, p_next, p_modulate, p_outline);
  412. return 0;
  413. }
  414. ERR_FAIL_COND_V(c->texture_idx < -1 || c->texture_idx >= textures.size(), 0);
  415. if (!p_outline && c->texture_idx != -1) {
  416. Point2 cpos = p_pos;
  417. cpos.x += c->h_align;
  418. cpos.y -= ascent;
  419. cpos.y += c->v_align;
  420. 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);
  421. }
  422. return get_char_size(p_char, p_next).width;
  423. }
  424. Size2 BitmapFont::get_char_size(CharType p_char, CharType p_next) const {
  425. const Character *c = char_map.getptr(p_char);
  426. if (!c) {
  427. if (fallback.is_valid())
  428. return fallback->get_char_size(p_char, p_next);
  429. return Size2();
  430. }
  431. Size2 ret(c->advance, c->rect.size.y);
  432. if (p_next) {
  433. KerningPairKey kpk;
  434. kpk.A = p_char;
  435. kpk.B = p_next;
  436. const Map<KerningPairKey, int>::Element *E = kerning_map.find(kpk);
  437. if (E) {
  438. ret.width -= E->get();
  439. }
  440. }
  441. return ret;
  442. }
  443. void BitmapFont::_bind_methods() {
  444. ClassDB::bind_method(D_METHOD("create_from_fnt", "path"), &BitmapFont::create_from_fnt);
  445. ClassDB::bind_method(D_METHOD("set_height", "px"), &BitmapFont::set_height);
  446. ClassDB::bind_method(D_METHOD("set_ascent", "px"), &BitmapFont::set_ascent);
  447. ClassDB::bind_method(D_METHOD("add_kerning_pair", "char_a", "char_b", "kerning"), &BitmapFont::add_kerning_pair);
  448. ClassDB::bind_method(D_METHOD("get_kerning_pair", "char_a", "char_b"), &BitmapFont::get_kerning_pair);
  449. ClassDB::bind_method(D_METHOD("add_texture", "texture"), &BitmapFont::add_texture);
  450. ClassDB::bind_method(D_METHOD("add_char", "character", "texture", "rect", "align", "advance"), &BitmapFont::add_char, DEFVAL(Point2()), DEFVAL(-1));
  451. ClassDB::bind_method(D_METHOD("get_texture_count"), &BitmapFont::get_texture_count);
  452. ClassDB::bind_method(D_METHOD("get_texture", "idx"), &BitmapFont::get_texture);
  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. }