OpenTTD Source: src/fontcache.h Source File

OpenTTD Source 1.11.0-RC1

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 static int GetDefaultFontHeight(FontSize fs);
33
34 public:
35 FontCache(FontSize fs);
36 virtual ~FontCache();
37
42 inline FontSize GetSize() const { return this->fs; }
43
48 virtual 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 SpriteID GetUnicodeGlyph(WChar key) = 0;
80
86 virtual void SetUnicodeGlyph(WChar key, SpriteID sprite) = 0;
87
89 virtual void InitializeUnicodeGlyphMap() = 0;
90
92 virtual void ClearFontCache() = 0;
93
99 virtual const Sprite *GetGlyph(GlyphID key) = 0;
100
106 virtual uint GetGlyphWidth(GlyphID key) = 0;
107
112 virtual bool GetDrawGlyphShadow() = 0;
113
119 virtual GlyphID MapCharToGlyph(WChar key) = 0;
120
127 virtual const void *GetFontTable(uint32 tag, size_t &length) = 0;
128
133 virtual const void *GetOSHandle()
134 {
135 return nullptr;
136 }
137
142 virtual const char *GetFontName() = 0;
143
149 static inline FontCache *Get(FontSize fs)
150 {
151 assert(fs < FS_END);
152 return FontCache::caches[fs];
153 }
154
158 inline bool HasParent()
159 {
160 return this->parent != nullptr;
161 }
162
166 virtual bool IsBuiltInFont() = 0;
167 };
168
170 static inline SpriteID GetUnicodeGlyph(FontSize size, WChar key)
171 {
172 return FontCache::Get(size)->GetUnicodeGlyph(key);
173 }
174
176 static inline void SetUnicodeGlyph(FontSize size, WChar key, SpriteID sprite)
177 {
178 FontCache::Get(size)->SetUnicodeGlyph(key, sprite);
179 }
180
182 static inline void InitializeUnicodeGlyphMap()
183 {
184 for (FontSize fs = FS_BEGIN; fs < FS_END; fs++) {
185 FontCache::Get(fs)->InitializeUnicodeGlyphMap();
186 }
187 }
188
189 static inline void ClearFontCache()
190 {
191 for (FontSize fs = FS_BEGIN; fs < FS_END; fs++) {
192 FontCache::Get(fs)->ClearFontCache();
193 }
194 }
195
197 static inline const Sprite *GetGlyph(FontSize size, WChar key)
198 {
199 FontCache *fc = FontCache::Get(size);
200 return fc->GetGlyph(fc->MapCharToGlyph(key));
201 }
202
204 static inline uint GetGlyphWidth(FontSize size, WChar key)
205 {
206 FontCache *fc = FontCache::Get(size);
207 return fc->GetGlyphWidth(fc->MapCharToGlyph(key));
208 }
209
210 static inline bool GetDrawGlyphShadow(FontSize size)
211 {
212 return FontCache::Get(size)->GetDrawGlyphShadow();
213 }
214
216 struct FreeTypeSubSetting {
217 char font[MAX_PATH];
218 uint size;
219 bool aa;
220
221 const void *os_handle = nullptr;
222 };
223
225 struct FreeTypeSettings {
226 FreeTypeSubSetting small;
227 FreeTypeSubSetting medium;
228 FreeTypeSubSetting large;
229 FreeTypeSubSetting mono;
230 };
231
232 extern FreeTypeSettings _freetype;
233
234 void InitFreeType(bool monospace);
235 void UninitFreeType();
236 bool HasAntialiasedFonts();
237
238 #endif /* FONTCACHE_H */
GlyphID
uint32 GlyphID
Glyphs are characters from a font.
Definition: fontcache.h:17
FontCache::GetUnicodeGlyph
virtual SpriteID GetUnicodeGlyph(WChar key)=0
Get the SpriteID mapped to the given key.
WChar
char32_t WChar
Type for wide characters, i.e.
Definition: string_type.h:35
FontCache::GetSize
FontSize GetSize() const
Get the FontSize of the font.
Definition: fontcache.h:42
FreeTypeSettings::large
FreeTypeSubSetting large
The largest font; mostly used for newspapers.
Definition: fontcache.h:228
FontCache::GetDescender
int GetDescender() const
Get the descender value of the font.
Definition: fontcache.h:60
FS_BEGIN
@ FS_BEGIN
First font.
Definition: gfx_type.h:213
FontCache::height
int height
The height of the font.
Definition: fontcache.h:27
FreeTypeSettings::mono
FreeTypeSubSetting mono
The mono space font used for license/readme viewers.
Definition: fontcache.h:229
FreeTypeSubSetting::aa
bool aa
Whether to do anti aliasing or not.
Definition: fontcache.h:219
FontCache::GetAscender
int GetAscender() const
Get the ascender value of the font.
Definition: fontcache.h:54
FreeTypeSubSetting::os_handle
const void * os_handle
Optional native OS font info. Only valid during font search.
Definition: fontcache.h:221
FreeTypeSubSetting::font
char font[MAX_PATH]
The name of the font, or path to the font.
Definition: fontcache.h:217
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::GetFontName
virtual const char * GetFontName()=0
Get the name of this font.
FontCache::units_per_em
int units_per_em
The units per EM value of the font.
Definition: fontcache.h:30
InitFreeType
void InitFreeType(bool monospace)
(Re)initialize the freetype related things, i.e.
Definition: fontcache.cpp:683
FontCache::HasParent
bool HasParent()
Check whether the font cache has a parent.
Definition: fontcache.h:158
UninitFreeType
void UninitFreeType()
Free everything allocated w.r.t.
Definition: fontcache.cpp:706
FreeTypeSubSetting::size
uint size
The (requested) size of the font.
Definition: fontcache.h:218
FontCache::~FontCache
virtual ~FontCache()
Clean everything up.
Definition: fontcache.cpp:51
HasAntialiasedFonts
bool HasAntialiasedFonts()
Should any of the active fonts be anti-aliased?
Definition: fontcache.cpp:723
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:133
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:204
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:176
FontCache::caches
static FontCache * caches[FS_END]
All the font caches.
Definition: fontcache.h:23
FontCache
Font cache for basic fonts.
Definition: fontcache.h:21
FreeTypeSubSetting
Settings for a single freetype font.
Definition: fontcache.h:216
FontCache::GetGlyph
virtual const Sprite * GetGlyph(GlyphID key)=0
Get the glyph (sprite) of the given key.
FreeTypeSettings::medium
FreeTypeSubSetting medium
The normal font size.
Definition: fontcache.h:227
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:197
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
InitializeUnicodeGlyphMap
static void InitializeUnicodeGlyphMap()
Initialize the glyph map.
Definition: fontcache.h:182
FontCache::ClearFontCache
virtual void ClearFontCache()=0
Clear the font cache.
FontSize
FontSize
Available font sizes.
Definition: gfx_type.h:206
GetUnicodeGlyph
static SpriteID GetUnicodeGlyph(FontSize size, WChar key)
Get the SpriteID mapped to the given font size and key.
Definition: fontcache.h:170
FontCache::MapCharToGlyph
virtual GlyphID MapCharToGlyph(WChar key)=0
Map a character into a glyph.
FreeTypeSettings::small
FreeTypeSubSetting small
The smallest font; mostly used for zoomed out view.
Definition: fontcache.h:226
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:41
FontCache::InitializeUnicodeGlyphMap
virtual void InitializeUnicodeGlyphMap()=0
Initialize the glyph map.
FreeTypeSettings
Settings for the freetype fonts.
Definition: fontcache.h:225
FontCache::Get
static FontCache * Get(FontSize fs)
Get the font cache of a given font size.
Definition: fontcache.h:149
FontCache::GetHeight
virtual int GetHeight() const
Get the height of the font.
Definition: fontcache.h:48