OpenTTD Source: src/fontcache.h Source File

OpenTTD Source 13.2.1

fontcache.h
Go to the documentation of this file.
1 /*
2 * This file is part of OpenTTD.
3 * OpenTTD is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, version 2.
4 * OpenTTD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
5 * See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with OpenTTD. If not, see <http://www.gnu.org/licenses/>.
6 */
7
10 #ifndef FONTCACHE_H
11 #define FONTCACHE_H
12
13 #include "string_type.h"
14 #include "spritecache.h"
15
17 typedef uint32 GlyphID;
18 static const GlyphID SPRITE_GLYPH = 1U << 30;
19
21 class FontCache {
22 private:
23 static FontCache *caches[FS_END];
24 protected:
25 FontCache *parent;
26 const FontSize fs;
27 int height;
28 int ascender;
29 int descender;
30 int units_per_em;
31
32 public:
33 FontCache(FontSize fs);
34 virtual ~FontCache();
35
36 static int GetDefaultFontHeight(FontSize fs);
37
42 inline FontSize GetSize() const { return this->fs; }
43
48 inline int GetHeight() const { return this->height; }
49
54 inline int GetAscender() const { return this->ascender; }
55
60 inline int GetDescender() const{ return this->descender; }
61
66 inline int GetUnitsPerEM() const { return this->units_per_em; }
67
72 virtual int GetFontSize() const { return this->height; }
73
79 virtual void SetUnicodeGlyph(WChar key, SpriteID sprite) = 0;
80
82 virtual void InitializeUnicodeGlyphMap() = 0;
83
85 virtual void ClearFontCache() = 0;
86
92 virtual const Sprite *GetGlyph(GlyphID key) = 0;
93
99 virtual uint GetGlyphWidth(GlyphID key) = 0;
100
105 virtual bool GetDrawGlyphShadow() = 0;
106
112 virtual GlyphID MapCharToGlyph(WChar key) = 0;
113
120 virtual const void *GetFontTable(uint32 tag, size_t &length) = 0;
121
126 virtual const void *GetOSHandle()
127 {
128 return nullptr;
129 }
130
135 virtual std::string GetFontName() = 0;
136
142 static inline FontCache *Get(FontSize fs)
143 {
144 assert(fs < FS_END);
145 return FontCache::caches[fs];
146 }
147
151 inline bool HasParent()
152 {
153 return this->parent != nullptr;
154 }
155
159 virtual bool IsBuiltInFont() = 0;
160 };
161
163 static inline void SetUnicodeGlyph(FontSize size, WChar key, SpriteID sprite)
164 {
165 FontCache::Get(size)->SetUnicodeGlyph(key, sprite);
166 }
167
169 static inline void InitializeUnicodeGlyphMap()
170 {
171 for (FontSize fs = FS_BEGIN; fs < FS_END; fs++) {
172 FontCache::Get(fs)->InitializeUnicodeGlyphMap();
173 }
174 }
175
176 static inline void ClearFontCache()
177 {
178 for (FontSize fs = FS_BEGIN; fs < FS_END; fs++) {
179 FontCache::Get(fs)->ClearFontCache();
180 }
181 }
182
184 static inline const Sprite *GetGlyph(FontSize size, WChar key)
185 {
186 FontCache *fc = FontCache::Get(size);
187 return fc->GetGlyph(fc->MapCharToGlyph(key));
188 }
189
191 static inline uint GetGlyphWidth(FontSize size, WChar key)
192 {
193 FontCache *fc = FontCache::Get(size);
194 return fc->GetGlyphWidth(fc->MapCharToGlyph(key));
195 }
196
197 static inline bool GetDrawGlyphShadow(FontSize size)
198 {
199 return FontCache::Get(size)->GetDrawGlyphShadow();
200 }
201
203 struct FontCacheSubSetting {
204 std::string font;
205 uint size;
206 bool aa;
207
208 const void *os_handle = nullptr;
209 };
210
212 struct FontCacheSettings {
213 FontCacheSubSetting small;
214 FontCacheSubSetting medium;
215 FontCacheSubSetting large;
216 FontCacheSubSetting mono;
217 };
218
219 extern FontCacheSettings _fcsettings;
220
226 static inline FontCacheSubSetting *GetFontCacheSubSetting(FontSize fs)
227 {
228 switch (fs) {
229 default: NOT_REACHED();
230 case FS_SMALL: return &_fcsettings.small;
231 case FS_NORMAL: return &_fcsettings.medium;
232 case FS_LARGE: return &_fcsettings.large;
233 case FS_MONO: return &_fcsettings.mono;
234 }
235 }
236
237 void InitFontCache(bool monospace);
238 void UninitFontCache();
239 bool HasAntialiasedFonts();
240
241 bool GetFontAAState(FontSize size, bool check_blitter = true);
242 void SetFont(FontSize fontsize, const std::string &font, uint size, bool aa);
243
244 #endif /* FONTCACHE_H */
GlyphID
uint32 GlyphID
Glyphs are characters from a font.
Definition: fontcache.h:17
UninitFontCache
void UninitFontCache()
Free everything allocated w.r.t.
Definition: fontcache.cpp:152
WChar
char32_t WChar
Type for wide characters, i.e.
Definition: string_type.h:36
FontCache::GetSize
FontSize GetSize() const
Get the FontSize of the font.
Definition: fontcache.h:42
FontCacheSubSetting
Settings for a single font.
Definition: fontcache.h:203
FontCache::GetDescender
int GetDescender() const
Get the descender value of the font.
Definition: fontcache.h:60
FontCache::GetHeight
int GetHeight() const
Get the height of the font.
Definition: fontcache.h:48
FS_BEGIN
@ FS_BEGIN
First font.
Definition: gfx_type.h:209
FontCache::height
int height
The height of the font.
Definition: fontcache.h:27
FontCacheSubSetting::aa
bool aa
Whether to do anti aliasing or not.
Definition: fontcache.h:206
FS_LARGE
@ FS_LARGE
Index of the large font in the font tables.
Definition: gfx_type.h:205
FontCache::GetAscender
int GetAscender() const
Get the ascender value of the font.
Definition: fontcache.h:54
FontCacheSubSetting::size
uint size
The (requested) size of the font.
Definition: fontcache.h:205
FontCache::GetDrawGlyphShadow
virtual bool GetDrawGlyphShadow()=0
Do we need to draw a glyph shadow?
SpriteID
uint32 SpriteID
The number of a sprite, without mapping bits and colourtables.
Definition: gfx_type.h:17
FontCache::IsBuiltInFont
virtual bool IsBuiltInFont()=0
Is this a built-in sprite font?
FontCache::units_per_em
int units_per_em
The units per EM value of the font.
Definition: fontcache.h:30
FontCacheSettings
Settings for the four different fonts.
Definition: fontcache.h:212
FontCache::HasParent
bool HasParent()
Check whether the font cache has a parent.
Definition: fontcache.h:151
FS_NORMAL
@ FS_NORMAL
Index of the normal font in the font tables.
Definition: gfx_type.h:203
FontCacheSubSetting::font
std::string font
The name of the font, or path to the font.
Definition: fontcache.h:204
FS_SMALL
@ FS_SMALL
Index of the small font in the font tables.
Definition: gfx_type.h:204
FontCacheSettings::large
FontCacheSubSetting large
The largest font; mostly used for newspapers.
Definition: fontcache.h:215
GetFontCacheSubSetting
static FontCacheSubSetting * GetFontCacheSubSetting(FontSize fs)
Get the settings of a given font size.
Definition: fontcache.h:226
InitFontCache
void InitFontCache(bool monospace)
(Re)initialize the font cache related things, i.e.
Definition: fontcache.cpp:128
FontCache::~FontCache
virtual ~FontCache()
Clean everything up.
Definition: fontcache.cpp:44
FontCacheSettings::medium
FontCacheSubSetting medium
The normal font size.
Definition: fontcache.h:214
HasAntialiasedFonts
bool HasAntialiasedFonts()
Should any of the active fonts be anti-aliased?
Definition: fontcache.cpp:169
FontCache::GetUnitsPerEM
int GetUnitsPerEM() const
Get the units per EM value of the font.
Definition: fontcache.h:66
FontCache::fs
const FontSize fs
The size of the font.
Definition: fontcache.h:26
FontCache::GetOSHandle
virtual const void * GetOSHandle()
Get the native OS font handle, if there is one.
Definition: fontcache.h:126
string_type.h
FontCache::GetGlyphWidth
virtual uint GetGlyphWidth(GlyphID key)=0
Get the width of the glyph with the given key.
GetGlyphWidth
static uint GetGlyphWidth(FontSize size, WChar key)
Get the width of a glyph.
Definition: fontcache.h:191
FontCache::descender
int descender
The descender value of the font.
Definition: fontcache.h:29
spritecache.h
SetUnicodeGlyph
static void SetUnicodeGlyph(FontSize size, WChar key, SpriteID sprite)
Map a SpriteID to the font size and key.
Definition: fontcache.h:163
FontCache::caches
static FontCache * caches[FS_END]
All the font caches.
Definition: fontcache.h:23
FontCache::GetFontName
virtual std::string GetFontName()=0
Get the name of this font.
FontCache
Font cache for basic fonts.
Definition: fontcache.h:21
FontCache::GetGlyph
virtual const Sprite * GetGlyph(GlyphID key)=0
Get the glyph (sprite) of the given key.
FontCache::parent
FontCache * parent
The parent of this font cache.
Definition: fontcache.h:25
GetGlyph
static const Sprite * GetGlyph(FontSize size, WChar key)
Get the Sprite for a glyph.
Definition: fontcache.h:184
FontCacheSettings::mono
FontCacheSubSetting mono
The mono space font used for license/readme viewers.
Definition: fontcache.h:216
FontCache::GetFontSize
virtual int GetFontSize() const
Get the nominal font size of the font.
Definition: fontcache.h:72
FontCache::GetFontTable
virtual const void * GetFontTable(uint32 tag, size_t &length)=0
Read a font table from the font.
FontCache::ascender
int ascender
The ascender value of the font.
Definition: fontcache.h:28
FontCacheSubSetting::os_handle
const void * os_handle
Optional native OS font info. Only valid during font search.
Definition: fontcache.h:208
InitializeUnicodeGlyphMap
static void InitializeUnicodeGlyphMap()
Initialize the glyph map.
Definition: fontcache.h:169
FS_MONO
@ FS_MONO
Index of the monospaced font in the font tables.
Definition: gfx_type.h:206
FontCache::ClearFontCache
virtual void ClearFontCache()=0
Clear the font cache.
FontSize
FontSize
Available font sizes.
Definition: gfx_type.h:202
FontCache::MapCharToGlyph
virtual GlyphID MapCharToGlyph(WChar key)=0
Map a character into a glyph.
FontCacheSettings::small
FontCacheSubSetting small
The smallest font; mostly used for zoomed out view.
Definition: fontcache.h:213
FontCache::SetUnicodeGlyph
virtual void SetUnicodeGlyph(WChar key, SpriteID sprite)=0
Map a SpriteID to the key.
Sprite
Data structure describing a sprite.
Definition: spritecache.h:17
FontCache::FontCache
FontCache(FontSize fs)
Create a new font cache.
Definition: fontcache.cpp:34
FontCache::InitializeUnicodeGlyphMap
virtual void InitializeUnicodeGlyphMap()=0
Initialize the glyph map.
FontCache::Get
static FontCache * Get(FontSize fs)
Get the font cache of a given font size.
Definition: fontcache.h:142