From a319e6e6232f3e6b9056bd26822f5aa3738b6cd6 Mon Sep 17 00:00:00 2001 From: bruvzg <7645683+bruvzg@users.noreply.github.com> Date: Thu, 7 Jul 2022 15:11:08 +0300 Subject: [PATCH] Fix regressions from Font refactor Fixes `p_font.is_null()` errors due to incorrect resource type used. Fixes code editor font not set correctly, and OpenType features applied to the wrong font. --- editor/code_editor.cpp | 108 +++++++++---------- editor/editor_fonts.cpp | 63 +++++++---- editor/plugins/canvas_item_editor_plugin.cpp | 2 +- scene/gui/rich_text_label.cpp | 4 +- 4 files changed, 95 insertions(+), 82 deletions(-) diff --git a/editor/code_editor.cpp b/editor/code_editor.cpp index 73059464bec..a1979c76191 100644 --- a/editor/code_editor.cpp +++ b/editor/code_editor.cpp @@ -1638,37 +1638,34 @@ void CodeTextEditor::_apply_settings_change() { font_size = EditorSettings::get_singleton()->get("interface/editor/code_font_size"); int ot_mode = EditorSettings::get_singleton()->get("interface/editor/code_font_contextual_ligatures"); - Ref fb = text_editor->get_theme_font(SNAME("font")); - Ref fc = fb; - if (fc.is_null()) { - fc.instantiate(); - fc->set_base_font(fb); - } - - switch (ot_mode) { - case 1: { // Disable ligatures. - fc->set_opentype_features(Dictionary()); - } break; - case 2: { // Custom. - Vector subtag = String(EditorSettings::get_singleton()->get("interface/editor/code_font_custom_opentype_features")).split(","); - Dictionary ftrs; - for (int i = 0; i < subtag.size(); i++) { - Vector subtag_a = subtag[i].split("="); - if (subtag_a.size() == 2) { - ftrs[TS->name_to_tag(subtag_a[0])] = subtag_a[1].to_int(); - } else if (subtag_a.size() == 1) { - ftrs[TS->name_to_tag(subtag_a[0])] = 1; + Ref fc = text_editor->get_theme_font(SNAME("font")); + if (fc.is_valid()) { + switch (ot_mode) { + case 1: { // Disable ligatures. + Dictionary ftrs; + ftrs[TS->name_to_tag("calt")] = 0; + fc->set_opentype_features(ftrs); + } break; + case 2: { // Custom. + Vector subtag = String(EditorSettings::get_singleton()->get("interface/editor/code_font_custom_opentype_features")).split(","); + Dictionary ftrs; + for (int i = 0; i < subtag.size(); i++) { + Vector subtag_a = subtag[i].split("="); + if (subtag_a.size() == 2) { + ftrs[TS->name_to_tag(subtag_a[0])] = subtag_a[1].to_int(); + } else if (subtag_a.size() == 1) { + ftrs[TS->name_to_tag(subtag_a[0])] = 1; + } } - } - fc->set_opentype_features(ftrs); - } break; - default: { // Default. - Dictionary ftrs; - ftrs[TS->name_to_tag("calt")] = 1; - fc->set_opentype_features(ftrs); - } break; + fc->set_opentype_features(ftrs); + } break; + default: { // Default. + Dictionary ftrs; + ftrs[TS->name_to_tag("calt")] = 1; + fc->set_opentype_features(ftrs); + } break; + } } - text_editor->add_theme_font_override("font", fc); text_editor->set_code_hint_draw_below(EDITOR_GET("text_editor/completion/put_callhint_tooltip_below_current_line")); @@ -1870,34 +1867,33 @@ CodeTextEditor::CodeTextEditor() { text_editor->set_v_size_flags(SIZE_EXPAND_FILL); int ot_mode = EditorSettings::get_singleton()->get("interface/editor/code_font_contextual_ligatures"); - Ref fb = text_editor->get_theme_font(SNAME("font")); - Ref fc = fb; - if (fc.is_null()) { - fc.instantiate(); - fc->set_base_font(fb); - } - switch (ot_mode) { - case 1: { // Disable ligatures. - fc->set_opentype_features(Dictionary()); - } break; - case 2: { // Custom. - Vector subtag = String(EditorSettings::get_singleton()->get("interface/editor/code_font_custom_opentype_features")).split(","); - Dictionary ftrs; - for (int i = 0; i < subtag.size(); i++) { - Vector subtag_a = subtag[i].split("="); - if (subtag_a.size() == 2) { - ftrs[TS->name_to_tag(subtag_a[0])] = subtag_a[1].to_int(); - } else if (subtag_a.size() == 1) { - ftrs[TS->name_to_tag(subtag_a[0])] = 1; + Ref fc = text_editor->get_theme_font(SNAME("font")); + if (fc.is_valid()) { + switch (ot_mode) { + case 1: { // Disable ligatures. + Dictionary ftrs; + ftrs[TS->name_to_tag("calt")] = 0; + fc->set_opentype_features(ftrs); + } break; + case 2: { // Custom. + Vector subtag = String(EditorSettings::get_singleton()->get("interface/editor/code_font_custom_opentype_features")).split(","); + Dictionary ftrs; + for (int i = 0; i < subtag.size(); i++) { + Vector subtag_a = subtag[i].split("="); + if (subtag_a.size() == 2) { + ftrs[TS->name_to_tag(subtag_a[0])] = subtag_a[1].to_int(); + } else if (subtag_a.size() == 1) { + ftrs[TS->name_to_tag(subtag_a[0])] = 1; + } } - } - fc->set_opentype_features(ftrs); - } break; - default: { // Default. - Dictionary ftrs; - ftrs[TS->name_to_tag("calt")] = 1; - fc->set_opentype_features(ftrs); - } break; + fc->set_opentype_features(ftrs); + } break; + default: { // Default. + Dictionary ftrs; + ftrs[TS->name_to_tag("calt")] = 1; + fc->set_opentype_features(ftrs); + } break; + } } text_editor->add_theme_font_override("font", fc); diff --git a/editor/editor_fonts.cpp b/editor/editor_fonts.cpp index 8deee57dc93..d58dc98f07b 100644 --- a/editor/editor_fonts.cpp +++ b/editor/editor_fonts.cpp @@ -164,11 +164,6 @@ void editor_register_fonts(Ref p_theme) { default_font_bold_msdf->set_fallbacks(fallbacks_bold); Ref default_font_mono = load_internal_font(_font_JetBrainsMono_Regular, _font_JetBrainsMono_Regular_size, font_hinting, font_antialiased, true, font_subpixel_positioning); - { - Dictionary opentype_features_mono; - opentype_features_mono["calt"] = 0; - default_font_mono->set_opentype_feature_overrides(opentype_features_mono); // Disable contextual alternates (coding ligatures). - } default_font_mono->set_fallbacks(fallbacks); // Init base font configs and load custom fonts. @@ -276,24 +271,46 @@ void editor_register_fonts(Ref p_theme) { EditorSettings::get_singleton()->set_manually("interface/editor/code_font", ""); mono_fc->set_base_font(default_font_mono); } - - String code_font_custom_variations = EditorSettings::get_singleton()->get("interface/editor/code_font_custom_variations"); - Dictionary variations_mono; - if (!code_font_custom_variations.is_empty()) { - Vector variation_tags = code_font_custom_variations.split(","); - for (int i = 0; i < variation_tags.size(); i++) { - Vector subtag_a = variation_tags[i].split("="); - if (subtag_a.size() == 2) { - variations_mono[TS->name_to_tag(subtag_a[0])] = subtag_a[1].to_float(); - } else if (subtag_a.size() == 1) { - variations_mono[TS->name_to_tag(subtag_a[0])] = 1; - } - } - mono_fc->set_variation_opentype(variations_mono); - } mono_fc->set_spacing(TextServer::SPACING_TOP, -EDSCALE); mono_fc->set_spacing(TextServer::SPACING_BOTTOM, -EDSCALE); + Ref mono_other_fc = mono_fc->duplicate(); + + // Enable contextual alternates (coding ligatures) and custom features for the source editor font. + int ot_mode = EditorSettings::get_singleton()->get("interface/editor/code_font_contextual_ligatures"); + switch (ot_mode) { + case 1: { // Disable ligatures. + Dictionary ftrs; + ftrs[TS->name_to_tag("calt")] = 0; + mono_fc->set_opentype_features(ftrs); + } break; + case 2: { // Custom. + Vector subtag = String(EditorSettings::get_singleton()->get("interface/editor/code_font_custom_opentype_features")).split(","); + Dictionary ftrs; + for (int i = 0; i < subtag.size(); i++) { + Vector subtag_a = subtag[i].split("="); + if (subtag_a.size() == 2) { + ftrs[TS->name_to_tag(subtag_a[0])] = subtag_a[1].to_int(); + } else if (subtag_a.size() == 1) { + ftrs[TS->name_to_tag(subtag_a[0])] = 1; + } + } + mono_fc->set_opentype_features(ftrs); + } break; + default: { // Default. + Dictionary ftrs; + ftrs[TS->name_to_tag("calt")] = 1; + mono_fc->set_opentype_features(ftrs); + } break; + } + + { + // Disable contextual alternates (coding ligatures). + Dictionary ftrs; + ftrs[TS->name_to_tag("calt")] = 0; + mono_other_fc->set_opentype_features(ftrs); + } + Ref italic_fc = default_fc->duplicate(); italic_fc->set_variation_transform(Transform2D(1.0, 0.2, 0.0, 1.0, 0.0, 0.0)); @@ -359,11 +376,11 @@ void editor_register_fonts(Ref p_theme) { p_theme->set_font("source", "EditorFonts", mono_fc); p_theme->set_font_size("expression_size", "EditorFonts", (int(EDITOR_GET("interface/editor/code_font_size")) - 1) * EDSCALE); - p_theme->set_font("expression", "EditorFonts", mono_fc); + p_theme->set_font("expression", "EditorFonts", mono_other_fc); p_theme->set_font_size("output_source_size", "EditorFonts", int(EDITOR_GET("run/output/font_size")) * EDSCALE); - p_theme->set_font("output_source", "EditorFonts", mono_fc); + p_theme->set_font("output_source", "EditorFonts", mono_other_fc); p_theme->set_font_size("status_source_size", "EditorFonts", default_font_size); - p_theme->set_font("status_source", "EditorFonts", mono_fc); + p_theme->set_font("status_source", "EditorFonts", mono_other_fc); } diff --git a/editor/plugins/canvas_item_editor_plugin.cpp b/editor/plugins/canvas_item_editor_plugin.cpp index 9ff32203f74..7e525a46982 100644 --- a/editor/plugins/canvas_item_editor_plugin.cpp +++ b/editor/plugins/canvas_item_editor_plugin.cpp @@ -3697,7 +3697,7 @@ void CanvasItemEditor::_draw_transform_message() { return; } - Ref font = get_theme_font(SNAME("font"), SNAME("Label")); + Ref font = get_theme_font(SNAME("font"), SNAME("Label")); int font_size = get_theme_font_size(SNAME("font_size"), SNAME("Label")); Point2 msgpos = Point2(RULER_WIDTH + 5 * EDSCALE, viewport->get_size().y - 20 * EDSCALE); viewport->draw_string(font, msgpos + Point2(1, 1), transform_message, HORIZONTAL_ALIGNMENT_LEFT, -1, font_size, Color(0, 0, 0, 0.8)); diff --git a/scene/gui/rich_text_label.cpp b/scene/gui/rich_text_label.cpp index 2441279797e..05824d54f17 100644 --- a/scene/gui/rich_text_label.cpp +++ b/scene/gui/rich_text_label.cpp @@ -2081,7 +2081,7 @@ Ref RichTextLabel::_find_font(Item *p_item) { fontitem = fontitem->parent; } - return Ref(); + return Ref(); } int RichTextLabel::_find_font_size(Item *p_item) { @@ -4002,7 +4002,7 @@ void RichTextLabel::append_text(const String &p_bbcode) { if (subtag_a.size() == 2) { if (subtag_a[0] == "name" || subtag_a[0] == "n") { String fnt = subtag_a[1]; - Ref font_data = ResourceLoader::load(fnt, "FontFile"); + Ref font_data = ResourceLoader::load(fnt, "Font"); if (font_data.is_valid()) { fc->set_base_font(font_data); }