OpenTTD Source 20250816-master-g94ba9b1454
fontcache.cpp
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#include "stdafx.h"
11#include "fontcache.h"
12#include "blitter/factory.hpp"
13#include "gfx_layout.h"
14#include "openttd.h"
15#include "settings_func.h"
16#include "strings_func.h"
17#include "viewport_func.h"
18#include "window_func.h"
19#include "fileio_func.h"
20
21#include "safeguards.h"
22
24/* static */ const int FontCache::DEFAULT_FONT_HEIGHT[FS_END] = {10, 6, 18, 10};
26/* static */ const int FontCache::DEFAULT_FONT_ASCENDER[FS_END] = {8, 5, 15, 8};
27
28FontCacheSettings _fcsettings;
29
36/* static */ std::unique_ptr<FontCache> FontProviderManager::LoadFont(FontSize fs, FontType fonttype)
37{
38 for (auto &provider : FontProviderManager::GetProviders()) {
39 auto fc = provider->LoadFont(fs, fonttype);
40 if (fc != nullptr) return fc;
41 }
42
43 return nullptr;
44}
45
55/* static */ bool FontProviderManager::FindFallbackFont(FontCacheSettings *settings, const std::string &language_isocode, MissingGlyphSearcher *callback)
56{
57 return std::ranges::any_of(FontProviderManager::GetProviders(),
58 [&](auto *provider) { return provider->FindFallbackFont(settings, language_isocode, callback); });
59}
60
61int FontCache::GetDefaultFontHeight(FontSize fs)
62{
64}
65
72{
74 if (fc != nullptr) {
75 return fc->GetFontName();
76 } else {
77 return "[NULL]";
78 }
79}
80
81
88{
89 return FontCache::Get(size)->GetHeight();
90}
91
92
93/* static */ std::array<std::unique_ptr<FontCache>, FS_END> FontCache::caches{};
94
99{
100 for (FontSize fs = FS_BEGIN; fs != FS_END; fs++) {
101 if (FontCache::Get(fs) != nullptr) continue;
103 }
104}
105
106/* Check if a glyph should be rendered with anti-aliasing. */
107bool GetFontAAState()
108{
109 /* AA is only supported for 32 bpp */
110 if (BlitterFactory::GetCurrentBlitter()->GetScreenDepth() != 32) return false;
111
112 return _fcsettings.global_aa;
113}
114
115void SetFont(FontSize fontsize, const std::string &font, uint size)
116{
117 FontCacheSubSetting *setting = GetFontCacheSubSetting(fontsize);
118 bool changed = false;
119
120 if (setting->font != font) {
121 setting->font = font;
122 changed = true;
123 }
124
125 if (setting->size != size) {
126 setting->size = size;
127 changed = true;
128 }
129
130 if (!changed) return;
131
132 if (fontsize != FS_MONO) {
133 /* Try to reload only the modified font. */
134 FontCacheSettings backup = _fcsettings;
135 for (FontSize fs = FS_BEGIN; fs < FS_END; fs++) {
136 if (fs == fontsize) continue;
137 FontCache *fc = FontCache::Get(fs);
138 GetFontCacheSubSetting(fs)->font = fc->HasParent() ? fc->GetFontName() : "";
139 }
141 _fcsettings = std::move(backup);
142 } else {
144 }
145
146 LoadStringWidthTable(fontsize);
148 ReInitAllWindows(true);
149
150 if (_save_config) SaveToConfig();
151}
152
157static bool IsDefaultFont(const FontCacheSubSetting &setting)
158{
159 return setting.font.empty() && setting.os_handle == nullptr;
160}
161
168{
169 const FontCacheSubSetting &setting = *GetFontCacheSubSetting(fs);
170 return IsDefaultFont(setting) ? FontCache::GetDefaultFontHeight(fs) : setting.size;
171}
172
173#if defined(WITH_FREETYPE) || defined(_WIN32) || defined(WITH_COCOA)
179static std::string GetDefaultTruetypeFont(FontSize fs)
180{
181 switch (fs) {
182 case FS_NORMAL: return "OpenTTD-Sans.ttf";
183 case FS_SMALL: return "OpenTTD-Small.ttf";
184 case FS_LARGE: return "OpenTTD-Serif.ttf";
185 case FS_MONO: return "OpenTTD-Mono.ttf";
186 default: NOT_REACHED();
187 }
188}
189#endif /* defined(WITH_FREETYPE) || defined(_WIN32) || defined(WITH_COCOA) */
190
196static std::string GetDefaultTruetypeFontFile([[maybe_unused]] FontSize fs)
197{
198#if defined(WITH_FREETYPE) || defined(_WIN32) || defined(WITH_COCOA)
199 /* Find font file. */
201#else
202 return {};
203#endif /* defined(WITH_FREETYPE) || defined(_WIN32) || defined(WITH_COCOA) */
204}
205
212{
214 if (!settings->font.empty()) return settings->font;
215 if (_fcsettings.prefer_sprite) return {};
217}
218
223/* static */ void FontCache::Register(std::unique_ptr<FontCache> &&fc)
224{
225 if (fc == nullptr) return;
226
227 FontSize fs = fc->fs;
228
229 fc->parent = std::move(FontCache::caches[fs]);
230 FontCache::caches[fs] = std::move(fc);
231}
232
237/* static */ void FontCache::LoadFontCaches(FontSizes fontsizes)
238{
240
241 for (FontSize fs : fontsizes) {
243
244 /* Unload everything except the sprite font cache. */
245 while (FontCache::Get(fs)->HasParent()) {
247 }
248
250 }
251}
252
257/* static */ void FontCache::ClearFontCaches(FontSizes fontsizes)
258{
259 for (FontSize fs : fontsizes) {
261 }
262}
263
268{
269 for (auto &fc : FontCache::caches) {
270 fc.reset();
271 }
272}
void UpdateAllVirtCoords()
Update the viewport coordinates of all signs.
static Blitter * GetCurrentBlitter()
Get the current active blitter (always set by calling SelectBlitter).
Definition factory.hpp:136
Enum-as-bit-set wrapper.
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.
bool HasParent()
Check whether the font cache has a parent.
Definition fontcache.h:141
std::unique_ptr< FontCache > parent
The parent of this font cache.
Definition fontcache.h:25
static std::array< std::unique_ptr< FontCache >, FS_END > caches
All the font caches.
Definition fontcache.h:93
static void ClearFontCaches(FontSizes fontsizes)
Clear cached information for the specified font caches.
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
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.
static FontCache * Get(FontSize fs)
Get the font cache of a given font size.
Definition fontcache.h:130
static void InitializeFontCaches()
Initialise font caches with the base sprite font cache for all sizes.
Definition fontcache.cpp:98
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
static void ResetFontCache(FontSize size)
Reset cached font information.
A searcher for missing glyphs.
static std::vector< FontCacheFactory * > & GetProviders()
Get the currently known providers.
Factory to 'query' all available blitters.
std::string FioFindFullPath(Subdirectory subdir, std::string_view filename)
Find a path to the filename in one of the search directories.
Definition fileio.cpp:144
Functions for Standard In/Out file operations.
@ BASESET_DIR
Subdirectory for all base data (base sets, intro game)
Definition fileio_type.h:96
fluid_settings_t * settings
FluidSynth settings handle.
static std::string GetDefaultTruetypeFontFile(FontSize fs)
Get path of default font file for a given font size.
uint GetFontCacheFontSize(FontSize fs)
Get the scalable font size to use for a FontSize.
static bool IsDefaultFont(const FontCacheSubSetting &setting)
Test if a font setting uses the default font.
int GetCharacterHeight(FontSize size)
Get height of a character for a given font size.
Definition fontcache.cpp:87
std::string GetFontCacheFontName(FontSize fs)
Get font to use for a given font size.
static std::string GetDefaultTruetypeFont(FontSize fs)
Get name of default font file for a given font size.
Functions to read fonts from files and cache them.
FontType
Different types of font that can be loaded.
Definition fontcache.h:214
@ TrueType
Scalable TrueType fonts.
@ Sprite
Bitmap sprites from GRF files.
FontCacheSubSetting * GetFontCacheSubSetting(FontSize fs)
Get the settings of a given font size.
Definition fontcache.h:196
void LoadStringWidthTable(FontSizes fontsizes)
Initialize _stringwidth_table cache for the specified font sizes.
Definition gfx.cpp:1254
Functions related to laying out the texts.
FontSize
Available font sizes.
Definition gfx_type.h:249
@ FS_MONO
Index of the monospaced font in the font tables.
Definition gfx_type.h:253
@ FS_SMALL
Index of the small font in the font tables.
Definition gfx_type.h:251
@ FS_BEGIN
First font.
Definition gfx_type.h:256
@ FS_NORMAL
Index of the normal font in the font tables.
Definition gfx_type.h:250
@ FS_LARGE
Index of the large font in the font tables.
Definition gfx_type.h:252
Some generic types.
A number of safeguards to prevent using unsafe methods.
void SaveToConfig()
Save the values to the configuration file.
Functions related to setting/changing the settings.
Definition of base types and functions in a cross-platform compatible way.
void CheckForMissingGlyphs(bool base_font, MissingGlyphSearcher *searcher)
Check whether the currently loaded language pack uses characters that the currently loaded font does ...
Definition strings.cpp:2363
Functions related to OTTD's strings.
Settings for the four different fonts.
Definition fontcache.h:180
bool prefer_sprite
Whether to prefer the built-in sprite font over resizable fonts.
Definition fontcache.h:185
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
Functions related to (drawing on) viewports.
void ReInitAllWindows(bool zoom_changed)
Re-initialize all windows.
Definition window.cpp:3383
Window functions not directly related to making/drawing windows.