bitmap_font_fb.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357
  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-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 "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;
  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::load_from_memory(const uint8_t *p_data, size_t p_size, int p_base_size) {
  165. _THREAD_SAFE_METHOD_
  166. ERR_FAIL_COND_V(p_data == nullptr, ERR_CANT_CREATE);
  167. ERR_FAIL_COND_V(p_size != sizeof(TextServer::BitmapFontData), ERR_CANT_CREATE);
  168. const TextServer::BitmapFontData *data = (const TextServer::BitmapFontData *)p_data;
  169. if (RenderingServer::get_singleton() != nullptr) {
  170. Ref<Image> image = memnew(Image(data->img));
  171. Ref<ImageTexture> tex = memnew(ImageTexture);
  172. tex->create_from_image(image);
  173. textures.push_back(tex);
  174. }
  175. for (int i = 0; i < data->charcount; i++) {
  176. const int *c = &data->char_rects[i * 8];
  177. Character chr;
  178. chr.rect.position.x = c[1];
  179. chr.rect.position.y = c[2];
  180. chr.rect.size.x = c[3];
  181. chr.rect.size.y = c[4];
  182. chr.texture_idx = 0;
  183. if (c[7] < 0) {
  184. chr.advance.x = c[3];
  185. } else {
  186. chr.advance.x = c[7];
  187. }
  188. chr.align = Vector2(c[6], c[5]);
  189. char_map[c[0]] = chr;
  190. }
  191. for (int i = 0; i < data->kerning_count; i++) {
  192. KerningPairKey kpk;
  193. kpk.A = data->kernings[i * 3 + 0];
  194. kpk.B = data->kernings[i * 3 + 1];
  195. if (data->kernings[i * 3 + 2] == 0 && kerning_map.has(kpk)) {
  196. kerning_map.erase(kpk);
  197. } else {
  198. kerning_map[kpk] = data->kernings[i * 3 + 2];
  199. }
  200. }
  201. height = data->height;
  202. ascent = data->ascent;
  203. base_size = p_base_size;
  204. if (base_size == 0) {
  205. base_size = height;
  206. }
  207. valid = true;
  208. return OK;
  209. }
  210. float BitmapFontDataFallback::get_height(int p_size) const {
  211. ERR_FAIL_COND_V(!valid, 0.f);
  212. return height * (float(p_size) / float(base_size));
  213. }
  214. float BitmapFontDataFallback::get_ascent(int p_size) const {
  215. ERR_FAIL_COND_V(!valid, 0.f);
  216. return ascent * (float(p_size) / float(base_size));
  217. }
  218. float BitmapFontDataFallback::get_descent(int p_size) const {
  219. ERR_FAIL_COND_V(!valid, 0.f);
  220. return (height - ascent) * (float(p_size) / float(base_size));
  221. }
  222. float BitmapFontDataFallback::get_underline_position(int p_size) const {
  223. ERR_FAIL_COND_V(!valid, 0.f);
  224. return 2 * (float(p_size) / float(base_size));
  225. }
  226. float BitmapFontDataFallback::get_underline_thickness(int p_size) const {
  227. ERR_FAIL_COND_V(!valid, 0.f);
  228. return 1 * (float(p_size) / float(base_size));
  229. }
  230. void BitmapFontDataFallback::set_distance_field_hint(bool p_distance_field) {
  231. distance_field_hint = p_distance_field;
  232. }
  233. bool BitmapFontDataFallback::get_distance_field_hint() const {
  234. return distance_field_hint;
  235. }
  236. float BitmapFontDataFallback::get_base_size() const {
  237. return base_size;
  238. }
  239. bool BitmapFontDataFallback::has_char(char32_t p_char) const {
  240. _THREAD_SAFE_METHOD_
  241. ERR_FAIL_COND_V(!valid, false);
  242. return char_map.has(p_char);
  243. }
  244. String BitmapFontDataFallback::get_supported_chars() const {
  245. _THREAD_SAFE_METHOD_
  246. ERR_FAIL_COND_V(!valid, String());
  247. String chars;
  248. const char32_t *k = nullptr;
  249. while ((k = char_map.next(k))) {
  250. chars += char32_t(*k);
  251. }
  252. return chars;
  253. }
  254. Vector2 BitmapFontDataFallback::get_advance(char32_t p_char, int p_size) const {
  255. _THREAD_SAFE_METHOD_
  256. ERR_FAIL_COND_V(!valid, Vector2());
  257. const Character *c = char_map.getptr(p_char);
  258. ERR_FAIL_COND_V(c == nullptr, Vector2());
  259. return c->advance * (float(p_size) / float(base_size));
  260. }
  261. Vector2 BitmapFontDataFallback::get_kerning(char32_t p_char, char32_t p_next, int p_size) const {
  262. _THREAD_SAFE_METHOD_
  263. ERR_FAIL_COND_V(!valid, Vector2());
  264. KerningPairKey kpk;
  265. kpk.A = p_char;
  266. kpk.B = p_next;
  267. const Map<KerningPairKey, int>::Element *E = kerning_map.find(kpk);
  268. if (E) {
  269. return Vector2(-E->get() * (float(p_size) / float(base_size)), 0);
  270. } else {
  271. return Vector2();
  272. }
  273. }
  274. Vector2 BitmapFontDataFallback::draw_glyph(RID p_canvas, int p_size, const Vector2 &p_pos, uint32_t p_index, const Color &p_color) const {
  275. _THREAD_SAFE_METHOD_
  276. if (p_index == 0) {
  277. return Vector2();
  278. }
  279. ERR_FAIL_COND_V(!valid, Vector2());
  280. const Character *c = char_map.getptr(p_index);
  281. ERR_FAIL_COND_V(c == nullptr, Vector2());
  282. ERR_FAIL_COND_V(c->texture_idx < -1 || c->texture_idx >= textures.size(), Vector2());
  283. if (c->texture_idx != -1) {
  284. Point2 cpos = p_pos;
  285. cpos += c->align * (float(p_size) / float(base_size));
  286. cpos.y -= ascent * (float(p_size) / float(base_size));
  287. if (RenderingServer::get_singleton() != nullptr) {
  288. //if (distance_field_hint) { // Not implemented.
  289. // RenderingServer::get_singleton()->canvas_item_set_distance_field_mode(p_canvas, true);
  290. //}
  291. 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);
  292. //if (distance_field_hint) {
  293. // RenderingServer::get_singleton()->canvas_item_set_distance_field_mode(p_canvas, false);
  294. //}
  295. }
  296. }
  297. return c->advance * (float(p_size) / float(base_size));
  298. }
  299. 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 {
  300. _THREAD_SAFE_METHOD_
  301. if (p_index == 0) {
  302. return Vector2();
  303. }
  304. ERR_FAIL_COND_V(!valid, Vector2());
  305. const Character *c = char_map.getptr(p_index);
  306. ERR_FAIL_COND_V(c == nullptr, Vector2());
  307. ERR_FAIL_COND_V(c->texture_idx < -1 || c->texture_idx >= textures.size(), Vector2());
  308. // Not supported, return advance for compatibility.
  309. return c->advance * (float(p_size) / float(base_size));
  310. }