Feature: Wide rivers
[openttd-github.git] / src / fontcache.cpp
blob91dae57564c5dcaae15539a5ee3790883aff5d68
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 */
8 /** @file fontcache.cpp Cache for characters from fonts. */
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"
17 #include "safeguards.h"
19 /** Default heights for the different sizes of fonts. */
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};
23 FontCacheSettings _fcsettings;
25 /**
26 * Create a new font cache.
27 * @param fs The size of the font.
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)
33 assert(this->parent == nullptr || this->fs == this->parent->fs);
34 FontCache::caches[this->fs] = this;
35 Layouter::ResetFontCache(this->fs);
38 /** Clean everything up. */
39 FontCache::~FontCache()
41 assert(this->fs == this->parent->fs);
42 FontCache::caches[this->fs] = this->parent;
43 Layouter::ResetFontCache(this->fs);
46 int FontCache::GetDefaultFontHeight(FontSize fs)
48 return _default_font_height[fs];
52 /**
53 * Get height of a character for a given font size.
54 * @param size Font size to get height of
55 * @return Height of characters in the given font (pixels)
57 int GetCharacterHeight(FontSize size)
59 return FontCache::Get(size)->GetHeight();
63 /* static */ FontCache *FontCache::caches[FS_END] = { new SpriteFontCache(FS_NORMAL), new SpriteFontCache(FS_SMALL), new SpriteFontCache(FS_LARGE), new SpriteFontCache(FS_MONO) };
67 /* Check if a glyph should be rendered with anti-aliasing. */
68 bool GetFontAAState(FontSize size, bool check_blitter)
70 /* AA is only supported for 32 bpp */
71 if (check_blitter && BlitterFactory::GetCurrentBlitter()->GetScreenDepth() != 32) return false;
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;
83 /**
84 * (Re)initialize the font cache related things, i.e. load the non-sprite fonts.
85 * @param monospace Whether to initialise the monospace or regular fonts.
87 void InitFontCache(bool monospace)
89 for (FontSize fs = FS_BEGIN; fs < FS_END; fs++) {
90 if (monospace != (fs == FS_MONO)) continue;
92 FontCache *fc = FontCache::Get(fs);
93 if (fc->HasParent()) delete fc;
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
109 * Free everything allocated w.r.t. fonts.
111 void UninitFontCache()
113 for (FontSize fs = FS_BEGIN; fs < FS_END; fs++) {
114 FontCache *fc = FontCache::Get(fs);
115 if (fc->HasParent()) delete fc;
118 #ifdef WITH_FREETYPE
119 extern void UninitFreeType();
120 UninitFreeType();
121 #endif /* WITH_FREETYPE */
125 * Should any of the active fonts be anti-aliased?
126 * @return True if any of the loaded fonts want anti-aliased drawing.
128 bool HasAntialiasedFonts()
130 for (FontSize fs = FS_BEGIN; fs < FS_END; fs++) {
131 if (!FontCache::Get(fs)->IsBuiltInFont() && GetFontAAState(fs, false)) return true;
134 return false;
137 #if !defined(_WIN32) && !defined(__APPLE__) && !defined(WITH_FONTCONFIG) && !defined(WITH_COCOA)
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) */