fontengine.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405
  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. #if USE_FREETYPE
  24. #include "irrlicht_changes/CGUITTFont.h"
  25. #endif
  26. /** maximum size distance for getting a "similar" font size */
  27. #define MAX_FONT_SIZE_OFFSET 10
  28. /** reference to access font engine, has to be initialized by main */
  29. FontEngine* g_fontengine = NULL;
  30. /** callback to be used on change of font size setting */
  31. static void font_setting_changed(const std::string &name, void *userdata)
  32. {
  33. g_fontengine->readSettings();
  34. }
  35. unsigned int get_font_cache_index(FontMode mode, bool bold = false, bool italic = false)
  36. {
  37. return (mode << 2) | (bold << 1) | italic;
  38. }
  39. /******************************************************************************/
  40. FontEngine::FontEngine(Settings* main_settings, gui::IGUIEnvironment* env) :
  41. m_settings(main_settings),
  42. m_env(env)
  43. {
  44. for (u32 &i : m_default_size) {
  45. i = (FontMode) FONT_SIZE_UNSPECIFIED;
  46. }
  47. assert(m_settings != NULL); // pre-condition
  48. assert(m_env != NULL); // pre-condition
  49. assert(m_env->getSkin() != NULL); // pre-condition
  50. readSettings();
  51. if (m_currentMode == FM_Standard) {
  52. m_settings->registerChangedCallback("font_size", font_setting_changed, NULL);
  53. m_settings->registerChangedCallback("font_bold", font_setting_changed, NULL);
  54. m_settings->registerChangedCallback("font_italic", font_setting_changed, NULL);
  55. m_settings->registerChangedCallback("font_path", font_setting_changed, NULL);
  56. m_settings->registerChangedCallback("font_path_bold", font_setting_changed, NULL);
  57. m_settings->registerChangedCallback("font_path_italic", font_setting_changed, NULL);
  58. m_settings->registerChangedCallback("font_path_bolditalic", font_setting_changed, NULL);
  59. m_settings->registerChangedCallback("font_shadow", font_setting_changed, NULL);
  60. m_settings->registerChangedCallback("font_shadow_alpha", font_setting_changed, NULL);
  61. }
  62. else if (m_currentMode == FM_Fallback) {
  63. m_settings->registerChangedCallback("fallback_font_size", font_setting_changed, NULL);
  64. m_settings->registerChangedCallback("fallback_font_path", font_setting_changed, NULL);
  65. m_settings->registerChangedCallback("fallback_font_shadow", font_setting_changed, NULL);
  66. m_settings->registerChangedCallback("fallback_font_shadow_alpha", font_setting_changed, NULL);
  67. }
  68. m_settings->registerChangedCallback("mono_font_path", font_setting_changed, NULL);
  69. m_settings->registerChangedCallback("mono_font_size", font_setting_changed, NULL);
  70. m_settings->registerChangedCallback("screen_dpi", font_setting_changed, NULL);
  71. m_settings->registerChangedCallback("gui_scaling", font_setting_changed, NULL);
  72. }
  73. /******************************************************************************/
  74. FontEngine::~FontEngine()
  75. {
  76. cleanCache();
  77. }
  78. /******************************************************************************/
  79. void FontEngine::cleanCache()
  80. {
  81. for (auto &font_cache_it : m_font_cache) {
  82. for (auto &font_it : font_cache_it) {
  83. font_it.second->drop();
  84. font_it.second = NULL;
  85. }
  86. font_cache_it.clear();
  87. }
  88. }
  89. /******************************************************************************/
  90. irr::gui::IGUIFont *FontEngine::getFont(unsigned int font_size, FontMode mode,
  91. bool bold, bool italic)
  92. {
  93. if (mode == FM_Unspecified) {
  94. mode = m_currentMode;
  95. } else if (m_currentMode == FM_Simple) {
  96. // Freetype disabled -> Force simple mode
  97. mode = (mode == FM_Mono || mode == FM_SimpleMono) ?
  98. FM_SimpleMono : FM_Simple;
  99. }
  100. // Fallback to default size
  101. if (font_size == FONT_SIZE_UNSPECIFIED)
  102. font_size = m_default_size[mode];
  103. unsigned int cache_index = get_font_cache_index(mode, bold, italic);
  104. const auto &cache = m_font_cache[cache_index];
  105. if (cache.find(font_size) == cache.end()) {
  106. if (mode == FM_Simple || mode == FM_SimpleMono)
  107. initSimpleFont(font_size, mode);
  108. else
  109. initFont(font_size, mode, bold, italic);
  110. }
  111. if (m_font_cache[cache_index].find(font_size) ==
  112. m_font_cache[cache_index].end())
  113. initFont(font_size, mode, bold, italic);
  114. const auto &font = cache.find(font_size);
  115. return font != cache.end() ? font->second : nullptr;
  116. }
  117. /******************************************************************************/
  118. unsigned int FontEngine::getTextHeight(unsigned int font_size, FontMode mode,
  119. bool bold, bool italic)
  120. {
  121. irr::gui::IGUIFont *font = getFont(font_size, mode, bold, italic);
  122. // use current skin font as fallback
  123. if (font == NULL) {
  124. font = m_env->getSkin()->getFont();
  125. }
  126. FATAL_ERROR_IF(font == NULL, "Could not get skin font");
  127. return font->getDimension(L"Some unimportant example String").Height;
  128. }
  129. /******************************************************************************/
  130. unsigned int FontEngine::getTextWidth(const std::wstring& text,
  131. unsigned int font_size, FontMode mode, bool bold, bool italic)
  132. {
  133. irr::gui::IGUIFont *font = getFont(font_size, mode, bold, italic);
  134. // use current skin font as fallback
  135. if (font == NULL) {
  136. font = m_env->getSkin()->getFont();
  137. }
  138. FATAL_ERROR_IF(font == NULL, "Could not get font");
  139. return font->getDimension(text.c_str()).Width;
  140. }
  141. /** get line height for a specific font (including empty room between lines) */
  142. unsigned int FontEngine::getLineHeight(unsigned int font_size, FontMode mode,
  143. bool bold, bool italic)
  144. {
  145. irr::gui::IGUIFont *font = getFont(font_size, mode, bold, italic);
  146. // use current skin font as fallback
  147. if (font == NULL) {
  148. font = m_env->getSkin()->getFont();
  149. }
  150. FATAL_ERROR_IF(font == NULL, "Could not get font");
  151. return font->getDimension(L"Some unimportant example String").Height
  152. + font->getKerningHeight();
  153. }
  154. /******************************************************************************/
  155. unsigned int FontEngine::getDefaultFontSize()
  156. {
  157. return m_default_size[m_currentMode];
  158. }
  159. /******************************************************************************/
  160. void FontEngine::readSettings()
  161. {
  162. if (USE_FREETYPE && g_settings->getBool("freetype")) {
  163. m_default_size[FM_Standard] = m_settings->getU16("font_size");
  164. m_default_size[FM_Fallback] = m_settings->getU16("fallback_font_size");
  165. m_default_size[FM_Mono] = m_settings->getU16("mono_font_size");
  166. m_currentMode = is_yes(gettext("needs_fallback_font")) ?
  167. FM_Fallback : FM_Standard;
  168. m_default_bold = m_settings->getBool("font_bold");
  169. m_default_italic = m_settings->getBool("font_italic");
  170. } else {
  171. m_currentMode = FM_Simple;
  172. }
  173. m_default_size[FM_Simple] = m_settings->getU16("font_size");
  174. m_default_size[FM_SimpleMono] = m_settings->getU16("mono_font_size");
  175. cleanCache();
  176. updateFontCache();
  177. updateSkin();
  178. }
  179. /******************************************************************************/
  180. void FontEngine::updateSkin()
  181. {
  182. gui::IGUIFont *font = getFont();
  183. if (font)
  184. m_env->getSkin()->setFont(font);
  185. else
  186. errorstream << "FontEngine: Default font file: " <<
  187. "\n\t\"" << m_settings->get("font_path") << "\"" <<
  188. "\n\trequired for current screen configuration was not found" <<
  189. " or was invalid file format." <<
  190. "\n\tUsing irrlicht default font." << std::endl;
  191. // If we did fail to create a font our own make irrlicht find a default one
  192. font = m_env->getSkin()->getFont();
  193. FATAL_ERROR_IF(font == NULL, "Could not create/get font");
  194. u32 text_height = font->getDimension(L"Hello, world!").Height;
  195. infostream << "text_height=" << text_height << std::endl;
  196. }
  197. /******************************************************************************/
  198. void FontEngine::updateFontCache()
  199. {
  200. /* the only font to be initialized is default one,
  201. * all others are re-initialized on demand */
  202. getFont(FONT_SIZE_UNSPECIFIED, FM_Unspecified);
  203. }
  204. /******************************************************************************/
  205. void FontEngine::initFont(unsigned int basesize, FontMode mode,
  206. bool bold, bool italic)
  207. {
  208. assert(mode != FM_Unspecified);
  209. assert(basesize != FONT_SIZE_UNSPECIFIED);
  210. int cache_index = get_font_cache_index(mode, bold, italic);
  211. if (m_font_cache[cache_index].find(basesize) !=
  212. m_font_cache[cache_index].end())
  213. return;
  214. std::string setting_prefix = "";
  215. switch (mode) {
  216. case FM_Fallback:
  217. setting_prefix = "fallback_";
  218. break;
  219. case FM_Mono:
  220. case FM_SimpleMono:
  221. setting_prefix = "mono_";
  222. break;
  223. default:
  224. break;
  225. }
  226. std::string setting_suffix = (bold) ?
  227. ((italic) ? "_bold_italic" : "_bold") :
  228. ((italic) ? "_italic" : "");
  229. u32 size = std::floor(RenderingEngine::getDisplayDensity() *
  230. m_settings->getFloat("gui_scaling") * basesize);
  231. if (size == 0) {
  232. errorstream << "FontEngine: attempt to use font size 0" << std::endl;
  233. errorstream << " display density: " << RenderingEngine::getDisplayDensity() << std::endl;
  234. abort();
  235. }
  236. u16 font_shadow = 0;
  237. u16 font_shadow_alpha = 0;
  238. g_settings->getU16NoEx(setting_prefix + "font_shadow", font_shadow);
  239. g_settings->getU16NoEx(setting_prefix + "font_shadow_alpha",
  240. font_shadow_alpha);
  241. std::string wanted_font_path;
  242. wanted_font_path = g_settings->get(setting_prefix + "font_path" + setting_suffix);
  243. std::string fallback_settings[] = {
  244. wanted_font_path,
  245. m_settings->get("fallback_font_path"),
  246. m_settings->getDefault(setting_prefix + "font_path")
  247. };
  248. #if USE_FREETYPE
  249. for (const std::string &font_path : fallback_settings) {
  250. irr::gui::IGUIFont *font = gui::CGUITTFont::createTTFont(m_env,
  251. font_path.c_str(), size, true, true, font_shadow,
  252. font_shadow_alpha);
  253. if (font) {
  254. m_font_cache[cache_index][basesize] = font;
  255. return;
  256. }
  257. errorstream << "FontEngine: Cannot load '" << font_path <<
  258. "'. Trying to fall back to another path." << std::endl;
  259. }
  260. // give up
  261. errorstream << "minetest can not continue without a valid font. "
  262. "Please correct the 'font_path' setting or install the font "
  263. "file in the proper location" << std::endl;
  264. #else
  265. errorstream << "FontEngine: Tried to load freetype fonts but Minetest was"
  266. " not compiled with that library." << std::endl;
  267. #endif
  268. abort();
  269. }
  270. /** initialize a font without freetype */
  271. void FontEngine::initSimpleFont(unsigned int basesize, FontMode mode)
  272. {
  273. assert(mode == FM_Simple || mode == FM_SimpleMono);
  274. const std::string &font_path = m_settings->get(
  275. (mode == FM_SimpleMono) ? "mono_font_path" : "font_path");
  276. size_t pos_dot = font_path.find_last_of('.');
  277. std::string basename = font_path;
  278. std::string ending = lowercase(font_path.substr(pos_dot));
  279. if (ending == ".ttf") {
  280. errorstream << "FontEngine: Found font \"" << font_path
  281. << "\" but freetype is not available." << std::endl;
  282. return;
  283. }
  284. if (ending == ".xml" || ending == ".png")
  285. basename = font_path.substr(0, pos_dot);
  286. if (basesize == FONT_SIZE_UNSPECIFIED)
  287. basesize = DEFAULT_FONT_SIZE;
  288. u32 size = std::floor(
  289. RenderingEngine::getDisplayDensity() *
  290. m_settings->getFloat("gui_scaling") *
  291. basesize);
  292. irr::gui::IGUIFont *font = nullptr;
  293. std::string font_extensions[] = { ".png", ".xml" };
  294. // Find nearest matching font scale
  295. // Does a "zig-zag motion" (positibe/negative), from 0 to MAX_FONT_SIZE_OFFSET
  296. for (s32 zoffset = 0; zoffset < MAX_FONT_SIZE_OFFSET * 2; zoffset++) {
  297. std::stringstream path;
  298. // LSB to sign
  299. s32 sign = (zoffset & 1) ? -1 : 1;
  300. s32 offset = zoffset >> 1;
  301. for (const std::string &ext : font_extensions) {
  302. path.str(""); // Clear
  303. path << basename << "_" << (size + offset * sign) << ext;
  304. if (!fs::PathExists(path.str()))
  305. continue;
  306. font = m_env->getFont(path.str().c_str());
  307. if (font) {
  308. verbosestream << "FontEngine: found font: " << path.str() << std::endl;
  309. break;
  310. }
  311. }
  312. if (font)
  313. break;
  314. }
  315. // try name direct
  316. if (font == NULL) {
  317. if (fs::PathExists(font_path)) {
  318. font = m_env->getFont(font_path.c_str());
  319. if (font)
  320. verbosestream << "FontEngine: found font: " << font_path << std::endl;
  321. }
  322. }
  323. if (font)
  324. m_font_cache[get_font_cache_index(mode)][basesize] = font;
  325. }