editor_fonts.cpp 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. /*************************************************************************/
  2. /* editor_fonts.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2020 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 "doc_code_font.h"
  33. #include "doc_font.h"
  34. #include "doc_title_font.h"
  35. #include "editor_scale.h"
  36. #include "editor_settings.h"
  37. #include "scene/resources/default_theme/default_theme.h"
  38. #include "scene/resources/dynamic_font.h"
  39. static Ref<BitmapFont> make_font(int p_height, int p_ascent, int p_valign, int p_charcount, const int *p_chars, const Ref<Texture> &p_texture) {
  40. Ref<BitmapFont> font(memnew(BitmapFont));
  41. font->add_texture(p_texture);
  42. for (int i = 0; i < p_charcount; i++) {
  43. const int *c = &p_chars[i * 8];
  44. int chr = c[0];
  45. Rect2 frect;
  46. frect.pos.x = c[1];
  47. frect.pos.y = c[2];
  48. frect.size.x = c[3];
  49. frect.size.y = c[4];
  50. Point2 align(c[5], c[6] + p_valign);
  51. int advance = c[7];
  52. font->add_char(chr, 0, frect, align, advance);
  53. }
  54. font->set_height(p_height);
  55. font->set_ascent(p_ascent);
  56. return font;
  57. }
  58. #define MAKE_FALLBACKS(m_name) \
  59. m_name->add_fallback(FontArabic); \
  60. m_name->add_fallback(FontHebrew); \
  61. m_name->add_fallback(FontThai); \
  62. m_name->add_fallback(FontJapanese); \
  63. m_name->add_fallback(FontFallback);
  64. #define MAKE_DEFAULT_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_font_data(DefaultFont); \
  69. m_name->set_spacing(DynamicFont::SPACING_TOP, -1); \
  70. m_name->set_spacing(DynamicFont::SPACING_BOTTOM, -1); \
  71. MAKE_FALLBACKS(m_name);
  72. void editor_register_fonts(Ref<Theme> p_theme) {
  73. /* Droid Sans */
  74. Ref<DynamicFontData> DefaultFont;
  75. DefaultFont.instance();
  76. DefaultFont->set_font_ptr(_font_NotoSansUI_Regular, _font_NotoSansUI_Regular_size);
  77. DefaultFont->set_force_autohinter(true); //just looks better..i think?
  78. Ref<DynamicFontData> FontFallback;
  79. FontFallback.instance();
  80. FontFallback->set_font_ptr(_font_DroidSansFallback, _font_DroidSansFallback_size);
  81. FontFallback->set_force_autohinter(true); //just looks better..i think?
  82. Ref<DynamicFontData> FontJapanese;
  83. FontJapanese.instance();
  84. FontJapanese->set_font_ptr(_font_DroidSansJapanese, _font_DroidSansJapanese_size);
  85. FontJapanese->set_force_autohinter(true); //just looks better..i think?
  86. Ref<DynamicFontData> FontArabic;
  87. FontArabic.instance();
  88. FontArabic->set_font_ptr(_font_NotoNaskhArabicUI_Regular, _font_NotoNaskhArabicUI_Regular_size);
  89. FontArabic->set_force_autohinter(true); //just looks better..i think?
  90. Ref<DynamicFontData> FontHebrew;
  91. FontHebrew.instance();
  92. FontHebrew->set_font_ptr(_font_NotoSansHebrew_Regular, _font_NotoSansHebrew_Regular_size);
  93. FontHebrew->set_force_autohinter(true); //just looks better..i think?
  94. Ref<DynamicFontData> FontThai;
  95. FontThai.instance();
  96. FontThai->set_font_ptr(_font_NotoSansThaiUI_Regular, _font_NotoSansThaiUI_Regular_size);
  97. FontThai->set_force_autohinter(true); //just looks better..i think?
  98. /* Source Code Pro */
  99. Ref<DynamicFontData> dfmono;
  100. dfmono.instance();
  101. dfmono->set_font_ptr(_font_source_code_pro, _font_source_code_pro_size);
  102. //dfd->set_force_autohinter(true); //just looks better..i think?
  103. MAKE_DEFAULT_FONT(df, int(EditorSettings::get_singleton()->get("global/font_size")) * EDSCALE);
  104. p_theme->set_default_theme_font(df);
  105. // Ref<BitmapFont> doc_font = make_font(_bi_font_doc_font_height,_bi_font_doc_font_ascent,0,_bi_font_doc_font_charcount,_bi_font_doc_font_characters,p_theme->get_icon("DocFont","EditorIcons"));
  106. // Ref<BitmapFont> doc_title_font = make_font(_bi_font_doc_title_font_height,_bi_font_doc_title_font_ascent,0,_bi_font_doc_title_font_charcount,_bi_font_doc_title_font_characters,p_theme->get_icon("DocTitleFont","EditorIcons"));
  107. // Ref<BitmapFont> doc_code_font = make_font(_bi_font_doc_code_font_height,_bi_font_doc_code_font_ascent,0,_bi_font_doc_code_font_charcount,_bi_font_doc_code_font_characters,p_theme->get_icon("DocCodeFont","EditorIcons"));
  108. MAKE_DEFAULT_FONT(df_title, int(EDITOR_DEF("help/help_title_font_size", 18)) * EDSCALE);
  109. MAKE_DEFAULT_FONT(df_doc, int(EDITOR_DEF("help/help_font_size", 16)) * EDSCALE);
  110. p_theme->set_font("doc", "EditorFonts", df_doc);
  111. p_theme->set_font("doc_title", "EditorFonts", df_title);
  112. Ref<DynamicFont> df_code;
  113. df_code.instance();
  114. df_code->set_size(int(EditorSettings::get_singleton()->get("global/source_font_size")) * EDSCALE);
  115. df_code->set_font_data(dfmono);
  116. MAKE_FALLBACKS(df_code);
  117. p_theme->set_font("source", "EditorFonts", df_code);
  118. Ref<DynamicFont> df_doc_code;
  119. df_doc_code.instance();
  120. df_doc_code->set_size(int(EDITOR_DEF("help/help_source_font_size", 14)) * EDSCALE);
  121. df_doc_code->set_font_data(dfmono);
  122. MAKE_FALLBACKS(df_doc_code);
  123. p_theme->set_font("doc_source", "EditorFonts", df_doc_code);
  124. if (editor_is_hidpi()) {
  125. //replace default theme
  126. Ref<Texture> di;
  127. Ref<StyleBox> ds;
  128. fill_default_theme(p_theme, df, df_doc, di, ds, true);
  129. } else {
  130. Ref<Texture> di;
  131. Ref<StyleBox> ds;
  132. fill_default_theme(p_theme, df, df_doc, di, ds, false);
  133. }
  134. }