Luanti 5.11.0-dev
 
Loading...
Searching...
No Matches
fontengine.h
Go to the documentation of this file.
1// Luanti
2// SPDX-License-Identifier: LGPL-2.1-or-later
3// Copyright (C) 2010-2014 sapier <sapier at gmx dot net>
4
5#pragma once
6
7#include <map>
8#include "util/basic_macros.h"
9#include "irrlichttypes.h"
10#include "irrString.h" // utf8_to_wide
12
13namespace irr {
14 namespace gui {
15 class IGUIEnvironment;
16 class IGUIFont;
17 }
18}
19
20#define FONT_SIZE_UNSPECIFIED 0xFFFFFFFF
21
22enum FontMode : u8 {
25 _FM_Fallback, // do not use directly
28};
29
30struct FontSpec {
31 FontSpec(unsigned int font_size, FontMode mode, bool bold, bool italic) :
32 size(font_size),
33 mode(mode),
34 bold(bold),
35 italic(italic) {}
36
37 u16 getHash() const
38 {
39 return (mode << 2) | (static_cast<u8>(bold) << 1) | static_cast<u8>(italic);
40 }
41
42 unsigned int size;
44 bool bold;
45 bool italic;
46};
47
49{
50public:
51
52 FontEngine(gui::IGUIEnvironment* env);
53
55
56 // Get best possible font specified by FontSpec
57 irr::gui::IGUIFont *getFont(FontSpec spec);
58
59 irr::gui::IGUIFont *getFont(unsigned int font_size=FONT_SIZE_UNSPECIFIED,
61 {
62 FontSpec spec(font_size, mode, m_default_bold, m_default_italic);
63 return getFont(spec);
64 }
65
67 unsigned int getTextHeight(const FontSpec &spec);
68
70 unsigned int getTextHeight(
71 unsigned int font_size=FONT_SIZE_UNSPECIFIED,
73 {
74 FontSpec spec(font_size, mode, m_default_bold, m_default_italic);
75 return getTextHeight(spec);
76 }
77
78 unsigned int getTextWidth(const std::wstring &text, const FontSpec &spec);
79
81 unsigned int getTextWidth(const std::wstring& text,
82 unsigned int font_size=FONT_SIZE_UNSPECIFIED,
84 {
85 FontSpec spec(font_size, mode, m_default_bold, m_default_italic);
86 return getTextWidth(text, spec);
87 }
88
89 unsigned int getTextWidth(const std::string &text, const FontSpec &spec)
90 {
91 return getTextWidth(utf8_to_wide(text), spec);
92 }
93
94 unsigned int getTextWidth(const std::string& text,
95 unsigned int font_size=FONT_SIZE_UNSPECIFIED,
97 {
98 FontSpec spec(font_size, mode, m_default_bold, m_default_italic);
99 return getTextWidth(utf8_to_wide(text), spec);
100 }
101
103 unsigned int getLineHeight(const FontSpec &spec);
104
105 unsigned int getLineHeight(unsigned int font_size=FONT_SIZE_UNSPECIFIED,
107 {
108 FontSpec spec(font_size, mode, m_default_bold, m_default_italic);
109 return getLineHeight(spec);
110 }
111
113 unsigned int getDefaultFontSize();
114
116 unsigned int getFontSize(FontMode mode);
117
119 void readSettings();
120
121private:
122 irr::gui::IGUIFont *getFont(FontSpec spec, bool may_fail);
123
125 void updateFontCache();
126
128 gui::IGUIFont *initFont(const FontSpec &spec);
129
131 void updateSkin();
132
134 void cleanCache();
135
137 gui::IGUIEnvironment* m_env = nullptr;
138
140 std::recursive_mutex m_font_mutex;
141
143 std::map<unsigned int, irr::gui::IGUIFont*> m_font_cache[FM_MaxMode << 2];
144
147
149 bool m_default_bold = false;
150 bool m_default_italic = false;
151
154
156};
157
Definition fontengine.h:49
static const FontMode m_currentMode
default font engine mode (fixed)
Definition fontengine.h:153
std::recursive_mutex m_font_mutex
mutex used to protect font init and cache
Definition fontengine.h:140
unsigned int getTextHeight(unsigned int font_size=FONT_SIZE_UNSPECIFIED, FontMode mode=FM_Unspecified)
get text width if a text for a specific font
Definition fontengine.h:70
irr::gui::IGUIFont * getFont(unsigned int font_size=FONT_SIZE_UNSPECIFIED, FontMode mode=FM_Unspecified)
Definition fontengine.h:59
gui::IGUIFont * initFont(const FontSpec &spec)
initialize a new TTF font
Definition fontengine.cpp:193
bool m_default_italic
Definition fontengine.h:150
unsigned int getTextHeight(const FontSpec &spec)
get text height for a specific font
Definition fontengine.cpp:122
~FontEngine()
Definition fontengine.cpp:57
gui::IGUIEnvironment * m_env
pointer to irrlicht gui environment
Definition fontengine.h:137
std::map< unsigned int, irr::gui::IGUIFont * > m_font_cache[FM_MaxMode<< 2]
internal storage for caching fonts of different size
Definition fontengine.h:143
bool m_default_bold
default bold and italic
Definition fontengine.h:149
void updateSkin()
update current minetest skin with font changes
Definition fontengine.cpp:176
unsigned int getFontSize(FontMode mode)
get font size for a specific mode
Definition fontengine.cpp:152
FontEngine(gui::IGUIEnvironment *env)
Definition fontengine.cpp:39
unsigned int getTextWidth(const std::wstring &text, unsigned int font_size=FONT_SIZE_UNSPECIFIED, FontMode mode=FM_Unspecified)
get text width if a text for a specific font
Definition fontengine.h:81
unsigned int getTextWidth(const std::wstring &text, const FontSpec &spec)
Definition fontengine.cpp:130
unsigned int getTextWidth(const std::string &text, unsigned int font_size=FONT_SIZE_UNSPECIFIED, FontMode mode=FM_Unspecified)
Definition fontengine.h:94
unsigned int m_default_size[FM_MaxMode]
default font size to use
Definition fontengine.h:146
irr::gui::IGUIFont * getFont(FontSpec spec)
Definition fontengine.cpp:80
unsigned int getTextWidth(const std::string &text, const FontSpec &spec)
Definition fontengine.h:89
void readSettings()
update internal parameters from settings
Definition fontengine.cpp:161
DISABLE_CLASS_COPY(FontEngine)
unsigned int getDefaultFontSize()
get default font size
Definition fontengine.cpp:147
unsigned int getLineHeight(unsigned int font_size=FONT_SIZE_UNSPECIFIED, FontMode mode=FM_Unspecified)
Definition fontengine.h:105
unsigned int getLineHeight(const FontSpec &spec)
get line height for a specific font (including empty room between lines)
Definition fontengine.cpp:138
void updateFontCache()
update content of font cache in case of a setting change made it invalid
Definition fontengine.cpp:185
void cleanCache()
clean cache
Definition fontengine.cpp:65
FontMode
Definition fontengine.h:22
@ FM_Standard
Definition fontengine.h:23
@ FM_MaxMode
Definition fontengine.h:26
@ FM_Unspecified
Definition fontengine.h:27
@ FM_Mono
Definition fontengine.h:24
@ _FM_Fallback
Definition fontengine.h:25
FontEngine * g_fontengine
interface to access main font engine
Definition fontengine.cpp:19
#define FONT_SIZE_UNSPECIFIED
Definition fontengine.h:20
Definition clientmap.h:30
std::wstring utf8_to_wide(std::string_view input)
Definition string.cpp:87
Definition fontengine.h:30
bool italic
Definition fontengine.h:45
bool bold
Definition fontengine.h:44
FontSpec(unsigned int font_size, FontMode mode, bool bold, bool italic)
Definition fontengine.h:31
unsigned int size
Definition fontengine.h:42
u16 getHash() const
Definition fontengine.h:37
FontMode mode
Definition fontengine.h:43