OpenTTD Source 20260421-master-gc2fbc6fdeb
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 <any>
14#include "gfx_type.h"
15#include "provider_manager.h"
16#include "spritecache_type.h"
17
19typedef uint32_t GlyphID;
20static const GlyphID SPRITE_GLYPH = 1U << 30;
21
23class FontCache {
24protected:
26 std::unique_ptr<FontCache> parent;
27 const FontSize fs;
28 int height = 0;
29 int ascender = 0;
30 int descender = 0;
31
32 FontCache(FontSize fs) : fs(fs) {}
33 static void Register(std::unique_ptr<FontCache> &&fc);
34
35public:
37 virtual ~FontCache() = default;
38
39 static void InitializeFontCaches();
40 static void UninitializeFontCaches();
41 static void LoadFontCaches(FontSizes fontsizes);
42 static void ClearFontCaches(FontSizes fontsizes);
43
48
49 static int GetDefaultFontHeight(FontSize fs);
50
51 static void AddFallback(FontSizes fontsizes, std::string_view name, const std::any &os_handle = {});
52
53 static bool TryFallback(FontSizes fontsizes, const std::set<char32_t> &glyphs, const std::string &name, const std::any &os_handle = {});
54
59 inline FontSize GetSize() const { return this->fs; }
60
65 inline int GetHeight() const { return this->height; }
66
71 inline int GetAscender() const { return this->ascender; }
72
77 inline int GetDescender() const{ return this->descender; }
78
83 virtual int GetFontSize() const { return this->height; }
84
86 virtual void ClearFontCache() = 0;
87
93 virtual const Sprite *GetGlyph(GlyphID key) = 0;
100 virtual uint GetGlyphWidth(GlyphID key) = 0;
101
106 virtual bool GetDrawGlyphShadow() = 0;
107
114 virtual GlyphID MapCharToGlyph(char32_t key, bool fallback = true) = 0;
115
120 virtual const void *GetOSHandle()
121 {
122 return nullptr;
123 }
124
129 virtual std::string GetFontName() = 0;
130
136 static inline FontCache *Get(FontSize fs)
137 {
138 assert(fs < FontSize::End);
139 return FontCache::caches[fs].get();
140 }
141
142 static std::string GetName(FontSize fs);
143
148 inline bool HasParent()
149 {
150 return this->parent != nullptr;
151 }
152
157 virtual bool IsBuiltInFont() = 0;
158};
159
166inline const Sprite *GetGlyph(FontSize size, char32_t key)
167{
168 FontCache *fc = FontCache::Get(size);
169 return fc->GetGlyph(fc->MapCharToGlyph(key));
170}
171
178inline uint GetGlyphWidth(FontSize size, char32_t key)
179{
180 FontCache *fc = FontCache::Get(size);
181 return fc->GetGlyphWidth(fc->MapCharToGlyph(key));
182}
183
184inline bool GetDrawGlyphShadow(FontSize size)
185{
186 return FontCache::Get(size)->GetDrawGlyphShadow();
187}
188
191 std::string font;
192 uint size;
193
194 std::any os_handle;
195};
196
206
207extern FontCacheSettings _fcsettings;
208
215{
216 switch (fs) {
217 default: NOT_REACHED();
218 case FontSize::Small: return &_fcsettings.small;
219 case FontSize::Normal: return &_fcsettings.medium;
220 case FontSize::Large: return &_fcsettings.large;
221 case FontSize::Monospace: return &_fcsettings.mono;
222 }
223}
224
226
227bool GetFontAAState();
228void SetFont(FontSize fontsize, const std::string &font, uint size);
229
231enum class FontType : uint8_t {
234};
235
237class FontCacheFactory : public BaseProvider<FontCacheFactory> {
238public:
239 FontCacheFactory(std::string_view name, std::string_view description) : BaseProvider<FontCacheFactory>(name, description)
240 {
241 ProviderManager<FontCacheFactory>::Register(*this);
242 }
243
246 {
247 ProviderManager<FontCacheFactory>::Unregister(*this);
248 }
249
259 virtual std::unique_ptr<FontCache> LoadFont(FontSize fs, FontType fonttype, bool search, const std::string &font_name, const std::any &os_handle) const = 0;
260
268 virtual bool FindFallbackFont(const std::string &language_isocode, class MissingGlyphSearcher *callback) const = 0;
269};
270
271class FontProviderManager : ProviderManager<FontCacheFactory> {
272public:
273 static std::unique_ptr<FontCache> LoadFont(FontSize fs, FontType fonttype, bool search, const std::string &font_name, const std::any &os_handle = {});
274 static bool FindFallbackFont(const std::string &language_isocode, MissingGlyphSearcher *callback);
275};
276
277/* Implemented in spritefontcache.cpp */
279void SetUnicodeGlyph(FontSize size, char32_t key, SpriteID sprite);
280
281#endif /* FONTCACHE_H */
constexpr BaseProvider(std::string_view name, std::string_view description)
const std::string_view description
A sort-of mixin that implements 'at(pos)' and 'operator[](pos)' only for a specific enum class.
virtual std::unique_ptr< FontCache > LoadFont(FontSize fs, FontType fonttype, bool search, const std::string &font_name, const std::any &os_handle) const =0
Try loading a font with this factory.
~FontCacheFactory() override
Unregister this factory.
Definition fontcache.h:245
virtual bool FindFallbackFont(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:23
virtual std::string GetFontName()=0
Get the name of this font.
int GetHeight() const
Get the height of the font.
Definition fontcache.h:65
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:28
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:148
virtual int GetFontSize() const
Get the nominal font size of the font.
Definition fontcache.h:83
std::unique_ptr< FontCache > parent
The parent of this font cache.
Definition fontcache.h:26
virtual const void * GetOSHandle()
Get the native OS font handle, if there is one.
Definition fontcache.h:120
virtual GlyphID MapCharToGlyph(char32_t key, bool fallback=true)=0
Map a character into a glyph.
static const EnumClassIndexContainer< std::array< int, to_underlying(FontSize::End)>, FontSize > DEFAULT_FONT_ASCENDER
Default unscaled font ascenders.
Definition fontcache.h:26
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 const EnumClassIndexContainer< std::array< int, to_underlying(FontSize::End)>, FontSize > DEFAULT_FONT_HEIGHT
Default unscaled font heights.
Definition fontcache.h:24
static void AddFallback(FontSizes fontsizes, std::string_view name, const std::any &os_handle={})
Add a fallback font, with optional OS-specific handle.
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:27
static std::string GetName(FontSize fs)
Get the font name of a given font size.
Definition fontcache.cpp:72
int GetAscender() const
Get the ascender value of the font.
Definition fontcache.h:71
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:30
static FontCache * Get(FontSize fs)
Get the font cache of a given font size.
Definition fontcache.h:136
FontSize GetSize() const
Get the FontSize of the font.
Definition fontcache.h:59
int GetDescender() const
Get the descender value of the font.
Definition fontcache.h:77
static bool TryFallback(FontSizes fontsizes, const std::set< char32_t > &glyphs, const std::string &name, const std::any &os_handle={})
Test a fallback font, with optional OS-specific handle, for specific glyphs.
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:99
static EnumClassIndexContainer< std::array< std::unique_ptr< FontCache >, to_underlying(FontSize::End)>, FontSize > caches
All the font caches.
Definition fontcache.h:94
int ascender
The ascender value of the font.
Definition fontcache.h:29
static std::unique_ptr< FontCache > LoadFont(FontSize fs, FontType fonttype, bool search, const std::string &font_name, const std::any &os_handle={})
Try loading a font with any fontcache factory.
Definition fontcache.cpp:39
static bool FindFallbackFont(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:56
A searcher for missing glyphs.
constexpr std::underlying_type_t< enum_type > to_underlying(enum_type e)
Implementation of std::to_underlying (from C++23).
Definition enum_type.hpp:21
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:166
FontType
Different types of font that can be loaded.
Definition fontcache.h:231
@ TrueType
Scalable TrueType fonts.
Definition fontcache.h:233
uint GetGlyphWidth(FontSize size, char32_t key)
Get the width of a glyph.
Definition fontcache.h:178
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:214
uint32_t GlyphID
Glyphs are characters from a font.
Definition fontcache.h:19
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
@ Small
Index of the small font in the font tables.
Definition gfx_type.h:250
@ Large
Index of the large font in the font tables.
Definition gfx_type.h:251
@ End
Marker for the end of the enumerations.
Definition gfx_type.h:254
@ Normal
Index of the normal font in the font tables.
Definition gfx_type.h:249
@ Monospace
Index of the monospaced font in the font tables.
Definition gfx_type.h:252
Definition of the ProviderManager.
Types related to the sprite cache.
Settings for the four different fonts.
Definition fontcache.h:198
FontCacheSubSetting large
The largest font; mostly used for newspapers.
Definition fontcache.h:201
FontCacheSubSetting mono
The mono space font used for license/readme viewers.
Definition fontcache.h:202
FontCacheSubSetting medium
The normal font size.
Definition fontcache.h:200
bool prefer_sprite
Whether to prefer the built-in sprite font over resizable fonts.
Definition fontcache.h:203
FontCacheSubSetting small
The smallest font; mostly used for zoomed out view.
Definition fontcache.h:199
bool global_aa
Whether to anti alias all font sizes.
Definition fontcache.h:204
Settings for a single font.
Definition fontcache.h:190
std::any os_handle
Optional native OS font info.
Definition fontcache.h:194
std::string font
The name of the font, or path to the font.
Definition fontcache.h:191
uint size
The (requested) size of the font.
Definition fontcache.h:192
Data structure describing a sprite.