OpenTTD Source 20250719-master-ga8650c6b06
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
17typedef uint32_t GlyphID;
18static const GlyphID SPRITE_GLYPH = 1U << 30;
19
21class FontCache {
22protected:
23 static FontCache *caches[FS_END];
25 const FontSize fs;
26 int height = 0;
27 int ascender = 0;
28 int descender = 0;
29
30public:
32 virtual ~FontCache();
33
34 static void InitializeFontCaches();
35
37 static const int DEFAULT_FONT_HEIGHT[FS_END];
39 static const int DEFAULT_FONT_ASCENDER[FS_END];
40
41 static int GetDefaultFontHeight(FontSize fs);
42
47 inline FontSize GetSize() const { return this->fs; }
48
53 inline int GetHeight() const { return this->height; }
54
59 inline int GetAscender() const { return this->ascender; }
60
65 inline int GetDescender() const{ return this->descender; }
66
71 virtual int GetFontSize() const { return this->height; }
72
78 virtual void SetUnicodeGlyph(char32_t key, SpriteID sprite) = 0;
79
81 virtual void InitializeUnicodeGlyphMap() = 0;
82
84 virtual void ClearFontCache() = 0;
85
91 virtual const Sprite *GetGlyph(GlyphID key) = 0;
92
98 virtual uint GetGlyphWidth(GlyphID key) = 0;
99
104 virtual bool GetDrawGlyphShadow() = 0;
105
112 virtual GlyphID MapCharToGlyph(char32_t key, bool fallback = true) = 0;
113
118 virtual const void *GetOSHandle()
119 {
120 return nullptr;
121 }
122
127 virtual std::string GetFontName() = 0;
128
134 static inline FontCache *Get(FontSize fs)
135 {
136 assert(fs < FS_END);
137 return FontCache::caches[fs];
138 }
139
140 static std::string GetName(FontSize fs);
141
145 inline bool HasParent()
146 {
147 return this->parent != nullptr;
148 }
149
153 virtual bool IsBuiltInFont() = 0;
154};
155
157inline void SetUnicodeGlyph(FontSize size, char32_t key, SpriteID sprite)
158{
159 FontCache::Get(size)->SetUnicodeGlyph(key, sprite);
160}
161
164{
165 for (FontSize fs = FS_BEGIN; fs < FS_END; fs++) {
167 }
168}
169
170inline void ClearFontCache(FontSizes fontsizes)
171{
172 for (FontSize fs : fontsizes) {
174 }
175}
176
178inline const Sprite *GetGlyph(FontSize size, char32_t key)
179{
180 FontCache *fc = FontCache::Get(size);
181 return fc->GetGlyph(fc->MapCharToGlyph(key));
182}
183
185inline uint GetGlyphWidth(FontSize size, char32_t key)
186{
187 FontCache *fc = FontCache::Get(size);
188 return fc->GetGlyphWidth(fc->MapCharToGlyph(key));
189}
190
191inline bool GetDrawGlyphShadow(FontSize size)
192{
193 return FontCache::Get(size)->GetDrawGlyphShadow();
194}
195
198 std::string font;
199 uint size;
200
201 const void *os_handle = nullptr;
202};
203
213
214extern FontCacheSettings _fcsettings;
215
222{
223 switch (fs) {
224 default: NOT_REACHED();
225 case FS_SMALL: return &_fcsettings.small;
226 case FS_NORMAL: return &_fcsettings.medium;
227 case FS_LARGE: return &_fcsettings.large;
228 case FS_MONO: return &_fcsettings.mono;
229 }
230}
231
233std::string GetFontCacheFontName(FontSize fs);
234void InitFontCache(FontSizes fontsizes);
235void UninitFontCache();
236
237bool GetFontAAState();
238void SetFont(FontSize fontsize, const std::string &font, uint size);
239
240#endif /* FONTCACHE_H */
Enum-as-bit-set wrapper.
Font cache for basic fonts.
Definition fontcache.h:21
virtual std::string GetFontName()=0
Get the name of this font.
int GetHeight() const
Get the height of the font.
Definition fontcache.h:53
virtual void ClearFontCache()=0
Clear the font cache.
int height
The height of the font.
Definition fontcache.h:26
FontCache * parent
The parent of this font cache.
Definition fontcache.h:24
bool HasParent()
Check whether the font cache has a parent.
Definition fontcache.h:145
virtual int GetFontSize() const
Get the nominal font size of the font.
Definition fontcache.h:71
virtual const void * GetOSHandle()
Get the native OS font handle, if there is one.
Definition fontcache.h:118
virtual GlyphID MapCharToGlyph(char32_t key, bool fallback=true)=0
Map a character into a glyph.
virtual bool IsBuiltInFont()=0
Is this a built-in sprite font?
static FontCache * caches[FS_END]
All the font caches.
Definition fontcache.h:23
virtual const Sprite * GetGlyph(GlyphID key)=0
Get the glyph (sprite) of the given key.
virtual void InitializeUnicodeGlyphMap()=0
Initialize the glyph map.
virtual uint GetGlyphWidth(GlyphID key)=0
Get the width of the glyph with the given key.
virtual ~FontCache()
Clean everything up.
Definition fontcache.cpp:44
const FontSize fs
The size of the font.
Definition fontcache.h:25
static std::string GetName(FontSize fs)
Get the font name of a given font size.
Definition fontcache.cpp:61
int GetAscender() const
Get the ascender value of the font.
Definition fontcache.h:59
static const int DEFAULT_FONT_ASCENDER[FS_END]
Default unscaled font ascenders.
Definition fontcache.h:28
static const int DEFAULT_FONT_HEIGHT[FS_END]
Default unscaled font heights.
Definition fontcache.h:26
int descender
The descender value of the font.
Definition fontcache.h:28
static FontCache * Get(FontSize fs)
Get the font cache of a given font size.
Definition fontcache.h:134
FontSize GetSize() const
Get the FontSize of the font.
Definition fontcache.h:47
int GetDescender() const
Get the descender value of the font.
Definition fontcache.h:65
virtual bool GetDrawGlyphShadow()=0
Do we need to draw a glyph shadow?
int ascender
The ascender value of the font.
Definition fontcache.h:27
virtual void SetUnicodeGlyph(char32_t key, SpriteID sprite)=0
Map a SpriteID to the key.
void SetUnicodeGlyph(FontSize size, char32_t key, SpriteID sprite)
Map a SpriteID to the font size and key.
Definition fontcache.h:157
const Sprite * GetGlyph(FontSize size, char32_t key)
Get the Sprite for a glyph.
Definition fontcache.h:178
void UninitFontCache()
Free everything allocated w.r.t.
uint GetGlyphWidth(FontSize size, char32_t key)
Get the width of a glyph.
Definition fontcache.h:185
void InitializeUnicodeGlyphMap()
Initialize the glyph map.
Definition fontcache.h:163
uint GetFontCacheFontSize(FontSize fs)
Get the scalable font size to use for a FontSize.
FontCacheSubSetting * GetFontCacheSubSetting(FontSize fs)
Get the settings of a given font size.
Definition fontcache.h:221
void InitFontCache(FontSizes fontsizes)
(Re)initialize the font cache related things, i.e.
std::string GetFontCacheFontName(FontSize fs)
Get font to use for a given font size.
uint32_t GlyphID
Glyphs are characters from a font.
Definition fontcache.h:17
uint32_t SpriteID
The number of a sprite, without mapping bits and colourtables.
Definition gfx_type.h:17
FontSize
Available font sizes.
Definition gfx_type.h:250
@ FS_MONO
Index of the monospaced font in the font tables.
Definition gfx_type.h:254
@ FS_SMALL
Index of the small font in the font tables.
Definition gfx_type.h:252
@ FS_BEGIN
First font.
Definition gfx_type.h:257
@ FS_NORMAL
Index of the normal font in the font tables.
Definition gfx_type.h:251
@ FS_LARGE
Index of the large font in the font tables.
Definition gfx_type.h:253
Functions to cache sprites in memory.
Types for strings.
Settings for the four different fonts.
Definition fontcache.h:205
FontCacheSubSetting large
The largest font; mostly used for newspapers.
Definition fontcache.h:208
FontCacheSubSetting mono
The mono space font used for license/readme viewers.
Definition fontcache.h:209
FontCacheSubSetting medium
The normal font size.
Definition fontcache.h:207
bool prefer_sprite
Whether to prefer the built-in sprite font over resizable fonts.
Definition fontcache.h:210
FontCacheSubSetting small
The smallest font; mostly used for zoomed out view.
Definition fontcache.h:206
bool global_aa
Whether to anti alias all font sizes.
Definition fontcache.h:211
Settings for a single font.
Definition fontcache.h:197
std::string font
The name of the font, or path to the font.
Definition fontcache.h:198
uint size
The (requested) size of the font.
Definition fontcache.h:199
const void * os_handle
Optional native OS font info. Only valid during font search.
Definition fontcache.h:201
Data structure describing a sprite.