editor_fonts.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  1. /*************************************************************************/
  2. /* editor_fonts.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2019 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2019 Godot Engine contributors (cf. AUTHORS.md) */
  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. // the custom spacings might only work with Noto Sans
  45. #define MAKE_DEFAULT_FONT(m_name, m_size) \
  46. Ref<DynamicFont> m_name; \
  47. m_name.instance(); \
  48. m_name->set_size(m_size); \
  49. if (CustomFont.is_valid()) { \
  50. m_name->set_font_data(CustomFont); \
  51. m_name->add_fallback(DefaultFont); \
  52. } else { \
  53. m_name->set_font_data(DefaultFont); \
  54. } \
  55. m_name->set_spacing(DynamicFont::SPACING_TOP, -EDSCALE); \
  56. m_name->set_spacing(DynamicFont::SPACING_BOTTOM, -EDSCALE); \
  57. MAKE_FALLBACKS(m_name);
  58. #define MAKE_BOLD_FONT(m_name, m_size) \
  59. Ref<DynamicFont> m_name; \
  60. m_name.instance(); \
  61. m_name->set_size(m_size); \
  62. if (CustomFont.is_valid()) { \
  63. m_name->set_font_data(CustomFontBold); \
  64. m_name->add_fallback(DefaultFontBold); \
  65. } else { \
  66. m_name->set_font_data(DefaultFontBold); \
  67. } \
  68. m_name->set_spacing(DynamicFont::SPACING_TOP, -EDSCALE); \
  69. m_name->set_spacing(DynamicFont::SPACING_BOTTOM, -EDSCALE); \
  70. MAKE_FALLBACKS(m_name);
  71. #define MAKE_SOURCE_FONT(m_name, m_size) \
  72. Ref<DynamicFont> m_name; \
  73. m_name.instance(); \
  74. m_name->set_size(m_size); \
  75. if (CustomFontSource.is_valid()) { \
  76. m_name->set_font_data(CustomFontSource); \
  77. m_name->add_fallback(dfmono); \
  78. } else { \
  79. m_name->set_font_data(dfmono); \
  80. } \
  81. m_name->set_spacing(DynamicFont::SPACING_TOP, -EDSCALE); \
  82. m_name->set_spacing(DynamicFont::SPACING_BOTTOM, -EDSCALE); \
  83. MAKE_FALLBACKS(m_name);
  84. void editor_register_fonts(Ref<Theme> p_theme) {
  85. DirAccess *dir = DirAccess::create(DirAccess::ACCESS_FILESYSTEM);
  86. /* Custom font */
  87. bool font_antialiased = (bool)EditorSettings::get_singleton()->get("interface/editor/main_font_antialiased");
  88. DynamicFontData::Hinting font_hinting = (DynamicFontData::Hinting)(int)EditorSettings::get_singleton()->get("interface/editor/main_font_hinting");
  89. String custom_font_path = EditorSettings::get_singleton()->get("interface/editor/main_font");
  90. Ref<DynamicFontData> CustomFont;
  91. if (custom_font_path.length() > 0 && dir->file_exists(custom_font_path)) {
  92. CustomFont.instance();
  93. CustomFont->set_antialiased(font_antialiased);
  94. CustomFont->set_hinting(font_hinting);
  95. CustomFont->set_font_path(custom_font_path);
  96. CustomFont->set_force_autohinter(true); //just looks better..i think?
  97. } else {
  98. EditorSettings::get_singleton()->set_manually("interface/editor/main_font", "");
  99. }
  100. /* Custom Bold font */
  101. String custom_font_path_bold = EditorSettings::get_singleton()->get("interface/editor/main_font_bold");
  102. Ref<DynamicFontData> CustomFontBold;
  103. if (custom_font_path_bold.length() > 0 && dir->file_exists(custom_font_path_bold)) {
  104. CustomFontBold.instance();
  105. CustomFontBold->set_antialiased(font_antialiased);
  106. CustomFontBold->set_hinting(font_hinting);
  107. CustomFontBold->set_font_path(custom_font_path_bold);
  108. CustomFontBold->set_force_autohinter(true); //just looks better..i think?
  109. } else {
  110. EditorSettings::get_singleton()->set_manually("interface/editor/main_font_bold", "");
  111. }
  112. /* Custom source code font */
  113. String custom_font_path_source = EditorSettings::get_singleton()->get("interface/editor/code_font");
  114. bool font_source_antialiased = (bool)EditorSettings::get_singleton()->get("interface/editor/code_font_antialiased");
  115. DynamicFontData::Hinting font_source_hinting = (DynamicFontData::Hinting)(int)EditorSettings::get_singleton()->get("interface/editor/code_font_hinting");
  116. Ref<DynamicFontData> CustomFontSource;
  117. if (custom_font_path_source.length() > 0 && dir->file_exists(custom_font_path_source)) {
  118. CustomFontSource.instance();
  119. CustomFontSource->set_antialiased(font_source_antialiased);
  120. CustomFontSource->set_hinting(font_source_hinting);
  121. CustomFontSource->set_font_path(custom_font_path_source);
  122. } else {
  123. EditorSettings::get_singleton()->set_manually("interface/editor/code_font", "");
  124. }
  125. memdelete(dir);
  126. /* Droid Sans */
  127. Ref<DynamicFontData> DefaultFont;
  128. DefaultFont.instance();
  129. DefaultFont->set_antialiased(font_antialiased);
  130. DefaultFont->set_hinting(font_hinting);
  131. DefaultFont->set_font_ptr(_font_NotoSansUI_Regular, _font_NotoSansUI_Regular_size);
  132. DefaultFont->set_force_autohinter(true); //just looks better..i think?
  133. Ref<DynamicFontData> DefaultFontBold;
  134. DefaultFontBold.instance();
  135. DefaultFontBold->set_antialiased(font_antialiased);
  136. DefaultFontBold->set_hinting(font_hinting);
  137. DefaultFontBold->set_font_ptr(_font_NotoSansUI_Bold, _font_NotoSansUI_Bold_size);
  138. DefaultFontBold->set_force_autohinter(true); // just looks better..i think?
  139. Ref<DynamicFontData> FontFallback;
  140. FontFallback.instance();
  141. FontFallback->set_antialiased(font_antialiased);
  142. FontFallback->set_hinting(font_hinting);
  143. FontFallback->set_font_ptr(_font_DroidSansFallback, _font_DroidSansFallback_size);
  144. FontFallback->set_force_autohinter(true); //just looks better..i think?
  145. Ref<DynamicFontData> FontJapanese;
  146. FontJapanese.instance();
  147. FontJapanese->set_antialiased(font_antialiased);
  148. FontJapanese->set_hinting(font_hinting);
  149. FontJapanese->set_font_ptr(_font_DroidSansJapanese, _font_DroidSansJapanese_size);
  150. FontJapanese->set_force_autohinter(true); //just looks better..i think?
  151. Ref<DynamicFontData> FontArabic;
  152. FontArabic.instance();
  153. FontArabic->set_antialiased(font_antialiased);
  154. FontArabic->set_hinting(font_hinting);
  155. FontArabic->set_font_ptr(_font_NotoNaskhArabicUI_Regular, _font_NotoNaskhArabicUI_Regular_size);
  156. FontArabic->set_force_autohinter(true); //just looks better..i think?
  157. Ref<DynamicFontData> FontHebrew;
  158. FontHebrew.instance();
  159. FontHebrew->set_antialiased(font_antialiased);
  160. FontHebrew->set_hinting(font_hinting);
  161. FontHebrew->set_font_ptr(_font_NotoSansHebrew_Regular, _font_NotoSansHebrew_Regular_size);
  162. FontHebrew->set_force_autohinter(true); //just looks better..i think?
  163. Ref<DynamicFontData> FontThai;
  164. FontThai.instance();
  165. FontThai->set_antialiased(font_antialiased);
  166. FontThai->set_hinting(font_hinting);
  167. FontThai->set_font_ptr(_font_NotoSansThaiUI_Regular, _font_NotoSansThaiUI_Regular_size);
  168. FontThai->set_force_autohinter(true); //just looks better..i think?
  169. Ref<DynamicFontData> FontHindi;
  170. FontHindi.instance();
  171. FontHindi->set_antialiased(font_antialiased);
  172. FontHindi->set_hinting(font_hinting);
  173. FontHindi->set_font_ptr(_font_NotoSansDevanagariUI_Regular, _font_NotoSansDevanagariUI_Regular_size);
  174. FontHindi->set_force_autohinter(true); //just looks better..i think?
  175. /* Hack */
  176. Ref<DynamicFontData> dfmono;
  177. dfmono.instance();
  178. dfmono->set_antialiased(font_source_antialiased);
  179. dfmono->set_hinting(font_source_hinting);
  180. dfmono->set_font_ptr(_font_Hack_Regular, _font_Hack_Regular_size);
  181. //dfd->set_force_autohinter(true); //just looks better..i think?
  182. int default_font_size = int(EditorSettings::get_singleton()->get("interface/editor/main_font_size")) * EDSCALE;
  183. // Default font
  184. MAKE_DEFAULT_FONT(df, default_font_size);
  185. p_theme->set_default_theme_font(df);
  186. // Bold font
  187. MAKE_BOLD_FONT(df_bold, default_font_size);
  188. p_theme->set_font("bold", "EditorFonts", df_bold);
  189. // Title font
  190. MAKE_BOLD_FONT(df_title, default_font_size + 2 * EDSCALE);
  191. p_theme->set_font("title", "EditorFonts", df_title);
  192. // Doc font
  193. MAKE_BOLD_FONT(df_doc_title, int(EDITOR_DEF("text_editor/help/help_title_font_size", 23)) * EDSCALE);
  194. MAKE_DEFAULT_FONT(df_doc, int(EDITOR_DEF("text_editor/help/help_font_size", 15)) * EDSCALE);
  195. p_theme->set_font("doc", "EditorFonts", df_doc);
  196. p_theme->set_font("doc_title", "EditorFonts", df_doc_title);
  197. MAKE_SOURCE_FONT(df_doc_code, int(EDITOR_DEF("text_editor/help/help_source_font_size", 14)) * EDSCALE);
  198. p_theme->set_font("doc_source", "EditorFonts", df_doc_code);
  199. // Ruler font
  200. MAKE_DEFAULT_FONT(df_rulers, 8 * EDSCALE);
  201. p_theme->set_font("rulers", "EditorFonts", df_rulers);
  202. // Code font
  203. MAKE_SOURCE_FONT(df_code, int(EditorSettings::get_singleton()->get("interface/editor/code_font_size")) * EDSCALE);
  204. p_theme->set_font("source", "EditorFonts", df_code);
  205. MAKE_SOURCE_FONT(df_output_code, int(EDITOR_DEF("run/output/font_size", 13)) * EDSCALE);
  206. p_theme->set_font("output_source", "EditorFonts", df_output_code);
  207. MAKE_SOURCE_FONT(df_text_editor_status_code, 14 * EDSCALE);
  208. p_theme->set_font("status_source", "EditorFonts", df_text_editor_status_code);
  209. }