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"
27 #include "irrlicht_changes/CGUITTFont.h"
28 #include "util/numeric.h" // rangelim
30 /** maximum size distance for getting a "similar" font size */
31 #define MAX_FONT_SIZE_OFFSET 10
33 /** reference to access font engine, has to be initialized by main */
34 FontEngine* g_fontengine = NULL;
36 /** callback to be used on change of font size setting */
37 static void font_setting_changed(const std::string &name, void *userdata)
39 g_fontengine->readSettings();
42 /******************************************************************************/
43 FontEngine::FontEngine(gui::IGUIEnvironment* env) :
46 for (u32 &i : m_default_size) {
47 i = FONT_SIZE_UNSPECIFIED;
50 assert(g_settings != NULL); // pre-condition
51 assert(m_env != NULL); // pre-condition
52 assert(m_env->getSkin() != NULL); // pre-condition
56 const char *settings[] = {
57 "font_size", "font_bold", "font_italic", "font_size_divisible_by",
58 "mono_font_size", "mono_font_size_divisible_by",
59 "font_shadow", "font_shadow_alpha",
60 "font_path", "font_path_bold", "font_path_italic", "font_path_bold_italic",
61 "mono_font_path", "mono_font_path_bold", "mono_font_path_italic",
62 "mono_font_path_bold_italic",
64 "screen_dpi", "gui_scaling",
67 for (auto name : settings)
68 g_settings->registerChangedCallback(name, font_setting_changed, NULL);
71 /******************************************************************************/
72 FontEngine::~FontEngine()
77 /******************************************************************************/
78 void FontEngine::cleanCache()
80 RecursiveMutexAutoLock l(m_font_mutex);
82 for (auto &font_cache_it : m_font_cache) {
84 for (auto &font_it : font_cache_it) {
85 font_it.second->drop();
86 font_it.second = nullptr;
88 font_cache_it.clear();
92 /******************************************************************************/
93 irr::gui::IGUIFont *FontEngine::getFont(FontSpec spec)
95 return getFont(spec, false);
98 irr::gui::IGUIFont *FontEngine::getFont(FontSpec spec, bool may_fail)
100 if (spec.mode == FM_Unspecified) {
101 spec.mode = m_currentMode;
102 } else if (spec.mode == _FM_Fallback) {
103 // Fallback font doesn't support these
108 // Fallback to default size
109 if (spec.size == FONT_SIZE_UNSPECIFIED)
110 spec.size = m_default_size[spec.mode];
112 RecursiveMutexAutoLock l(m_font_mutex);
114 const auto &cache = m_font_cache[spec.getHash()];
115 auto it = cache.find(spec.size);
116 if (it != cache.end())
119 // Font does not yet exist
120 gui::IGUIFont *font = initFont(spec);
122 if (!font && !may_fail) {
123 errorstream << "Minetest cannot continue without a valid font. "
124 "Please correct the 'font_path' setting or install the font "
125 "file in the proper location." << std::endl;
129 m_font_cache[spec.getHash()][spec.size] = font;
134 /******************************************************************************/
135 unsigned int FontEngine::getTextHeight(const FontSpec &spec)
137 gui::IGUIFont *font = getFont(spec);
139 return font->getDimension(L"Some unimportant example String").Height;
142 /******************************************************************************/
143 unsigned int FontEngine::getTextWidth(const std::wstring &text, const FontSpec &spec)
145 gui::IGUIFont *font = getFont(spec);
147 return font->getDimension(text.c_str()).Width;
150 /** get line height for a specific font (including empty room between lines) */
151 unsigned int FontEngine::getLineHeight(const FontSpec &spec)
153 gui::IGUIFont *font = getFont(spec);
155 return font->getDimension(L"Some unimportant example String").Height
156 + font->getKerningHeight();
159 /******************************************************************************/
160 unsigned int FontEngine::getDefaultFontSize()
162 return m_default_size[m_currentMode];
165 unsigned int FontEngine::getFontSize(FontMode mode)
167 if (mode == FM_Unspecified)
168 return m_default_size[FM_Standard];
170 return m_default_size[mode];
173 /******************************************************************************/
174 void FontEngine::readSettings()
176 m_default_size[FM_Standard] = rangelim(g_settings->getU16("font_size"), 5, 72);
177 m_default_size[_FM_Fallback] = m_default_size[FM_Standard];
178 m_default_size[FM_Mono] = rangelim(g_settings->getU16("mono_font_size"), 5, 72);
180 m_default_bold = g_settings->getBool("font_bold");
181 m_default_italic = g_settings->getBool("font_italic");
188 /******************************************************************************/
189 void FontEngine::updateSkin()
191 gui::IGUIFont *font = getFont();
194 m_env->getSkin()->setFont(font);
197 /******************************************************************************/
198 void FontEngine::updateFontCache()
200 /* the only font to be initialized is default one,
201 * all others are re-initialized on demand */
202 getFont(FONT_SIZE_UNSPECIFIED, FM_Unspecified);
205 /******************************************************************************/
206 gui::IGUIFont *FontEngine::initFont(const FontSpec &spec)
208 assert(spec.mode != FM_Unspecified);
209 assert(spec.size != FONT_SIZE_UNSPECIFIED);
211 std::string setting_prefix = "";
212 if (spec.mode == FM_Mono)
213 setting_prefix = "mono_";
215 std::string setting_suffix = "";
217 setting_suffix.append("_bold");
219 setting_suffix.append("_italic");
221 // Font size in pixels for FreeType
222 u32 size = rangelim(spec.size * RenderingEngine::getDisplayDensity() *
223 g_settings->getFloat("gui_scaling"), 1U, 500U);
225 // Constrain the font size to a certain multiple, if necessary
226 u16 divisible_by = g_settings->getU16(setting_prefix + "font_size_divisible_by");
227 if (divisible_by > 1) {
228 size = std::max<u32>(
229 std::round((double)size / divisible_by) * divisible_by, divisible_by);
232 sanity_check(size != 0);
235 u16 font_shadow_alpha = 0;
236 g_settings->getU16NoEx(setting_prefix + "font_shadow", font_shadow);
237 g_settings->getU16NoEx(setting_prefix + "font_shadow_alpha",
240 std::string path_setting;
241 if (spec.mode == _FM_Fallback)
242 path_setting = "fallback_font_path";
244 path_setting = setting_prefix + "font_path" + setting_suffix;
246 std::string fallback_settings[] = {
247 g_settings->get(path_setting),
248 Settings::getLayer(SL_DEFAULTS)->get(path_setting)
251 for (const std::string &font_path : fallback_settings) {
252 gui::CGUITTFont *font = gui::CGUITTFont::createTTFont(m_env,
253 font_path.c_str(), size, true, true, font_shadow,
257 errorstream << "FontEngine: Cannot load '" << font_path <<
258 "'. Trying to fall back to another path." << std::endl;
262 if (spec.mode != _FM_Fallback) {
263 FontSpec spec2(spec);
264 spec2.mode = _FM_Fallback;
265 font->setFallback(getFont(spec2, true));