fontengine.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505
  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. #if USE_FREETYPE
  23. #include "gettext.h"
  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. m_currentMode = FM_Simple;
  47. #if USE_FREETYPE
  48. if (g_settings->getBool("freetype")) {
  49. m_default_size[FM_Standard] = m_settings->getU16("font_size");
  50. m_default_size[FM_Fallback] = m_settings->getU16("fallback_font_size");
  51. m_default_size[FM_Mono] = m_settings->getU16("mono_font_size");
  52. if (is_yes(gettext("needs_fallback_font"))) {
  53. m_currentMode = FM_Fallback;
  54. }
  55. else {
  56. m_currentMode = FM_Standard;
  57. }
  58. }
  59. // having freetype but not using it is quite a strange case so we need to do
  60. // special handling for it
  61. if (m_currentMode == FM_Simple) {
  62. std::stringstream fontsize;
  63. fontsize << DEFAULT_FONT_SIZE;
  64. m_settings->setDefault("font_size", fontsize.str());
  65. m_settings->setDefault("mono_font_size", fontsize.str());
  66. }
  67. #endif
  68. m_default_size[FM_Simple] = m_settings->getU16("font_size");
  69. m_default_size[FM_SimpleMono] = m_settings->getU16("mono_font_size");
  70. updateSkin();
  71. if (m_currentMode == FM_Standard) {
  72. m_settings->registerChangedCallback("font_size", font_setting_changed, NULL);
  73. m_settings->registerChangedCallback("font_path", font_setting_changed, NULL);
  74. m_settings->registerChangedCallback("font_shadow", font_setting_changed, NULL);
  75. m_settings->registerChangedCallback("font_shadow_alpha", font_setting_changed, NULL);
  76. }
  77. else if (m_currentMode == FM_Fallback) {
  78. m_settings->registerChangedCallback("fallback_font_size", font_setting_changed, NULL);
  79. m_settings->registerChangedCallback("fallback_font_path", font_setting_changed, NULL);
  80. m_settings->registerChangedCallback("fallback_font_shadow", font_setting_changed, NULL);
  81. m_settings->registerChangedCallback("fallback_font_shadow_alpha", font_setting_changed, NULL);
  82. }
  83. m_settings->registerChangedCallback("mono_font_path", font_setting_changed, NULL);
  84. m_settings->registerChangedCallback("mono_font_size", font_setting_changed, NULL);
  85. m_settings->registerChangedCallback("screen_dpi", font_setting_changed, NULL);
  86. m_settings->registerChangedCallback("gui_scaling", font_setting_changed, NULL);
  87. }
  88. /******************************************************************************/
  89. FontEngine::~FontEngine()
  90. {
  91. cleanCache();
  92. }
  93. /******************************************************************************/
  94. void FontEngine::cleanCache()
  95. {
  96. for (auto &font_cache_it : m_font_cache) {
  97. for (auto &font_it : font_cache_it) {
  98. font_it.second->drop();
  99. font_it.second = NULL;
  100. }
  101. font_cache_it.clear();
  102. }
  103. }
  104. /******************************************************************************/
  105. irr::gui::IGUIFont* FontEngine::getFont(unsigned int font_size, FontMode mode)
  106. {
  107. if (mode == FM_Unspecified) {
  108. mode = m_currentMode;
  109. }
  110. else if ((mode == FM_Mono) && (m_currentMode == FM_Simple)) {
  111. mode = FM_SimpleMono;
  112. }
  113. if (font_size == FONT_SIZE_UNSPECIFIED) {
  114. font_size = m_default_size[mode];
  115. }
  116. if ((font_size == m_lastSize) && (mode == m_lastMode)) {
  117. return m_lastFont;
  118. }
  119. if (m_font_cache[mode].find(font_size) == m_font_cache[mode].end()) {
  120. initFont(font_size, mode);
  121. }
  122. if (m_font_cache[mode].find(font_size) == m_font_cache[mode].end()) {
  123. return NULL;
  124. }
  125. m_lastSize = font_size;
  126. m_lastMode = mode;
  127. m_lastFont = m_font_cache[mode][font_size];
  128. return m_font_cache[mode][font_size];
  129. }
  130. /******************************************************************************/
  131. unsigned int FontEngine::getTextHeight(unsigned int font_size, FontMode mode)
  132. {
  133. irr::gui::IGUIFont* font = getFont(font_size, mode);
  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 skin font");
  139. return font->getDimension(L"Some unimportant example String").Height;
  140. }
  141. /******************************************************************************/
  142. unsigned int FontEngine::getTextWidth(const std::wstring& text,
  143. unsigned int font_size, FontMode mode)
  144. {
  145. irr::gui::IGUIFont* font = getFont(font_size, mode);
  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(text.c_str()).Width;
  152. }
  153. /** get line height for a specific font (including empty room between lines) */
  154. unsigned int FontEngine::getLineHeight(unsigned int font_size, FontMode mode)
  155. {
  156. irr::gui::IGUIFont* font = getFont(font_size, mode);
  157. // use current skin font as fallback
  158. if (font == NULL) {
  159. font = m_env->getSkin()->getFont();
  160. }
  161. FATAL_ERROR_IF(font == NULL, "Could not get font");
  162. return font->getDimension(L"Some unimportant example String").Height
  163. + font->getKerningHeight();
  164. }
  165. /******************************************************************************/
  166. unsigned int FontEngine::getDefaultFontSize()
  167. {
  168. return m_default_size[m_currentMode];
  169. }
  170. /******************************************************************************/
  171. void FontEngine::readSettings()
  172. {
  173. #if USE_FREETYPE
  174. if (g_settings->getBool("freetype")) {
  175. m_default_size[FM_Standard] = m_settings->getU16("font_size");
  176. m_default_size[FM_Fallback] = m_settings->getU16("fallback_font_size");
  177. m_default_size[FM_Mono] = m_settings->getU16("mono_font_size");
  178. if (is_yes(gettext("needs_fallback_font"))) {
  179. m_currentMode = FM_Fallback;
  180. }
  181. else {
  182. m_currentMode = FM_Standard;
  183. }
  184. }
  185. #endif
  186. m_default_size[FM_Simple] = m_settings->getU16("font_size");
  187. m_default_size[FM_SimpleMono] = m_settings->getU16("mono_font_size");
  188. cleanCache();
  189. updateFontCache();
  190. updateSkin();
  191. }
  192. /******************************************************************************/
  193. void FontEngine::updateSkin()
  194. {
  195. gui::IGUIFont *font = getFont();
  196. if (font)
  197. m_env->getSkin()->setFont(font);
  198. else
  199. errorstream << "FontEngine: Default font file: " <<
  200. "\n\t\"" << m_settings->get("font_path") << "\"" <<
  201. "\n\trequired for current screen configuration was not found" <<
  202. " or was invalid file format." <<
  203. "\n\tUsing irrlicht default font." << std::endl;
  204. // If we did fail to create a font our own make irrlicht find a default one
  205. font = m_env->getSkin()->getFont();
  206. FATAL_ERROR_IF(font == NULL, "Could not create/get font");
  207. u32 text_height = font->getDimension(L"Hello, world!").Height;
  208. infostream << "text_height=" << text_height << std::endl;
  209. }
  210. /******************************************************************************/
  211. void FontEngine::updateFontCache()
  212. {
  213. /* the only font to be initialized is default one,
  214. * all others are re-initialized on demand */
  215. initFont(m_default_size[m_currentMode], m_currentMode);
  216. /* reset font quick access */
  217. m_lastMode = FM_Unspecified;
  218. m_lastSize = 0;
  219. m_lastFont = NULL;
  220. }
  221. /******************************************************************************/
  222. void FontEngine::initFont(unsigned int basesize, FontMode mode)
  223. {
  224. std::string font_config_prefix;
  225. if (mode == FM_Unspecified) {
  226. mode = m_currentMode;
  227. }
  228. switch (mode) {
  229. case FM_Standard:
  230. font_config_prefix = "";
  231. break;
  232. case FM_Fallback:
  233. font_config_prefix = "fallback_";
  234. break;
  235. case FM_Mono:
  236. font_config_prefix = "mono_";
  237. if (m_currentMode == FM_Simple)
  238. mode = FM_SimpleMono;
  239. break;
  240. case FM_Simple: /* Fallthrough */
  241. case FM_SimpleMono: /* Fallthrough */
  242. default:
  243. font_config_prefix = "";
  244. }
  245. if (m_font_cache[mode].find(basesize) != m_font_cache[mode].end())
  246. return;
  247. if ((mode == FM_Simple) || (mode == FM_SimpleMono)) {
  248. initSimpleFont(basesize, mode);
  249. return;
  250. }
  251. #if USE_FREETYPE
  252. else {
  253. if (!is_yes(m_settings->get("freetype"))) {
  254. return;
  255. }
  256. u32 size = std::floor(RenderingEngine::getDisplayDensity() *
  257. m_settings->getFloat("gui_scaling") * basesize);
  258. u32 font_shadow = 0;
  259. u32 font_shadow_alpha = 0;
  260. try {
  261. font_shadow =
  262. g_settings->getU16(font_config_prefix + "font_shadow");
  263. } catch (SettingNotFoundException&) {}
  264. try {
  265. font_shadow_alpha =
  266. g_settings->getU16(font_config_prefix + "font_shadow_alpha");
  267. } catch (SettingNotFoundException&) {}
  268. std::string font_path = g_settings->get(font_config_prefix + "font_path");
  269. irr::gui::IGUIFont* font = gui::CGUITTFont::createTTFont(m_env,
  270. font_path.c_str(), size, true, true, font_shadow,
  271. font_shadow_alpha);
  272. if (font) {
  273. m_font_cache[mode][basesize] = font;
  274. return;
  275. }
  276. if (font_config_prefix == "mono_") {
  277. const std::string &mono_font_path = m_settings->getDefault("mono_font_path");
  278. if (font_path != mono_font_path) {
  279. // try original mono font
  280. errorstream << "FontEngine: failed to load custom mono "
  281. "font: " << font_path << ", trying to fall back to "
  282. "original mono font" << std::endl;
  283. font = gui::CGUITTFont::createTTFont(m_env,
  284. mono_font_path.c_str(), size, true, true,
  285. font_shadow, font_shadow_alpha);
  286. if (font) {
  287. m_font_cache[mode][basesize] = font;
  288. return;
  289. }
  290. }
  291. } else {
  292. // try fallback font
  293. errorstream << "FontEngine: failed to load: " << font_path <<
  294. ", trying to fall back to fallback font" << std::endl;
  295. font_path = g_settings->get(font_config_prefix + "fallback_font_path");
  296. font = gui::CGUITTFont::createTTFont(m_env,
  297. font_path.c_str(), size, true, true, font_shadow,
  298. font_shadow_alpha);
  299. if (font) {
  300. m_font_cache[mode][basesize] = font;
  301. return;
  302. }
  303. const std::string &fallback_font_path = m_settings->getDefault("fallback_font_path");
  304. if (font_path != fallback_font_path) {
  305. // try original fallback font
  306. errorstream << "FontEngine: failed to load custom fallback "
  307. "font: " << font_path << ", trying to fall back to "
  308. "original fallback font" << std::endl;
  309. font = gui::CGUITTFont::createTTFont(m_env,
  310. fallback_font_path.c_str(), size, true, true,
  311. font_shadow, font_shadow_alpha);
  312. if (font) {
  313. m_font_cache[mode][basesize] = font;
  314. return;
  315. }
  316. }
  317. }
  318. // give up
  319. errorstream << "FontEngine: failed to load freetype font: "
  320. << font_path << std::endl;
  321. errorstream << "minetest can not continue without a valid font. "
  322. "Please correct the 'font_path' setting or install the font "
  323. "file in the proper location" << std::endl;
  324. abort();
  325. }
  326. #endif
  327. }
  328. /** initialize a font without freetype */
  329. void FontEngine::initSimpleFont(unsigned int basesize, FontMode mode)
  330. {
  331. assert(mode == FM_Simple || mode == FM_SimpleMono); // pre-condition
  332. std::string font_path;
  333. if (mode == FM_Simple) {
  334. font_path = m_settings->get("font_path");
  335. } else {
  336. font_path = m_settings->get("mono_font_path");
  337. }
  338. std::string basename = font_path;
  339. std::string ending = font_path.substr(font_path.length() -4);
  340. if (ending == ".ttf") {
  341. errorstream << "FontEngine: Not trying to open \"" << font_path
  342. << "\" which seems to be a truetype font." << std::endl;
  343. return;
  344. }
  345. if ((ending == ".xml") || (ending == ".png")) {
  346. basename = font_path.substr(0,font_path.length()-4);
  347. }
  348. if (basesize == FONT_SIZE_UNSPECIFIED)
  349. basesize = DEFAULT_FONT_SIZE;
  350. u32 size = std::floor(
  351. RenderingEngine::getDisplayDensity() *
  352. m_settings->getFloat("gui_scaling") *
  353. basesize);
  354. irr::gui::IGUIFont* font = NULL;
  355. for(unsigned int offset = 0; offset < MAX_FONT_SIZE_OFFSET; offset++) {
  356. // try opening positive offset
  357. std::stringstream fontsize_plus_png;
  358. fontsize_plus_png << basename << "_" << (size + offset) << ".png";
  359. if (fs::PathExists(fontsize_plus_png.str())) {
  360. font = m_env->getFont(fontsize_plus_png.str().c_str());
  361. if (font) {
  362. verbosestream << "FontEngine: found font: " << fontsize_plus_png.str() << std::endl;
  363. break;
  364. }
  365. }
  366. std::stringstream fontsize_plus_xml;
  367. fontsize_plus_xml << basename << "_" << (size + offset) << ".xml";
  368. if (fs::PathExists(fontsize_plus_xml.str())) {
  369. font = m_env->getFont(fontsize_plus_xml.str().c_str());
  370. if (font) {
  371. verbosestream << "FontEngine: found font: " << fontsize_plus_xml.str() << std::endl;
  372. break;
  373. }
  374. }
  375. // try negative offset
  376. std::stringstream fontsize_minus_png;
  377. fontsize_minus_png << basename << "_" << (size - offset) << ".png";
  378. if (fs::PathExists(fontsize_minus_png.str())) {
  379. font = m_env->getFont(fontsize_minus_png.str().c_str());
  380. if (font) {
  381. verbosestream << "FontEngine: found font: " << fontsize_minus_png.str() << std::endl;
  382. break;
  383. }
  384. }
  385. std::stringstream fontsize_minus_xml;
  386. fontsize_minus_xml << basename << "_" << (size - offset) << ".xml";
  387. if (fs::PathExists(fontsize_minus_xml.str())) {
  388. font = m_env->getFont(fontsize_minus_xml.str().c_str());
  389. if (font) {
  390. verbosestream << "FontEngine: found font: " << fontsize_minus_xml.str() << std::endl;
  391. break;
  392. }
  393. }
  394. }
  395. // try name direct
  396. if (font == NULL) {
  397. if (fs::PathExists(font_path)) {
  398. font = m_env->getFont(font_path.c_str());
  399. if (font)
  400. verbosestream << "FontEngine: found font: " << font_path << std::endl;
  401. }
  402. }
  403. if (font) {
  404. font->grab();
  405. m_font_cache[mode][basesize] = font;
  406. }
  407. }