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"
22 #include "client/renderingengine.h"
29 #include "irrlicht_changes/CGUITTFont.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* g_fontengine = NULL;
38 /** callback to be used on change of font size setting */
39 static void font_setting_changed(const std::string &name, void *userdata)
41 g_fontengine->readSettings();
44 /******************************************************************************/
45 FontEngine::FontEngine(gui::IGUIEnvironment* env) :
49 for (u32 &i : m_default_size) {
50 i = (FontMode) FONT_SIZE_UNSPECIFIED;
53 assert(g_settings != NULL); // pre-condition
54 assert(m_env != NULL); // pre-condition
55 assert(m_env->getSkin() != NULL); // pre-condition
59 if (m_currentMode != FM_Simple) {
60 g_settings->registerChangedCallback("font_size", font_setting_changed, NULL);
61 g_settings->registerChangedCallback("font_bold", font_setting_changed, NULL);
62 g_settings->registerChangedCallback("font_italic", font_setting_changed, NULL);
63 g_settings->registerChangedCallback("font_path", font_setting_changed, NULL);
64 g_settings->registerChangedCallback("font_path_bold", font_setting_changed, NULL);
65 g_settings->registerChangedCallback("font_path_italic", font_setting_changed, NULL);
66 g_settings->registerChangedCallback("font_path_bolditalic", font_setting_changed, NULL);
67 g_settings->registerChangedCallback("font_shadow", font_setting_changed, NULL);
68 g_settings->registerChangedCallback("font_shadow_alpha", font_setting_changed, NULL);
69 g_settings->registerChangedCallback("fallback_font_path", font_setting_changed, NULL);
72 g_settings->registerChangedCallback("mono_font_path", font_setting_changed, NULL);
73 g_settings->registerChangedCallback("mono_font_size", font_setting_changed, NULL);
74 g_settings->registerChangedCallback("screen_dpi", font_setting_changed, NULL);
75 g_settings->registerChangedCallback("gui_scaling", font_setting_changed, NULL);
78 /******************************************************************************/
79 FontEngine::~FontEngine()
84 /******************************************************************************/
85 void FontEngine::cleanCache()
87 RecursiveMutexAutoLock l(m_font_mutex);
89 for (auto &font_cache_it : m_font_cache) {
91 for (auto &font_it : font_cache_it) {
92 font_it.second->drop();
93 font_it.second = nullptr;
95 font_cache_it.clear();
99 /******************************************************************************/
100 irr::gui::IGUIFont *FontEngine::getFont(FontSpec spec)
102 return getFont(spec, false);
105 irr::gui::IGUIFont *FontEngine::getFont(FontSpec spec, bool may_fail)
107 if (spec.mode == FM_Unspecified) {
108 spec.mode = m_currentMode;
109 } else if (m_currentMode == FM_Simple) {
110 // Freetype disabled -> Force simple mode
111 spec.mode = (spec.mode == FM_Mono ||
112 spec.mode == FM_SimpleMono) ?
113 FM_SimpleMono : FM_Simple;
114 // Support for those could be added, but who cares?
117 } else if (spec.mode == _FM_Fallback) {
118 // Fallback font doesn't support these either
123 // Fallback to default size
124 if (spec.size == FONT_SIZE_UNSPECIFIED)
125 spec.size = m_default_size[spec.mode];
127 RecursiveMutexAutoLock l(m_font_mutex);
129 const auto &cache = m_font_cache[spec.getHash()];
130 auto it = cache.find(spec.size);
131 if (it != cache.end())
134 // Font does not yet exist
135 gui::IGUIFont *font = nullptr;
136 if (spec.mode == FM_Simple || spec.mode == FM_SimpleMono)
137 font = initSimpleFont(spec);
139 font = initFont(spec);
141 if (!font && !may_fail) {
142 errorstream << "Minetest cannot continue without a valid font. "
143 "Please correct the 'font_path' setting or install the font "
144 "file in the proper location." << std::endl;
148 m_font_cache[spec.getHash()][spec.size] = font;
153 /******************************************************************************/
154 unsigned int FontEngine::getTextHeight(const FontSpec &spec)
156 gui::IGUIFont *font = getFont(spec);
158 return font->getDimension(L"Some unimportant example String").Height;
161 /******************************************************************************/
162 unsigned int FontEngine::getTextWidth(const std::wstring &text, const FontSpec &spec)
164 gui::IGUIFont *font = getFont(spec);
166 return font->getDimension(text.c_str()).Width;
169 /** get line height for a specific font (including empty room between lines) */
170 unsigned int FontEngine::getLineHeight(const FontSpec &spec)
172 gui::IGUIFont *font = getFont(spec);
174 return font->getDimension(L"Some unimportant example String").Height
175 + font->getKerningHeight();
178 /******************************************************************************/
179 unsigned int FontEngine::getDefaultFontSize()
181 return m_default_size[m_currentMode];
184 unsigned int FontEngine::getFontSize(FontMode mode)
186 if (m_currentMode == FM_Simple) {
187 if (mode == FM_Mono || mode == FM_SimpleMono)
188 return m_default_size[FM_SimpleMono];
190 return m_default_size[FM_Simple];
193 if (mode == FM_Unspecified)
194 return m_default_size[FM_Standard];
196 return m_default_size[mode];
199 /******************************************************************************/
200 void FontEngine::readSettings()
202 if (USE_FREETYPE && g_settings->getBool("freetype")) {
203 m_default_size[FM_Standard] = g_settings->getU16("font_size");
204 m_default_size[_FM_Fallback] = g_settings->getU16("font_size");
205 m_default_size[FM_Mono] = g_settings->getU16("mono_font_size");
207 m_default_bold = g_settings->getBool("font_bold");
208 m_default_italic = g_settings->getBool("font_italic");
211 m_currentMode = FM_Simple;
214 m_default_size[FM_Simple] = g_settings->getU16("font_size");
215 m_default_size[FM_SimpleMono] = g_settings->getU16("mono_font_size");
222 /******************************************************************************/
223 void FontEngine::updateSkin()
225 gui::IGUIFont *font = getFont();
228 m_env->getSkin()->setFont(font);
231 /******************************************************************************/
232 void FontEngine::updateFontCache()
234 /* the only font to be initialized is default one,
235 * all others are re-initialized on demand */
236 getFont(FONT_SIZE_UNSPECIFIED, FM_Unspecified);
239 /******************************************************************************/
240 gui::IGUIFont *FontEngine::initFont(const FontSpec &spec)
242 assert(spec.mode != FM_Unspecified);
243 assert(spec.size != FONT_SIZE_UNSPECIFIED);
245 std::string setting_prefix = "";
246 if (spec.mode == FM_Mono)
247 setting_prefix = "mono_";
249 std::string setting_suffix = "";
251 setting_suffix.append("_bold");
253 setting_suffix.append("_italic");
255 u32 size = std::floor(RenderingEngine::getDisplayDensity() *
256 g_settings->getFloat("gui_scaling") * spec.size);
259 errorstream << "FontEngine: attempt to use font size 0" << std::endl;
260 errorstream << " display density: " << RenderingEngine::getDisplayDensity() << std::endl;
265 u16 font_shadow_alpha = 0;
266 g_settings->getU16NoEx(setting_prefix + "font_shadow", font_shadow);
267 g_settings->getU16NoEx(setting_prefix + "font_shadow_alpha",
270 std::string path_setting;
271 if (spec.mode == _FM_Fallback)
272 path_setting = "fallback_font_path";
274 path_setting = setting_prefix + "font_path" + setting_suffix;
276 std::string fallback_settings[] = {
277 g_settings->get(path_setting),
278 Settings::getLayer(SL_DEFAULTS)->get(path_setting)
282 for (const std::string &font_path : fallback_settings) {
283 gui::CGUITTFont *font = gui::CGUITTFont::createTTFont(m_env,
284 font_path.c_str(), size, true, true, font_shadow,
288 errorstream << "FontEngine: Cannot load '" << font_path <<
289 "'. Trying to fall back to another path." << std::endl;
293 if (spec.mode != _FM_Fallback) {
294 FontSpec spec2(spec);
295 spec2.mode = _FM_Fallback;
296 font->setFallback(getFont(spec2, true));
301 errorstream << "FontEngine: Tried to load TTF font but Minetest was"
302 " compiled without Freetype." << std::endl;
307 /** initialize a font without freetype */
308 gui::IGUIFont *FontEngine::initSimpleFont(const FontSpec &spec)
310 assert(spec.mode == FM_Simple || spec.mode == FM_SimpleMono);
311 assert(spec.size != FONT_SIZE_UNSPECIFIED);
313 const std::string &font_path = g_settings->get(
314 (spec.mode == FM_SimpleMono) ? "mono_font_path" : "font_path");
316 size_t pos_dot = font_path.find_last_of('.');
317 std::string basename = font_path, ending;
318 if (pos_dot != std::string::npos)
319 ending = lowercase(font_path.substr(pos_dot));
321 if (ending == ".ttf") {
322 errorstream << "FontEngine: Found font \"" << font_path
323 << "\" but freetype is not available." << std::endl;
327 if (ending == ".xml" || ending == ".png")
328 basename = font_path.substr(0, pos_dot);
330 u32 size = std::floor(
331 RenderingEngine::getDisplayDensity() *
332 g_settings->getFloat("gui_scaling") *
335 irr::gui::IGUIFont *font = nullptr;
336 std::string font_extensions[] = { ".png", ".xml" };
338 // Find nearest matching font scale
339 // Does a "zig-zag motion" (positibe/negative), from 0 to MAX_FONT_SIZE_OFFSET
340 for (s32 zoffset = 0; zoffset < MAX_FONT_SIZE_OFFSET * 2; zoffset++) {
341 std::stringstream path;
344 s32 sign = (zoffset & 1) ? -1 : 1;
345 s32 offset = zoffset >> 1;
347 for (const std::string &ext : font_extensions) {
348 path.str(""); // Clear
349 path << basename << "_" << (size + offset * sign) << ext;
351 if (!fs::PathExists(path.str()))
354 font = m_env->getFont(path.str().c_str());
357 verbosestream << "FontEngine: found font: " << path.str() << std::endl;
368 if (fs::PathExists(font_path)) {
369 font = m_env->getFont(font_path.c_str());
371 verbosestream << "FontEngine: found font: " << font_path << std::endl;