OpenTTD Source  14.0-beta3
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 
26 static const int _default_font_height[FS_END] = {10, 6, 18, 10};
27 static const int _default_font_ascender[FS_END] = { 8, 5, 15, 8};
28 
29 FontCacheSettings _fcsettings;
30 
35 FontCache::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  units_per_em(1)
38 {
39  assert(this->parent == nullptr || this->fs == this->parent->fs);
40  FontCache::caches[this->fs] = this;
41  Layouter::ResetFontCache(this->fs);
42 }
43 
46 {
47  assert(this->fs == this->parent->fs);
48  FontCache::caches[this->fs] = this->parent;
50 }
51 
52 int FontCache::GetDefaultFontHeight(FontSize fs)
53 {
54  return _default_font_height[fs];
55 }
56 
63 {
65  if (fc != nullptr) {
66  return fc->GetFontName();
67  } else {
68  return "[NULL]";
69  }
70 }
71 
72 
79 {
80  return FontCache::Get(size)->GetHeight();
81 }
82 
83 
84 /* static */ FontCache *FontCache::caches[FS_END];
85 
86 /* static */ void FontCache::InitializeFontCaches()
87 {
88  for (FontSize fs = FS_BEGIN; fs != FS_END; fs++) {
89  if (FontCache::caches[fs] == nullptr) new SpriteFontCache(fs); /* FontCache inserts itself into to the cache. */
90  }
91 }
92 
93 /* Check if a glyph should be rendered with anti-aliasing. */
94 bool GetFontAAState(FontSize size, bool check_blitter)
95 {
96  /* AA is only supported for 32 bpp */
97  if (check_blitter && BlitterFactory::GetCurrentBlitter()->GetScreenDepth() != 32) return false;
98 
99  return _fcsettings.global_aa || GetFontCacheSubSetting(size)->aa;
100 }
101 
102 void SetFont(FontSize fontsize, const std::string &font, uint size, bool aa)
103 {
104  FontCacheSubSetting *setting = GetFontCacheSubSetting(fontsize);
105  bool changed = false;
106 
107  if (setting->font != font) {
108  setting->font = font;
109  changed = true;
110  }
111 
112  if (setting->size != size) {
113  setting->size = size;
114  changed = true;
115  }
116 
117  if (setting->aa != aa) {
118  setting->aa = aa;
119  changed = true;
120  }
121 
122  if (!changed) return;
123 
124  if (fontsize != FS_MONO) {
125  /* Try to reload only the modified font. */
126  FontCacheSettings backup = _fcsettings;
127  for (FontSize fs = FS_BEGIN; fs < FS_END; fs++) {
128  if (fs == fontsize) continue;
129  FontCache *fc = FontCache::Get(fs);
130  GetFontCacheSubSetting(fs)->font = fc->HasParent() ? fc->GetFontName() : "";
131  }
133  _fcsettings = backup;
134  } else {
135  InitFontCache(true);
136  }
137 
140  ReInitAllWindows(true);
141 
142  if (_save_config) SaveToConfig();
143 }
144 
145 #ifdef WITH_FREETYPE
146 extern void LoadFreeTypeFont(FontSize fs);
147 extern void LoadFreeTypeFont(FontSize fs, const std::string &file_name, uint size);
148 extern void UninitFreeType();
149 #elif defined(_WIN32)
150 extern void LoadWin32Font(FontSize fs);
151 extern void LoadWin32Font(FontSize fs, const std::string &file_name, uint size);
152 #elif defined(WITH_COCOA)
153 extern void LoadCoreTextFont(FontSize fs);
154 extern void LoadCoreTextFont(FontSize fs, const std::string &file_name, uint size);
155 #endif
156 
157 static void TryLoadDefaultTrueTypeFont([[maybe_unused]] FontSize fs)
158 {
159 #if defined(WITH_FREETYPE) || defined(_WIN32) || defined(WITH_COCOA)
160  std::string font_name{};
161  switch (fs) {
162  case FS_NORMAL:
163  font_name = "OpenTTD-Sans.ttf";
164  break;
165  case FS_SMALL:
166  font_name = "OpenTTD-Small.ttf";
167  break;
168  case FS_LARGE:
169  font_name = "OpenTTD-Serif.ttf";
170  break;
171  case FS_MONO:
172  font_name = "OpenTTD-Mono.ttf";
173  break;
174 
175  default: NOT_REACHED();
176  }
177 
178  /* Find font file. */
179  std::string full_font = FioFindFullPath(BASESET_DIR, font_name);
180  if (!full_font.empty()) {
181  int size = FontCache::GetDefaultFontHeight(fs);
182 #ifdef WITH_FREETYPE
183  LoadFreeTypeFont(fs, full_font, size);
184 #elif defined(_WIN32)
185  LoadWin32Font(fs, full_font, size);
186 #elif defined(WITH_COCOA)
187  LoadCoreTextFont(fs, full_font, size);
188 #endif
189  }
190 #endif /* defined(WITH_FREETYPE) || defined(_WIN32) || defined(WITH_COCOA) */
191 }
192 
197 void InitFontCache(bool monospace)
198 {
199  FontCache::InitializeFontCaches();
200 
201  for (FontSize fs = FS_BEGIN; fs < FS_END; fs++) {
202  if (monospace != (fs == FS_MONO)) continue;
203 
204  FontCache *fc = FontCache::Get(fs);
205  if (fc->HasParent()) delete fc;
206 
207  if (!_fcsettings.prefer_sprite && GetFontCacheSubSetting(fs)->font.empty()) {
208  TryLoadDefaultTrueTypeFont(fs);
209  } else {
210 #ifdef WITH_FREETYPE
211  LoadFreeTypeFont(fs);
212 #elif defined(_WIN32)
213  LoadWin32Font(fs);
214 #elif defined(WITH_COCOA)
215  LoadCoreTextFont(fs);
216 #endif
217  }
218  }
219 }
220 
225 {
226  for (FontSize fs = FS_BEGIN; fs < FS_END; fs++) {
227  FontCache *fc = FontCache::Get(fs);
228  if (fc->HasParent()) delete fc;
229  }
230 
231 #ifdef WITH_FREETYPE
232  UninitFreeType();
233 #endif /* WITH_FREETYPE */
234 }
235 
241 {
242  for (FontSize fs = FS_BEGIN; fs < FS_END; fs++) {
243  if (!FontCache::Get(fs)->IsBuiltInFont() && GetFontAAState(fs, false)) return true;
244  }
245 
246  return false;
247 }
248 
249 #if !defined(_WIN32) && !defined(__APPLE__) && !defined(WITH_FONTCONFIG) && !defined(WITH_COCOA)
250 
251 bool SetFallbackFont(FontCacheSettings *, const std::string &, int, MissingGlyphSearcher *) { return false; }
252 #endif /* !defined(_WIN32) && !defined(__APPLE__) && !defined(WITH_FONTCONFIG) && !defined(WITH_COCOA) */
LoadStringWidthTable
void LoadStringWidthTable(bool monospace)
Initialize _stringwidth_table cache.
Definition: gfx.cpp:1233
factory.hpp
_default_font_height
static const int _default_font_height[FS_END]
Default heights for the different sizes of fonts.
Definition: fontcache.cpp:26
MissingGlyphSearcher
A searcher for missing glyphs.
Definition: strings_func.h:115
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
FontCacheSubSetting
Settings for a single font.
Definition: fontcache.h:207
FontCache::GetHeight
int GetHeight() const
Get the height of the font.
Definition: fontcache.h:49
spritefontcache.h
FS_BEGIN
@ FS_BEGIN
First font.
Definition: gfx_type.h:209
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:159
BASESET_DIR
@ BASESET_DIR
Subdirectory for all base data (base sets, intro game)
Definition: fileio_type.h:116
SaveToConfig
void SaveToConfig()
Save the values to the configuration file.
Definition: settings.cpp:1453
FontCacheSubSetting::aa
bool aa
Whether to do anti aliasing or not.
Definition: fontcache.h:210
FS_LARGE
@ FS_LARGE
Index of the large font in the font tables.
Definition: gfx_type.h:205
fileio_func.h
Layouter::ResetFontCache
static void ResetFontCache(FontSize size)
Reset cached font information.
Definition: gfx_layout.cpp:351
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:209
UninitFontCache
void UninitFontCache()
Free everything allocated w.r.t.
Definition: fontcache.cpp:224
FontCacheSettings
Settings for the four different fonts.
Definition: fontcache.h:216
FontCache::HasParent
bool HasParent()
Check whether the font cache has a parent.
Definition: fontcache.h:155
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:386
ReInitAllWindows
void ReInitAllWindows(bool zoom_changed)
Re-initialize all windows.
Definition: window.cpp:3316
FontCacheSubSetting::font
std::string font
The name of the font, or path to the font.
Definition: fontcache.h:208
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:240
InitFontCache
void InitFontCache(bool monospace)
(Re)initialize the font cache related things, i.e.
Definition: fontcache.cpp:197
FontCacheSettings::prefer_sprite
bool prefer_sprite
Whether to prefer the built-in sprite font over resizable fonts.
Definition: fontcache.h:221
BlitterFactory::GetCurrentBlitter
static Blitter * GetCurrentBlitter()
Get the current active blitter (always set by calling SelectBlitter).
Definition: factory.hpp:138
FontCache::~FontCache
virtual ~FontCache()
Clean everything up.
Definition: fontcache.cpp:45
safeguards.h
UninitFreeType
void UninitFreeType()
Free everything allocated w.r.t.
Definition: freetypefontcache.cpp:347
fontdetection.h
GetFontCacheSubSetting
FontCacheSubSetting * GetFontCacheSubSetting(FontSize fs)
Get the settings of a given font size.
Definition: fontcache.h:232
gfx_layout.h
FontCache::fs
const FontSize fs
The size of the font.
Definition: fontcache.h:25
stdafx.h
viewport_func.h
FontCache::GetName
static std::string GetName(FontSize fs)
Get the font name of a given font size.
Definition: fontcache.cpp:62
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
UpdateAllVirtCoords
void UpdateAllVirtCoords()
Update the viewport coordinates of all signs.
Definition: afterload.cpp:222
FontCache::parent
FontCache * parent
The parent of this font cache.
Definition: fontcache.h:24
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:2268
GetCharacterHeight
int GetCharacterHeight(FontSize size)
Get height of a character for a given font size.
Definition: fontcache.cpp:78
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:341
LoadFreeTypeFont
void LoadFreeTypeFont(FontSize fs)
Loads the freetype font.
Definition: freetypefontcache.cpp:161
FontCacheSettings::global_aa
bool global_aa
Whether to anti alias all font sizes.
Definition: fontcache.h:222
FontCache::FontCache
FontCache(FontSize fs)
Create a new font cache.
Definition: fontcache.cpp:35
FontCache::Get
static FontCache * Get(FontSize fs)
Get the font cache of a given font size.
Definition: fontcache.h:144