editor_fonts.cpp 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549
  1. /**************************************************************************/
  2. /* editor_fonts.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. #include "editor_fonts.h"
  31. #include "core/io/dir_access.h"
  32. #include "core/os/os.h"
  33. #include "core/string/translation_server.h"
  34. #include "editor/editor_string_names.h"
  35. #include "editor/settings/editor_settings.h"
  36. #include "editor/themes/builtin_fonts.gen.h"
  37. #include "editor/themes/editor_scale.h"
  38. #include "scene/resources/font.h"
  39. #include "scene/scene_string_names.h"
  40. Ref<FontFile> 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_font_disable_embedded_bitmaps, bool p_msdf = false, TypedArray<Font> *r_fallbacks = nullptr) {
  41. Ref<FontFile> font;
  42. font.instantiate();
  43. Vector<uint8_t> data = FileAccess::get_file_as_bytes(p_path);
  44. font->set_data(data);
  45. font->set_multichannel_signed_distance_field(p_msdf);
  46. font->set_antialiasing(p_aa);
  47. font->set_hinting(p_hinting);
  48. font->set_force_autohinter(p_autohint);
  49. font->set_subpixel_positioning(p_font_subpixel_positioning);
  50. font->set_disable_embedded_bitmaps(p_font_disable_embedded_bitmaps);
  51. if (r_fallbacks != nullptr) {
  52. r_fallbacks->push_back(font);
  53. }
  54. return font;
  55. }
  56. Ref<SystemFont> load_system_font(const PackedStringArray &p_names, TextServer::Hinting p_hinting, TextServer::FontAntialiasing p_aa, bool p_autohint, TextServer::SubpixelPositioning p_font_subpixel_positioning, bool p_font_disable_embedded_bitmaps, bool p_msdf = false, TypedArray<Font> *r_fallbacks = nullptr) {
  57. Ref<SystemFont> font;
  58. font.instantiate();
  59. font->set_font_names(p_names);
  60. font->set_multichannel_signed_distance_field(p_msdf);
  61. font->set_antialiasing(p_aa);
  62. font->set_hinting(p_hinting);
  63. font->set_force_autohinter(p_autohint);
  64. font->set_subpixel_positioning(p_font_subpixel_positioning);
  65. font->set_disable_embedded_bitmaps(p_font_disable_embedded_bitmaps);
  66. if (r_fallbacks != nullptr) {
  67. r_fallbacks->push_back(font);
  68. }
  69. return font;
  70. }
  71. Ref<FontFile> 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_font_disable_embedded_bitmaps, bool p_msdf = false, TypedArray<Font> *r_fallbacks = nullptr) {
  72. Ref<FontFile> font;
  73. font.instantiate();
  74. font->set_data_ptr(p_data, p_size);
  75. font->set_multichannel_signed_distance_field(p_msdf);
  76. font->set_antialiasing(p_aa);
  77. font->set_hinting(p_hinting);
  78. font->set_force_autohinter(p_autohint);
  79. font->set_subpixel_positioning(p_font_subpixel_positioning);
  80. font->set_disable_embedded_bitmaps(p_font_disable_embedded_bitmaps);
  81. if (r_fallbacks != nullptr) {
  82. r_fallbacks->push_back(font);
  83. }
  84. return font;
  85. }
  86. Ref<FontVariation> make_bold_font(const Ref<Font> &p_font, double p_embolden, TypedArray<Font> *r_fallbacks = nullptr) {
  87. Ref<FontVariation> font_var;
  88. font_var.instantiate();
  89. font_var->set_base_font(p_font);
  90. font_var->set_variation_embolden(p_embolden);
  91. if (r_fallbacks != nullptr) {
  92. r_fallbacks->push_back(font_var);
  93. }
  94. return font_var;
  95. }
  96. void editor_register_fonts(const Ref<Theme> &p_theme) {
  97. Ref<DirAccess> dir = DirAccess::create(DirAccess::ACCESS_FILESYSTEM);
  98. TextServer::FontAntialiasing font_antialiasing = (TextServer::FontAntialiasing)(int)EDITOR_GET("interface/editor/font_antialiasing");
  99. int font_hinting_setting = (int)EDITOR_GET("interface/editor/font_hinting");
  100. TextServer::SubpixelPositioning font_subpixel_positioning = (TextServer::SubpixelPositioning)(int)EDITOR_GET("interface/editor/font_subpixel_positioning");
  101. bool font_disable_embedded_bitmaps = (bool)EDITOR_GET("interface/editor/font_disable_embedded_bitmaps");
  102. bool font_allow_msdf = (bool)EDITOR_GET("interface/editor/font_allow_msdf");
  103. TextServer::Hinting font_hinting;
  104. TextServer::Hinting font_mono_hinting;
  105. switch (font_hinting_setting) {
  106. case 0:
  107. // The "Auto" setting uses the setting that best matches the OS' font rendering:
  108. // - macOS doesn't use font hinting.
  109. // - Windows uses ClearType, which is in between "Light" and "Normal" hinting.
  110. // - Linux has configurable font hinting, but most distributions including Ubuntu default to "Light".
  111. #ifdef MACOS_ENABLED
  112. font_hinting = TextServer::HINTING_NONE;
  113. font_mono_hinting = TextServer::HINTING_NONE;
  114. #else
  115. font_hinting = TextServer::HINTING_LIGHT;
  116. font_mono_hinting = TextServer::HINTING_LIGHT;
  117. #endif
  118. break;
  119. case 1:
  120. font_hinting = TextServer::HINTING_NONE;
  121. font_mono_hinting = TextServer::HINTING_NONE;
  122. break;
  123. case 2:
  124. font_hinting = TextServer::HINTING_LIGHT;
  125. font_mono_hinting = TextServer::HINTING_LIGHT;
  126. break;
  127. default:
  128. font_hinting = TextServer::HINTING_NORMAL;
  129. font_mono_hinting = TextServer::HINTING_LIGHT;
  130. break;
  131. }
  132. // Load built-in fonts.
  133. const int default_font_size = int(EDITOR_GET("interface/editor/main_font_size")) * EDSCALE;
  134. const float embolden_strength = 0.6;
  135. Ref<Font> default_font = load_internal_font(_font_Inter_Regular, _font_Inter_Regular_size, font_hinting, font_antialiasing, true, font_subpixel_positioning, font_disable_embedded_bitmaps, false);
  136. Ref<Font> default_font_msdf = load_internal_font(_font_Inter_Regular, _font_Inter_Regular_size, font_hinting, font_antialiasing, true, font_subpixel_positioning, font_disable_embedded_bitmaps, font_allow_msdf);
  137. Dictionary default_features;
  138. default_features["calt"] = false; // Disable contextual alternates by default.
  139. default_features["ss04"] = true; // Serifed I, tailed l for better distinction.
  140. default_features["tnum"] = true; // Tabular numbers for better alignment.
  141. String noto_cjk_path;
  142. String noto_cjk_bold_path;
  143. {
  144. Vector<String> var_suffix;
  145. // Note: Most Noto Sans CJK versions support all glyph variations, but select the best matching one in case it's not.
  146. String locale = TranslationServer::get_singleton()->get_tool_locale();
  147. if (!locale.begins_with("zh") && !locale.begins_with("ja") && !locale.begins_with("ko")) {
  148. locale = OS::get_singleton()->get_locale();
  149. }
  150. if (locale.begins_with("zh") && (locale.contains("Hans") || locale.contains("CN") || locale.contains("SG"))) {
  151. var_suffix = { "SC", "TC", "HK", "KR", "JP" };
  152. } else if (locale.begins_with("zh") && locale.contains("HK")) {
  153. var_suffix = { "HK", "TC", "SC", "KR", "JP" };
  154. } else if (locale.begins_with("zh") && (locale.contains("Hant") || locale.contains("MO") || locale.contains("TW"))) {
  155. var_suffix = { "TC", "HK", "SC", "KR", "JP" };
  156. } else if (locale.begins_with("ko")) {
  157. var_suffix = { "KR", "HK", "SC", "TC", "JP" };
  158. } else if (locale.begins_with("ko")) {
  159. var_suffix = { "JP", "HK", "KR", "SC", "TC" };
  160. } else {
  161. var_suffix = { "HK", "KR", "SC", "TC", "JP" };
  162. }
  163. for (int64_t i = 0; i < var_suffix.size(); i++) {
  164. if (noto_cjk_path.is_empty()) {
  165. noto_cjk_path = OS::get_singleton()->get_system_font_path("Noto Sans CJK " + var_suffix[i], 400, 100);
  166. }
  167. if (noto_cjk_bold_path.is_empty()) {
  168. noto_cjk_bold_path = OS::get_singleton()->get_system_font_path("Noto Sans CJK " + var_suffix[i], 800, 100);
  169. }
  170. }
  171. }
  172. TypedArray<Font> fallbacks;
  173. Ref<FontFile> arabic_font = load_internal_font(_font_Vazirmatn_Regular, _font_Vazirmatn_Regular_size, font_hinting, font_antialiasing, true, font_subpixel_positioning, font_disable_embedded_bitmaps, false, &fallbacks);
  174. Ref<FontFile> bengali_font = load_internal_font(_font_NotoSansBengaliUI_Regular, _font_NotoSansBengaliUI_Regular_size, font_hinting, font_antialiasing, true, font_subpixel_positioning, font_disable_embedded_bitmaps, false, &fallbacks);
  175. Ref<FontFile> devanagari_font = load_internal_font(_font_NotoSansDevanagariUI_Regular, _font_NotoSansDevanagariUI_Regular_size, font_hinting, font_antialiasing, true, font_subpixel_positioning, font_disable_embedded_bitmaps, false, &fallbacks);
  176. Ref<FontFile> georgian_font = load_internal_font(_font_NotoSansGeorgian_Regular, _font_NotoSansGeorgian_Regular_size, font_hinting, font_antialiasing, true, font_subpixel_positioning, font_disable_embedded_bitmaps, false, &fallbacks);
  177. Ref<FontFile> hebrew_font = load_internal_font(_font_NotoSansHebrew_Regular, _font_NotoSansHebrew_Regular_size, font_hinting, font_antialiasing, true, font_subpixel_positioning, font_disable_embedded_bitmaps, false, &fallbacks);
  178. Ref<FontFile> malayalam_font = load_internal_font(_font_NotoSansMalayalamUI_Regular, _font_NotoSansMalayalamUI_Regular_size, font_hinting, font_antialiasing, true, font_subpixel_positioning, font_disable_embedded_bitmaps, false, &fallbacks);
  179. Ref<FontFile> oriya_font = load_internal_font(_font_NotoSansOriya_Regular, _font_NotoSansOriya_Regular_size, font_hinting, font_antialiasing, true, font_subpixel_positioning, font_disable_embedded_bitmaps, false, &fallbacks);
  180. Ref<FontFile> sinhala_font = load_internal_font(_font_NotoSansSinhalaUI_Regular, _font_NotoSansSinhalaUI_Regular_size, font_hinting, font_antialiasing, true, font_subpixel_positioning, font_disable_embedded_bitmaps, false, &fallbacks);
  181. Ref<FontFile> tamil_font = load_internal_font(_font_NotoSansTamilUI_Regular, _font_NotoSansTamilUI_Regular_size, font_hinting, font_antialiasing, true, font_subpixel_positioning, font_disable_embedded_bitmaps, false, &fallbacks);
  182. Ref<FontFile> telugu_font = load_internal_font(_font_NotoSansTeluguUI_Regular, _font_NotoSansTeluguUI_Regular_size, font_hinting, font_antialiasing, true, font_subpixel_positioning, font_disable_embedded_bitmaps, false, &fallbacks);
  183. Ref<FontFile> thai_font = load_internal_font(_font_NotoSansThai_Regular, _font_NotoSansThai_Regular_size, font_hinting, font_antialiasing, true, font_subpixel_positioning, font_disable_embedded_bitmaps, false, &fallbacks);
  184. if (!noto_cjk_path.is_empty()) {
  185. load_external_font(noto_cjk_path, font_hinting, font_antialiasing, true, font_subpixel_positioning, font_disable_embedded_bitmaps, false, &fallbacks);
  186. }
  187. Ref<FontFile> fallback_font = load_internal_font(_font_DroidSansFallback, _font_DroidSansFallback_size, font_hinting, font_antialiasing, true, font_subpixel_positioning, font_disable_embedded_bitmaps, false, &fallbacks);
  188. fallback_font->set_language_support_override("ja", false);
  189. fallback_font->set_language_support_override("zh", true);
  190. fallback_font->set_language_support_override("ko", true);
  191. fallback_font->set_language_support_override("*", false);
  192. Ref<FontFile> japanese_font = load_internal_font(_font_DroidSansJapanese, _font_DroidSansJapanese_size, font_hinting, font_antialiasing, true, font_subpixel_positioning, font_disable_embedded_bitmaps, false, &fallbacks);
  193. japanese_font->set_language_support_override("ja", true);
  194. japanese_font->set_language_support_override("zh", false);
  195. japanese_font->set_language_support_override("ko", false);
  196. japanese_font->set_language_support_override("*", false);
  197. default_font->set_fallbacks(fallbacks);
  198. default_font_msdf->set_fallbacks(fallbacks);
  199. Ref<FontFile> default_font_bold = load_internal_font(_font_Inter_Bold, _font_Inter_Bold_size, font_hinting, font_antialiasing, true, font_subpixel_positioning, font_disable_embedded_bitmaps, false);
  200. Ref<FontFile> default_font_bold_msdf = load_internal_font(_font_Inter_Bold, _font_Inter_Bold_size, font_hinting, font_antialiasing, true, font_subpixel_positioning, font_disable_embedded_bitmaps, font_allow_msdf);
  201. TypedArray<Font> fallbacks_bold;
  202. Ref<FontFile> arabic_font_bold = load_internal_font(_font_Vazirmatn_Bold, _font_Vazirmatn_Bold_size, font_hinting, font_antialiasing, true, font_subpixel_positioning, font_disable_embedded_bitmaps, false, &fallbacks_bold);
  203. Ref<FontFile> bengali_font_bold = load_internal_font(_font_NotoSansBengaliUI_Bold, _font_NotoSansBengaliUI_Bold_size, font_hinting, font_antialiasing, true, font_subpixel_positioning, font_disable_embedded_bitmaps, false, &fallbacks_bold);
  204. Ref<FontFile> devanagari_font_bold = load_internal_font(_font_NotoSansDevanagariUI_Bold, _font_NotoSansDevanagariUI_Bold_size, font_hinting, font_antialiasing, true, font_subpixel_positioning, font_disable_embedded_bitmaps, false, &fallbacks_bold);
  205. Ref<FontFile> georgian_font_bold = load_internal_font(_font_NotoSansGeorgian_Bold, _font_NotoSansGeorgian_Bold_size, font_hinting, font_antialiasing, true, font_subpixel_positioning, font_disable_embedded_bitmaps, false, &fallbacks_bold);
  206. Ref<FontFile> hebrew_font_bold = load_internal_font(_font_NotoSansHebrew_Bold, _font_NotoSansHebrew_Bold_size, font_hinting, font_antialiasing, true, font_subpixel_positioning, font_disable_embedded_bitmaps, false, &fallbacks_bold);
  207. Ref<FontFile> malayalam_font_bold = load_internal_font(_font_NotoSansMalayalamUI_Bold, _font_NotoSansMalayalamUI_Bold_size, font_hinting, font_antialiasing, true, font_subpixel_positioning, font_disable_embedded_bitmaps, false, &fallbacks_bold);
  208. Ref<FontFile> oriya_font_bold = load_internal_font(_font_NotoSansOriya_Bold, _font_NotoSansOriya_Bold_size, font_hinting, font_antialiasing, true, font_subpixel_positioning, font_disable_embedded_bitmaps, false, &fallbacks_bold);
  209. Ref<FontFile> sinhala_font_bold = load_internal_font(_font_NotoSansSinhalaUI_Bold, _font_NotoSansSinhalaUI_Bold_size, font_hinting, font_antialiasing, true, font_subpixel_positioning, font_disable_embedded_bitmaps, false, &fallbacks_bold);
  210. Ref<FontFile> tamil_font_bold = load_internal_font(_font_NotoSansTamilUI_Bold, _font_NotoSansTamilUI_Bold_size, font_hinting, font_antialiasing, true, font_subpixel_positioning, font_disable_embedded_bitmaps, false, &fallbacks_bold);
  211. Ref<FontFile> telugu_font_bold = load_internal_font(_font_NotoSansTeluguUI_Bold, _font_NotoSansTeluguUI_Bold_size, font_hinting, font_antialiasing, true, font_subpixel_positioning, font_disable_embedded_bitmaps, false, &fallbacks_bold);
  212. Ref<FontFile> thai_font_bold = load_internal_font(_font_NotoSansThai_Bold, _font_NotoSansThai_Bold_size, font_hinting, font_antialiasing, true, font_subpixel_positioning, font_disable_embedded_bitmaps, false, &fallbacks_bold);
  213. if (!noto_cjk_bold_path.is_empty()) {
  214. load_external_font(noto_cjk_bold_path, font_hinting, font_antialiasing, true, font_subpixel_positioning, font_disable_embedded_bitmaps, false, &fallbacks_bold);
  215. }
  216. Ref<FontVariation> fallback_font_bold = make_bold_font(fallback_font, embolden_strength, &fallbacks_bold);
  217. Ref<FontVariation> japanese_font_bold = make_bold_font(japanese_font, embolden_strength, &fallbacks_bold);
  218. if (OS::get_singleton()->has_feature("system_fonts")) {
  219. PackedStringArray emoji_font_names = {
  220. "Apple Color Emoji",
  221. "Segoe UI Emoji",
  222. "Noto Color Emoji",
  223. "Twitter Color Emoji",
  224. "OpenMoji",
  225. "EmojiOne Color"
  226. };
  227. Ref<SystemFont> emoji_font = load_system_font(emoji_font_names, font_hinting, font_antialiasing, true, font_subpixel_positioning, font_disable_embedded_bitmaps, false);
  228. fallbacks.push_back(emoji_font);
  229. fallbacks_bold.push_back(emoji_font);
  230. }
  231. default_font_bold->set_fallbacks(fallbacks_bold);
  232. default_font_bold_msdf->set_fallbacks(fallbacks_bold);
  233. Ref<FontFile> default_font_mono = load_internal_font(_font_JetBrainsMono_Regular, _font_JetBrainsMono_Regular_size, font_mono_hinting, font_antialiasing, true, font_subpixel_positioning, font_disable_embedded_bitmaps);
  234. default_font_mono->set_fallbacks(fallbacks);
  235. // Init base font configs and load custom fonts.
  236. String custom_font_path = EDITOR_GET("interface/editor/main_font");
  237. String custom_font_path_bold = EDITOR_GET("interface/editor/main_font_bold");
  238. String custom_font_path_source = EDITOR_GET("interface/editor/code_font");
  239. Ref<FontVariation> default_fc;
  240. default_fc.instantiate();
  241. if (custom_font_path.length() > 0 && dir->file_exists(custom_font_path)) {
  242. Ref<FontFile> custom_font = load_external_font(custom_font_path, font_hinting, font_antialiasing, true, font_subpixel_positioning, font_disable_embedded_bitmaps);
  243. {
  244. TypedArray<Font> fallback_custom = { default_font };
  245. custom_font->set_fallbacks(fallback_custom);
  246. }
  247. default_fc->set_base_font(custom_font);
  248. } else {
  249. EditorSettings::get_singleton()->set_manually("interface/editor/main_font", "");
  250. default_fc->set_opentype_features(default_features);
  251. default_fc->set_base_font(default_font);
  252. }
  253. default_fc->set_spacing(TextServer::SPACING_TOP, -EDSCALE);
  254. default_fc->set_spacing(TextServer::SPACING_BOTTOM, -EDSCALE);
  255. Dictionary default_fc_opentype;
  256. default_fc_opentype["weight"] = 400;
  257. default_fc->set_variation_opentype(default_fc_opentype);
  258. Ref<FontVariation> default_fc_msdf;
  259. default_fc_msdf.instantiate();
  260. if (custom_font_path.length() > 0 && dir->file_exists(custom_font_path)) {
  261. Ref<FontFile> custom_font = load_external_font(custom_font_path, font_hinting, font_antialiasing, true, font_subpixel_positioning, font_disable_embedded_bitmaps, font_allow_msdf);
  262. {
  263. TypedArray<Font> fallback_custom = { default_font_msdf };
  264. custom_font->set_fallbacks(fallback_custom);
  265. }
  266. default_fc_msdf->set_base_font(custom_font);
  267. } else {
  268. EditorSettings::get_singleton()->set_manually("interface/editor/main_font", "");
  269. default_fc_msdf->set_opentype_features(default_features);
  270. default_fc_msdf->set_base_font(default_font_msdf);
  271. }
  272. default_fc_msdf->set_spacing(TextServer::SPACING_TOP, -EDSCALE);
  273. default_fc_msdf->set_spacing(TextServer::SPACING_BOTTOM, -EDSCALE);
  274. default_fc_msdf->set_variation_opentype(default_fc_opentype);
  275. Ref<FontVariation> bold_fc;
  276. bold_fc.instantiate();
  277. if (custom_font_path_bold.length() > 0 && dir->file_exists(custom_font_path_bold)) {
  278. Ref<FontFile> custom_font = load_external_font(custom_font_path_bold, font_hinting, font_antialiasing, true, font_subpixel_positioning, font_disable_embedded_bitmaps);
  279. {
  280. TypedArray<Font> fallback_custom = { default_font_bold };
  281. custom_font->set_fallbacks(fallback_custom);
  282. }
  283. bold_fc->set_base_font(custom_font);
  284. } else if (custom_font_path.length() > 0 && dir->file_exists(custom_font_path)) {
  285. Ref<FontFile> custom_font = load_external_font(custom_font_path, font_hinting, font_antialiasing, true, font_subpixel_positioning, font_disable_embedded_bitmaps);
  286. {
  287. TypedArray<Font> fallback_custom = { default_font_bold };
  288. custom_font->set_fallbacks(fallback_custom);
  289. }
  290. bold_fc->set_base_font(custom_font);
  291. if (!custom_font->get_supported_variation_list().has(TS->name_to_tag("wght"))) {
  292. bold_fc->set_variation_embolden(embolden_strength);
  293. }
  294. } else {
  295. EditorSettings::get_singleton()->set_manually("interface/editor/main_font_bold", "");
  296. bold_fc->set_opentype_features(default_features);
  297. bold_fc->set_base_font(default_font_bold);
  298. }
  299. bold_fc->set_spacing(TextServer::SPACING_TOP, -EDSCALE);
  300. bold_fc->set_spacing(TextServer::SPACING_BOTTOM, -EDSCALE);
  301. Dictionary bold_fc_opentype;
  302. bold_fc_opentype["weight"] = 700;
  303. bold_fc->set_variation_opentype(bold_fc_opentype);
  304. Ref<FontVariation> bold_fc_msdf;
  305. bold_fc_msdf.instantiate();
  306. if (custom_font_path_bold.length() > 0 && dir->file_exists(custom_font_path_bold)) {
  307. Ref<FontFile> custom_font = load_external_font(custom_font_path_bold, font_hinting, font_antialiasing, true, font_subpixel_positioning, font_disable_embedded_bitmaps, font_allow_msdf);
  308. {
  309. TypedArray<Font> fallback_custom = { default_font_bold_msdf };
  310. custom_font->set_fallbacks(fallback_custom);
  311. }
  312. bold_fc_msdf->set_base_font(custom_font);
  313. } else if (custom_font_path.length() > 0 && dir->file_exists(custom_font_path)) {
  314. Ref<FontFile> custom_font = load_external_font(custom_font_path, font_hinting, font_antialiasing, true, font_subpixel_positioning, font_disable_embedded_bitmaps, font_allow_msdf);
  315. {
  316. TypedArray<Font> fallback_custom = { default_font_bold_msdf };
  317. custom_font->set_fallbacks(fallback_custom);
  318. }
  319. bold_fc_msdf->set_base_font(custom_font);
  320. if (!custom_font->get_supported_variation_list().has(TS->name_to_tag("wght"))) {
  321. bold_fc_msdf->set_variation_embolden(embolden_strength);
  322. }
  323. } else {
  324. EditorSettings::get_singleton()->set_manually("interface/editor/main_font_bold", "");
  325. bold_fc_msdf->set_opentype_features(default_features);
  326. bold_fc_msdf->set_base_font(default_font_bold_msdf);
  327. }
  328. bold_fc_msdf->set_spacing(TextServer::SPACING_TOP, -EDSCALE);
  329. bold_fc_msdf->set_spacing(TextServer::SPACING_BOTTOM, -EDSCALE);
  330. bold_fc_msdf->set_variation_opentype(bold_fc_opentype);
  331. if (!String(EDITOR_GET("interface/editor/main_font_custom_opentype_features")).is_empty()) {
  332. Vector<String> subtag = String(EDITOR_GET("interface/editor/main_font_custom_opentype_features")).split(",");
  333. if (!subtag.is_empty()) {
  334. Dictionary ftrs;
  335. for (int i = 0; i < subtag.size(); i++) {
  336. Vector<String> subtag_a = subtag[i].split("=");
  337. if (subtag_a.size() == 2) {
  338. ftrs[TS->name_to_tag(subtag_a[0])] = subtag_a[1].to_int();
  339. } else if (subtag_a.size() == 1) {
  340. ftrs[TS->name_to_tag(subtag_a[0])] = 1;
  341. }
  342. }
  343. default_fc->set_opentype_features(ftrs);
  344. default_fc_msdf->set_opentype_features(ftrs);
  345. bold_fc->set_opentype_features(ftrs);
  346. bold_fc_msdf->set_opentype_features(ftrs);
  347. }
  348. }
  349. Ref<FontVariation> mono_fc;
  350. mono_fc.instantiate();
  351. if (custom_font_path_source.length() > 0 && dir->file_exists(custom_font_path_source)) {
  352. Ref<FontFile> custom_font = load_external_font(custom_font_path_source, font_mono_hinting, font_antialiasing, true, font_subpixel_positioning, font_disable_embedded_bitmaps);
  353. {
  354. TypedArray<Font> fallback_custom = { default_font_mono };
  355. custom_font->set_fallbacks(fallback_custom);
  356. }
  357. mono_fc->set_base_font(custom_font);
  358. } else {
  359. EditorSettings::get_singleton()->set_manually("interface/editor/code_font", "");
  360. mono_fc->set_base_font(default_font_mono);
  361. }
  362. mono_fc->set_spacing(TextServer::SPACING_TOP, -EDSCALE);
  363. mono_fc->set_spacing(TextServer::SPACING_BOTTOM, -EDSCALE);
  364. Ref<FontVariation> mono_other_fc = mono_fc->duplicate();
  365. // Enable contextual alternates (coding ligatures) and custom features for the source editor font.
  366. int ot_mode = EDITOR_GET("interface/editor/code_font_contextual_ligatures");
  367. switch (ot_mode) {
  368. case 1: { // Disable ligatures.
  369. Dictionary ftrs;
  370. ftrs[TS->name_to_tag("calt")] = 0;
  371. mono_fc->set_opentype_features(ftrs);
  372. } break;
  373. case 2: { // Custom.
  374. Vector<String> subtag = String(EDITOR_GET("interface/editor/code_font_custom_opentype_features")).split(",");
  375. Dictionary ftrs;
  376. for (int i = 0; i < subtag.size(); i++) {
  377. Vector<String> subtag_a = subtag[i].split("=");
  378. if (subtag_a.size() == 2) {
  379. ftrs[TS->name_to_tag(subtag_a[0])] = subtag_a[1].to_int();
  380. } else if (subtag_a.size() == 1) {
  381. ftrs[TS->name_to_tag(subtag_a[0])] = 1;
  382. }
  383. }
  384. mono_fc->set_opentype_features(ftrs);
  385. } break;
  386. default: { // Enabled.
  387. Dictionary ftrs;
  388. ftrs[TS->name_to_tag("calt")] = 1;
  389. mono_fc->set_opentype_features(ftrs);
  390. } break;
  391. }
  392. {
  393. // Disable contextual alternates (coding ligatures).
  394. Dictionary ftrs;
  395. ftrs[TS->name_to_tag("calt")] = 0;
  396. mono_other_fc->set_opentype_features(ftrs);
  397. }
  398. // Use fake bold/italics to style the editor log's `print_rich()` output.
  399. // Use stronger embolden strength to make bold easier to distinguish from regular text.
  400. Ref<FontVariation> mono_other_fc_bold = mono_other_fc->duplicate();
  401. mono_other_fc_bold->set_variation_embolden(0.8);
  402. Ref<FontVariation> mono_other_fc_italic = mono_other_fc->duplicate();
  403. mono_other_fc_italic->set_variation_transform(Transform2D(1.0, 0.2, 0.0, 1.0, 0.0, 0.0));
  404. Ref<FontVariation> mono_other_fc_bold_italic = mono_other_fc->duplicate();
  405. mono_other_fc_bold_italic->set_variation_embolden(0.8);
  406. mono_other_fc_bold_italic->set_variation_transform(Transform2D(1.0, 0.2, 0.0, 1.0, 0.0, 0.0));
  407. Ref<FontVariation> mono_other_fc_mono = mono_other_fc->duplicate();
  408. // Use a different font style to distinguish `[code]` in rich prints.
  409. // This emulates the "faint" styling used in ANSI escape codes by using a slightly thinner font.
  410. mono_other_fc_mono->set_variation_embolden(-0.25);
  411. mono_other_fc_mono->set_variation_transform(Transform2D(1.0, 0.1, 0.0, 1.0, 0.0, 0.0));
  412. Ref<FontVariation> italic_fc = default_fc->duplicate();
  413. italic_fc->set_variation_transform(Transform2D(1.0, 0.2, 0.0, 1.0, 0.0, 0.0));
  414. Ref<FontVariation> bold_italic_fc = bold_fc->duplicate();
  415. bold_italic_fc->set_variation_transform(Transform2D(1.0, 0.2, 0.0, 1.0, 0.0, 0.0));
  416. // Setup theme.
  417. p_theme->set_default_font(default_fc); // Default theme font config.
  418. p_theme->set_default_font_size(default_font_size);
  419. // Main font.
  420. p_theme->set_font("main", EditorStringName(EditorFonts), default_fc);
  421. p_theme->set_font("main_msdf", EditorStringName(EditorFonts), default_fc_msdf);
  422. p_theme->set_font_size("main_size", EditorStringName(EditorFonts), default_font_size);
  423. p_theme->set_font("bold", EditorStringName(EditorFonts), bold_fc);
  424. p_theme->set_font("main_bold_msdf", EditorStringName(EditorFonts), bold_fc_msdf);
  425. p_theme->set_font_size("bold_size", EditorStringName(EditorFonts), default_font_size);
  426. p_theme->set_font("italic", EditorStringName(EditorFonts), italic_fc);
  427. p_theme->set_font_size("italic_size", EditorStringName(EditorFonts), default_font_size);
  428. // Title font.
  429. p_theme->set_font("title", EditorStringName(EditorFonts), bold_fc);
  430. p_theme->set_font_size("title_size", EditorStringName(EditorFonts), default_font_size + 1 * EDSCALE);
  431. p_theme->set_type_variation("MainScreenButton", "Button");
  432. p_theme->set_font(SceneStringName(font), "MainScreenButton", bold_fc);
  433. p_theme->set_font_size(SceneStringName(font_size), "MainScreenButton", default_font_size + 2 * EDSCALE);
  434. // Labels.
  435. p_theme->set_font(SceneStringName(font), "Label", default_fc);
  436. p_theme->set_type_variation("HeaderSmall", "Label");
  437. p_theme->set_font(SceneStringName(font), "HeaderSmall", bold_fc);
  438. p_theme->set_font_size(SceneStringName(font_size), "HeaderSmall", default_font_size);
  439. p_theme->set_type_variation("HeaderMedium", "Label");
  440. p_theme->set_font(SceneStringName(font), "HeaderMedium", bold_fc);
  441. p_theme->set_font_size(SceneStringName(font_size), "HeaderMedium", default_font_size + 1 * EDSCALE);
  442. p_theme->set_type_variation("HeaderLarge", "Label");
  443. p_theme->set_font(SceneStringName(font), "HeaderLarge", bold_fc);
  444. p_theme->set_font_size(SceneStringName(font_size), "HeaderLarge", default_font_size + 3 * EDSCALE);
  445. p_theme->set_font("normal_font", "RichTextLabel", default_fc);
  446. p_theme->set_font("bold_font", "RichTextLabel", bold_fc);
  447. p_theme->set_font("italics_font", "RichTextLabel", italic_fc);
  448. p_theme->set_font("bold_italics_font", "RichTextLabel", bold_italic_fc);
  449. // Documentation fonts
  450. p_theme->set_font_size("doc_size", EditorStringName(EditorFonts), int(EDITOR_GET("text_editor/help/help_font_size")) * EDSCALE);
  451. p_theme->set_font("doc", EditorStringName(EditorFonts), default_fc);
  452. p_theme->set_font("doc_bold", EditorStringName(EditorFonts), bold_fc);
  453. p_theme->set_font("doc_italic", EditorStringName(EditorFonts), italic_fc);
  454. p_theme->set_font_size("doc_title_size", EditorStringName(EditorFonts), int(EDITOR_GET("text_editor/help/help_title_font_size")) * EDSCALE);
  455. p_theme->set_font("doc_title", EditorStringName(EditorFonts), bold_fc);
  456. p_theme->set_font_size("doc_source_size", EditorStringName(EditorFonts), int(EDITOR_GET("text_editor/help/help_source_font_size")) * EDSCALE);
  457. p_theme->set_font("doc_source", EditorStringName(EditorFonts), mono_fc);
  458. p_theme->set_font_size("doc_keyboard_size", EditorStringName(EditorFonts), (int(EDITOR_GET("text_editor/help/help_source_font_size")) - 1) * EDSCALE);
  459. p_theme->set_font("doc_keyboard", EditorStringName(EditorFonts), mono_fc);
  460. // Ruler font
  461. p_theme->set_font_size("rulers_size", EditorStringName(EditorFonts), 8 * EDSCALE);
  462. p_theme->set_font("rulers", EditorStringName(EditorFonts), default_fc);
  463. // Rotation widget font
  464. p_theme->set_font_size("rotation_control_size", EditorStringName(EditorFonts), 13 * EDSCALE);
  465. p_theme->set_font("rotation_control", EditorStringName(EditorFonts), default_fc);
  466. // Code font
  467. p_theme->set_font_size("source_size", EditorStringName(EditorFonts), int(EDITOR_GET("interface/editor/code_font_size")) * EDSCALE);
  468. p_theme->set_font("source", EditorStringName(EditorFonts), mono_fc);
  469. p_theme->set_font_size("expression_size", EditorStringName(EditorFonts), (int(EDITOR_GET("interface/editor/code_font_size")) - 1) * EDSCALE);
  470. p_theme->set_font("expression", EditorStringName(EditorFonts), mono_other_fc);
  471. p_theme->set_font_size("output_source_size", EditorStringName(EditorFonts), int(EDITOR_GET("run/output/font_size")) * EDSCALE);
  472. p_theme->set_font("output_source", EditorStringName(EditorFonts), mono_other_fc);
  473. p_theme->set_font("output_source_bold", EditorStringName(EditorFonts), mono_other_fc_bold);
  474. p_theme->set_font("output_source_italic", EditorStringName(EditorFonts), mono_other_fc_italic);
  475. p_theme->set_font("output_source_bold_italic", EditorStringName(EditorFonts), mono_other_fc_bold_italic);
  476. p_theme->set_font("output_source_mono", EditorStringName(EditorFonts), mono_other_fc_mono);
  477. p_theme->set_font_size("status_source_size", EditorStringName(EditorFonts), default_font_size);
  478. p_theme->set_font("status_source", EditorStringName(EditorFonts), mono_other_fc);
  479. }