| 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 | |
| 31 | #include "resource_importer_dynamic_font.h" |
| 32 | |
| 33 | #include "core/io/file_access.h" |
| 34 | #include "core/io/resource_saver.h" |
| 35 | #include "editor/import/dynamic_font_import_settings.h" |
| 36 | #include "scene/resources/font.h" |
| 37 | #include "servers/text/text_server.h" |
| 38 | |
| 39 | String ResourceImporterDynamicFont::get_importer_name() const { |
| 40 | return "font_data_dynamic"; |
| 41 | } |
| 42 | |
| 43 | String ResourceImporterDynamicFont::get_visible_name() const { |
| 44 | return "Font Data (Dynamic Font)"; |
| 45 | } |
| 46 | |
| 47 | void ResourceImporterDynamicFont::get_recognized_extensions(List<String> *p_extensions) const { |
| 48 | if (p_extensions) { |
| 49 | p_extensions->push_back("ttf"); |
| 50 | p_extensions->push_back("ttc"); |
| 51 | p_extensions->push_back("otf"); |
| 52 | p_extensions->push_back("otc"); |
| 53 | p_extensions->push_back("woff"); |
| 54 | p_extensions->push_back("woff2"); |
| 55 | p_extensions->push_back("pfb"); |
| 56 | p_extensions->push_back("pfm"); |
| 57 | } |
| 58 | } |
| 59 | |
| 60 | String ResourceImporterDynamicFont::get_save_extension() const { |
| 61 | return "fontdata"; |
| 62 | } |
| 63 | |
| 64 | String ResourceImporterDynamicFont::get_resource_type() const { |
| 65 | return "FontFile"; |
| 66 | } |
| 67 | |
| 68 | void ResourceImporterDynamicFont::get_build_dependencies(const String &p_path, HashSet<String> *r_dependencies) { |
| 69 | Ref<FontFile> font = ResourceLoader::load(p_path); |
| 70 | if (font.is_valid() && font->is_multichannel_signed_distance_field()) { |
| 71 | r_dependencies->insert("module_msdfgen_enabled"); |
| 72 | } |
| 73 | } |
| 74 | |
| 75 | bool ResourceImporterDynamicFont::get_option_visibility(const String &p_path, const String &p_option, const HashMap<StringName, Variant> &p_options) const { |
| 76 | if (p_option == "msdf_pixel_range" && !bool(p_options["multichannel_signed_distance_field"])) { |
| 77 | return false; |
| 78 | } |
| 79 | if (p_option == "msdf_size" && !bool(p_options["multichannel_signed_distance_field"])) { |
| 80 | return false; |
| 81 | } |
| 82 | if (p_option == "antialiasing" && bool(p_options["multichannel_signed_distance_field"])) { |
| 83 | return false; |
| 84 | } |
| 85 | if (p_option == "oversampling" && bool(p_options["multichannel_signed_distance_field"])) { |
| 86 | return false; |
| 87 | } |
| 88 | if (p_option == "subpixel_positioning" && bool(p_options["multichannel_signed_distance_field"])) { |
| 89 | return false; |
| 90 | } |
| 91 | if (p_option == "keep_rounding_remainders" && bool(p_options["multichannel_signed_distance_field"])) { |
| 92 | return false; |
| 93 | } |
| 94 | return true; |
| 95 | } |
| 96 | |
| 97 | int ResourceImporterDynamicFont::get_preset_count() const { |
| 98 | return PRESET_MAX; |
| 99 | } |
| 100 | |
| 101 | String ResourceImporterDynamicFont::get_preset_name(int p_idx) const { |
| 102 | switch (p_idx) { |
| 103 | case PRESET_DYNAMIC: |
| 104 | return TTR("Dynamically rendered TrueType/OpenType font"); |
| 105 | case PRESET_MSDF: |
| 106 | return TTR("Prerendered multichannel(+true) signed distance field"); |
| 107 | default: |
| 108 | return String(); |
| 109 | } |
| 110 | } |
| 111 | |
| 112 | void ResourceImporterDynamicFont::get_import_options(const String &p_path, List<ImportOption> *r_options, int p_preset) const { |
| 113 | bool msdf = p_preset == PRESET_MSDF; |
| 114 | |
| 115 | r_options->push_back(ImportOption(PropertyInfo(Variant::NIL, "Rendering", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_GROUP), Variant())); |
| 116 | |
| 117 | r_options->push_back(ImportOption(PropertyInfo(Variant::INT, "antialiasing", PROPERTY_HINT_ENUM, "None,Grayscale,LCD Subpixel"), 1)); |
| 118 | r_options->push_back(ImportOption(PropertyInfo(Variant::BOOL, "generate_mipmaps"), false)); |
| 119 | r_options->push_back(ImportOption(PropertyInfo(Variant::BOOL, "disable_embedded_bitmaps"), true)); |
| 120 | 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)); |
| 121 | r_options->push_back(ImportOption(PropertyInfo(Variant::INT, "msdf_pixel_range", PROPERTY_HINT_RANGE, "1,100,1"), 8)); |
| 122 | r_options->push_back(ImportOption(PropertyInfo(Variant::INT, "msdf_size", PROPERTY_HINT_RANGE, "1,250,1"), 48)); |
| 123 | |
| 124 | r_options->push_back(ImportOption(PropertyInfo(Variant::BOOL, "allow_system_fallback"), true)); |
| 125 | r_options->push_back(ImportOption(PropertyInfo(Variant::BOOL, "force_autohinter"), false)); |
| 126 | r_options->push_back(ImportOption(PropertyInfo(Variant::BOOL, "modulate_color_glyphs"), false)); |
| 127 | r_options->push_back(ImportOption(PropertyInfo(Variant::INT, "hinting", PROPERTY_HINT_ENUM, "None,Light,Normal"), 1)); |
| 128 | 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)); |
| 129 | r_options->push_back(ImportOption(PropertyInfo(Variant::BOOL, "keep_rounding_remainders"), true)); |
| 130 | r_options->push_back(ImportOption(PropertyInfo(Variant::FLOAT, "oversampling", PROPERTY_HINT_RANGE, "0,10,0.1"), 0.0)); |
| 131 | |
| 132 | r_options->push_back(ImportOption(PropertyInfo(Variant::NIL, "Fallbacks", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_GROUP), Variant())); |
| 133 | r_options->push_back(ImportOption(PropertyInfo(Variant::ARRAY, "fallbacks", PROPERTY_HINT_ARRAY_TYPE, MAKE_RESOURCE_TYPE_HINT("Font")), Array())); |
| 134 | |
| 135 | r_options->push_back(ImportOption(PropertyInfo(Variant::NIL, "Compress", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_GROUP), Variant())); |
| 136 | r_options->push_back(ImportOption(PropertyInfo(Variant::BOOL, "compress"), true)); |
| 137 | |
| 138 | // Hide from the main UI, only for advanced import dialog. |
| 139 | r_options->push_back(ImportOption(PropertyInfo(Variant::ARRAY, "preload", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_STORAGE), Array())); |
| 140 | r_options->push_back(ImportOption(PropertyInfo(Variant::DICTIONARY, "language_support", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_STORAGE), Dictionary())); |
| 141 | r_options->push_back(ImportOption(PropertyInfo(Variant::DICTIONARY, "script_support", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_STORAGE), Dictionary())); |
| 142 | r_options->push_back(ImportOption(PropertyInfo(Variant::DICTIONARY, "opentype_features", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_STORAGE), Dictionary())); |
| 143 | } |
| 144 | |
| 145 | bool ResourceImporterDynamicFont::has_advanced_options() const { |
| 146 | return true; |
| 147 | } |
| 148 | void ResourceImporterDynamicFont::show_advanced_options(const String &p_path) { |
| 149 | DynamicFontImportSettingsDialog::get_singleton()->open_settings(p_path); |
| 150 | } |
| 151 | |
| 152 | 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) { |
| 153 | print_verbose("Importing dynamic font from: " + p_source_file); |
| 154 | |
| 155 | int antialiasing = p_options["antialiasing"]; |
| 156 | bool generate_mipmaps = p_options["generate_mipmaps"]; |
| 157 | bool disable_embedded_bitmaps = p_options["disable_embedded_bitmaps"]; |
| 158 | bool msdf = p_options["multichannel_signed_distance_field"]; |
| 159 | int px_range = p_options["msdf_pixel_range"]; |
| 160 | int px_size = p_options["msdf_size"]; |
| 161 | Dictionary ot_ov = p_options["opentype_features"]; |
| 162 | |
| 163 | bool autohinter = p_options["force_autohinter"]; |
| 164 | bool modulate_color_glyphs = p_options["modulate_color_glyphs"]; |
| 165 | bool allow_system_fallback = p_options["allow_system_fallback"]; |
| 166 | int hinting = p_options["hinting"]; |
| 167 | int subpixel_positioning = p_options["subpixel_positioning"]; |
| 168 | bool keep_rounding_remainders = p_options["keep_rounding_remainders"]; |
| 169 | real_t oversampling = p_options["oversampling"]; |
| 170 | Array fallbacks = p_options["fallbacks"]; |
| 171 | |
| 172 | // Load base font data. |
| 173 | Vector<uint8_t> data = FileAccess::get_file_as_bytes(p_source_file); |
| 174 | |
| 175 | // Create font. |
| 176 | Ref<FontFile> font; |
| 177 | font.instantiate(); |
| 178 | font->set_data(data); |
| 179 | font->set_antialiasing((TextServer::FontAntialiasing)antialiasing); |
| 180 | font->set_disable_embedded_bitmaps(disable_embedded_bitmaps); |
| 181 | font->set_generate_mipmaps(generate_mipmaps); |
| 182 | font->set_multichannel_signed_distance_field(msdf); |
| 183 | font->set_msdf_pixel_range(px_range); |
| 184 | font->set_msdf_size(px_size); |
| 185 | font->set_opentype_feature_overrides(ot_ov); |
| 186 | font->set_fixed_size(0); |
| 187 | font->set_force_autohinter(autohinter); |
| 188 | font->set_modulate_color_glyphs(modulate_color_glyphs); |
| 189 | font->set_allow_system_fallback(allow_system_fallback); |
| 190 | font->set_hinting((TextServer::Hinting)hinting); |
| 191 | font->set_oversampling(oversampling); |
| 192 | font->set_fallbacks(fallbacks); |
| 193 | |
| 194 | if (subpixel_positioning == 4 /* Auto (Except Pixel Fonts) */) { |
| 195 | Array rids = font->get_rids(); |
| 196 | if (!rids.is_empty()) { |
| 197 | PackedInt32Array glyphs = TS->font_get_supported_glyphs(rids[0]); |
| 198 | bool is_pixel = true; |
| 199 | for (int32_t gl : glyphs) { |
| 200 | Dictionary ct = TS->font_get_glyph_contours(rids[0], 16, gl); |
| 201 | PackedInt32Array contours = ct["contours"]; |
| 202 | PackedVector3Array points = ct["points"]; |
| 203 | int prev_start = 0; |
| 204 | for (int i = 0; i < contours.size(); i++) { |
| 205 | for (int j = prev_start; j <= contours[i]; j++) { |
| 206 | int next_point = (j < contours[i]) ? (j + 1) : prev_start; |
| 207 | if ((points[j].z != (real_t)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))) { |
| 208 | is_pixel = false; |
| 209 | break; |
| 210 | } |
| 211 | } |
| 212 | prev_start = contours[i] + 1; |
| 213 | if (!is_pixel) { |
| 214 | break; |
| 215 | } |
| 216 | } |
| 217 | if (!is_pixel) { |
| 218 | break; |
| 219 | } |
| 220 | } |
| 221 | if (is_pixel && !glyphs.is_empty()) { |
| 222 | print_line(vformat("%s: Pixel font detected, disabling subpixel positioning.", p_source_file)); |
| 223 | subpixel_positioning = TextServer::SUBPIXEL_POSITIONING_DISABLED; |
| 224 | } else { |
| 225 | subpixel_positioning = TextServer::SUBPIXEL_POSITIONING_AUTO; |
| 226 | } |
| 227 | } |
| 228 | } |
| 229 | font->set_subpixel_positioning((TextServer::SubpixelPositioning)subpixel_positioning); |
| 230 | font->set_keep_rounding_remainders(keep_rounding_remainders); |
| 231 | |
| 232 | Dictionary langs = p_options["language_support"]; |
| 233 | for (const KeyValue<Variant, Variant> &kv : langs) { |
| 234 | String key = kv.key; |
| 235 | bool enabled = kv.value; |
| 236 | font->set_language_support_override(key, enabled); |
| 237 | } |
| 238 | |
| 239 | Dictionary scripts = p_options["script_support"]; |
| 240 | for (const KeyValue<Variant, Variant> &kv : scripts) { |
| 241 | String key = kv.key; |
| 242 | bool enabled = kv.value; |
| 243 | font->set_script_support_override(key, enabled); |
| 244 | } |
| 245 | |
| 246 | Array preload_configurations = p_options["preload"]; |
| 247 | |
| 248 | for (int i = 0; i < preload_configurations.size(); i++) { |
| 249 | Dictionary preload_config = preload_configurations[i]; |
| 250 | |
| 251 | Dictionary variation = preload_config.has("variation_opentype") ? preload_config["variation_opentype"].operator Dictionary() : Dictionary(); |
| 252 | double embolden = preload_config.has("variation_embolden") ? preload_config["variation_embolden"].operator double() : 0; |
| 253 | int face_index = preload_config.has("variation_face_index") ? preload_config["variation_face_index"].operator int() : 0; |
| 254 | Transform2D transform = preload_config.has("variation_transform") ? preload_config["variation_transform"].operator Transform2D() : Transform2D(); |
| 255 | Vector2i size = preload_config.has("size") ? preload_config["size"].operator Vector2i() : Vector2i(16, 0); |
| 256 | String name = preload_config.has("name") ? preload_config["name"].operator String() : vformat("Configuration %d", i); |
| 257 | |
| 258 | RID conf_rid = font->find_variation(variation, face_index, embolden, transform); |
| 259 | |
| 260 | Array chars = preload_config["chars"]; |
| 261 | for (int j = 0; j < chars.size(); j++) { |
| 262 | char32_t c = chars[j].operator int(); |
| 263 | TS->font_render_range(conf_rid, size, c, c); |
| 264 | } |
| 265 | |
| 266 | Array glyphs = preload_config["glyphs"]; |
| 267 | for (int j = 0; j < glyphs.size(); j++) { |
| 268 | int32_t c = glyphs[j]; |
| 269 | TS->font_render_glyph(conf_rid, size, c); |
| 270 | } |
| 271 | } |
| 272 | |
| 273 | int flg = 0; |
| 274 | if ((bool)p_options["compress"]) { |
| 275 | flg |= ResourceSaver::SaverFlags::FLAG_COMPRESS; |
| 276 | } |
| 277 | |
| 278 | print_verbose("Saving to: " + p_save_path + ".fontdata"); |
| 279 | Error err = ResourceSaver::save(font, p_save_path + ".fontdata", flg); |
| 280 | ERR_FAIL_COND_V_MSG(err != OK, err, "Cannot save font to file \"" + p_save_path + ".res\"."); |
| 281 | print_verbose("Done saving to: " + p_save_path + ".fontdata"); |
| 282 | return OK; |
| 283 | } |