OpenTTD Source: src/fontcache.h Source File

OpenTTD Source 15.0-beta1

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_t GlyphID;
18static const GlyphID SPRITE_GLYPH = 1U << 30;
19
21 class FontCache {
22protected:
23 static FontCache *caches[FS_END];
24 FontCache *parent;
25 const FontSize fs;
26 int height;
27 int ascender;
28 int descender;
29
30public:
31 FontCache(FontSize fs);
32 virtual ~FontCache();
33
34 static void InitializeFontCaches();
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 virtual int GetFontSize() const { return this->height; }
67
73 virtual void SetUnicodeGlyph(char32_t key, SpriteID sprite) = 0;
74
76 virtual void InitializeUnicodeGlyphMap() = 0;
77
79 virtual void ClearFontCache() = 0;
80
86 virtual const Sprite *GetGlyph(GlyphID key) = 0;
87
93 virtual uint GetGlyphWidth(GlyphID key) = 0;
94
99 virtual bool GetDrawGlyphShadow() = 0;
100
107 virtual GlyphID MapCharToGlyph(char32_t key, bool fallback = true) = 0;
108
113 virtual const void *GetOSHandle()
114 {
115 return nullptr;
116 }
117
122 virtual std::string GetFontName() = 0;
123
129 static inline FontCache *Get(FontSize fs)
130 {
131 assert(fs < FS_END);
132 return FontCache::caches[fs];
133 }
134
135 static std::string GetName(FontSize fs);
136
140 inline bool HasParent()
141 {
142 return this->parent != nullptr;
143 }
144
148 virtual bool IsBuiltInFont() = 0;
149};
150
152 inline void SetUnicodeGlyph(FontSize size, char32_t key, SpriteID sprite)
153{
154 FontCache::Get(size)->SetUnicodeGlyph(key, sprite);
155}
156
158 inline void InitializeUnicodeGlyphMap()
159{
160 for (FontSize fs = FS_BEGIN; fs < FS_END; fs++) {
161 FontCache::Get(fs)->InitializeUnicodeGlyphMap();
162 }
163}
164
165inline void ClearFontCache()
166{
167 for (FontSize fs = FS_BEGIN; fs < FS_END; fs++) {
168 FontCache::Get(fs)->ClearFontCache();
169 }
170}
171
173 inline const Sprite *GetGlyph(FontSize size, char32_t key)
174{
175 FontCache *fc = FontCache::Get(size);
176 return fc->GetGlyph(fc->MapCharToGlyph(key));
177}
178
180 inline uint GetGlyphWidth(FontSize size, char32_t key)
181{
182 FontCache *fc = FontCache::Get(size);
183 return fc->GetGlyphWidth(fc->MapCharToGlyph(key));
184}
185
186inline bool GetDrawGlyphShadow(FontSize size)
187{
188 return FontCache::Get(size)->GetDrawGlyphShadow();
189}
190
192 struct FontCacheSubSetting {
193 std::string font;
194 uint size;
195
196 const void *os_handle = nullptr;
197};
198
200 struct FontCacheSettings {
201 FontCacheSubSetting small;
202 FontCacheSubSetting medium;
203 FontCacheSubSetting large;
204 FontCacheSubSetting mono;
205 bool prefer_sprite;
206 bool global_aa;
207};
208
209extern FontCacheSettings _fcsettings;
210
216 inline FontCacheSubSetting *GetFontCacheSubSetting(FontSize fs)
217{
218 switch (fs) {
219 default: NOT_REACHED();
220 case FS_SMALL: return &_fcsettings.small;
221 case FS_NORMAL: return &_fcsettings.medium;
222 case FS_LARGE: return &_fcsettings.large;
223 case FS_MONO: return &_fcsettings.mono;
224 }
225}
226
227uint GetFontCacheFontSize(FontSize fs);
228std::string GetFontCacheFontName(FontSize fs);
229void InitFontCache(bool monospace);
230void UninitFontCache();
231
232bool GetFontAAState();
233void SetFont(FontSize fontsize, const std::string &font, uint size);
234
235#endif /* FONTCACHE_H */
FontCache
Font cache for basic fonts.
Definition fontcache.h:21
FontCache::GetFontName
virtual std::string GetFontName()=0
Get the name of this font.
FontCache::GetHeight
int GetHeight() const
Get the height of the font.
Definition fontcache.h:48
FontCache::ClearFontCache
virtual void ClearFontCache()=0
Clear the font cache.
FontCache::height
int height
The height of the font.
Definition fontcache.h:26
FontCache::parent
FontCache * parent
The parent of this font cache.
Definition fontcache.h:24
FontCache::HasParent
bool HasParent()
Check whether the font cache has a parent.
Definition fontcache.h:140
FontCache::GetFontSize
virtual int GetFontSize() const
Get the nominal font size of the font.
Definition fontcache.h:66
FontCache::GetOSHandle
virtual const void * GetOSHandle()
Get the native OS font handle, if there is one.
Definition fontcache.h:113
FontCache::MapCharToGlyph
virtual GlyphID MapCharToGlyph(char32_t key, bool fallback=true)=0
Map a character into a glyph.
FontCache::IsBuiltInFont
virtual bool IsBuiltInFont()=0
Is this a built-in sprite font?
FontCache::caches
static FontCache * caches[FS_END]
All the font caches.
Definition fontcache.h:23
FontCache::GetGlyph
virtual const Sprite * GetGlyph(GlyphID key)=0
Get the glyph (sprite) of the given key.
FontCache::InitializeUnicodeGlyphMap
virtual void InitializeUnicodeGlyphMap()=0
Initialize the glyph map.
FontCache::GetGlyphWidth
virtual uint GetGlyphWidth(GlyphID key)=0
Get the width of the glyph with the given key.
FontCache::~FontCache
virtual ~FontCache()
Clean everything up.
Definition fontcache.cpp:44
FontCache::fs
const FontSize fs
The size of the font.
Definition fontcache.h:25
FontCache::GetName
static std::string GetName(FontSize fs)
Get the font name of a given font size.
Definition fontcache.cpp:61
FontCache::GetAscender
int GetAscender() const
Get the ascender value of the font.
Definition fontcache.h:54
FontCache::descender
int descender
The descender value of the font.
Definition fontcache.h:28
FontCache::Get
static FontCache * Get(FontSize fs)
Get the font cache of a given font size.
Definition fontcache.h:129
FontCache::GetSize
FontSize GetSize() const
Get the FontSize of the font.
Definition fontcache.h:42
FontCache::GetDescender
int GetDescender() const
Get the descender value of the font.
Definition fontcache.h:60
FontCache::GetDrawGlyphShadow
virtual bool GetDrawGlyphShadow()=0
Do we need to draw a glyph shadow?
FontCache::ascender
int ascender
The ascender value of the font.
Definition fontcache.h:27
FontCache::SetUnicodeGlyph
virtual void SetUnicodeGlyph(char32_t key, SpriteID sprite)=0
Map a SpriteID to the key.
SetUnicodeGlyph
void SetUnicodeGlyph(FontSize size, char32_t key, SpriteID sprite)
Map a SpriteID to the font size and key.
Definition fontcache.h:152
InitFontCache
void InitFontCache(bool monospace)
(Re)initialize the font cache related things, i.e.
Definition fontcache.cpp:218
GetGlyph
const Sprite * GetGlyph(FontSize size, char32_t key)
Get the Sprite for a glyph.
Definition fontcache.h:173
UninitFontCache
void UninitFontCache()
Free everything allocated w.r.t.
Definition fontcache.cpp:241
GetGlyphWidth
uint GetGlyphWidth(FontSize size, char32_t key)
Get the width of a glyph.
Definition fontcache.h:180
InitializeUnicodeGlyphMap
void InitializeUnicodeGlyphMap()
Initialize the glyph map.
Definition fontcache.h:158
GetFontCacheFontSize
uint GetFontCacheFontSize(FontSize fs)
Get the scalable font size to use for a FontSize.
Definition fontcache.cpp:162
GetFontCacheSubSetting
FontCacheSubSetting * GetFontCacheSubSetting(FontSize fs)
Get the settings of a given font size.
Definition fontcache.h:216
GetFontCacheFontName
std::string GetFontCacheFontName(FontSize fs)
Get font to use for a given font size.
Definition fontcache.cpp:206
GlyphID
uint32_t GlyphID
Glyphs are characters from a font.
Definition fontcache.h:17
SpriteID
uint32_t SpriteID
The number of a sprite, without mapping bits and colourtables.
Definition gfx_type.h:18
FontSize
FontSize
Available font sizes.
Definition gfx_type.h:208
FS_MONO
@ FS_MONO
Index of the monospaced font in the font tables.
Definition gfx_type.h:212
FS_SMALL
@ FS_SMALL
Index of the small font in the font tables.
Definition gfx_type.h:210
FS_BEGIN
@ FS_BEGIN
First font.
Definition gfx_type.h:215
FS_NORMAL
@ FS_NORMAL
Index of the normal font in the font tables.
Definition gfx_type.h:209
FS_LARGE
@ FS_LARGE
Index of the large font in the font tables.
Definition gfx_type.h:211
spritecache.h
Functions to cache sprites in memory.
string_type.h
Types for strings.
FontCacheSettings
Settings for the four different fonts.
Definition fontcache.h:200
FontCacheSettings::large
FontCacheSubSetting large
The largest font; mostly used for newspapers.
Definition fontcache.h:203
FontCacheSettings::mono
FontCacheSubSetting mono
The mono space font used for license/readme viewers.
Definition fontcache.h:204
FontCacheSettings::medium
FontCacheSubSetting medium
The normal font size.
Definition fontcache.h:202
FontCacheSettings::prefer_sprite
bool prefer_sprite
Whether to prefer the built-in sprite font over resizable fonts.
Definition fontcache.h:205
FontCacheSettings::small
FontCacheSubSetting small
The smallest font; mostly used for zoomed out view.
Definition fontcache.h:201
FontCacheSettings::global_aa
bool global_aa
Whether to anti alias all font sizes.
Definition fontcache.h:206
FontCacheSubSetting
Settings for a single font.
Definition fontcache.h:192
FontCacheSubSetting::font
std::string font
The name of the font, or path to the font.
Definition fontcache.h:193
FontCacheSubSetting::size
uint size
The (requested) size of the font.
Definition fontcache.h:194
FontCacheSubSetting::os_handle
const void * os_handle
Optional native OS font info. Only valid during font search.
Definition fontcache.h:196
Sprite
Data structure describing a sprite.
Definition spritecache.h:17