OpenTTD Source: src/fontcache.h Source File

OpenTTD Source 15.3

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
18 typedef uint32_t GlyphID;
19static const GlyphID SPRITE_GLYPH = 1U << 30;
20
22 class 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:
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
153 inline const Sprite *GetGlyph(FontSize size, char32_t key)
154{
155 FontCache *fc = FontCache::Get(size);
156 return fc->GetGlyph(fc->MapCharToGlyph(key));
157}
158
160 inline 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
172 struct FontCacheSubSetting {
173 std::string font;
174 uint size;
175
176 const void *os_handle = nullptr;
177};
178
180 struct FontCacheSettings {
181 FontCacheSubSetting small;
182 FontCacheSubSetting medium;
183 FontCacheSubSetting large;
184 FontCacheSubSetting mono;
185 bool prefer_sprite;
186 bool global_aa;
187};
188
189extern FontCacheSettings _fcsettings;
190
196 inline FontCacheSubSetting *GetFontCacheSubSetting(FontSize fs)
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
207uint GetFontCacheFontSize(FontSize fs);
208std::string GetFontCacheFontName(FontSize fs);
209
210bool GetFontAAState();
211void SetFont(FontSize fontsize, const std::string &font, uint size);
212
214 enum class FontType : uint8_t {
215 Sprite,
216 TrueType,
217};
218
220 class FontCacheFactory : public BaseProvider<FontCacheFactory> {
221public:
222 FontCacheFactory(std::string_view name, std::string_view description) : BaseProvider<FontCacheFactory>(name, description)
223 {
224 ProviderManager<FontCacheFactory>::Register(*this);
225 }
226
227 virtual ~FontCacheFactory()
228 {
229 ProviderManager<FontCacheFactory>::Unregister(*this);
230 }
231
232 virtual std::unique_ptr<FontCache> LoadFont(FontSize fs, FontType fonttype) const = 0;
233 virtual bool FindFallbackFont(struct FontCacheSettings *settings, const std::string &language_isocode, class MissingGlyphSearcher *callback) const = 0;
234};
235
236 class 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 */
243void InitializeUnicodeGlyphMap();
244void SetUnicodeGlyph(FontSize size, char32_t key, SpriteID sprite);
245
246#endif /* FONTCACHE_H */
BaseProvider
Definition provider_manager.h:54
EnumBitSet
Enum-as-bit-set wrapper.
Definition enum_type.hpp:177
FontCacheFactory
Factory for FontCaches.
Definition fontcache.h:220
FontCache
Font cache for basic fonts.
Definition fontcache.h:22
FontCache::GetFontName
virtual std::string GetFontName()=0
Get the name of this font.
FontCache::GetHeight
int GetHeight() const
Get the height of the font.
Definition fontcache.h:59
FontCache::ClearFontCache
virtual void ClearFontCache()=0
Clear the font cache.
FontCache::UninitializeFontCaches
static void UninitializeFontCaches()
Free everything allocated w.r.t.
Definition fontcache.cpp:267
FontCache::height
int height
The height of the font.
Definition fontcache.h:27
FontCache::HasParent
bool HasParent()
Check whether the font cache has a parent.
Definition fontcache.h:141
FontCache::GetFontSize
virtual int GetFontSize() const
Get the nominal font size of the font.
Definition fontcache.h:77
FontCache::parent
std::unique_ptr< FontCache > parent
The parent of this font cache.
Definition fontcache.h:25
FontCache::GetOSHandle
virtual const void * GetOSHandle()
Get the native OS font handle, if there is one.
Definition fontcache.h:114
FontCache::MapCharToGlyph
virtual GlyphID MapCharToGlyph(char32_t key, bool fallback=true)=0
Map a character into a glyph.
FontCache::caches
static std::array< std::unique_ptr< FontCache >, FS_END > caches
All the font caches.
Definition fontcache.h:93
FontCache::IsBuiltInFont
virtual bool IsBuiltInFont()=0
Is this a built-in sprite font?
FontCache::GetGlyph
virtual const Sprite * GetGlyph(GlyphID key)=0
Get the glyph (sprite) of the given key.
FontCache::ClearFontCaches
static void ClearFontCaches(FontSizes fontsizes)
Clear cached information for the specified font caches.
Definition fontcache.cpp:257
FontCache::GetGlyphWidth
virtual uint GetGlyphWidth(GlyphID key)=0
Get the width of the glyph with the given key.
FontCache::Register
static void Register(std::unique_ptr< FontCache > &&fc)
Register a FontCache for its font size.
Definition fontcache.cpp:223
FontCache::fs
const FontSize fs
The size of the font.
Definition fontcache.h:26
FontCache::GetName
static std::string GetName(FontSize fs)
Get the font name of a given font size.
Definition fontcache.cpp:71
FontCache::GetAscender
int GetAscender() const
Get the ascender value of the font.
Definition fontcache.h:65
FontCache::DEFAULT_FONT_ASCENDER
static const int DEFAULT_FONT_ASCENDER[FS_END]
Default unscaled font ascenders.
Definition fontcache.h:26
FontCache::DEFAULT_FONT_HEIGHT
static const int DEFAULT_FONT_HEIGHT[FS_END]
Default unscaled font heights.
Definition fontcache.h:24
FontCache::LoadFontCaches
static void LoadFontCaches(FontSizes fontsizes)
(Re)initialize the font cache related things, i.e.
Definition fontcache.cpp:237
FontCache::descender
int descender
The descender value of the font.
Definition fontcache.h:29
FontCache::Get
static FontCache * Get(FontSize fs)
Get the font cache of a given font size.
Definition fontcache.h:130
FontCache::GetSize
FontSize GetSize() const
Get the FontSize of the font.
Definition fontcache.h:53
FontCache::GetDescender
int GetDescender() const
Get the descender value of the font.
Definition fontcache.h:71
FontCache::GetDrawGlyphShadow
virtual bool GetDrawGlyphShadow()=0
Do we need to draw a glyph shadow?
FontCache::InitializeFontCaches
static void InitializeFontCaches()
Initialise font caches with the base sprite font cache for all sizes.
Definition fontcache.cpp:98
FontCache::ascender
int ascender
The ascender value of the font.
Definition fontcache.h:28
FontProviderManager
Definition fontcache.h:236
FontProviderManager::LoadFont
static std::unique_ptr< FontCache > LoadFont(FontSize fs, FontType fonttype)
Try loading a font with any fontcache factory.
Definition fontcache.cpp:36
FontProviderManager::FindFallbackFont
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
MissingGlyphSearcher
A searcher for missing glyphs.
Definition strings_func.h:156
ProviderManager
The ProviderManager manages a single Provider-type.
Definition provider_manager.h:21
settings
fluid_settings_t * settings
FluidSynth settings handle.
Definition fluidsynth.cpp:25
SetUnicodeGlyph
void SetUnicodeGlyph(FontSize size, char32_t key, SpriteID sprite)
Set the SpriteID for a unicode character.
Definition spritefontcache.cpp:56
GetGlyph
const Sprite * GetGlyph(FontSize size, char32_t key)
Get the Sprite for a glyph.
Definition fontcache.h:153
FontType
FontType
Different types of font that can be loaded.
Definition fontcache.h:214
FontType::TrueType
@ TrueType
Scalable TrueType fonts.
FontType::Sprite
@ Sprite
Bitmap sprites from GRF files.
GetGlyphWidth
uint GetGlyphWidth(FontSize size, char32_t key)
Get the width of a glyph.
Definition fontcache.h:160
InitializeUnicodeGlyphMap
void InitializeUnicodeGlyphMap()
Initialize the glyph map.
Definition spritefontcache.cpp:106
GetFontCacheFontSize
uint GetFontCacheFontSize(FontSize fs)
Get the scalable font size to use for a FontSize.
Definition fontcache.cpp:167
GetFontCacheSubSetting
FontCacheSubSetting * GetFontCacheSubSetting(FontSize fs)
Get the settings of a given font size.
Definition fontcache.h:196
GetFontCacheFontName
std::string GetFontCacheFontName(FontSize fs)
Get font to use for a given font size.
Definition fontcache.cpp:211
GlyphID
uint32_t GlyphID
Glyphs are characters from a font.
Definition fontcache.h:18
gfx_type.h
Types related to the graphics and/or input devices.
SpriteID
uint32_t SpriteID
The number of a sprite, without mapping bits and colourtables.
Definition gfx_type.h:17
FontSize
FontSize
Available font sizes.
Definition gfx_type.h:248
FS_MONO
@ FS_MONO
Index of the monospaced font in the font tables.
Definition gfx_type.h:252
FS_SMALL
@ FS_SMALL
Index of the small font in the font tables.
Definition gfx_type.h:250
FS_NORMAL
@ FS_NORMAL
Index of the normal font in the font tables.
Definition gfx_type.h:249
FS_LARGE
@ FS_LARGE
Index of the large font in the font tables.
Definition gfx_type.h:251
provider_manager.h
Definition of the ProviderManager.
spritecache_type.h
Types related to the sprite cache.
FontCacheSettings
Settings for the four different fonts.
Definition fontcache.h:180
FontCacheSettings::large
FontCacheSubSetting large
The largest font; mostly used for newspapers.
Definition fontcache.h:183
FontCacheSettings::mono
FontCacheSubSetting mono
The mono space font used for license/readme viewers.
Definition fontcache.h:184
FontCacheSettings::medium
FontCacheSubSetting medium
The normal font size.
Definition fontcache.h:182
FontCacheSettings::prefer_sprite
bool prefer_sprite
Whether to prefer the built-in sprite font over resizable fonts.
Definition fontcache.h:185
FontCacheSettings::small
FontCacheSubSetting small
The smallest font; mostly used for zoomed out view.
Definition fontcache.h:181
FontCacheSettings::global_aa
bool global_aa
Whether to anti alias all font sizes.
Definition fontcache.h:186
FontCacheSubSetting
Settings for a single font.
Definition fontcache.h:172
FontCacheSubSetting::font
std::string font
The name of the font, or path to the font.
Definition fontcache.h:173
FontCacheSubSetting::size
uint size
The (requested) size of the font.
Definition fontcache.h:174
FontCacheSubSetting::os_handle
const void * os_handle
Optional native OS font info. Only valid during font search.
Definition fontcache.h:176
Sprite
Data structure describing a sprite.
Definition spritecache_type.h:16