resource_importer_imagefont.cpp 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. /**************************************************************************/
  2. /* resource_importer_imagefont.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 "resource_importer_imagefont.h"
  31. #include "core/io/image_loader.h"
  32. #include "core/io/resource_saver.h"
  33. String ResourceImporterImageFont::get_importer_name() const {
  34. return "font_data_image";
  35. }
  36. String ResourceImporterImageFont::get_visible_name() const {
  37. return "Font Data (Monospace Image Font)";
  38. }
  39. void ResourceImporterImageFont::get_recognized_extensions(List<String> *p_extensions) const {
  40. if (p_extensions) {
  41. ImageLoader::get_recognized_extensions(p_extensions);
  42. }
  43. }
  44. String ResourceImporterImageFont::get_save_extension() const {
  45. return "fontdata";
  46. }
  47. String ResourceImporterImageFont::get_resource_type() const {
  48. return "FontFile";
  49. }
  50. bool ResourceImporterImageFont::get_option_visibility(const String &p_path, const String &p_option, const HashMap<StringName, Variant> &p_options) const {
  51. return true;
  52. }
  53. void ResourceImporterImageFont::get_import_options(const String &p_path, List<ImportOption> *r_options, int p_preset) const {
  54. r_options->push_back(ImportOption(PropertyInfo(Variant::PACKED_STRING_ARRAY, "character_ranges"), Vector<String>()));
  55. r_options->push_back(ImportOption(PropertyInfo(Variant::INT, "columns"), 1));
  56. r_options->push_back(ImportOption(PropertyInfo(Variant::INT, "rows"), 1));
  57. r_options->push_back(ImportOption(PropertyInfo(Variant::RECT2I, "image_margin"), Rect2i()));
  58. r_options->push_back(ImportOption(PropertyInfo(Variant::RECT2I, "character_margin"), Rect2i()));
  59. r_options->push_back(ImportOption(PropertyInfo(Variant::ARRAY, "fallbacks", PROPERTY_HINT_ARRAY_TYPE, MAKE_RESOURCE_TYPE_HINT("Font")), Array()));
  60. r_options->push_back(ImportOption(PropertyInfo(Variant::BOOL, "compress"), true));
  61. }
  62. bool ResourceImporterImageFont::_decode_range(const String &p_token, int32_t &r_pos) {
  63. if (p_token.begins_with("U+") || p_token.begins_with("u+") || p_token.begins_with("0x")) {
  64. // Unicode character hex index.
  65. r_pos = p_token.substr(2).hex_to_int();
  66. return true;
  67. } else if (p_token.length() == 3 && p_token[0] == '\'' && p_token[2] == '\'') {
  68. // Unicode character.
  69. r_pos = p_token.unicode_at(1);
  70. return true;
  71. } else if (p_token.is_numeric()) {
  72. // Unicode character decimal index.
  73. r_pos = p_token.to_int();
  74. return true;
  75. } else {
  76. return false;
  77. }
  78. }
  79. Error ResourceImporterImageFont::import(const String &p_source_file, const String &p_save_path, const HashMap<StringName, Variant> &p_options, List<String> *r_platform_variants, List<String> *r_gen_files, Variant *r_metadata) {
  80. print_verbose("Importing image font from: " + p_source_file);
  81. int columns = p_options["columns"];
  82. int rows = p_options["rows"];
  83. Vector<String> ranges = p_options["character_ranges"];
  84. Array fallbacks = p_options["fallbacks"];
  85. Rect2i img_margin = p_options["image_margin"];
  86. Rect2i char_margin = p_options["character_margin"];
  87. Ref<Image> img;
  88. img.instantiate();
  89. Error err = ImageLoader::load_image(p_source_file, img);
  90. ERR_FAIL_COND_V_MSG(err != OK, ERR_FILE_CANT_READ, TTR("Can't load font texture:") + " \"" + p_source_file + "\".");
  91. int count = columns * rows;
  92. int chr_cell_width = (img->get_width() - img_margin.position.x - img_margin.size.x) / columns;
  93. int chr_cell_height = (img->get_height() - img_margin.position.y - img_margin.size.y) / rows;
  94. ERR_FAIL_COND_V_MSG(chr_cell_width <= 0 || chr_cell_height <= 0, ERR_FILE_CANT_READ, TTR("Image margin too big."));
  95. int chr_width = chr_cell_width - char_margin.position.x - char_margin.size.x;
  96. int chr_height = chr_cell_height - char_margin.position.y - char_margin.size.y;
  97. ERR_FAIL_COND_V_MSG(chr_width <= 0 || chr_height <= 0, ERR_FILE_CANT_READ, TTR("Character margin too bit."));
  98. Ref<FontFile> font;
  99. font.instantiate();
  100. font->set_antialiasing(TextServer::FONT_ANTIALIASING_NONE);
  101. font->set_generate_mipmaps(false);
  102. font->set_multichannel_signed_distance_field(false);
  103. font->set_fixed_size(chr_height);
  104. font->set_subpixel_positioning(TextServer::SUBPIXEL_POSITIONING_DISABLED);
  105. font->set_force_autohinter(false);
  106. font->set_allow_system_fallback(false);
  107. font->set_hinting(TextServer::HINTING_NONE);
  108. font->set_oversampling(1.0f);
  109. font->set_fallbacks(fallbacks);
  110. font->set_texture_image(0, Vector2i(chr_height, 0), 0, img);
  111. int pos = 0;
  112. for (int i = 0; i < ranges.size(); i++) {
  113. int32_t start, end;
  114. Vector<String> tokens = ranges[i].split("-");
  115. if (tokens.size() == 2) {
  116. if (!_decode_range(tokens[0], start) || !_decode_range(tokens[1], end)) {
  117. WARN_PRINT("Invalid range: \"" + ranges[i] + "\"");
  118. continue;
  119. }
  120. } else if (tokens.size() == 1) {
  121. if (!_decode_range(tokens[0], start)) {
  122. WARN_PRINT("Invalid range: \"" + ranges[i] + "\"");
  123. continue;
  124. }
  125. end = start;
  126. } else {
  127. WARN_PRINT("Invalid range: \"" + ranges[i] + "\"");
  128. continue;
  129. }
  130. for (int32_t idx = start; idx <= end; idx++) {
  131. ERR_FAIL_COND_V_MSG(pos >= count, ERR_CANT_CREATE, "Too many characters in range, should be " + itos(columns * rows));
  132. int x = pos % columns;
  133. int y = pos / columns;
  134. font->set_glyph_advance(0, chr_height, idx, Vector2(chr_width, 0));
  135. font->set_glyph_offset(0, Vector2i(chr_height, 0), idx, Vector2(0, -0.5 * chr_height));
  136. font->set_glyph_size(0, Vector2i(chr_height, 0), idx, Vector2(chr_width, chr_height));
  137. font->set_glyph_uv_rect(0, Vector2i(chr_height, 0), idx, Rect2(img_margin.position.x + chr_cell_width * x + char_margin.position.x, img_margin.position.y + chr_cell_height * y + char_margin.position.y, chr_width, chr_height));
  138. font->set_glyph_texture_idx(0, Vector2i(chr_height, 0), idx, 0);
  139. pos++;
  140. }
  141. }
  142. font->set_cache_ascent(0, chr_height, 0.5 * chr_height);
  143. font->set_cache_descent(0, chr_height, 0.5 * chr_height);
  144. int flg = 0;
  145. if ((bool)p_options["compress"]) {
  146. flg |= ResourceSaver::SaverFlags::FLAG_COMPRESS;
  147. }
  148. print_verbose("Saving to: " + p_save_path + ".fontdata");
  149. err = ResourceSaver::save(font, p_save_path + ".fontdata", flg);
  150. ERR_FAIL_COND_V_MSG(err != OK, err, "Cannot save font to file \"" + p_save_path + ".res\".");
  151. print_verbose("Done saving to: " + p_save_path + ".fontdata");
  152. return OK;
  153. }
  154. ResourceImporterImageFont::ResourceImporterImageFont() {
  155. }