fontengine.cpp 14 KB

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