OpenTTD Source: src/fontcache.cpp Source File

OpenTTD Source 15.0-beta1

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"
15#include "fontcache/spritefontcache.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
26 static const int _default_font_height[FS_END] = {10, 6, 18, 10};
27static const int _default_font_ascender[FS_END] = { 8, 5, 15, 8};
28
29 FontCacheSettings _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;
40 Layouter::ResetFontCache(this->fs);
41}
42
44FontCache::~FontCache()
45{
46 assert(this->fs == this->parent->fs);
47 FontCache::caches[this->fs] = this->parent;
48 Layouter::ResetFontCache(this->fs);
49}
50
51int FontCache::GetDefaultFontHeight(FontSize fs)
52{
53 return _default_font_height[fs];
54}
55
61std::string FontCache::GetName(FontSize fs)
62{
63 FontCache *fc = FontCache::Get(fs);
64 if (fc != nullptr) {
65 return fc->GetFontName();
66 } else {
67 return "[NULL]";
68 }
69}
70
71
77 int GetCharacterHeight(FontSize size)
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 }
126 CheckForMissingGlyphs();
127 _fcsettings = backup;
128 } else {
129 InitFontCache(true);
130 }
131
132 LoadStringWidthTable();
133 UpdateAllVirtCoords();
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
152 static bool IsDefaultFont(const FontCacheSubSetting &setting)
153{
154 return setting.font.empty() && setting.os_handle == nullptr;
155}
156
162uint GetFontCacheFontSize(FontSize fs)
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)
174 static 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
191 static std::string GetDefaultTruetypeFontFile([[maybe_unused]] FontSize fs)
192{
193#if defined(WITH_FREETYPE) || defined(_WIN32) || defined(WITH_COCOA)
194 /* Find font file. */
195 return FioFindFullPath(BASESET_DIR, GetDefaultTruetypeFont(fs));
196#else
197 return {};
198#endif /* defined(WITH_FREETYPE) || defined(_WIN32) || defined(WITH_COCOA) */
199}
200
206std::string GetFontCacheFontName(FontSize fs)
207{
208 const FontCacheSubSetting *settings = GetFontCacheSubSetting(fs);
209 if (!settings->font.empty()) return settings->font;
210 if (_fcsettings.prefer_sprite) return {};
211 return GetDefaultTruetypeFontFile(fs);
212}
213
218 void 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
229 LoadFreeTypeFont(fs);
230#elif defined(_WIN32)
231 LoadWin32Font(fs);
232#elif defined(WITH_COCOA)
233 LoadCoreTextFont(fs);
234#endif
235 }
236}
237
241 void UninitFontCache()
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
249 UninitFreeType();
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) */
UpdateAllVirtCoords
void UpdateAllVirtCoords()
Update the viewport coordinates of all signs.
Definition afterload.cpp:224
BlitterFactory::GetCurrentBlitter
static Blitter * GetCurrentBlitter()
Get the current active blitter (always set by calling SelectBlitter).
Definition factory.hpp:138
FontCache
Font cache for basic fonts.
Definition fontcache.h:21
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:48
FontCache::parent
FontCache * parent
The parent of this font cache.
Definition fontcache.h:24
FontCache::HasParent
bool HasParent()
Check whether the font cache has a parent.
Definition fontcache.h:140
FontCache::caches
static FontCache * caches[FS_END]
All the font caches.
Definition fontcache.h:23
FontCache::FontCache
FontCache(FontSize fs)
Create a new font cache.
Definition fontcache.cpp:35
FontCache::~FontCache
virtual ~FontCache()
Clean everything up.
Definition fontcache.cpp:44
FontCache::fs
const FontSize fs
The size of the font.
Definition fontcache.h:25
FontCache::GetName
static std::string GetName(FontSize fs)
Get the font name of a given font size.
Definition fontcache.cpp:61
FontCache::Get
static FontCache * Get(FontSize fs)
Get the font cache of a given font size.
Definition fontcache.h:129
Layouter::ResetFontCache
static void ResetFontCache(FontSize size)
Reset cached font information.
Definition gfx_layout.cpp:353
MissingGlyphSearcher
A searcher for missing glyphs.
Definition strings_func.h:117
SpriteFontCache
Font cache for fonts that are based on a freetype font.
Definition spritefontcache.h:17
factory.hpp
Factory to 'query' all available blitters.
FioFindFullPath
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
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:123
settings
fluid_settings_t * settings
FluidSynth settings handle.
Definition fluidsynth.cpp:21
LoadCoreTextFont
void LoadCoreTextFont(FontSize fs)
Loads the TrueType font.
Definition font_osx.cpp:329
LoadWin32Font
void LoadWin32Font(FontSize fs)
Loads the GDI font.
Definition font_win32.cpp:352
InitFontCache
void InitFontCache(bool monospace)
(Re)initialize the font cache related things, i.e.
Definition fontcache.cpp:218
UninitFreeType
void UninitFreeType()
Free everything allocated w.r.t.
Definition freetypefontcache.cpp:303
LoadFreeTypeFont
void LoadFreeTypeFont(FontSize fs)
Loads the freetype font.
Definition freetypefontcache.cpp:159
_default_font_height
static const int _default_font_height[FS_END]
Default heights for the different sizes of fonts.
Definition fontcache.cpp:26
UninitFontCache
void UninitFontCache()
Free everything allocated w.r.t.
Definition fontcache.cpp:241
GetDefaultTruetypeFontFile
static std::string GetDefaultTruetypeFontFile(FontSize fs)
Get path of default font file for a given font size.
Definition fontcache.cpp:191
GetFontCacheFontSize
uint GetFontCacheFontSize(FontSize fs)
Get the scalable font size to use for a FontSize.
Definition fontcache.cpp:162
IsDefaultFont
static bool IsDefaultFont(const FontCacheSubSetting &setting)
Test if a font setting uses the default font.
Definition fontcache.cpp:152
GetCharacterHeight
int GetCharacterHeight(FontSize size)
Get height of a character for a given font size.
Definition fontcache.cpp:77
GetFontCacheFontName
std::string GetFontCacheFontName(FontSize fs)
Get font to use for a given font size.
Definition fontcache.cpp:206
GetDefaultTruetypeFont
static std::string GetDefaultTruetypeFont(FontSize fs)
Get name of default font file for a given font size.
Definition fontcache.cpp:174
fontcache.h
Functions to read fonts from files and cache them.
GetFontCacheSubSetting
FontCacheSubSetting * GetFontCacheSubSetting(FontSize fs)
Get the settings of a given font size.
Definition fontcache.h:216
fontdetection.h
Functions related to detecting/finding the right font.
SetFallbackFont
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
LoadStringWidthTable
void LoadStringWidthTable(bool monospace)
Initialize _stringwidth_table cache.
Definition gfx.cpp:1210
gfx_layout.h
Functions related to laying out the texts.
FontSize
FontSize
Available font sizes.
Definition gfx_type.h:208
FS_MONO
@ FS_MONO
Index of the monospaced font in the font tables.
Definition gfx_type.h:212
FS_SMALL
@ FS_SMALL
Index of the small font in the font tables.
Definition gfx_type.h:210
FS_BEGIN
@ FS_BEGIN
First font.
Definition gfx_type.h:215
FS_NORMAL
@ FS_NORMAL
Index of the normal font in the font tables.
Definition gfx_type.h:209
FS_LARGE
@ FS_LARGE
Index of the large font in the font tables.
Definition gfx_type.h:211
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:1447
settings_func.h
Functions related to setting/changing the settings.
spritefontcache.h
Sprite font cache implementation definition.
stdafx.h
Definition of base types and functions in a cross-platform compatible way.
CheckForMissingGlyphs
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
strings_func.h
Functions related to OTTD's strings.
FontCacheSettings
Settings for the four different fonts.
Definition fontcache.h:200
FontCacheSettings::prefer_sprite
bool prefer_sprite
Whether to prefer the built-in sprite font over resizable fonts.
Definition fontcache.h:205
FontCacheSettings::global_aa
bool global_aa
Whether to anti alias all font sizes.
Definition fontcache.h:206
FontCacheSubSetting
Settings for a single font.
Definition fontcache.h:192
FontCacheSubSetting::font
std::string font
The name of the font, or path to the font.
Definition fontcache.h:193
FontCacheSubSetting::size
uint size
The (requested) size of the font.
Definition fontcache.h:194
FontCacheSubSetting::os_handle
const void * os_handle
Optional native OS font info. Only valid during font search.
Definition fontcache.h:196
viewport_func.h
Functions related to (drawing on) viewports.
ReInitAllWindows
void ReInitAllWindows(bool zoom_changed)
Re-initialize all windows.
Definition window.cpp:3335
window_func.h
Window functions not directly related to making/drawing windows.