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, void *userdata) {
39 g_fontengine->readSettings();
42 /******************************************************************************/
43 FontEngine::FontEngine(Settings* main_settings, gui::IGUIEnvironment* env) :
44 m_settings(main_settings),
47 m_currentMode(FM_Standard),
53 for (unsigned int i = 0; i < FM_MaxMode; i++) {
54 m_default_size[i] = (FontMode) FONT_SIZE_UNSPECIFIED;
57 assert(m_settings != NULL); // pre-condition
58 assert(m_env != NULL); // pre-condition
59 assert(m_env->getSkin() != NULL); // pre-condition
61 m_currentMode = FM_Simple;
64 if (g_settings->getBool("freetype")) {
65 m_default_size[FM_Standard] = m_settings->getU16("font_size");
66 m_default_size[FM_Fallback] = m_settings->getU16("fallback_font_size");
67 m_default_size[FM_Mono] = m_settings->getU16("mono_font_size");
69 if (is_yes(gettext("needs_fallback_font"))) {
70 m_currentMode = FM_Fallback;
73 m_currentMode = FM_Standard;
77 // having freetype but not using it is quite a strange case so we need to do
78 // special handling for it
79 if (m_currentMode == FM_Simple) {
80 std::stringstream fontsize;
81 fontsize << DEFAULT_FONT_SIZE;
82 m_settings->setDefault("font_size", fontsize.str());
83 m_settings->setDefault("mono_font_size", fontsize.str());
87 m_default_size[FM_Simple] = m_settings->getU16("font_size");
88 m_default_size[FM_SimpleMono] = m_settings->getU16("mono_font_size");
92 if (m_currentMode == FM_Standard) {
93 m_settings->registerChangedCallback("font_size", font_setting_changed, NULL);
94 m_settings->registerChangedCallback("font_path", font_setting_changed, NULL);
95 m_settings->registerChangedCallback("font_shadow", font_setting_changed, NULL);
96 m_settings->registerChangedCallback("font_shadow_alpha", font_setting_changed, NULL);
98 else if (m_currentMode == FM_Fallback) {
99 m_settings->registerChangedCallback("fallback_font_size", font_setting_changed, NULL);
100 m_settings->registerChangedCallback("fallback_font_path", font_setting_changed, NULL);
101 m_settings->registerChangedCallback("fallback_font_shadow", font_setting_changed, NULL);
102 m_settings->registerChangedCallback("fallback_font_shadow_alpha", font_setting_changed, NULL);
105 m_settings->registerChangedCallback("mono_font_path", font_setting_changed, NULL);
106 m_settings->registerChangedCallback("mono_font_size", font_setting_changed, NULL);
107 m_settings->registerChangedCallback("screen_dpi", font_setting_changed, NULL);
108 m_settings->registerChangedCallback("gui_scaling", font_setting_changed, NULL);
111 /******************************************************************************/
112 FontEngine::~FontEngine()
117 /******************************************************************************/
118 void FontEngine::cleanCache()
120 for ( unsigned int i = 0; i < FM_MaxMode; i++) {
122 for (std::map<unsigned int, irr::gui::IGUIFont*>::iterator iter
123 = m_font_cache[i].begin();
124 iter != m_font_cache[i].end(); iter++) {
125 iter->second->drop();
128 m_font_cache[i].clear();
132 /******************************************************************************/
133 irr::gui::IGUIFont* FontEngine::getFont(unsigned int font_size, FontMode mode)
135 if (mode == FM_Unspecified) {
136 mode = m_currentMode;
138 else if ((mode == FM_Mono) && (m_currentMode == FM_Simple)) {
139 mode = FM_SimpleMono;
142 if (font_size == FONT_SIZE_UNSPECIFIED) {
143 font_size = m_default_size[mode];
146 if ((font_size == m_lastSize) && (mode == m_lastMode)) {
150 if (m_font_cache[mode].find(font_size) == m_font_cache[mode].end()) {
151 initFont(font_size, mode);
154 if (m_font_cache[mode].find(font_size) == m_font_cache[mode].end()) {
158 m_lastSize = font_size;
160 m_lastFont = m_font_cache[mode][font_size];
162 return m_font_cache[mode][font_size];
165 /******************************************************************************/
166 unsigned int FontEngine::getTextHeight(unsigned int font_size, FontMode mode)
168 irr::gui::IGUIFont* font = getFont(font_size, mode);
170 // use current skin font as fallback
172 font = m_env->getSkin()->getFont();
174 FATAL_ERROR_IF(font == NULL, "Could not get skin font");
176 return font->getDimension(L"Some unimportant example String").Height;
179 /******************************************************************************/
180 unsigned int FontEngine::getTextWidth(const std::wstring& text,
181 unsigned int font_size, FontMode mode)
183 irr::gui::IGUIFont* font = getFont(font_size, mode);
185 // use current skin font as fallback
187 font = m_env->getSkin()->getFont();
189 FATAL_ERROR_IF(font == NULL, "Could not get font");
191 return font->getDimension(text.c_str()).Width;
195 /** get line height for a specific font (including empty room between lines) */
196 unsigned int FontEngine::getLineHeight(unsigned int font_size, FontMode mode)
198 irr::gui::IGUIFont* font = getFont(font_size, mode);
200 // use current skin font as fallback
202 font = m_env->getSkin()->getFont();
204 FATAL_ERROR_IF(font == NULL, "Could not get font");
206 return font->getDimension(L"Some unimportant example String").Height
207 + font->getKerningHeight();
210 /******************************************************************************/
211 unsigned int FontEngine::getDefaultFontSize()
213 return m_default_size[m_currentMode];
216 /******************************************************************************/
217 void FontEngine::readSettings()
220 if (g_settings->getBool("freetype")) {
221 m_default_size[FM_Standard] = m_settings->getU16("font_size");
222 m_default_size[FM_Fallback] = m_settings->getU16("fallback_font_size");
223 m_default_size[FM_Mono] = m_settings->getU16("mono_font_size");
225 if (is_yes(gettext("needs_fallback_font"))) {
226 m_currentMode = FM_Fallback;
229 m_currentMode = FM_Standard;
233 m_default_size[FM_Simple] = m_settings->getU16("font_size");
234 m_default_size[FM_SimpleMono] = m_settings->getU16("mono_font_size");
241 /******************************************************************************/
242 void FontEngine::updateSkin()
244 gui::IGUIFont *font = getFont();
247 m_env->getSkin()->setFont(font);
249 errorstream << "FontEngine: Default font file: " <<
250 "\n\t\"" << m_settings->get("font_path") << "\"" <<
251 "\n\trequired for current screen configuration was not found" <<
252 " or was invalid file format." <<
253 "\n\tUsing irrlicht default font." << std::endl;
255 // If we did fail to create a font our own make irrlicht find a default one
256 font = m_env->getSkin()->getFont();
257 FATAL_ERROR_IF(font == NULL, "Could not create/get font");
259 u32 text_height = font->getDimension(L"Hello, world!").Height;
260 infostream << "text_height=" << text_height << std::endl;
263 /******************************************************************************/
264 void FontEngine::updateFontCache()
266 /* the only font to be initialized is default one,
267 * all others are re-initialized on demand */
268 initFont(m_default_size[m_currentMode], m_currentMode);
270 /* reset font quick access */
271 m_lastMode = FM_Unspecified;
276 /******************************************************************************/
277 void FontEngine::initFont(unsigned int basesize, FontMode mode)
280 std::string font_config_prefix;
282 if (mode == FM_Unspecified) {
283 mode = m_currentMode;
289 font_config_prefix = "";
293 font_config_prefix = "fallback_";
297 font_config_prefix = "mono_";
298 if (m_currentMode == FM_Simple)
299 mode = FM_SimpleMono;
302 case FM_Simple: /* Fallthrough */
303 case FM_SimpleMono: /* Fallthrough */
305 font_config_prefix = "";
309 if (m_font_cache[mode].find(basesize) != m_font_cache[mode].end())
312 if ((mode == FM_Simple) || (mode == FM_SimpleMono)) {
313 initSimpleFont(basesize, mode);
318 if (! is_yes(m_settings->get("freetype"))) {
321 unsigned int size = floor(
322 porting::getDisplayDensity() *
323 m_settings->getFloat("gui_scaling") *
326 u32 font_shadow_alpha = 0;
330 g_settings->getU16(font_config_prefix + "font_shadow");
331 } catch (SettingNotFoundException&) {}
334 g_settings->getU16(font_config_prefix + "font_shadow_alpha");
335 } catch (SettingNotFoundException&) {}
337 std::string font_path = g_settings->get(font_config_prefix + "font_path");
339 irr::gui::IGUIFont* font = gui::CGUITTFont::createTTFont(m_env,
340 font_path.c_str(), size, true, true, font_shadow,
344 m_font_cache[mode][basesize] = font;
347 errorstream << "FontEngine: failed to load freetype font: "
348 << font_path << std::endl;
354 /** initialize a font without freetype */
355 void FontEngine::initSimpleFont(unsigned int basesize, FontMode mode)
357 assert(mode == FM_Simple || mode == FM_SimpleMono); // pre-condition
359 std::string font_path = "";
360 if (mode == FM_Simple) {
361 font_path = m_settings->get("font_path");
363 font_path = m_settings->get("mono_font_path");
365 std::string basename = font_path;
366 std::string ending = font_path.substr(font_path.length() -4);
368 if (ending == ".ttf") {
369 errorstream << "FontEngine: Not trying to open \"" << font_path
370 << "\" which seems to be a truetype font." << std::endl;
374 if ((ending == ".xml") || (ending == ".png")) {
375 basename = font_path.substr(0,font_path.length()-4);
378 if (basesize == FONT_SIZE_UNSPECIFIED)
379 basesize = DEFAULT_FONT_SIZE;
381 unsigned int size = floor(
382 porting::getDisplayDensity() *
383 m_settings->getFloat("gui_scaling") *
386 irr::gui::IGUIFont* font = NULL;
388 for(unsigned int offset = 0; offset < MAX_FONT_SIZE_OFFSET; offset++) {
390 // try opening positive offset
391 std::stringstream fontsize_plus_png;
392 fontsize_plus_png << basename << "_" << (size + offset) << ".png";
394 if (fs::PathExists(fontsize_plus_png.str())) {
395 font = m_env->getFont(fontsize_plus_png.str().c_str());
398 verbosestream << "FontEngine: found font: " << fontsize_plus_png.str() << std::endl;
403 std::stringstream fontsize_plus_xml;
404 fontsize_plus_xml << basename << "_" << (size + offset) << ".xml";
406 if (fs::PathExists(fontsize_plus_xml.str())) {
407 font = m_env->getFont(fontsize_plus_xml.str().c_str());
410 verbosestream << "FontEngine: found font: " << fontsize_plus_xml.str() << std::endl;
415 // try negative offset
416 std::stringstream fontsize_minus_png;
417 fontsize_minus_png << basename << "_" << (size - offset) << ".png";
419 if (fs::PathExists(fontsize_minus_png.str())) {
420 font = m_env->getFont(fontsize_minus_png.str().c_str());
423 verbosestream << "FontEngine: found font: " << fontsize_minus_png.str() << std::endl;
428 std::stringstream fontsize_minus_xml;
429 fontsize_minus_xml << basename << "_" << (size - offset) << ".xml";
431 if (fs::PathExists(fontsize_minus_xml.str())) {
432 font = m_env->getFont(fontsize_minus_xml.str().c_str());
435 verbosestream << "FontEngine: found font: " << fontsize_minus_xml.str() << std::endl;
443 if (fs::PathExists(font_path)) {
444 font = m_env->getFont(font_path.c_str());
446 verbosestream << "FontEngine: found font: " << font_path << std::endl;
452 m_font_cache[mode][basesize] = font;