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