resource_importer_dynamic_font.cpp 13 KB

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