1 | /**************************************************************************/ |
2 | /* resource_importer_imagefont.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_imagefont.h" |
32 | |
33 | #include "core/io/image_loader.h" |
34 | #include "core/io/resource_saver.h" |
35 | |
36 | String ResourceImporterImageFont::get_importer_name() const { |
37 | return "font_data_image" ; |
38 | } |
39 | |
40 | String ResourceImporterImageFont::get_visible_name() const { |
41 | return "Font Data (Monospace Image Font)" ; |
42 | } |
43 | |
44 | void ResourceImporterImageFont::get_recognized_extensions(List<String> *p_extensions) const { |
45 | if (p_extensions) { |
46 | ImageLoader::get_recognized_extensions(p_extensions); |
47 | } |
48 | } |
49 | |
50 | String ResourceImporterImageFont::get_save_extension() const { |
51 | return "fontdata" ; |
52 | } |
53 | |
54 | String ResourceImporterImageFont::get_resource_type() const { |
55 | return "FontFile" ; |
56 | } |
57 | |
58 | bool ResourceImporterImageFont::get_option_visibility(const String &p_path, const String &p_option, const HashMap<StringName, Variant> &p_options) const { |
59 | return true; |
60 | } |
61 | |
62 | void ResourceImporterImageFont::get_import_options(const String &p_path, List<ImportOption> *r_options, int p_preset) const { |
63 | r_options->push_back(ImportOption(PropertyInfo(Variant::PACKED_STRING_ARRAY, "character_ranges" ), Vector<String>())); |
64 | r_options->push_back(ImportOption(PropertyInfo(Variant::INT, "columns" ), 1)); |
65 | r_options->push_back(ImportOption(PropertyInfo(Variant::INT, "rows" ), 1)); |
66 | r_options->push_back(ImportOption(PropertyInfo(Variant::RECT2I, "image_margin" ), Rect2i())); |
67 | r_options->push_back(ImportOption(PropertyInfo(Variant::RECT2I, "character_margin" ), Rect2i())); |
68 | |
69 | r_options->push_back(ImportOption(PropertyInfo(Variant::ARRAY, "fallbacks" , PROPERTY_HINT_ARRAY_TYPE, MAKE_RESOURCE_TYPE_HINT("Font" )), Array())); |
70 | |
71 | r_options->push_back(ImportOption(PropertyInfo(Variant::BOOL, "compress" ), true)); |
72 | } |
73 | |
74 | bool ResourceImporterImageFont::_decode_range(const String &p_token, int32_t &r_pos) { |
75 | if (p_token.begins_with("U+" ) || p_token.begins_with("u+" ) || p_token.begins_with("0x" )) { |
76 | // Unicode character hex index. |
77 | r_pos = p_token.substr(2).hex_to_int(); |
78 | return true; |
79 | } else if (p_token.length() == 3 && p_token[0] == '\'' && p_token[2] == '\'') { |
80 | // Unicode character. |
81 | r_pos = p_token.unicode_at(1); |
82 | return true; |
83 | } else if (p_token.is_numeric()) { |
84 | // Unicode character decimal index. |
85 | r_pos = p_token.to_int(); |
86 | return true; |
87 | } else { |
88 | return false; |
89 | } |
90 | } |
91 | |
92 | Error ResourceImporterImageFont::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) { |
93 | print_verbose("Importing image font from: " + p_source_file); |
94 | |
95 | int columns = p_options["columns" ]; |
96 | int rows = p_options["rows" ]; |
97 | Vector<String> ranges = p_options["character_ranges" ]; |
98 | Array fallbacks = p_options["fallbacks" ]; |
99 | Rect2i img_margin = p_options["image_margin" ]; |
100 | Rect2i char_margin = p_options["character_margin" ]; |
101 | |
102 | Ref<Image> img; |
103 | img.instantiate(); |
104 | Error err = ImageLoader::load_image(p_source_file, img); |
105 | ERR_FAIL_COND_V_MSG(err != OK, ERR_FILE_CANT_READ, TTR("Can't load font texture:" ) + " \"" + p_source_file + "\"." ); |
106 | |
107 | int count = columns * rows; |
108 | int chr_cell_width = (img->get_width() - img_margin.position.x - img_margin.size.x) / columns; |
109 | int chr_cell_height = (img->get_height() - img_margin.position.y - img_margin.size.y) / rows; |
110 | ERR_FAIL_COND_V_MSG(chr_cell_width <= 0 || chr_cell_height <= 0, ERR_FILE_CANT_READ, TTR("Image margin too big." )); |
111 | |
112 | int chr_width = chr_cell_width - char_margin.position.x - char_margin.size.x; |
113 | int chr_height = chr_cell_height - char_margin.position.y - char_margin.size.y; |
114 | ERR_FAIL_COND_V_MSG(chr_width <= 0 || chr_height <= 0, ERR_FILE_CANT_READ, TTR("Character margin too big." )); |
115 | |
116 | Ref<FontFile> font; |
117 | font.instantiate(); |
118 | font->set_antialiasing(TextServer::FONT_ANTIALIASING_NONE); |
119 | font->set_generate_mipmaps(false); |
120 | font->set_multichannel_signed_distance_field(false); |
121 | font->set_fixed_size(chr_height); |
122 | font->set_subpixel_positioning(TextServer::SUBPIXEL_POSITIONING_DISABLED); |
123 | font->set_force_autohinter(false); |
124 | font->set_allow_system_fallback(false); |
125 | font->set_hinting(TextServer::HINTING_NONE); |
126 | font->set_oversampling(1.0f); |
127 | font->set_fallbacks(fallbacks); |
128 | font->set_texture_image(0, Vector2i(chr_height, 0), 0, img); |
129 | |
130 | int pos = 0; |
131 | for (int i = 0; i < ranges.size(); i++) { |
132 | int32_t start, end; |
133 | Vector<String> tokens = ranges[i].split("-" ); |
134 | if (tokens.size() == 2) { |
135 | if (!_decode_range(tokens[0], start) || !_decode_range(tokens[1], end)) { |
136 | WARN_PRINT("Invalid range: \"" + ranges[i] + "\"" ); |
137 | continue; |
138 | } |
139 | } else if (tokens.size() == 1) { |
140 | if (!_decode_range(tokens[0], start)) { |
141 | WARN_PRINT("Invalid range: \"" + ranges[i] + "\"" ); |
142 | continue; |
143 | } |
144 | end = start; |
145 | } else { |
146 | WARN_PRINT("Invalid range: \"" + ranges[i] + "\"" ); |
147 | continue; |
148 | } |
149 | for (int32_t idx = start; idx <= end; idx++) { |
150 | ERR_FAIL_COND_V_MSG(pos >= count, ERR_CANT_CREATE, "Too many characters in range, should be " + itos(columns * rows)); |
151 | int x = pos % columns; |
152 | int y = pos / columns; |
153 | font->set_glyph_advance(0, chr_height, idx, Vector2(chr_width, 0)); |
154 | font->set_glyph_offset(0, Vector2i(chr_height, 0), idx, Vector2i(0, -0.5 * chr_height)); |
155 | font->set_glyph_size(0, Vector2i(chr_height, 0), idx, Vector2(chr_width, chr_height)); |
156 | font->set_glyph_uv_rect(0, Vector2i(chr_height, 0), idx, Rect2(img_margin.position.x + chr_cell_width * x + char_margin.position.x, img_margin.position.y + chr_cell_height * y + char_margin.position.y, chr_width, chr_height)); |
157 | font->set_glyph_texture_idx(0, Vector2i(chr_height, 0), idx, 0); |
158 | pos++; |
159 | } |
160 | } |
161 | font->set_cache_ascent(0, chr_height, 0.5 * chr_height); |
162 | font->set_cache_descent(0, chr_height, 0.5 * chr_height); |
163 | |
164 | int flg = 0; |
165 | if ((bool)p_options["compress" ]) { |
166 | flg |= ResourceSaver::SaverFlags::FLAG_COMPRESS; |
167 | } |
168 | |
169 | print_verbose("Saving to: " + p_save_path + ".fontdata" ); |
170 | err = ResourceSaver::save(font, p_save_path + ".fontdata" , flg); |
171 | ERR_FAIL_COND_V_MSG(err != OK, err, "Cannot save font to file \"" + p_save_path + ".res\"." ); |
172 | print_verbose("Done saving to: " + p_save_path + ".fontdata" ); |
173 | return OK; |
174 | } |
175 | |
176 | ResourceImporterImageFont::ResourceImporterImageFont() { |
177 | } |
178 | |