OpenTTD Source 20250905-master-g122023be8d
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 "provider_manager.h"
14#include "string_type.h"
15#include "spritecache.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
32 static void Register(std::unique_ptr<FontCache> &&fc);
33
34public:
35 virtual ~FontCache() = default;
36
37 static void InitializeFontCaches();
38 static void UninitializeFontCaches();
39 static void LoadFontCaches(FontSizes fontsizes);
40 static void ClearFontCaches(FontSizes fontsizes);
41
43 static const int DEFAULT_FONT_HEIGHT[FS_END];
45 static const int DEFAULT_FONT_ASCENDER[FS_END];
46
47 static int GetDefaultFontHeight(FontSize fs);
48
53 inline FontSize GetSize() const { return this->fs; }
54
59 inline int GetHeight() const { return this->height; }
60
65 inline int GetAscender() const { return this->ascender; }
66
71 inline int GetDescender() const{ return this->descender; }
72
77 virtual int GetFontSize() const { return this->height; }
78
80 virtual void ClearFontCache() = 0;
81
87 virtual const Sprite *GetGlyph(GlyphID key) = 0;
88
94 virtual uint GetGlyphWidth(GlyphID key) = 0;
95
100 virtual bool GetDrawGlyphShadow() = 0;
101
108 virtual GlyphID MapCharToGlyph(char32_t key, bool fallback = true) = 0;
109
114 virtual const void *GetOSHandle()
115 {
116 return nullptr;
117 }
118
123 virtual std::string GetFontName() = 0;
124
130 static inline FontCache *Get(FontSize fs)
131 {
132 assert(fs < FS_END);
133 return FontCache::caches[fs].get();
134 }
135
136 static std::string GetName(FontSize fs);
137
141 inline bool HasParent()
142 {
143 return this->parent != nullptr;
144 }
145
149 virtual bool IsBuiltInFont() = 0;
150};
151
153inline const Sprite *GetGlyph(FontSize size, char32_t key)
154{
155 FontCache *fc = FontCache::Get(size);
156 return fc->GetGlyph(fc->MapCharToGlyph(key));
157}
158
160inline uint GetGlyphWidth(FontSize size, char32_t key)
161{
162 FontCache *fc = FontCache::Get(size);
163 return fc->GetGlyphWidth(fc->MapCharToGlyph(key));
164}
165
166inline bool GetDrawGlyphShadow(FontSize size)
167{
168 return FontCache::Get(size)->GetDrawGlyphShadow();
169}
170
173 std::string font;
174 uint size;
175
176 const void *os_handle = nullptr;
177};
178
188
189extern FontCacheSettings _fcsettings;
190
197{
198 switch (fs) {
199 default: NOT_REACHED();
200 case FS_SMALL: return &_fcsettings.small;
201 case FS_NORMAL: return &_fcsettings.medium;
202 case FS_LARGE: return &_fcsettings.large;
203 case FS_MONO: return &_fcsettings.mono;
204 }
205}
206
208std::string GetFontCacheFontName(FontSize fs);
209
210bool GetFontAAState();
211void SetFont(FontSize fontsize, const std::string &font, uint size);
212
214enum class FontType : uint8_t {
215 Sprite,
216 TrueType,
217};
218
220class FontCacheFactory : public BaseProvider<FontCacheFactory> {
221public:
222 FontCacheFactory(std::string_view name, std::string_view description) : BaseProvider<FontCacheFactory>(name, description)
223 {
225 }
226
227 virtual ~FontCacheFactory()
228 {
230 }
231
232 virtual std::unique_ptr<FontCache> LoadFont(FontSize fs, FontType fonttype) = 0;
233 virtual bool FindFallbackFont(struct FontCacheSettings *settings, const std::string &language_isocode, class MissingGlyphSearcher *callback) = 0;
234};
235
236class FontProviderManager : ProviderManager<FontCacheFactory> {
237public:
238 static std::unique_ptr<FontCache> LoadFont(FontSize fs, FontType fonttype);
239 static bool FindFallbackFont(FontCacheSettings *settings, const std::string &language_isocode, MissingGlyphSearcher *callback);
240};
241
242/* Implemented in spritefontcache.cpp */
244void SetUnicodeGlyph(FontSize size, char32_t key, SpriteID sprite);
245
246#endif /* FONTCACHE_H */
Enum-as-bit-set wrapper.
Factory for FontCaches.
Definition fontcache.h:220
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:59
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
bool HasParent()
Check whether the font cache has a parent.
Definition fontcache.h:141
virtual int GetFontSize() const
Get the nominal font size of the font.
Definition fontcache.h:77
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:114
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:65
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:130
FontSize GetSize() const
Get the FontSize of the font.
Definition fontcache.h:53
int GetDescender() const
Get the descender value of the font.
Definition fontcache.h:71
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.
The ProviderManager manages a single Provider-type.
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:153
FontType
Different types of font that can be loaded.
Definition fontcache.h:214
@ 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:160
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:196
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
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.
Functions to cache sprites in memory.
Types for strings.
Settings for the four different fonts.
Definition fontcache.h:180
FontCacheSubSetting large
The largest font; mostly used for newspapers.
Definition fontcache.h:183
FontCacheSubSetting mono
The mono space font used for license/readme viewers.
Definition fontcache.h:184
FontCacheSubSetting medium
The normal font size.
Definition fontcache.h:182
bool prefer_sprite
Whether to prefer the built-in sprite font over resizable fonts.
Definition fontcache.h:185
FontCacheSubSetting small
The smallest font; mostly used for zoomed out view.
Definition fontcache.h:181
bool global_aa
Whether to anti alias all font sizes.
Definition fontcache.h:186
Settings for a single font.
Definition fontcache.h:172
std::string font
The name of the font, or path to the font.
Definition fontcache.h:173
uint size
The (requested) size of the font.
Definition fontcache.h:174
const void * os_handle
Optional native OS font info. Only valid during font search.
Definition fontcache.h:176
Data structure describing a sprite.