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/>.
8 /** @file fontcache.cpp Cache for characters from fonts. */
11 #include "fontcache.h"
12 #include "fontdetection.h"
13 #include "blitter/factory.hpp"
14 #include "gfx_layout.h"
15 #include "fontcache/spritefontcache.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"
23 #include "safeguards.h"
25 /** Default heights for the different sizes of fonts. */
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};
29 FontCacheSettings _fcsettings
;
32 * Create a new font cache.
33 * @param fs The size of the font.
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
])
38 assert(this->parent
== nullptr || this->fs
== this->parent
->fs
);
39 FontCache::caches
[this->fs
] = this;
40 Layouter::ResetFontCache(this->fs
);
43 /** Clean everything up. */
44 FontCache::~FontCache()
46 assert(this->fs
== this->parent
->fs
);
47 FontCache::caches
[this->fs
] = this->parent
;
48 Layouter::ResetFontCache(this->fs
);
51 int FontCache::GetDefaultFontHeight(FontSize fs
)
53 return _default_font_height
[fs
];
57 * Get the font name of a given font size.
58 * @param fs The font size to look up.
59 * @return The font name.
61 std::string
FontCache::GetName(FontSize fs
)
63 FontCache
*fc
= FontCache::Get(fs
);
65 return fc
->GetFontName();
73 * Get height of a character for a given font size.
74 * @param size Font size to get height of
75 * @return Height of characters in the given font (pixels)
77 int GetCharacterHeight(FontSize size
)
79 return FontCache::Get(size
)->GetHeight();
83 /* static */ FontCache
*FontCache::caches
[FS_END
];
85 /* static */ void FontCache::InitializeFontCaches()
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. */
92 /* Check if a glyph should be rendered with anti-aliasing. */
95 /* AA is only supported for 32 bpp */
96 if (BlitterFactory::GetCurrentBlitter()->GetScreenDepth() != 32) return false;
98 return _fcsettings
.global_aa
;
101 void SetFont(FontSize fontsize
, const std::string
&font
, uint size
)
103 FontCacheSubSetting
*setting
= GetFontCacheSubSetting(fontsize
);
104 bool changed
= false;
106 if (setting
->font
!= font
) {
107 setting
->font
= font
;
111 if (setting
->size
!= size
) {
112 setting
->size
= size
;
116 if (!changed
) return;
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() : "";
126 CheckForMissingGlyphs();
127 _fcsettings
= backup
;
132 LoadStringWidthTable();
133 UpdateAllVirtCoords();
134 ReInitAllWindows(true);
136 if (_save_config
) SaveToConfig();
140 extern void LoadFreeTypeFont(FontSize fs
);
141 extern void UninitFreeType();
142 #elif defined(_WIN32)
143 extern void LoadWin32Font(FontSize fs
);
144 #elif defined(WITH_COCOA)
145 extern void LoadCoreTextFont(FontSize fs
);
149 * Test if a font setting uses the default font.
150 * @return true iff the font is not configured and no fallback font data is present.
152 static bool IsDefaultFont(const FontCacheSubSetting
&setting
)
154 return setting
.font
.empty() && setting
.os_handle
== nullptr;
158 * Get the scalable font size to use for a FontSize.
159 * @param fs FontSize to get the scalable font size for.
160 * @return Scalable font size to use.
162 uint
GetFontCacheFontSize(FontSize fs
)
164 const FontCacheSubSetting
&setting
= *GetFontCacheSubSetting(fs
);
165 return IsDefaultFont(setting
) ? FontCache::GetDefaultFontHeight(fs
) : setting
.size
;
168 #if defined(WITH_FREETYPE) || defined(_WIN32) || defined(WITH_COCOA)
170 * Get name of default font file for a given font size.
171 * @param fs Font size.
172 * @return Name of default font file.
174 static std::string
GetDefaultTruetypeFont(FontSize 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();
184 #endif /* defined(WITH_FREETYPE) || defined(_WIN32) || defined(WITH_COCOA) */
187 * Get path of default font file for a given font size.
188 * @param fs Font size.
189 * @return Full path of default font file.
191 static std::string
GetDefaultTruetypeFontFile([[maybe_unused
]] FontSize fs
)
193 #if defined(WITH_FREETYPE) || defined(_WIN32) || defined(WITH_COCOA)
194 /* Find font file. */
195 return FioFindFullPath(BASESET_DIR
, GetDefaultTruetypeFont(fs
));
198 #endif /* defined(WITH_FREETYPE) || defined(_WIN32) || defined(WITH_COCOA) */
202 * Get font to use for a given font size.
203 * @param fs Font size.
204 * @return If configured, the font name to use, or the path of the default TrueType font if sprites are not preferred.
206 std::string
GetFontCacheFontName(FontSize fs
)
208 const FontCacheSubSetting
*settings
= GetFontCacheSubSetting(fs
);
209 if (!settings
->font
.empty()) return settings
->font
;
210 if (_fcsettings
.prefer_sprite
) return {};
211 return GetDefaultTruetypeFontFile(fs
);
215 * (Re)initialize the font cache related things, i.e. load the non-sprite fonts.
216 * @param monospace Whether to initialise the monospace or regular fonts.
218 void InitFontCache(bool monospace
)
220 FontCache::InitializeFontCaches();
222 for (FontSize fs
= FS_BEGIN
; fs
< FS_END
; fs
++) {
223 if (monospace
!= (fs
== FS_MONO
)) continue;
225 FontCache
*fc
= FontCache::Get(fs
);
226 if (fc
->HasParent()) delete fc
;
229 LoadFreeTypeFont(fs
);
230 #elif defined(_WIN32)
232 #elif defined(WITH_COCOA)
233 LoadCoreTextFont(fs
);
239 * Free everything allocated w.r.t. fonts.
241 void UninitFontCache()
243 for (FontSize fs
= FS_BEGIN
; fs
< FS_END
; fs
++) {
244 FontCache
*fc
= FontCache::Get(fs
);
245 if (fc
->HasParent()) delete fc
;
250 #endif /* WITH_FREETYPE */
253 #if !defined(_WIN32) && !defined(__APPLE__) && !defined(WITH_FONTCONFIG) && !defined(WITH_COCOA)
255 bool SetFallbackFont(FontCacheSettings
*, const std::string
&, int, MissingGlyphSearcher
*) { return false; }
256 #endif /* !defined(_WIN32) && !defined(__APPLE__) && !defined(WITH_FONTCONFIG) && !defined(WITH_COCOA) */