resource_importer_dynamic_font.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. /*************************************************************************/
  2. /* resource_importer_dynamic_font.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_dynamic_font.h"
  31. #include "core/io/file_access.h"
  32. #include "core/io/resource_saver.h"
  33. #include "editor/import/dynamic_font_import_settings.h"
  34. #include "scene/resources/font.h"
  35. #include "servers/text_server.h"
  36. #include "modules/modules_enabled.gen.h" // For freetype.
  37. String ResourceImporterDynamicFont::get_importer_name() const {
  38. return "font_data_dynamic";
  39. }
  40. String ResourceImporterDynamicFont::get_visible_name() const {
  41. return "Font Data (Dynamic Font)";
  42. }
  43. void ResourceImporterDynamicFont::get_recognized_extensions(List<String> *p_extensions) const {
  44. if (p_extensions) {
  45. #ifdef MODULE_FREETYPE_ENABLED
  46. p_extensions->push_back("ttf");
  47. p_extensions->push_back("ttc");
  48. p_extensions->push_back("otf");
  49. p_extensions->push_back("otc");
  50. p_extensions->push_back("woff");
  51. p_extensions->push_back("woff2");
  52. p_extensions->push_back("pfb");
  53. p_extensions->push_back("pfm");
  54. #endif
  55. }
  56. }
  57. String ResourceImporterDynamicFont::get_save_extension() const {
  58. return "fontdata";
  59. }
  60. String ResourceImporterDynamicFont::get_resource_type() const {
  61. return "FontFile";
  62. }
  63. bool ResourceImporterDynamicFont::get_option_visibility(const String &p_path, const String &p_option, const HashMap<StringName, Variant> &p_options) const {
  64. if (p_option == "msdf_pixel_range" && !bool(p_options["multichannel_signed_distance_field"])) {
  65. return false;
  66. }
  67. if (p_option == "msdf_size" && !bool(p_options["multichannel_signed_distance_field"])) {
  68. return false;
  69. }
  70. if (p_option == "oversampling" && bool(p_options["multichannel_signed_distance_field"])) {
  71. return false;
  72. }
  73. if (p_option == "subpixel_positioning" && bool(p_options["multichannel_signed_distance_field"])) {
  74. return false;
  75. }
  76. return true;
  77. }
  78. int ResourceImporterDynamicFont::get_preset_count() const {
  79. return PRESET_MAX;
  80. }
  81. String ResourceImporterDynamicFont::get_preset_name(int p_idx) const {
  82. switch (p_idx) {
  83. case PRESET_DYNAMIC:
  84. return TTR("Dynamically rendered TrueType/OpenType font");
  85. case PRESET_MSDF:
  86. return TTR("Prerendered multichannel(+true) signed distance field");
  87. default:
  88. return String();
  89. }
  90. }
  91. void ResourceImporterDynamicFont::get_import_options(const String &p_path, List<ImportOption> *r_options, int p_preset) const {
  92. bool msdf = p_preset == PRESET_MSDF;
  93. r_options->push_back(ImportOption(PropertyInfo(Variant::NIL, "Rendering", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_GROUP), Variant()));
  94. r_options->push_back(ImportOption(PropertyInfo(Variant::BOOL, "antialiased"), true));
  95. r_options->push_back(ImportOption(PropertyInfo(Variant::BOOL, "generate_mipmaps"), false));
  96. r_options->push_back(ImportOption(PropertyInfo(Variant::BOOL, "multichannel_signed_distance_field", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_DEFAULT | PROPERTY_USAGE_UPDATE_ALL_IF_MODIFIED), (msdf) ? true : false));
  97. r_options->push_back(ImportOption(PropertyInfo(Variant::INT, "msdf_pixel_range", PROPERTY_HINT_RANGE, "1,100,1"), 8));
  98. r_options->push_back(ImportOption(PropertyInfo(Variant::INT, "msdf_size", PROPERTY_HINT_RANGE, "1,250,1"), 48));
  99. r_options->push_back(ImportOption(PropertyInfo(Variant::BOOL, "force_autohinter"), false));
  100. r_options->push_back(ImportOption(PropertyInfo(Variant::INT, "hinting", PROPERTY_HINT_ENUM, "None,Light,Normal"), 1));
  101. r_options->push_back(ImportOption(PropertyInfo(Variant::INT, "subpixel_positioning", PROPERTY_HINT_ENUM, "Disabled,Auto,One half of a pixel,One quarter of a pixel"), 1));
  102. r_options->push_back(ImportOption(PropertyInfo(Variant::FLOAT, "oversampling", PROPERTY_HINT_RANGE, "0,10,0.1"), 0.0));
  103. r_options->push_back(ImportOption(PropertyInfo(Variant::NIL, "Fallbacks", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_GROUP), Variant()));
  104. 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()));
  105. r_options->push_back(ImportOption(PropertyInfo(Variant::NIL, "Compress", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_GROUP), Variant()));
  106. r_options->push_back(ImportOption(PropertyInfo(Variant::BOOL, "compress"), true));
  107. // Hide from the main UI, only for advanced import dialog.
  108. r_options->push_back(ImportOption(PropertyInfo(Variant::ARRAY, "preload", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_STORAGE), Array()));
  109. r_options->push_back(ImportOption(PropertyInfo(Variant::DICTIONARY, "language_support", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_STORAGE), Dictionary()));
  110. r_options->push_back(ImportOption(PropertyInfo(Variant::DICTIONARY, "script_support", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_STORAGE), Dictionary()));
  111. r_options->push_back(ImportOption(PropertyInfo(Variant::DICTIONARY, "opentype_features", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_STORAGE), Dictionary()));
  112. }
  113. bool ResourceImporterDynamicFont::has_advanced_options() const {
  114. return true;
  115. }
  116. void ResourceImporterDynamicFont::show_advanced_options(const String &p_path) {
  117. DynamicFontImportSettings::get_singleton()->open_settings(p_path);
  118. }
  119. Error ResourceImporterDynamicFont::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) {
  120. print_verbose("Importing dynamic font from: " + p_source_file);
  121. bool antialiased = p_options["antialiased"];
  122. bool generate_mipmaps = p_options["generate_mipmaps"];
  123. bool msdf = p_options["multichannel_signed_distance_field"];
  124. int px_range = p_options["msdf_pixel_range"];
  125. int px_size = p_options["msdf_size"];
  126. Dictionary ot_ov = p_options["opentype_features"];
  127. bool autohinter = p_options["force_autohinter"];
  128. int hinting = p_options["hinting"];
  129. int subpixel_positioning = p_options["subpixel_positioning"];
  130. real_t oversampling = p_options["oversampling"];
  131. Array fallbacks = p_options["fallbacks"];
  132. // Load base font data.
  133. Vector<uint8_t> data = FileAccess::get_file_as_array(p_source_file);
  134. // Create font.
  135. Ref<FontFile> font;
  136. font.instantiate();
  137. font->set_data(data);
  138. font->set_antialiased(antialiased);
  139. font->set_generate_mipmaps(generate_mipmaps);
  140. font->set_multichannel_signed_distance_field(msdf);
  141. font->set_msdf_pixel_range(px_range);
  142. font->set_msdf_size(px_size);
  143. font->set_opentype_feature_overrides(ot_ov);
  144. font->set_fixed_size(0);
  145. font->set_force_autohinter(autohinter);
  146. font->set_subpixel_positioning((TextServer::SubpixelPositioning)subpixel_positioning);
  147. font->set_hinting((TextServer::Hinting)hinting);
  148. font->set_oversampling(oversampling);
  149. font->set_fallbacks(fallbacks);
  150. Dictionary langs = p_options["language_support"];
  151. for (int i = 0; i < langs.size(); i++) {
  152. String key = langs.get_key_at_index(i);
  153. bool enabled = langs.get_value_at_index(i);
  154. font->set_language_support_override(key, enabled);
  155. }
  156. Dictionary scripts = p_options["script_support"];
  157. for (int i = 0; i < scripts.size(); i++) {
  158. String key = scripts.get_key_at_index(i);
  159. bool enabled = scripts.get_value_at_index(i);
  160. font->set_script_support_override(key, enabled);
  161. }
  162. Array preload_configurations = p_options["preload"];
  163. for (int i = 0; i < preload_configurations.size(); i++) {
  164. Dictionary preload_config = preload_configurations[i];
  165. Dictionary variation = preload_config.has("variation_opentype") ? preload_config["variation_opentype"].operator Dictionary() : Dictionary();
  166. double embolden = preload_config.has("variation_embolden") ? preload_config["variation_embolden"].operator double() : 0;
  167. int face_index = preload_config.has("variation_face_index") ? preload_config["variation_face_index"].operator int() : 0;
  168. Transform2D transform = preload_config.has("variation_transform") ? preload_config["variation_transform"].operator Transform2D() : Transform2D();
  169. Vector2i size = preload_config.has("size") ? preload_config["size"].operator Vector2i() : Vector2i(16, 0);
  170. String name = preload_config.has("name") ? preload_config["name"].operator String() : vformat("Configuration %d", i);
  171. RID conf_rid = font->find_variation(variation, face_index, embolden, transform);
  172. Array chars = preload_config["chars"];
  173. for (int j = 0; j < chars.size(); j++) {
  174. char32_t c = chars[j].operator int();
  175. TS->font_render_range(conf_rid, size, c, c);
  176. }
  177. Array glyphs = preload_config["glyphs"];
  178. for (int j = 0; j < glyphs.size(); j++) {
  179. int32_t c = glyphs[j];
  180. TS->font_render_glyph(conf_rid, size, c);
  181. }
  182. }
  183. int flg = 0;
  184. if ((bool)p_options["compress"]) {
  185. flg |= ResourceSaver::SaverFlags::FLAG_COMPRESS;
  186. }
  187. print_verbose("Saving to: " + p_save_path + ".fontdata");
  188. Error err = ResourceSaver::save(font, p_save_path + ".fontdata", flg);
  189. ERR_FAIL_COND_V_MSG(err != OK, err, "Cannot save font to file \"" + p_save_path + ".res\".");
  190. print_verbose("Done saving to: " + p_save_path + ".fontdata");
  191. return OK;
  192. }
  193. ResourceImporterDynamicFont::ResourceImporterDynamicFont() {
  194. }