resource_importer_imagefont.cpp 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  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. r_options->push_back(ImportOption(PropertyInfo(Variant::INT, "scaling_mode", PROPERTY_HINT_ENUM, "Disabled,Enabled (Integer),Enabled (Fractional)"), TextServer::FIXED_SIZE_SCALE_ENABLED));
  62. }
  63. bool ResourceImporterImageFont::_decode_range(const String &p_token, int32_t &r_pos) {
  64. if (p_token.begins_with("U+") || p_token.begins_with("u+") || p_token.begins_with("0x")) {
  65. // Unicode character hex index.
  66. r_pos = p_token.substr(2).hex_to_int();
  67. return true;
  68. } else if (p_token.length() == 3 && p_token[0] == '\'' && p_token[2] == '\'') {
  69. // Unicode character.
  70. r_pos = p_token.unicode_at(1);
  71. return true;
  72. } else if (p_token.is_numeric()) {
  73. // Unicode character decimal index.
  74. r_pos = p_token.to_int();
  75. return true;
  76. } else {
  77. return false;
  78. }
  79. }
  80. 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) {
  81. print_verbose("Importing image font from: " + p_source_file);
  82. int columns = p_options["columns"];
  83. int rows = p_options["rows"];
  84. Vector<String> ranges = p_options["character_ranges"];
  85. Array fallbacks = p_options["fallbacks"];
  86. Rect2i img_margin = p_options["image_margin"];
  87. Rect2i char_margin = p_options["character_margin"];
  88. TextServer::FixedSizeScaleMode smode = (TextServer::FixedSizeScaleMode)p_options["scaling_mode"].operator int();
  89. Ref<Image> img;
  90. img.instantiate();
  91. Error err = ImageLoader::load_image(p_source_file, img);
  92. ERR_FAIL_COND_V_MSG(err != OK, ERR_FILE_CANT_READ, vformat("Can't load font texture: \"%s\".", p_source_file));
  93. int count = columns * rows;
  94. int chr_cell_width = (img->get_width() - img_margin.position.x - img_margin.size.x) / columns;
  95. int chr_cell_height = (img->get_height() - img_margin.position.y - img_margin.size.y) / rows;
  96. ERR_FAIL_COND_V_MSG(chr_cell_width <= 0 || chr_cell_height <= 0, ERR_FILE_CANT_READ, "Image margin too big.");
  97. int chr_width = chr_cell_width - char_margin.position.x - char_margin.size.x;
  98. int chr_height = chr_cell_height - char_margin.position.y - char_margin.size.y;
  99. ERR_FAIL_COND_V_MSG(chr_width <= 0 || chr_height <= 0, ERR_FILE_CANT_READ, "Character margin too big.");
  100. Ref<FontFile> font;
  101. font.instantiate();
  102. font->set_antialiasing(TextServer::FONT_ANTIALIASING_NONE);
  103. font->set_generate_mipmaps(false);
  104. font->set_multichannel_signed_distance_field(false);
  105. font->set_fixed_size(chr_height);
  106. font->set_subpixel_positioning(TextServer::SUBPIXEL_POSITIONING_DISABLED);
  107. font->set_force_autohinter(false);
  108. font->set_allow_system_fallback(false);
  109. font->set_hinting(TextServer::HINTING_NONE);
  110. font->set_oversampling(1.0f);
  111. font->set_fallbacks(fallbacks);
  112. font->set_texture_image(0, Vector2i(chr_height, 0), 0, img);
  113. font->set_fixed_size_scale_mode(smode);
  114. int pos = 0;
  115. for (int i = 0; i < ranges.size(); i++) {
  116. int32_t start, end;
  117. Vector<String> tokens = ranges[i].split("-");
  118. if (tokens.size() == 2) {
  119. if (!_decode_range(tokens[0], start) || !_decode_range(tokens[1], end)) {
  120. WARN_PRINT("Invalid range: \"" + ranges[i] + "\"");
  121. continue;
  122. }
  123. } else if (tokens.size() == 1) {
  124. if (!_decode_range(tokens[0], start)) {
  125. WARN_PRINT("Invalid range: \"" + ranges[i] + "\"");
  126. continue;
  127. }
  128. end = start;
  129. } else {
  130. WARN_PRINT("Invalid range: \"" + ranges[i] + "\"");
  131. continue;
  132. }
  133. for (int32_t idx = start; idx <= end; idx++) {
  134. ERR_FAIL_COND_V_MSG(pos >= count, ERR_CANT_CREATE, "Too many characters in range, should be " + itos(columns * rows));
  135. int x = pos % columns;
  136. int y = pos / columns;
  137. font->set_glyph_advance(0, chr_height, idx, Vector2(chr_width, 0));
  138. font->set_glyph_offset(0, Vector2i(chr_height, 0), idx, Vector2i(0, -0.5 * chr_height));
  139. font->set_glyph_size(0, Vector2i(chr_height, 0), idx, Vector2(chr_width, chr_height));
  140. 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));
  141. font->set_glyph_texture_idx(0, Vector2i(chr_height, 0), idx, 0);
  142. pos++;
  143. }
  144. }
  145. font->set_cache_ascent(0, chr_height, 0.5 * chr_height);
  146. font->set_cache_descent(0, chr_height, 0.5 * chr_height);
  147. int flg = 0;
  148. if ((bool)p_options["compress"]) {
  149. flg |= ResourceSaver::SaverFlags::FLAG_COMPRESS;
  150. }
  151. print_verbose("Saving to: " + p_save_path + ".fontdata");
  152. err = ResourceSaver::save(font, p_save_path + ".fontdata", flg);
  153. ERR_FAIL_COND_V_MSG(err != OK, err, "Cannot save font to file \"" + p_save_path + ".res\".");
  154. print_verbose("Done saving to: " + p_save_path + ".fontdata");
  155. return OK;
  156. }
  157. ResourceImporterImageFont::ResourceImporterImageFont() {
  158. }