]> git.lizzy.rs Git - minetest.git/blob - src/client/fontengine.h
Server: Ignore whitespace-only chat messages
[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 "irrlichttypes.h"
26 #include <IGUIFont.h>
27 #include <IGUISkin.h>
28 #include <IGUIEnvironment.h>
29 #include "settings.h"
30
31 #define FONT_SIZE_UNSPECIFIED 0xFFFFFFFF
32
33 enum FontMode : u8 {
34         FM_Standard = 0,
35         FM_Mono,
36         _FM_Fallback, // do not use directly
37         FM_Simple,
38         FM_SimpleMono,
39         FM_MaxMode,
40         FM_Unspecified
41 };
42
43 struct FontSpec {
44         FontSpec(unsigned int font_size, FontMode mode, bool bold, bool italic) :
45                 size(font_size),
46                 mode(mode),
47                 bold(bold),
48                 italic(italic) {}
49
50         u16 getHash() const
51         {
52                 return (mode << 2) | (static_cast<u8>(bold) << 1) | static_cast<u8>(italic);
53         }
54
55         unsigned int size;
56         FontMode mode;
57         bool bold;
58         bool italic;
59 };
60
61 class FontEngine
62 {
63 public:
64
65         FontEngine(gui::IGUIEnvironment* env);
66
67         ~FontEngine();
68
69         // Get best possible font specified by FontSpec
70         irr::gui::IGUIFont *getFont(FontSpec spec);
71
72         irr::gui::IGUIFont *getFont(unsigned int font_size=FONT_SIZE_UNSPECIFIED,
73                         FontMode mode=FM_Unspecified)
74         {
75                 FontSpec spec(font_size, mode, m_default_bold, m_default_italic);
76                 return getFont(spec);
77         }
78
79         /** get text height for a specific font */
80         unsigned int getTextHeight(const FontSpec &spec);
81
82         /** get text width if a text for a specific font */
83         unsigned int getTextHeight(
84                         unsigned int font_size=FONT_SIZE_UNSPECIFIED,
85                         FontMode mode=FM_Unspecified)
86         {
87                 FontSpec spec(font_size, mode, m_default_bold, m_default_italic);
88                 return getTextHeight(spec);
89         }
90
91         unsigned int getTextWidth(const std::wstring &text, const FontSpec &spec);
92
93         /** get text width if a text for a specific font */
94         unsigned int getTextWidth(const std::wstring& text,
95                         unsigned int font_size=FONT_SIZE_UNSPECIFIED,
96                         FontMode mode=FM_Unspecified)
97         {
98                 FontSpec spec(font_size, mode, m_default_bold, m_default_italic);
99                 return getTextWidth(text, spec);
100         }
101
102         unsigned int getTextWidth(const std::string &text, const FontSpec &spec)
103         {
104                 return getTextWidth(utf8_to_wide(text), spec);
105         }
106
107         unsigned int getTextWidth(const std::string& text,
108                         unsigned int font_size=FONT_SIZE_UNSPECIFIED,
109                         FontMode mode=FM_Unspecified)
110         {
111                 FontSpec spec(font_size, mode, m_default_bold, m_default_italic);
112                 return getTextWidth(utf8_to_wide(text), spec);
113         }
114
115         /** get line height for a specific font (including empty room between lines) */
116         unsigned int getLineHeight(const FontSpec &spec);
117
118         unsigned int getLineHeight(unsigned int font_size=FONT_SIZE_UNSPECIFIED,
119                         FontMode mode=FM_Unspecified)
120         {
121                 FontSpec spec(font_size, mode, m_default_bold, m_default_italic);
122                 return getLineHeight(spec);
123         }
124
125         /** get default font size */
126         unsigned int getDefaultFontSize();
127
128         /** get font size for a specific mode */
129         unsigned int getFontSize(FontMode mode);
130
131         /** update internal parameters from settings */
132         void readSettings();
133
134 private:
135         irr::gui::IGUIFont *getFont(FontSpec spec, bool may_fail);
136
137         /** update content of font cache in case of a setting change made it invalid */
138         void updateFontCache();
139
140         /** initialize a new TTF font */
141         gui::IGUIFont *initFont(const FontSpec &spec);
142
143         /** initialize a font without freetype */
144         gui::IGUIFont *initSimpleFont(const FontSpec &spec);
145
146         /** update current minetest skin with font changes */
147         void updateSkin();
148
149         /** clean cache */
150         void cleanCache();
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;