OpenTTD Source: src/fontcache.cpp Source File

OpenTTD Source 15.0-RC2

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 <https://www.gnu.org/licenses/old-licenses/gpl-2.0>.
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
28 FontCacheSettings _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{
63 return FontCache::DEFAULT_FONT_HEIGHT[fs];
64}
65
71std::string FontCache::GetName(FontSize fs)
72{
73 FontCache *fc = FontCache::Get(fs);
74 if (fc != nullptr) {
75 return fc->GetFontName();
76 } else {
77 return "[NULL]";
78 }
79}
80
81
87 int GetCharacterHeight(FontSize size)
88{
89 return FontCache::Get(size)->GetHeight();
90}
91
92
93/* static */ std::array<std::unique_ptr<FontCache>, FS_END> FontCache::caches{};
94
98/* static */ void FontCache::InitializeFontCaches()
99{
100 for (FontSize fs = FS_BEGIN; fs != FS_END; fs++) {
101 if (FontCache::Get(fs) != nullptr) continue;
102 FontCache::Register(FontProviderManager::LoadFont(fs, FontType::Sprite));
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 }
140 CheckForMissingGlyphs();
141 _fcsettings = std::move(backup);
142 } else {
143 FontCache::LoadFontCaches(fontsize);
144 }
145
146 LoadStringWidthTable(fontsize);
147 UpdateAllVirtCoords();
148 ReInitAllWindows(true);
149
150 if (_save_config) SaveToConfig();
151}
152
157 static bool IsDefaultFont(const FontCacheSubSetting &setting)
158{
159 return setting.font.empty() && setting.os_handle == nullptr;
160}
161
167uint GetFontCacheFontSize(FontSize fs)
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)
179 static 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
196 static std::string GetDefaultTruetypeFontFile([[maybe_unused]] FontSize fs)
197{
198#if defined(WITH_FREETYPE) || defined(_WIN32) || defined(WITH_COCOA)
199 /* Find font file. */
200 return FioFindFullPath(BASESET_DIR, GetDefaultTruetypeFont(fs));
201#else
202 return {};
203#endif /* defined(WITH_FREETYPE) || defined(_WIN32) || defined(WITH_COCOA) */
204}
205
211std::string GetFontCacheFontName(FontSize fs)
212{
213 const FontCacheSubSetting *settings = GetFontCacheSubSetting(fs);
214 if (!settings->font.empty()) return settings->font;
215 if (_fcsettings.prefer_sprite) return {};
216 return GetDefaultTruetypeFontFile(fs);
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{
239 FontCache::InitializeFontCaches();
240
241 for (FontSize fs : fontsizes) {
242 Layouter::ResetFontCache(fs);
243
244 /* Unload everything except the sprite font cache. */
245 while (FontCache::Get(fs)->HasParent()) {
246 FontCache::caches[fs] = std::move(FontCache::caches[fs]->parent);
247 }
248
249 FontCache::Register(FontProviderManager::LoadFont(fs, FontType::TrueType));
250 }
251}
252
257/* static */ void FontCache::ClearFontCaches(FontSizes fontsizes)
258{
259 for (FontSize fs : fontsizes) {
260 FontCache::Get(fs)->ClearFontCache();
261 }
262}
263
267/* static */ void FontCache::UninitializeFontCaches()
268{
269 for (auto &fc : FontCache::caches) {
270 fc.reset();
271 }
272}
UpdateAllVirtCoords
void UpdateAllVirtCoords()
Update the viewport coordinates of all signs.
Definition afterload.cpp:220
BlitterFactory::GetCurrentBlitter
static Blitter * GetCurrentBlitter()
Get the current active blitter (always set by calling SelectBlitter).
Definition factory.hpp:136
EnumBitSet
Enum-as-bit-set wrapper.
Definition enum_type.hpp:177
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::HasParent
bool HasParent()
Check whether the font cache has a parent.
Definition fontcache.h:141
FontCache::parent
std::unique_ptr< FontCache > parent
The parent of this font cache.
Definition fontcache.h:25
FontCache::caches
static std::array< std::unique_ptr< FontCache >, FS_END > caches
All the font caches.
Definition fontcache.h:93
FontCache::ClearFontCaches
static void ClearFontCaches(FontSizes fontsizes)
Clear cached information for the specified font caches.
Definition fontcache.cpp:257
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::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::Get
static FontCache * Get(FontSize fs)
Get the font cache of a given font size.
Definition fontcache.h:130
FontCache::InitializeFontCaches
static void InitializeFontCaches()
Initialise font caches with the base sprite font cache for all sizes.
Definition fontcache.cpp:98
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
Layouter::ResetFontCache
static void ResetFontCache(FontSize size)
Reset cached font information.
Definition gfx_layout.cpp:367
MissingGlyphSearcher
A searcher for missing glyphs.
Definition strings_func.h:156
ProviderManager< FontCacheFactory >::GetProviders
static std::vector< const FontCacheFactory * > & GetProviders()
Get the currently known providers.
Definition provider_manager.h:46
factory.hpp
Factory to 'query' all available blitters.
FioFindFullPath
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
fileio_func.h
Functions for Standard In/Out file operations.
BASESET_DIR
@ BASESET_DIR
Subdirectory for all base data (base sets, intro game)
Definition fileio_type.h:96
settings
fluid_settings_t * settings
FluidSynth settings handle.
Definition fluidsynth.cpp:25
GetDefaultTruetypeFontFile
static std::string GetDefaultTruetypeFontFile(FontSize fs)
Get path of default font file for a given font size.
Definition fontcache.cpp:196
GetFontCacheFontSize
uint GetFontCacheFontSize(FontSize fs)
Get the scalable font size to use for a FontSize.
Definition fontcache.cpp:167
IsDefaultFont
static bool IsDefaultFont(const FontCacheSubSetting &setting)
Test if a font setting uses the default font.
Definition fontcache.cpp:157
GetCharacterHeight
int GetCharacterHeight(FontSize size)
Get height of a character for a given font size.
Definition fontcache.cpp:87
GetFontCacheFontName
std::string GetFontCacheFontName(FontSize fs)
Get font to use for a given font size.
Definition fontcache.cpp:211
GetDefaultTruetypeFont
static std::string GetDefaultTruetypeFont(FontSize fs)
Get name of default font file for a given font size.
Definition fontcache.cpp:179
fontcache.h
Functions to read fonts from files and cache them.
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.
GetFontCacheSubSetting
FontCacheSubSetting * GetFontCacheSubSetting(FontSize fs)
Get the settings of a given font size.
Definition fontcache.h:196
LoadStringWidthTable
void LoadStringWidthTable(FontSizes fontsizes)
Initialize _stringwidth_table cache for the specified font sizes.
Definition gfx.cpp:1253
gfx_layout.h
Functions related to laying out the texts.
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_BEGIN
@ FS_BEGIN
First font.
Definition gfx_type.h:255
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
openttd.h
Some generic types.
safeguards.h
A number of safeguards to prevent using unsafe methods.
SaveToConfig
void SaveToConfig()
Save the values to the configuration file.
Definition settings.cpp:1469
settings_func.h
Functions related to setting/changing the settings.
stdafx.h
Definition of base types and functions in a cross-platform compatible way.
CheckForMissingGlyphs
void CheckForMissingGlyphs(MissingGlyphSearcher *searcher)
Check whether the currently loaded language pack uses characters that the currently loaded font does ...
Definition strings.cpp:2363
strings_func.h
Functions related to OTTD's strings.
FontCacheSettings
Settings for the four different fonts.
Definition fontcache.h:180
FontCacheSettings::prefer_sprite
bool prefer_sprite
Whether to prefer the built-in sprite font over resizable fonts.
Definition fontcache.h:185
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
viewport_func.h
Functions related to (drawing on) viewports.
ReInitAllWindows
void ReInitAllWindows(bool zoom_changed)
Re-initialize all windows.
Definition window.cpp:3410
window_func.h
Window functions not directly related to making/drawing windows.