OpenTTD Source  13.0-beta2
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 
17 #include "safeguards.h"
18 
20 static const int _default_font_height[FS_END] = {10, 6, 18, 10};
21 static const int _default_font_ascender[FS_END] = { 8, 5, 15, 8};
22 
23 FontCacheSettings _fcsettings;
24 
29 FontCache::FontCache(FontSize fs) : parent(FontCache::Get(fs)), fs(fs), height(_default_font_height[fs]),
30  ascender(_default_font_ascender[fs]), descender(_default_font_ascender[fs] - _default_font_height[fs]),
31  units_per_em(1)
32 {
33  assert(this->parent == nullptr || this->fs == this->parent->fs);
34  FontCache::caches[this->fs] = this;
35  Layouter::ResetFontCache(this->fs);
36 }
37 
40 {
41  assert(this->fs == this->parent->fs);
42  FontCache::caches[this->fs] = this->parent;
44 }
45 
46 int FontCache::GetDefaultFontHeight(FontSize fs)
47 {
48  return _default_font_height[fs];
49 }
50 
51 
58 {
59  return FontCache::Get(size)->GetHeight();
60 }
61 
62 
64 
65 
66 
67 /* Check if a glyph should be rendered with anti-aliasing. */
68 bool GetFontAAState(FontSize size, bool check_blitter)
69 {
70  /* AA is only supported for 32 bpp */
71  if (check_blitter && BlitterFactory::GetCurrentBlitter()->GetScreenDepth() != 32) return false;
72 
73  switch (size) {
74  default: NOT_REACHED();
75  case FS_NORMAL: return _fcsettings.medium.aa;
76  case FS_SMALL: return _fcsettings.small.aa;
77  case FS_LARGE: return _fcsettings.large.aa;
78  case FS_MONO: return _fcsettings.mono.aa;
79  }
80 }
81 
82 
87 void InitFontCache(bool monospace)
88 {
89  for (FontSize fs = FS_BEGIN; fs < FS_END; fs++) {
90  if (monospace != (fs == FS_MONO)) continue;
91 
92  FontCache *fc = FontCache::Get(fs);
93  if (fc->HasParent()) delete fc;
94 
95 #ifdef WITH_FREETYPE
96  extern void LoadFreeTypeFont(FontSize fs);
97  LoadFreeTypeFont(fs);
98 #elif defined(_WIN32)
99  extern void LoadWin32Font(FontSize fs);
100  LoadWin32Font(fs);
101 #elif defined(WITH_COCOA)
102  extern void LoadCoreTextFont(FontSize fs);
103  LoadCoreTextFont(fs);
104 #endif
105  }
106 }
107 
112 {
113  for (FontSize fs = FS_BEGIN; fs < FS_END; fs++) {
114  FontCache *fc = FontCache::Get(fs);
115  if (fc->HasParent()) delete fc;
116  }
117 
118 #ifdef WITH_FREETYPE
119  extern void UninitFreeType();
120  UninitFreeType();
121 #endif /* WITH_FREETYPE */
122 }
123 
129 {
130  for (FontSize fs = FS_BEGIN; fs < FS_END; fs++) {
131  if (!FontCache::Get(fs)->IsBuiltInFont() && GetFontAAState(fs, false)) return true;
132  }
133 
134  return false;
135 }
136 
137 #if !defined(_WIN32) && !defined(__APPLE__) && !defined(WITH_FONTCONFIG) && !defined(WITH_COCOA)
138 
139 bool SetFallbackFont(FontCacheSettings *settings, const char *language_isocode, int winlangid, MissingGlyphSearcher *callback) { return false; }
140 #endif /* !defined(_WIN32) && !defined(__APPLE__) && !defined(WITH_FONTCONFIG) && !defined(WITH_COCOA) */
factory.hpp
_default_font_height
static const int _default_font_height[FS_END]
Default heights for the different sizes of fonts.
Definition: fontcache.cpp:20
MissingGlyphSearcher
A searcher for missing glyphs.
Definition: strings_func.h:243
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:213
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:209
Layouter::ResetFontCache
static void ResetFontCache(FontSize size)
Reset cached font information.
Definition: gfx_layout.cpp:865
SpriteFontCache
Font cache for fonts that are based on a freetype font.
Definition: spritefontcache.h:17
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:111
UninitFreeType
void UninitFreeType()
Free everything allocated w.r.t.
Definition: freetypefontcache.cpp:320
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
FS_NORMAL
@ FS_NORMAL
Index of the normal font in the font tables.
Definition: gfx_type.h:207
LoadWin32Font
void LoadWin32Font(FontSize fs)
Loads the GDI font.
Definition: font_win32.cpp:581
FS_SMALL
@ FS_SMALL
Index of the small font in the font tables.
Definition: gfx_type.h:208
HasAntialiasedFonts
bool HasAntialiasedFonts()
Should any of the active fonts be anti-aliased?
Definition: fontcache.cpp:128
InitFontCache
void InitFontCache(bool monospace)
(Re)initialize the font cache related things, i.e.
Definition: fontcache.cpp:87
FontCacheSettings::large
FontCacheSubSetting large
The largest font; mostly used for newspapers.
Definition: fontcache.h:215
BlitterFactory::GetCurrentBlitter
static Blitter * GetCurrentBlitter()
Get the current active blitter (always set by calling SelectBlitter).
Definition: factory.hpp:141
FontCache::~FontCache
virtual ~FontCache()
Clean everything up.
Definition: fontcache.cpp:39
safeguards.h
FontCacheSettings::medium
FontCacheSubSetting medium
The normal font size.
Definition: fontcache.h:214
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
FontCache::caches
static FontCache * caches[FS_END]
All the font caches.
Definition: fontcache.h:23
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
FontCache::parent
FontCache * parent
The parent of this font cache.
Definition: fontcache.h:25
FontCacheSettings::mono
FontCacheSubSetting mono
The mono space font used for license/readme viewers.
Definition: fontcache.h:216
GetCharacterHeight
int GetCharacterHeight(FontSize size)
Get height of a character for a given font size.
Definition: fontcache.cpp:57
FS_MONO
@ FS_MONO
Index of the monospaced font in the font tables.
Definition: gfx_type.h:210
fontcache.h
FontSize
FontSize
Available font sizes.
Definition: gfx_type.h:206
LoadCoreTextFont
void LoadCoreTextFont(FontSize fs)
Loads the TrueType font.
Definition: font_osx.cpp:351
FontCacheSettings::small
FontCacheSubSetting small
The smallest font; mostly used for zoomed out view.
Definition: fontcache.h:213
FontCache::FontCache
FontCache(FontSize fs)
Create a new font cache.
Definition: fontcache.cpp:29
FontCache::Get
static FontCache * Get(FontSize fs)
Get the font cache of a given font size.
Definition: fontcache.h:142