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 (unsigned int i = 0; i < FM_MaxMode; i++) {
50 m_default_size[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 ( unsigned int i = 0; i < FM_MaxMode; i++) {
118 for (std::map<unsigned int, irr::gui::IGUIFont*>::iterator iter
119 = m_font_cache[i].begin();
120 iter != m_font_cache[i].end(); ++iter) {
121 iter->second->drop();
124 m_font_cache[i].clear();
128 /******************************************************************************/
129 irr::gui::IGUIFont* FontEngine::getFont(unsigned int font_size, FontMode mode)
131 if (mode == FM_Unspecified) {
132 mode = m_currentMode;
134 else if ((mode == FM_Mono) && (m_currentMode == FM_Simple)) {
135 mode = FM_SimpleMono;
138 if (font_size == FONT_SIZE_UNSPECIFIED) {
139 font_size = m_default_size[mode];
142 if ((font_size == m_lastSize) && (mode == m_lastMode)) {
146 if (m_font_cache[mode].find(font_size) == m_font_cache[mode].end()) {
147 initFont(font_size, mode);
150 if (m_font_cache[mode].find(font_size) == m_font_cache[mode].end()) {
154 m_lastSize = font_size;
156 m_lastFont = m_font_cache[mode][font_size];
158 return m_font_cache[mode][font_size];
161 /******************************************************************************/
162 unsigned int FontEngine::getTextHeight(unsigned int font_size, FontMode mode)
164 irr::gui::IGUIFont* font = getFont(font_size, mode);
166 // use current skin font as fallback
168 font = m_env->getSkin()->getFont();
170 FATAL_ERROR_IF(font == NULL, "Could not get skin font");
172 return font->getDimension(L"Some unimportant example String").Height;
175 /******************************************************************************/
176 unsigned int FontEngine::getTextWidth(const std::wstring& text,
177 unsigned int font_size, FontMode mode)
179 irr::gui::IGUIFont* font = getFont(font_size, mode);
181 // use current skin font as fallback
183 font = m_env->getSkin()->getFont();
185 FATAL_ERROR_IF(font == NULL, "Could not get font");
187 return font->getDimension(text.c_str()).Width;
191 /** get line height for a specific font (including empty room between lines) */
192 unsigned int FontEngine::getLineHeight(unsigned int font_size, FontMode mode)
194 irr::gui::IGUIFont* font = getFont(font_size, mode);
196 // use current skin font as fallback
198 font = m_env->getSkin()->getFont();
200 FATAL_ERROR_IF(font == NULL, "Could not get font");
202 return font->getDimension(L"Some unimportant example String").Height
203 + font->getKerningHeight();
206 /******************************************************************************/
207 unsigned int FontEngine::getDefaultFontSize()
209 return m_default_size[m_currentMode];
212 /******************************************************************************/
213 void FontEngine::readSettings()
216 if (g_settings->getBool("freetype")) {
217 m_default_size[FM_Standard] = m_settings->getU16("font_size");
218 m_default_size[FM_Fallback] = m_settings->getU16("fallback_font_size");
219 m_default_size[FM_Mono] = m_settings->getU16("mono_font_size");
221 if (is_yes(gettext("needs_fallback_font"))) {
222 m_currentMode = FM_Fallback;
225 m_currentMode = FM_Standard;
229 m_default_size[FM_Simple] = m_settings->getU16("font_size");
230 m_default_size[FM_SimpleMono] = m_settings->getU16("mono_font_size");
237 /******************************************************************************/
238 void FontEngine::updateSkin()
240 gui::IGUIFont *font = getFont();
243 m_env->getSkin()->setFont(font);
245 errorstream << "FontEngine: Default font file: " <<
246 "\n\t\"" << m_settings->get("font_path") << "\"" <<
247 "\n\trequired for current screen configuration was not found" <<
248 " or was invalid file format." <<
249 "\n\tUsing irrlicht default font." << std::endl;
251 // If we did fail to create a font our own make irrlicht find a default one
252 font = m_env->getSkin()->getFont();
253 FATAL_ERROR_IF(font == NULL, "Could not create/get font");
255 u32 text_height = font->getDimension(L"Hello, world!").Height;
256 infostream << "text_height=" << text_height << std::endl;
259 /******************************************************************************/
260 void FontEngine::updateFontCache()
262 /* the only font to be initialized is default one,
263 * all others are re-initialized on demand */
264 initFont(m_default_size[m_currentMode], m_currentMode);
266 /* reset font quick access */
267 m_lastMode = FM_Unspecified;
272 /******************************************************************************/
273 void FontEngine::initFont(unsigned int basesize, FontMode mode)
276 std::string font_config_prefix;
278 if (mode == FM_Unspecified) {
279 mode = m_currentMode;
285 font_config_prefix = "";
289 font_config_prefix = "fallback_";
293 font_config_prefix = "mono_";
294 if (m_currentMode == FM_Simple)
295 mode = FM_SimpleMono;
298 case FM_Simple: /* Fallthrough */
299 case FM_SimpleMono: /* Fallthrough */
301 font_config_prefix = "";
305 if (m_font_cache[mode].find(basesize) != m_font_cache[mode].end())
308 if ((mode == FM_Simple) || (mode == FM_SimpleMono)) {
309 initSimpleFont(basesize, mode);
314 if (! is_yes(m_settings->get("freetype"))) {
317 unsigned int size = floor(RenderingEngine::getDisplayDensity() *
318 m_settings->getFloat("gui_scaling") * basesize);
320 u32 font_shadow_alpha = 0;
324 g_settings->getU16(font_config_prefix + "font_shadow");
325 } catch (SettingNotFoundException&) {}
328 g_settings->getU16(font_config_prefix + "font_shadow_alpha");
329 } catch (SettingNotFoundException&) {}
331 std::string font_path = g_settings->get(font_config_prefix + "font_path");
333 irr::gui::IGUIFont* font = gui::CGUITTFont::createTTFont(m_env,
334 font_path.c_str(), size, true, true, font_shadow,
338 m_font_cache[mode][basesize] = font;
343 errorstream << "FontEngine: failed to load: " << font_path << ", trying to fall back "
344 "to fallback font" << std::endl;
346 font_path = g_settings->get(font_config_prefix + "fallback_font_path");
348 font = gui::CGUITTFont::createTTFont(m_env,
349 font_path.c_str(), size, true, true, font_shadow,
353 m_font_cache[mode][basesize] = font;
358 errorstream << "FontEngine: failed to load freetype font: "
359 << font_path << std::endl;
360 errorstream << "minetest can not continue without a valid font. Please correct "
361 "the 'font_path' setting or install the font file in the proper "
362 "location" << std::endl;
368 /** initialize a font without freetype */
369 void FontEngine::initSimpleFont(unsigned int basesize, FontMode mode)
371 assert(mode == FM_Simple || mode == FM_SimpleMono); // pre-condition
373 std::string font_path = "";
374 if (mode == FM_Simple) {
375 font_path = m_settings->get("font_path");
377 font_path = m_settings->get("mono_font_path");
379 std::string basename = font_path;
380 std::string ending = font_path.substr(font_path.length() -4);
382 if (ending == ".ttf") {
383 errorstream << "FontEngine: Not trying to open \"" << font_path
384 << "\" which seems to be a truetype font." << std::endl;
388 if ((ending == ".xml") || (ending == ".png")) {
389 basename = font_path.substr(0,font_path.length()-4);
392 if (basesize == FONT_SIZE_UNSPECIFIED)
393 basesize = DEFAULT_FONT_SIZE;
395 unsigned int size = floor(
396 RenderingEngine::getDisplayDensity() *
397 m_settings->getFloat("gui_scaling") *
400 irr::gui::IGUIFont* font = NULL;
402 for(unsigned int offset = 0; offset < MAX_FONT_SIZE_OFFSET; offset++) {
404 // try opening positive offset
405 std::stringstream fontsize_plus_png;
406 fontsize_plus_png << basename << "_" << (size + offset) << ".png";
408 if (fs::PathExists(fontsize_plus_png.str())) {
409 font = m_env->getFont(fontsize_plus_png.str().c_str());
412 verbosestream << "FontEngine: found font: " << fontsize_plus_png.str() << std::endl;
417 std::stringstream fontsize_plus_xml;
418 fontsize_plus_xml << basename << "_" << (size + offset) << ".xml";
420 if (fs::PathExists(fontsize_plus_xml.str())) {
421 font = m_env->getFont(fontsize_plus_xml.str().c_str());
424 verbosestream << "FontEngine: found font: " << fontsize_plus_xml.str() << std::endl;
429 // try negative offset
430 std::stringstream fontsize_minus_png;
431 fontsize_minus_png << basename << "_" << (size - offset) << ".png";
433 if (fs::PathExists(fontsize_minus_png.str())) {
434 font = m_env->getFont(fontsize_minus_png.str().c_str());
437 verbosestream << "FontEngine: found font: " << fontsize_minus_png.str() << std::endl;
442 std::stringstream fontsize_minus_xml;
443 fontsize_minus_xml << basename << "_" << (size - offset) << ".xml";
445 if (fs::PathExists(fontsize_minus_xml.str())) {
446 font = m_env->getFont(fontsize_minus_xml.str().c_str());
449 verbosestream << "FontEngine: found font: " << fontsize_minus_xml.str() << std::endl;
457 if (fs::PathExists(font_path)) {
458 font = m_env->getFont(font_path.c_str());
460 verbosestream << "FontEngine: found font: " << font_path << std::endl;
466 m_font_cache[mode][basesize] = font;