Add confirmation on new player registration (#6849)
[oweals/minetest.git] / src / 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 "client/renderingengine.h"
22 #include "config.h"
23 #include "porting.h"
24 #include "filesys.h"
25
26 #if USE_FREETYPE
27 #include "gettext.h"
28 #include "irrlicht_changes/CGUITTFont.h"
29 #endif
30
31 /** maximum size distance for getting a "similar" font size */
32 #define MAX_FONT_SIZE_OFFSET 10
33
34 /** reference to access font engine, has to be initialized by main */
35 FontEngine* g_fontengine = NULL;
36
37 /** callback to be used on change of font size setting */
38 static void font_setting_changed(const std::string &name, void *userdata)
39 {
40         g_fontengine->readSettings();
41 }
42
43 /******************************************************************************/
44 FontEngine::FontEngine(Settings* main_settings, gui::IGUIEnvironment* env) :
45         m_settings(main_settings),
46         m_env(env)
47 {
48
49         for (u32 &i : m_default_size) {
50                 i = (FontMode) FONT_SIZE_UNSPECIFIED;
51         }
52
53         assert(m_settings != NULL); // pre-condition
54         assert(m_env != NULL); // pre-condition
55         assert(m_env->getSkin() != NULL); // pre-condition
56
57         m_currentMode = FM_Simple;
58
59 #if USE_FREETYPE
60         if (g_settings->getBool("freetype")) {
61                 m_default_size[FM_Standard] = m_settings->getU16("font_size");
62                 m_default_size[FM_Fallback] = m_settings->getU16("fallback_font_size");
63                 m_default_size[FM_Mono]     = m_settings->getU16("mono_font_size");
64
65                 if (is_yes(gettext("needs_fallback_font"))) {
66                         m_currentMode = FM_Fallback;
67                 }
68                 else {
69                         m_currentMode = FM_Standard;
70                 }
71         }
72
73         // having freetype but not using it is quite a strange case so we need to do
74         // special handling for it
75         if (m_currentMode == FM_Simple) {
76                 std::stringstream fontsize;
77                 fontsize << DEFAULT_FONT_SIZE;
78                 m_settings->setDefault("font_size", fontsize.str());
79                 m_settings->setDefault("mono_font_size", fontsize.str());
80         }
81 #endif
82
83         m_default_size[FM_Simple]       = m_settings->getU16("font_size");
84         m_default_size[FM_SimpleMono]   = m_settings->getU16("mono_font_size");
85
86         updateSkin();
87
88         if (m_currentMode == FM_Standard) {
89                 m_settings->registerChangedCallback("font_size", font_setting_changed, NULL);
90                 m_settings->registerChangedCallback("font_path", font_setting_changed, NULL);
91                 m_settings->registerChangedCallback("font_shadow", font_setting_changed, NULL);
92                 m_settings->registerChangedCallback("font_shadow_alpha", font_setting_changed, NULL);
93         }
94         else if (m_currentMode == FM_Fallback) {
95                 m_settings->registerChangedCallback("fallback_font_size", font_setting_changed, NULL);
96                 m_settings->registerChangedCallback("fallback_font_path", font_setting_changed, NULL);
97                 m_settings->registerChangedCallback("fallback_font_shadow", font_setting_changed, NULL);
98                 m_settings->registerChangedCallback("fallback_font_shadow_alpha", font_setting_changed, NULL);
99         }
100
101         m_settings->registerChangedCallback("mono_font_path", font_setting_changed, NULL);
102         m_settings->registerChangedCallback("mono_font_size", font_setting_changed, NULL);
103         m_settings->registerChangedCallback("screen_dpi", font_setting_changed, NULL);
104         m_settings->registerChangedCallback("gui_scaling", font_setting_changed, NULL);
105 }
106
107 /******************************************************************************/
108 FontEngine::~FontEngine()
109 {
110         cleanCache();
111 }
112
113 /******************************************************************************/
114 void FontEngine::cleanCache()
115 {
116         for (auto &font_cache_it : m_font_cache) {
117
118                 for (auto &font_it : font_cache_it) {
119                         font_it.second->drop();
120                         font_it.second = NULL;
121                 }
122                 font_cache_it.clear();
123         }
124 }
125
126 /******************************************************************************/
127 irr::gui::IGUIFont* FontEngine::getFont(unsigned int font_size, FontMode mode)
128 {
129         if (mode == FM_Unspecified) {
130                 mode = m_currentMode;
131         }
132         else if ((mode == FM_Mono) && (m_currentMode == FM_Simple)) {
133                 mode = FM_SimpleMono;
134         }
135
136         if (font_size == FONT_SIZE_UNSPECIFIED) {
137                 font_size = m_default_size[mode];
138         }
139
140         if ((font_size == m_lastSize) && (mode == m_lastMode)) {
141                 return m_lastFont;
142         }
143
144         if (m_font_cache[mode].find(font_size) == m_font_cache[mode].end()) {
145                 initFont(font_size, mode);
146         }
147
148         if (m_font_cache[mode].find(font_size) == m_font_cache[mode].end()) {
149                 return NULL;
150         }
151
152         m_lastSize = font_size;
153         m_lastMode = mode;
154         m_lastFont = m_font_cache[mode][font_size];
155
156         return m_font_cache[mode][font_size];
157 }
158
159 /******************************************************************************/
160 unsigned int FontEngine::getTextHeight(unsigned int font_size, FontMode mode)
161 {
162         irr::gui::IGUIFont* font = getFont(font_size, mode);
163
164         // use current skin font as fallback
165         if (font == NULL) {
166                 font = m_env->getSkin()->getFont();
167         }
168         FATAL_ERROR_IF(font == NULL, "Could not get skin font");
169
170         return font->getDimension(L"Some unimportant example String").Height;
171 }
172
173 /******************************************************************************/
174 unsigned int FontEngine::getTextWidth(const std::wstring& text,
175                 unsigned int font_size, FontMode mode)
176 {
177         irr::gui::IGUIFont* font = getFont(font_size, mode);
178
179         // use current skin font as fallback
180         if (font == NULL) {
181                 font = m_env->getSkin()->getFont();
182         }
183         FATAL_ERROR_IF(font == NULL, "Could not get font");
184
185         return font->getDimension(text.c_str()).Width;
186 }
187
188
189 /** get line height for a specific font (including empty room between lines) */
190 unsigned int FontEngine::getLineHeight(unsigned int font_size, FontMode mode)
191 {
192         irr::gui::IGUIFont* font = getFont(font_size, mode);
193
194         // use current skin font as fallback
195         if (font == NULL) {
196                 font = m_env->getSkin()->getFont();
197         }
198         FATAL_ERROR_IF(font == NULL, "Could not get font");
199
200         return font->getDimension(L"Some unimportant example String").Height
201                         + font->getKerningHeight();
202 }
203
204 /******************************************************************************/
205 unsigned int FontEngine::getDefaultFontSize()
206 {
207         return m_default_size[m_currentMode];
208 }
209
210 /******************************************************************************/
211 void FontEngine::readSettings()
212 {
213 #if USE_FREETYPE
214         if (g_settings->getBool("freetype")) {
215                 m_default_size[FM_Standard] = m_settings->getU16("font_size");
216                 m_default_size[FM_Fallback] = m_settings->getU16("fallback_font_size");
217                 m_default_size[FM_Mono]     = m_settings->getU16("mono_font_size");
218
219                 if (is_yes(gettext("needs_fallback_font"))) {
220                         m_currentMode = FM_Fallback;
221                 }
222                 else {
223                         m_currentMode = FM_Standard;
224                 }
225         }
226 #endif
227         m_default_size[FM_Simple]       = m_settings->getU16("font_size");
228         m_default_size[FM_SimpleMono]   = m_settings->getU16("mono_font_size");
229
230         cleanCache();
231         updateFontCache();
232         updateSkin();
233 }
234
235 /******************************************************************************/
236 void FontEngine::updateSkin()
237 {
238         gui::IGUIFont *font = getFont();
239
240         if (font)
241                 m_env->getSkin()->setFont(font);
242         else
243                 errorstream << "FontEngine: Default font file: " <<
244                                 "\n\t\"" << m_settings->get("font_path") << "\"" <<
245                                 "\n\trequired for current screen configuration was not found" <<
246                                 " or was invalid file format." <<
247                                 "\n\tUsing irrlicht default font." << std::endl;
248
249         // If we did fail to create a font our own make irrlicht find a default one
250         font = m_env->getSkin()->getFont();
251         FATAL_ERROR_IF(font == NULL, "Could not create/get font");
252
253         u32 text_height = font->getDimension(L"Hello, world!").Height;
254         infostream << "text_height=" << text_height << std::endl;
255 }
256
257 /******************************************************************************/
258 void FontEngine::updateFontCache()
259 {
260         /* the only font to be initialized is default one,
261          * all others are re-initialized on demand */
262         initFont(m_default_size[m_currentMode], m_currentMode);
263
264         /* reset font quick access */
265         m_lastMode = FM_Unspecified;
266         m_lastSize = 0;
267         m_lastFont = NULL;
268 }
269
270 /******************************************************************************/
271 void FontEngine::initFont(unsigned int basesize, FontMode mode)
272 {
273
274         std::string font_config_prefix;
275
276         if (mode == FM_Unspecified) {
277                 mode = m_currentMode;
278         }
279
280         switch (mode) {
281
282                 case FM_Standard:
283                         font_config_prefix = "";
284                         break;
285
286                 case FM_Fallback:
287                         font_config_prefix = "fallback_";
288                         break;
289
290                 case FM_Mono:
291                         font_config_prefix = "mono_";
292                         if (m_currentMode == FM_Simple)
293                                 mode = FM_SimpleMono;
294                         break;
295
296                 case FM_Simple: /* Fallthrough */
297                 case FM_SimpleMono: /* Fallthrough */
298                 default:
299                         font_config_prefix = "";
300
301         }
302
303         if (m_font_cache[mode].find(basesize) != m_font_cache[mode].end())
304                 return;
305
306         if ((mode == FM_Simple) || (mode == FM_SimpleMono)) {
307                 initSimpleFont(basesize, mode);
308                 return;
309         }
310 #if USE_FREETYPE
311         else {
312                 if (! is_yes(m_settings->get("freetype"))) {
313                         return;
314                 }
315                 unsigned int size = floor(RenderingEngine::getDisplayDensity() *
316                                 m_settings->getFloat("gui_scaling") * basesize);
317                 u32 font_shadow       = 0;
318                 u32 font_shadow_alpha = 0;
319
320                 try {
321                         font_shadow =
322                                         g_settings->getU16(font_config_prefix + "font_shadow");
323                 } catch (SettingNotFoundException&) {}
324                 try {
325                         font_shadow_alpha =
326                                         g_settings->getU16(font_config_prefix + "font_shadow_alpha");
327                 } catch (SettingNotFoundException&) {}
328
329                 std::string font_path = g_settings->get(font_config_prefix + "font_path");
330
331                 irr::gui::IGUIFont* font = gui::CGUITTFont::createTTFont(m_env,
332                                 font_path.c_str(), size, true, true, font_shadow,
333                                 font_shadow_alpha);
334
335                 if (font) {
336                         m_font_cache[mode][basesize] = font;
337                         return;
338                 }
339
340                 if (font_config_prefix == "mono_") {
341                         const std::string &mono_font_path = m_settings->getDefault("mono_font_path");
342
343                         if (font_path != mono_font_path) {
344                                 // try original mono font
345                                 errorstream << "FontEngine: failed to load custom mono "
346                                                 "font: " << font_path << ", trying to fall back to "
347                                                 "original mono font" << std::endl;
348
349                                 font = gui::CGUITTFont::createTTFont(m_env,
350                                         mono_font_path.c_str(), size, true, true,
351                                         font_shadow, font_shadow_alpha);
352
353                                 if (font) {
354                                         m_font_cache[mode][basesize] = font;
355                                         return;
356                                 }
357                         }
358                 } else {
359                         // try fallback font
360                         errorstream << "FontEngine: failed to load: " << font_path <<
361                                         ", trying to fall back to fallback font" << std::endl;
362
363                         font_path = g_settings->get(font_config_prefix + "fallback_font_path");
364
365                         font = gui::CGUITTFont::createTTFont(m_env,
366                                 font_path.c_str(), size, true, true, font_shadow,
367                                 font_shadow_alpha);
368
369                         if (font) {
370                                 m_font_cache[mode][basesize] = font;
371                                 return;
372                         }
373
374                         const std::string &fallback_font_path = m_settings->getDefault("fallback_font_path");
375
376                         if (font_path != fallback_font_path) {
377                                 // try original fallback font
378                                 errorstream << "FontEngine: failed to load custom fallback "
379                                                 "font: " << font_path << ", trying to fall back to "
380                                                 "original fallback font" << std::endl;
381
382                                 font = gui::CGUITTFont::createTTFont(m_env,
383                                         fallback_font_path.c_str(), size, true, true,
384                                         font_shadow, font_shadow_alpha);
385
386                                 if (font) {
387                                         m_font_cache[mode][basesize] = font;
388                                         return;
389                                 }
390                         }
391                 }
392
393                 // give up
394                 errorstream << "FontEngine: failed to load freetype font: "
395                                 << font_path << std::endl;
396                 errorstream << "minetest can not continue without a valid font. "
397                                 "Please correct the 'font_path' setting or install the font "
398                                 "file in the proper location" << std::endl;
399                 abort();
400         }
401 #endif
402 }
403
404 /** initialize a font without freetype */
405 void FontEngine::initSimpleFont(unsigned int basesize, FontMode mode)
406 {
407         assert(mode == FM_Simple || mode == FM_SimpleMono); // pre-condition
408
409         std::string font_path;
410         if (mode == FM_Simple) {
411                 font_path = m_settings->get("font_path");
412         } else {
413                 font_path = m_settings->get("mono_font_path");
414         }
415         std::string basename = font_path;
416         std::string ending = font_path.substr(font_path.length() -4);
417
418         if (ending == ".ttf") {
419                 errorstream << "FontEngine: Not trying to open \"" << font_path
420                                 << "\" which seems to be a truetype font." << std::endl;
421                 return;
422         }
423
424         if ((ending == ".xml") || (ending == ".png")) {
425                 basename = font_path.substr(0,font_path.length()-4);
426         }
427
428         if (basesize == FONT_SIZE_UNSPECIFIED)
429                 basesize = DEFAULT_FONT_SIZE;
430
431         unsigned int size = floor(
432                         RenderingEngine::getDisplayDensity() *
433                         m_settings->getFloat("gui_scaling") *
434                         basesize);
435
436         irr::gui::IGUIFont* font = NULL;
437
438         for(unsigned int offset = 0; offset < MAX_FONT_SIZE_OFFSET; offset++) {
439
440                 // try opening positive offset
441                 std::stringstream fontsize_plus_png;
442                 fontsize_plus_png << basename << "_" << (size + offset) << ".png";
443
444                 if (fs::PathExists(fontsize_plus_png.str())) {
445                         font = m_env->getFont(fontsize_plus_png.str().c_str());
446
447                         if (font) {
448                                 verbosestream << "FontEngine: found font: " << fontsize_plus_png.str() << std::endl;
449                                 break;
450                         }
451                 }
452
453                 std::stringstream fontsize_plus_xml;
454                 fontsize_plus_xml << basename << "_" << (size + offset) << ".xml";
455
456                 if (fs::PathExists(fontsize_plus_xml.str())) {
457                         font = m_env->getFont(fontsize_plus_xml.str().c_str());
458
459                         if (font) {
460                                 verbosestream << "FontEngine: found font: " << fontsize_plus_xml.str() << std::endl;
461                                 break;
462                         }
463                 }
464
465                 // try negative offset
466                 std::stringstream fontsize_minus_png;
467                 fontsize_minus_png << basename << "_" << (size - offset) << ".png";
468
469                 if (fs::PathExists(fontsize_minus_png.str())) {
470                         font = m_env->getFont(fontsize_minus_png.str().c_str());
471
472                         if (font) {
473                                 verbosestream << "FontEngine: found font: " << fontsize_minus_png.str() << std::endl;
474                                 break;
475                         }
476                 }
477
478                 std::stringstream fontsize_minus_xml;
479                 fontsize_minus_xml << basename << "_" << (size - offset) << ".xml";
480
481                 if (fs::PathExists(fontsize_minus_xml.str())) {
482                         font = m_env->getFont(fontsize_minus_xml.str().c_str());
483
484                         if (font) {
485                                 verbosestream << "FontEngine: found font: " << fontsize_minus_xml.str() << std::endl;
486                                 break;
487                         }
488                 }
489         }
490
491         // try name direct
492         if (font == NULL) {
493                 if (fs::PathExists(font_path)) {
494                         font = m_env->getFont(font_path.c_str());
495                         if (font)
496                                 verbosestream << "FontEngine: found font: " << font_path << std::endl;
497                 }
498         }
499
500         if (font) {
501                 font->grab();
502                 m_font_cache[mode][basesize] = font;
503         }
504 }