editor_fonts.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  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 "builtin_fonts.gen.h"
  32. #include "core/os/dir_access.h"
  33. #include "editor_scale.h"
  34. #include "editor_settings.h"
  35. #include "scene/resources/default_theme/default_theme.h"
  36. #include "scene/resources/dynamic_font.h"
  37. #define MAKE_FALLBACKS(m_name) \
  38. m_name->add_fallback(FontArabic); \
  39. m_name->add_fallback(FontHebrew); \
  40. m_name->add_fallback(FontThai); \
  41. m_name->add_fallback(FontHindi); \
  42. m_name->add_fallback(FontJapanese); \
  43. m_name->add_fallback(FontFallback);
  44. // Enable filtering and mipmaps on the editor fonts to improve text appearance
  45. // in editors that are zoomed in/out without having dedicated fonts to generate.
  46. // This is the case in GraphEdit-based editors such as the visual script and
  47. // visual shader editors.
  48. // the custom spacings might only work with Noto Sans
  49. #define MAKE_DEFAULT_FONT(m_name, m_size) \
  50. Ref<DynamicFont> m_name; \
  51. m_name.instance(); \
  52. m_name->set_size(m_size); \
  53. m_name->set_use_filter(true); \
  54. m_name->set_use_mipmaps(true); \
  55. if (CustomFont.is_valid()) { \
  56. m_name->set_font_data(CustomFont); \
  57. m_name->add_fallback(DefaultFont); \
  58. } else { \
  59. m_name->set_font_data(DefaultFont); \
  60. } \
  61. m_name->set_spacing(DynamicFont::SPACING_TOP, -EDSCALE); \
  62. m_name->set_spacing(DynamicFont::SPACING_BOTTOM, -EDSCALE); \
  63. MAKE_FALLBACKS(m_name);
  64. #define MAKE_BOLD_FONT(m_name, m_size) \
  65. Ref<DynamicFont> m_name; \
  66. m_name.instance(); \
  67. m_name->set_size(m_size); \
  68. m_name->set_use_filter(true); \
  69. m_name->set_use_mipmaps(true); \
  70. if (CustomFontBold.is_valid()) { \
  71. m_name->set_font_data(CustomFontBold); \
  72. m_name->add_fallback(DefaultFontBold); \
  73. } else { \
  74. m_name->set_font_data(DefaultFontBold); \
  75. } \
  76. m_name->set_spacing(DynamicFont::SPACING_TOP, -EDSCALE); \
  77. m_name->set_spacing(DynamicFont::SPACING_BOTTOM, -EDSCALE); \
  78. MAKE_FALLBACKS(m_name);
  79. #define MAKE_SOURCE_FONT(m_name, m_size) \
  80. Ref<DynamicFont> m_name; \
  81. m_name.instance(); \
  82. m_name->set_size(m_size); \
  83. m_name->set_use_filter(true); \
  84. m_name->set_use_mipmaps(true); \
  85. if (CustomFontSource.is_valid()) { \
  86. m_name->set_font_data(CustomFontSource); \
  87. m_name->add_fallback(dfmono); \
  88. } else { \
  89. m_name->set_font_data(dfmono); \
  90. } \
  91. m_name->set_spacing(DynamicFont::SPACING_TOP, -EDSCALE); \
  92. m_name->set_spacing(DynamicFont::SPACING_BOTTOM, -EDSCALE); \
  93. MAKE_FALLBACKS(m_name);
  94. void editor_register_fonts(Ref<Theme> p_theme) {
  95. DirAccess *dir = DirAccess::create(DirAccess::ACCESS_FILESYSTEM);
  96. /* Custom font */
  97. bool font_antialiased = (bool)EditorSettings::get_singleton()->get("interface/editor/font_antialiased");
  98. int font_hinting_setting = (int)EditorSettings::get_singleton()->get("interface/editor/font_hinting");
  99. DynamicFontData::Hinting font_hinting;
  100. switch (font_hinting_setting) {
  101. case 0:
  102. // The "Auto" setting uses the setting that best matches the OS' font rendering:
  103. // - macOS doesn't use font hinting.
  104. // - Windows uses ClearType, which is in between "Light" and "Normal" hinting.
  105. // - Linux has configurable font hinting, but most distributions including Ubuntu default to "Light".
  106. #ifdef OSX_ENABLED
  107. font_hinting = DynamicFontData::HINTING_NONE;
  108. #else
  109. font_hinting = DynamicFontData::HINTING_LIGHT;
  110. #endif
  111. break;
  112. case 1:
  113. font_hinting = DynamicFontData::HINTING_NONE;
  114. break;
  115. case 2:
  116. font_hinting = DynamicFontData::HINTING_LIGHT;
  117. break;
  118. default:
  119. font_hinting = DynamicFontData::HINTING_NORMAL;
  120. break;
  121. }
  122. String custom_font_path = EditorSettings::get_singleton()->get("interface/editor/main_font");
  123. Ref<DynamicFontData> CustomFont;
  124. if (custom_font_path.length() > 0 && dir->file_exists(custom_font_path)) {
  125. CustomFont.instance();
  126. CustomFont->set_antialiased(font_antialiased);
  127. CustomFont->set_hinting(font_hinting);
  128. CustomFont->set_font_path(custom_font_path);
  129. CustomFont->set_force_autohinter(true); //just looks better..i think?
  130. } else {
  131. EditorSettings::get_singleton()->set_manually("interface/editor/main_font", "");
  132. }
  133. /* Custom Bold font */
  134. String custom_font_path_bold = EditorSettings::get_singleton()->get("interface/editor/main_font_bold");
  135. Ref<DynamicFontData> CustomFontBold;
  136. if (custom_font_path_bold.length() > 0 && dir->file_exists(custom_font_path_bold)) {
  137. CustomFontBold.instance();
  138. CustomFontBold->set_antialiased(font_antialiased);
  139. CustomFontBold->set_hinting(font_hinting);
  140. CustomFontBold->set_font_path(custom_font_path_bold);
  141. CustomFontBold->set_force_autohinter(true); //just looks better..i think?
  142. } else {
  143. EditorSettings::get_singleton()->set_manually("interface/editor/main_font_bold", "");
  144. }
  145. /* Custom source code font */
  146. String custom_font_path_source = EditorSettings::get_singleton()->get("interface/editor/code_font");
  147. Ref<DynamicFontData> CustomFontSource;
  148. if (custom_font_path_source.length() > 0 && dir->file_exists(custom_font_path_source)) {
  149. CustomFontSource.instance();
  150. CustomFontSource->set_antialiased(font_antialiased);
  151. CustomFontSource->set_hinting(font_hinting);
  152. CustomFontSource->set_font_path(custom_font_path_source);
  153. } else {
  154. EditorSettings::get_singleton()->set_manually("interface/editor/code_font", "");
  155. }
  156. memdelete(dir);
  157. /* Droid Sans */
  158. Ref<DynamicFontData> DefaultFont;
  159. DefaultFont.instance();
  160. DefaultFont->set_antialiased(font_antialiased);
  161. DefaultFont->set_hinting(font_hinting);
  162. DefaultFont->set_font_ptr(_font_NotoSansUI_Regular, _font_NotoSansUI_Regular_size);
  163. DefaultFont->set_force_autohinter(true); //just looks better..i think?
  164. Ref<DynamicFontData> DefaultFontBold;
  165. DefaultFontBold.instance();
  166. DefaultFontBold->set_antialiased(font_antialiased);
  167. DefaultFontBold->set_hinting(font_hinting);
  168. DefaultFontBold->set_font_ptr(_font_NotoSansUI_Bold, _font_NotoSansUI_Bold_size);
  169. DefaultFontBold->set_force_autohinter(true); // just looks better..i think?
  170. Ref<DynamicFontData> FontFallback;
  171. FontFallback.instance();
  172. FontFallback->set_antialiased(font_antialiased);
  173. FontFallback->set_hinting(font_hinting);
  174. FontFallback->set_font_ptr(_font_DroidSansFallback, _font_DroidSansFallback_size);
  175. FontFallback->set_force_autohinter(true); //just looks better..i think?
  176. Ref<DynamicFontData> FontJapanese;
  177. FontJapanese.instance();
  178. FontJapanese->set_antialiased(font_antialiased);
  179. FontJapanese->set_hinting(font_hinting);
  180. FontJapanese->set_font_ptr(_font_DroidSansJapanese, _font_DroidSansJapanese_size);
  181. FontJapanese->set_force_autohinter(true); //just looks better..i think?
  182. Ref<DynamicFontData> FontArabic;
  183. FontArabic.instance();
  184. FontArabic->set_antialiased(font_antialiased);
  185. FontArabic->set_hinting(font_hinting);
  186. FontArabic->set_font_ptr(_font_NotoNaskhArabicUI_Regular, _font_NotoNaskhArabicUI_Regular_size);
  187. FontArabic->set_force_autohinter(true); //just looks better..i think?
  188. Ref<DynamicFontData> FontHebrew;
  189. FontHebrew.instance();
  190. FontHebrew->set_antialiased(font_antialiased);
  191. FontHebrew->set_hinting(font_hinting);
  192. FontHebrew->set_font_ptr(_font_NotoSansHebrew_Regular, _font_NotoSansHebrew_Regular_size);
  193. FontHebrew->set_force_autohinter(true); //just looks better..i think?
  194. Ref<DynamicFontData> FontThai;
  195. FontThai.instance();
  196. FontThai->set_antialiased(font_antialiased);
  197. FontThai->set_hinting(font_hinting);
  198. FontThai->set_font_ptr(_font_NotoSansThaiUI_Regular, _font_NotoSansThaiUI_Regular_size);
  199. FontThai->set_force_autohinter(true); //just looks better..i think?
  200. Ref<DynamicFontData> FontHindi;
  201. FontHindi.instance();
  202. FontHindi->set_antialiased(font_antialiased);
  203. FontHindi->set_hinting(font_hinting);
  204. FontHindi->set_font_ptr(_font_NotoSansDevanagariUI_Regular, _font_NotoSansDevanagariUI_Regular_size);
  205. FontHindi->set_force_autohinter(true); //just looks better..i think?
  206. /* Hack */
  207. Ref<DynamicFontData> dfmono;
  208. dfmono.instance();
  209. dfmono->set_antialiased(font_antialiased);
  210. dfmono->set_hinting(font_hinting);
  211. dfmono->set_font_ptr(_font_Hack_Regular, _font_Hack_Regular_size);
  212. int default_font_size = int(EDITOR_GET("interface/editor/main_font_size")) * EDSCALE;
  213. // Default font
  214. MAKE_DEFAULT_FONT(df, default_font_size);
  215. p_theme->set_default_theme_font(df);
  216. p_theme->set_font("main", "EditorFonts", df);
  217. // Bold font
  218. MAKE_BOLD_FONT(df_bold, default_font_size);
  219. p_theme->set_font("bold", "EditorFonts", df_bold);
  220. // Title font
  221. MAKE_BOLD_FONT(df_title, default_font_size + 2 * EDSCALE);
  222. p_theme->set_font("title", "EditorFonts", df_title);
  223. // Documentation fonts
  224. MAKE_DEFAULT_FONT(df_doc, int(EDITOR_GET("text_editor/help/help_font_size")) * EDSCALE);
  225. MAKE_BOLD_FONT(df_doc_bold, int(EDITOR_GET("text_editor/help/help_font_size")) * EDSCALE);
  226. MAKE_BOLD_FONT(df_doc_title, int(EDITOR_GET("text_editor/help/help_title_font_size")) * EDSCALE);
  227. MAKE_SOURCE_FONT(df_doc_code, int(EDITOR_GET("text_editor/help/help_source_font_size")) * EDSCALE);
  228. p_theme->set_font("doc", "EditorFonts", df_doc);
  229. p_theme->set_font("doc_bold", "EditorFonts", df_doc_bold);
  230. p_theme->set_font("doc_title", "EditorFonts", df_doc_title);
  231. p_theme->set_font("doc_source", "EditorFonts", df_doc_code);
  232. // Ruler font
  233. MAKE_DEFAULT_FONT(df_rulers, 8 * EDSCALE);
  234. p_theme->set_font("rulers", "EditorFonts", df_rulers);
  235. // Rotation widget font
  236. MAKE_DEFAULT_FONT(df_rotation_control, 14 * EDSCALE);
  237. p_theme->set_font("rotation_control", "EditorFonts", df_rotation_control);
  238. // Code font
  239. MAKE_SOURCE_FONT(df_code, int(EDITOR_GET("interface/editor/code_font_size")) * EDSCALE);
  240. p_theme->set_font("source", "EditorFonts", df_code);
  241. MAKE_SOURCE_FONT(df_expression, (int(EDITOR_GET("interface/editor/code_font_size")) - 1) * EDSCALE);
  242. p_theme->set_font("expression", "EditorFonts", df_expression);
  243. MAKE_SOURCE_FONT(df_output_code, int(EDITOR_GET("run/output/font_size")) * EDSCALE);
  244. p_theme->set_font("output_source", "EditorFonts", df_output_code);
  245. MAKE_SOURCE_FONT(df_text_editor_status_code, default_font_size);
  246. p_theme->set_font("status_source", "EditorFonts", df_text_editor_status_code);
  247. }