fontengine.cpp 12 KB

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