editor_fonts.cpp 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466
  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. TextServer::Hinting font_hinting;
  101. TextServer::Hinting font_mono_hinting;
  102. switch (font_hinting_setting) {
  103. case 0:
  104. // The "Auto" setting uses the setting that best matches the OS' font rendering:
  105. // - macOS doesn't use font hinting.
  106. // - Windows uses ClearType, which is in between "Light" and "Normal" hinting.
  107. // - Linux has configurable font hinting, but most distributions including Ubuntu default to "Light".
  108. #ifdef MACOS_ENABLED
  109. font_hinting = TextServer::HINTING_NONE;
  110. font_mono_hinting = TextServer::HINTING_NONE;
  111. #else
  112. font_hinting = TextServer::HINTING_LIGHT;
  113. font_mono_hinting = TextServer::HINTING_LIGHT;
  114. #endif
  115. break;
  116. case 1:
  117. font_hinting = TextServer::HINTING_NONE;
  118. font_mono_hinting = TextServer::HINTING_NONE;
  119. break;
  120. case 2:
  121. font_hinting = TextServer::HINTING_LIGHT;
  122. font_mono_hinting = TextServer::HINTING_LIGHT;
  123. break;
  124. default:
  125. font_hinting = TextServer::HINTING_NORMAL;
  126. font_mono_hinting = TextServer::HINTING_LIGHT;
  127. break;
  128. }
  129. // Load built-in fonts.
  130. const int default_font_size = int(EDITOR_GET("interface/editor/main_font_size")) * EDSCALE;
  131. const float embolden_strength = 0.6;
  132. 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);
  133. 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, true);
  134. TypedArray<Font> fallbacks;
  135. Ref<FontFile> arabic_font = load_internal_font(_font_NotoNaskhArabicUI_Regular, _font_NotoNaskhArabicUI_Regular_size, font_hinting, font_antialiasing, true, font_subpixel_positioning, font_disable_embedded_bitmaps, false, &fallbacks);
  136. 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);
  137. 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);
  138. 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);
  139. 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);
  140. 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);
  141. 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);
  142. 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);
  143. 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);
  144. 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);
  145. 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);
  146. 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);
  147. 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);
  148. default_font->set_fallbacks(fallbacks);
  149. default_font_msdf->set_fallbacks(fallbacks);
  150. 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);
  151. 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, true);
  152. TypedArray<Font> fallbacks_bold;
  153. Ref<FontFile> arabic_font_bold = load_internal_font(_font_NotoNaskhArabicUI_Bold, _font_NotoNaskhArabicUI_Bold_size, font_hinting, font_antialiasing, true, font_subpixel_positioning, font_disable_embedded_bitmaps, false, &fallbacks_bold);
  154. 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);
  155. 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);
  156. 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);
  157. 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);
  158. 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);
  159. 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);
  160. 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);
  161. 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);
  162. 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);
  163. 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);
  164. Ref<FontVariation> fallback_font_bold = make_bold_font(fallback_font, embolden_strength, &fallbacks_bold);
  165. Ref<FontVariation> japanese_font_bold = make_bold_font(japanese_font, embolden_strength, &fallbacks_bold);
  166. if (OS::get_singleton()->has_feature("system_fonts")) {
  167. PackedStringArray emoji_font_names;
  168. emoji_font_names.push_back("Apple Color Emoji");
  169. emoji_font_names.push_back("Segoe UI Emoji");
  170. emoji_font_names.push_back("Noto Color Emoji");
  171. emoji_font_names.push_back("Twitter Color Emoji");
  172. emoji_font_names.push_back("OpenMoji");
  173. emoji_font_names.push_back("EmojiOne Color");
  174. Ref<SystemFont> emoji_font = load_system_font(emoji_font_names, font_hinting, font_antialiasing, true, font_subpixel_positioning, font_disable_embedded_bitmaps, false);
  175. fallbacks.push_back(emoji_font);
  176. fallbacks_bold.push_back(emoji_font);
  177. }
  178. default_font_bold->set_fallbacks(fallbacks_bold);
  179. default_font_bold_msdf->set_fallbacks(fallbacks_bold);
  180. 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);
  181. default_font_mono->set_fallbacks(fallbacks);
  182. // Init base font configs and load custom fonts.
  183. String custom_font_path = EDITOR_GET("interface/editor/main_font");
  184. String custom_font_path_bold = EDITOR_GET("interface/editor/main_font_bold");
  185. String custom_font_path_source = EDITOR_GET("interface/editor/code_font");
  186. Ref<FontVariation> default_fc;
  187. default_fc.instantiate();
  188. if (custom_font_path.length() > 0 && dir->file_exists(custom_font_path)) {
  189. Ref<FontFile> custom_font = load_external_font(custom_font_path, font_hinting, font_antialiasing, true, font_subpixel_positioning, font_disable_embedded_bitmaps);
  190. {
  191. TypedArray<Font> fallback_custom;
  192. fallback_custom.push_back(default_font);
  193. custom_font->set_fallbacks(fallback_custom);
  194. }
  195. default_fc->set_base_font(custom_font);
  196. } else {
  197. EditorSettings::get_singleton()->set_manually("interface/editor/main_font", "");
  198. default_fc->set_base_font(default_font);
  199. }
  200. default_fc->set_spacing(TextServer::SPACING_TOP, -EDSCALE);
  201. default_fc->set_spacing(TextServer::SPACING_BOTTOM, -EDSCALE);
  202. Ref<FontVariation> default_fc_msdf;
  203. default_fc_msdf.instantiate();
  204. if (custom_font_path.length() > 0 && dir->file_exists(custom_font_path)) {
  205. Ref<FontFile> custom_font = load_external_font(custom_font_path, font_hinting, font_antialiasing, true, font_subpixel_positioning, font_disable_embedded_bitmaps);
  206. {
  207. TypedArray<Font> fallback_custom;
  208. fallback_custom.push_back(default_font_msdf);
  209. custom_font->set_fallbacks(fallback_custom);
  210. }
  211. default_fc_msdf->set_base_font(custom_font);
  212. } else {
  213. EditorSettings::get_singleton()->set_manually("interface/editor/main_font", "");
  214. default_fc_msdf->set_base_font(default_font_msdf);
  215. }
  216. default_fc_msdf->set_spacing(TextServer::SPACING_TOP, -EDSCALE);
  217. default_fc_msdf->set_spacing(TextServer::SPACING_BOTTOM, -EDSCALE);
  218. Ref<FontVariation> bold_fc;
  219. bold_fc.instantiate();
  220. if (custom_font_path_bold.length() > 0 && dir->file_exists(custom_font_path_bold)) {
  221. Ref<FontFile> custom_font = load_external_font(custom_font_path_bold, font_hinting, font_antialiasing, true, font_subpixel_positioning, font_disable_embedded_bitmaps);
  222. {
  223. TypedArray<Font> fallback_custom;
  224. fallback_custom.push_back(default_font_bold);
  225. custom_font->set_fallbacks(fallback_custom);
  226. }
  227. bold_fc->set_base_font(custom_font);
  228. } else if (custom_font_path.length() > 0 && dir->file_exists(custom_font_path)) {
  229. Ref<FontFile> custom_font = load_external_font(custom_font_path, font_hinting, font_antialiasing, true, font_subpixel_positioning, font_disable_embedded_bitmaps);
  230. {
  231. TypedArray<Font> fallback_custom;
  232. fallback_custom.push_back(default_font_bold);
  233. custom_font->set_fallbacks(fallback_custom);
  234. }
  235. bold_fc->set_base_font(custom_font);
  236. bold_fc->set_variation_embolden(embolden_strength);
  237. } else {
  238. EditorSettings::get_singleton()->set_manually("interface/editor/main_font_bold", "");
  239. bold_fc->set_base_font(default_font_bold);
  240. }
  241. bold_fc->set_spacing(TextServer::SPACING_TOP, -EDSCALE);
  242. bold_fc->set_spacing(TextServer::SPACING_BOTTOM, -EDSCALE);
  243. Ref<FontVariation> bold_fc_msdf;
  244. bold_fc_msdf.instantiate();
  245. if (custom_font_path_bold.length() > 0 && dir->file_exists(custom_font_path_bold)) {
  246. Ref<FontFile> custom_font = load_external_font(custom_font_path_bold, font_hinting, font_antialiasing, true, font_subpixel_positioning, font_disable_embedded_bitmaps);
  247. {
  248. TypedArray<Font> fallback_custom;
  249. fallback_custom.push_back(default_font_bold_msdf);
  250. custom_font->set_fallbacks(fallback_custom);
  251. }
  252. bold_fc_msdf->set_base_font(custom_font);
  253. } else if (custom_font_path.length() > 0 && dir->file_exists(custom_font_path)) {
  254. Ref<FontFile> custom_font = load_external_font(custom_font_path, font_hinting, font_antialiasing, true, font_subpixel_positioning, font_disable_embedded_bitmaps);
  255. {
  256. TypedArray<Font> fallback_custom;
  257. fallback_custom.push_back(default_font_bold_msdf);
  258. custom_font->set_fallbacks(fallback_custom);
  259. }
  260. bold_fc_msdf->set_base_font(custom_font);
  261. bold_fc_msdf->set_variation_embolden(embolden_strength);
  262. } else {
  263. EditorSettings::get_singleton()->set_manually("interface/editor/main_font_bold", "");
  264. bold_fc_msdf->set_base_font(default_font_bold_msdf);
  265. }
  266. bold_fc_msdf->set_spacing(TextServer::SPACING_TOP, -EDSCALE);
  267. bold_fc_msdf->set_spacing(TextServer::SPACING_BOTTOM, -EDSCALE);
  268. Ref<FontVariation> mono_fc;
  269. mono_fc.instantiate();
  270. if (custom_font_path_source.length() > 0 && dir->file_exists(custom_font_path_source)) {
  271. Ref<FontFile> custom_font = load_external_font(custom_font_path_source, font_mono_hinting, font_antialiasing, true, font_subpixel_positioning, font_disable_embedded_bitmaps);
  272. {
  273. TypedArray<Font> fallback_custom;
  274. fallback_custom.push_back(default_font_mono);
  275. custom_font->set_fallbacks(fallback_custom);
  276. }
  277. mono_fc->set_base_font(custom_font);
  278. } else {
  279. EditorSettings::get_singleton()->set_manually("interface/editor/code_font", "");
  280. mono_fc->set_base_font(default_font_mono);
  281. }
  282. mono_fc->set_spacing(TextServer::SPACING_TOP, -EDSCALE);
  283. mono_fc->set_spacing(TextServer::SPACING_BOTTOM, -EDSCALE);
  284. Ref<FontVariation> mono_other_fc = mono_fc->duplicate();
  285. // Enable contextual alternates (coding ligatures) and custom features for the source editor font.
  286. int ot_mode = EDITOR_GET("interface/editor/code_font_contextual_ligatures");
  287. switch (ot_mode) {
  288. case 1: { // Disable ligatures.
  289. Dictionary ftrs;
  290. ftrs[TS->name_to_tag("calt")] = 0;
  291. mono_fc->set_opentype_features(ftrs);
  292. } break;
  293. case 2: { // Custom.
  294. Vector<String> subtag = String(EDITOR_GET("interface/editor/code_font_custom_opentype_features")).split(",");
  295. Dictionary ftrs;
  296. for (int i = 0; i < subtag.size(); i++) {
  297. Vector<String> subtag_a = subtag[i].split("=");
  298. if (subtag_a.size() == 2) {
  299. ftrs[TS->name_to_tag(subtag_a[0])] = subtag_a[1].to_int();
  300. } else if (subtag_a.size() == 1) {
  301. ftrs[TS->name_to_tag(subtag_a[0])] = 1;
  302. }
  303. }
  304. mono_fc->set_opentype_features(ftrs);
  305. } break;
  306. default: { // Enabled.
  307. Dictionary ftrs;
  308. ftrs[TS->name_to_tag("calt")] = 1;
  309. mono_fc->set_opentype_features(ftrs);
  310. } break;
  311. }
  312. {
  313. // Disable contextual alternates (coding ligatures).
  314. Dictionary ftrs;
  315. ftrs[TS->name_to_tag("calt")] = 0;
  316. mono_other_fc->set_opentype_features(ftrs);
  317. }
  318. // Use fake bold/italics to style the editor log's `print_rich()` output.
  319. // Use stronger embolden strength to make bold easier to distinguish from regular text.
  320. Ref<FontVariation> mono_other_fc_bold = mono_other_fc->duplicate();
  321. mono_other_fc_bold->set_variation_embolden(0.8);
  322. Ref<FontVariation> mono_other_fc_italic = mono_other_fc->duplicate();
  323. mono_other_fc_italic->set_variation_transform(Transform2D(1.0, 0.2, 0.0, 1.0, 0.0, 0.0));
  324. Ref<FontVariation> mono_other_fc_bold_italic = mono_other_fc->duplicate();
  325. mono_other_fc_bold_italic->set_variation_embolden(0.8);
  326. mono_other_fc_bold_italic->set_variation_transform(Transform2D(1.0, 0.2, 0.0, 1.0, 0.0, 0.0));
  327. Ref<FontVariation> mono_other_fc_mono = mono_other_fc->duplicate();
  328. // Use a different font style to distinguish `[code]` in rich prints.
  329. // This emulates the "faint" styling used in ANSI escape codes by using a slightly thinner font.
  330. mono_other_fc_mono->set_variation_embolden(-0.25);
  331. mono_other_fc_mono->set_variation_transform(Transform2D(1.0, 0.1, 0.0, 1.0, 0.0, 0.0));
  332. Ref<FontVariation> italic_fc = default_fc->duplicate();
  333. italic_fc->set_variation_transform(Transform2D(1.0, 0.2, 0.0, 1.0, 0.0, 0.0));
  334. Ref<FontVariation> bold_italic_fc = bold_fc->duplicate();
  335. bold_italic_fc->set_variation_transform(Transform2D(1.0, 0.2, 0.0, 1.0, 0.0, 0.0));
  336. // Setup theme.
  337. p_theme->set_default_font(default_fc); // Default theme font config.
  338. p_theme->set_default_font_size(default_font_size);
  339. // Main font.
  340. p_theme->set_font("main", EditorStringName(EditorFonts), default_fc);
  341. p_theme->set_font("main_msdf", EditorStringName(EditorFonts), default_fc_msdf);
  342. p_theme->set_font_size("main_size", EditorStringName(EditorFonts), default_font_size);
  343. p_theme->set_font("bold", EditorStringName(EditorFonts), bold_fc);
  344. p_theme->set_font("main_bold_msdf", EditorStringName(EditorFonts), bold_fc_msdf);
  345. p_theme->set_font_size("bold_size", EditorStringName(EditorFonts), default_font_size);
  346. p_theme->set_font("italic", EditorStringName(EditorFonts), italic_fc);
  347. p_theme->set_font_size("italic_size", EditorStringName(EditorFonts), default_font_size);
  348. // Title font.
  349. p_theme->set_font("title", EditorStringName(EditorFonts), bold_fc);
  350. p_theme->set_font_size("title_size", EditorStringName(EditorFonts), default_font_size + 1 * EDSCALE);
  351. p_theme->set_type_variation("MainScreenButton", "Button");
  352. p_theme->set_font(SceneStringName(font), "MainScreenButton", bold_fc);
  353. p_theme->set_font_size(SceneStringName(font_size), "MainScreenButton", default_font_size + 2 * EDSCALE);
  354. // Labels.
  355. p_theme->set_font(SceneStringName(font), "Label", default_fc);
  356. p_theme->set_type_variation("HeaderSmall", "Label");
  357. p_theme->set_font(SceneStringName(font), "HeaderSmall", bold_fc);
  358. p_theme->set_font_size(SceneStringName(font_size), "HeaderSmall", default_font_size);
  359. p_theme->set_type_variation("HeaderMedium", "Label");
  360. p_theme->set_font(SceneStringName(font), "HeaderMedium", bold_fc);
  361. p_theme->set_font_size(SceneStringName(font_size), "HeaderMedium", default_font_size + 1 * EDSCALE);
  362. p_theme->set_type_variation("HeaderLarge", "Label");
  363. p_theme->set_font(SceneStringName(font), "HeaderLarge", bold_fc);
  364. p_theme->set_font_size(SceneStringName(font_size), "HeaderLarge", default_font_size + 3 * EDSCALE);
  365. p_theme->set_font("normal_font", "RichTextLabel", default_fc);
  366. p_theme->set_font("bold_font", "RichTextLabel", bold_fc);
  367. p_theme->set_font("italics_font", "RichTextLabel", italic_fc);
  368. p_theme->set_font("bold_italics_font", "RichTextLabel", bold_italic_fc);
  369. // Documentation fonts
  370. p_theme->set_font_size("doc_size", EditorStringName(EditorFonts), int(EDITOR_GET("text_editor/help/help_font_size")) * EDSCALE);
  371. p_theme->set_font("doc", EditorStringName(EditorFonts), default_fc);
  372. p_theme->set_font("doc_bold", EditorStringName(EditorFonts), bold_fc);
  373. p_theme->set_font("doc_italic", EditorStringName(EditorFonts), italic_fc);
  374. p_theme->set_font_size("doc_title_size", EditorStringName(EditorFonts), int(EDITOR_GET("text_editor/help/help_title_font_size")) * EDSCALE);
  375. p_theme->set_font("doc_title", EditorStringName(EditorFonts), bold_fc);
  376. p_theme->set_font_size("doc_source_size", EditorStringName(EditorFonts), int(EDITOR_GET("text_editor/help/help_source_font_size")) * EDSCALE);
  377. p_theme->set_font("doc_source", EditorStringName(EditorFonts), mono_fc);
  378. p_theme->set_font_size("doc_keyboard_size", EditorStringName(EditorFonts), (int(EDITOR_GET("text_editor/help/help_source_font_size")) - 1) * EDSCALE);
  379. p_theme->set_font("doc_keyboard", EditorStringName(EditorFonts), mono_fc);
  380. // Ruler font
  381. p_theme->set_font_size("rulers_size", EditorStringName(EditorFonts), 8 * EDSCALE);
  382. p_theme->set_font("rulers", EditorStringName(EditorFonts), default_fc);
  383. // Rotation widget font
  384. p_theme->set_font_size("rotation_control_size", EditorStringName(EditorFonts), 13 * EDSCALE);
  385. p_theme->set_font("rotation_control", EditorStringName(EditorFonts), default_fc);
  386. // Code font
  387. p_theme->set_font_size("source_size", EditorStringName(EditorFonts), int(EDITOR_GET("interface/editor/code_font_size")) * EDSCALE);
  388. p_theme->set_font("source", EditorStringName(EditorFonts), mono_fc);
  389. p_theme->set_font_size("expression_size", EditorStringName(EditorFonts), (int(EDITOR_GET("interface/editor/code_font_size")) - 1) * EDSCALE);
  390. p_theme->set_font("expression", EditorStringName(EditorFonts), mono_other_fc);
  391. p_theme->set_font_size("output_source_size", EditorStringName(EditorFonts), int(EDITOR_GET("run/output/font_size")) * EDSCALE);
  392. p_theme->set_font("output_source", EditorStringName(EditorFonts), mono_other_fc);
  393. p_theme->set_font("output_source_bold", EditorStringName(EditorFonts), mono_other_fc_bold);
  394. p_theme->set_font("output_source_italic", EditorStringName(EditorFonts), mono_other_fc_italic);
  395. p_theme->set_font("output_source_bold_italic", EditorStringName(EditorFonts), mono_other_fc_bold_italic);
  396. p_theme->set_font("output_source_mono", EditorStringName(EditorFonts), mono_other_fc_mono);
  397. p_theme->set_font_size("status_source_size", EditorStringName(EditorFonts), default_font_size);
  398. p_theme->set_font("status_source", EditorStringName(EditorFonts), mono_other_fc);
  399. }