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.
19 #include "fontengine.h"
23 #include "constants.h"
28 #include "xCGUITTFont.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),
48 m_currentMode(FM_Standard),
54 for (unsigned int i = 0; i < FM_MaxMode; i++) {
55 m_default_size[i] = (FontMode) FONT_SIZE_UNSPECIFIED;
58 assert(m_settings != NULL); // pre-condition
59 assert(m_env != NULL); // pre-condition
60 assert(m_env->getSkin() != NULL); // pre-condition
62 m_currentMode = FM_Simple;
65 if (g_settings->getBool("freetype")) {
66 m_default_size[FM_Standard] = m_settings->getU16("font_size");
67 m_default_size[FM_Fallback] = m_settings->getU16("fallback_font_size");
68 m_default_size[FM_Mono] = m_settings->getU16("mono_font_size");
70 if (is_yes(gettext("needs_fallback_font"))) {
71 m_currentMode = FM_Fallback;
74 m_currentMode = FM_Standard;
78 // having freetype but not using it is quite a strange case so we need to do
79 // special handling for it
80 if (m_currentMode == FM_Simple) {
81 std::stringstream fontsize;
82 fontsize << DEFAULT_FONT_SIZE;
83 m_settings->setDefault("font_size", fontsize.str());
84 m_settings->setDefault("mono_font_size", fontsize.str());
88 m_default_size[FM_Simple] = m_settings->getU16("font_size");
89 m_default_size[FM_SimpleMono] = m_settings->getU16("mono_font_size");
93 if (m_currentMode == FM_Standard) {
94 m_settings->registerChangedCallback("font_size", font_setting_changed, NULL);
95 m_settings->registerChangedCallback("font_path", font_setting_changed, NULL);
96 m_settings->registerChangedCallback("font_shadow", font_setting_changed, NULL);
97 m_settings->registerChangedCallback("font_shadow_alpha", font_setting_changed, NULL);
99 else if (m_currentMode == FM_Fallback) {
100 m_settings->registerChangedCallback("fallback_font_size", font_setting_changed, NULL);
101 m_settings->registerChangedCallback("fallback_font_path", font_setting_changed, NULL);
102 m_settings->registerChangedCallback("fallback_font_shadow", font_setting_changed, NULL);
103 m_settings->registerChangedCallback("fallback_font_shadow_alpha", font_setting_changed, NULL);
106 m_settings->registerChangedCallback("mono_font_path", font_setting_changed, NULL);
107 m_settings->registerChangedCallback("mono_font_size", font_setting_changed, NULL);
108 m_settings->registerChangedCallback("screen_dpi", font_setting_changed, NULL);
109 m_settings->registerChangedCallback("gui_scaling", font_setting_changed, NULL);
112 /******************************************************************************/
113 FontEngine::~FontEngine()
118 /******************************************************************************/
119 void FontEngine::cleanCache()
121 for ( unsigned int i = 0; i < FM_MaxMode; i++) {
123 for (std::map<unsigned int, irr::gui::IGUIFont*>::iterator iter
124 = m_font_cache[i].begin();
125 iter != m_font_cache[i].end(); iter++) {
126 iter->second->drop();
129 m_font_cache[i].clear();
133 /******************************************************************************/
134 irr::gui::IGUIFont* FontEngine::getFont(unsigned int font_size, FontMode mode)
136 if (mode == FM_Unspecified) {
137 mode = m_currentMode;
139 else if ((mode == FM_Mono) && (m_currentMode == FM_Simple)) {
140 mode = FM_SimpleMono;
143 if (font_size == FONT_SIZE_UNSPECIFIED) {
144 font_size = m_default_size[mode];
147 if ((font_size == m_lastSize) && (mode == m_lastMode)) {
151 if (m_font_cache[mode].find(font_size) == m_font_cache[mode].end()) {
152 initFont(font_size, mode);
155 if (m_font_cache[mode].find(font_size) == m_font_cache[mode].end()) {
159 m_lastSize = font_size;
161 m_lastFont = m_font_cache[mode][font_size];
163 return m_font_cache[mode][font_size];
166 /******************************************************************************/
167 unsigned int FontEngine::getTextHeight(unsigned int font_size, FontMode mode)
169 irr::gui::IGUIFont* font = getFont(font_size, mode);
171 // use current skin font as fallback
173 font = m_env->getSkin()->getFont();
175 FATAL_ERROR_IF(font == NULL, "Could not get skin font");
177 return font->getDimension(L"Some unimportant example String").Height;
180 /******************************************************************************/
181 unsigned int FontEngine::getTextWidth(const std::wstring& text,
182 unsigned int font_size, FontMode mode)
184 irr::gui::IGUIFont* font = getFont(font_size, mode);
186 // use current skin font as fallback
188 font = m_env->getSkin()->getFont();
190 FATAL_ERROR_IF(font == NULL, "Could not get font");
192 return font->getDimension(text.c_str()).Width;
196 /** get line height for a specific font (including empty room between lines) */
197 unsigned int FontEngine::getLineHeight(unsigned int font_size, FontMode mode)
199 irr::gui::IGUIFont* font = getFont(font_size, mode);
201 // use current skin font as fallback
203 font = m_env->getSkin()->getFont();
205 FATAL_ERROR_IF(font == NULL, "Could not get font");
207 return font->getDimension(L"Some unimportant example String").Height
208 + font->getKerningHeight();
211 /******************************************************************************/
212 unsigned int FontEngine::getDefaultFontSize()
214 return m_default_size[m_currentMode];
217 /******************************************************************************/
218 void FontEngine::readSettings()
221 if (g_settings->getBool("freetype")) {
222 m_default_size[FM_Standard] = m_settings->getU16("font_size");
223 m_default_size[FM_Fallback] = m_settings->getU16("fallback_font_size");
224 m_default_size[FM_Mono] = m_settings->getU16("mono_font_size");
226 if (is_yes(gettext("needs_fallback_font"))) {
227 m_currentMode = FM_Fallback;
230 m_currentMode = FM_Standard;
234 m_default_size[FM_Simple] = m_settings->getU16("font_size");
235 m_default_size[FM_SimpleMono] = m_settings->getU16("mono_font_size");
242 /******************************************************************************/
243 void FontEngine::updateSkin()
245 gui::IGUIFont *font = getFont();
248 m_env->getSkin()->setFont(font);
250 errorstream << "FontEngine: Default font file: " <<
251 "\n\t\"" << m_settings->get("font_path") << "\"" <<
252 "\n\trequired for current screen configuration was not found" <<
253 " or was invalid file format." <<
254 "\n\tUsing irrlicht default font." << std::endl;
256 // If we did fail to create a font our own make irrlicht find a default one
257 font = m_env->getSkin()->getFont();
258 FATAL_ERROR_IF(font == NULL, "Could not create/get font");
260 u32 text_height = font->getDimension(L"Hello, world!").Height;
261 infostream << "text_height=" << text_height << std::endl;
264 /******************************************************************************/
265 void FontEngine::updateFontCache()
267 /* the only font to be initialized is default one,
268 * all others are re-initialized on demand */
269 initFont(m_default_size[m_currentMode], m_currentMode);
271 /* reset font quick access */
272 m_lastMode = FM_Unspecified;
277 /******************************************************************************/
278 void FontEngine::initFont(unsigned int basesize, FontMode mode)
281 std::string font_config_prefix;
283 if (mode == FM_Unspecified) {
284 mode = m_currentMode;
290 font_config_prefix = "";
294 font_config_prefix = "fallback_";
298 font_config_prefix = "mono_";
299 if (m_currentMode == FM_Simple)
300 mode = FM_SimpleMono;
303 case FM_Simple: /* Fallthrough */
304 case FM_SimpleMono: /* Fallthrough */
306 font_config_prefix = "";
310 if (m_font_cache[mode].find(basesize) != m_font_cache[mode].end())
313 if ((mode == FM_Simple) || (mode == FM_SimpleMono)) {
314 initSimpleFont(basesize, mode);
319 if (! is_yes(m_settings->get("freetype"))) {
322 unsigned int size = floor(
323 porting::getDisplayDensity() *
324 m_settings->getFloat("gui_scaling") *
327 u32 font_shadow_alpha = 0;
331 g_settings->getU16(font_config_prefix + "font_shadow");
332 } catch (SettingNotFoundException&) {}
335 g_settings->getU16(font_config_prefix + "font_shadow_alpha");
336 } catch (SettingNotFoundException&) {}
338 std::string font_path = g_settings->get(font_config_prefix + "font_path");
340 irr::gui::IGUIFont* font = gui::CGUITTFont::createTTFont(m_env,
341 font_path.c_str(), size, true, true, font_shadow,
345 m_font_cache[mode][basesize] = font;
348 errorstream << "FontEngine: failed to load freetype font: "
349 << font_path << std::endl;
355 /** initialize a font without freetype */
356 void FontEngine::initSimpleFont(unsigned int basesize, FontMode mode)
358 assert(mode == FM_Simple || mode == FM_SimpleMono); // pre-condition
360 std::string font_path = "";
361 if (mode == FM_Simple) {
362 font_path = m_settings->get("font_path");
364 font_path = m_settings->get("mono_font_path");
366 std::string basename = font_path;
367 std::string ending = font_path.substr(font_path.length() -4);
369 if (ending == ".ttf") {
370 errorstream << "FontEngine: Not trying to open \"" << font_path
371 << "\" which seems to be a truetype font." << std::endl;
375 if ((ending == ".xml") || (ending == ".png")) {
376 basename = font_path.substr(0,font_path.length()-4);
379 if (basesize == FONT_SIZE_UNSPECIFIED)
380 basesize = DEFAULT_FONT_SIZE;
382 unsigned int size = floor(
383 porting::getDisplayDensity() *
384 m_settings->getFloat("gui_scaling") *
387 irr::gui::IGUIFont* font = NULL;
389 for(unsigned int offset = 0; offset < MAX_FONT_SIZE_OFFSET; offset++) {
391 // try opening positive offset
392 std::stringstream fontsize_plus_png;
393 fontsize_plus_png << basename << "_" << (size + offset) << ".png";
395 if (fs::PathExists(fontsize_plus_png.str())) {
396 font = m_env->getFont(fontsize_plus_png.str().c_str());
399 verbosestream << "FontEngine: found font: " << fontsize_plus_png.str() << std::endl;
404 std::stringstream fontsize_plus_xml;
405 fontsize_plus_xml << basename << "_" << (size + offset) << ".xml";
407 if (fs::PathExists(fontsize_plus_xml.str())) {
408 font = m_env->getFont(fontsize_plus_xml.str().c_str());
411 verbosestream << "FontEngine: found font: " << fontsize_plus_xml.str() << std::endl;
416 // try negative offset
417 std::stringstream fontsize_minus_png;
418 fontsize_minus_png << basename << "_" << (size - offset) << ".png";
420 if (fs::PathExists(fontsize_minus_png.str())) {
421 font = m_env->getFont(fontsize_minus_png.str().c_str());
424 verbosestream << "FontEngine: found font: " << fontsize_minus_png.str() << std::endl;
429 std::stringstream fontsize_minus_xml;
430 fontsize_minus_xml << basename << "_" << (size - offset) << ".xml";
432 if (fs::PathExists(fontsize_minus_xml.str())) {
433 font = m_env->getFont(fontsize_minus_xml.str().c_str());
436 verbosestream << "FontEngine: found font: " << fontsize_minus_xml.str() << std::endl;
444 if (fs::PathExists(font_path)) {
445 font = m_env->getFont(font_path.c_str());
447 verbosestream << "FontEngine: found font: " << font_path << std::endl;
453 m_font_cache[mode][basesize] = font;