3 Copyright (C) 2010-2014 sapier <sapier at gmx dot net>
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.
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.
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.
23 #include "util/basic_macros.h"
24 #include "irrlichttypes.h"
27 #include <IGUIEnvironment.h>
29 #include "threading/mutex_auto_lock.h"
31 #define FONT_SIZE_UNSPECIFIED 0xFFFFFFFF
36 _FM_Fallback, // do not use directly
42 FontSpec(unsigned int font_size, FontMode mode, bool bold, bool italic) :
50 return (mode << 2) | (static_cast<u8>(bold) << 1) | static_cast<u8>(italic);
63 FontEngine(gui::IGUIEnvironment* env);
67 // Get best possible font specified by FontSpec
68 irr::gui::IGUIFont *getFont(FontSpec spec);
70 irr::gui::IGUIFont *getFont(unsigned int font_size=FONT_SIZE_UNSPECIFIED,
71 FontMode mode=FM_Unspecified)
73 FontSpec spec(font_size, mode, m_default_bold, m_default_italic);
77 /** get text height for a specific font */
78 unsigned int getTextHeight(const FontSpec &spec);
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)
85 FontSpec spec(font_size, mode, m_default_bold, m_default_italic);
86 return getTextHeight(spec);
89 unsigned int getTextWidth(const std::wstring &text, const FontSpec &spec);
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)
96 FontSpec spec(font_size, mode, m_default_bold, m_default_italic);
97 return getTextWidth(text, spec);
100 unsigned int getTextWidth(const std::string &text, const FontSpec &spec)
102 return getTextWidth(utf8_to_wide(text), spec);
105 unsigned int getTextWidth(const std::string& text,
106 unsigned int font_size=FONT_SIZE_UNSPECIFIED,
107 FontMode mode=FM_Unspecified)
109 FontSpec spec(font_size, mode, m_default_bold, m_default_italic);
110 return getTextWidth(utf8_to_wide(text), spec);
113 /** get line height for a specific font (including empty room between lines) */
114 unsigned int getLineHeight(const FontSpec &spec);
116 unsigned int getLineHeight(unsigned int font_size=FONT_SIZE_UNSPECIFIED,
117 FontMode mode=FM_Unspecified)
119 FontSpec spec(font_size, mode, m_default_bold, m_default_italic);
120 return getLineHeight(spec);
123 /** get default font size */
124 unsigned int getDefaultFontSize();
126 /** get font size for a specific mode */
127 unsigned int getFontSize(FontMode mode);
129 /** update internal parameters from settings */
133 irr::gui::IGUIFont *getFont(FontSpec spec, bool may_fail);
135 /** update content of font cache in case of a setting change made it invalid */
136 void updateFontCache();
138 /** initialize a new TTF font */
139 gui::IGUIFont *initFont(const FontSpec &spec);
141 /** update current minetest skin with font changes */
147 /** pointer to irrlicht gui environment */
148 gui::IGUIEnvironment* m_env = nullptr;
150 /** mutex used to protect font init and cache */
151 std::recursive_mutex m_font_mutex;
153 /** internal storage for caching fonts of different size */
154 std::map<unsigned int, irr::gui::IGUIFont*> m_font_cache[FM_MaxMode << 2];
156 /** default font size to use */
157 unsigned int m_default_size[FM_MaxMode];
159 /** default bold and italic */
160 bool m_default_bold = false;
161 bool m_default_italic = false;
163 /** default font engine mode (fixed) */
164 static const FontMode m_currentMode = FM_Standard;
166 DISABLE_CLASS_COPY(FontEngine);
169 /** interface to access main font engine*/
170 extern FontEngine* g_fontengine;