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"
21 #include "client/renderingengine.h"
28 #include "irrlicht_changes/CGUITTFont.h"
31 /** maximum size distance for getting a "similar" font size */
32 #define MAX_FONT_SIZE_OFFSET 10
34 /** reference to access font engine, has to be initialized by main */
35 FontEngine* g_fontengine = NULL;
37 /** callback to be used on change of font size setting */
38 static void font_setting_changed(const std::string &name, void *userdata)
40 g_fontengine->readSettings();
43 /******************************************************************************/
44 FontEngine::FontEngine(Settings* main_settings, gui::IGUIEnvironment* env) :
45 m_settings(main_settings),
49 for (u32 &i : m_default_size) {
50 i = (FontMode) FONT_SIZE_UNSPECIFIED;
53 assert(m_settings != NULL); // pre-condition
54 assert(m_env != NULL); // pre-condition
55 assert(m_env->getSkin() != NULL); // pre-condition
57 m_currentMode = FM_Simple;
60 if (g_settings->getBool("freetype")) {
61 m_default_size[FM_Standard] = m_settings->getU16("font_size");
62 m_default_size[FM_Fallback] = m_settings->getU16("fallback_font_size");
63 m_default_size[FM_Mono] = m_settings->getU16("mono_font_size");
65 if (is_yes(gettext("needs_fallback_font"))) {
66 m_currentMode = FM_Fallback;
69 m_currentMode = FM_Standard;
73 // having freetype but not using it is quite a strange case so we need to do
74 // special handling for it
75 if (m_currentMode == FM_Simple) {
76 std::stringstream fontsize;
77 fontsize << DEFAULT_FONT_SIZE;
78 m_settings->setDefault("font_size", fontsize.str());
79 m_settings->setDefault("mono_font_size", fontsize.str());
83 m_default_size[FM_Simple] = m_settings->getU16("font_size");
84 m_default_size[FM_SimpleMono] = m_settings->getU16("mono_font_size");
88 if (m_currentMode == FM_Standard) {
89 m_settings->registerChangedCallback("font_size", font_setting_changed, NULL);
90 m_settings->registerChangedCallback("font_path", font_setting_changed, NULL);
91 m_settings->registerChangedCallback("font_shadow", font_setting_changed, NULL);
92 m_settings->registerChangedCallback("font_shadow_alpha", font_setting_changed, NULL);
94 else if (m_currentMode == FM_Fallback) {
95 m_settings->registerChangedCallback("fallback_font_size", font_setting_changed, NULL);
96 m_settings->registerChangedCallback("fallback_font_path", font_setting_changed, NULL);
97 m_settings->registerChangedCallback("fallback_font_shadow", font_setting_changed, NULL);
98 m_settings->registerChangedCallback("fallback_font_shadow_alpha", font_setting_changed, NULL);
101 m_settings->registerChangedCallback("mono_font_path", font_setting_changed, NULL);
102 m_settings->registerChangedCallback("mono_font_size", font_setting_changed, NULL);
103 m_settings->registerChangedCallback("screen_dpi", font_setting_changed, NULL);
104 m_settings->registerChangedCallback("gui_scaling", font_setting_changed, NULL);
107 /******************************************************************************/
108 FontEngine::~FontEngine()
113 /******************************************************************************/
114 void FontEngine::cleanCache()
116 for (auto &font_cache_it : m_font_cache) {
118 for (auto &font_it : font_cache_it) {
119 font_it.second->drop();
120 font_it.second = NULL;
122 font_cache_it.clear();
126 /******************************************************************************/
127 irr::gui::IGUIFont* FontEngine::getFont(unsigned int font_size, FontMode mode)
129 if (mode == FM_Unspecified) {
130 mode = m_currentMode;
132 else if ((mode == FM_Mono) && (m_currentMode == FM_Simple)) {
133 mode = FM_SimpleMono;
136 if (font_size == FONT_SIZE_UNSPECIFIED) {
137 font_size = m_default_size[mode];
140 if ((font_size == m_lastSize) && (mode == m_lastMode)) {
144 if (m_font_cache[mode].find(font_size) == m_font_cache[mode].end()) {
145 initFont(font_size, mode);
148 if (m_font_cache[mode].find(font_size) == m_font_cache[mode].end()) {
152 m_lastSize = font_size;
154 m_lastFont = m_font_cache[mode][font_size];
156 return m_font_cache[mode][font_size];
159 /******************************************************************************/
160 unsigned int FontEngine::getTextHeight(unsigned int font_size, FontMode mode)
162 irr::gui::IGUIFont* font = getFont(font_size, mode);
164 // use current skin font as fallback
166 font = m_env->getSkin()->getFont();
168 FATAL_ERROR_IF(font == NULL, "Could not get skin font");
170 return font->getDimension(L"Some unimportant example String").Height;
173 /******************************************************************************/
174 unsigned int FontEngine::getTextWidth(const std::wstring& text,
175 unsigned int font_size, FontMode mode)
177 irr::gui::IGUIFont* font = getFont(font_size, mode);
179 // use current skin font as fallback
181 font = m_env->getSkin()->getFont();
183 FATAL_ERROR_IF(font == NULL, "Could not get font");
185 return font->getDimension(text.c_str()).Width;
189 /** get line height for a specific font (including empty room between lines) */
190 unsigned int FontEngine::getLineHeight(unsigned int font_size, FontMode mode)
192 irr::gui::IGUIFont* font = getFont(font_size, mode);
194 // use current skin font as fallback
196 font = m_env->getSkin()->getFont();
198 FATAL_ERROR_IF(font == NULL, "Could not get font");
200 return font->getDimension(L"Some unimportant example String").Height
201 + font->getKerningHeight();
204 /******************************************************************************/
205 unsigned int FontEngine::getDefaultFontSize()
207 return m_default_size[m_currentMode];
210 /******************************************************************************/
211 void FontEngine::readSettings()
214 if (g_settings->getBool("freetype")) {
215 m_default_size[FM_Standard] = m_settings->getU16("font_size");
216 m_default_size[FM_Fallback] = m_settings->getU16("fallback_font_size");
217 m_default_size[FM_Mono] = m_settings->getU16("mono_font_size");
219 if (is_yes(gettext("needs_fallback_font"))) {
220 m_currentMode = FM_Fallback;
223 m_currentMode = FM_Standard;
227 m_default_size[FM_Simple] = m_settings->getU16("font_size");
228 m_default_size[FM_SimpleMono] = m_settings->getU16("mono_font_size");
235 /******************************************************************************/
236 void FontEngine::updateSkin()
238 gui::IGUIFont *font = getFont();
241 m_env->getSkin()->setFont(font);
243 errorstream << "FontEngine: Default font file: " <<
244 "\n\t\"" << m_settings->get("font_path") << "\"" <<
245 "\n\trequired for current screen configuration was not found" <<
246 " or was invalid file format." <<
247 "\n\tUsing irrlicht default font." << std::endl;
249 // If we did fail to create a font our own make irrlicht find a default one
250 font = m_env->getSkin()->getFont();
251 FATAL_ERROR_IF(font == NULL, "Could not create/get font");
253 u32 text_height = font->getDimension(L"Hello, world!").Height;
254 infostream << "text_height=" << text_height << std::endl;
257 /******************************************************************************/
258 void FontEngine::updateFontCache()
260 /* the only font to be initialized is default one,
261 * all others are re-initialized on demand */
262 initFont(m_default_size[m_currentMode], m_currentMode);
264 /* reset font quick access */
265 m_lastMode = FM_Unspecified;
270 /******************************************************************************/
271 void FontEngine::initFont(unsigned int basesize, FontMode mode)
274 std::string font_config_prefix;
276 if (mode == FM_Unspecified) {
277 mode = m_currentMode;
283 font_config_prefix = "";
287 font_config_prefix = "fallback_";
291 font_config_prefix = "mono_";
292 if (m_currentMode == FM_Simple)
293 mode = FM_SimpleMono;
296 case FM_Simple: /* Fallthrough */
297 case FM_SimpleMono: /* Fallthrough */
299 font_config_prefix = "";
303 if (m_font_cache[mode].find(basesize) != m_font_cache[mode].end())
306 if ((mode == FM_Simple) || (mode == FM_SimpleMono)) {
307 initSimpleFont(basesize, mode);
312 if (! is_yes(m_settings->get("freetype"))) {
315 unsigned int size = floor(RenderingEngine::getDisplayDensity() *
316 m_settings->getFloat("gui_scaling") * basesize);
318 u32 font_shadow_alpha = 0;
322 g_settings->getU16(font_config_prefix + "font_shadow");
323 } catch (SettingNotFoundException&) {}
326 g_settings->getU16(font_config_prefix + "font_shadow_alpha");
327 } catch (SettingNotFoundException&) {}
329 std::string font_path = g_settings->get(font_config_prefix + "font_path");
331 irr::gui::IGUIFont* font = gui::CGUITTFont::createTTFont(m_env,
332 font_path.c_str(), size, true, true, font_shadow,
336 m_font_cache[mode][basesize] = font;
340 if (font_config_prefix == "mono_") {
341 const std::string &mono_font_path = m_settings->getDefault("mono_font_path");
343 if (font_path != mono_font_path) {
344 // try original mono font
345 errorstream << "FontEngine: failed to load custom mono "
346 "font: " << font_path << ", trying to fall back to "
347 "original mono font" << std::endl;
349 font = gui::CGUITTFont::createTTFont(m_env,
350 mono_font_path.c_str(), size, true, true,
351 font_shadow, font_shadow_alpha);
354 m_font_cache[mode][basesize] = font;
360 errorstream << "FontEngine: failed to load: " << font_path <<
361 ", trying to fall back to fallback font" << std::endl;
363 font_path = g_settings->get(font_config_prefix + "fallback_font_path");
365 font = gui::CGUITTFont::createTTFont(m_env,
366 font_path.c_str(), size, true, true, font_shadow,
370 m_font_cache[mode][basesize] = font;
374 const std::string &fallback_font_path = m_settings->getDefault("fallback_font_path");
376 if (font_path != fallback_font_path) {
377 // try original fallback font
378 errorstream << "FontEngine: failed to load custom fallback "
379 "font: " << font_path << ", trying to fall back to "
380 "original fallback font" << std::endl;
382 font = gui::CGUITTFont::createTTFont(m_env,
383 fallback_font_path.c_str(), size, true, true,
384 font_shadow, font_shadow_alpha);
387 m_font_cache[mode][basesize] = font;
394 errorstream << "FontEngine: failed to load freetype font: "
395 << font_path << std::endl;
396 errorstream << "minetest can not continue without a valid font. "
397 "Please correct the 'font_path' setting or install the font "
398 "file in the proper location" << std::endl;
404 /** initialize a font without freetype */
405 void FontEngine::initSimpleFont(unsigned int basesize, FontMode mode)
407 assert(mode == FM_Simple || mode == FM_SimpleMono); // pre-condition
409 std::string font_path;
410 if (mode == FM_Simple) {
411 font_path = m_settings->get("font_path");
413 font_path = m_settings->get("mono_font_path");
415 std::string basename = font_path;
416 std::string ending = font_path.substr(font_path.length() -4);
418 if (ending == ".ttf") {
419 errorstream << "FontEngine: Not trying to open \"" << font_path
420 << "\" which seems to be a truetype font." << std::endl;
424 if ((ending == ".xml") || (ending == ".png")) {
425 basename = font_path.substr(0,font_path.length()-4);
428 if (basesize == FONT_SIZE_UNSPECIFIED)
429 basesize = DEFAULT_FONT_SIZE;
431 unsigned int size = floor(
432 RenderingEngine::getDisplayDensity() *
433 m_settings->getFloat("gui_scaling") *
436 irr::gui::IGUIFont* font = NULL;
438 for(unsigned int offset = 0; offset < MAX_FONT_SIZE_OFFSET; offset++) {
440 // try opening positive offset
441 std::stringstream fontsize_plus_png;
442 fontsize_plus_png << basename << "_" << (size + offset) << ".png";
444 if (fs::PathExists(fontsize_plus_png.str())) {
445 font = m_env->getFont(fontsize_plus_png.str().c_str());
448 verbosestream << "FontEngine: found font: " << fontsize_plus_png.str() << std::endl;
453 std::stringstream fontsize_plus_xml;
454 fontsize_plus_xml << basename << "_" << (size + offset) << ".xml";
456 if (fs::PathExists(fontsize_plus_xml.str())) {
457 font = m_env->getFont(fontsize_plus_xml.str().c_str());
460 verbosestream << "FontEngine: found font: " << fontsize_plus_xml.str() << std::endl;
465 // try negative offset
466 std::stringstream fontsize_minus_png;
467 fontsize_minus_png << basename << "_" << (size - offset) << ".png";
469 if (fs::PathExists(fontsize_minus_png.str())) {
470 font = m_env->getFont(fontsize_minus_png.str().c_str());
473 verbosestream << "FontEngine: found font: " << fontsize_minus_png.str() << std::endl;
478 std::stringstream fontsize_minus_xml;
479 fontsize_minus_xml << basename << "_" << (size - offset) << ".xml";
481 if (fs::PathExists(fontsize_minus_xml.str())) {
482 font = m_env->getFont(fontsize_minus_xml.str().c_str());
485 verbosestream << "FontEngine: found font: " << fontsize_minus_xml.str() << std::endl;
493 if (fs::PathExists(font_path)) {
494 font = m_env->getFont(font_path.c_str());
496 verbosestream << "FontEngine: found font: " << font_path << std::endl;
502 m_font_cache[mode][basesize] = font;