bitmap_font_adv.cpp 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580
  1. /*************************************************************************/
  2. /* bitmap_font_adv.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 "bitmap_font_adv.h"
  31. /*************************************************************************/
  32. /* hb_bmp_font_t HarfBuzz Bitmap font interface */
  33. /*************************************************************************/
  34. struct hb_bmp_font_t {
  35. BitmapFontDataAdvanced *face = nullptr;
  36. float font_size = 0.0;
  37. bool unref = false; /* Whether to destroy bm_face when done. */
  38. };
  39. static hb_bmp_font_t *_hb_bmp_font_create(BitmapFontDataAdvanced *p_face, float p_font_size, bool p_unref) {
  40. hb_bmp_font_t *bm_font = reinterpret_cast<hb_bmp_font_t *>(calloc(1, sizeof(hb_bmp_font_t)));
  41. if (!bm_font) {
  42. return nullptr;
  43. }
  44. bm_font->face = p_face;
  45. bm_font->font_size = p_font_size;
  46. bm_font->unref = p_unref;
  47. return bm_font;
  48. }
  49. static void _hb_bmp_font_destroy(void *data) {
  50. hb_bmp_font_t *bm_font = reinterpret_cast<hb_bmp_font_t *>(data);
  51. free(bm_font);
  52. }
  53. static hb_bool_t hb_bmp_get_nominal_glyph(hb_font_t *font, void *font_data, hb_codepoint_t unicode, hb_codepoint_t *glyph, void *user_data) {
  54. const hb_bmp_font_t *bm_font = reinterpret_cast<const hb_bmp_font_t *>(font_data);
  55. if (!bm_font->face) {
  56. return false;
  57. }
  58. if (!bm_font->face->has_char(unicode)) {
  59. if (bm_font->face->has_char(0xF000u + unicode)) {
  60. *glyph = 0xF000u + unicode;
  61. return true;
  62. } else {
  63. return false;
  64. }
  65. }
  66. *glyph = unicode;
  67. return true;
  68. }
  69. static hb_position_t hb_bmp_get_glyph_h_advance(hb_font_t *font, void *font_data, hb_codepoint_t glyph, void *user_data) {
  70. const hb_bmp_font_t *bm_font = reinterpret_cast<const hb_bmp_font_t *>(font_data);
  71. if (!bm_font->face) {
  72. return 0;
  73. }
  74. if (!bm_font->face->has_char(glyph)) {
  75. return 0;
  76. }
  77. return bm_font->face->get_advance(glyph, bm_font->font_size).x * 64;
  78. }
  79. static hb_position_t hb_bmp_get_glyph_v_advance(hb_font_t *font, void *font_data, hb_codepoint_t glyph, void *user_data) {
  80. const hb_bmp_font_t *bm_font = reinterpret_cast<const hb_bmp_font_t *>(font_data);
  81. if (!bm_font->face) {
  82. return 0;
  83. }
  84. if (!bm_font->face->has_char(glyph)) {
  85. return 0;
  86. }
  87. return -bm_font->face->get_advance(glyph, bm_font->font_size).y * 64;
  88. }
  89. static hb_position_t hb_bmp_get_glyph_h_kerning(hb_font_t *font, void *font_data, hb_codepoint_t left_glyph, hb_codepoint_t right_glyph, void *user_data) {
  90. const hb_bmp_font_t *bm_font = reinterpret_cast<const hb_bmp_font_t *>(font_data);
  91. if (!bm_font->face) {
  92. return 0;
  93. }
  94. if (!bm_font->face->has_char(left_glyph)) {
  95. return 0;
  96. }
  97. if (!bm_font->face->has_char(right_glyph)) {
  98. return 0;
  99. }
  100. return bm_font->face->get_kerning(left_glyph, right_glyph, bm_font->font_size).x * 64;
  101. }
  102. static hb_bool_t hb_bmp_get_glyph_v_origin(hb_font_t *font, void *font_data, hb_codepoint_t glyph, hb_position_t *x, hb_position_t *y, void *user_data) {
  103. const hb_bmp_font_t *bm_font = reinterpret_cast<const hb_bmp_font_t *>(font_data);
  104. if (!bm_font->face) {
  105. return false;
  106. }
  107. if (!bm_font->face->has_char(glyph)) {
  108. return false;
  109. }
  110. *x = bm_font->face->get_advance(glyph, bm_font->font_size).x * 32;
  111. *y = bm_font->face->get_ascent(bm_font->font_size) * 64;
  112. return true;
  113. }
  114. static hb_bool_t hb_bmp_get_glyph_extents(hb_font_t *font, void *font_data, hb_codepoint_t glyph, hb_glyph_extents_t *extents, void *user_data) {
  115. const hb_bmp_font_t *bm_font = reinterpret_cast<const hb_bmp_font_t *>(font_data);
  116. if (!bm_font->face) {
  117. return false;
  118. }
  119. if (!bm_font->face->has_char(glyph)) {
  120. return false;
  121. }
  122. extents->x_bearing = 0;
  123. extents->y_bearing = 0;
  124. extents->width = bm_font->face->get_size(glyph, bm_font->font_size).x * 64;
  125. extents->height = bm_font->face->get_size(glyph, bm_font->font_size).y * 64;
  126. return true;
  127. }
  128. static hb_bool_t hb_bmp_get_font_h_extents(hb_font_t *font, void *font_data, hb_font_extents_t *metrics, void *user_data) {
  129. const hb_bmp_font_t *bm_font = reinterpret_cast<const hb_bmp_font_t *>(font_data);
  130. if (!bm_font->face) {
  131. return false;
  132. }
  133. metrics->ascender = bm_font->face->get_ascent(bm_font->font_size);
  134. metrics->descender = bm_font->face->get_descent(bm_font->font_size);
  135. metrics->line_gap = 0;
  136. return true;
  137. }
  138. static hb_font_funcs_t *funcs = nullptr;
  139. void hb_bmp_create_font_funcs() {
  140. funcs = hb_font_funcs_create();
  141. hb_font_funcs_set_font_h_extents_func(funcs, hb_bmp_get_font_h_extents, nullptr, nullptr);
  142. //hb_font_funcs_set_font_v_extents_func (funcs, hb_bmp_get_font_v_extents, nullptr, nullptr);
  143. hb_font_funcs_set_nominal_glyph_func(funcs, hb_bmp_get_nominal_glyph, nullptr, nullptr);
  144. //hb_font_funcs_set_variation_glyph_func (funcs, hb_bmp_get_variation_glyph, nullptr, nullptr);
  145. hb_font_funcs_set_glyph_h_advance_func(funcs, hb_bmp_get_glyph_h_advance, nullptr, nullptr);
  146. hb_font_funcs_set_glyph_v_advance_func(funcs, hb_bmp_get_glyph_v_advance, nullptr, nullptr);
  147. //hb_font_funcs_set_glyph_h_origin_func(funcs, hb_bmp_get_glyph_h_origin, nullptr, nullptr);
  148. hb_font_funcs_set_glyph_v_origin_func(funcs, hb_bmp_get_glyph_v_origin, nullptr, nullptr);
  149. hb_font_funcs_set_glyph_h_kerning_func(funcs, hb_bmp_get_glyph_h_kerning, nullptr, nullptr);
  150. //hb_font_funcs_set_glyph_v_kerning_func (funcs, hb_bmp_get_glyph_v_kerning, nullptr, nullptr);
  151. hb_font_funcs_set_glyph_extents_func(funcs, hb_bmp_get_glyph_extents, nullptr, nullptr);
  152. //hb_font_funcs_set_glyph_contour_point_func (funcs, hb_bmp_get_glyph_contour_point, nullptr, nullptr);
  153. //hb_font_funcs_set_glyph_name_func (funcs, hb_bmp_get_glyph_name, nullptr, nullptr);
  154. //hb_font_funcs_set_glyph_from_name_func (funcs, hb_bmp_get_glyph_from_name, nullptr, nullptr);
  155. hb_font_funcs_make_immutable(funcs);
  156. }
  157. void hb_bmp_free_font_funcs() {
  158. if (funcs != nullptr) {
  159. hb_font_funcs_destroy(funcs);
  160. funcs = nullptr;
  161. }
  162. }
  163. static void _hb_bmp_font_set_funcs(hb_font_t *p_font, BitmapFontDataAdvanced *p_face, int p_size, bool p_unref) {
  164. hb_font_set_funcs(p_font, funcs, _hb_bmp_font_create(p_face, p_size, p_unref), _hb_bmp_font_destroy);
  165. }
  166. hb_font_t *hb_bmp_font_create(BitmapFontDataAdvanced *p_face, int p_size, hb_destroy_func_t p_destroy) {
  167. hb_font_t *font;
  168. hb_face_t *face = hb_face_create(nullptr, 0);
  169. font = hb_font_create(face);
  170. hb_face_destroy(face);
  171. _hb_bmp_font_set_funcs(font, p_face, p_size, false);
  172. return font;
  173. }
  174. /*************************************************************************/
  175. /* BitmapFontDataAdvanced */
  176. /*************************************************************************/
  177. Error BitmapFontDataAdvanced::load_from_file(const String &p_filename, int p_base_size) {
  178. _THREAD_SAFE_METHOD_
  179. //fnt format used by angelcode bmfont
  180. //http://www.angelcode.com/products/bmfont/
  181. FileAccess *f = FileAccess::open(p_filename, FileAccess::READ);
  182. ERR_FAIL_COND_V_MSG(!f, ERR_FILE_NOT_FOUND, "Can't open font: " + p_filename + ".");
  183. while (true) {
  184. String line = f->get_line();
  185. int delimiter = line.find(" ");
  186. String type = line.substr(0, delimiter);
  187. int pos = delimiter + 1;
  188. Map<String, String> keys;
  189. while (pos < line.size() && line[pos] == ' ') {
  190. pos++;
  191. }
  192. while (pos < line.size()) {
  193. int eq = line.find("=", pos);
  194. if (eq == -1) {
  195. break;
  196. }
  197. String key = line.substr(pos, eq - pos);
  198. int end = -1;
  199. String value;
  200. if (line[eq + 1] == '"') {
  201. end = line.find("\"", eq + 2);
  202. if (end == -1) {
  203. break;
  204. }
  205. value = line.substr(eq + 2, end - 1 - eq - 1);
  206. pos = end + 1;
  207. } else {
  208. end = line.find(" ", eq + 1);
  209. if (end == -1) {
  210. end = line.size();
  211. }
  212. value = line.substr(eq + 1, end - eq);
  213. pos = end;
  214. }
  215. while (pos < line.size() && line[pos] == ' ') {
  216. pos++;
  217. }
  218. keys[key] = value;
  219. }
  220. if (type == "info") {
  221. if (keys.has("size")) {
  222. base_size = keys["size"].to_int();
  223. }
  224. } else if (type == "common") {
  225. if (keys.has("lineHeight")) {
  226. height = keys["lineHeight"].to_int();
  227. }
  228. if (keys.has("base")) {
  229. ascent = keys["base"].to_int();
  230. }
  231. } else if (type == "page") {
  232. if (keys.has("file")) {
  233. String base_dir = p_filename.get_base_dir();
  234. String file = base_dir.plus_file(keys["file"]);
  235. if (RenderingServer::get_singleton() != nullptr) {
  236. Ref<Texture2D> tex = ResourceLoader::load(file);
  237. if (tex.is_null()) {
  238. ERR_PRINT("Can't load font texture!");
  239. } else {
  240. ERR_FAIL_COND_V_MSG(tex.is_null(), ERR_FILE_CANT_READ, "It's not a reference to a valid Texture object.");
  241. textures.push_back(tex);
  242. }
  243. }
  244. }
  245. } else if (type == "char") {
  246. Character c;
  247. char32_t idx = 0;
  248. if (keys.has("id")) {
  249. idx = keys["id"].to_int();
  250. }
  251. if (keys.has("x")) {
  252. c.rect.position.x = keys["x"].to_int();
  253. }
  254. if (keys.has("y")) {
  255. c.rect.position.y = keys["y"].to_int();
  256. }
  257. if (keys.has("width")) {
  258. c.rect.size.width = keys["width"].to_int();
  259. }
  260. if (keys.has("height")) {
  261. c.rect.size.height = keys["height"].to_int();
  262. }
  263. if (keys.has("xoffset")) {
  264. c.align.x = keys["xoffset"].to_int();
  265. }
  266. if (keys.has("yoffset")) {
  267. c.align.y = keys["yoffset"].to_int();
  268. }
  269. if (keys.has("page")) {
  270. c.texture_idx = keys["page"].to_int();
  271. }
  272. if (keys.has("xadvance")) {
  273. c.advance.x = keys["xadvance"].to_int();
  274. }
  275. if (keys.has("yadvance")) {
  276. c.advance.x = keys["yadvance"].to_int();
  277. }
  278. if (c.advance.x < 0) {
  279. c.advance.x = c.rect.size.width + 1;
  280. }
  281. if (c.advance.y < 0) {
  282. c.advance.y = c.rect.size.height + 1;
  283. }
  284. char_map[idx] = c;
  285. } else if (type == "kerning") {
  286. KerningPairKey kpk;
  287. float k = 0.0;
  288. if (keys.has("first")) {
  289. kpk.A = keys["first"].to_int();
  290. }
  291. if (keys.has("second")) {
  292. kpk.B = keys["second"].to_int();
  293. }
  294. if (keys.has("amount")) {
  295. k = keys["amount"].to_int();
  296. }
  297. kerning_map[kpk] = k;
  298. }
  299. if (f->eof_reached()) {
  300. break;
  301. }
  302. }
  303. if (base_size == 0) {
  304. base_size = height;
  305. }
  306. valid = true;
  307. memdelete(f);
  308. return OK;
  309. }
  310. Error BitmapFontDataAdvanced::load_from_memory(const uint8_t *p_data, size_t p_size, int p_base_size) {
  311. _THREAD_SAFE_METHOD_
  312. ERR_FAIL_COND_V(p_data == nullptr, ERR_CANT_CREATE);
  313. ERR_FAIL_COND_V(p_size != sizeof(TextServer::BitmapFontData), ERR_CANT_CREATE);
  314. const TextServer::BitmapFontData *data = (const TextServer::BitmapFontData *)p_data;
  315. if (RenderingServer::get_singleton() != nullptr) {
  316. Ref<Image> image = memnew(Image(data->img));
  317. Ref<ImageTexture> tex = memnew(ImageTexture);
  318. tex->create_from_image(image);
  319. textures.push_back(tex);
  320. }
  321. for (int i = 0; i < data->charcount; i++) {
  322. const int *c = &data->char_rects[i * 8];
  323. Character chr;
  324. chr.rect.position.x = c[1];
  325. chr.rect.position.y = c[2];
  326. chr.rect.size.x = c[3];
  327. chr.rect.size.y = c[4];
  328. if (c[7] < 0) {
  329. chr.advance.x = c[3];
  330. } else {
  331. chr.advance.x = c[7];
  332. }
  333. chr.align = Vector2(c[6], c[5]);
  334. char_map[c[0]] = chr;
  335. }
  336. for (int i = 0; i < data->kerning_count; i++) {
  337. KerningPairKey kpk;
  338. kpk.A = data->kernings[i * 3 + 0];
  339. kpk.B = data->kernings[i * 3 + 1];
  340. if (data->kernings[i * 3 + 2] == 0 && kerning_map.has(kpk)) {
  341. kerning_map.erase(kpk);
  342. } else {
  343. kerning_map[kpk] = data->kernings[i * 3 + 2];
  344. }
  345. }
  346. height = data->height;
  347. ascent = data->ascent;
  348. base_size = p_base_size;
  349. if (base_size == 0) {
  350. base_size = height;
  351. }
  352. valid = true;
  353. return OK;
  354. }
  355. float BitmapFontDataAdvanced::get_height(int p_size) const {
  356. ERR_FAIL_COND_V(!valid, 0.f);
  357. return height * (float(p_size) / float(base_size));
  358. }
  359. float BitmapFontDataAdvanced::get_ascent(int p_size) const {
  360. ERR_FAIL_COND_V(!valid, 0.f);
  361. return ascent * (float(p_size) / float(base_size));
  362. }
  363. float BitmapFontDataAdvanced::get_descent(int p_size) const {
  364. ERR_FAIL_COND_V(!valid, 0.f);
  365. return (height - ascent) * (float(p_size) / float(base_size));
  366. }
  367. float BitmapFontDataAdvanced::get_underline_position(int p_size) const {
  368. ERR_FAIL_COND_V(!valid, 0.f);
  369. return 2 * (float(p_size) / float(base_size));
  370. }
  371. float BitmapFontDataAdvanced::get_underline_thickness(int p_size) const {
  372. ERR_FAIL_COND_V(!valid, 0.f);
  373. return 1 * (float(p_size) / float(base_size));
  374. }
  375. void BitmapFontDataAdvanced::set_distance_field_hint(bool p_distance_field) {
  376. distance_field_hint = p_distance_field;
  377. }
  378. bool BitmapFontDataAdvanced::get_distance_field_hint() const {
  379. return distance_field_hint;
  380. }
  381. float BitmapFontDataAdvanced::get_base_size() const {
  382. return base_size;
  383. }
  384. hb_font_t *BitmapFontDataAdvanced::get_hb_handle(int p_size) {
  385. _THREAD_SAFE_METHOD_
  386. ERR_FAIL_COND_V(!valid, nullptr);
  387. if (!cache.has(p_size)) {
  388. cache[p_size] = hb_bmp_font_create(this, p_size, nullptr);
  389. }
  390. return cache[p_size];
  391. }
  392. bool BitmapFontDataAdvanced::has_char(char32_t p_char) const {
  393. _THREAD_SAFE_METHOD_
  394. ERR_FAIL_COND_V(!valid, false);
  395. return char_map.has(p_char);
  396. }
  397. String BitmapFontDataAdvanced::get_supported_chars() const {
  398. _THREAD_SAFE_METHOD_
  399. ERR_FAIL_COND_V(!valid, String());
  400. String chars;
  401. const uint32_t *k = nullptr;
  402. while ((k = char_map.next(k))) {
  403. chars += char32_t(*k);
  404. }
  405. return chars;
  406. }
  407. Vector2 BitmapFontDataAdvanced::get_advance(uint32_t p_char, int p_size) const {
  408. _THREAD_SAFE_METHOD_
  409. ERR_FAIL_COND_V(!valid, Vector2());
  410. const Character *c = char_map.getptr(p_char);
  411. ERR_FAIL_COND_V(c == nullptr, Vector2());
  412. return c->advance * (float(p_size) / float(base_size));
  413. }
  414. Vector2 BitmapFontDataAdvanced::get_align(uint32_t p_char, int p_size) const {
  415. _THREAD_SAFE_METHOD_
  416. ERR_FAIL_COND_V(!valid, Vector2());
  417. const Character *c = char_map.getptr(p_char);
  418. ERR_FAIL_COND_V(c == nullptr, Vector2());
  419. return c->align * (float(p_size) / float(base_size));
  420. }
  421. Vector2 BitmapFontDataAdvanced::get_size(uint32_t p_char, int p_size) const {
  422. _THREAD_SAFE_METHOD_
  423. ERR_FAIL_COND_V(!valid, Vector2());
  424. const Character *c = char_map.getptr(p_char);
  425. ERR_FAIL_COND_V(c == nullptr, Vector2());
  426. return c->rect.size * (float(p_size) / float(base_size));
  427. }
  428. Vector2 BitmapFontDataAdvanced::get_kerning(uint32_t p_char, uint32_t p_next, int p_size) const {
  429. _THREAD_SAFE_METHOD_
  430. ERR_FAIL_COND_V(!valid, Vector2());
  431. KerningPairKey kpk;
  432. kpk.A = p_char;
  433. kpk.B = p_next;
  434. const Map<KerningPairKey, int>::Element *E = kerning_map.find(kpk);
  435. if (E) {
  436. return Vector2(-E->get() * (float(p_size) / float(base_size)), 0.f);
  437. } else {
  438. return Vector2();
  439. }
  440. }
  441. Vector2 BitmapFontDataAdvanced::draw_glyph(RID p_canvas, int p_size, const Vector2 &p_pos, uint32_t p_index, const Color &p_color) const {
  442. _THREAD_SAFE_METHOD_
  443. if (p_index == 0) {
  444. return Vector2();
  445. }
  446. ERR_FAIL_COND_V(!valid, Vector2());
  447. const Character *c = char_map.getptr(p_index);
  448. ERR_FAIL_COND_V(c == nullptr, Vector2());
  449. ERR_FAIL_COND_V(c->texture_idx < -1 || c->texture_idx >= textures.size(), Vector2());
  450. if (c->texture_idx != -1) {
  451. Point2i cpos = p_pos;
  452. cpos += c->align * (float(p_size) / float(base_size));
  453. cpos.y -= ascent * (float(p_size) / float(base_size));
  454. if (RenderingServer::get_singleton() != nullptr) {
  455. //if (distance_field_hint) { // Not implemented.
  456. // RenderingServer::get_singleton()->canvas_item_set_distance_field_mode(p_canvas, true);
  457. //}
  458. RenderingServer::get_singleton()->canvas_item_add_texture_rect_region(p_canvas, Rect2(cpos, c->rect.size * (float(p_size) / float(base_size))), textures[c->texture_idx]->get_rid(), c->rect, p_color, false, false);
  459. //if (distance_field_hint) {
  460. // RenderingServer::get_singleton()->canvas_item_set_distance_field_mode(p_canvas, false);
  461. //}
  462. }
  463. }
  464. return c->advance * (float(p_size) / float(base_size));
  465. }
  466. Vector2 BitmapFontDataAdvanced::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 {
  467. _THREAD_SAFE_METHOD_
  468. if (p_index == 0) {
  469. return Vector2();
  470. }
  471. ERR_FAIL_COND_V(!valid, Vector2());
  472. const Character *c = char_map.getptr(p_index);
  473. ERR_FAIL_COND_V(c == nullptr, Vector2());
  474. ERR_FAIL_COND_V(c->texture_idx < -1 || c->texture_idx >= textures.size(), Vector2());
  475. // Not supported, return advance for compatibility.
  476. return c->advance * (float(p_size) / float(base_size));
  477. }
  478. BitmapFontDataAdvanced::~BitmapFontDataAdvanced() {
  479. for (Map<float, hb_font_t *>::Element *E = cache.front(); E; E = E->next()) {
  480. hb_font_destroy(E->get());
  481. }
  482. }