editor_fonts.cpp 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485
  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 "editor/editor_settings.h"
  33. #include "editor/editor_string_names.h"
  34. #include "editor/themes/builtin_fonts.gen.h"
  35. #include "editor/themes/editor_scale.h"
  36. #include "scene/resources/font.h"
  37. #include "scene/scene_string_names.h"
  38. 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) {
  39. Ref<FontFile> font;
  40. font.instantiate();
  41. Vector<uint8_t> data = FileAccess::get_file_as_bytes(p_path);
  42. font->set_data(data);
  43. font->set_multichannel_signed_distance_field(p_msdf);
  44. font->set_antialiasing(p_aa);
  45. font->set_hinting(p_hinting);
  46. font->set_force_autohinter(p_autohint);
  47. font->set_subpixel_positioning(p_font_subpixel_positioning);
  48. font->set_disable_embedded_bitmaps(p_font_disable_embedded_bitmaps);
  49. if (r_fallbacks != nullptr) {
  50. r_fallbacks->push_back(font);
  51. }
  52. return font;
  53. }
  54. 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) {
  55. Ref<SystemFont> font;
  56. font.instantiate();
  57. font->set_font_names(p_names);
  58. font->set_multichannel_signed_distance_field(p_msdf);
  59. font->set_antialiasing(p_aa);
  60. font->set_hinting(p_hinting);
  61. font->set_force_autohinter(p_autohint);
  62. font->set_subpixel_positioning(p_font_subpixel_positioning);
  63. font->set_disable_embedded_bitmaps(p_font_disable_embedded_bitmaps);
  64. if (r_fallbacks != nullptr) {
  65. r_fallbacks->push_back(font);
  66. }
  67. return font;
  68. }
  69. 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) {
  70. Ref<FontFile> font;
  71. font.instantiate();
  72. font->set_data_ptr(p_data, p_size);
  73. font->set_multichannel_signed_distance_field(p_msdf);
  74. font->set_antialiasing(p_aa);
  75. font->set_hinting(p_hinting);
  76. font->set_force_autohinter(p_autohint);
  77. font->set_subpixel_positioning(p_font_subpixel_positioning);
  78. font->set_disable_embedded_bitmaps(p_font_disable_embedded_bitmaps);
  79. if (r_fallbacks != nullptr) {
  80. r_fallbacks->push_back(font);
  81. }
  82. return font;
  83. }
  84. Ref<FontVariation> make_bold_font(const Ref<Font> &p_font, double p_embolden, TypedArray<Font> *r_fallbacks = nullptr) {
  85. Ref<FontVariation> font_var;
  86. font_var.instantiate();
  87. font_var->set_base_font(p_font);
  88. font_var->set_variation_embolden(p_embolden);
  89. if (r_fallbacks != nullptr) {
  90. r_fallbacks->push_back(font_var);
  91. }
  92. return font_var;
  93. }
  94. void editor_register_fonts(const Ref<Theme> &p_theme) {
  95. Ref<DirAccess> dir = DirAccess::create(DirAccess::ACCESS_FILESYSTEM);
  96. TextServer::FontAntialiasing font_antialiasing = (TextServer::FontAntialiasing)(int)EDITOR_GET("interface/editor/font_antialiasing");
  97. int font_hinting_setting = (int)EDITOR_GET("interface/editor/font_hinting");
  98. TextServer::SubpixelPositioning font_subpixel_positioning = (TextServer::SubpixelPositioning)(int)EDITOR_GET("interface/editor/font_subpixel_positioning");
  99. bool font_disable_embedded_bitmaps = (bool)EDITOR_GET("interface/editor/font_disable_embedded_bitmaps");
  100. bool font_allow_msdf = (bool)EDITOR_GET("interface/editor/font_allow_msdf");
  101. TextServer::Hinting font_hinting;
  102. TextServer::Hinting font_mono_hinting;
  103. switch (font_hinting_setting) {
  104. case 0:
  105. // The "Auto" setting uses the setting that best matches the OS' font rendering:
  106. // - macOS doesn't use font hinting.
  107. // - Windows uses ClearType, which is in between "Light" and "Normal" hinting.
  108. // - Linux has configurable font hinting, but most distributions including Ubuntu default to "Light".
  109. #ifdef MACOS_ENABLED
  110. font_hinting = TextServer::HINTING_NONE;
  111. font_mono_hinting = TextServer::HINTING_NONE;
  112. #else
  113. font_hinting = TextServer::HINTING_LIGHT;
  114. font_mono_hinting = TextServer::HINTING_LIGHT;
  115. #endif
  116. break;
  117. case 1:
  118. font_hinting = TextServer::HINTING_NONE;
  119. font_mono_hinting = TextServer::HINTING_NONE;
  120. break;
  121. case 2:
  122. font_hinting = TextServer::HINTING_LIGHT;
  123. font_mono_hinting = TextServer::HINTING_LIGHT;
  124. break;
  125. default:
  126. font_hinting = TextServer::HINTING_NORMAL;
  127. font_mono_hinting = TextServer::HINTING_LIGHT;
  128. break;
  129. }
  130. // Load built-in fonts.
  131. const int default_font_size = int(EDITOR_GET("interface/editor/main_font_size")) * EDSCALE;
  132. const float embolden_strength = 0.6;
  133. Ref<Font> default_font = load_internal_font(_font_NotoSans_Regular, _font_NotoSans_Regular_size, font_hinting, font_antialiasing, true, font_subpixel_positioning, font_disable_embedded_bitmaps, false);
  134. Ref<Font> default_font_msdf = load_internal_font(_font_NotoSans_Regular, _font_NotoSans_Regular_size, font_hinting, font_antialiasing, true, font_subpixel_positioning, font_disable_embedded_bitmaps, font_allow_msdf);
  135. String noto_cjk_path;
  136. String noto_cjk_bold_path;
  137. String var_suffix[] = { "HK", "KR", "SC", "TC", "JP" }; // Note: All Noto Sans CJK versions support all glyph variations, it should not match current locale.
  138. for (size_t i = 0; i < sizeof(var_suffix) / sizeof(String); i++) {
  139. if (noto_cjk_path.is_empty()) {
  140. noto_cjk_path = OS::get_singleton()->get_system_font_path("Noto Sans CJK " + var_suffix[i], 400, 100);
  141. }
  142. if (noto_cjk_bold_path.is_empty()) {
  143. noto_cjk_bold_path = OS::get_singleton()->get_system_font_path("Noto Sans CJK " + var_suffix[i], 800, 100);
  144. }
  145. }
  146. TypedArray<Font> fallbacks;
  147. 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);
  148. 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);
  149. 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);
  150. 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);
  151. 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);
  152. 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);
  153. 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);
  154. 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);
  155. 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);
  156. 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);
  157. 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);
  158. if (!noto_cjk_path.is_empty()) {
  159. load_external_font(noto_cjk_path, font_hinting, font_antialiasing, true, font_subpixel_positioning, font_disable_embedded_bitmaps, false, &fallbacks);
  160. }
  161. 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);
  162. 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);
  163. default_font->set_fallbacks(fallbacks);
  164. default_font_msdf->set_fallbacks(fallbacks);
  165. Ref<FontFile> default_font_bold = load_internal_font(_font_NotoSans_Bold, _font_NotoSans_Bold_size, font_hinting, font_antialiasing, true, font_subpixel_positioning, font_disable_embedded_bitmaps, false);
  166. Ref<FontFile> default_font_bold_msdf = load_internal_font(_font_NotoSans_Bold, _font_NotoSans_Bold_size, font_hinting, font_antialiasing, true, font_subpixel_positioning, font_disable_embedded_bitmaps, font_allow_msdf);
  167. TypedArray<Font> fallbacks_bold;
  168. 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);
  169. 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);
  170. 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);
  171. 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);
  172. 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);
  173. 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);
  174. 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);
  175. 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);
  176. 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);
  177. 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);
  178. 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);
  179. if (!noto_cjk_bold_path.is_empty()) {
  180. load_external_font(noto_cjk_bold_path, font_hinting, font_antialiasing, true, font_subpixel_positioning, font_disable_embedded_bitmaps, false, &fallbacks_bold);
  181. }
  182. Ref<FontVariation> fallback_font_bold = make_bold_font(fallback_font, embolden_strength, &fallbacks_bold);
  183. Ref<FontVariation> japanese_font_bold = make_bold_font(japanese_font, embolden_strength, &fallbacks_bold);
  184. if (OS::get_singleton()->has_feature("system_fonts")) {
  185. PackedStringArray emoji_font_names;
  186. emoji_font_names.push_back("Apple Color Emoji");
  187. emoji_font_names.push_back("Segoe UI Emoji");
  188. emoji_font_names.push_back("Noto Color Emoji");
  189. emoji_font_names.push_back("Twitter Color Emoji");
  190. emoji_font_names.push_back("OpenMoji");
  191. emoji_font_names.push_back("EmojiOne Color");
  192. Ref<SystemFont> emoji_font = load_system_font(emoji_font_names, font_hinting, font_antialiasing, true, font_subpixel_positioning, font_disable_embedded_bitmaps, false);
  193. fallbacks.push_back(emoji_font);
  194. fallbacks_bold.push_back(emoji_font);
  195. }
  196. default_font_bold->set_fallbacks(fallbacks_bold);
  197. default_font_bold_msdf->set_fallbacks(fallbacks_bold);
  198. 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);
  199. default_font_mono->set_fallbacks(fallbacks);
  200. // Init base font configs and load custom fonts.
  201. String custom_font_path = EDITOR_GET("interface/editor/main_font");
  202. String custom_font_path_bold = EDITOR_GET("interface/editor/main_font_bold");
  203. String custom_font_path_source = EDITOR_GET("interface/editor/code_font");
  204. Ref<FontVariation> default_fc;
  205. default_fc.instantiate();
  206. if (custom_font_path.length() > 0 && dir->file_exists(custom_font_path)) {
  207. Ref<FontFile> custom_font = load_external_font(custom_font_path, font_hinting, font_antialiasing, true, font_subpixel_positioning, font_disable_embedded_bitmaps);
  208. {
  209. TypedArray<Font> fallback_custom;
  210. fallback_custom.push_back(default_font);
  211. custom_font->set_fallbacks(fallback_custom);
  212. }
  213. default_fc->set_base_font(custom_font);
  214. } else {
  215. EditorSettings::get_singleton()->set_manually("interface/editor/main_font", "");
  216. default_fc->set_base_font(default_font);
  217. }
  218. default_fc->set_spacing(TextServer::SPACING_TOP, -EDSCALE);
  219. default_fc->set_spacing(TextServer::SPACING_BOTTOM, -EDSCALE);
  220. Ref<FontVariation> default_fc_msdf;
  221. default_fc_msdf.instantiate();
  222. if (custom_font_path.length() > 0 && dir->file_exists(custom_font_path)) {
  223. 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);
  224. {
  225. TypedArray<Font> fallback_custom;
  226. fallback_custom.push_back(default_font_msdf);
  227. custom_font->set_fallbacks(fallback_custom);
  228. }
  229. default_fc_msdf->set_base_font(custom_font);
  230. } else {
  231. EditorSettings::get_singleton()->set_manually("interface/editor/main_font", "");
  232. default_fc_msdf->set_base_font(default_font_msdf);
  233. }
  234. default_fc_msdf->set_spacing(TextServer::SPACING_TOP, -EDSCALE);
  235. default_fc_msdf->set_spacing(TextServer::SPACING_BOTTOM, -EDSCALE);
  236. Ref<FontVariation> bold_fc;
  237. bold_fc.instantiate();
  238. if (custom_font_path_bold.length() > 0 && dir->file_exists(custom_font_path_bold)) {
  239. Ref<FontFile> custom_font = load_external_font(custom_font_path_bold, font_hinting, font_antialiasing, true, font_subpixel_positioning, font_disable_embedded_bitmaps);
  240. {
  241. TypedArray<Font> fallback_custom;
  242. fallback_custom.push_back(default_font_bold);
  243. custom_font->set_fallbacks(fallback_custom);
  244. }
  245. bold_fc->set_base_font(custom_font);
  246. } else if (custom_font_path.length() > 0 && dir->file_exists(custom_font_path)) {
  247. Ref<FontFile> custom_font = load_external_font(custom_font_path, font_hinting, font_antialiasing, true, font_subpixel_positioning, font_disable_embedded_bitmaps);
  248. {
  249. TypedArray<Font> fallback_custom;
  250. fallback_custom.push_back(default_font_bold);
  251. custom_font->set_fallbacks(fallback_custom);
  252. }
  253. bold_fc->set_base_font(custom_font);
  254. bold_fc->set_variation_embolden(embolden_strength);
  255. } else {
  256. EditorSettings::get_singleton()->set_manually("interface/editor/main_font_bold", "");
  257. bold_fc->set_base_font(default_font_bold);
  258. }
  259. bold_fc->set_spacing(TextServer::SPACING_TOP, -EDSCALE);
  260. bold_fc->set_spacing(TextServer::SPACING_BOTTOM, -EDSCALE);
  261. Ref<FontVariation> bold_fc_msdf;
  262. bold_fc_msdf.instantiate();
  263. if (custom_font_path_bold.length() > 0 && dir->file_exists(custom_font_path_bold)) {
  264. 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);
  265. {
  266. TypedArray<Font> fallback_custom;
  267. fallback_custom.push_back(default_font_bold_msdf);
  268. custom_font->set_fallbacks(fallback_custom);
  269. }
  270. bold_fc_msdf->set_base_font(custom_font);
  271. } else if (custom_font_path.length() > 0 && dir->file_exists(custom_font_path)) {
  272. 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);
  273. {
  274. TypedArray<Font> fallback_custom;
  275. fallback_custom.push_back(default_font_bold_msdf);
  276. custom_font->set_fallbacks(fallback_custom);
  277. }
  278. bold_fc_msdf->set_base_font(custom_font);
  279. bold_fc_msdf->set_variation_embolden(embolden_strength);
  280. } else {
  281. EditorSettings::get_singleton()->set_manually("interface/editor/main_font_bold", "");
  282. bold_fc_msdf->set_base_font(default_font_bold_msdf);
  283. }
  284. bold_fc_msdf->set_spacing(TextServer::SPACING_TOP, -EDSCALE);
  285. bold_fc_msdf->set_spacing(TextServer::SPACING_BOTTOM, -EDSCALE);
  286. Ref<FontVariation> mono_fc;
  287. mono_fc.instantiate();
  288. if (custom_font_path_source.length() > 0 && dir->file_exists(custom_font_path_source)) {
  289. Ref<FontFile> custom_font = load_external_font(custom_font_path_source, font_mono_hinting, font_antialiasing, true, font_subpixel_positioning, font_disable_embedded_bitmaps);
  290. {
  291. TypedArray<Font> fallback_custom;
  292. fallback_custom.push_back(default_font_mono);
  293. custom_font->set_fallbacks(fallback_custom);
  294. }
  295. mono_fc->set_base_font(custom_font);
  296. } else {
  297. EditorSettings::get_singleton()->set_manually("interface/editor/code_font", "");
  298. mono_fc->set_base_font(default_font_mono);
  299. }
  300. mono_fc->set_spacing(TextServer::SPACING_TOP, -EDSCALE);
  301. mono_fc->set_spacing(TextServer::SPACING_BOTTOM, -EDSCALE);
  302. Ref<FontVariation> mono_other_fc = mono_fc->duplicate();
  303. // Enable contextual alternates (coding ligatures) and custom features for the source editor font.
  304. int ot_mode = EDITOR_GET("interface/editor/code_font_contextual_ligatures");
  305. switch (ot_mode) {
  306. case 1: { // Disable ligatures.
  307. Dictionary ftrs;
  308. ftrs[TS->name_to_tag("calt")] = 0;
  309. mono_fc->set_opentype_features(ftrs);
  310. } break;
  311. case 2: { // Custom.
  312. Vector<String> subtag = String(EDITOR_GET("interface/editor/code_font_custom_opentype_features")).split(",");
  313. Dictionary ftrs;
  314. for (int i = 0; i < subtag.size(); i++) {
  315. Vector<String> subtag_a = subtag[i].split("=");
  316. if (subtag_a.size() == 2) {
  317. ftrs[TS->name_to_tag(subtag_a[0])] = subtag_a[1].to_int();
  318. } else if (subtag_a.size() == 1) {
  319. ftrs[TS->name_to_tag(subtag_a[0])] = 1;
  320. }
  321. }
  322. mono_fc->set_opentype_features(ftrs);
  323. } break;
  324. default: { // Enabled.
  325. Dictionary ftrs;
  326. ftrs[TS->name_to_tag("calt")] = 1;
  327. mono_fc->set_opentype_features(ftrs);
  328. } break;
  329. }
  330. {
  331. // Disable contextual alternates (coding ligatures).
  332. Dictionary ftrs;
  333. ftrs[TS->name_to_tag("calt")] = 0;
  334. mono_other_fc->set_opentype_features(ftrs);
  335. }
  336. // Use fake bold/italics to style the editor log's `print_rich()` output.
  337. // Use stronger embolden strength to make bold easier to distinguish from regular text.
  338. Ref<FontVariation> mono_other_fc_bold = mono_other_fc->duplicate();
  339. mono_other_fc_bold->set_variation_embolden(0.8);
  340. Ref<FontVariation> mono_other_fc_italic = mono_other_fc->duplicate();
  341. mono_other_fc_italic->set_variation_transform(Transform2D(1.0, 0.2, 0.0, 1.0, 0.0, 0.0));
  342. Ref<FontVariation> mono_other_fc_bold_italic = mono_other_fc->duplicate();
  343. mono_other_fc_bold_italic->set_variation_embolden(0.8);
  344. mono_other_fc_bold_italic->set_variation_transform(Transform2D(1.0, 0.2, 0.0, 1.0, 0.0, 0.0));
  345. Ref<FontVariation> mono_other_fc_mono = mono_other_fc->duplicate();
  346. // Use a different font style to distinguish `[code]` in rich prints.
  347. // This emulates the "faint" styling used in ANSI escape codes by using a slightly thinner font.
  348. mono_other_fc_mono->set_variation_embolden(-0.25);
  349. mono_other_fc_mono->set_variation_transform(Transform2D(1.0, 0.1, 0.0, 1.0, 0.0, 0.0));
  350. Ref<FontVariation> italic_fc = default_fc->duplicate();
  351. italic_fc->set_variation_transform(Transform2D(1.0, 0.2, 0.0, 1.0, 0.0, 0.0));
  352. Ref<FontVariation> bold_italic_fc = bold_fc->duplicate();
  353. bold_italic_fc->set_variation_transform(Transform2D(1.0, 0.2, 0.0, 1.0, 0.0, 0.0));
  354. // Setup theme.
  355. p_theme->set_default_font(default_fc); // Default theme font config.
  356. p_theme->set_default_font_size(default_font_size);
  357. // Main font.
  358. p_theme->set_font("main", EditorStringName(EditorFonts), default_fc);
  359. p_theme->set_font("main_msdf", EditorStringName(EditorFonts), default_fc_msdf);
  360. p_theme->set_font_size("main_size", EditorStringName(EditorFonts), default_font_size);
  361. p_theme->set_font("bold", EditorStringName(EditorFonts), bold_fc);
  362. p_theme->set_font("main_bold_msdf", EditorStringName(EditorFonts), bold_fc_msdf);
  363. p_theme->set_font_size("bold_size", EditorStringName(EditorFonts), default_font_size);
  364. p_theme->set_font("italic", EditorStringName(EditorFonts), italic_fc);
  365. p_theme->set_font_size("italic_size", EditorStringName(EditorFonts), default_font_size);
  366. // Title font.
  367. p_theme->set_font("title", EditorStringName(EditorFonts), bold_fc);
  368. p_theme->set_font_size("title_size", EditorStringName(EditorFonts), default_font_size + 1 * EDSCALE);
  369. p_theme->set_type_variation("MainScreenButton", "Button");
  370. p_theme->set_font(SceneStringName(font), "MainScreenButton", bold_fc);
  371. p_theme->set_font_size(SceneStringName(font_size), "MainScreenButton", default_font_size + 2 * EDSCALE);
  372. // Labels.
  373. p_theme->set_font(SceneStringName(font), "Label", default_fc);
  374. p_theme->set_type_variation("HeaderSmall", "Label");
  375. p_theme->set_font(SceneStringName(font), "HeaderSmall", bold_fc);
  376. p_theme->set_font_size(SceneStringName(font_size), "HeaderSmall", default_font_size);
  377. p_theme->set_type_variation("HeaderMedium", "Label");
  378. p_theme->set_font(SceneStringName(font), "HeaderMedium", bold_fc);
  379. p_theme->set_font_size(SceneStringName(font_size), "HeaderMedium", default_font_size + 1 * EDSCALE);
  380. p_theme->set_type_variation("HeaderLarge", "Label");
  381. p_theme->set_font(SceneStringName(font), "HeaderLarge", bold_fc);
  382. p_theme->set_font_size(SceneStringName(font_size), "HeaderLarge", default_font_size + 3 * EDSCALE);
  383. p_theme->set_font("normal_font", "RichTextLabel", default_fc);
  384. p_theme->set_font("bold_font", "RichTextLabel", bold_fc);
  385. p_theme->set_font("italics_font", "RichTextLabel", italic_fc);
  386. p_theme->set_font("bold_italics_font", "RichTextLabel", bold_italic_fc);
  387. // Documentation fonts
  388. p_theme->set_font_size("doc_size", EditorStringName(EditorFonts), int(EDITOR_GET("text_editor/help/help_font_size")) * EDSCALE);
  389. p_theme->set_font("doc", EditorStringName(EditorFonts), default_fc);
  390. p_theme->set_font("doc_bold", EditorStringName(EditorFonts), bold_fc);
  391. p_theme->set_font("doc_italic", EditorStringName(EditorFonts), italic_fc);
  392. p_theme->set_font_size("doc_title_size", EditorStringName(EditorFonts), int(EDITOR_GET("text_editor/help/help_title_font_size")) * EDSCALE);
  393. p_theme->set_font("doc_title", EditorStringName(EditorFonts), bold_fc);
  394. p_theme->set_font_size("doc_source_size", EditorStringName(EditorFonts), int(EDITOR_GET("text_editor/help/help_source_font_size")) * EDSCALE);
  395. p_theme->set_font("doc_source", EditorStringName(EditorFonts), mono_fc);
  396. p_theme->set_font_size("doc_keyboard_size", EditorStringName(EditorFonts), (int(EDITOR_GET("text_editor/help/help_source_font_size")) - 1) * EDSCALE);
  397. p_theme->set_font("doc_keyboard", EditorStringName(EditorFonts), mono_fc);
  398. // Ruler font
  399. p_theme->set_font_size("rulers_size", EditorStringName(EditorFonts), 8 * EDSCALE);
  400. p_theme->set_font("rulers", EditorStringName(EditorFonts), default_fc);
  401. // Rotation widget font
  402. p_theme->set_font_size("rotation_control_size", EditorStringName(EditorFonts), 13 * EDSCALE);
  403. p_theme->set_font("rotation_control", EditorStringName(EditorFonts), default_fc);
  404. // Code font
  405. p_theme->set_font_size("source_size", EditorStringName(EditorFonts), int(EDITOR_GET("interface/editor/code_font_size")) * EDSCALE);
  406. p_theme->set_font("source", EditorStringName(EditorFonts), mono_fc);
  407. p_theme->set_font_size("expression_size", EditorStringName(EditorFonts), (int(EDITOR_GET("interface/editor/code_font_size")) - 1) * EDSCALE);
  408. p_theme->set_font("expression", EditorStringName(EditorFonts), mono_other_fc);
  409. p_theme->set_font_size("output_source_size", EditorStringName(EditorFonts), int(EDITOR_GET("run/output/font_size")) * EDSCALE);
  410. p_theme->set_font("output_source", EditorStringName(EditorFonts), mono_other_fc);
  411. p_theme->set_font("output_source_bold", EditorStringName(EditorFonts), mono_other_fc_bold);
  412. p_theme->set_font("output_source_italic", EditorStringName(EditorFonts), mono_other_fc_italic);
  413. p_theme->set_font("output_source_bold_italic", EditorStringName(EditorFonts), mono_other_fc_bold_italic);
  414. p_theme->set_font("output_source_mono", EditorStringName(EditorFonts), mono_other_fc_mono);
  415. p_theme->set_font_size("status_source_size", EditorStringName(EditorFonts), default_font_size);
  416. p_theme->set_font("status_source", EditorStringName(EditorFonts), mono_other_fc);
  417. }