fontengine.cpp 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  1. /*
  2. Minetest
  3. Copyright (C) 2010-2014 sapier <sapier at gmx dot net>
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU Lesser General Public License as published by
  6. the Free Software Foundation; either version 2.1 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU Lesser General Public License for more details.
  12. You should have received a copy of the GNU Lesser General Public License along
  13. with this program; if not, write to the Free Software Foundation, Inc.,
  14. 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  15. */
  16. #include "fontengine.h"
  17. #include <cmath>
  18. #include "client/renderingengine.h"
  19. #include "config.h"
  20. #include "porting.h"
  21. #include "filesys.h"
  22. #include "gettext.h"
  23. #include "irrlicht_changes/CGUITTFont.h"
  24. #include "util/numeric.h" // rangelim
  25. /** maximum size distance for getting a "similar" font size */
  26. #define MAX_FONT_SIZE_OFFSET 10
  27. /** reference to access font engine, has to be initialized by main */
  28. FontEngine* g_fontengine = NULL;
  29. /** callback to be used on change of font size setting */
  30. static void font_setting_changed(const std::string &name, void *userdata)
  31. {
  32. g_fontengine->readSettings();
  33. }
  34. /******************************************************************************/
  35. FontEngine::FontEngine(gui::IGUIEnvironment* env) :
  36. m_env(env)
  37. {
  38. for (u32 &i : m_default_size) {
  39. i = FONT_SIZE_UNSPECIFIED;
  40. }
  41. assert(g_settings != NULL); // pre-condition
  42. assert(m_env != NULL); // pre-condition
  43. assert(m_env->getSkin() != NULL); // pre-condition
  44. readSettings();
  45. const char *settings[] = {
  46. "font_size", "font_bold", "font_italic", "font_size_divisible_by",
  47. "mono_font_size", "mono_font_size_divisible_by",
  48. "font_shadow", "font_shadow_alpha",
  49. "font_path", "font_path_bold", "font_path_italic", "font_path_bold_italic",
  50. "mono_font_path", "mono_font_path_bold", "mono_font_path_italic",
  51. "mono_font_path_bold_italic",
  52. "fallback_font_path",
  53. "screen_dpi", "gui_scaling",
  54. };
  55. for (auto name : settings)
  56. g_settings->registerChangedCallback(name, font_setting_changed, NULL);
  57. }
  58. /******************************************************************************/
  59. FontEngine::~FontEngine()
  60. {
  61. cleanCache();
  62. }
  63. /******************************************************************************/
  64. void FontEngine::cleanCache()
  65. {
  66. RecursiveMutexAutoLock l(m_font_mutex);
  67. for (auto &font_cache_it : m_font_cache) {
  68. for (auto &font_it : font_cache_it) {
  69. font_it.second->drop();
  70. font_it.second = nullptr;
  71. }
  72. font_cache_it.clear();
  73. }
  74. }
  75. /******************************************************************************/
  76. irr::gui::IGUIFont *FontEngine::getFont(FontSpec spec)
  77. {
  78. return getFont(spec, false);
  79. }
  80. irr::gui::IGUIFont *FontEngine::getFont(FontSpec spec, bool may_fail)
  81. {
  82. if (spec.mode == FM_Unspecified) {
  83. spec.mode = m_currentMode;
  84. } else if (spec.mode == _FM_Fallback) {
  85. // Fallback font doesn't support these
  86. spec.bold = false;
  87. spec.italic = false;
  88. }
  89. // Fallback to default size
  90. if (spec.size == FONT_SIZE_UNSPECIFIED)
  91. spec.size = m_default_size[spec.mode];
  92. RecursiveMutexAutoLock l(m_font_mutex);
  93. const auto &cache = m_font_cache[spec.getHash()];
  94. auto it = cache.find(spec.size);
  95. if (it != cache.end())
  96. return it->second;
  97. // Font does not yet exist
  98. gui::IGUIFont *font = initFont(spec);
  99. if (!font && !may_fail) {
  100. errorstream << "Minetest cannot continue without a valid font. "
  101. "Please correct the 'font_path' setting or install the font "
  102. "file in the proper location." << std::endl;
  103. abort();
  104. }
  105. m_font_cache[spec.getHash()][spec.size] = font;
  106. return font;
  107. }
  108. /******************************************************************************/
  109. unsigned int FontEngine::getTextHeight(const FontSpec &spec)
  110. {
  111. gui::IGUIFont *font = getFont(spec);
  112. return font->getDimension(L"Some unimportant example String").Height;
  113. }
  114. /******************************************************************************/
  115. unsigned int FontEngine::getTextWidth(const std::wstring &text, const FontSpec &spec)
  116. {
  117. gui::IGUIFont *font = getFont(spec);
  118. return font->getDimension(text.c_str()).Width;
  119. }
  120. /** get line height for a specific font (including empty room between lines) */
  121. unsigned int FontEngine::getLineHeight(const FontSpec &spec)
  122. {
  123. gui::IGUIFont *font = getFont(spec);
  124. return font->getDimension(L"Some unimportant example String").Height
  125. + font->getKerningHeight();
  126. }
  127. /******************************************************************************/
  128. unsigned int FontEngine::getDefaultFontSize()
  129. {
  130. return m_default_size[m_currentMode];
  131. }
  132. unsigned int FontEngine::getFontSize(FontMode mode)
  133. {
  134. if (mode == FM_Unspecified)
  135. return m_default_size[FM_Standard];
  136. return m_default_size[mode];
  137. }
  138. /******************************************************************************/
  139. void FontEngine::readSettings()
  140. {
  141. m_default_size[FM_Standard] = rangelim(g_settings->getU16("font_size"), 5, 72);
  142. m_default_size[_FM_Fallback] = m_default_size[FM_Standard];
  143. m_default_size[FM_Mono] = rangelim(g_settings->getU16("mono_font_size"), 5, 72);
  144. m_default_bold = g_settings->getBool("font_bold");
  145. m_default_italic = g_settings->getBool("font_italic");
  146. cleanCache();
  147. updateFontCache();
  148. updateSkin();
  149. }
  150. /******************************************************************************/
  151. void FontEngine::updateSkin()
  152. {
  153. gui::IGUIFont *font = getFont();
  154. assert(font);
  155. m_env->getSkin()->setFont(font);
  156. }
  157. /******************************************************************************/
  158. void FontEngine::updateFontCache()
  159. {
  160. /* the only font to be initialized is default one,
  161. * all others are re-initialized on demand */
  162. getFont(FONT_SIZE_UNSPECIFIED, FM_Unspecified);
  163. }
  164. /******************************************************************************/
  165. gui::IGUIFont *FontEngine::initFont(const FontSpec &spec)
  166. {
  167. assert(spec.mode != FM_Unspecified);
  168. assert(spec.size != FONT_SIZE_UNSPECIFIED);
  169. std::string setting_prefix = "";
  170. if (spec.mode == FM_Mono)
  171. setting_prefix = "mono_";
  172. std::string setting_suffix = "";
  173. if (spec.bold)
  174. setting_suffix.append("_bold");
  175. if (spec.italic)
  176. setting_suffix.append("_italic");
  177. // Font size in pixels for FreeType
  178. u32 size = rangelim(spec.size * RenderingEngine::getDisplayDensity() *
  179. g_settings->getFloat("gui_scaling"), 1U, 500U);
  180. // Constrain the font size to a certain multiple, if necessary
  181. u16 divisible_by = g_settings->getU16(setting_prefix + "font_size_divisible_by");
  182. if (divisible_by > 1) {
  183. size = std::max<u32>(
  184. std::round((double)size / divisible_by) * divisible_by, divisible_by);
  185. }
  186. sanity_check(size != 0);
  187. u16 font_shadow = 0;
  188. u16 font_shadow_alpha = 0;
  189. g_settings->getU16NoEx(setting_prefix + "font_shadow", font_shadow);
  190. g_settings->getU16NoEx(setting_prefix + "font_shadow_alpha",
  191. font_shadow_alpha);
  192. std::string path_setting;
  193. if (spec.mode == _FM_Fallback)
  194. path_setting = "fallback_font_path";
  195. else
  196. path_setting = setting_prefix + "font_path" + setting_suffix;
  197. std::string fallback_settings[] = {
  198. g_settings->get(path_setting),
  199. Settings::getLayer(SL_DEFAULTS)->get(path_setting)
  200. };
  201. for (const std::string &font_path : fallback_settings) {
  202. gui::CGUITTFont *font = gui::CGUITTFont::createTTFont(m_env,
  203. font_path.c_str(), size, true, true, font_shadow,
  204. font_shadow_alpha);
  205. if (!font) {
  206. errorstream << "FontEngine: Cannot load '" << font_path <<
  207. "'. Trying to fall back to another path." << std::endl;
  208. continue;
  209. }
  210. if (spec.mode != _FM_Fallback) {
  211. FontSpec spec2(spec);
  212. spec2.mode = _FM_Fallback;
  213. font->setFallback(getFont(spec2, true));
  214. }
  215. return font;
  216. }
  217. return nullptr;
  218. }