resource_importer_dynamic_font.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  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. String ResourceImporterDynamicFont::get_importer_name() const {
  37. return "font_data_dynamic";
  38. }
  39. String ResourceImporterDynamicFont::get_visible_name() const {
  40. return "Font Data (Dynamic Font)";
  41. }
  42. void ResourceImporterDynamicFont::get_recognized_extensions(List<String> *p_extensions) const {
  43. if (p_extensions) {
  44. p_extensions->push_back("ttf");
  45. p_extensions->push_back("ttc");
  46. p_extensions->push_back("otf");
  47. p_extensions->push_back("otc");
  48. p_extensions->push_back("woff");
  49. p_extensions->push_back("woff2");
  50. p_extensions->push_back("pfb");
  51. p_extensions->push_back("pfm");
  52. }
  53. }
  54. String ResourceImporterDynamicFont::get_save_extension() const {
  55. return "fontdata";
  56. }
  57. String ResourceImporterDynamicFont::get_resource_type() const {
  58. return "FontFile";
  59. }
  60. bool ResourceImporterDynamicFont::get_option_visibility(const String &p_path, const String &p_option, const HashMap<StringName, Variant> &p_options) const {
  61. if (p_option == "msdf_pixel_range" && !bool(p_options["multichannel_signed_distance_field"])) {
  62. return false;
  63. }
  64. if (p_option == "msdf_size" && !bool(p_options["multichannel_signed_distance_field"])) {
  65. return false;
  66. }
  67. if (p_option == "antialiasing" && 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. if (p_option == "keep_rounding_remainders" && 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,Auto (Except Pixel Fonts)"), 4));
  107. r_options->push_back(ImportOption(PropertyInfo(Variant::BOOL, "keep_rounding_remainders"), true));
  108. r_options->push_back(ImportOption(PropertyInfo(Variant::FLOAT, "oversampling", PROPERTY_HINT_RANGE, "0,10,0.1"), 0.0));
  109. r_options->push_back(ImportOption(PropertyInfo(Variant::NIL, "Fallbacks", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_GROUP), Variant()));
  110. r_options->push_back(ImportOption(PropertyInfo(Variant::ARRAY, "fallbacks", PROPERTY_HINT_ARRAY_TYPE, MAKE_RESOURCE_TYPE_HINT("Font")), Array()));
  111. r_options->push_back(ImportOption(PropertyInfo(Variant::NIL, "Compress", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_GROUP), Variant()));
  112. r_options->push_back(ImportOption(PropertyInfo(Variant::BOOL, "compress"), true));
  113. // Hide from the main UI, only for advanced import dialog.
  114. r_options->push_back(ImportOption(PropertyInfo(Variant::ARRAY, "preload", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_STORAGE), Array()));
  115. r_options->push_back(ImportOption(PropertyInfo(Variant::DICTIONARY, "language_support", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_STORAGE), Dictionary()));
  116. r_options->push_back(ImportOption(PropertyInfo(Variant::DICTIONARY, "script_support", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_STORAGE), Dictionary()));
  117. r_options->push_back(ImportOption(PropertyInfo(Variant::DICTIONARY, "opentype_features", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_STORAGE), Dictionary()));
  118. }
  119. bool ResourceImporterDynamicFont::has_advanced_options() const {
  120. return true;
  121. }
  122. void ResourceImporterDynamicFont::show_advanced_options(const String &p_path) {
  123. DynamicFontImportSettingsDialog::get_singleton()->open_settings(p_path);
  124. }
  125. 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) {
  126. print_verbose("Importing dynamic font from: " + p_source_file);
  127. int antialiasing = p_options["antialiasing"];
  128. bool generate_mipmaps = p_options["generate_mipmaps"];
  129. bool disable_embedded_bitmaps = p_options["disable_embedded_bitmaps"];
  130. bool msdf = p_options["multichannel_signed_distance_field"];
  131. int px_range = p_options["msdf_pixel_range"];
  132. int px_size = p_options["msdf_size"];
  133. Dictionary ot_ov = p_options["opentype_features"];
  134. bool autohinter = p_options["force_autohinter"];
  135. bool allow_system_fallback = p_options["allow_system_fallback"];
  136. int hinting = p_options["hinting"];
  137. int subpixel_positioning = p_options["subpixel_positioning"];
  138. bool keep_rounding_remainders = p_options["keep_rounding_remainders"];
  139. real_t oversampling = p_options["oversampling"];
  140. Array fallbacks = p_options["fallbacks"];
  141. // Load base font data.
  142. Vector<uint8_t> data = FileAccess::get_file_as_bytes(p_source_file);
  143. // Create font.
  144. Ref<FontFile> font;
  145. font.instantiate();
  146. font->set_data(data);
  147. font->set_antialiasing((TextServer::FontAntialiasing)antialiasing);
  148. font->set_disable_embedded_bitmaps(disable_embedded_bitmaps);
  149. font->set_generate_mipmaps(generate_mipmaps);
  150. font->set_multichannel_signed_distance_field(msdf);
  151. font->set_msdf_pixel_range(px_range);
  152. font->set_msdf_size(px_size);
  153. font->set_opentype_feature_overrides(ot_ov);
  154. font->set_fixed_size(0);
  155. font->set_force_autohinter(autohinter);
  156. font->set_allow_system_fallback(allow_system_fallback);
  157. font->set_hinting((TextServer::Hinting)hinting);
  158. font->set_oversampling(oversampling);
  159. font->set_fallbacks(fallbacks);
  160. if (subpixel_positioning == 4 /* Auto (Except Pixel Fonts) */) {
  161. Array rids = font->get_rids();
  162. if (!rids.is_empty()) {
  163. PackedInt32Array glyphs = TS->font_get_supported_glyphs(rids[0]);
  164. bool is_pixel = true;
  165. for (int32_t gl : glyphs) {
  166. Dictionary ct = TS->font_get_glyph_contours(rids[0], 16, gl);
  167. PackedInt32Array contours = ct["contours"];
  168. PackedVector3Array points = ct["points"];
  169. int prev_start = 0;
  170. for (int i = 0; i < contours.size(); i++) {
  171. for (int j = prev_start; j <= contours[i]; j++) {
  172. int next_point = (j < contours[i]) ? (j + 1) : prev_start;
  173. 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))) {
  174. is_pixel = false;
  175. break;
  176. }
  177. }
  178. prev_start = contours[i] + 1;
  179. if (!is_pixel) {
  180. break;
  181. }
  182. }
  183. if (!is_pixel) {
  184. break;
  185. }
  186. }
  187. if (is_pixel && !glyphs.is_empty()) {
  188. print_line(vformat("%s: Pixel font detected, disabling subpixel positioning.", p_source_file));
  189. subpixel_positioning = TextServer::SUBPIXEL_POSITIONING_DISABLED;
  190. } else {
  191. subpixel_positioning = TextServer::SUBPIXEL_POSITIONING_AUTO;
  192. }
  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. }