3 Copyright (C) 2010-2014 sapier <sapier at gmx dot net>
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU Lesser General Public License as published by
7 the Free Software Foundation; either version 2.1 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public License along
16 with this program; if not, write to the Free Software Foundation, Inc.,
17 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20 #include "fontengine.h"
22 #include "client/renderingengine.h"
29 #include "irrlicht_changes/CGUITTFont.h"
32 /** maximum size distance for getting a "similar" font size */
33 #define MAX_FONT_SIZE_OFFSET 10
35 /** reference to access font engine, has to be initialized by main */
36 FontEngine* g_fontengine = NULL;
38 /** callback to be used on change of font size setting */
39 static void font_setting_changed(const std::string &name, void *userdata)
41 g_fontengine->readSettings();
44 /******************************************************************************/
45 FontEngine::FontEngine(Settings* main_settings, gui::IGUIEnvironment* env) :
46 m_settings(main_settings),
50 for (u32 &i : m_default_size) {
51 i = (FontMode) FONT_SIZE_UNSPECIFIED;
54 assert(m_settings != NULL); // pre-condition
55 assert(m_env != NULL); // pre-condition
56 assert(m_env->getSkin() != NULL); // pre-condition
58 m_currentMode = FM_Simple;
61 if (g_settings->getBool("freetype")) {
62 m_default_size[FM_Standard] = m_settings->getU16("font_size");
63 m_default_size[FM_Fallback] = m_settings->getU16("fallback_font_size");
64 m_default_size[FM_Mono] = m_settings->getU16("mono_font_size");
66 if (is_yes(gettext("needs_fallback_font"))) {
67 m_currentMode = FM_Fallback;
70 m_currentMode = FM_Standard;
74 // having freetype but not using it is quite a strange case so we need to do
75 // special handling for it
76 if (m_currentMode == FM_Simple) {
77 std::stringstream fontsize;
78 fontsize << DEFAULT_FONT_SIZE;
79 m_settings->setDefault("font_size", fontsize.str());
80 m_settings->setDefault("mono_font_size", fontsize.str());
84 m_default_size[FM_Simple] = m_settings->getU16("font_size");
85 m_default_size[FM_SimpleMono] = m_settings->getU16("mono_font_size");
89 if (m_currentMode == FM_Standard) {
90 m_settings->registerChangedCallback("font_size", font_setting_changed, NULL);
91 m_settings->registerChangedCallback("font_path", font_setting_changed, NULL);
92 m_settings->registerChangedCallback("font_shadow", font_setting_changed, NULL);
93 m_settings->registerChangedCallback("font_shadow_alpha", font_setting_changed, NULL);
95 else if (m_currentMode == FM_Fallback) {
96 m_settings->registerChangedCallback("fallback_font_size", font_setting_changed, NULL);
97 m_settings->registerChangedCallback("fallback_font_path", font_setting_changed, NULL);
98 m_settings->registerChangedCallback("fallback_font_shadow", font_setting_changed, NULL);
99 m_settings->registerChangedCallback("fallback_font_shadow_alpha", font_setting_changed, NULL);
102 m_settings->registerChangedCallback("mono_font_path", font_setting_changed, NULL);
103 m_settings->registerChangedCallback("mono_font_size", font_setting_changed, NULL);
104 m_settings->registerChangedCallback("screen_dpi", font_setting_changed, NULL);
105 m_settings->registerChangedCallback("gui_scaling", font_setting_changed, NULL);
108 /******************************************************************************/
109 FontEngine::~FontEngine()
114 /******************************************************************************/
115 void FontEngine::cleanCache()
117 for (auto &font_cache_it : m_font_cache) {
119 for (auto &font_it : font_cache_it) {
120 font_it.second->drop();
121 font_it.second = NULL;
123 font_cache_it.clear();
127 /******************************************************************************/
128 irr::gui::IGUIFont* FontEngine::getFont(unsigned int font_size, FontMode mode)
130 if (mode == FM_Unspecified) {
131 mode = m_currentMode;
133 else if ((mode == FM_Mono) && (m_currentMode == FM_Simple)) {
134 mode = FM_SimpleMono;
137 if (font_size == FONT_SIZE_UNSPECIFIED) {
138 font_size = m_default_size[mode];
141 if ((font_size == m_lastSize) && (mode == m_lastMode)) {
145 if (m_font_cache[mode].find(font_size) == m_font_cache[mode].end()) {
146 initFont(font_size, mode);
149 if (m_font_cache[mode].find(font_size) == m_font_cache[mode].end()) {
153 m_lastSize = font_size;
155 m_lastFont = m_font_cache[mode][font_size];
157 return m_font_cache[mode][font_size];
160 /******************************************************************************/
161 unsigned int FontEngine::getTextHeight(unsigned int font_size, FontMode mode)
163 irr::gui::IGUIFont* font = getFont(font_size, mode);
165 // use current skin font as fallback
167 font = m_env->getSkin()->getFont();
169 FATAL_ERROR_IF(font == NULL, "Could not get skin font");
171 return font->getDimension(L"Some unimportant example String").Height;
174 /******************************************************************************/
175 unsigned int FontEngine::getTextWidth(const std::wstring& text,
176 unsigned int font_size, FontMode mode)
178 irr::gui::IGUIFont* font = getFont(font_size, mode);
180 // use current skin font as fallback
182 font = m_env->getSkin()->getFont();
184 FATAL_ERROR_IF(font == NULL, "Could not get font");
186 return font->getDimension(text.c_str()).Width;
190 /** get line height for a specific font (including empty room between lines) */
191 unsigned int FontEngine::getLineHeight(unsigned int font_size, FontMode mode)
193 irr::gui::IGUIFont* font = getFont(font_size, mode);
195 // use current skin font as fallback
197 font = m_env->getSkin()->getFont();
199 FATAL_ERROR_IF(font == NULL, "Could not get font");
201 return font->getDimension(L"Some unimportant example String").Height
202 + font->getKerningHeight();
205 /******************************************************************************/
206 unsigned int FontEngine::getDefaultFontSize()
208 return m_default_size[m_currentMode];
211 /******************************************************************************/
212 void FontEngine::readSettings()
215 if (g_settings->getBool("freetype")) {
216 m_default_size[FM_Standard] = m_settings->getU16("font_size");
217 m_default_size[FM_Fallback] = m_settings->getU16("fallback_font_size");
218 m_default_size[FM_Mono] = m_settings->getU16("mono_font_size");
220 if (is_yes(gettext("needs_fallback_font"))) {
221 m_currentMode = FM_Fallback;
224 m_currentMode = FM_Standard;
228 m_default_size[FM_Simple] = m_settings->getU16("font_size");
229 m_default_size[FM_SimpleMono] = m_settings->getU16("mono_font_size");
236 /******************************************************************************/
237 void FontEngine::updateSkin()
239 gui::IGUIFont *font = getFont();
242 m_env->getSkin()->setFont(font);
244 errorstream << "FontEngine: Default font file: " <<
245 "\n\t\"" << m_settings->get("font_path") << "\"" <<
246 "\n\trequired for current screen configuration was not found" <<
247 " or was invalid file format." <<
248 "\n\tUsing irrlicht default font." << std::endl;
250 // If we did fail to create a font our own make irrlicht find a default one
251 font = m_env->getSkin()->getFont();
252 FATAL_ERROR_IF(font == NULL, "Could not create/get font");
254 u32 text_height = font->getDimension(L"Hello, world!").Height;
255 infostream << "text_height=" << text_height << std::endl;
258 /******************************************************************************/
259 void FontEngine::updateFontCache()
261 /* the only font to be initialized is default one,
262 * all others are re-initialized on demand */
263 initFont(m_default_size[m_currentMode], m_currentMode);
265 /* reset font quick access */
266 m_lastMode = FM_Unspecified;
271 /******************************************************************************/
272 void FontEngine::initFont(unsigned int basesize, FontMode mode)
275 std::string font_config_prefix;
277 if (mode == FM_Unspecified) {
278 mode = m_currentMode;
284 font_config_prefix = "";
288 font_config_prefix = "fallback_";
292 font_config_prefix = "mono_";
293 if (m_currentMode == FM_Simple)
294 mode = FM_SimpleMono;
297 case FM_Simple: /* Fallthrough */
298 case FM_SimpleMono: /* Fallthrough */
300 font_config_prefix = "";
304 if (m_font_cache[mode].find(basesize) != m_font_cache[mode].end())
307 if ((mode == FM_Simple) || (mode == FM_SimpleMono)) {
308 initSimpleFont(basesize, mode);
313 if (!is_yes(m_settings->get("freetype"))) {
316 u32 size = std::floor(RenderingEngine::getDisplayDensity() *
317 m_settings->getFloat("gui_scaling") * basesize);
319 u32 font_shadow_alpha = 0;
323 g_settings->getU16(font_config_prefix + "font_shadow");
324 } catch (SettingNotFoundException&) {}
327 g_settings->getU16(font_config_prefix + "font_shadow_alpha");
328 } catch (SettingNotFoundException&) {}
330 std::string font_path = g_settings->get(font_config_prefix + "font_path");
332 irr::gui::IGUIFont* font = gui::CGUITTFont::createTTFont(m_env,
333 font_path.c_str(), size, true, true, font_shadow,
337 m_font_cache[mode][basesize] = font;
341 if (font_config_prefix == "mono_") {
342 const std::string &mono_font_path = m_settings->getDefault("mono_font_path");
344 if (font_path != mono_font_path) {
345 // try original mono font
346 errorstream << "FontEngine: failed to load custom mono "
347 "font: " << font_path << ", trying to fall back to "
348 "original mono font" << std::endl;
350 font = gui::CGUITTFont::createTTFont(m_env,
351 mono_font_path.c_str(), size, true, true,
352 font_shadow, font_shadow_alpha);
355 m_font_cache[mode][basesize] = font;
361 errorstream << "FontEngine: failed to load: " << font_path <<
362 ", trying to fall back to fallback font" << std::endl;
364 font_path = g_settings->get(font_config_prefix + "fallback_font_path");
366 font = gui::CGUITTFont::createTTFont(m_env,
367 font_path.c_str(), size, true, true, font_shadow,
371 m_font_cache[mode][basesize] = font;
375 const std::string &fallback_font_path = m_settings->getDefault("fallback_font_path");
377 if (font_path != fallback_font_path) {
378 // try original fallback font
379 errorstream << "FontEngine: failed to load custom fallback "
380 "font: " << font_path << ", trying to fall back to "
381 "original fallback font" << std::endl;
383 font = gui::CGUITTFont::createTTFont(m_env,
384 fallback_font_path.c_str(), size, true, true,
385 font_shadow, font_shadow_alpha);
388 m_font_cache[mode][basesize] = font;
395 errorstream << "FontEngine: failed to load freetype font: "
396 << font_path << std::endl;
397 errorstream << "minetest can not continue without a valid font. "
398 "Please correct the 'font_path' setting or install the font "
399 "file in the proper location" << std::endl;
405 /** initialize a font without freetype */
406 void FontEngine::initSimpleFont(unsigned int basesize, FontMode mode)
408 assert(mode == FM_Simple || mode == FM_SimpleMono); // pre-condition
410 std::string font_path;
411 if (mode == FM_Simple) {
412 font_path = m_settings->get("font_path");
414 font_path = m_settings->get("mono_font_path");
416 std::string basename = font_path;
417 std::string ending = font_path.substr(font_path.length() -4);
419 if (ending == ".ttf") {
420 errorstream << "FontEngine: Not trying to open \"" << font_path
421 << "\" which seems to be a truetype font." << std::endl;
425 if ((ending == ".xml") || (ending == ".png")) {
426 basename = font_path.substr(0,font_path.length()-4);
429 if (basesize == FONT_SIZE_UNSPECIFIED)
430 basesize = DEFAULT_FONT_SIZE;
432 u32 size = std::floor(
433 RenderingEngine::getDisplayDensity() *
434 m_settings->getFloat("gui_scaling") *
437 irr::gui::IGUIFont* font = NULL;
439 for(unsigned int offset = 0; offset < MAX_FONT_SIZE_OFFSET; offset++) {
441 // try opening positive offset
442 std::stringstream fontsize_plus_png;
443 fontsize_plus_png << basename << "_" << (size + offset) << ".png";
445 if (fs::PathExists(fontsize_plus_png.str())) {
446 font = m_env->getFont(fontsize_plus_png.str().c_str());
449 verbosestream << "FontEngine: found font: " << fontsize_plus_png.str() << std::endl;
454 std::stringstream fontsize_plus_xml;
455 fontsize_plus_xml << basename << "_" << (size + offset) << ".xml";
457 if (fs::PathExists(fontsize_plus_xml.str())) {
458 font = m_env->getFont(fontsize_plus_xml.str().c_str());
461 verbosestream << "FontEngine: found font: " << fontsize_plus_xml.str() << std::endl;
466 // try negative offset
467 std::stringstream fontsize_minus_png;
468 fontsize_minus_png << basename << "_" << (size - offset) << ".png";
470 if (fs::PathExists(fontsize_minus_png.str())) {
471 font = m_env->getFont(fontsize_minus_png.str().c_str());
474 verbosestream << "FontEngine: found font: " << fontsize_minus_png.str() << std::endl;
479 std::stringstream fontsize_minus_xml;
480 fontsize_minus_xml << basename << "_" << (size - offset) << ".xml";
482 if (fs::PathExists(fontsize_minus_xml.str())) {
483 font = m_env->getFont(fontsize_minus_xml.str().c_str());
486 verbosestream << "FontEngine: found font: " << fontsize_minus_xml.str() << std::endl;
494 if (fs::PathExists(font_path)) {
495 font = m_env->getFont(font_path.c_str());
497 verbosestream << "FontEngine: found font: " << font_path << std::endl;
503 m_font_cache[mode][basesize] = font;