OpenTTD Source 20260311-master-g511d3794ce
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
9
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;
19static const GlyphID SPRITE_GLYPH = 1U << 30;
20
22class FontCache {
23protected:
24 static std::array<std::unique_ptr<FontCache>, FS_END> caches;
25 std::unique_ptr<FontCache> parent;
26 const FontSize fs;
27 int height = 0;
28 int ascender = 0;
29 int descender = 0;
30
31 FontCache(FontSize fs) : fs(fs) {}
32 static void Register(std::unique_ptr<FontCache> &&fc);
33
34public:
36 virtual ~FontCache() = default;
37
38 static void InitializeFontCaches();
39 static void UninitializeFontCaches();
40 static void LoadFontCaches(FontSizes fontsizes);
41 static void ClearFontCaches(FontSizes fontsizes);
42
44 static const int DEFAULT_FONT_HEIGHT[FS_END];
46 static const int DEFAULT_FONT_ASCENDER[FS_END];
47
48 static int GetDefaultFontHeight(FontSize fs);
49
54 inline FontSize GetSize() const { return this->fs; }
55
60 inline int GetHeight() const { return this->height; }
61
66 inline int GetAscender() const { return this->ascender; }
67
72 inline int GetDescender() const{ return this->descender; }
73
78 virtual int GetFontSize() const { return this->height; }
79
81 virtual void ClearFontCache() = 0;
82
88 virtual const Sprite *GetGlyph(GlyphID key) = 0;
89
95 virtual uint GetGlyphWidth(GlyphID key) = 0;
96
101 virtual bool GetDrawGlyphShadow() = 0;
102
109 virtual GlyphID MapCharToGlyph(char32_t key, bool fallback = true) = 0;
110
115 virtual const void *GetOSHandle()
116 {
117 return nullptr;
118 }
119
124 virtual std::string GetFontName() = 0;
125
131 static inline FontCache *Get(FontSize fs)
132 {
133 assert(fs < FS_END);
134 return FontCache::caches[fs].get();
135 }
136
137 static std::string GetName(FontSize fs);
138
143 inline bool HasParent()
144 {
145 return this->parent != nullptr;
146 }
147
152 virtual bool IsBuiltInFont() = 0;
153};
154
161inline const Sprite *GetGlyph(FontSize size, char32_t key)
162{
163 FontCache *fc = FontCache::Get(size);
164 return fc->GetGlyph(fc->MapCharToGlyph(key));
165}
166
173inline uint GetGlyphWidth(FontSize size, char32_t key)
174{
175 FontCache *fc = FontCache::Get(size);
176 return fc->GetGlyphWidth(fc->MapCharToGlyph(key));
177}
178
179inline bool GetDrawGlyphShadow(FontSize size)
180{
181 return FontCache::Get(size)->GetDrawGlyphShadow();
182}
183
186 std::string font;
187 uint size;
188
189 const void *os_handle = nullptr;
190};
191
201
202extern FontCacheSettings _fcsettings;
203
210{
211 switch (fs) {
212 default: NOT_REACHED();
213 case FS_SMALL: return &_fcsettings.small;
214 case FS_NORMAL: return &_fcsettings.medium;
215 case FS_LARGE: return &_fcsettings.large;
216 case FS_MONO: return &_fcsettings.mono;
217 }
218}
219
221std::string GetFontCacheFontName(FontSize fs);
222
223bool GetFontAAState();
224void SetFont(FontSize fontsize, const std::string &font, uint size);
225
227enum class FontType : uint8_t {
230};
231
233class FontCacheFactory : public BaseProvider<FontCacheFactory> {
234public:
235 FontCacheFactory(std::string_view name, std::string_view description) : BaseProvider<FontCacheFactory>(name, description)
236 {
237 ProviderManager<FontCacheFactory>::Register(*this);
238 }
239
242 {
243 ProviderManager<FontCacheFactory>::Unregister(*this);
244 }
245
252 virtual std::unique_ptr<FontCache> LoadFont(FontSize fs, FontType fonttype) const = 0;
253
263 virtual bool FindFallbackFont(struct FontCacheSettings *settings, const std::string &language_isocode, class MissingGlyphSearcher *callback) const = 0;
264};
265
266class FontProviderManager : ProviderManager<FontCacheFactory> {
267public:
268 static std::unique_ptr<FontCache> LoadFont(FontSize fs, FontType fonttype);
269 static bool FindFallbackFont(FontCacheSettings *settings, const std::string &language_isocode, MissingGlyphSearcher *callback);
270};
271
272/* Implemented in spritefontcache.cpp */
274void SetUnicodeGlyph(FontSize size, char32_t key, SpriteID sprite);
275
276#endif /* FONTCACHE_H */
virtual std::unique_ptr< FontCache > LoadFont(FontSize fs, FontType fonttype) const =0
Try loading a font with this factory.
~FontCacheFactory() override
Unregister this factory.
Definition fontcache.h:241
virtual bool FindFallbackFont(struct FontCacheSettings *settings, const std::string &language_isocode, class MissingGlyphSearcher *callback) const =0
We would like to have a fallback font as the current one doesn't contain all characters we need.
Font cache for basic fonts.
Definition fontcache.h:22
virtual std::string GetFontName()=0
Get the name of this font.
int GetHeight() const
Get the height of the font.
Definition fontcache.h:60
virtual void ClearFontCache()=0
Clear the font cache.
static void UninitializeFontCaches()
Free everything allocated w.r.t.
int height
The height of the font.
Definition fontcache.h:27
virtual ~FontCache()=default
Ensure the destructor of the sub classes are called as well.
bool HasParent()
Check whether the font cache has a parent.
Definition fontcache.h:143
virtual int GetFontSize() const
Get the nominal font size of the font.
Definition fontcache.h:78
std::unique_ptr< FontCache > parent
The parent of this font cache.
Definition fontcache.h:25
virtual const void * GetOSHandle()
Get the native OS font handle, if there is one.
Definition fontcache.h:115
virtual GlyphID MapCharToGlyph(char32_t key, bool fallback=true)=0
Map a character into a glyph.
static std::array< std::unique_ptr< FontCache >, FS_END > caches
All the font caches.
Definition fontcache.h:93
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.
virtual uint GetGlyphWidth(GlyphID key)=0
Get the width of the glyph with the given key.
static void Register(std::unique_ptr< FontCache > &&fc)
Register a FontCache for its font size.
const FontSize fs
The size of the font.
Definition fontcache.h:26
static std::string GetName(FontSize fs)
Get the font name of a given font size.
Definition fontcache.cpp:71
int GetAscender() const
Get the ascender value of the font.
Definition fontcache.h:66
static const int DEFAULT_FONT_ASCENDER[FS_END]
Default unscaled font ascenders.
Definition fontcache.h:26
static const int DEFAULT_FONT_HEIGHT[FS_END]
Default unscaled font heights.
Definition fontcache.h:24
static void LoadFontCaches(FontSizes fontsizes)
(Re)initialize the font cache related things, i.e.
int descender
The descender value of the font.
Definition fontcache.h:29
static FontCache * Get(FontSize fs)
Get the font cache of a given font size.
Definition fontcache.h:131
FontSize GetSize() const
Get the FontSize of the font.
Definition fontcache.h:54
int GetDescender() const
Get the descender value of the font.
Definition fontcache.h:72
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.
Definition fontcache.cpp:98
int ascender
The ascender value of the font.
Definition fontcache.h:28
static std::unique_ptr< FontCache > LoadFont(FontSize fs, FontType fonttype)
Try loading a font with any fontcache factory.
Definition fontcache.cpp:36
static bool FindFallbackFont(FontCacheSettings *settings, const std::string &language_isocode, MissingGlyphSearcher *callback)
We would like to have a fallback font as the current one doesn't contain all characters we need.
Definition fontcache.cpp:55
A searcher for missing glyphs.
fluid_settings_t * settings
FluidSynth settings handle.
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:161
FontType
Different types of font that can be loaded.
Definition fontcache.h:227
@ TrueType
Scalable TrueType fonts.
Definition fontcache.h:229
uint GetGlyphWidth(FontSize size, char32_t key)
Get the width of a glyph.
Definition fontcache.h:173
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:209
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:193
FontCacheSubSetting large
The largest font; mostly used for newspapers.
Definition fontcache.h:196
FontCacheSubSetting mono
The mono space font used for license/readme viewers.
Definition fontcache.h:197
FontCacheSubSetting medium
The normal font size.
Definition fontcache.h:195
bool prefer_sprite
Whether to prefer the built-in sprite font over resizable fonts.
Definition fontcache.h:198
FontCacheSubSetting small
The smallest font; mostly used for zoomed out view.
Definition fontcache.h:194
bool global_aa
Whether to anti alias all font sizes.
Definition fontcache.h:199
Settings for a single font.
Definition fontcache.h:185
std::string font
The name of the font, or path to the font.
Definition fontcache.h:186
uint size
The (requested) size of the font.
Definition fontcache.h:187
const void * os_handle
Optional native OS font info. Only valid during font search.
Definition fontcache.h:189
Data structure describing a sprite.