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"
29 /** maximum size distance for getting a "similar" font size */
30 #define MAX_FONT_SIZE_OFFSET 10
32 /** reference to access font engine, has to be initialized by main */
33 FontEngine* g_fontengine = NULL;
35 /** callback to be used on change of font size setting */
36 static void font_setting_changed(const std::string &name, void *userdata)
38 g_fontengine->readSettings();
41 /******************************************************************************/
42 FontEngine::FontEngine(gui::IGUIEnvironment* env) :
45 for (u32 &i : m_default_size) {
46 i = FONT_SIZE_UNSPECIFIED;
49 assert(g_settings != NULL); // pre-condition
50 assert(m_env != NULL); // pre-condition
51 assert(m_env->getSkin() != NULL); // pre-condition
55 const char *settings[] = {
56 "font_size", "font_bold", "font_italic", "font_size_divisible_by",
57 "mono_font_size", "mono_font_size_divisible_by",
58 "font_shadow", "font_shadow_alpha",
59 "font_path", "font_path_bold", "font_path_italic", "font_path_bold_italic",
60 "mono_font_path", "mono_font_path_bold", "mono_font_path_italic",
61 "mono_font_path_bold_italic",
63 "screen_dpi", "gui_scaling",
66 for (auto name : settings)
67 g_settings->registerChangedCallback(name, font_setting_changed, NULL);
70 /******************************************************************************/
71 FontEngine::~FontEngine()
76 /******************************************************************************/
77 void FontEngine::cleanCache()
79 RecursiveMutexAutoLock l(m_font_mutex);
81 for (auto &font_cache_it : m_font_cache) {
83 for (auto &font_it : font_cache_it) {
84 font_it.second->drop();
85 font_it.second = nullptr;
87 font_cache_it.clear();
91 /******************************************************************************/
92 irr::gui::IGUIFont *FontEngine::getFont(FontSpec spec)
94 return getFont(spec, false);
97 irr::gui::IGUIFont *FontEngine::getFont(FontSpec spec, bool may_fail)
99 if (spec.mode == FM_Unspecified) {
100 spec.mode = m_currentMode;
101 } else if (spec.mode == _FM_Fallback) {
102 // Fallback font doesn't support these
107 // Fallback to default size
108 if (spec.size == FONT_SIZE_UNSPECIFIED)
109 spec.size = m_default_size[spec.mode];
111 RecursiveMutexAutoLock l(m_font_mutex);
113 const auto &cache = m_font_cache[spec.getHash()];
114 auto it = cache.find(spec.size);
115 if (it != cache.end())
118 // Font does not yet exist
119 gui::IGUIFont *font = initFont(spec);
121 if (!font && !may_fail) {
122 errorstream << "Minetest cannot continue without a valid font. "
123 "Please correct the 'font_path' setting or install the font "
124 "file in the proper location." << std::endl;
128 m_font_cache[spec.getHash()][spec.size] = font;
133 /******************************************************************************/
134 unsigned int FontEngine::getTextHeight(const FontSpec &spec)
136 gui::IGUIFont *font = getFont(spec);
138 return font->getDimension(L"Some unimportant example String").Height;
141 /******************************************************************************/
142 unsigned int FontEngine::getTextWidth(const std::wstring &text, const FontSpec &spec)
144 gui::IGUIFont *font = getFont(spec);
146 return font->getDimension(text.c_str()).Width;
149 /** get line height for a specific font (including empty room between lines) */
150 unsigned int FontEngine::getLineHeight(const FontSpec &spec)
152 gui::IGUIFont *font = getFont(spec);
154 return font->getDimension(L"Some unimportant example String").Height
155 + font->getKerningHeight();
158 /******************************************************************************/
159 unsigned int FontEngine::getDefaultFontSize()
161 return m_default_size[m_currentMode];
164 unsigned int FontEngine::getFontSize(FontMode mode)
166 if (mode == FM_Unspecified)
167 return m_default_size[FM_Standard];
169 return m_default_size[mode];
172 /******************************************************************************/
173 void FontEngine::readSettings()
175 m_default_size[FM_Standard] = g_settings->getU16("font_size");
176 m_default_size[_FM_Fallback] = g_settings->getU16("font_size");
177 m_default_size[FM_Mono] = g_settings->getU16("mono_font_size");
179 m_default_bold = g_settings->getBool("font_bold");
180 m_default_italic = g_settings->getBool("font_italic");
187 /******************************************************************************/
188 void FontEngine::updateSkin()
190 gui::IGUIFont *font = getFont();
193 m_env->getSkin()->setFont(font);
196 /******************************************************************************/
197 void FontEngine::updateFontCache()
199 /* the only font to be initialized is default one,
200 * all others are re-initialized on demand */
201 getFont(FONT_SIZE_UNSPECIFIED, FM_Unspecified);
204 /******************************************************************************/
205 gui::IGUIFont *FontEngine::initFont(const FontSpec &spec)
207 assert(spec.mode != FM_Unspecified);
208 assert(spec.size != FONT_SIZE_UNSPECIFIED);
210 std::string setting_prefix = "";
211 if (spec.mode == FM_Mono)
212 setting_prefix = "mono_";
214 std::string setting_suffix = "";
216 setting_suffix.append("_bold");
218 setting_suffix.append("_italic");
220 u32 size = std::max<u32>(spec.size * RenderingEngine::getDisplayDensity() *
221 g_settings->getFloat("gui_scaling"), 1);
223 // Constrain the font size to a certain multiple, if necessary
224 u16 divisible_by = g_settings->getU16(setting_prefix + "font_size_divisible_by");
225 if (divisible_by > 1) {
226 size = std::max<u32>(
227 std::round((double)size / divisible_by) * divisible_by, divisible_by);
230 sanity_check(size != 0);
233 u16 font_shadow_alpha = 0;
234 g_settings->getU16NoEx(setting_prefix + "font_shadow", font_shadow);
235 g_settings->getU16NoEx(setting_prefix + "font_shadow_alpha",
238 std::string path_setting;
239 if (spec.mode == _FM_Fallback)
240 path_setting = "fallback_font_path";
242 path_setting = setting_prefix + "font_path" + setting_suffix;
244 std::string fallback_settings[] = {
245 g_settings->get(path_setting),
246 Settings::getLayer(SL_DEFAULTS)->get(path_setting)
249 for (const std::string &font_path : fallback_settings) {
250 gui::CGUITTFont *font = gui::CGUITTFont::createTTFont(m_env,
251 font_path.c_str(), size, true, true, font_shadow,
255 errorstream << "FontEngine: Cannot load '" << font_path <<
256 "'. Trying to fall back to another path." << std::endl;
260 if (spec.mode != _FM_Fallback) {
261 FontSpec spec2(spec);
262 spec2.mode = _FM_Fallback;
263 font->setFallback(getFont(spec2, true));