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.h Functions to read fonts from files and cache them. */
13 #include "string_type.h"
14 #include "spritecache.h"
16 /** Glyphs are characters from a font. */
17 typedef uint32_t GlyphID
;
18 static const GlyphID SPRITE_GLYPH
= 1U << 30;
20 /** Font cache for basic fonts. */
23 static FontCache
*caches
[FS_END
]; ///< All the font caches.
24 FontCache
*parent
; ///< The parent of this font cache.
25 const FontSize fs
; ///< The size of the font.
26 int height
; ///< The height of the font.
27 int ascender
; ///< The ascender value of the font.
28 int descender
; ///< The descender value of the font.
31 FontCache(FontSize fs
);
34 static void InitializeFontCaches();
36 static int GetDefaultFontHeight(FontSize fs
);
39 * Get the FontSize of the font.
40 * @return The FontSize.
42 inline FontSize
GetSize() const { return this->fs
; }
45 * Get the height of the font.
46 * @return The height of the font.
48 inline int GetHeight() const { return this->height
; }
51 * Get the ascender value of the font.
52 * @return The ascender value of the font.
54 inline int GetAscender() const { return this->ascender
; }
57 * Get the descender value of the font.
58 * @return The descender value of the font.
60 inline int GetDescender() const{ return this->descender
; }
63 * Get the nominal font size of the font.
64 * @return The nominal font size.
66 virtual int GetFontSize() const { return this->height
; }
69 * Map a SpriteID to the key
70 * @param key The key to map to.
71 * @param sprite The sprite that is being mapped.
73 virtual void SetUnicodeGlyph(char32_t key
, SpriteID sprite
) = 0;
75 /** Initialize the glyph map */
76 virtual void InitializeUnicodeGlyphMap() = 0;
78 /** Clear the font cache. */
79 virtual void ClearFontCache() = 0;
82 * Get the glyph (sprite) of the given key.
83 * @param key The key to look up.
86 virtual const Sprite
*GetGlyph(GlyphID key
) = 0;
89 * Get the width of the glyph with the given key.
90 * @param key The key to look up.
93 virtual uint
GetGlyphWidth(GlyphID key
) = 0;
96 * Do we need to draw a glyph shadow?
97 * @return True if it has to be done, otherwise false.
99 virtual bool GetDrawGlyphShadow() = 0;
102 * Map a character into a glyph.
103 * @param key The character.
104 * @param fallback Allow fallback to the parent font.
105 * @return The glyph ID used to draw the character.
107 virtual GlyphID
MapCharToGlyph(char32_t key
, bool fallback
= true) = 0;
110 * Get the native OS font handle, if there is one.
111 * @return Opaque OS font handle.
113 virtual const void *GetOSHandle()
119 * Get the name of this font.
120 * @return The name of the font.
122 virtual std::string
GetFontName() = 0;
125 * Get the font cache of a given font size.
126 * @param fs The font size to look up.
127 * @return The font cache.
129 static inline FontCache
*Get(FontSize fs
)
132 return FontCache::caches
[fs
];
135 static std::string
GetName(FontSize fs
);
138 * Check whether the font cache has a parent.
140 inline bool HasParent()
142 return this->parent
!= nullptr;
146 * Is this a built-in sprite font?
148 virtual bool IsBuiltInFont() = 0;
151 /** Map a SpriteID to the font size and key */
152 inline void SetUnicodeGlyph(FontSize size
, char32_t key
, SpriteID sprite
)
154 FontCache::Get(size
)->SetUnicodeGlyph(key
, sprite
);
157 /** Initialize the glyph map */
158 inline void InitializeUnicodeGlyphMap()
160 for (FontSize fs
= FS_BEGIN
; fs
< FS_END
; fs
++) {
161 FontCache::Get(fs
)->InitializeUnicodeGlyphMap();
165 inline void ClearFontCache()
167 for (FontSize fs
= FS_BEGIN
; fs
< FS_END
; fs
++) {
168 FontCache::Get(fs
)->ClearFontCache();
172 /** Get the Sprite for a glyph */
173 inline const Sprite
*GetGlyph(FontSize size
, char32_t key
)
175 FontCache
*fc
= FontCache::Get(size
);
176 return fc
->GetGlyph(fc
->MapCharToGlyph(key
));
179 /** Get the width of a glyph */
180 inline uint
GetGlyphWidth(FontSize size
, char32_t key
)
182 FontCache
*fc
= FontCache::Get(size
);
183 return fc
->GetGlyphWidth(fc
->MapCharToGlyph(key
));
186 inline bool GetDrawGlyphShadow(FontSize size
)
188 return FontCache::Get(size
)->GetDrawGlyphShadow();
191 /** Settings for a single font. */
192 struct FontCacheSubSetting
{
193 std::string font
; ///< The name of the font, or path to the font.
194 uint size
; ///< The (requested) size of the font.
196 const void *os_handle
= nullptr; ///< Optional native OS font info. Only valid during font search.
199 /** Settings for the four different fonts. */
200 struct FontCacheSettings
{
201 FontCacheSubSetting small
; ///< The smallest font; mostly used for zoomed out view.
202 FontCacheSubSetting medium
; ///< The normal font size.
203 FontCacheSubSetting large
; ///< The largest font; mostly used for newspapers.
204 FontCacheSubSetting mono
; ///< The mono space font used for license/readme viewers.
205 bool prefer_sprite
; ///< Whether to prefer the built-in sprite font over resizable fonts.
206 bool global_aa
; ///< Whether to anti alias all font sizes.
209 extern FontCacheSettings _fcsettings
;
212 * Get the settings of a given font size.
213 * @param fs The font size to look up.
214 * @return The settings.
216 inline FontCacheSubSetting
*GetFontCacheSubSetting(FontSize fs
)
219 default: NOT_REACHED();
220 case FS_SMALL
: return &_fcsettings
.small
;
221 case FS_NORMAL
: return &_fcsettings
.medium
;
222 case FS_LARGE
: return &_fcsettings
.large
;
223 case FS_MONO
: return &_fcsettings
.mono
;
227 uint
GetFontCacheFontSize(FontSize fs
);
228 std::string
GetFontCacheFontName(FontSize fs
);
229 void InitFontCache(bool monospace
);
230 void UninitFontCache();
232 bool GetFontAAState();
233 void SetFont(FontSize fontsize
, const std::string
&font
, uint size
);
235 #endif /* FONTCACHE_H */