font.cpp 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826
  1. /**************************************************************************/
  2. /* font.cpp */
  3. /**************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /**************************************************************************/
  8. /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
  9. /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
  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. }
  66. 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);
  67. ++chars_drawn;
  68. }
  69. if (has_outline()) {
  70. ofs = Vector2(0, 0);
  71. for (int i = 0; i < chars_drawn; i++) {
  72. ofs.x += draw_char(p_canvas_item, p_pos + ofs, p_text[i], p_text[i + 1], p_modulate, false);
  73. }
  74. }
  75. }
  76. void Font::update_changes() {
  77. emit_changed();
  78. }
  79. void Font::_bind_methods() {
  80. 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)));
  81. ClassDB::bind_method(D_METHOD("get_ascent"), &Font::get_ascent);
  82. ClassDB::bind_method(D_METHOD("get_descent"), &Font::get_descent);
  83. ClassDB::bind_method(D_METHOD("get_height"), &Font::get_height);
  84. ClassDB::bind_method(D_METHOD("is_distance_field_hint"), &Font::is_distance_field_hint);
  85. ClassDB::bind_method(D_METHOD("get_char_size", "char", "next"), &Font::get_char_size, DEFVAL(0));
  86. ClassDB::bind_method(D_METHOD("get_string_size", "string"), &Font::get_string_size);
  87. ClassDB::bind_method(D_METHOD("get_wordwrap_string_size", "string", "width"), &Font::get_wordwrap_string_size);
  88. ClassDB::bind_method(D_METHOD("has_outline"), &Font::has_outline);
  89. 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));
  90. ClassDB::bind_method(D_METHOD("get_char_texture", "char", "next", "outline"), &Font::get_char_texture, DEFVAL(0), DEFVAL(false));
  91. ClassDB::bind_method(D_METHOD("get_char_texture_size", "char", "next", "outline"), &Font::get_char_texture_size, DEFVAL(0), DEFVAL(false));
  92. ClassDB::bind_method(D_METHOD("get_char_tx_offset", "char", "next", "outline"), &Font::get_char_tx_offset, DEFVAL(0), DEFVAL(false));
  93. ClassDB::bind_method(D_METHOD("get_char_tx_size", "char", "next", "outline"), &Font::get_char_tx_size, DEFVAL(0), DEFVAL(false));
  94. ClassDB::bind_method(D_METHOD("get_char_tx_uv_rect", "char", "next", "outline"), &Font::get_char_tx_uv_rect, DEFVAL(0), DEFVAL(false));
  95. ClassDB::bind_method(D_METHOD("update_changes"), &Font::update_changes);
  96. ClassDB::bind_method(D_METHOD("get_char_contours", "char", "next"), &Font::get_char_contours, DEFVAL(0));
  97. BIND_ENUM_CONSTANT(CONTOUR_CURVE_TAG_ON);
  98. BIND_ENUM_CONSTANT(CONTOUR_CURVE_TAG_OFF_CONIC);
  99. BIND_ENUM_CONSTANT(CONTOUR_CURVE_TAG_OFF_CUBIC);
  100. }
  101. Font::Font() {
  102. }
  103. /////////////////////////////////////////////////////////////////
  104. void BitmapFont::_set_chars(const PoolVector<int> &p_chars) {
  105. int len = p_chars.size();
  106. //char 1 charsize 1 texture, 4 rect, 2 align, advance 1
  107. ERR_FAIL_COND(len % 9);
  108. if (!len) {
  109. return; //none to do
  110. }
  111. int chars = len / 9;
  112. PoolVector<int>::Read r = p_chars.read();
  113. for (int i = 0; i < chars; i++) {
  114. const int *data = &r[i * 9];
  115. add_char(data[0], data[1], Rect2(data[2], data[3], data[4], data[5]), Size2(data[6], data[7]), data[8]);
  116. }
  117. }
  118. PoolVector<int> BitmapFont::_get_chars() const {
  119. PoolVector<int> chars;
  120. const int32_t *key = nullptr;
  121. while ((key = char_map.next(key))) {
  122. const Character *c = char_map.getptr(*key);
  123. ERR_FAIL_COND_V(!c, PoolVector<int>());
  124. chars.push_back(*key);
  125. chars.push_back(c->texture_idx);
  126. chars.push_back(c->rect.position.x);
  127. chars.push_back(c->rect.position.y);
  128. chars.push_back(c->rect.size.x);
  129. chars.push_back(c->rect.size.y);
  130. chars.push_back(c->h_align);
  131. chars.push_back(c->v_align);
  132. chars.push_back(c->advance);
  133. }
  134. return chars;
  135. }
  136. void BitmapFont::_set_kernings(const PoolVector<int> &p_kernings) {
  137. int len = p_kernings.size();
  138. ERR_FAIL_COND(len % 3);
  139. if (!len) {
  140. return;
  141. }
  142. PoolVector<int>::Read r = p_kernings.read();
  143. for (int i = 0; i < len / 3; i++) {
  144. const int *data = &r[i * 3];
  145. add_kerning_pair(data[0], data[1], data[2]);
  146. }
  147. }
  148. PoolVector<int> BitmapFont::_get_kernings() const {
  149. PoolVector<int> kernings;
  150. for (Map<KerningPairKey, int>::Element *E = kerning_map.front(); E; E = E->next()) {
  151. kernings.push_back(E->key().A);
  152. kernings.push_back(E->key().B);
  153. kernings.push_back(E->get());
  154. }
  155. return kernings;
  156. }
  157. void BitmapFont::_set_textures(const Vector<Variant> &p_textures) {
  158. textures.clear();
  159. for (int i = 0; i < p_textures.size(); i++) {
  160. Ref<Texture> tex = p_textures[i];
  161. ERR_CONTINUE(!tex.is_valid());
  162. add_texture(tex);
  163. }
  164. }
  165. Vector<Variant> BitmapFont::_get_textures() const {
  166. Vector<Variant> rtextures;
  167. for (int i = 0; i < textures.size(); i++) {
  168. rtextures.push_back(textures[i].get_ref_ptr());
  169. }
  170. return rtextures;
  171. }
  172. Error BitmapFont::create_from_fnt(const String &p_file) {
  173. //fnt format used by angelcode bmfont
  174. //http://www.angelcode.com/products/bmfont/
  175. FileAccess *f = FileAccess::open(p_file, FileAccess::READ);
  176. ERR_FAIL_COND_V_MSG(!f, ERR_FILE_NOT_FOUND, "Can't open font: " + p_file + ".");
  177. clear();
  178. while (true) {
  179. String line = f->get_line();
  180. int delimiter = line.find(" ");
  181. String type = line.substr(0, delimiter);
  182. int pos = delimiter + 1;
  183. Map<String, String> keys;
  184. while (pos < line.size() && line[pos] == ' ') {
  185. pos++;
  186. }
  187. while (pos < line.size()) {
  188. int eq = line.find("=", pos);
  189. if (eq == -1) {
  190. break;
  191. }
  192. String key = line.substr(pos, eq - pos);
  193. int end = -1;
  194. String value;
  195. if (line[eq + 1] == '"') {
  196. end = line.find("\"", eq + 2);
  197. if (end == -1) {
  198. break;
  199. }
  200. value = line.substr(eq + 2, end - 1 - eq - 1);
  201. pos = end + 1;
  202. } else {
  203. end = line.find(" ", eq + 1);
  204. if (end == -1) {
  205. end = line.size();
  206. }
  207. value = line.substr(eq + 1, end - eq);
  208. pos = end;
  209. }
  210. while (pos < line.size() && line[pos] == ' ') {
  211. pos++;
  212. }
  213. keys[key] = value;
  214. }
  215. if (type == "info") {
  216. if (keys.has("face")) {
  217. set_name(keys["face"]);
  218. }
  219. /*
  220. if (keys.has("size"))
  221. font->set_height(keys["size"].to_int());
  222. */
  223. } else if (type == "common") {
  224. if (keys.has("lineHeight")) {
  225. set_height(keys["lineHeight"].to_int());
  226. }
  227. if (keys.has("base")) {
  228. set_ascent(keys["base"].to_int());
  229. }
  230. } else if (type == "page") {
  231. if (keys.has("file")) {
  232. String base_dir = p_file.get_base_dir();
  233. String file = base_dir.plus_file(keys["file"]);
  234. Ref<Texture> tex = ResourceLoader::load(file);
  235. if (tex.is_null()) {
  236. ERR_PRINT("Can't load font texture!");
  237. } else {
  238. add_texture(tex);
  239. }
  240. }
  241. } else if (type == "char") {
  242. int32_t idx = 0;
  243. if (keys.has("id")) {
  244. idx = keys["id"].to_int();
  245. }
  246. Rect2 rect;
  247. if (keys.has("x")) {
  248. rect.position.x = keys["x"].to_int();
  249. }
  250. if (keys.has("y")) {
  251. rect.position.y = keys["y"].to_int();
  252. }
  253. if (keys.has("width")) {
  254. rect.size.width = keys["width"].to_int();
  255. }
  256. if (keys.has("height")) {
  257. rect.size.height = keys["height"].to_int();
  258. }
  259. Point2 ofs;
  260. if (keys.has("xoffset")) {
  261. ofs.x = keys["xoffset"].to_int();
  262. }
  263. if (keys.has("yoffset")) {
  264. ofs.y = keys["yoffset"].to_int();
  265. }
  266. int texture = 0;
  267. if (keys.has("page")) {
  268. texture = keys["page"].to_int();
  269. }
  270. int advance = -1;
  271. if (keys.has("xadvance")) {
  272. advance = keys["xadvance"].to_int();
  273. }
  274. add_char(idx, texture, rect, ofs, advance);
  275. } else if (type == "kerning") {
  276. int32_t first = 0, second = 0;
  277. int k = 0;
  278. if (keys.has("first")) {
  279. first = keys["first"].to_int();
  280. }
  281. if (keys.has("second")) {
  282. second = keys["second"].to_int();
  283. }
  284. if (keys.has("amount")) {
  285. k = keys["amount"].to_int();
  286. }
  287. add_kerning_pair(first, second, -k);
  288. }
  289. if (f->eof_reached()) {
  290. break;
  291. }
  292. }
  293. memdelete(f);
  294. return OK;
  295. }
  296. void BitmapFont::set_height(float p_height) {
  297. height = p_height;
  298. }
  299. float BitmapFont::get_height() const {
  300. return height;
  301. }
  302. void BitmapFont::set_ascent(float p_ascent) {
  303. ascent = p_ascent;
  304. }
  305. float BitmapFont::get_ascent() const {
  306. return ascent;
  307. }
  308. float BitmapFont::get_descent() const {
  309. return height - ascent;
  310. }
  311. void BitmapFont::add_texture(const Ref<Texture> &p_texture) {
  312. ERR_FAIL_COND_MSG(p_texture.is_null(), "It's not a reference to a valid Texture object.");
  313. textures.push_back(p_texture);
  314. }
  315. int BitmapFont::get_texture_count() const {
  316. return textures.size();
  317. };
  318. Ref<Texture> BitmapFont::get_texture(int p_idx) const {
  319. ERR_FAIL_INDEX_V(p_idx, textures.size(), Ref<Texture>());
  320. return textures[p_idx];
  321. };
  322. int BitmapFont::get_character_count() const {
  323. return char_map.size();
  324. };
  325. Vector<int32_t> BitmapFont::get_char_keys() const {
  326. Vector<int32_t> chars;
  327. chars.resize(char_map.size());
  328. const int32_t *ct = nullptr;
  329. int count = 0;
  330. while ((ct = char_map.next(ct))) {
  331. chars.write[count++] = *ct;
  332. };
  333. return chars;
  334. };
  335. BitmapFont::Character BitmapFont::get_character(int32_t p_char) const {
  336. if (!char_map.has(p_char)) {
  337. ERR_FAIL_V(Character());
  338. };
  339. return char_map[p_char];
  340. };
  341. void BitmapFont::add_char(int32_t p_char, int p_texture_idx, const Rect2 &p_rect, const Size2 &p_align, float p_advance) {
  342. if (p_advance < 0) {
  343. p_advance = p_rect.size.width;
  344. }
  345. Character c;
  346. c.rect = p_rect;
  347. c.texture_idx = p_texture_idx;
  348. c.v_align = p_align.y;
  349. c.advance = p_advance;
  350. c.h_align = p_align.x;
  351. char_map[p_char] = c;
  352. }
  353. void BitmapFont::add_kerning_pair(int32_t p_A, int32_t p_B, int p_kerning) {
  354. KerningPairKey kpk;
  355. kpk.A = p_A;
  356. kpk.B = p_B;
  357. if (p_kerning == 0 && kerning_map.has(kpk)) {
  358. kerning_map.erase(kpk);
  359. } else {
  360. kerning_map[kpk] = p_kerning;
  361. }
  362. }
  363. Vector<BitmapFont::KerningPairKey> BitmapFont::get_kerning_pair_keys() const {
  364. Vector<BitmapFont::KerningPairKey> ret;
  365. ret.resize(kerning_map.size());
  366. int i = 0;
  367. for (Map<KerningPairKey, int>::Element *E = kerning_map.front(); E; E = E->next()) {
  368. ret.write[i++] = E->key();
  369. }
  370. return ret;
  371. }
  372. int BitmapFont::get_kerning_pair(int32_t p_A, int32_t p_B) const {
  373. KerningPairKey kpk;
  374. kpk.A = p_A;
  375. kpk.B = p_B;
  376. const Map<KerningPairKey, int>::Element *E = kerning_map.find(kpk);
  377. if (E) {
  378. return E->get();
  379. }
  380. return 0;
  381. }
  382. void BitmapFont::set_distance_field_hint(bool p_distance_field) {
  383. distance_field_hint = p_distance_field;
  384. emit_changed();
  385. }
  386. bool BitmapFont::is_distance_field_hint() const {
  387. return distance_field_hint;
  388. }
  389. void BitmapFont::clear() {
  390. height = 1;
  391. ascent = 0;
  392. char_map.clear();
  393. textures.clear();
  394. kerning_map.clear();
  395. distance_field_hint = false;
  396. }
  397. Size2 Font::get_string_size(const String &p_string) const {
  398. float w = 0;
  399. int l = p_string.length();
  400. if (l == 0) {
  401. return Size2(0, get_height());
  402. }
  403. const CharType *sptr = &p_string[0];
  404. for (int i = 0; i < l; i++) {
  405. w += get_char_size(sptr[i], sptr[i + 1]).width;
  406. }
  407. return Size2(w, get_height());
  408. }
  409. Size2 Font::get_wordwrap_string_size(const String &p_string, float p_width) const {
  410. ERR_FAIL_COND_V(p_width <= 0, Vector2(0, get_height()));
  411. int l = p_string.length();
  412. if (l == 0) {
  413. return Size2(p_width, get_height());
  414. }
  415. float line_w = 0;
  416. float h = 0;
  417. float space_w = get_char_size(' ').width;
  418. Vector<String> lines = p_string.split("\n");
  419. for (int i = 0; i < lines.size(); i++) {
  420. h += get_height();
  421. String t = lines[i];
  422. line_w = 0;
  423. Vector<String> words = t.split(" ");
  424. for (int j = 0; j < words.size(); j++) {
  425. line_w += get_string_size(words[j]).x;
  426. if (line_w > p_width) {
  427. h += get_height();
  428. line_w = get_string_size(words[j]).x;
  429. } else {
  430. line_w += space_w;
  431. }
  432. }
  433. }
  434. return Size2(p_width, h);
  435. }
  436. void BitmapFont::set_fallback(const Ref<BitmapFont> &p_fallback) {
  437. for (Ref<BitmapFont> fallback_child = p_fallback; fallback_child != nullptr; fallback_child = fallback_child->get_fallback()) {
  438. ERR_FAIL_COND_MSG(fallback_child == this, "Can't set as fallback one of its parents to prevent crashes due to recursive loop.");
  439. }
  440. fallback = p_fallback;
  441. }
  442. Ref<BitmapFont> BitmapFont::get_fallback() const {
  443. return fallback;
  444. }
  445. RID BitmapFont::get_char_texture(CharType p_char, CharType p_next, bool p_outline) const {
  446. int32_t ch = p_char;
  447. if (((p_char & 0xfffffc00) == 0xd800) && (p_next & 0xfffffc00) == 0xdc00) { // decode surrogate pair.
  448. ch = (p_char << 10UL) + p_next - ((0xd800 << 10UL) + 0xdc00 - 0x10000);
  449. }
  450. if ((p_char & 0xfffffc00) == 0xdc00) { // skip trail surrogate.
  451. return RID();
  452. }
  453. const Character *c = char_map.getptr(ch);
  454. if (!c) {
  455. if (fallback.is_valid()) {
  456. return fallback->get_char_texture(p_char, p_next, p_outline);
  457. }
  458. return RID();
  459. }
  460. ERR_FAIL_COND_V(c->texture_idx < -1 || c->texture_idx >= textures.size(), RID());
  461. if (!p_outline && c->texture_idx != -1) {
  462. return textures[c->texture_idx]->get_rid();
  463. } else {
  464. return RID();
  465. }
  466. }
  467. Size2 BitmapFont::get_char_texture_size(CharType p_char, CharType p_next, bool p_outline) const {
  468. int32_t ch = p_char;
  469. if (((p_char & 0xfffffc00) == 0xd800) && (p_next & 0xfffffc00) == 0xdc00) { // decode surrogate pair.
  470. ch = (p_char << 10UL) + p_next - ((0xd800 << 10UL) + 0xdc00 - 0x10000);
  471. }
  472. if ((p_char & 0xfffffc00) == 0xdc00) { // skip trail surrogate.
  473. return Size2();
  474. }
  475. const Character *c = char_map.getptr(ch);
  476. if (!c) {
  477. if (fallback.is_valid()) {
  478. return fallback->get_char_texture_size(p_char, p_next, p_outline);
  479. }
  480. return Size2();
  481. }
  482. ERR_FAIL_COND_V(c->texture_idx < -1 || c->texture_idx >= textures.size(), Size2());
  483. if (!p_outline && c->texture_idx != -1) {
  484. return textures[c->texture_idx]->get_size();
  485. } else {
  486. return Size2();
  487. }
  488. }
  489. Vector2 BitmapFont::get_char_tx_offset(CharType p_char, CharType p_next, bool p_outline) const {
  490. int32_t ch = p_char;
  491. if (((p_char & 0xfffffc00) == 0xd800) && (p_next & 0xfffffc00) == 0xdc00) { // decode surrogate pair.
  492. ch = (p_char << 10UL) + p_next - ((0xd800 << 10UL) + 0xdc00 - 0x10000);
  493. }
  494. if ((p_char & 0xfffffc00) == 0xdc00) { // skip trail surrogate.
  495. return Vector2();
  496. }
  497. const Character *c = char_map.getptr(ch);
  498. if (!c) {
  499. if (fallback.is_valid()) {
  500. return fallback->get_char_tx_offset(p_char, p_next, p_outline);
  501. }
  502. return Vector2();
  503. }
  504. ERR_FAIL_COND_V(c->texture_idx < -1 || c->texture_idx >= textures.size(), Vector2());
  505. if (!p_outline && c->texture_idx != -1) {
  506. Point2 cpos;
  507. cpos.x += c->h_align;
  508. cpos.y -= ascent;
  509. cpos.y += c->v_align;
  510. return cpos;
  511. } else {
  512. return Vector2();
  513. }
  514. }
  515. Size2 BitmapFont::get_char_tx_size(CharType p_char, CharType p_next, bool p_outline) const {
  516. int32_t ch = p_char;
  517. if (((p_char & 0xfffffc00) == 0xd800) && (p_next & 0xfffffc00) == 0xdc00) { // decode surrogate pair.
  518. ch = (p_char << 10UL) + p_next - ((0xd800 << 10UL) + 0xdc00 - 0x10000);
  519. }
  520. if ((p_char & 0xfffffc00) == 0xdc00) { // skip trail surrogate.
  521. return Size2();
  522. }
  523. const Character *c = char_map.getptr(ch);
  524. if (!c) {
  525. if (fallback.is_valid()) {
  526. return fallback->get_char_tx_size(p_char, p_next, p_outline);
  527. }
  528. return Size2();
  529. }
  530. ERR_FAIL_COND_V(c->texture_idx < -1 || c->texture_idx >= textures.size(), Size2());
  531. if (!p_outline && c->texture_idx != -1) {
  532. return c->rect.size;
  533. } else {
  534. return Size2();
  535. }
  536. }
  537. Rect2 BitmapFont::get_char_tx_uv_rect(CharType p_char, CharType p_next, bool p_outline) const {
  538. int32_t ch = p_char;
  539. if (((p_char & 0xfffffc00) == 0xd800) && (p_next & 0xfffffc00) == 0xdc00) { // decode surrogate pair.
  540. ch = (p_char << 10UL) + p_next - ((0xd800 << 10UL) + 0xdc00 - 0x10000);
  541. }
  542. if ((p_char & 0xfffffc00) == 0xdc00) { // skip trail surrogate.
  543. return Rect2();
  544. }
  545. const Character *c = char_map.getptr(ch);
  546. if (!c) {
  547. if (fallback.is_valid()) {
  548. return fallback->get_char_tx_uv_rect(p_char, p_next, p_outline);
  549. }
  550. return Rect2();
  551. }
  552. ERR_FAIL_COND_V(c->texture_idx < -1 || c->texture_idx >= textures.size(), Rect2());
  553. if (!p_outline && c->texture_idx != -1) {
  554. return c->rect;
  555. } else {
  556. return Rect2();
  557. }
  558. }
  559. 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 {
  560. int32_t ch = p_char;
  561. if (((p_char & 0xfffffc00) == 0xd800) && (p_next & 0xfffffc00) == 0xdc00) { // decode surrogate pair.
  562. ch = (p_char << 10UL) + p_next - ((0xd800 << 10UL) + 0xdc00 - 0x10000);
  563. }
  564. if ((p_char & 0xfffffc00) == 0xdc00) { // skip trail surrogate.
  565. return 0;
  566. }
  567. const Character *c = char_map.getptr(ch);
  568. if (!c) {
  569. if (fallback.is_valid()) {
  570. return fallback->draw_char(p_canvas_item, p_pos, p_char, p_next, p_modulate, p_outline);
  571. }
  572. return 0;
  573. }
  574. ERR_FAIL_COND_V(c->texture_idx < -1 || c->texture_idx >= textures.size(), 0);
  575. if (!p_outline && c->texture_idx != -1) {
  576. Point2 cpos = p_pos;
  577. cpos.x += c->h_align;
  578. cpos.y -= ascent;
  579. cpos.y += c->v_align;
  580. 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);
  581. }
  582. return get_char_size(p_char, p_next).width;
  583. }
  584. Size2 BitmapFont::get_char_size(CharType p_char, CharType p_next) const {
  585. int32_t ch = p_char;
  586. bool skip_kerning = false;
  587. if (((p_char & 0xfffffc00) == 0xd800) && (p_next & 0xfffffc00) == 0xdc00) { // decode surrogate pair.
  588. ch = (p_char << 10UL) + p_next - ((0xd800 << 10UL) + 0xdc00 - 0x10000);
  589. skip_kerning = true;
  590. }
  591. if ((p_char & 0xfffffc00) == 0xdc00) { // skip trail surrogate.
  592. return Size2();
  593. }
  594. const Character *c = char_map.getptr(ch);
  595. if (!c) {
  596. if (fallback.is_valid()) {
  597. return fallback->get_char_size(p_char, p_next);
  598. }
  599. return Size2();
  600. }
  601. Size2 ret(c->advance, c->rect.size.y);
  602. if (!skip_kerning) {
  603. if (p_next) {
  604. KerningPairKey kpk;
  605. kpk.A = p_char;
  606. kpk.B = p_next;
  607. const Map<KerningPairKey, int>::Element *E = kerning_map.find(kpk);
  608. if (E) {
  609. ret.width -= E->get();
  610. }
  611. }
  612. }
  613. return ret;
  614. }
  615. void BitmapFont::_bind_methods() {
  616. ClassDB::bind_method(D_METHOD("create_from_fnt", "path"), &BitmapFont::create_from_fnt);
  617. ClassDB::bind_method(D_METHOD("set_height", "px"), &BitmapFont::set_height);
  618. ClassDB::bind_method(D_METHOD("set_ascent", "px"), &BitmapFont::set_ascent);
  619. ClassDB::bind_method(D_METHOD("add_kerning_pair", "char_a", "char_b", "kerning"), &BitmapFont::add_kerning_pair);
  620. ClassDB::bind_method(D_METHOD("get_kerning_pair", "char_a", "char_b"), &BitmapFont::get_kerning_pair);
  621. ClassDB::bind_method(D_METHOD("add_texture", "texture"), &BitmapFont::add_texture);
  622. ClassDB::bind_method(D_METHOD("add_char", "character", "texture", "rect", "align", "advance"), &BitmapFont::add_char, DEFVAL(Point2()), DEFVAL(-1));
  623. ClassDB::bind_method(D_METHOD("get_texture_count"), &BitmapFont::get_texture_count);
  624. ClassDB::bind_method(D_METHOD("get_texture", "idx"), &BitmapFont::get_texture);
  625. ClassDB::bind_method(D_METHOD("set_distance_field_hint", "enable"), &BitmapFont::set_distance_field_hint);
  626. ClassDB::bind_method(D_METHOD("clear"), &BitmapFont::clear);
  627. ClassDB::bind_method(D_METHOD("_set_chars"), &BitmapFont::_set_chars);
  628. ClassDB::bind_method(D_METHOD("_get_chars"), &BitmapFont::_get_chars);
  629. ClassDB::bind_method(D_METHOD("_set_kernings"), &BitmapFont::_set_kernings);
  630. ClassDB::bind_method(D_METHOD("_get_kernings"), &BitmapFont::_get_kernings);
  631. ClassDB::bind_method(D_METHOD("_set_textures"), &BitmapFont::_set_textures);
  632. ClassDB::bind_method(D_METHOD("_get_textures"), &BitmapFont::_get_textures);
  633. ClassDB::bind_method(D_METHOD("set_fallback", "fallback"), &BitmapFont::set_fallback);
  634. ClassDB::bind_method(D_METHOD("get_fallback"), &BitmapFont::get_fallback);
  635. ADD_PROPERTY(PropertyInfo(Variant::ARRAY, "textures", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NOEDITOR | PROPERTY_USAGE_INTERNAL), "_set_textures", "_get_textures");
  636. ADD_PROPERTY(PropertyInfo(Variant::POOL_INT_ARRAY, "chars", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NOEDITOR | PROPERTY_USAGE_INTERNAL), "_set_chars", "_get_chars");
  637. ADD_PROPERTY(PropertyInfo(Variant::POOL_INT_ARRAY, "kernings", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NOEDITOR | PROPERTY_USAGE_INTERNAL), "_set_kernings", "_get_kernings");
  638. ADD_PROPERTY(PropertyInfo(Variant::REAL, "height", PROPERTY_HINT_RANGE, "1,1024,1"), "set_height", "get_height");
  639. ADD_PROPERTY(PropertyInfo(Variant::REAL, "ascent", PROPERTY_HINT_RANGE, "0,1024,1"), "set_ascent", "get_ascent");
  640. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "distance_field"), "set_distance_field_hint", "is_distance_field_hint");
  641. ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "fallback", PROPERTY_HINT_RESOURCE_TYPE, "BitmapFont"), "set_fallback", "get_fallback");
  642. }
  643. BitmapFont::BitmapFont() {
  644. clear();
  645. }
  646. BitmapFont::~BitmapFont() {
  647. clear();
  648. }
  649. ////////////
  650. RES ResourceFormatLoaderBMFont::load(const String &p_path, const String &p_original_path, Error *r_error) {
  651. if (r_error) {
  652. *r_error = ERR_FILE_CANT_OPEN;
  653. }
  654. Ref<BitmapFont> font;
  655. font.instance();
  656. Error err = font->create_from_fnt(p_path);
  657. if (err) {
  658. if (r_error) {
  659. *r_error = err;
  660. }
  661. return RES();
  662. }
  663. return font;
  664. }
  665. void ResourceFormatLoaderBMFont::get_recognized_extensions(List<String> *p_extensions) const {
  666. p_extensions->push_back("fnt");
  667. }
  668. bool ResourceFormatLoaderBMFont::handles_type(const String &p_type) const {
  669. return (p_type == "BitmapFont");
  670. }
  671. String ResourceFormatLoaderBMFont::get_resource_type(const String &p_path) const {
  672. String el = p_path.get_extension().to_lower();
  673. if (el == "fnt") {
  674. return "BitmapFont";
  675. }
  676. return "";
  677. }