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"
24 #include "constants.h"
29 #include "xCGUITTFont.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* glb_fontengine = NULL;
38 /** callback to be used on change of font size setting */
39 static void font_setting_changed(const std::string) {
40 glb_fontengine->readSettings();
43 /******************************************************************************/
44 FontEngine::FontEngine(Settings* main_settings, gui::IGUIEnvironment* env) :
45 m_settings(main_settings),
49 m_currentMode(FM_Standard),
55 for ( unsigned int i = 0; i < FM_MaxMode; i++) {
56 m_default_size[i] = (FontMode) FONT_SIZE_UNSPECIFIED;
59 assert(m_settings != NULL);
60 assert(m_env != NULL);
61 assert(m_env->getSkin() != NULL);
63 m_currentMode = FM_Simple;
66 if (g_settings->getBool("freetype")) {
67 m_default_size[FM_Standard] = m_settings->getU16("font_size");
68 m_default_size[FM_Fallback] = m_settings->getU16("fallback_font_size");
69 m_default_size[FM_Mono] = m_settings->getU16("mono_font_size");
71 if (is_yes(gettext("needs_fallback_font"))) {
72 m_currentMode = FM_Fallback;
75 m_currentMode = FM_Standard;
79 // having freetype but not using it is quite a strange case so we need to do
80 // special handling for it
81 if (m_currentMode == FM_Simple) {
82 std::stringstream fontsize;
83 fontsize << DEFAULT_FONT_SIZE;
84 m_settings->setDefault("font_size", fontsize.str());
85 m_settings->setDefault("mono_font_size", fontsize.str());
89 m_default_size[FM_Simple] = m_settings->getU16("font_size");
90 m_default_size[FM_SimpleMono] = m_settings->getU16("mono_font_size");
94 if (m_currentMode == FM_Standard) {
95 m_settings->registerChangedCallback("font_size", font_setting_changed);
96 m_settings->registerChangedCallback("font_path", font_setting_changed);
97 m_settings->registerChangedCallback("font_shadow", font_setting_changed);
98 m_settings->registerChangedCallback("font_shadow_alpha", font_setting_changed);
100 else if (m_currentMode == FM_Fallback) {
101 m_settings->registerChangedCallback("fallback_font_size", font_setting_changed);
102 m_settings->registerChangedCallback("fallback_font_path", font_setting_changed);
103 m_settings->registerChangedCallback("fallback_font_shadow", font_setting_changed);
104 m_settings->registerChangedCallback("fallback_font_shadow_alpha", font_setting_changed);
107 m_settings->registerChangedCallback("mono_font_path", font_setting_changed);
108 m_settings->registerChangedCallback("mono_font_size", font_setting_changed);
109 m_settings->registerChangedCallback("screen_dpi", font_setting_changed);
110 m_settings->registerChangedCallback("gui_scaling", font_setting_changed);
113 /******************************************************************************/
114 FontEngine::~FontEngine()
119 /******************************************************************************/
120 void FontEngine::cleanCache()
122 for ( unsigned int i = 0; i < FM_MaxMode; i++) {
124 for (std::map<unsigned int, irr::gui::IGUIFont*>::iterator iter
125 = m_font_cache[i].begin();
126 iter != m_font_cache[i].end(); iter++) {
127 iter->second->drop();
130 m_font_cache[i].clear();
134 /******************************************************************************/
135 irr::gui::IGUIFont* FontEngine::getFont(unsigned int font_size, FontMode mode)
137 if (mode == FM_Unspecified) {
138 mode = m_currentMode;
140 else if ((mode == FM_Mono) && (m_currentMode == FM_Simple)) {
141 mode = FM_SimpleMono;
144 if (font_size == FONT_SIZE_UNSPECIFIED) {
145 font_size = m_default_size[mode];
148 if ((font_size == m_lastSize) && (mode == m_lastMode)) {
152 if (m_font_cache[mode].find(font_size) == m_font_cache[mode].end()) {
153 initFont(font_size, mode);
156 if (m_font_cache[mode].find(font_size) == m_font_cache[mode].end()) {
160 m_lastSize = font_size;
162 m_lastFont = m_font_cache[mode][font_size];
164 return m_font_cache[mode][font_size];
167 /******************************************************************************/
168 unsigned int FontEngine::getTextHeight(unsigned int font_size, FontMode mode)
170 irr::gui::IGUIFont* font = getFont(font_size, mode);
172 // use current skin font as fallback
174 font = m_env->getSkin()->getFont();
176 assert(font != NULL);
178 return font->getDimension(L"Some unimportant example String").Height;
181 /******************************************************************************/
182 unsigned int FontEngine::getTextWidth(const std::wstring& text,
183 unsigned int font_size, FontMode mode)
185 irr::gui::IGUIFont* font = getFont(font_size, mode);
187 // use current skin font as fallback
189 font = m_env->getSkin()->getFont();
191 assert(font != NULL);
193 return font->getDimension(text.c_str()).Width;
197 /** get line height for a specific font (including empty room between lines) */
198 unsigned int FontEngine::getLineHeight(unsigned int font_size, FontMode mode)
200 irr::gui::IGUIFont* font = getFont(font_size, mode);
202 // use current skin font as fallback
204 font = m_env->getSkin()->getFont();
206 assert(font != NULL);
208 return font->getDimension(L"Some unimportant example String").Height
209 + font->getKerningHeight();
212 /******************************************************************************/
213 unsigned int FontEngine::getDefaultFontSize()
215 return m_default_size[m_currentMode];
218 /******************************************************************************/
219 void FontEngine::readSettings()
222 if (g_settings->getBool("freetype")) {
223 m_default_size[FM_Standard] = m_settings->getU16("font_size");
224 m_default_size[FM_Fallback] = m_settings->getU16("fallback_font_size");
225 m_default_size[FM_Mono] = m_settings->getU16("mono_font_size");
227 if (is_yes(gettext("needs_fallback_font"))) {
228 m_currentMode = FM_Fallback;
231 m_currentMode = FM_Standard;
235 m_default_size[FM_Simple] = m_settings->getU16("font_size");
236 m_default_size[FM_SimpleMono] = m_settings->getU16("mono_font_size");
243 /******************************************************************************/
244 void FontEngine::updateSkin()
246 gui::IGUIFont *font = getFont();
249 m_env->getSkin()->setFont(font);
251 errorstream << "FontEngine: Default font file: " <<
252 "\n\t\"" << m_settings->get("font_path") << "\"" <<
253 "\n\trequired for current screen configuration was not found" <<
254 " or was invalid file format." <<
255 "\n\tUsing irrlicht default font." << std::endl;
257 // If we did fail to create a font our own make irrlicht find a default one
258 font = m_env->getSkin()->getFont();
261 u32 text_height = font->getDimension(L"Hello, world!").Height;
262 infostream << "text_height=" << text_height << std::endl;
265 /******************************************************************************/
266 void FontEngine::updateFontCache()
268 /* the only font to be initialized is default one,
269 * all others are re-initialized on demand */
270 initFont(m_default_size[m_currentMode], m_currentMode);
272 /* reset font quick access */
273 m_lastMode = FM_Unspecified;
278 /******************************************************************************/
279 void FontEngine::initFont(unsigned int basesize, FontMode mode)
282 std::string font_config_prefix;
284 if (mode == FM_Unspecified) {
285 mode = m_currentMode;
291 font_config_prefix = "";
295 font_config_prefix = "fallback_";
299 font_config_prefix = "mono_";
300 if (m_currentMode == FM_Simple)
301 mode = FM_SimpleMono;
304 case FM_Simple: /* Fallthrough */
305 case FM_SimpleMono: /* Fallthrough */
307 font_config_prefix = "";
311 if (m_font_cache[mode].find(basesize) != m_font_cache[mode].end())
314 if ((mode == FM_Simple) || (mode == FM_SimpleMono)) {
315 initSimpleFont(basesize, mode);
320 if (! is_yes(m_settings->get("freetype"))) {
323 unsigned int size = floor(
324 porting::getDisplayDensity() *
325 m_settings->getFloat("gui_scaling") *
328 u32 font_shadow_alpha = 0;
332 g_settings->getU16(font_config_prefix + "font_shadow");
333 } catch (SettingNotFoundException&) {}
336 g_settings->getU16(font_config_prefix + "font_shadow_alpha");
337 } catch (SettingNotFoundException&) {}
339 std::string font_path = g_settings->get(font_config_prefix + "font_path");
341 if (font_path.substr(font_path.length() -4) != ".ttf") {
342 errorstream << "FontEngine: \"" << font_path
343 << "\" doesn't seem to be a ttf File." << std::endl;
347 irr::gui::IGUIFont* font = gui::CGUITTFont::createTTFont(m_env,
348 font_path.c_str(), size, true, true, font_shadow,
353 m_font_cache[mode][basesize] = font;
356 errorstream << "FontEngine: failed to load freetype font: "
357 << font_path << std::endl;
363 /** initialize a font without freetype */
364 void FontEngine::initSimpleFont(unsigned int basesize, FontMode mode)
366 assert((mode == FM_Simple) || (mode == FM_SimpleMono));
368 std::string font_path = "";
369 if (mode == FM_Simple) {
370 font_path = m_settings->get("font_path");
372 font_path = m_settings->get("mono_font_path");
374 std::string basename = font_path;
375 std::string ending = font_path.substr(font_path.length() -4);
377 if (ending == ".ttf") {
378 errorstream << "FontEngine: Not trying to open \"" << font_path
379 << "\" which seems to be a truetype font." << std::endl;
383 if ((ending == ".xml") || ( ending == ".png")) {
384 basename = font_path.substr(0,font_path.length()-4);
387 if (basesize == FONT_SIZE_UNSPECIFIED)
388 basesize = DEFAULT_FONT_SIZE;
390 unsigned int size = floor(
391 porting::getDisplayDensity() *
392 m_settings->getFloat("gui_scaling") *
395 irr::gui::IGUIFont* font = NULL;
397 for(unsigned int offset = 0; offset < MAX_FONT_SIZE_OFFSET; offset++) {
399 // try opening positive offset
400 std::stringstream fontsize_plus_png;
401 fontsize_plus_png << basename << "_" << (size + offset) << ".png";
403 if (fs::PathExists(fontsize_plus_png.str())) {
404 font = m_env->getFont(fontsize_plus_png.str().c_str());
407 verbosestream << "FontEngine: found font: " << fontsize_plus_png.str() << std::endl;
412 std::stringstream fontsize_plus_xml;
413 fontsize_plus_xml << basename << "_" << (size + offset) << ".xml";
415 if (fs::PathExists(fontsize_plus_xml.str())) {
416 font = m_env->getFont(fontsize_plus_xml.str().c_str());
419 verbosestream << "FontEngine: found font: " << fontsize_plus_xml.str() << std::endl;
424 // try negative offset
425 std::stringstream fontsize_minus_png;
426 fontsize_minus_png << basename << "_" << (size - offset) << ".png";
428 if (fs::PathExists(fontsize_minus_png.str())) {
429 font = m_env->getFont(fontsize_minus_png.str().c_str());
432 verbosestream << "FontEngine: found font: " << fontsize_minus_png.str() << std::endl;
437 std::stringstream fontsize_minus_xml;
438 fontsize_minus_xml << basename << "_" << (size - offset) << ".xml";
440 if (fs::PathExists(fontsize_minus_xml.str())) {
441 font = m_env->getFont(fontsize_minus_xml.str().c_str());
444 verbosestream << "FontEngine: found font: " << fontsize_minus_xml.str() << std::endl;
452 if (fs::PathExists(font_path)) {
453 font = m_env->getFont(font_path.c_str());
455 verbosestream << "FontEngine: found font: " << font_path << std::endl;
461 m_font_cache[mode][basesize] = font;