resource_importer_dynamic_font.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  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) 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_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 == "antialiasing" && bool(p_options["multichannel_signed_distance_field"])) {
  71. return false;
  72. }
  73. if (p_option == "oversampling" && bool(p_options["multichannel_signed_distance_field"])) {
  74. return false;
  75. }
  76. if (p_option == "subpixel_positioning" && bool(p_options["multichannel_signed_distance_field"])) {
  77. return false;
  78. }
  79. return true;
  80. }
  81. int ResourceImporterDynamicFont::get_preset_count() const {
  82. return PRESET_MAX;
  83. }
  84. String ResourceImporterDynamicFont::get_preset_name(int p_idx) const {
  85. switch (p_idx) {
  86. case PRESET_DYNAMIC:
  87. return TTR("Dynamically rendered TrueType/OpenType font");
  88. case PRESET_MSDF:
  89. return TTR("Prerendered multichannel(+true) signed distance field");
  90. default:
  91. return String();
  92. }
  93. }
  94. void ResourceImporterDynamicFont::get_import_options(const String &p_path, List<ImportOption> *r_options, int p_preset) const {
  95. bool msdf = p_preset == PRESET_MSDF;
  96. r_options->push_back(ImportOption(PropertyInfo(Variant::NIL, "Rendering", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_GROUP), Variant()));
  97. r_options->push_back(ImportOption(PropertyInfo(Variant::INT, "antialiasing", PROPERTY_HINT_ENUM, "None,Grayscale,LCD Subpixel"), 1));
  98. r_options->push_back(ImportOption(PropertyInfo(Variant::BOOL, "generate_mipmaps"), false));
  99. r_options->push_back(ImportOption(PropertyInfo(Variant::BOOL, "disable_embedded_bitmaps"), true));
  100. 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));
  101. r_options->push_back(ImportOption(PropertyInfo(Variant::INT, "msdf_pixel_range", PROPERTY_HINT_RANGE, "1,100,1"), 8));
  102. r_options->push_back(ImportOption(PropertyInfo(Variant::INT, "msdf_size", PROPERTY_HINT_RANGE, "1,250,1"), 48));
  103. r_options->push_back(ImportOption(PropertyInfo(Variant::BOOL, "allow_system_fallback"), true));
  104. r_options->push_back(ImportOption(PropertyInfo(Variant::BOOL, "force_autohinter"), false));
  105. r_options->push_back(ImportOption(PropertyInfo(Variant::INT, "hinting", PROPERTY_HINT_ENUM, "None,Light,Normal"), 1));
  106. 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));
  107. r_options->push_back(ImportOption(PropertyInfo(Variant::FLOAT, "oversampling", PROPERTY_HINT_RANGE, "0,10,0.1"), 0.0));
  108. r_options->push_back(ImportOption(PropertyInfo(Variant::NIL, "Fallbacks", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_GROUP), Variant()));
  109. r_options->push_back(ImportOption(PropertyInfo(Variant::ARRAY, "fallbacks", PROPERTY_HINT_ARRAY_TYPE, MAKE_RESOURCE_TYPE_HINT("Font")), Array()));
  110. r_options->push_back(ImportOption(PropertyInfo(Variant::NIL, "Compress", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_GROUP), Variant()));
  111. r_options->push_back(ImportOption(PropertyInfo(Variant::BOOL, "compress"), true));
  112. // Hide from the main UI, only for advanced import dialog.
  113. r_options->push_back(ImportOption(PropertyInfo(Variant::ARRAY, "preload", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_STORAGE), Array()));
  114. r_options->push_back(ImportOption(PropertyInfo(Variant::DICTIONARY, "language_support", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_STORAGE), Dictionary()));
  115. r_options->push_back(ImportOption(PropertyInfo(Variant::DICTIONARY, "script_support", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_STORAGE), Dictionary()));
  116. r_options->push_back(ImportOption(PropertyInfo(Variant::DICTIONARY, "opentype_features", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_STORAGE), Dictionary()));
  117. }
  118. bool ResourceImporterDynamicFont::has_advanced_options() const {
  119. return true;
  120. }
  121. void ResourceImporterDynamicFont::show_advanced_options(const String &p_path) {
  122. DynamicFontImportSettingsDialog::get_singleton()->open_settings(p_path);
  123. }
  124. 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) {
  125. print_verbose("Importing dynamic font from: " + p_source_file);
  126. int antialiasing = p_options["antialiasing"];
  127. bool generate_mipmaps = p_options["generate_mipmaps"];
  128. bool disable_embedded_bitmaps = p_options["disable_embedded_bitmaps"];
  129. bool msdf = p_options["multichannel_signed_distance_field"];
  130. int px_range = p_options["msdf_pixel_range"];
  131. int px_size = p_options["msdf_size"];
  132. Dictionary ot_ov = p_options["opentype_features"];
  133. bool autohinter = p_options["force_autohinter"];
  134. bool allow_system_fallback = p_options["allow_system_fallback"];
  135. int hinting = p_options["hinting"];
  136. int subpixel_positioning = p_options["subpixel_positioning"];
  137. real_t oversampling = p_options["oversampling"];
  138. Array fallbacks = p_options["fallbacks"];
  139. // Load base font data.
  140. Vector<uint8_t> data = FileAccess::get_file_as_bytes(p_source_file);
  141. // Create font.
  142. Ref<FontFile> font;
  143. font.instantiate();
  144. font->set_data(data);
  145. font->set_antialiasing((TextServer::FontAntialiasing)antialiasing);
  146. font->set_disable_embedded_bitmaps(disable_embedded_bitmaps);
  147. font->set_generate_mipmaps(generate_mipmaps);
  148. font->set_multichannel_signed_distance_field(msdf);
  149. font->set_msdf_pixel_range(px_range);
  150. font->set_msdf_size(px_size);
  151. font->set_opentype_feature_overrides(ot_ov);
  152. font->set_fixed_size(0);
  153. font->set_force_autohinter(autohinter);
  154. font->set_allow_system_fallback(allow_system_fallback);
  155. font->set_subpixel_positioning((TextServer::SubpixelPositioning)subpixel_positioning);
  156. font->set_hinting((TextServer::Hinting)hinting);
  157. font->set_oversampling(oversampling);
  158. font->set_fallbacks(fallbacks);
  159. Dictionary langs = p_options["language_support"];
  160. for (int i = 0; i < langs.size(); i++) {
  161. String key = langs.get_key_at_index(i);
  162. bool enabled = langs.get_value_at_index(i);
  163. font->set_language_support_override(key, enabled);
  164. }
  165. Dictionary scripts = p_options["script_support"];
  166. for (int i = 0; i < scripts.size(); i++) {
  167. String key = scripts.get_key_at_index(i);
  168. bool enabled = scripts.get_value_at_index(i);
  169. font->set_script_support_override(key, enabled);
  170. }
  171. Array preload_configurations = p_options["preload"];
  172. for (int i = 0; i < preload_configurations.size(); i++) {
  173. Dictionary preload_config = preload_configurations[i];
  174. Dictionary variation = preload_config.has("variation_opentype") ? preload_config["variation_opentype"].operator Dictionary() : Dictionary();
  175. double embolden = preload_config.has("variation_embolden") ? preload_config["variation_embolden"].operator double() : 0;
  176. int face_index = preload_config.has("variation_face_index") ? preload_config["variation_face_index"].operator int() : 0;
  177. Transform2D transform = preload_config.has("variation_transform") ? preload_config["variation_transform"].operator Transform2D() : Transform2D();
  178. Vector2i size = preload_config.has("size") ? preload_config["size"].operator Vector2i() : Vector2i(16, 0);
  179. String name = preload_config.has("name") ? preload_config["name"].operator String() : vformat("Configuration %d", i);
  180. RID conf_rid = font->find_variation(variation, face_index, embolden, transform);
  181. Array chars = preload_config["chars"];
  182. for (int j = 0; j < chars.size(); j++) {
  183. char32_t c = chars[j].operator int();
  184. TS->font_render_range(conf_rid, size, c, c);
  185. }
  186. Array glyphs = preload_config["glyphs"];
  187. for (int j = 0; j < glyphs.size(); j++) {
  188. int32_t c = glyphs[j];
  189. TS->font_render_glyph(conf_rid, size, c);
  190. }
  191. }
  192. int flg = 0;
  193. if ((bool)p_options["compress"]) {
  194. flg |= ResourceSaver::SaverFlags::FLAG_COMPRESS;
  195. }
  196. print_verbose("Saving to: " + p_save_path + ".fontdata");
  197. Error err = ResourceSaver::save(font, p_save_path + ".fontdata", flg);
  198. ERR_FAIL_COND_V_MSG(err != OK, err, "Cannot save font to file \"" + p_save_path + ".res\".");
  199. print_verbose("Done saving to: " + p_save_path + ".fontdata");
  200. return OK;
  201. }
  202. ResourceImporterDynamicFont::ResourceImporterDynamicFont() {
  203. }