editor_fonts.cpp 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. /*************************************************************************/
  2. /* editor_fonts.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2017 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2017 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.position.x = c[1];
  47. frect.position.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. // the custom spacings might only work with Noto Sans
  65. #define MAKE_DEFAULT_FONT(m_name, m_size) \
  66. Ref<DynamicFont> m_name; \
  67. m_name.instance(); \
  68. m_name->set_size(m_size); \
  69. m_name->set_font_data(DefaultFont); \
  70. m_name->set_spacing(DynamicFont::SPACING_TOP, -EDSCALE); \
  71. m_name->set_spacing(DynamicFont::SPACING_BOTTOM, -EDSCALE); \
  72. MAKE_FALLBACKS(m_name);
  73. void editor_register_fonts(Ref<Theme> p_theme) {
  74. /* Droid Sans */
  75. Ref<DynamicFontData> DefaultFont;
  76. DefaultFont.instance();
  77. DefaultFont->set_font_ptr(_font_NotoSansUI_Regular, _font_NotoSansUI_Regular_size);
  78. DefaultFont->set_force_autohinter(true); //just looks better..i think?
  79. Ref<DynamicFontData> FontFallback;
  80. FontFallback.instance();
  81. FontFallback->set_font_ptr(_font_DroidSansFallback, _font_DroidSansFallback_size);
  82. FontFallback->set_force_autohinter(true); //just looks better..i think?
  83. Ref<DynamicFontData> FontJapanese;
  84. FontJapanese.instance();
  85. FontJapanese->set_font_ptr(_font_DroidSansJapanese, _font_DroidSansJapanese_size);
  86. FontJapanese->set_force_autohinter(true); //just looks better..i think?
  87. Ref<DynamicFontData> FontArabic;
  88. FontArabic.instance();
  89. FontArabic->set_font_ptr(_font_NotoNaskhArabicUI_Regular, _font_NotoNaskhArabicUI_Regular_size);
  90. FontArabic->set_force_autohinter(true); //just looks better..i think?
  91. Ref<DynamicFontData> FontHebrew;
  92. FontHebrew.instance();
  93. FontHebrew->set_font_ptr(_font_NotoSansHebrew_Regular, _font_NotoSansHebrew_Regular_size);
  94. FontHebrew->set_force_autohinter(true); //just looks better..i think?
  95. Ref<DynamicFontData> FontThai;
  96. FontThai.instance();
  97. FontThai->set_font_ptr(_font_NotoSansThaiUI_Regular, _font_NotoSansThaiUI_Regular_size);
  98. FontThai->set_force_autohinter(true); //just looks better..i think?
  99. /* Source Code Pro */
  100. Ref<DynamicFontData> dfmono;
  101. dfmono.instance();
  102. dfmono->set_font_ptr(_font_Hack_Regular, _font_Hack_Regular_size);
  103. //dfd->set_force_autohinter(true); //just looks better..i think?
  104. MAKE_DEFAULT_FONT(df, int(EditorSettings::get_singleton()->get("interface/editor/font_size")) * EDSCALE);
  105. p_theme->set_default_theme_font(df);
  106. //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"));
  107. //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"));
  108. //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"));
  109. MAKE_DEFAULT_FONT(df_title, int(EDITOR_DEF("text_editor/help/help_title_font_size", 16)) * EDSCALE);
  110. MAKE_DEFAULT_FONT(df_doc, int(EDITOR_DEF("text_editor/help/help_font_size", 14)) * EDSCALE);
  111. p_theme->set_font("doc", "EditorFonts", df_doc);
  112. p_theme->set_font("doc_title", "EditorFonts", df_title);
  113. MAKE_DEFAULT_FONT(df_rulers, int(EDITOR_DEF("canvas_item_editor/rulers", 8)) * EDSCALE);
  114. p_theme->set_font("rulers", "EditorFonts", df_rulers);
  115. Ref<DynamicFont> df_code;
  116. df_code.instance();
  117. df_code->set_size(int(EditorSettings::get_singleton()->get("interface/editor/source_font_size")) * EDSCALE);
  118. df_code->set_font_data(dfmono);
  119. MAKE_FALLBACKS(df_code);
  120. p_theme->set_font("source", "EditorFonts", df_code);
  121. Ref<DynamicFont> df_doc_code;
  122. df_doc_code.instance();
  123. df_doc_code->set_size(int(EDITOR_DEF("text_editor/help/help_source_font_size", 16)) * EDSCALE);
  124. df_doc_code->set_spacing(DynamicFont::SPACING_TOP, -EDSCALE);
  125. df_doc_code->set_spacing(DynamicFont::SPACING_BOTTOM, -EDSCALE);
  126. df_doc_code->set_font_data(dfmono);
  127. MAKE_FALLBACKS(df_doc_code);
  128. p_theme->set_font("doc_source", "EditorFonts", df_doc_code);
  129. Ref<DynamicFont> df_output_code;
  130. df_output_code.instance();
  131. df_output_code->set_size(int(EDITOR_DEF("run/output/font_size", 13)) * EDSCALE);
  132. df_output_code->set_spacing(DynamicFont::SPACING_TOP, -EDSCALE);
  133. df_output_code->set_spacing(DynamicFont::SPACING_BOTTOM, -EDSCALE);
  134. df_output_code->set_font_data(dfmono);
  135. MAKE_FALLBACKS(df_output_code);
  136. p_theme->set_font("output_source", "EditorFonts", df_output_code);
  137. Ref<DynamicFont> df_text_editor_status_code;
  138. df_output_code.instance();
  139. df_output_code->set_size(14 * EDSCALE);
  140. df_output_code->set_spacing(DynamicFont::SPACING_TOP, -EDSCALE);
  141. df_output_code->set_spacing(DynamicFont::SPACING_BOTTOM, -EDSCALE);
  142. df_output_code->set_font_data(dfmono);
  143. MAKE_FALLBACKS(df_output_code);
  144. p_theme->set_font("status_source", "EditorFonts", df_output_code);
  145. //replace default theme
  146. Ref<Texture> di;
  147. Ref<StyleBox> ds;
  148. fill_default_theme(p_theme, df, df_doc, di, ds, EDSCALE);
  149. }