OpenTTD Source 20241224-master-gf74b0cf984
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 "fontdetection.h"
13#include "blitter/factory.hpp"
14#include "gfx_layout.h"
16#include "openttd.h"
17#include "settings_func.h"
18#include "strings_func.h"
19#include "viewport_func.h"
20#include "window_func.h"
21#include "fileio_func.h"
22
23#include "safeguards.h"
24
26static const int _default_font_height[FS_END] = {10, 6, 18, 10};
27static const int _default_font_ascender[FS_END] = { 8, 5, 15, 8};
28
29FontCacheSettings _fcsettings;
30
35FontCache::FontCache(FontSize fs) : parent(FontCache::Get(fs)), fs(fs), height(_default_font_height[fs]),
36 ascender(_default_font_ascender[fs]), descender(_default_font_ascender[fs] - _default_font_height[fs])
37{
38 assert(this->parent == nullptr || this->fs == this->parent->fs);
39 FontCache::caches[this->fs] = this;
41}
42
45{
46 assert(this->fs == this->parent->fs);
47 FontCache::caches[this->fs] = this->parent;
49}
50
51int FontCache::GetDefaultFontHeight(FontSize fs)
52{
54}
55
62{
64 if (fc != nullptr) {
65 return fc->GetFontName();
66 } else {
67 return "[NULL]";
68 }
69}
70
71
78{
79 return FontCache::Get(size)->GetHeight();
80}
81
82
83/* static */ FontCache *FontCache::caches[FS_END];
84
85/* static */ void FontCache::InitializeFontCaches()
86{
87 for (FontSize fs = FS_BEGIN; fs != FS_END; fs++) {
88 if (FontCache::caches[fs] == nullptr) new SpriteFontCache(fs); /* FontCache inserts itself into to the cache. */
89 }
90}
91
92/* Check if a glyph should be rendered with anti-aliasing. */
93bool GetFontAAState()
94{
95 /* AA is only supported for 32 bpp */
96 if (BlitterFactory::GetCurrentBlitter()->GetScreenDepth() != 32) return false;
97
98 return _fcsettings.global_aa;
99}
100
101void SetFont(FontSize fontsize, const std::string &font, uint size)
102{
103 FontCacheSubSetting *setting = GetFontCacheSubSetting(fontsize);
104 bool changed = false;
105
106 if (setting->font != font) {
107 setting->font = font;
108 changed = true;
109 }
110
111 if (setting->size != size) {
112 setting->size = size;
113 changed = true;
114 }
115
116 if (!changed) return;
117
118 if (fontsize != FS_MONO) {
119 /* Try to reload only the modified font. */
120 FontCacheSettings backup = _fcsettings;
121 for (FontSize fs = FS_BEGIN; fs < FS_END; fs++) {
122 if (fs == fontsize) continue;
123 FontCache *fc = FontCache::Get(fs);
124 GetFontCacheSubSetting(fs)->font = fc->HasParent() ? fc->GetFontName() : "";
125 }
127 _fcsettings = backup;
128 } else {
129 InitFontCache(true);
130 }
131
134 ReInitAllWindows(true);
135
136 if (_save_config) SaveToConfig();
137}
138
139#ifdef WITH_FREETYPE
140extern void LoadFreeTypeFont(FontSize fs);
141extern void UninitFreeType();
142#elif defined(_WIN32)
143extern void LoadWin32Font(FontSize fs);
144#elif defined(WITH_COCOA)
145extern void LoadCoreTextFont(FontSize fs);
146#endif
147
152static bool IsDefaultFont(const FontCacheSubSetting &setting)
153{
154 return setting.font.empty() && setting.os_handle == nullptr;
155}
156
163{
164 const FontCacheSubSetting &setting = *GetFontCacheSubSetting(fs);
165 return IsDefaultFont(setting) ? FontCache::GetDefaultFontHeight(fs) : setting.size;
166}
167
168#if defined(WITH_FREETYPE) || defined(_WIN32) || defined(WITH_COCOA)
174static std::string GetDefaultTruetypeFont(FontSize fs)
175{
176 switch (fs) {
177 case FS_NORMAL: return "OpenTTD-Sans.ttf";
178 case FS_SMALL: return "OpenTTD-Small.ttf";
179 case FS_LARGE: return "OpenTTD-Serif.ttf";
180 case FS_MONO: return "OpenTTD-Mono.ttf";
181 default: NOT_REACHED();
182 }
183}
184#endif /* defined(WITH_FREETYPE) || defined(_WIN32) || defined(WITH_COCOA) */
185
191static std::string GetDefaultTruetypeFontFile([[maybe_unused]] FontSize fs)
192{
193#if defined(WITH_FREETYPE) || defined(_WIN32) || defined(WITH_COCOA)
194 /* Find font file. */
196#else
197 return {};
198#endif /* defined(WITH_FREETYPE) || defined(_WIN32) || defined(WITH_COCOA) */
199}
200
207{
209 if (!settings->font.empty()) return settings->font;
210 if (_fcsettings.prefer_sprite) return {};
212}
213
218void InitFontCache(bool monospace)
219{
220 FontCache::InitializeFontCaches();
221
222 for (FontSize fs = FS_BEGIN; fs < FS_END; fs++) {
223 if (monospace != (fs == FS_MONO)) continue;
224
225 FontCache *fc = FontCache::Get(fs);
226 if (fc->HasParent()) delete fc;
227
228#ifdef WITH_FREETYPE
230#elif defined(_WIN32)
231 LoadWin32Font(fs);
232#elif defined(WITH_COCOA)
234#endif
235 }
236}
237
242{
243 for (FontSize fs = FS_BEGIN; fs < FS_END; fs++) {
244 FontCache *fc = FontCache::Get(fs);
245 if (fc->HasParent()) delete fc;
246 }
247
248#ifdef WITH_FREETYPE
250#endif /* WITH_FREETYPE */
251}
252
253#if !defined(_WIN32) && !defined(__APPLE__) && !defined(WITH_FONTCONFIG) && !defined(WITH_COCOA)
254
255bool SetFallbackFont(FontCacheSettings *, const std::string &, int, MissingGlyphSearcher *) { return false; }
256#endif /* !defined(_WIN32) && !defined(__APPLE__) && !defined(WITH_FONTCONFIG) && !defined(WITH_COCOA) */
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:138
Font cache for basic fonts.
Definition fontcache.h:21
virtual std::string GetFontName()=0
Get the name of this font.
int GetHeight() const
Get the height of the font.
Definition fontcache.h:48
FontCache * parent
The parent of this font cache.
Definition fontcache.h:24
bool HasParent()
Check whether the font cache has a parent.
Definition fontcache.h:140
static FontCache * caches[FS_END]
All the font caches.
Definition fontcache.h:23
FontCache(FontSize fs)
Create a new font cache.
Definition fontcache.cpp:35
virtual ~FontCache()
Clean everything up.
Definition fontcache.cpp:44
const FontSize fs
The size of the font.
Definition fontcache.h:25
static std::string GetName(FontSize fs)
Get the font name of a given font size.
Definition fontcache.cpp:61
static FontCache * Get(FontSize fs)
Get the font cache of a given font size.
Definition fontcache.h:129
static void ResetFontCache(FontSize size)
Reset cached font information.
A searcher for missing glyphs.
Font cache for fonts that are based on a freetype font.
Factory to 'query' all available blitters.
std::string FioFindFullPath(Subdirectory subdir, const std::string &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)
fluid_settings_t * settings
FluidSynth settings handle.
void LoadCoreTextFont(FontSize fs)
Loads the TrueType font.
Definition font_osx.cpp:329
void LoadWin32Font(FontSize fs)
Loads the GDI font.
void InitFontCache(bool monospace)
(Re)initialize the font cache related things, i.e.
void UninitFreeType()
Free everything allocated w.r.t.
void LoadFreeTypeFont(FontSize fs)
Loads the freetype font.
static const int _default_font_height[FS_END]
Default heights for the different sizes of fonts.
Definition fontcache.cpp:26
void UninitFontCache()
Free everything allocated w.r.t.
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:77
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.
FontCacheSubSetting * GetFontCacheSubSetting(FontSize fs)
Get the settings of a given font size.
Definition fontcache.h:216
Functions related to detecting/finding the right font.
bool SetFallbackFont(struct FontCacheSettings *settings, const std::string &language_isocode, int winlangid, class MissingGlyphSearcher *callback)
We would like to have a fallback font as the current one doesn't contain all characters we need.
Definition font_osx.cpp:27
void LoadStringWidthTable(bool monospace)
Initialize _stringwidth_table cache.
Definition gfx.cpp:1210
Functions related to laying out the texts.
FontSize
Available font sizes.
Definition gfx_type.h:208
@ FS_MONO
Index of the monospaced font in the font tables.
Definition gfx_type.h:212
@ FS_SMALL
Index of the small font in the font tables.
Definition gfx_type.h:210
@ FS_BEGIN
First font.
Definition gfx_type.h:215
@ FS_NORMAL
Index of the normal font in the font tables.
Definition gfx_type.h:209
@ FS_LARGE
Index of the large font in the font tables.
Definition gfx_type.h:211
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.
Sprite font cache implementation definition.
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:2291
Functions related to OTTD's strings.
Settings for the four different fonts.
Definition fontcache.h:200
bool prefer_sprite
Whether to prefer the built-in sprite font over resizable fonts.
Definition fontcache.h:205
bool global_aa
Whether to anti alias all font sizes.
Definition fontcache.h:206
Settings for a single font.
Definition fontcache.h:192
std::string font
The name of the font, or path to the font.
Definition fontcache.h:193
uint size
The (requested) size of the font.
Definition fontcache.h:194
const void * os_handle
Optional native OS font info. Only valid during font search.
Definition fontcache.h:196
Functions related to (drawing on) viewports.
void ReInitAllWindows(bool zoom_changed)
Re-initialize all windows.
Definition window.cpp:3335
Window functions not directly related to making/drawing windows.