|
@@ -61,10 +61,13 @@ bool ResourceImporterImageFont::get_option_visibility(const String &p_path, cons
|
|
|
|
|
|
void ResourceImporterImageFont::get_import_options(const String &p_path, List<ImportOption> *r_options, int p_preset) const {
|
|
|
r_options->push_back(ImportOption(PropertyInfo(Variant::PACKED_STRING_ARRAY, "character_ranges"), Vector<String>()));
|
|
|
+ r_options->push_back(ImportOption(PropertyInfo(Variant::PACKED_STRING_ARRAY, "kerning_pairs"), Vector<String>()));
|
|
|
r_options->push_back(ImportOption(PropertyInfo(Variant::INT, "columns"), 1));
|
|
|
r_options->push_back(ImportOption(PropertyInfo(Variant::INT, "rows"), 1));
|
|
|
r_options->push_back(ImportOption(PropertyInfo(Variant::RECT2I, "image_margin"), Rect2i()));
|
|
|
r_options->push_back(ImportOption(PropertyInfo(Variant::RECT2I, "character_margin"), Rect2i()));
|
|
|
+ r_options->push_back(ImportOption(PropertyInfo(Variant::INT, "ascent"), 0));
|
|
|
+ r_options->push_back(ImportOption(PropertyInfo(Variant::INT, "descent"), 0));
|
|
|
|
|
|
r_options->push_back(ImportOption(PropertyInfo(Variant::ARRAY, "fallbacks", PROPERTY_HINT_ARRAY_TYPE, MAKE_RESOURCE_TYPE_HINT("Font")), Array()));
|
|
|
|
|
@@ -72,30 +75,15 @@ void ResourceImporterImageFont::get_import_options(const String &p_path, List<Im
|
|
|
r_options->push_back(ImportOption(PropertyInfo(Variant::INT, "scaling_mode", PROPERTY_HINT_ENUM, "Disabled,Enabled (Integer),Enabled (Fractional)"), TextServer::FIXED_SIZE_SCALE_ENABLED));
|
|
|
}
|
|
|
|
|
|
-bool ResourceImporterImageFont::_decode_range(const String &p_token, int32_t &r_pos) {
|
|
|
- if (p_token.begins_with("U+") || p_token.begins_with("u+") || p_token.begins_with("0x")) {
|
|
|
- // Unicode character hex index.
|
|
|
- r_pos = p_token.substr(2).hex_to_int();
|
|
|
- return true;
|
|
|
- } else if (p_token.length() == 3 && p_token[0] == '\'' && p_token[2] == '\'') {
|
|
|
- // Unicode character.
|
|
|
- r_pos = p_token.unicode_at(1);
|
|
|
- return true;
|
|
|
- } else if (p_token.is_numeric()) {
|
|
|
- // Unicode character decimal index.
|
|
|
- r_pos = p_token.to_int();
|
|
|
- return true;
|
|
|
- } else {
|
|
|
- return false;
|
|
|
- }
|
|
|
-}
|
|
|
-
|
|
|
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) {
|
|
|
print_verbose("Importing image font from: " + p_source_file);
|
|
|
|
|
|
int columns = p_options["columns"];
|
|
|
int rows = p_options["rows"];
|
|
|
+ int ascent = p_options["ascent"];
|
|
|
+ int descent = p_options["descent"];
|
|
|
Vector<String> ranges = p_options["character_ranges"];
|
|
|
+ Vector<String> kern = p_options["kerning_pairs"];
|
|
|
Array fallbacks = p_options["fallbacks"];
|
|
|
Rect2i img_margin = p_options["image_margin"];
|
|
|
Rect2i char_margin = p_options["character_margin"];
|
|
@@ -130,39 +118,186 @@ Error ResourceImporterImageFont::import(const String &p_source_file, const Strin
|
|
|
font->set_texture_image(0, Vector2i(chr_height, 0), 0, img);
|
|
|
font->set_fixed_size_scale_mode(smode);
|
|
|
|
|
|
- int pos = 0;
|
|
|
- for (int i = 0; i < ranges.size(); i++) {
|
|
|
- int32_t start, end;
|
|
|
- Vector<String> tokens = ranges[i].split("-");
|
|
|
- if (tokens.size() == 2) {
|
|
|
- if (!_decode_range(tokens[0], start) || !_decode_range(tokens[1], end)) {
|
|
|
- WARN_PRINT("Invalid range: \"" + ranges[i] + "\"");
|
|
|
- continue;
|
|
|
+ int32_t pos = 0;
|
|
|
+ for (const String &range : ranges) {
|
|
|
+ int32_t start = -1;
|
|
|
+ int32_t end = -1;
|
|
|
+ int chr_adv = 0;
|
|
|
+ Vector2i chr_off;
|
|
|
+
|
|
|
+ {
|
|
|
+ enum RangeParseStep {
|
|
|
+ STEP_START_BEGIN,
|
|
|
+ STEP_START_READ_HEX,
|
|
|
+ STEP_START_READ_DEC,
|
|
|
+ STEP_END_BEGIN,
|
|
|
+ STEP_END_READ_HEX,
|
|
|
+ STEP_END_READ_DEC,
|
|
|
+ STEP_ADVANCE_BEGIN,
|
|
|
+ STEP_OFF_X_BEGIN,
|
|
|
+ STEP_OFF_Y_BEGIN,
|
|
|
+ STEP_FINISHED,
|
|
|
+ };
|
|
|
+ RangeParseStep step = STEP_START_BEGIN;
|
|
|
+ String token;
|
|
|
+ for (int c = 0; c < range.length(); c++) {
|
|
|
+ switch (step) {
|
|
|
+ case STEP_START_BEGIN:
|
|
|
+ case STEP_END_BEGIN: {
|
|
|
+ // Read range start/end first symbol.
|
|
|
+ if (range[c] == 'U' || range[c] == 'u') {
|
|
|
+ if ((c <= range.length() - 2) && range[c + 1] == '+') {
|
|
|
+ token = String();
|
|
|
+ if (step == STEP_START_BEGIN) {
|
|
|
+ step = STEP_START_READ_HEX;
|
|
|
+ } else {
|
|
|
+ step = STEP_END_READ_HEX;
|
|
|
+ }
|
|
|
+ c++; // Skip "+".
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+ } else if (range[c] == '0') {
|
|
|
+ if ((c <= range.length() - 2) && range[c + 1] == 'x') {
|
|
|
+ token = String();
|
|
|
+ if (step == STEP_START_BEGIN) {
|
|
|
+ step = STEP_START_READ_HEX;
|
|
|
+ } else {
|
|
|
+ step = STEP_END_READ_HEX;
|
|
|
+ }
|
|
|
+ c++; // Skip "x".
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+ } else if (range[c] == '\'' || range[c] == '\"') {
|
|
|
+ if ((c <= range.length() - 3) && (range[c + 2] == '\'' || range[c + 2] == '\"')) {
|
|
|
+ token = String();
|
|
|
+ if (step == STEP_START_BEGIN) {
|
|
|
+ start = range.unicode_at(c + 1);
|
|
|
+ step = STEP_END_BEGIN;
|
|
|
+ } else {
|
|
|
+ end = range.unicode_at(c + 1);
|
|
|
+ step = STEP_ADVANCE_BEGIN;
|
|
|
+ }
|
|
|
+ c = c + 2; // Skip the rest or token.
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+ } else if (is_digit(range[c])) {
|
|
|
+ // Read decimal value, start.
|
|
|
+ c++;
|
|
|
+ token = String();
|
|
|
+ if (step == STEP_START_BEGIN) {
|
|
|
+ step = STEP_START_READ_DEC;
|
|
|
+ } else {
|
|
|
+ step = STEP_END_READ_DEC;
|
|
|
+ }
|
|
|
+ token += range[c];
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+ [[fallthrough]];
|
|
|
+ }
|
|
|
+ case STEP_ADVANCE_BEGIN:
|
|
|
+ case STEP_OFF_X_BEGIN:
|
|
|
+ case STEP_OFF_Y_BEGIN: {
|
|
|
+ // Read advance and offset.
|
|
|
+ if (range[c] == ' ') {
|
|
|
+ int next = range.find(" ", c + 1);
|
|
|
+ if (next < c) {
|
|
|
+ next = range.length();
|
|
|
+ }
|
|
|
+ if (step == STEP_OFF_X_BEGIN) {
|
|
|
+ chr_off.x = range.substr(c + 1, next - (c + 1)).to_int();
|
|
|
+ step = STEP_OFF_Y_BEGIN;
|
|
|
+ } else if (step == STEP_OFF_Y_BEGIN) {
|
|
|
+ chr_off.y = range.substr(c + 1, next - (c + 1)).to_int();
|
|
|
+ step = STEP_FINISHED;
|
|
|
+ } else {
|
|
|
+ chr_adv = range.substr(c + 1, next - (c + 1)).to_int();
|
|
|
+ step = STEP_OFF_X_BEGIN;
|
|
|
+ }
|
|
|
+ c = next - 1;
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+ } break;
|
|
|
+ case STEP_START_READ_HEX:
|
|
|
+ case STEP_END_READ_HEX: {
|
|
|
+ // Read hexadecimal value.
|
|
|
+ if (is_hex_digit(range[c])) {
|
|
|
+ token += range[c];
|
|
|
+ } else {
|
|
|
+ if (step == STEP_START_READ_HEX) {
|
|
|
+ start = token.hex_to_int();
|
|
|
+ step = STEP_END_BEGIN;
|
|
|
+ } else {
|
|
|
+ end = token.hex_to_int();
|
|
|
+ step = STEP_ADVANCE_BEGIN;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ } break;
|
|
|
+ case STEP_START_READ_DEC:
|
|
|
+ case STEP_END_READ_DEC: {
|
|
|
+ // Read decimal value.
|
|
|
+ if (is_digit(range[c])) {
|
|
|
+ token += range[c];
|
|
|
+ } else {
|
|
|
+ if (step == STEP_START_READ_DEC) {
|
|
|
+ start = token.to_int();
|
|
|
+ step = STEP_END_BEGIN;
|
|
|
+ } else {
|
|
|
+ end = token.to_int();
|
|
|
+ step = STEP_ADVANCE_BEGIN;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ } break;
|
|
|
+ default: {
|
|
|
+ WARN_PRINT(vformat("Invalid character \"%d\" in the range: \"%s\"", c, range));
|
|
|
+ } break;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ if (end == -1) {
|
|
|
+ end = start;
|
|
|
}
|
|
|
- } else if (tokens.size() == 1) {
|
|
|
- if (!_decode_range(tokens[0], start)) {
|
|
|
- WARN_PRINT("Invalid range: \"" + ranges[i] + "\"");
|
|
|
+ if (start == -1) {
|
|
|
+ WARN_PRINT(vformat("Invalid range: \"%s\"", range));
|
|
|
continue;
|
|
|
}
|
|
|
- end = start;
|
|
|
- } else {
|
|
|
- WARN_PRINT("Invalid range: \"" + ranges[i] + "\"");
|
|
|
- continue;
|
|
|
}
|
|
|
- for (int32_t idx = start; idx <= end; idx++) {
|
|
|
+
|
|
|
+ for (int32_t idx = MIN(start, end); idx <= MAX(start, end); idx++) {
|
|
|
ERR_FAIL_COND_V_MSG(pos >= count, ERR_CANT_CREATE, "Too many characters in range, should be " + itos(columns * rows));
|
|
|
int x = pos % columns;
|
|
|
int y = pos / columns;
|
|
|
- font->set_glyph_advance(0, chr_height, idx, Vector2(chr_width, 0));
|
|
|
- font->set_glyph_offset(0, Vector2i(chr_height, 0), idx, Vector2i(0, -0.5 * chr_height));
|
|
|
+ font->set_glyph_advance(0, chr_height, idx, Vector2(chr_width + chr_adv, 0));
|
|
|
+ font->set_glyph_offset(0, Vector2i(chr_height, 0), idx, Vector2i(0, -0.5 * chr_height) + chr_off);
|
|
|
font->set_glyph_size(0, Vector2i(chr_height, 0), idx, Vector2(chr_width, chr_height));
|
|
|
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));
|
|
|
font->set_glyph_texture_idx(0, Vector2i(chr_height, 0), idx, 0);
|
|
|
pos++;
|
|
|
}
|
|
|
}
|
|
|
- font->set_cache_ascent(0, chr_height, 0.5 * chr_height);
|
|
|
- font->set_cache_descent(0, chr_height, 0.5 * chr_height);
|
|
|
+ for (const String &kp : kern) {
|
|
|
+ const Vector<String> &kp_tokens = kp.split(" ");
|
|
|
+ if (kp_tokens.size() != 3) {
|
|
|
+ WARN_PRINT(vformat("Invalid kerning pairs string: \"%s\"", kp));
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+ int offset = kp_tokens[2].to_int();
|
|
|
+ for (int a = 0; a < kp_tokens[0].length(); a++) {
|
|
|
+ for (int b = 0; b < kp_tokens[1].length(); b++) {
|
|
|
+ font->set_kerning(0, chr_height, Vector2i(kp_tokens[0].unicode_at(a), kp_tokens[1].unicode_at(b)), Vector2(offset, 0));
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ if (ascent > 0) {
|
|
|
+ font->set_cache_ascent(0, chr_height, ascent);
|
|
|
+ } else {
|
|
|
+ font->set_cache_ascent(0, chr_height, 0.5 * chr_height);
|
|
|
+ }
|
|
|
+
|
|
|
+ if (descent > 0) {
|
|
|
+ font->set_cache_descent(0, chr_height, descent);
|
|
|
+ } else {
|
|
|
+ font->set_cache_descent(0, chr_height, 0.5 * chr_height);
|
|
|
+ }
|
|
|
|
|
|
int flg = 0;
|
|
|
if ((bool)p_options["compress"]) {
|