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