resource_importer_imagefont.cpp 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. /*************************************************************************/
  2. /* resource_importer_imagefont.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2022 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2022 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 "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::INT, "font_size"), 14));
  58. r_options->push_back(ImportOption(PropertyInfo(Variant::ARRAY, "fallbacks", PROPERTY_HINT_ARRAY_TYPE, vformat("%s/%s:%s", Variant::OBJECT, PROPERTY_HINT_RESOURCE_TYPE, "Font")), Array()));
  59. r_options->push_back(ImportOption(PropertyInfo(Variant::BOOL, "compress"), true));
  60. }
  61. bool ResourceImporterImageFont::_decode_range(const String &p_token, int32_t &r_pos) {
  62. if (p_token.begins_with("U+") || p_token.begins_with("u+") || p_token.begins_with("0x")) {
  63. // Unicode character hex index.
  64. r_pos = p_token.substr(2).hex_to_int();
  65. return true;
  66. } else if (p_token.length() == 3 && p_token[0] == '\'' && p_token[2] == '\'') {
  67. // Unicode character.
  68. r_pos = p_token.unicode_at(1);
  69. return true;
  70. } else if (p_token.is_numeric()) {
  71. // Unicode character decimal index.
  72. r_pos = p_token.to_int();
  73. return true;
  74. } else {
  75. return false;
  76. }
  77. }
  78. 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) {
  79. print_verbose("Importing image font from: " + p_source_file);
  80. int columns = p_options["columns"];
  81. int rows = p_options["rows"];
  82. int base_size = p_options["font_size"];
  83. Vector<String> ranges = p_options["character_ranges"];
  84. Array fallbacks = p_options["fallbacks"];
  85. Ref<FontFile> font;
  86. font.instantiate();
  87. font->set_antialiasing(TextServer::FONT_ANTIALIASING_NONE);
  88. font->set_generate_mipmaps(false);
  89. font->set_multichannel_signed_distance_field(false);
  90. font->set_fixed_size(base_size);
  91. font->set_subpixel_positioning(TextServer::SUBPIXEL_POSITIONING_DISABLED);
  92. font->set_force_autohinter(false);
  93. font->set_hinting(TextServer::HINTING_NONE);
  94. font->set_oversampling(1.0f);
  95. font->set_fallbacks(fallbacks);
  96. Ref<Image> img;
  97. img.instantiate();
  98. Error err = ImageLoader::load_image(p_source_file, img);
  99. ERR_FAIL_COND_V_MSG(err != OK, ERR_FILE_CANT_READ, TTR("Can't load font texture:") + " \"" + p_source_file + "\".");
  100. font->set_texture_image(0, Vector2i(base_size, 0), 0, img);
  101. int count = columns * rows;
  102. int chr_width = img->get_width() / columns;
  103. int chr_height = img->get_height() / rows;
  104. int pos = 0;
  105. for (int i = 0; i < ranges.size(); i++) {
  106. int32_t start, end;
  107. Vector<String> tokens = ranges[i].split("-");
  108. if (tokens.size() == 2) {
  109. if (!_decode_range(tokens[0], start) || !_decode_range(tokens[1], end)) {
  110. WARN_PRINT("Invalid range: \"" + ranges[i] + "\"");
  111. continue;
  112. }
  113. } else if (tokens.size() == 1) {
  114. if (!_decode_range(tokens[0], start)) {
  115. WARN_PRINT("Invalid range: \"" + ranges[i] + "\"");
  116. continue;
  117. }
  118. end = start;
  119. } else {
  120. WARN_PRINT("Invalid range: \"" + ranges[i] + "\"");
  121. continue;
  122. }
  123. for (int32_t idx = start; idx <= end; idx++) {
  124. int x = pos % columns;
  125. int y = pos / columns;
  126. font->set_glyph_advance(0, base_size, idx, Vector2(chr_width, 0));
  127. font->set_glyph_offset(0, Vector2i(base_size, 0), idx, Vector2(0, -0.5 * chr_height));
  128. font->set_glyph_size(0, Vector2i(base_size, 0), idx, Vector2(chr_width, chr_height));
  129. font->set_glyph_uv_rect(0, Vector2i(base_size, 0), idx, Rect2(chr_width * x, chr_height * y, chr_width, chr_height));
  130. font->set_glyph_texture_idx(0, Vector2i(base_size, 0), idx, 0);
  131. pos++;
  132. ERR_FAIL_COND_V_MSG(pos >= count, ERR_CANT_CREATE, "Too many characters in range.");
  133. }
  134. }
  135. font->set_cache_ascent(0, base_size, 0.5 * chr_height);
  136. font->set_cache_descent(0, base_size, 0.5 * chr_height);
  137. int flg = 0;
  138. if ((bool)p_options["compress"]) {
  139. flg |= ResourceSaver::SaverFlags::FLAG_COMPRESS;
  140. }
  141. print_verbose("Saving to: " + p_save_path + ".fontdata");
  142. err = ResourceSaver::save(font, p_save_path + ".fontdata", flg);
  143. ERR_FAIL_COND_V_MSG(err != OK, err, "Cannot save font to file \"" + p_save_path + ".res\".");
  144. print_verbose("Done saving to: " + p_save_path + ".fontdata");
  145. return OK;
  146. }
  147. ResourceImporterImageFont::ResourceImporterImageFont() {
  148. }