/*************************************************************************/ /* editor_fonts.cpp */ /*************************************************************************/ /* This file is part of: */ /* GODOT ENGINE */ /* https://godotengine.org */ /*************************************************************************/ /* Copyright (c) 2007-2022 Juan Linietsky, Ariel Manzur. */ /* Copyright (c) 2014-2022 Godot Engine contributors (cf. AUTHORS.md). */ /* */ /* Permission is hereby granted, free of charge, to any person obtaining */ /* a copy of this software and associated documentation files (the */ /* "Software"), to deal in the Software without restriction, including */ /* without limitation the rights to use, copy, modify, merge, publish, */ /* distribute, sublicense, and/or sell copies of the Software, and to */ /* permit persons to whom the Software is furnished to do so, subject to */ /* the following conditions: */ /* */ /* The above copyright notice and this permission notice shall be */ /* included in all copies or substantial portions of the Software. */ /* */ /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */ /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */ /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/ /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */ /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */ /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */ /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ /*************************************************************************/ #include "editor_fonts.h" #include "builtin_fonts.gen.h" #include "core/io/dir_access.h" #include "editor/editor_scale.h" #include "editor/editor_settings.h" #include "scene/resources/default_theme/default_theme.h" #include "scene/resources/font.h" Ref load_external_font(const String &p_path, TextServer::Hinting p_hinting, TextServer::FontAntialiasing p_aa, bool p_autohint, TextServer::SubpixelPositioning p_font_subpixel_positioning, bool p_msdf = false, TypedArray *r_fallbacks = nullptr) { Ref font; font.instantiate(); Vector data = FileAccess::get_file_as_array(p_path); font->set_data(data); font->set_multichannel_signed_distance_field(p_msdf); font->set_antialiasing(p_aa); font->set_hinting(p_hinting); font->set_force_autohinter(p_autohint); font->set_subpixel_positioning(p_font_subpixel_positioning); if (r_fallbacks != nullptr) { r_fallbacks->push_back(font); } return font; } Ref load_internal_font(const uint8_t *p_data, size_t p_size, TextServer::Hinting p_hinting, TextServer::FontAntialiasing p_aa, bool p_autohint, TextServer::SubpixelPositioning p_font_subpixel_positioning, bool p_msdf = false, TypedArray *r_fallbacks = nullptr) { Ref font; font.instantiate(); font->set_data_ptr(p_data, p_size); font->set_multichannel_signed_distance_field(p_msdf); font->set_antialiasing(p_aa); font->set_hinting(p_hinting); font->set_force_autohinter(p_autohint); font->set_subpixel_positioning(p_font_subpixel_positioning); if (r_fallbacks != nullptr) { r_fallbacks->push_back(font); } return font; } Ref make_bold_font(const Ref &p_font, double p_embolden, TypedArray *r_fallbacks = nullptr) { Ref font_var; font_var.instantiate(); font_var->set_base_font(p_font); font_var->set_variation_embolden(p_embolden); if (r_fallbacks != nullptr) { r_fallbacks->push_back(font_var); } return font_var; } void editor_register_fonts(Ref p_theme) { Ref dir = DirAccess::create(DirAccess::ACCESS_FILESYSTEM); TextServer::FontAntialiasing font_antialiasing = (TextServer::FontAntialiasing)(int)EDITOR_GET("interface/editor/font_antialiasing"); int font_hinting_setting = (int)EDITOR_GET("interface/editor/font_hinting"); TextServer::SubpixelPositioning font_subpixel_positioning = (TextServer::SubpixelPositioning)(int)EDITOR_GET("interface/editor/font_subpixel_positioning"); TextServer::Hinting font_hinting; TextServer::Hinting font_mono_hinting; switch (font_hinting_setting) { case 0: // The "Auto" setting uses the setting that best matches the OS' font rendering: // - macOS doesn't use font hinting. // - Windows uses ClearType, which is in between "Light" and "Normal" hinting. // - Linux has configurable font hinting, but most distributions including Ubuntu default to "Light". #ifdef MACOS_ENABLED font_hinting = TextServer::HINTING_NONE; font_mono_hinting = TextServer::HINTING_NONE; #else font_hinting = TextServer::HINTING_LIGHT; font_mono_hinting = TextServer::HINTING_LIGHT; #endif break; case 1: font_hinting = TextServer::HINTING_NONE; font_mono_hinting = TextServer::HINTING_NONE; break; case 2: font_hinting = TextServer::HINTING_LIGHT; font_mono_hinting = TextServer::HINTING_LIGHT; break; default: font_hinting = TextServer::HINTING_NORMAL; font_mono_hinting = TextServer::HINTING_LIGHT; break; } // Load built-in fonts. const int default_font_size = int(EDITOR_GET("interface/editor/main_font_size")) * EDSCALE; const float embolden_strength = 0.6; Ref default_font = load_internal_font(_font_NotoSans_Regular, _font_NotoSans_Regular_size, font_hinting, font_antialiasing, true, font_subpixel_positioning, false); Ref default_font_msdf = load_internal_font(_font_NotoSans_Regular, _font_NotoSans_Regular_size, font_hinting, font_antialiasing, true, font_subpixel_positioning, true); TypedArray fallbacks; Ref arabic_font = load_internal_font(_font_NotoNaskhArabicUI_Regular, _font_NotoNaskhArabicUI_Regular_size, font_hinting, font_antialiasing, true, font_subpixel_positioning, false, &fallbacks); Ref bengali_font = load_internal_font(_font_NotoSansBengaliUI_Regular, _font_NotoSansBengaliUI_Regular_size, font_hinting, font_antialiasing, true, font_subpixel_positioning, false, &fallbacks); Ref devanagari_font = load_internal_font(_font_NotoSansDevanagariUI_Regular, _font_NotoSansDevanagariUI_Regular_size, font_hinting, font_antialiasing, true, font_subpixel_positioning, false, &fallbacks); Ref georgian_font = load_internal_font(_font_NotoSansGeorgian_Regular, _font_NotoSansGeorgian_Regular_size, font_hinting, font_antialiasing, true, font_subpixel_positioning, false, &fallbacks); Ref hebrew_font = load_internal_font(_font_NotoSansHebrew_Regular, _font_NotoSansHebrew_Regular_size, font_hinting, font_antialiasing, true, font_subpixel_positioning, false, &fallbacks); Ref malayalam_font = load_internal_font(_font_NotoSansMalayalamUI_Regular, _font_NotoSansMalayalamUI_Regular_size, font_hinting, font_antialiasing, true, font_subpixel_positioning, false, &fallbacks); Ref oriya_font = load_internal_font(_font_NotoSansOriyaUI_Regular, _font_NotoSansOriyaUI_Regular_size, font_hinting, font_antialiasing, true, font_subpixel_positioning, false, &fallbacks); Ref sinhala_font = load_internal_font(_font_NotoSansSinhalaUI_Regular, _font_NotoSansSinhalaUI_Regular_size, font_hinting, font_antialiasing, true, font_subpixel_positioning, false, &fallbacks); Ref tamil_font = load_internal_font(_font_NotoSansTamilUI_Regular, _font_NotoSansTamilUI_Regular_size, font_hinting, font_antialiasing, true, font_subpixel_positioning, false, &fallbacks); Ref telugu_font = load_internal_font(_font_NotoSansTeluguUI_Regular, _font_NotoSansTeluguUI_Regular_size, font_hinting, font_antialiasing, true, font_subpixel_positioning, false, &fallbacks); Ref thai_font = load_internal_font(_font_NotoSansThaiUI_Regular, _font_NotoSansThaiUI_Regular_size, font_hinting, font_antialiasing, true, font_subpixel_positioning, false, &fallbacks); Ref fallback_font = load_internal_font(_font_DroidSansFallback, _font_DroidSansFallback_size, font_hinting, font_antialiasing, true, font_subpixel_positioning, false, &fallbacks); Ref japanese_font = load_internal_font(_font_DroidSansJapanese, _font_DroidSansJapanese_size, font_hinting, font_antialiasing, true, font_subpixel_positioning, false, &fallbacks); default_font->set_fallbacks(fallbacks); default_font_msdf->set_fallbacks(fallbacks); Ref default_font_bold = load_internal_font(_font_NotoSans_Bold, _font_NotoSans_Bold_size, font_hinting, font_antialiasing, true, font_subpixel_positioning, false); Ref default_font_bold_msdf = load_internal_font(_font_NotoSans_Bold, _font_NotoSans_Bold_size, font_hinting, font_antialiasing, true, font_subpixel_positioning, true); TypedArray fallbacks_bold; Ref arabic_font_bold = load_internal_font(_font_NotoNaskhArabicUI_Bold, _font_NotoNaskhArabicUI_Bold_size, font_hinting, font_antialiasing, true, font_subpixel_positioning, false, &fallbacks_bold); Ref bengali_font_bold = load_internal_font(_font_NotoSansBengaliUI_Bold, _font_NotoSansBengaliUI_Bold_size, font_hinting, font_antialiasing, true, font_subpixel_positioning, false, &fallbacks_bold); Ref devanagari_font_bold = load_internal_font(_font_NotoSansDevanagariUI_Bold, _font_NotoSansDevanagariUI_Bold_size, font_hinting, font_antialiasing, true, font_subpixel_positioning, false, &fallbacks_bold); Ref georgian_font_bold = load_internal_font(_font_NotoSansGeorgian_Bold, _font_NotoSansGeorgian_Bold_size, font_hinting, font_antialiasing, true, font_subpixel_positioning, false, &fallbacks_bold); Ref hebrew_font_bold = load_internal_font(_font_NotoSansHebrew_Bold, _font_NotoSansHebrew_Bold_size, font_hinting, font_antialiasing, true, font_subpixel_positioning, false, &fallbacks_bold); Ref malayalam_font_bold = load_internal_font(_font_NotoSansMalayalamUI_Bold, _font_NotoSansMalayalamUI_Bold_size, font_hinting, font_antialiasing, true, font_subpixel_positioning, false, &fallbacks_bold); Ref oriya_font_bold = load_internal_font(_font_NotoSansOriyaUI_Bold, _font_NotoSansOriyaUI_Bold_size, font_hinting, font_antialiasing, true, font_subpixel_positioning, false, &fallbacks_bold); Ref sinhala_font_bold = load_internal_font(_font_NotoSansSinhalaUI_Bold, _font_NotoSansSinhalaUI_Bold_size, font_hinting, font_antialiasing, true, font_subpixel_positioning, false, &fallbacks_bold); Ref tamil_font_bold = load_internal_font(_font_NotoSansTamilUI_Bold, _font_NotoSansTamilUI_Bold_size, font_hinting, font_antialiasing, true, font_subpixel_positioning, false, &fallbacks_bold); Ref telugu_font_bold = load_internal_font(_font_NotoSansTeluguUI_Bold, _font_NotoSansTeluguUI_Bold_size, font_hinting, font_antialiasing, true, font_subpixel_positioning, false, &fallbacks_bold); Ref thai_font_bold = load_internal_font(_font_NotoSansThaiUI_Bold, _font_NotoSansThaiUI_Bold_size, font_hinting, font_antialiasing, true, font_subpixel_positioning, false, &fallbacks_bold); Ref fallback_font_bold = make_bold_font(fallback_font, embolden_strength, &fallbacks_bold); Ref japanese_font_bold = make_bold_font(japanese_font, embolden_strength, &fallbacks_bold); default_font_bold->set_fallbacks(fallbacks_bold); default_font_bold_msdf->set_fallbacks(fallbacks_bold); Ref default_font_mono = load_internal_font(_font_JetBrainsMono_Regular, _font_JetBrainsMono_Regular_size, font_mono_hinting, font_antialiasing, true, font_subpixel_positioning); default_font_mono->set_fallbacks(fallbacks); // Init base font configs and load custom fonts. String custom_font_path = EDITOR_GET("interface/editor/main_font"); String custom_font_path_bold = EDITOR_GET("interface/editor/main_font_bold"); String custom_font_path_source = EDITOR_GET("interface/editor/code_font"); Ref default_fc; default_fc.instantiate(); if (custom_font_path.length() > 0 && dir->file_exists(custom_font_path)) { Ref custom_font = load_external_font(custom_font_path, font_hinting, font_antialiasing, true, font_subpixel_positioning); { TypedArray fallback_custom; fallback_custom.push_back(default_font); custom_font->set_fallbacks(fallback_custom); } default_fc->set_base_font(custom_font); } else { EditorSettings::get_singleton()->set_manually("interface/editor/main_font", ""); default_fc->set_base_font(default_font); } default_fc->set_spacing(TextServer::SPACING_TOP, -EDSCALE); default_fc->set_spacing(TextServer::SPACING_BOTTOM, -EDSCALE); Ref default_fc_msdf; default_fc_msdf.instantiate(); if (custom_font_path.length() > 0 && dir->file_exists(custom_font_path)) { Ref custom_font = load_external_font(custom_font_path, font_hinting, font_antialiasing, true, font_subpixel_positioning); { TypedArray fallback_custom; fallback_custom.push_back(default_font_msdf); custom_font->set_fallbacks(fallback_custom); } default_fc_msdf->set_base_font(custom_font); } else { EditorSettings::get_singleton()->set_manually("interface/editor/main_font", ""); default_fc_msdf->set_base_font(default_font_msdf); } default_fc_msdf->set_spacing(TextServer::SPACING_TOP, -EDSCALE); default_fc_msdf->set_spacing(TextServer::SPACING_BOTTOM, -EDSCALE); Ref bold_fc; bold_fc.instantiate(); if (custom_font_path_bold.length() > 0 && dir->file_exists(custom_font_path_bold)) { Ref custom_font = load_external_font(custom_font_path_bold, font_hinting, font_antialiasing, true, font_subpixel_positioning); { TypedArray fallback_custom; fallback_custom.push_back(default_font_bold); custom_font->set_fallbacks(fallback_custom); } bold_fc->set_base_font(custom_font); } else if (custom_font_path.length() > 0 && dir->file_exists(custom_font_path)) { Ref custom_font = load_external_font(custom_font_path, font_hinting, font_antialiasing, true, font_subpixel_positioning); { TypedArray fallback_custom; fallback_custom.push_back(default_font_bold); custom_font->set_fallbacks(fallback_custom); } bold_fc->set_base_font(custom_font); bold_fc->set_variation_embolden(embolden_strength); } else { EditorSettings::get_singleton()->set_manually("interface/editor/main_font_bold", ""); bold_fc->set_base_font(default_font_bold); } bold_fc->set_spacing(TextServer::SPACING_TOP, -EDSCALE); bold_fc->set_spacing(TextServer::SPACING_BOTTOM, -EDSCALE); Ref bold_fc_msdf; bold_fc_msdf.instantiate(); if (custom_font_path_bold.length() > 0 && dir->file_exists(custom_font_path_bold)) { Ref custom_font = load_external_font(custom_font_path_bold, font_hinting, font_antialiasing, true, font_subpixel_positioning); { TypedArray fallback_custom; fallback_custom.push_back(default_font_bold_msdf); custom_font->set_fallbacks(fallback_custom); } bold_fc_msdf->set_base_font(custom_font); } else if (custom_font_path.length() > 0 && dir->file_exists(custom_font_path)) { Ref custom_font = load_external_font(custom_font_path, font_hinting, font_antialiasing, true, font_subpixel_positioning); { TypedArray fallback_custom; fallback_custom.push_back(default_font_bold_msdf); custom_font->set_fallbacks(fallback_custom); } bold_fc_msdf->set_base_font(custom_font); bold_fc_msdf->set_variation_embolden(embolden_strength); } else { EditorSettings::get_singleton()->set_manually("interface/editor/main_font_bold", ""); bold_fc_msdf->set_base_font(default_font_bold_msdf); } bold_fc_msdf->set_spacing(TextServer::SPACING_TOP, -EDSCALE); bold_fc_msdf->set_spacing(TextServer::SPACING_BOTTOM, -EDSCALE); Ref mono_fc; mono_fc.instantiate(); if (custom_font_path_source.length() > 0 && dir->file_exists(custom_font_path_source)) { Ref custom_font = load_external_font(custom_font_path_source, font_mono_hinting, font_antialiasing, true, font_subpixel_positioning); { TypedArray fallback_custom; fallback_custom.push_back(default_font_mono); custom_font->set_fallbacks(fallback_custom); } mono_fc->set_base_font(custom_font); } else { EditorSettings::get_singleton()->set_manually("interface/editor/code_font", ""); mono_fc->set_base_font(default_font_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 = EDITOR_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(EDITOR_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); } // Use fake bold/italics to style the editor log's `print_rich()` output. // Use stronger embolden strength to make bold easier to distinguish from regular text. Ref mono_other_fc_bold = mono_other_fc->duplicate(); mono_other_fc_bold->set_variation_embolden(0.8); Ref mono_other_fc_italic = mono_other_fc->duplicate(); mono_other_fc_italic->set_variation_transform(Transform2D(1.0, 0.2, 0.0, 1.0, 0.0, 0.0)); Ref mono_other_fc_bold_italic = mono_other_fc->duplicate(); mono_other_fc_bold_italic->set_variation_embolden(0.8); mono_other_fc_bold_italic->set_variation_transform(Transform2D(1.0, 0.2, 0.0, 1.0, 0.0, 0.0)); Ref mono_other_fc_mono = mono_other_fc->duplicate(); // Use a different font style to distinguish `[code]` in rich prints. // This emulates the "faint" styling used in ANSI escape codes by using a slightly thinner font. mono_other_fc_mono->set_variation_embolden(-0.25); mono_other_fc_mono->set_variation_transform(Transform2D(1.0, 0.1, 0.0, 1.0, 0.0, 0.0)); Ref italic_fc = default_fc->duplicate(); italic_fc->set_variation_transform(Transform2D(1.0, 0.2, 0.0, 1.0, 0.0, 0.0)); // Setup theme. p_theme->set_default_font(default_fc); // Default theme font config. p_theme->set_default_font_size(default_font_size); // Main font. p_theme->set_font("main", "EditorFonts", default_fc); p_theme->set_font("main_msdf", "EditorFonts", default_fc_msdf); p_theme->set_font_size("main_size", "EditorFonts", default_font_size); p_theme->set_font("bold", "EditorFonts", bold_fc); p_theme->set_font("main_bold_msdf", "EditorFonts", bold_fc_msdf); p_theme->set_font_size("bold_size", "EditorFonts", default_font_size); // Title font. p_theme->set_font("title", "EditorFonts", bold_fc); p_theme->set_font_size("title_size", "EditorFonts", default_font_size + 1 * EDSCALE); p_theme->set_font("main_button_font", "EditorFonts", bold_fc); p_theme->set_font_size("main_button_font_size", "EditorFonts", default_font_size + 1 * EDSCALE); p_theme->set_font("font", "Label", default_fc); p_theme->set_type_variation("HeaderSmall", "Label"); p_theme->set_font("font", "HeaderSmall", bold_fc); p_theme->set_font_size("font_size", "HeaderSmall", default_font_size); p_theme->set_type_variation("HeaderMedium", "Label"); p_theme->set_font("font", "HeaderMedium", bold_fc); p_theme->set_font_size("font_size", "HeaderMedium", default_font_size + 1 * EDSCALE); p_theme->set_type_variation("HeaderLarge", "Label"); p_theme->set_font("font", "HeaderLarge", bold_fc); p_theme->set_font_size("font_size", "HeaderLarge", default_font_size + 3 * EDSCALE); // Documentation fonts p_theme->set_font_size("doc_size", "EditorFonts", int(EDITOR_GET("text_editor/help/help_font_size")) * EDSCALE); p_theme->set_font("doc", "EditorFonts", default_fc); p_theme->set_font("doc_bold", "EditorFonts", bold_fc); p_theme->set_font("doc_italic", "EditorFonts", italic_fc); p_theme->set_font_size("doc_title_size", "EditorFonts", int(EDITOR_GET("text_editor/help/help_title_font_size")) * EDSCALE); p_theme->set_font("doc_title", "EditorFonts", bold_fc); p_theme->set_font_size("doc_source_size", "EditorFonts", int(EDITOR_GET("text_editor/help/help_source_font_size")) * EDSCALE); p_theme->set_font("doc_source", "EditorFonts", mono_fc); p_theme->set_font_size("doc_keyboard_size", "EditorFonts", (int(EDITOR_GET("text_editor/help/help_source_font_size")) - 1) * EDSCALE); p_theme->set_font("doc_keyboard", "EditorFonts", mono_fc); // Ruler font p_theme->set_font_size("rulers_size", "EditorFonts", 8 * EDSCALE); p_theme->set_font("rulers", "EditorFonts", default_fc); // Rotation widget font p_theme->set_font_size("rotation_control_size", "EditorFonts", 14 * EDSCALE); p_theme->set_font("rotation_control", "EditorFonts", default_fc); // Code font p_theme->set_font_size("source_size", "EditorFonts", int(EDITOR_GET("interface/editor/code_font_size")) * EDSCALE); 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_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_other_fc); p_theme->set_font("output_source_bold", "EditorFonts", mono_other_fc_bold); p_theme->set_font("output_source_italic", "EditorFonts", mono_other_fc_italic); p_theme->set_font("output_source_bold_italic", "EditorFonts", mono_other_fc_bold_italic); p_theme->set_font("output_source_mono", "EditorFonts", mono_other_fc_mono); p_theme->set_font_size("status_source_size", "EditorFonts", default_font_size); p_theme->set_font("status_source", "EditorFonts", mono_other_fc); }