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 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.
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 FontCache(FontSize fs
);
36 * Get the FontSize of the font.
37 * @return The FontSize.
39 inline FontSize
GetSize() const { return this->fs
; }
42 * Get the height of the font.
43 * @return The height of the font.
45 virtual int GetHeight() const { return this->height
; }
48 * Get the ascender value of the font.
49 * @return The ascender value of the font.
51 inline int GetAscender() const { return this->ascender
; }
54 * Get the descender value of the font.
55 * @return The descender value of the font.
57 inline int GetDescender() const{ return this->descender
; }
60 * Get the units per EM value of the font.
61 * @return The units per EM value of the font.
63 inline int GetUnitsPerEM() const { return this->units_per_em
; }
66 * Get the nominal font size of the font.
67 * @return The nominal font size.
69 virtual int GetFontSize() const { return this->height
; }
72 * Get the SpriteID mapped to the given key
73 * @param key The key to get the sprite for.
76 virtual SpriteID
GetUnicodeGlyph(WChar key
) = 0;
79 * Map a SpriteID to the key
80 * @param key The key to map to.
81 * @param sprite The sprite that is being mapped.
83 virtual void SetUnicodeGlyph(WChar key
, SpriteID sprite
) = 0;
85 /** Initialize the glyph map */
86 virtual void InitializeUnicodeGlyphMap() = 0;
88 /** Clear the font cache. */
89 virtual void ClearFontCache() = 0;
92 * Get the glyph (sprite) of the given key.
93 * @param key The key to look up.
96 virtual const Sprite
*GetGlyph(GlyphID key
) = 0;
99 * Get the width of the glyph with the given key.
100 * @param key The key to look up.
103 virtual uint
GetGlyphWidth(GlyphID key
) = 0;
106 * Do we need to draw a glyph shadow?
107 * @return True if it has to be done, otherwise false.
109 virtual bool GetDrawGlyphShadow() = 0;
112 * Map a character into a glyph.
113 * @param key The character.
114 * @return The glyph ID used to draw the character.
116 virtual GlyphID
MapCharToGlyph(WChar key
) = 0;
119 * Read a font table from the font.
120 * @param tag The of the table to load.
121 * @param length The length of the read data.
122 * @return The loaded table data.
124 virtual const void *GetFontTable(uint32 tag
, size_t &length
) = 0;
127 * Get the native OS font handle, if there is one.
128 * @return Opaque OS font handle.
130 virtual void *GetOSHandle()
136 * Get the name of this font.
137 * @return The name of the font.
139 virtual const char *GetFontName() = 0;
142 * Get the font cache of a given font size.
143 * @param fs The font size to look up.
144 * @return The font cache.
146 static inline FontCache
*Get(FontSize fs
)
149 return FontCache::caches
[fs
];
153 * Check whether the font cache has a parent.
155 inline bool HasParent()
157 return this->parent
!= nullptr;
161 * Is this a built-in sprite font?
163 virtual bool IsBuiltInFont() = 0;
166 /** Get the SpriteID mapped to the given font size and key */
167 static inline SpriteID
GetUnicodeGlyph(FontSize size
, WChar key
)
169 return FontCache::Get(size
)->GetUnicodeGlyph(key
);
172 /** Map a SpriteID to the font size and key */
173 static inline void SetUnicodeGlyph(FontSize size
, WChar key
, SpriteID sprite
)
175 FontCache::Get(size
)->SetUnicodeGlyph(key
, sprite
);
178 /** Initialize the glyph map */
179 static inline void InitializeUnicodeGlyphMap()
181 for (FontSize fs
= FS_BEGIN
; fs
< FS_END
; fs
++) {
182 FontCache::Get(fs
)->InitializeUnicodeGlyphMap();
186 static inline void ClearFontCache()
188 for (FontSize fs
= FS_BEGIN
; fs
< FS_END
; fs
++) {
189 FontCache::Get(fs
)->ClearFontCache();
193 /** Get the Sprite for a glyph */
194 static inline const Sprite
*GetGlyph(FontSize size
, WChar key
)
196 FontCache
*fc
= FontCache::Get(size
);
197 return fc
->GetGlyph(fc
->MapCharToGlyph(key
));
200 /** Get the width of a glyph */
201 static inline uint
GetGlyphWidth(FontSize size
, WChar key
)
203 FontCache
*fc
= FontCache::Get(size
);
204 return fc
->GetGlyphWidth(fc
->MapCharToGlyph(key
));
207 static inline bool GetDrawGlyphShadow(FontSize size
)
209 return FontCache::Get(size
)->GetDrawGlyphShadow();
212 #if defined(WITH_FREETYPE) || defined(_WIN32)
214 /** Settings for a single freetype font. */
215 struct FreeTypeSubSetting
{
216 char font
[MAX_PATH
]; ///< The name of the font, or path to the font.
217 uint size
; ///< The (requested) size of the font.
218 bool aa
; ///< Whether to do anti aliasing or not.
220 const void *os_handle
= nullptr; ///< Optional native OS font info.
223 /** Settings for the freetype fonts. */
224 struct FreeTypeSettings
{
225 FreeTypeSubSetting small
; ///< The smallest font; mostly used for zoomed out view.
226 FreeTypeSubSetting medium
; ///< The normal font size.
227 FreeTypeSubSetting large
; ///< The largest font; mostly used for newspapers.
228 FreeTypeSubSetting mono
; ///< The mono space font used for license/readme viewers.
231 extern FreeTypeSettings _freetype
;
233 #endif /* defined(WITH_FREETYPE) || defined(_WIN32) */
235 void InitFreeType(bool monospace
);
236 void UninitFreeType();
238 #endif /* FONTCACHE_H */