]> git.lizzy.rs Git - minetest.git/blob - src/client/fontengine.cpp
Check for falling `float` nodes in liquid transform (#12862)
[minetest.git] / src / client / fontengine.cpp
1 /*
2 Minetest
3 Copyright (C) 2010-2014 sapier <sapier at gmx dot net>
4
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.
9
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.
14
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.
18 */
19
20 #include "fontengine.h"
21 #include <cmath>
22 #include "client/renderingengine.h"
23 #include "config.h"
24 #include "porting.h"
25 #include "filesys.h"
26 #include "gettext.h"
27 #include "irrlicht_changes/CGUITTFont.h"
28 #include "util/numeric.h" // rangelim
29
30 /** maximum size distance for getting a "similar" font size */
31 #define MAX_FONT_SIZE_OFFSET 10
32
33 /** reference to access font engine, has to be initialized by main */
34 FontEngine* g_fontengine = NULL;
35
36 /** callback to be used on change of font size setting */
37 static void font_setting_changed(const std::string &name, void *userdata)
38 {
39         g_fontengine->readSettings();
40 }
41
42 /******************************************************************************/
43 FontEngine::FontEngine(gui::IGUIEnvironment* env) :
44         m_env(env)
45 {
46         for (u32 &i : m_default_size) {
47                 i = FONT_SIZE_UNSPECIFIED;
48         }
49
50         assert(g_settings != NULL); // pre-condition
51         assert(m_env != NULL); // pre-condition
52         assert(m_env->getSkin() != NULL); // pre-condition
53
54         readSettings();
55
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",
63                 "fallback_font_path",
64                 "screen_dpi", "gui_scaling",
65         };
66
67         for (auto name : settings)
68                 g_settings->registerChangedCallback(name, font_setting_changed, NULL);
69 }
70
71 /******************************************************************************/
72 FontEngine::~FontEngine()
73 {
74         cleanCache();
75 }
76
77 /******************************************************************************/
78 void FontEngine::cleanCache()
79 {
80         RecursiveMutexAutoLock l(m_font_mutex);
81
82         for (auto &font_cache_it : m_font_cache) {
83
84                 for (auto &font_it : font_cache_it) {
85                         font_it.second->drop();
86                         font_it.second = nullptr;
87                 }
88                 font_cache_it.clear();
89         }
90 }
91
92 /******************************************************************************/
93 irr::gui::IGUIFont *FontEngine::getFont(FontSpec spec)
94 {
95         return getFont(spec, false);
96 }
97
98 irr::gui::IGUIFont *FontEngine::getFont(FontSpec spec, bool may_fail)
99 {
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
104                 spec.bold = false;
105                 spec.italic = false;
106         }
107
108         // Fallback to default size
109         if (spec.size == FONT_SIZE_UNSPECIFIED)
110                 spec.size = m_default_size[spec.mode];
111
112         RecursiveMutexAutoLock l(m_font_mutex);
113
114         const auto &cache = m_font_cache[spec.getHash()];
115         auto it = cache.find(spec.size);
116         if (it != cache.end())
117                 return it->second;
118
119         // Font does not yet exist
120         gui::IGUIFont *font = initFont(spec);
121
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;
126                 abort();
127         }
128
129         m_font_cache[spec.getHash()][spec.size] = font;
130
131         return font;
132 }
133
134 /******************************************************************************/
135 unsigned int FontEngine::getTextHeight(const FontSpec &spec)
136 {
137         gui::IGUIFont *font = getFont(spec);
138
139         return font->getDimension(L"Some unimportant example String").Height;
140 }
141
142 /******************************************************************************/
143 unsigned int FontEngine::getTextWidth(const std::wstring &text, const FontSpec &spec)
144 {
145         gui::IGUIFont *font = getFont(spec);
146
147         return font->getDimension(text.c_str()).Width;
148 }
149
150 /** get line height for a specific font (including empty room between lines) */
151 unsigned int FontEngine::getLineHeight(const FontSpec &spec)
152 {
153         gui::IGUIFont *font = getFont(spec);
154
155         return font->getDimension(L"Some unimportant example String").Height
156                         + font->getKerningHeight();
157 }
158
159 /******************************************************************************/
160 unsigned int FontEngine::getDefaultFontSize()
161 {
162         return m_default_size[m_currentMode];
163 }
164
165 unsigned int FontEngine::getFontSize(FontMode mode)
166 {
167         if (mode == FM_Unspecified)
168                 return m_default_size[FM_Standard];
169
170         return m_default_size[mode];
171 }
172
173 /******************************************************************************/
174 void FontEngine::readSettings()
175 {
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);
179
180         m_default_bold = g_settings->getBool("font_bold");
181         m_default_italic = g_settings->getBool("font_italic");
182
183         cleanCache();
184         updateFontCache();
185         updateSkin();
186 }
187
188 /******************************************************************************/
189 void FontEngine::updateSkin()
190 {
191         gui::IGUIFont *font = getFont();
192         assert(font);
193
194         m_env->getSkin()->setFont(font);
195 }
196
197 /******************************************************************************/
198 void FontEngine::updateFontCache()
199 {
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);
203 }
204
205 /******************************************************************************/
206 gui::IGUIFont *FontEngine::initFont(const FontSpec &spec)
207 {
208         assert(spec.mode != FM_Unspecified);
209         assert(spec.size != FONT_SIZE_UNSPECIFIED);
210
211         std::string setting_prefix = "";
212         if (spec.mode == FM_Mono)
213                 setting_prefix = "mono_";
214
215         std::string setting_suffix = "";
216         if (spec.bold)
217                 setting_suffix.append("_bold");
218         if (spec.italic)
219                 setting_suffix.append("_italic");
220
221         // Font size in pixels for FreeType
222         u32 size = rangelim(spec.size * RenderingEngine::getDisplayDensity() *
223                         g_settings->getFloat("gui_scaling"), 1U, 500U);
224
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);
230         }
231
232         sanity_check(size != 0);
233
234         u16 font_shadow       = 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",
238                         font_shadow_alpha);
239
240         std::string path_setting;
241         if (spec.mode == _FM_Fallback)
242                 path_setting = "fallback_font_path";
243         else
244                 path_setting = setting_prefix + "font_path" + setting_suffix;
245
246         std::string fallback_settings[] = {
247                 g_settings->get(path_setting),
248                 Settings::getLayer(SL_DEFAULTS)->get(path_setting)
249         };
250
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,
254                                 font_shadow_alpha);
255
256                 if (!font) {
257                         errorstream << "FontEngine: Cannot load '" << font_path <<
258                                 "'. Trying to fall back to another path." << std::endl;
259                         continue;
260                 }
261
262                 if (spec.mode != _FM_Fallback) {
263                         FontSpec spec2(spec);
264                         spec2.mode = _FM_Fallback;
265                         font->setFallback(getFont(spec2, true));
266                 }
267                 return font;
268         }
269         return nullptr;
270 }