]> git.lizzy.rs Git - dragonfireclient.git/blob - src/fontengine.h
Clean up stack after script_get_backtrace (#7854)
[dragonfireclient.git] / src / fontengine.h
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 #pragma once
21
22 #include <map>
23 #include <vector>
24 #include "util/basic_macros.h"
25 #include <IGUIFont.h>
26 #include <IGUISkin.h>
27 #include <IGUIEnvironment.h>
28 #include "settings.h"
29
30 #define FONT_SIZE_UNSPECIFIED 0xFFFFFFFF
31
32 enum FontMode {
33         FM_Standard = 0,
34         FM_Mono,
35         FM_Fallback,
36         FM_Simple,
37         FM_SimpleMono,
38         FM_MaxMode,
39         FM_Unspecified
40 };
41
42 class FontEngine
43 {
44 public:
45
46         FontEngine(Settings* main_settings, gui::IGUIEnvironment* env);
47
48         ~FontEngine();
49
50         /** get Font */
51         irr::gui::IGUIFont* getFont(unsigned int font_size=FONT_SIZE_UNSPECIFIED,
52                         FontMode mode=FM_Unspecified);
53
54         /** get text height for a specific font */
55         unsigned int getTextHeight(unsigned int font_size=FONT_SIZE_UNSPECIFIED,
56                         FontMode mode=FM_Unspecified);
57
58         /** get text width if a text for a specific font */
59         unsigned int getTextWidth(const std::string& text,
60                         unsigned int font_size=FONT_SIZE_UNSPECIFIED,
61                         FontMode mode=FM_Unspecified)
62         {
63                 return getTextWidth(utf8_to_wide(text));
64         }
65
66         /** get text width if a text for a specific font */
67         unsigned int getTextWidth(const std::wstring& text,
68                         unsigned int font_size=FONT_SIZE_UNSPECIFIED,
69                         FontMode mode=FM_Unspecified);
70
71         /** get line height for a specific font (including empty room between lines) */
72         unsigned int getLineHeight(unsigned int font_size=FONT_SIZE_UNSPECIFIED,
73                         FontMode mode=FM_Unspecified);
74
75         /** get default font size */
76         unsigned int getDefaultFontSize();
77
78         /** initialize font engine */
79         void initialize(Settings* main_settings, gui::IGUIEnvironment* env);
80
81         /** update internal parameters from settings */
82         void readSettings();
83
84 private:
85         /** update content of font cache in case of a setting change made it invalid */
86         void updateFontCache();
87
88         /** initialize a new font */
89         void initFont(unsigned int basesize, FontMode mode=FM_Unspecified);
90
91         /** initialize a font without freetype */
92         void initSimpleFont(unsigned int basesize, FontMode mode);
93
94         /** update current minetest skin with font changes */
95         void updateSkin();
96
97         /** clean cache */
98         void cleanCache();
99
100         /** pointer to settings for registering callbacks or reading config */
101         Settings* m_settings = nullptr;
102
103         /** pointer to irrlicht gui environment */
104         gui::IGUIEnvironment* m_env = nullptr;
105
106         /** internal storage for caching fonts of different size */
107         std::map<unsigned int, irr::gui::IGUIFont*> m_font_cache[FM_MaxMode];
108
109         /** default font size to use */
110         unsigned int m_default_size[FM_MaxMode];
111
112         /** current font engine mode */
113         FontMode m_currentMode = FM_Standard;
114
115         /** font mode of last request */
116         FontMode m_lastMode;
117
118         /** size of last request */
119         unsigned int m_lastSize = 0;
120
121         /** last font returned */
122         irr::gui::IGUIFont* m_lastFont = nullptr;
123
124         DISABLE_CLASS_COPY(FontEngine);
125 };
126
127 /** interface to access main font engine*/
128 extern FontEngine* g_fontengine;