editor_fonts.cpp 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486
  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_string_names.h"
  33. #include "editor/settings/editor_settings.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 < std::size(var_suffix); 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. "Apple Color Emoji",
  187. "Segoe UI Emoji",
  188. "Noto Color Emoji",
  189. "Twitter Color Emoji",
  190. "OpenMoji",
  191. "EmojiOne Color"
  192. };
  193. Ref<SystemFont> emoji_font = load_system_font(emoji_font_names, font_hinting, font_antialiasing, true, font_subpixel_positioning, font_disable_embedded_bitmaps, false);
  194. fallbacks.push_back(emoji_font);
  195. fallbacks_bold.push_back(emoji_font);
  196. }
  197. default_font_bold->set_fallbacks(fallbacks_bold);
  198. default_font_bold_msdf->set_fallbacks(fallbacks_bold);
  199. 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);
  200. default_font_mono->set_fallbacks(fallbacks);
  201. // Init base font configs and load custom fonts.
  202. String custom_font_path = EDITOR_GET("interface/editor/main_font");
  203. String custom_font_path_bold = EDITOR_GET("interface/editor/main_font_bold");
  204. String custom_font_path_source = EDITOR_GET("interface/editor/code_font");
  205. Ref<FontVariation> default_fc;
  206. default_fc.instantiate();
  207. if (custom_font_path.length() > 0 && dir->file_exists(custom_font_path)) {
  208. Ref<FontFile> custom_font = load_external_font(custom_font_path, font_hinting, font_antialiasing, true, font_subpixel_positioning, font_disable_embedded_bitmaps);
  209. {
  210. TypedArray<Font> fallback_custom = { 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. Dictionary default_fc_opentype;
  221. default_fc_opentype["weight"] = 400;
  222. default_fc->set_variation_opentype(default_fc_opentype);
  223. Ref<FontVariation> default_fc_msdf;
  224. default_fc_msdf.instantiate();
  225. if (custom_font_path.length() > 0 && dir->file_exists(custom_font_path)) {
  226. 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);
  227. {
  228. TypedArray<Font> fallback_custom = { default_font_msdf };
  229. custom_font->set_fallbacks(fallback_custom);
  230. }
  231. default_fc_msdf->set_base_font(custom_font);
  232. } else {
  233. EditorSettings::get_singleton()->set_manually("interface/editor/main_font", "");
  234. default_fc_msdf->set_base_font(default_font_msdf);
  235. }
  236. default_fc_msdf->set_spacing(TextServer::SPACING_TOP, -EDSCALE);
  237. default_fc_msdf->set_spacing(TextServer::SPACING_BOTTOM, -EDSCALE);
  238. default_fc_msdf->set_variation_opentype(default_fc_opentype);
  239. Ref<FontVariation> bold_fc;
  240. bold_fc.instantiate();
  241. if (custom_font_path_bold.length() > 0 && dir->file_exists(custom_font_path_bold)) {
  242. Ref<FontFile> custom_font = load_external_font(custom_font_path_bold, font_hinting, font_antialiasing, true, font_subpixel_positioning, font_disable_embedded_bitmaps);
  243. {
  244. TypedArray<Font> fallback_custom = { default_font_bold };
  245. custom_font->set_fallbacks(fallback_custom);
  246. }
  247. bold_fc->set_base_font(custom_font);
  248. } else if (custom_font_path.length() > 0 && dir->file_exists(custom_font_path)) {
  249. Ref<FontFile> custom_font = load_external_font(custom_font_path, font_hinting, font_antialiasing, true, font_subpixel_positioning, font_disable_embedded_bitmaps);
  250. {
  251. TypedArray<Font> fallback_custom = { default_font_bold };
  252. custom_font->set_fallbacks(fallback_custom);
  253. }
  254. bold_fc->set_base_font(custom_font);
  255. bold_fc->set_variation_embolden(embolden_strength);
  256. } else {
  257. EditorSettings::get_singleton()->set_manually("interface/editor/main_font_bold", "");
  258. bold_fc->set_base_font(default_font_bold);
  259. }
  260. bold_fc->set_spacing(TextServer::SPACING_TOP, -EDSCALE);
  261. bold_fc->set_spacing(TextServer::SPACING_BOTTOM, -EDSCALE);
  262. Dictionary bold_fc_opentype;
  263. bold_fc_opentype["weight"] = 700;
  264. bold_fc->set_variation_opentype(bold_fc_opentype);
  265. Ref<FontVariation> bold_fc_msdf;
  266. bold_fc_msdf.instantiate();
  267. if (custom_font_path_bold.length() > 0 && dir->file_exists(custom_font_path_bold)) {
  268. 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);
  269. {
  270. TypedArray<Font> fallback_custom = { default_font_bold_msdf };
  271. custom_font->set_fallbacks(fallback_custom);
  272. }
  273. bold_fc_msdf->set_base_font(custom_font);
  274. } else if (custom_font_path.length() > 0 && dir->file_exists(custom_font_path)) {
  275. 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);
  276. {
  277. TypedArray<Font> fallback_custom = { default_font_bold_msdf };
  278. custom_font->set_fallbacks(fallback_custom);
  279. }
  280. bold_fc_msdf->set_base_font(custom_font);
  281. bold_fc_msdf->set_variation_embolden(embolden_strength);
  282. } else {
  283. EditorSettings::get_singleton()->set_manually("interface/editor/main_font_bold", "");
  284. bold_fc_msdf->set_base_font(default_font_bold_msdf);
  285. }
  286. bold_fc_msdf->set_spacing(TextServer::SPACING_TOP, -EDSCALE);
  287. bold_fc_msdf->set_spacing(TextServer::SPACING_BOTTOM, -EDSCALE);
  288. bold_fc_msdf->set_variation_opentype(bold_fc_opentype);
  289. Ref<FontVariation> mono_fc;
  290. mono_fc.instantiate();
  291. if (custom_font_path_source.length() > 0 && dir->file_exists(custom_font_path_source)) {
  292. Ref<FontFile> custom_font = load_external_font(custom_font_path_source, font_mono_hinting, font_antialiasing, true, font_subpixel_positioning, font_disable_embedded_bitmaps);
  293. {
  294. TypedArray<Font> fallback_custom = { default_font_mono };
  295. custom_font->set_fallbacks(fallback_custom);
  296. }
  297. mono_fc->set_base_font(custom_font);
  298. } else {
  299. EditorSettings::get_singleton()->set_manually("interface/editor/code_font", "");
  300. mono_fc->set_base_font(default_font_mono);
  301. }
  302. mono_fc->set_spacing(TextServer::SPACING_TOP, -EDSCALE);
  303. mono_fc->set_spacing(TextServer::SPACING_BOTTOM, -EDSCALE);
  304. Ref<FontVariation> mono_other_fc = mono_fc->duplicate();
  305. // Enable contextual alternates (coding ligatures) and custom features for the source editor font.
  306. int ot_mode = EDITOR_GET("interface/editor/code_font_contextual_ligatures");
  307. switch (ot_mode) {
  308. case 1: { // Disable ligatures.
  309. Dictionary ftrs;
  310. ftrs[TS->name_to_tag("calt")] = 0;
  311. mono_fc->set_opentype_features(ftrs);
  312. } break;
  313. case 2: { // Custom.
  314. Vector<String> subtag = String(EDITOR_GET("interface/editor/code_font_custom_opentype_features")).split(",");
  315. Dictionary ftrs;
  316. for (int i = 0; i < subtag.size(); i++) {
  317. Vector<String> subtag_a = subtag[i].split("=");
  318. if (subtag_a.size() == 2) {
  319. ftrs[TS->name_to_tag(subtag_a[0])] = subtag_a[1].to_int();
  320. } else if (subtag_a.size() == 1) {
  321. ftrs[TS->name_to_tag(subtag_a[0])] = 1;
  322. }
  323. }
  324. mono_fc->set_opentype_features(ftrs);
  325. } break;
  326. default: { // Enabled.
  327. Dictionary ftrs;
  328. ftrs[TS->name_to_tag("calt")] = 1;
  329. mono_fc->set_opentype_features(ftrs);
  330. } break;
  331. }
  332. {
  333. // Disable contextual alternates (coding ligatures).
  334. Dictionary ftrs;
  335. ftrs[TS->name_to_tag("calt")] = 0;
  336. mono_other_fc->set_opentype_features(ftrs);
  337. }
  338. // Use fake bold/italics to style the editor log's `print_rich()` output.
  339. // Use stronger embolden strength to make bold easier to distinguish from regular text.
  340. Ref<FontVariation> mono_other_fc_bold = mono_other_fc->duplicate();
  341. mono_other_fc_bold->set_variation_embolden(0.8);
  342. Ref<FontVariation> mono_other_fc_italic = mono_other_fc->duplicate();
  343. mono_other_fc_italic->set_variation_transform(Transform2D(1.0, 0.2, 0.0, 1.0, 0.0, 0.0));
  344. Ref<FontVariation> mono_other_fc_bold_italic = mono_other_fc->duplicate();
  345. mono_other_fc_bold_italic->set_variation_embolden(0.8);
  346. mono_other_fc_bold_italic->set_variation_transform(Transform2D(1.0, 0.2, 0.0, 1.0, 0.0, 0.0));
  347. Ref<FontVariation> mono_other_fc_mono = mono_other_fc->duplicate();
  348. // Use a different font style to distinguish `[code]` in rich prints.
  349. // This emulates the "faint" styling used in ANSI escape codes by using a slightly thinner font.
  350. mono_other_fc_mono->set_variation_embolden(-0.25);
  351. mono_other_fc_mono->set_variation_transform(Transform2D(1.0, 0.1, 0.0, 1.0, 0.0, 0.0));
  352. Ref<FontVariation> italic_fc = default_fc->duplicate();
  353. italic_fc->set_variation_transform(Transform2D(1.0, 0.2, 0.0, 1.0, 0.0, 0.0));
  354. Ref<FontVariation> bold_italic_fc = bold_fc->duplicate();
  355. bold_italic_fc->set_variation_transform(Transform2D(1.0, 0.2, 0.0, 1.0, 0.0, 0.0));
  356. // Setup theme.
  357. p_theme->set_default_font(default_fc); // Default theme font config.
  358. p_theme->set_default_font_size(default_font_size);
  359. // Main font.
  360. p_theme->set_font("main", EditorStringName(EditorFonts), default_fc);
  361. p_theme->set_font("main_msdf", EditorStringName(EditorFonts), default_fc_msdf);
  362. p_theme->set_font_size("main_size", EditorStringName(EditorFonts), default_font_size);
  363. p_theme->set_font("bold", EditorStringName(EditorFonts), bold_fc);
  364. p_theme->set_font("main_bold_msdf", EditorStringName(EditorFonts), bold_fc_msdf);
  365. p_theme->set_font_size("bold_size", EditorStringName(EditorFonts), default_font_size);
  366. p_theme->set_font("italic", EditorStringName(EditorFonts), italic_fc);
  367. p_theme->set_font_size("italic_size", EditorStringName(EditorFonts), default_font_size);
  368. // Title font.
  369. p_theme->set_font("title", EditorStringName(EditorFonts), bold_fc);
  370. p_theme->set_font_size("title_size", EditorStringName(EditorFonts), default_font_size + 1 * EDSCALE);
  371. p_theme->set_type_variation("MainScreenButton", "Button");
  372. p_theme->set_font(SceneStringName(font), "MainScreenButton", bold_fc);
  373. p_theme->set_font_size(SceneStringName(font_size), "MainScreenButton", default_font_size + 2 * EDSCALE);
  374. // Labels.
  375. p_theme->set_font(SceneStringName(font), "Label", default_fc);
  376. p_theme->set_type_variation("HeaderSmall", "Label");
  377. p_theme->set_font(SceneStringName(font), "HeaderSmall", bold_fc);
  378. p_theme->set_font_size(SceneStringName(font_size), "HeaderSmall", default_font_size);
  379. p_theme->set_type_variation("HeaderMedium", "Label");
  380. p_theme->set_font(SceneStringName(font), "HeaderMedium", bold_fc);
  381. p_theme->set_font_size(SceneStringName(font_size), "HeaderMedium", default_font_size + 1 * EDSCALE);
  382. p_theme->set_type_variation("HeaderLarge", "Label");
  383. p_theme->set_font(SceneStringName(font), "HeaderLarge", bold_fc);
  384. p_theme->set_font_size(SceneStringName(font_size), "HeaderLarge", default_font_size + 3 * EDSCALE);
  385. p_theme->set_font("normal_font", "RichTextLabel", default_fc);
  386. p_theme->set_font("bold_font", "RichTextLabel", bold_fc);
  387. p_theme->set_font("italics_font", "RichTextLabel", italic_fc);
  388. p_theme->set_font("bold_italics_font", "RichTextLabel", bold_italic_fc);
  389. // Documentation fonts
  390. p_theme->set_font_size("doc_size", EditorStringName(EditorFonts), int(EDITOR_GET("text_editor/help/help_font_size")) * EDSCALE);
  391. p_theme->set_font("doc", EditorStringName(EditorFonts), default_fc);
  392. p_theme->set_font("doc_bold", EditorStringName(EditorFonts), bold_fc);
  393. p_theme->set_font("doc_italic", EditorStringName(EditorFonts), italic_fc);
  394. p_theme->set_font_size("doc_title_size", EditorStringName(EditorFonts), int(EDITOR_GET("text_editor/help/help_title_font_size")) * EDSCALE);
  395. p_theme->set_font("doc_title", EditorStringName(EditorFonts), bold_fc);
  396. p_theme->set_font_size("doc_source_size", EditorStringName(EditorFonts), int(EDITOR_GET("text_editor/help/help_source_font_size")) * EDSCALE);
  397. p_theme->set_font("doc_source", EditorStringName(EditorFonts), mono_fc);
  398. p_theme->set_font_size("doc_keyboard_size", EditorStringName(EditorFonts), (int(EDITOR_GET("text_editor/help/help_source_font_size")) - 1) * EDSCALE);
  399. p_theme->set_font("doc_keyboard", EditorStringName(EditorFonts), mono_fc);
  400. // Ruler font
  401. p_theme->set_font_size("rulers_size", EditorStringName(EditorFonts), 8 * EDSCALE);
  402. p_theme->set_font("rulers", EditorStringName(EditorFonts), default_fc);
  403. // Rotation widget font
  404. p_theme->set_font_size("rotation_control_size", EditorStringName(EditorFonts), 13 * EDSCALE);
  405. p_theme->set_font("rotation_control", EditorStringName(EditorFonts), default_fc);
  406. // Code font
  407. p_theme->set_font_size("source_size", EditorStringName(EditorFonts), int(EDITOR_GET("interface/editor/code_font_size")) * EDSCALE);
  408. p_theme->set_font("source", EditorStringName(EditorFonts), mono_fc);
  409. p_theme->set_font_size("expression_size", EditorStringName(EditorFonts), (int(EDITOR_GET("interface/editor/code_font_size")) - 1) * EDSCALE);
  410. p_theme->set_font("expression", EditorStringName(EditorFonts), mono_other_fc);
  411. p_theme->set_font_size("output_source_size", EditorStringName(EditorFonts), int(EDITOR_GET("run/output/font_size")) * EDSCALE);
  412. p_theme->set_font("output_source", EditorStringName(EditorFonts), mono_other_fc);
  413. p_theme->set_font("output_source_bold", EditorStringName(EditorFonts), mono_other_fc_bold);
  414. p_theme->set_font("output_source_italic", EditorStringName(EditorFonts), mono_other_fc_italic);
  415. p_theme->set_font("output_source_bold_italic", EditorStringName(EditorFonts), mono_other_fc_bold_italic);
  416. p_theme->set_font("output_source_mono", EditorStringName(EditorFonts), mono_other_fc_mono);
  417. p_theme->set_font_size("status_source_size", EditorStringName(EditorFonts), default_font_size);
  418. p_theme->set_font("status_source", EditorStringName(EditorFonts), mono_other_fc);
  419. }