OpenTTD Source 20251213-master-g1091fa6071
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 <https://www.gnu.org/licenses/old-licenses/gpl-2.0>.
6 */
7
10#ifndef FONTCACHE_H
11#define FONTCACHE_H
12
13#include "gfx_type.h"
14#include "provider_manager.h"
15#include "spritecache_type.h"
16
18typedef uint32_t GlyphID;
19using FontIndex = uint8_t;
20
21static const FontIndex INVALID_FONT_INDEX = std::numeric_limits<FontIndex>::max();
22
23enum class FontLoadReason : uint8_t {
24 Default,
25 Configured,
26 LanguageFallback,
27 MissingFallback,
28 End,
29};
30
32class FontCache {
33protected:
34 using FontCaches = std::vector<std::unique_ptr<FontCache>>;
35 static FontCaches caches;
36
37 struct FontMetrics {
38 int height = 0;
39 int baseline = 0;
40 };
41
42 static std::array<FontMetrics, FS_END> metrics;
43 static std::array<FontIndex, FS_END> default_font_index;
44
45 const FontSize fs;
46 FontIndex font_index;
47 FontLoadReason load_reason;
48 int height = 0;
49 int ascender = 0;
50 int descender = 0;
51
53 static void Register(std::unique_ptr<FontCache> &&fc, FontLoadReason load_reason);
54 static void LoadDefaultFonts(FontSize fs);
55 static void LoadFallbackFonts(FontSize fs, FontLoadReason load_reason);
56
57public:
58 virtual ~FontCache() = default;
59
60 static void InitializeFontCaches();
61 static void UninitializeFontCaches();
62 static void LoadFontCaches(FontSizes fontsizes);
63 static void ClearFontCaches(FontSizes fontsizes);
64
66 static const int DEFAULT_FONT_HEIGHT[FS_END];
68 static const int DEFAULT_FONT_ASCENDER[FS_END];
69
70 static int GetDefaultFontHeight(FontSize fs);
71
72 static inline int GetFontBaseline(FontSize fs)
73 {
74 return FontCache::metrics[fs].baseline;
75 }
76
77 static void AddFallback(FontSizes fontsizes, FontLoadReason load_reason, std::string_view name, std::span<const std::byte> os_data = {});
78
85 template <typename T>
86 static void AddFallbackWithHandle(FontSizes fontsizes, FontLoadReason load_reason, std::string_view name, T &handle)
87 {
88 auto os_data = std::as_bytes(std::span(&handle, 1));
89 FontCache::AddFallback(fontsizes, load_reason, name, os_data);
90 }
91
96 inline FontSize GetSize() const { return this->fs; }
97
98 inline FontIndex GetIndex() const { return this->font_index; }
99
100 inline FontLoadReason GetFontLoadReason() const { return this->load_reason; }
101
106 inline int GetHeight() const { return this->height; }
107
112 inline int GetAscender() const { return this->ascender; }
113
118 inline int GetDescender() const{ return this->descender; }
119
124 virtual int GetFontSize() const { return this->height; }
125
127 virtual void ClearFontCache() = 0;
128
134 virtual const Sprite *GetGlyph(GlyphID key) = 0;
135
141 virtual uint GetGlyphWidth(GlyphID key) = 0;
142
147 virtual bool GetDrawGlyphShadow() = 0;
148
154 virtual GlyphID MapCharToGlyph(char32_t key) = 0;
155
160 virtual const void *GetOSHandle()
161 {
162 return nullptr;
163 }
164
169 virtual std::string GetFontName() = 0;
170
171 virtual int GetGlyphYOffset();
172
177 static inline std::span<const std::unique_ptr<FontCache>> Get()
178 {
179 return FontCache::caches;
180 }
181
187 static inline FontCache *Get(FontIndex font_index)
188 {
189 assert(font_index < FontCache::caches.size());
190 return FontCache::caches[font_index].get();
191 }
192
193 static inline int GetCharacterHeight(FontSize fs)
194 {
195 return FontCache::metrics[fs].height;
196 }
197
198 static void UpdateCharacterHeight(FontSize fs);
199
200 static inline FontIndex GetDefaultFontIndex(FontSize fs)
201 {
202 return FontCache::default_font_index[fs];
203 }
204
205 static inline class FontCache *GetDefaultFontCache(FontSize fs)
206 {
207 FontIndex index = FontCache::GetDefaultFontIndex(fs);
208 if (index != INVALID_FONT_INDEX) return FontCache::Get(index);
209 NOT_REACHED();
210 }
211
212 static inline FontIndex GetFontIndexForCharacter(FontSize fs, char32_t c)
213 {
214 for (auto it = std::rbegin(FontCache::caches); it != std::rend(FontCache::caches); ++it) {
215 FontCache *fc = it->get();
216 if (fc == nullptr) continue;
217 if (fc->GetSize() != fs) continue;
218 if (fc->MapCharToGlyph(c) == 0) continue;
219 return std::distance(std::begin(FontCache::caches), std::next(it).base());
220 }
221 return INVALID_FONT_INDEX;
222 }
223
227 virtual bool IsBuiltInFont() = 0;
228};
229
231inline const Sprite *GetGlyph(FontSize size, char32_t key)
232{
233 FontIndex font_index = FontCache::GetFontIndexForCharacter(size, key);
234 FontCache *fc = font_index != INVALID_FONT_INDEX ? FontCache::Get(font_index) : FontCache::GetDefaultFontCache(size);
235 if (fc == nullptr) return nullptr;
236 return fc->GetGlyph(fc->MapCharToGlyph(key));
237}
238
240inline uint GetGlyphWidth(FontSize size, char32_t key)
241{
242 FontIndex font_index = FontCache::GetFontIndexForCharacter(size, key);
243 FontCache *fc = font_index != INVALID_FONT_INDEX ? FontCache::Get(font_index) : FontCache::GetDefaultFontCache(size);
244 if (fc == nullptr) return 0;
245 return fc->GetGlyphWidth(fc->MapCharToGlyph(key));
246}
247
250 std::string font;
251 uint size;
252
254 FontLoadReason load_reason = FontLoadReason::LanguageFallback;
255 std::string name;
256 std::vector<std::byte> os_handle;
257 };
258
259 std::vector<FontCacheFallback> fallback_fonts;
260};
261
272
273extern FontCacheSettings _fcsettings;
274
281{
282 switch (fs) {
283 default: NOT_REACHED();
284 case FS_SMALL: return &_fcsettings.small;
285 case FS_NORMAL: return &_fcsettings.medium;
286 case FS_LARGE: return &_fcsettings.large;
287 case FS_MONO: return &_fcsettings.mono;
288 }
289}
290
292std::string GetFontCacheFontName(FontSize fs);
293
294bool GetFontAAState();
295void SetFont(FontSize fontsize, const std::string &font, uint size);
296
298enum class FontType : uint8_t {
299 Sprite,
300 TrueType,
301};
302
304class FontCacheFactory : public BaseProvider<FontCacheFactory> {
305public:
306 FontCacheFactory(std::string_view name, std::string_view description) : BaseProvider<FontCacheFactory>(name, description)
307 {
309 }
310
311 virtual ~FontCacheFactory()
312 {
314 }
315
316 virtual std::unique_ptr<FontCache> LoadFont(FontSize fs, FontType fonttype, bool search, const std::string &font_name, std::span<const std::byte> os_handle) const = 0;
317 virtual bool FindFallbackFont(const std::string &language_isocode, FontSizes fontsizes, class MissingGlyphSearcher *callback) const = 0;
318};
319
320class FontProviderManager : ProviderManager<FontCacheFactory> {
321public:
322 static std::unique_ptr<FontCache> LoadFont(FontSize fs, FontType fonttype, bool search, const std::string &font_name, std::span<const std::byte> os_handle);
323 static bool FindFallbackFont(const std::string &language_isocode, FontSizes fontsizes, class MissingGlyphSearcher *callback);
324};
325
326/* Implemented in spritefontcache.cpp */
328void SetUnicodeGlyph(FontSize size, char32_t key, SpriteID sprite);
329
330#endif /* FONTCACHE_H */
Factory for FontCaches.
Definition fontcache.h:304
Font cache for basic fonts.
Definition fontcache.h:32
virtual std::string GetFontName()=0
Get the name of this font.
int GetHeight() const
Get the height of the font.
Definition fontcache.h:106
virtual void ClearFontCache()=0
Clear the font cache.
static void UninitializeFontCaches()
Free everything allocated w.r.t.
static std::span< const std::unique_ptr< FontCache > > Get()
Get span of all FontCaches.
Definition fontcache.h:177
int height
The height of the font.
Definition fontcache.h:48
static void AddFallbackWithHandle(FontSizes fontsizes, FontLoadReason load_reason, std::string_view name, T &handle)
Add a fallback font, with OS-specific handle.
Definition fontcache.h:86
virtual int GetFontSize() const
Get the nominal font size of the font.
Definition fontcache.h:124
virtual const void * GetOSHandle()
Get the native OS font handle, if there is one.
Definition fontcache.h:160
virtual bool IsBuiltInFont()=0
Is this a built-in sprite font?
virtual const Sprite * GetGlyph(GlyphID key)=0
Get the glyph (sprite) of the given key.
static void ClearFontCaches(FontSizes fontsizes)
Clear cached information for the specified font caches.
static FontCache * Get(FontIndex font_index)
Get the font cache of a given font size.
Definition fontcache.h:187
virtual uint GetGlyphWidth(GlyphID key)=0
Get the width of the glyph with the given key.
const FontSize fs
The size of the font.
Definition fontcache.h:45
FontLoadReason load_reason
Reason why the font is loaded.
Definition fontcache.h:47
int GetAscender() const
Get the ascender value of the font.
Definition fontcache.h:112
static const int DEFAULT_FONT_ASCENDER[FS_END]
Default unscaled font ascenders.
Definition fontcache.h:29
static const int DEFAULT_FONT_HEIGHT[FS_END]
Default unscaled font heights.
Definition fontcache.h:27
virtual GlyphID MapCharToGlyph(char32_t key)=0
Map a character into a glyph.
static void LoadFontCaches(FontSizes fontsizes)
(Re)initialize the font cache related things, i.e.
FontIndex font_index
The index of the font.
Definition fontcache.h:46
int descender
The descender value of the font.
Definition fontcache.h:50
FontSize GetSize() const
Get the FontSize of the font.
Definition fontcache.h:96
int GetDescender() const
Get the descender value of the font.
Definition fontcache.h:118
virtual bool GetDrawGlyphShadow()=0
Do we need to draw a glyph shadow?
static void InitializeFontCaches()
Initialise font caches with the base sprite font cache for all sizes.
int ascender
The ascender value of the font.
Definition fontcache.h:49
static void AddFallback(FontSizes fontsizes, FontLoadReason load_reason, std::string_view name, std::span< const std::byte > os_data={})
Add a fallback font, with optional OS-specific handle.
static std::unique_ptr< FontCache > LoadFont(FontSize fs, FontType fonttype, bool search, const std::string &font_name, std::span< const std::byte > os_handle)
Try loading a font with any fontcache factory.
Definition fontcache.cpp:39
static bool FindFallbackFont(const std::string &language_isocode, FontSizes fontsizes, class MissingGlyphSearcher *callback)
We would like to have a fallback font as the current one doesn't contain all characters we need.
Definition fontcache.cpp:58
A searcher for missing glyphs.
The ProviderManager manages a single Provider-type.
@ End
End marker.
void SetUnicodeGlyph(FontSize size, char32_t key, SpriteID sprite)
Set the SpriteID for a unicode character.
const Sprite * GetGlyph(FontSize size, char32_t key)
Get the Sprite for a glyph.
Definition fontcache.h:231
FontType
Different types of font that can be loaded.
Definition fontcache.h:298
@ TrueType
Scalable TrueType fonts.
@ Sprite
Bitmap sprites from GRF files.
uint GetGlyphWidth(FontSize size, char32_t key)
Get the width of a glyph.
Definition fontcache.h:240
void InitializeUnicodeGlyphMap()
Initialize the glyph map.
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:280
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:18
Types related to the graphics and/or input devices.
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:248
@ FS_MONO
Index of the monospaced font in the font tables.
Definition gfx_type.h:252
@ FS_SMALL
Index of the small font in the font tables.
Definition gfx_type.h:250
@ FS_NORMAL
Index of the normal font in the font tables.
Definition gfx_type.h:249
@ FS_LARGE
Index of the large font in the font tables.
Definition gfx_type.h:251
Definition of the ProviderManager.
Types related to the sprite cache.
Settings for the four different fonts.
Definition fontcache.h:263
FontCacheSubSetting large
The largest font; mostly used for newspapers.
Definition fontcache.h:266
FontCacheSubSetting mono
The mono space font used for license/readme viewers.
Definition fontcache.h:267
bool prefer_default
Prefer OpenTTD's default font over autodetected fallback fonts.
Definition fontcache.h:270
FontCacheSubSetting medium
The normal font size.
Definition fontcache.h:265
bool prefer_sprite
Whether to prefer the built-in sprite font over resizable fonts.
Definition fontcache.h:268
FontCacheSubSetting small
The smallest font; mostly used for zoomed out view.
Definition fontcache.h:264
bool global_aa
Whether to anti alias all font sizes.
Definition fontcache.h:269
Settings for a single font.
Definition fontcache.h:249
std::string font
The name of the font, or path to the font.
Definition fontcache.h:250
uint size
The (requested) size of the font.
Definition fontcache.h:251
Data structure describing a sprite.