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