fontengine.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466
  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 != NULL) {
  272. m_font_cache[mode][basesize] = font;
  273. return;
  274. }
  275. // try fallback font
  276. errorstream << "FontEngine: failed to load: " << font_path << ", trying to fall back "
  277. "to fallback font" << std::endl;
  278. font_path = g_settings->get(font_config_prefix + "fallback_font_path");
  279. font = gui::CGUITTFont::createTTFont(m_env,
  280. font_path.c_str(), size, true, true, font_shadow,
  281. font_shadow_alpha);
  282. if (font != NULL) {
  283. m_font_cache[mode][basesize] = font;
  284. return;
  285. }
  286. // give up
  287. errorstream << "FontEngine: failed to load freetype font: "
  288. << font_path << std::endl;
  289. errorstream << "minetest can not continue without a valid font. Please correct "
  290. "the 'font_path' setting or install the font file in the proper "
  291. "location" << std::endl;
  292. abort();
  293. }
  294. #endif
  295. }
  296. /** initialize a font without freetype */
  297. void FontEngine::initSimpleFont(unsigned int basesize, FontMode mode)
  298. {
  299. assert(mode == FM_Simple || mode == FM_SimpleMono); // pre-condition
  300. std::string font_path;
  301. if (mode == FM_Simple) {
  302. font_path = m_settings->get("font_path");
  303. } else {
  304. font_path = m_settings->get("mono_font_path");
  305. }
  306. std::string basename = font_path;
  307. std::string ending = font_path.substr(font_path.length() -4);
  308. if (ending == ".ttf") {
  309. errorstream << "FontEngine: Not trying to open \"" << font_path
  310. << "\" which seems to be a truetype font." << std::endl;
  311. return;
  312. }
  313. if ((ending == ".xml") || (ending == ".png")) {
  314. basename = font_path.substr(0,font_path.length()-4);
  315. }
  316. if (basesize == FONT_SIZE_UNSPECIFIED)
  317. basesize = DEFAULT_FONT_SIZE;
  318. unsigned int size = floor(
  319. RenderingEngine::getDisplayDensity() *
  320. m_settings->getFloat("gui_scaling") *
  321. basesize);
  322. irr::gui::IGUIFont* font = NULL;
  323. for(unsigned int offset = 0; offset < MAX_FONT_SIZE_OFFSET; offset++) {
  324. // try opening positive offset
  325. std::stringstream fontsize_plus_png;
  326. fontsize_plus_png << basename << "_" << (size + offset) << ".png";
  327. if (fs::PathExists(fontsize_plus_png.str())) {
  328. font = m_env->getFont(fontsize_plus_png.str().c_str());
  329. if (font) {
  330. verbosestream << "FontEngine: found font: " << fontsize_plus_png.str() << std::endl;
  331. break;
  332. }
  333. }
  334. std::stringstream fontsize_plus_xml;
  335. fontsize_plus_xml << basename << "_" << (size + offset) << ".xml";
  336. if (fs::PathExists(fontsize_plus_xml.str())) {
  337. font = m_env->getFont(fontsize_plus_xml.str().c_str());
  338. if (font) {
  339. verbosestream << "FontEngine: found font: " << fontsize_plus_xml.str() << std::endl;
  340. break;
  341. }
  342. }
  343. // try negative offset
  344. std::stringstream fontsize_minus_png;
  345. fontsize_minus_png << basename << "_" << (size - offset) << ".png";
  346. if (fs::PathExists(fontsize_minus_png.str())) {
  347. font = m_env->getFont(fontsize_minus_png.str().c_str());
  348. if (font) {
  349. verbosestream << "FontEngine: found font: " << fontsize_minus_png.str() << std::endl;
  350. break;
  351. }
  352. }
  353. std::stringstream fontsize_minus_xml;
  354. fontsize_minus_xml << basename << "_" << (size - offset) << ".xml";
  355. if (fs::PathExists(fontsize_minus_xml.str())) {
  356. font = m_env->getFont(fontsize_minus_xml.str().c_str());
  357. if (font) {
  358. verbosestream << "FontEngine: found font: " << fontsize_minus_xml.str() << std::endl;
  359. break;
  360. }
  361. }
  362. }
  363. // try name direct
  364. if (font == NULL) {
  365. if (fs::PathExists(font_path)) {
  366. font = m_env->getFont(font_path.c_str());
  367. if (font)
  368. verbosestream << "FontEngine: found font: " << font_path << std::endl;
  369. }
  370. }
  371. if (font != NULL) {
  372. font->grab();
  373. m_font_cache[mode][basesize] = font;
  374. }
  375. }