OpenTTD Source: src/fontcache.cpp Source File

OpenTTD Source 13.2.1

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
22 #include "safeguards.h"
23
25 static const int _default_font_height[FS_END] = {10, 6, 18, 10};
26 static const int _default_font_ascender[FS_END] = { 8, 5, 15, 8};
27
28 FontCacheSettings _fcsettings;
29
34 FontCache::FontCache(FontSize fs) : parent(FontCache::Get(fs)), fs(fs), height(_default_font_height[fs]),
35 ascender(_default_font_ascender[fs]), descender(_default_font_ascender[fs] - _default_font_height[fs]),
36 units_per_em(1)
37 {
38 assert(this->parent == nullptr || this->fs == this->parent->fs);
39 FontCache::caches[this->fs] = this;
40 Layouter::ResetFontCache(this->fs);
41 }
42
44 FontCache::~FontCache()
45 {
46 assert(this->fs == this->parent->fs);
47 FontCache::caches[this->fs] = this->parent;
48 Layouter::ResetFontCache(this->fs);
49 }
50
51 int FontCache::GetDefaultFontHeight(FontSize fs)
52 {
53 return _default_font_height[fs];
54 }
55
56
62 int GetCharacterHeight(FontSize size)
63 {
64 return FontCache::Get(size)->GetHeight();
65 }
66
67
68 /* static */ FontCache *FontCache::caches[FS_END] = { new SpriteFontCache(FS_NORMAL), new SpriteFontCache(FS_SMALL), new SpriteFontCache(FS_LARGE), new SpriteFontCache(FS_MONO) };
69
70
71
72 /* Check if a glyph should be rendered with anti-aliasing. */
73 bool GetFontAAState(FontSize size, bool check_blitter)
74 {
75 /* AA is only supported for 32 bpp */
76 if (check_blitter && BlitterFactory::GetCurrentBlitter()->GetScreenDepth() != 32) return false;
77
78 return GetFontCacheSubSetting(size)->aa;
79 }
80
81 void SetFont(FontSize fontsize, const std::string& font, uint size, bool aa)
82 {
83 FontCacheSubSetting *setting = GetFontCacheSubSetting(fontsize);
84 bool changed = false;
85
86 if (setting->font != font) {
87 setting->font = font;
88 changed = true;
89 }
90
91 if (setting->size != size) {
92 setting->size = size;
93 changed = true;
94 }
95
96 if (setting->aa != aa) {
97 setting->aa = aa;
98 changed = true;
99 }
100
101 if (!changed) return;
102
103 if (fontsize != FS_MONO) {
104 /* Try to reload only the modified font. */
105 FontCacheSettings backup = _fcsettings;
106 for (FontSize fs = FS_BEGIN; fs < FS_END; fs++) {
107 if (fs == fontsize) continue;
108 FontCache *fc = FontCache::Get(fs);
109 GetFontCacheSubSetting(fs)->font = fc->HasParent() ? fc->GetFontName() : "";
110 }
111 CheckForMissingGlyphs();
112 _fcsettings = backup;
113 } else {
114 InitFontCache(true);
115 }
116
117 LoadStringWidthTable();
118 UpdateAllVirtCoords();
119 ReInitAllWindows(true);
120
121 if (_save_config) SaveToConfig();
122 }
123
128 void InitFontCache(bool monospace)
129 {
130 for (FontSize fs = FS_BEGIN; fs < FS_END; fs++) {
131 if (monospace != (fs == FS_MONO)) continue;
132
133 FontCache *fc = FontCache::Get(fs);
134 if (fc->HasParent()) delete fc;
135
136 #ifdef WITH_FREETYPE
137 extern void LoadFreeTypeFont(FontSize fs);
138 LoadFreeTypeFont(fs);
139 #elif defined(_WIN32)
140 extern void LoadWin32Font(FontSize fs);
141 LoadWin32Font(fs);
142 #elif defined(WITH_COCOA)
143 extern void LoadCoreTextFont(FontSize fs);
144 LoadCoreTextFont(fs);
145 #endif
146 }
147 }
148
152 void UninitFontCache()
153 {
154 for (FontSize fs = FS_BEGIN; fs < FS_END; fs++) {
155 FontCache *fc = FontCache::Get(fs);
156 if (fc->HasParent()) delete fc;
157 }
158
159 #ifdef WITH_FREETYPE
160 extern void UninitFreeType();
161 UninitFreeType();
162 #endif /* WITH_FREETYPE */
163 }
164
169 bool HasAntialiasedFonts()
170 {
171 for (FontSize fs = FS_BEGIN; fs < FS_END; fs++) {
172 if (!FontCache::Get(fs)->IsBuiltInFont() && GetFontAAState(fs, false)) return true;
173 }
174
175 return false;
176 }
177
178 #if !defined(_WIN32) && !defined(__APPLE__) && !defined(WITH_FONTCONFIG) && !defined(WITH_COCOA)
179
180 bool SetFallbackFont(FontCacheSettings *settings, const char *language_isocode, int winlangid, MissingGlyphSearcher *callback) { return false; }
181 #endif /* !defined(_WIN32) && !defined(__APPLE__) && !defined(WITH_FONTCONFIG) && !defined(WITH_COCOA) */
LoadStringWidthTable
void LoadStringWidthTable(bool monospace)
Initialize _stringwidth_table cache.
Definition: gfx.cpp:1446
factory.hpp
_default_font_height
static const int _default_font_height[FS_END]
Default heights for the different sizes of fonts.
Definition: fontcache.cpp:25
MissingGlyphSearcher
A searcher for missing glyphs.
Definition: strings_func.h:243
FontCacheSubSetting
Settings for a single font.
Definition: fontcache.h:203
FontCache::GetHeight
int GetHeight() const
Get the height of the font.
Definition: fontcache.h:48
spritefontcache.h
FS_BEGIN
@ FS_BEGIN
First font.
Definition: gfx_type.h:209
SaveToConfig
void SaveToConfig()
Save the values to the configuration file.
Definition: settings.cpp:1260
FontCacheSubSetting::aa
bool aa
Whether to do anti aliasing or not.
Definition: fontcache.h:206
FS_LARGE
@ FS_LARGE
Index of the large font in the font tables.
Definition: gfx_type.h:205
Layouter::ResetFontCache
static void ResetFontCache(FontSize size)
Reset cached font information.
Definition: gfx_layout.cpp:864
SpriteFontCache
Font cache for fonts that are based on a freetype font.
Definition: spritefontcache.h:17
FontCacheSubSetting::size
uint size
The (requested) size of the font.
Definition: fontcache.h:205
LoadFreeTypeFont
void LoadFreeTypeFont(FontSize fs)
Loads the freetype font.
Definition: freetypefontcache.cpp:123
UninitFontCache
void UninitFontCache()
Free everything allocated w.r.t.
Definition: fontcache.cpp:152
UninitFreeType
void UninitFreeType()
Free everything allocated w.r.t.
Definition: freetypefontcache.cpp:312
FontCacheSettings
Settings for the four different fonts.
Definition: fontcache.h:212
FontCache::HasParent
bool HasParent()
Check whether the font cache has a parent.
Definition: fontcache.h:151
settings_func.h
FS_NORMAL
@ FS_NORMAL
Index of the normal font in the font tables.
Definition: gfx_type.h:203
LoadWin32Font
void LoadWin32Font(FontSize fs)
Loads the GDI font.
Definition: font_win32.cpp:582
ReInitAllWindows
void ReInitAllWindows(bool zoom_changed)
Re-initialize all windows.
Definition: window.cpp:3377
FontCacheSubSetting::font
std::string font
The name of the font, or path to the font.
Definition: fontcache.h:204
FS_SMALL
@ FS_SMALL
Index of the small font in the font tables.
Definition: gfx_type.h:204
HasAntialiasedFonts
bool HasAntialiasedFonts()
Should any of the active fonts be anti-aliased?
Definition: fontcache.cpp:169
InitFontCache
void InitFontCache(bool monospace)
(Re)initialize the font cache related things, i.e.
Definition: fontcache.cpp:128
BlitterFactory::GetCurrentBlitter
static Blitter * GetCurrentBlitter()
Get the current active blitter (always set by calling SelectBlitter).
Definition: factory.hpp:141
GetFontCacheSubSetting
static FontCacheSubSetting * GetFontCacheSubSetting(FontSize fs)
Get the settings of a given font size.
Definition: fontcache.h:226
FontCache::~FontCache
virtual ~FontCache()
Clean everything up.
Definition: fontcache.cpp:44
safeguards.h
settings
fluid_settings_t * settings
FluidSynth settings handle.
Definition: fluidsynth.cpp:21
fontdetection.h
gfx_layout.h
FontCache::fs
const FontSize fs
The size of the font.
Definition: fontcache.h:26
stdafx.h
viewport_func.h
FontCache::caches
static FontCache * caches[FS_END]
All the font caches.
Definition: fontcache.h:23
strings_func.h
FontCache::GetFontName
virtual std::string GetFontName()=0
Get the name of this font.
FontCache
Font cache for basic fonts.
Definition: fontcache.h:21
SetFallbackFont
bool SetFallbackFont(struct FontCacheSettings *settings, const char *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:70
UpdateAllVirtCoords
void UpdateAllVirtCoords()
Update the viewport coordinates of all signs.
Definition: afterload.cpp:218
FontCache::parent
FontCache * parent
The parent of this font cache.
Definition: fontcache.h:25
openttd.h
window_func.h
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:2158
GetCharacterHeight
int GetCharacterHeight(FontSize size)
Get height of a character for a given font size.
Definition: fontcache.cpp:62
FS_MONO
@ FS_MONO
Index of the monospaced font in the font tables.
Definition: gfx_type.h:206
fontcache.h
FontSize
FontSize
Available font sizes.
Definition: gfx_type.h:202
LoadCoreTextFont
void LoadCoreTextFont(FontSize fs)
Loads the TrueType font.
Definition: font_osx.cpp:351
FontCache::FontCache
FontCache(FontSize fs)
Create a new font cache.
Definition: fontcache.cpp:34
FontCache::Get
static FontCache * Get(FontSize fs)
Get the font cache of a given font size.
Definition: fontcache.h:142