Fix #9521: Don't load at just removed docks that were part of a multi-dock station...
[openttd-github.git] / src / fontcache.h
blob2eb5634cfbbe2e5fa2777696ec0ceaf8a64cd61c
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.h Functions to read fonts from files and cache them. */
10 #ifndef FONTCACHE_H
11 #define FONTCACHE_H
13 #include "string_type.h"
14 #include "spritecache.h"
16 /** Glyphs are characters from a font. */
17 typedef uint32 GlyphID;
18 static const GlyphID SPRITE_GLYPH = 1U << 30;
20 /** Font cache for basic fonts. */
21 class FontCache {
22 private:
23 static FontCache *caches[FS_END]; ///< All the font caches.
24 protected:
25 FontCache *parent; ///< The parent of this font cache.
26 const FontSize fs; ///< The size of the font.
27 int height; ///< The height of the font.
28 int ascender; ///< The ascender value of the font.
29 int descender; ///< The descender value of the font.
30 int units_per_em; ///< The units per EM value of the font.
32 static int GetDefaultFontHeight(FontSize fs);
34 public:
35 FontCache(FontSize fs);
36 virtual ~FontCache();
38 /**
39 * Get the FontSize of the font.
40 * @return The FontSize.
42 inline FontSize GetSize() const { return this->fs; }
44 /**
45 * Get the height of the font.
46 * @return The height of the font.
48 inline int GetHeight() const { return this->height; }
50 /**
51 * Get the ascender value of the font.
52 * @return The ascender value of the font.
54 inline int GetAscender() const { return this->ascender; }
56 /**
57 * Get the descender value of the font.
58 * @return The descender value of the font.
60 inline int GetDescender() const{ return this->descender; }
62 /**
63 * Get the units per EM value of the font.
64 * @return The units per EM value of the font.
66 inline int GetUnitsPerEM() const { return this->units_per_em; }
68 /**
69 * Get the nominal font size of the font.
70 * @return The nominal font size.
72 virtual int GetFontSize() const { return this->height; }
74 /**
75 * Get the SpriteID mapped to the given key
76 * @param key The key to get the sprite for.
77 * @return The sprite.
79 virtual SpriteID GetUnicodeGlyph(WChar key) = 0;
81 /**
82 * Map a SpriteID to the key
83 * @param key The key to map to.
84 * @param sprite The sprite that is being mapped.
86 virtual void SetUnicodeGlyph(WChar key, SpriteID sprite) = 0;
88 /** Initialize the glyph map */
89 virtual void InitializeUnicodeGlyphMap() = 0;
91 /** Clear the font cache. */
92 virtual void ClearFontCache() = 0;
94 /**
95 * Get the glyph (sprite) of the given key.
96 * @param key The key to look up.
97 * @return The sprite.
99 virtual const Sprite *GetGlyph(GlyphID key) = 0;
102 * Get the width of the glyph with the given key.
103 * @param key The key to look up.
104 * @return The width.
106 virtual uint GetGlyphWidth(GlyphID key) = 0;
109 * Do we need to draw a glyph shadow?
110 * @return True if it has to be done, otherwise false.
112 virtual bool GetDrawGlyphShadow() = 0;
115 * Map a character into a glyph.
116 * @param key The character.
117 * @return The glyph ID used to draw the character.
119 virtual GlyphID MapCharToGlyph(WChar key) = 0;
122 * Read a font table from the font.
123 * @param tag The of the table to load.
124 * @param length The length of the read data.
125 * @return The loaded table data.
127 virtual const void *GetFontTable(uint32 tag, size_t &length) = 0;
130 * Get the native OS font handle, if there is one.
131 * @return Opaque OS font handle.
133 virtual const void *GetOSHandle()
135 return nullptr;
139 * Get the name of this font.
140 * @return The name of the font.
142 virtual const char *GetFontName() = 0;
145 * Get the font cache of a given font size.
146 * @param fs The font size to look up.
147 * @return The font cache.
149 static inline FontCache *Get(FontSize fs)
151 assert(fs < FS_END);
152 return FontCache::caches[fs];
156 * Check whether the font cache has a parent.
158 inline bool HasParent()
160 return this->parent != nullptr;
164 * Is this a built-in sprite font?
166 virtual bool IsBuiltInFont() = 0;
169 /** Get the SpriteID mapped to the given font size and key */
170 static inline SpriteID GetUnicodeGlyph(FontSize size, WChar key)
172 return FontCache::Get(size)->GetUnicodeGlyph(key);
175 /** Map a SpriteID to the font size and key */
176 static inline void SetUnicodeGlyph(FontSize size, WChar key, SpriteID sprite)
178 FontCache::Get(size)->SetUnicodeGlyph(key, sprite);
181 /** Initialize the glyph map */
182 static inline void InitializeUnicodeGlyphMap()
184 for (FontSize fs = FS_BEGIN; fs < FS_END; fs++) {
185 FontCache::Get(fs)->InitializeUnicodeGlyphMap();
189 static inline void ClearFontCache()
191 for (FontSize fs = FS_BEGIN; fs < FS_END; fs++) {
192 FontCache::Get(fs)->ClearFontCache();
196 /** Get the Sprite for a glyph */
197 static inline const Sprite *GetGlyph(FontSize size, WChar key)
199 FontCache *fc = FontCache::Get(size);
200 return fc->GetGlyph(fc->MapCharToGlyph(key));
203 /** Get the width of a glyph */
204 static inline uint GetGlyphWidth(FontSize size, WChar key)
206 FontCache *fc = FontCache::Get(size);
207 return fc->GetGlyphWidth(fc->MapCharToGlyph(key));
210 static inline bool GetDrawGlyphShadow(FontSize size)
212 return FontCache::Get(size)->GetDrawGlyphShadow();
215 /** Settings for a single freetype font. */
216 struct FreeTypeSubSetting {
217 std::string font; ///< The name of the font, or path to the font.
218 uint size; ///< The (requested) size of the font.
219 bool aa; ///< Whether to do anti aliasing or not.
221 const void *os_handle = nullptr; ///< Optional native OS font info. Only valid during font search.
224 /** Settings for the freetype fonts. */
225 struct FreeTypeSettings {
226 FreeTypeSubSetting small; ///< The smallest font; mostly used for zoomed out view.
227 FreeTypeSubSetting medium; ///< The normal font size.
228 FreeTypeSubSetting large; ///< The largest font; mostly used for newspapers.
229 FreeTypeSubSetting mono; ///< The mono space font used for license/readme viewers.
232 extern FreeTypeSettings _freetype;
234 void InitFreeType(bool monospace);
235 void UninitFreeType();
236 bool HasAntialiasedFonts();
238 #endif /* FONTCACHE_H */