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("font_size_divisible_by", font_setting_changed, NULL);
70 g_settings->registerChangedCallback("fallback_font_path", font_setting_changed, NULL);
73 g_settings->registerChangedCallback("mono_font_path", font_setting_changed, NULL);
74 g_settings->registerChangedCallback("mono_font_size", font_setting_changed, NULL);
75 g_settings->registerChangedCallback("mono_font_size_divisible_by", font_setting_changed, NULL);
76 g_settings->registerChangedCallback("screen_dpi", font_setting_changed, NULL);
77 g_settings->registerChangedCallback("gui_scaling", font_setting_changed, NULL);
80 /******************************************************************************/
81 FontEngine::~FontEngine()
86 /******************************************************************************/
87 void FontEngine::cleanCache()
89 RecursiveMutexAutoLock l(m_font_mutex);
91 for (auto &font_cache_it : m_font_cache) {
93 for (auto &font_it : font_cache_it) {
94 font_it.second->drop();
95 font_it.second = nullptr;
97 font_cache_it.clear();
101 /******************************************************************************/
102 irr::gui::IGUIFont *FontEngine::getFont(FontSpec spec)
104 return getFont(spec, false);
107 irr::gui::IGUIFont *FontEngine::getFont(FontSpec spec, bool may_fail)
109 if (spec.mode == FM_Unspecified) {
110 spec.mode = m_currentMode;
111 } else if (m_currentMode == FM_Simple) {
112 // Freetype disabled -> Force simple mode
113 spec.mode = (spec.mode == FM_Mono ||
114 spec.mode == FM_SimpleMono) ?
115 FM_SimpleMono : FM_Simple;
116 // Support for those could be added, but who cares?
119 } else if (spec.mode == _FM_Fallback) {
120 // Fallback font doesn't support these either
125 // Fallback to default size
126 if (spec.size == FONT_SIZE_UNSPECIFIED)
127 spec.size = m_default_size[spec.mode];
129 RecursiveMutexAutoLock l(m_font_mutex);
131 const auto &cache = m_font_cache[spec.getHash()];
132 auto it = cache.find(spec.size);
133 if (it != cache.end())
136 // Font does not yet exist
137 gui::IGUIFont *font = nullptr;
138 if (spec.mode == FM_Simple || spec.mode == FM_SimpleMono)
139 font = initSimpleFont(spec);
141 font = initFont(spec);
143 if (!font && !may_fail) {
144 errorstream << "Minetest cannot continue without a valid font. "
145 "Please correct the 'font_path' setting or install the font "
146 "file in the proper location." << std::endl;
150 m_font_cache[spec.getHash()][spec.size] = font;
155 /******************************************************************************/
156 unsigned int FontEngine::getTextHeight(const FontSpec &spec)
158 gui::IGUIFont *font = getFont(spec);
160 return font->getDimension(L"Some unimportant example String").Height;
163 /******************************************************************************/
164 unsigned int FontEngine::getTextWidth(const std::wstring &text, const FontSpec &spec)
166 gui::IGUIFont *font = getFont(spec);
168 return font->getDimension(text.c_str()).Width;
171 /** get line height for a specific font (including empty room between lines) */
172 unsigned int FontEngine::getLineHeight(const FontSpec &spec)
174 gui::IGUIFont *font = getFont(spec);
176 return font->getDimension(L"Some unimportant example String").Height
177 + font->getKerningHeight();
180 /******************************************************************************/
181 unsigned int FontEngine::getDefaultFontSize()
183 return m_default_size[m_currentMode];
186 unsigned int FontEngine::getFontSize(FontMode mode)
188 if (m_currentMode == FM_Simple) {
189 if (mode == FM_Mono || mode == FM_SimpleMono)
190 return m_default_size[FM_SimpleMono];
192 return m_default_size[FM_Simple];
195 if (mode == FM_Unspecified)
196 return m_default_size[FM_Standard];
198 return m_default_size[mode];
201 /******************************************************************************/
202 void FontEngine::readSettings()
204 if (USE_FREETYPE && g_settings->getBool("freetype")) {
205 m_default_size[FM_Standard] = g_settings->getU16("font_size");
206 m_default_size[_FM_Fallback] = g_settings->getU16("font_size");
207 m_default_size[FM_Mono] = g_settings->getU16("mono_font_size");
209 m_default_bold = g_settings->getBool("font_bold");
210 m_default_italic = g_settings->getBool("font_italic");
213 m_currentMode = FM_Simple;
216 m_default_size[FM_Simple] = g_settings->getU16("font_size");
217 m_default_size[FM_SimpleMono] = g_settings->getU16("mono_font_size");
224 /******************************************************************************/
225 void FontEngine::updateSkin()
227 gui::IGUIFont *font = getFont();
230 m_env->getSkin()->setFont(font);
233 /******************************************************************************/
234 void FontEngine::updateFontCache()
236 /* the only font to be initialized is default one,
237 * all others are re-initialized on demand */
238 getFont(FONT_SIZE_UNSPECIFIED, FM_Unspecified);
241 /******************************************************************************/
242 gui::IGUIFont *FontEngine::initFont(const FontSpec &spec)
244 assert(spec.mode != FM_Unspecified);
245 assert(spec.size != FONT_SIZE_UNSPECIFIED);
247 std::string setting_prefix = "";
248 if (spec.mode == FM_Mono)
249 setting_prefix = "mono_";
251 std::string setting_suffix = "";
253 setting_suffix.append("_bold");
255 setting_suffix.append("_italic");
257 u32 size = std::max<u32>(spec.size * RenderingEngine::getDisplayDensity() *
258 g_settings->getFloat("gui_scaling"), 1);
260 // Constrain the font size to a certain multiple, if necessary
261 u16 divisible_by = g_settings->getU16(setting_prefix + "font_size_divisible_by");
262 if (divisible_by > 1) {
263 size = std::max<u32>(
264 std::round((double)size / divisible_by) * divisible_by, divisible_by);
267 sanity_check(size != 0);
270 u16 font_shadow_alpha = 0;
271 g_settings->getU16NoEx(setting_prefix + "font_shadow", font_shadow);
272 g_settings->getU16NoEx(setting_prefix + "font_shadow_alpha",
275 std::string path_setting;
276 if (spec.mode == _FM_Fallback)
277 path_setting = "fallback_font_path";
279 path_setting = setting_prefix + "font_path" + setting_suffix;
281 std::string fallback_settings[] = {
282 g_settings->get(path_setting),
283 Settings::getLayer(SL_DEFAULTS)->get(path_setting)
287 for (const std::string &font_path : fallback_settings) {
288 gui::CGUITTFont *font = gui::CGUITTFont::createTTFont(m_env,
289 font_path.c_str(), size, true, true, font_shadow,
293 errorstream << "FontEngine: Cannot load '" << font_path <<
294 "'. Trying to fall back to another path." << std::endl;
298 if (spec.mode != _FM_Fallback) {
299 FontSpec spec2(spec);
300 spec2.mode = _FM_Fallback;
301 font->setFallback(getFont(spec2, true));
306 errorstream << "FontEngine: Tried to load TTF font but Minetest was"
307 " compiled without Freetype." << std::endl;
312 /** initialize a font without freetype */
313 gui::IGUIFont *FontEngine::initSimpleFont(const FontSpec &spec)
315 assert(spec.mode == FM_Simple || spec.mode == FM_SimpleMono);
316 assert(spec.size != FONT_SIZE_UNSPECIFIED);
318 const std::string &font_path = g_settings->get(
319 (spec.mode == FM_SimpleMono) ? "mono_font_path" : "font_path");
321 size_t pos_dot = font_path.find_last_of('.');
322 std::string basename = font_path, ending;
323 if (pos_dot != std::string::npos)
324 ending = lowercase(font_path.substr(pos_dot));
326 if (ending == ".ttf") {
327 errorstream << "FontEngine: Found font \"" << font_path
328 << "\" but freetype is not available." << std::endl;
332 if (ending == ".xml" || ending == ".png")
333 basename = font_path.substr(0, pos_dot);
335 u32 size = std::floor(
336 RenderingEngine::getDisplayDensity() *
337 g_settings->getFloat("gui_scaling") *
340 irr::gui::IGUIFont *font = nullptr;
341 std::string font_extensions[] = { ".png", ".xml" };
343 // Find nearest matching font scale
344 // Does a "zig-zag motion" (positibe/negative), from 0 to MAX_FONT_SIZE_OFFSET
345 for (s32 zoffset = 0; zoffset < MAX_FONT_SIZE_OFFSET * 2; zoffset++) {
346 std::stringstream path;
349 s32 sign = (zoffset & 1) ? -1 : 1;
350 s32 offset = zoffset >> 1;
352 for (const std::string &ext : font_extensions) {
353 path.str(""); // Clear
354 path << basename << "_" << (size + offset * sign) << ext;
356 if (!fs::PathExists(path.str()))
359 font = m_env->getFont(path.str().c_str());
362 verbosestream << "FontEngine: found font: " << path.str() << std::endl;
373 if (fs::PathExists(font_path)) {
374 font = m_env->getFont(font_path.c_str());
376 verbosestream << "FontEngine: found font: " << font_path << std::endl;