bitmap_font_fb.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352
  1. /*************************************************************************/
  2. /* bitmap_font_fb.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_fb.h"
  31. Error BitmapFontDataFallback::load_from_file(const String &p_filename, int p_base_size) {
  32. _THREAD_SAFE_METHOD_
  33. //fnt format used by angelcode bmfont
  34. //http://www.angelcode.com/products/bmfont/
  35. FileAccess *f = FileAccess::open(p_filename, FileAccess::READ);
  36. ERR_FAIL_COND_V_MSG(!f, ERR_FILE_NOT_FOUND, "Can't open font: " + p_filename + ".");
  37. while (true) {
  38. String line = f->get_line();
  39. int delimiter = line.find(" ");
  40. String type = line.substr(0, delimiter);
  41. int pos = delimiter + 1;
  42. Map<String, String> keys;
  43. while (pos < line.size() && line[pos] == ' ') {
  44. pos++;
  45. }
  46. while (pos < line.size()) {
  47. int eq = line.find("=", pos);
  48. if (eq == -1) {
  49. break;
  50. }
  51. String key = line.substr(pos, eq - pos);
  52. int end = -1;
  53. String value;
  54. if (line[eq + 1] == '"') {
  55. end = line.find("\"", eq + 2);
  56. if (end == -1) {
  57. break;
  58. }
  59. value = line.substr(eq + 2, end - 1 - eq - 1);
  60. pos = end + 1;
  61. } else {
  62. end = line.find(" ", eq + 1);
  63. if (end == -1) {
  64. end = line.size();
  65. }
  66. value = line.substr(eq + 1, end - eq);
  67. pos = end;
  68. }
  69. while (pos < line.size() && line[pos] == ' ') {
  70. pos++;
  71. }
  72. keys[key] = value;
  73. }
  74. if (type == "info") {
  75. if (keys.has("size")) {
  76. base_size = keys["size"].to_int();
  77. }
  78. } else if (type == "common") {
  79. if (keys.has("lineHeight")) {
  80. height = keys["lineHeight"].to_int();
  81. }
  82. if (keys.has("base")) {
  83. ascent = keys["base"].to_int();
  84. }
  85. } else if (type == "page") {
  86. if (keys.has("file")) {
  87. String base_dir = p_filename.get_base_dir();
  88. String file = base_dir.plus_file(keys["file"]);
  89. if (RenderingServer::get_singleton() != nullptr) {
  90. Ref<Texture2D> tex = ResourceLoader::load(file);
  91. if (tex.is_null()) {
  92. ERR_PRINT("Can't load font texture!");
  93. } else {
  94. ERR_FAIL_COND_V_MSG(tex.is_null(), ERR_FILE_CANT_READ, "It's not a reference to a valid Texture object.");
  95. textures.push_back(tex);
  96. }
  97. }
  98. }
  99. } else if (type == "char") {
  100. Character c;
  101. char32_t idx = 0;
  102. if (keys.has("id")) {
  103. idx = keys["id"].to_int();
  104. }
  105. if (keys.has("x")) {
  106. c.rect.position.x = keys["x"].to_int();
  107. }
  108. if (keys.has("y")) {
  109. c.rect.position.y = keys["y"].to_int();
  110. }
  111. if (keys.has("width")) {
  112. c.rect.size.width = keys["width"].to_int();
  113. }
  114. if (keys.has("height")) {
  115. c.rect.size.height = keys["height"].to_int();
  116. }
  117. if (keys.has("xoffset")) {
  118. c.align.x = keys["xoffset"].to_int();
  119. }
  120. if (keys.has("yoffset")) {
  121. c.align.y = keys["yoffset"].to_int();
  122. }
  123. if (keys.has("page")) {
  124. c.texture_idx = keys["page"].to_int();
  125. }
  126. if (keys.has("xadvance")) {
  127. c.advance.x = keys["xadvance"].to_int();
  128. }
  129. if (keys.has("yadvance")) {
  130. c.advance.y = keys["yadvance"].to_int();
  131. }
  132. if (c.advance.x < 0) {
  133. c.advance.x = c.rect.size.width + 1;
  134. }
  135. if (c.advance.y < 0) {
  136. c.advance.y = c.rect.size.height + 1;
  137. }
  138. char_map[idx] = c;
  139. } else if (type == "kerning") {
  140. KerningPairKey kpk;
  141. float k = 0.0;
  142. if (keys.has("first")) {
  143. kpk.A = keys["first"].to_int();
  144. }
  145. if (keys.has("second")) {
  146. kpk.B = keys["second"].to_int();
  147. }
  148. if (keys.has("amount")) {
  149. k = keys["amount"].to_int();
  150. }
  151. kerning_map[kpk] = k;
  152. }
  153. if (f->eof_reached()) {
  154. break;
  155. }
  156. }
  157. if (base_size == 0) {
  158. base_size = height;
  159. }
  160. valid = true;
  161. memdelete(f);
  162. return OK;
  163. }
  164. Error BitmapFontDataFallback::bitmap_new(float p_height, float p_ascent, int p_base_size) {
  165. height = p_height;
  166. ascent = p_ascent;
  167. base_size = p_base_size;
  168. if (base_size == 0) {
  169. base_size = height;
  170. }
  171. char_map.clear();
  172. textures.clear();
  173. kerning_map.clear();
  174. valid = true;
  175. return OK;
  176. }
  177. void BitmapFontDataFallback::bitmap_add_texture(const Ref<Texture> &p_texture) {
  178. ERR_FAIL_COND(!valid);
  179. ERR_FAIL_COND_MSG(p_texture.is_null(), "It's not a reference to a valid Texture object.");
  180. textures.push_back(p_texture);
  181. }
  182. void BitmapFontDataFallback::bitmap_add_char(char32_t p_char, int p_texture_idx, const Rect2 &p_rect, const Size2 &p_align, float p_advance) {
  183. ERR_FAIL_COND(!valid);
  184. Character chr;
  185. chr.rect = p_rect;
  186. chr.texture_idx = p_texture_idx;
  187. if (p_advance < 0) {
  188. chr.advance.x = chr.rect.size.x;
  189. } else {
  190. chr.advance.x = p_advance;
  191. }
  192. chr.align = p_align;
  193. char_map[p_char] = chr;
  194. }
  195. void BitmapFontDataFallback::bitmap_add_kerning_pair(char32_t p_A, char32_t p_B, int p_kerning) {
  196. ERR_FAIL_COND(!valid);
  197. KerningPairKey kpk;
  198. kpk.A = p_A;
  199. kpk.B = p_B;
  200. if (p_kerning == 0 && kerning_map.has(kpk)) {
  201. kerning_map.erase(kpk);
  202. } else {
  203. kerning_map[kpk] = p_kerning;
  204. }
  205. }
  206. float BitmapFontDataFallback::get_height(int p_size) const {
  207. ERR_FAIL_COND_V(!valid, 0.f);
  208. return height * (float(p_size) / float(base_size));
  209. }
  210. float BitmapFontDataFallback::get_ascent(int p_size) const {
  211. ERR_FAIL_COND_V(!valid, 0.f);
  212. return ascent * (float(p_size) / float(base_size));
  213. }
  214. float BitmapFontDataFallback::get_descent(int p_size) const {
  215. ERR_FAIL_COND_V(!valid, 0.f);
  216. return (height - ascent) * (float(p_size) / float(base_size));
  217. }
  218. float BitmapFontDataFallback::get_underline_position(int p_size) const {
  219. ERR_FAIL_COND_V(!valid, 0.f);
  220. return 2 * (float(p_size) / float(base_size));
  221. }
  222. float BitmapFontDataFallback::get_underline_thickness(int p_size) const {
  223. ERR_FAIL_COND_V(!valid, 0.f);
  224. return 1 * (float(p_size) / float(base_size));
  225. }
  226. void BitmapFontDataFallback::set_distance_field_hint(bool p_distance_field) {
  227. distance_field_hint = p_distance_field;
  228. }
  229. bool BitmapFontDataFallback::get_distance_field_hint() const {
  230. return distance_field_hint;
  231. }
  232. float BitmapFontDataFallback::get_base_size() const {
  233. return base_size;
  234. }
  235. bool BitmapFontDataFallback::has_char(char32_t p_char) const {
  236. _THREAD_SAFE_METHOD_
  237. ERR_FAIL_COND_V(!valid, false);
  238. return char_map.has(p_char);
  239. }
  240. String BitmapFontDataFallback::get_supported_chars() const {
  241. _THREAD_SAFE_METHOD_
  242. ERR_FAIL_COND_V(!valid, String());
  243. String chars;
  244. const char32_t *k = nullptr;
  245. while ((k = char_map.next(k))) {
  246. chars += char32_t(*k);
  247. }
  248. return chars;
  249. }
  250. Vector2 BitmapFontDataFallback::get_advance(char32_t p_char, int p_size) const {
  251. _THREAD_SAFE_METHOD_
  252. ERR_FAIL_COND_V(!valid, Vector2());
  253. const Character *c = char_map.getptr(p_char);
  254. ERR_FAIL_COND_V(c == nullptr, Vector2());
  255. return c->advance * (float(p_size) / float(base_size));
  256. }
  257. Vector2 BitmapFontDataFallback::get_kerning(char32_t p_char, char32_t p_next, int p_size) const {
  258. _THREAD_SAFE_METHOD_
  259. ERR_FAIL_COND_V(!valid, Vector2());
  260. KerningPairKey kpk;
  261. kpk.A = p_char;
  262. kpk.B = p_next;
  263. const Map<KerningPairKey, int>::Element *E = kerning_map.find(kpk);
  264. if (E) {
  265. return Vector2(-E->get() * (float(p_size) / float(base_size)), 0);
  266. } else {
  267. return Vector2();
  268. }
  269. }
  270. Vector2 BitmapFontDataFallback::draw_glyph(RID p_canvas, int p_size, const Vector2 &p_pos, uint32_t p_index, const Color &p_color) const {
  271. _THREAD_SAFE_METHOD_
  272. if (p_index == 0) {
  273. return Vector2();
  274. }
  275. ERR_FAIL_COND_V(!valid, Vector2());
  276. const Character *c = char_map.getptr(p_index);
  277. ERR_FAIL_COND_V(c == nullptr, Vector2());
  278. ERR_FAIL_COND_V(c->texture_idx < -1 || c->texture_idx >= textures.size(), Vector2());
  279. if (c->texture_idx != -1) {
  280. Point2i cpos = p_pos;
  281. cpos += (c->align + Vector2(0, -ascent)) * (float(p_size) / float(base_size));
  282. Size2i csize = c->rect.size * (float(p_size) / float(base_size));
  283. if (RenderingServer::get_singleton() != nullptr) {
  284. //if (distance_field_hint) { // Not implemented.
  285. // RenderingServer::get_singleton()->canvas_item_set_distance_field_mode(p_canvas, true);
  286. //}
  287. RenderingServer::get_singleton()->canvas_item_add_texture_rect_region(p_canvas, Rect2(cpos, csize), textures[c->texture_idx]->get_rid(), c->rect, p_color, false, false);
  288. //if (distance_field_hint) {
  289. // RenderingServer::get_singleton()->canvas_item_set_distance_field_mode(p_canvas, false);
  290. //}
  291. }
  292. }
  293. return c->advance * (float(p_size) / float(base_size));
  294. }
  295. Vector2 BitmapFontDataFallback::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 {
  296. _THREAD_SAFE_METHOD_
  297. if (p_index == 0) {
  298. return Vector2();
  299. }
  300. ERR_FAIL_COND_V(!valid, Vector2());
  301. const Character *c = char_map.getptr(p_index);
  302. ERR_FAIL_COND_V(c == nullptr, Vector2());
  303. ERR_FAIL_COND_V(c->texture_idx < -1 || c->texture_idx >= textures.size(), Vector2());
  304. // Not supported, return advance for compatibility.
  305. return c->advance * (float(p_size) / float(base_size));
  306. }