Sky API: Rename *_tint to fog_*_tint for consistency
[oweals/minetest.git] / src / client / 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 : u8 {
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 struct FontSpec {
43         FontSpec(unsigned int font_size, FontMode mode, bool bold, bool italic) :
44                 size(font_size),
45                 mode(mode),
46                 bold(bold),
47                 italic(italic) {}
48
49         u16 getHash()
50         {
51                 return (mode << 2) | (bold << 1) | italic;
52         }
53
54         unsigned int size;
55         FontMode mode;
56         bool bold;
57         bool italic;
58 };
59
60 class FontEngine
61 {
62 public:
63
64         FontEngine(Settings* main_settings, gui::IGUIEnvironment* env);
65
66         ~FontEngine();
67
68         // Get best possible font specified by FontSpec
69         irr::gui::IGUIFont *getFont(FontSpec spec);
70
71         irr::gui::IGUIFont *getFont(unsigned int font_size=FONT_SIZE_UNSPECIFIED,
72                         FontMode mode=FM_Unspecified)
73         {
74                 FontSpec spec(font_size, mode, m_default_bold, m_default_italic);
75                 return getFont(spec);
76         }
77
78         /** get text height for a specific font */
79         unsigned int getTextHeight(const FontSpec &spec);
80
81         /** get text width if a text for a specific font */
82         unsigned int getTextHeight(
83                         unsigned int font_size=FONT_SIZE_UNSPECIFIED,
84                         FontMode mode=FM_Unspecified)
85         {
86                 FontSpec spec(font_size, mode, m_default_bold, m_default_italic);
87                 return getTextHeight(spec);
88         }
89
90         unsigned int getTextWidth(const std::wstring &text, const FontSpec &spec);
91
92         /** get text width if a text for a specific font */
93         unsigned int getTextWidth(const std::wstring& text,
94                         unsigned int font_size=FONT_SIZE_UNSPECIFIED,
95                         FontMode mode=FM_Unspecified)
96         {
97                 FontSpec spec(font_size, mode, m_default_bold, m_default_italic);
98                 return getTextWidth(text, spec);
99         }
100
101         unsigned int getTextWidth(const std::string &text, const FontSpec &spec)
102         {
103                 return getTextWidth(utf8_to_wide(text), spec);
104         }
105
106         unsigned int getTextWidth(const std::string& text,
107                         unsigned int font_size=FONT_SIZE_UNSPECIFIED,
108                         FontMode mode=FM_Unspecified)
109         {
110                 FontSpec spec(font_size, mode, m_default_bold, m_default_italic);
111                 return getTextWidth(utf8_to_wide(text), spec);
112         }
113
114         /** get line height for a specific font (including empty room between lines) */
115         unsigned int getLineHeight(const FontSpec &spec);
116
117         unsigned int getLineHeight(unsigned int font_size=FONT_SIZE_UNSPECIFIED,
118                         FontMode mode=FM_Unspecified)
119         {
120                 FontSpec spec(font_size, mode, m_default_bold, m_default_italic);
121                 return getLineHeight(spec);
122         }
123
124         /** get default font size */
125         unsigned int getDefaultFontSize();
126
127         /** initialize font engine */
128         void initialize(Settings* main_settings, gui::IGUIEnvironment* env);
129
130         /** update internal parameters from settings */
131         void readSettings();
132
133 private:
134         /** update content of font cache in case of a setting change made it invalid */
135         void updateFontCache();
136
137         /** initialize a new font */
138         gui::IGUIFont *initFont(const FontSpec &spec);
139
140         /** initialize a font without freetype */
141         gui::IGUIFont *initSimpleFont(const FontSpec &spec);
142
143         /** update current minetest skin with font changes */
144         void updateSkin();
145
146         /** clean cache */
147         void cleanCache();
148
149         /** pointer to settings for registering callbacks or reading config */
150         Settings* m_settings = nullptr;
151
152         /** pointer to irrlicht gui environment */
153         gui::IGUIEnvironment* m_env = nullptr;
154
155         /** internal storage for caching fonts of different size */
156         std::map<unsigned int, irr::gui::IGUIFont*> m_font_cache[FM_MaxMode << 2];
157
158         /** default font size to use */
159         unsigned int m_default_size[FM_MaxMode];
160
161         /** default bold and italic */
162         bool m_default_bold = false;
163         bool m_default_italic = false;
164
165         /** current font engine mode */
166         FontMode m_currentMode = FM_Standard;
167
168         DISABLE_CLASS_COPY(FontEngine);
169 };
170
171 /** interface to access main font engine*/
172 extern FontEngine* g_fontengine;