1 /* $Id: fontcache.h 26170 2013-12-22 17:46:27Z rubidium $ */
4 * This file is part of OpenTTD.
5 * 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.
6 * 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.
7 * 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/>.
10 /** @file fontcache.h Functions to read fonts from files and cache them. */
15 #include "string_type.h"
16 #include "spritecache.h"
18 /** Glyphs are characters from a font. */
19 typedef uint32 GlyphID
;
20 static const GlyphID SPRITE_GLYPH
= 1U << 30;
22 extern int font_height_cache
[FS_END
]; ///< Cache of font heights
24 void UpdateFontHeightCache();
26 /** Font cache for basic fonts. */
29 static FontCache
*caches
[FS_END
]; ///< All the font caches.
31 FontCache
*parent
; ///< The parent of this font cache.
32 const FontSize fs
; ///< The size of the font.
33 int height
; ///< The height of the font.
34 int ascender
; ///< The ascender value of the font.
35 int descender
; ///< The descender value of the font.
36 int units_per_em
; ///< The units per EM value of the font.
38 FontCache(FontSize fs
);
42 * Get the FontSize of the font.
43 * @return The FontSize.
45 inline FontSize
GetSize() const { return this->fs
; }
48 * Get the height of the font.
49 * @return The height of the font.
51 virtual int GetHeight() const { return this->height
; }
54 * Get the ascender value of the font.
55 * @return The ascender value of the font.
57 inline int GetAscender() const { return this->ascender
; }
60 * Get the descender value of the font.
61 * @return The descender value of the font.
63 inline int GetDescender() const{ return this->descender
; }
66 * Get the units per EM value of the font.
67 * @return The units per EM value of the font.
69 inline int GetUnitsPerEM() const { return this->units_per_em
; }
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 name of this font.
128 * @return The name of the font.
130 virtual const char *GetFontName() = 0;
133 * Get the font cache of a given font size.
134 * @param fs The font size to look up.
135 * @return The font cache.
137 static inline FontCache
*Get(FontSize fs
)
140 return FontCache::caches
[fs
];
144 * Check whether the font cache has a parent.
146 inline bool HasParent()
148 return this->parent
!= nullptr;
152 /** Get the SpriteID mapped to the given font size and key */
153 static inline SpriteID
GetUnicodeGlyph(FontSize size
, WChar key
)
155 return FontCache::Get(size
)->GetUnicodeGlyph(key
);
158 /** Map a SpriteID to the font size and key */
159 static inline void SetUnicodeGlyph(FontSize size
, WChar key
, SpriteID sprite
)
161 FontCache::Get(size
)->SetUnicodeGlyph(key
, sprite
);
164 /** Initialize the glyph map */
165 static inline void InitializeUnicodeGlyphMap()
167 for (FontSize fs
= FS_BEGIN
; fs
< FS_END
; fs
++) {
168 FontCache::Get(fs
)->InitializeUnicodeGlyphMap();
172 static inline void ClearFontCache()
174 for (FontSize fs
= FS_BEGIN
; fs
< FS_END
; fs
++) {
175 FontCache::Get(fs
)->ClearFontCache();
179 /** Get the Sprite for a glyph */
180 static inline const Sprite
*GetGlyph(FontSize size
, WChar key
)
182 FontCache
*fc
= FontCache::Get(size
);
183 return fc
->GetGlyph(fc
->MapCharToGlyph(key
));
186 /** Get the width of a glyph */
187 static inline uint
GetGlyphWidth(FontSize size
, WChar key
)
189 FontCache
*fc
= FontCache::Get(size
);
190 return fc
->GetGlyphWidth(fc
->MapCharToGlyph(key
));
193 static inline bool GetDrawGlyphShadow(FontSize size
)
195 return FontCache::Get(size
)->GetDrawGlyphShadow();
200 /** Settings for a single freetype font. */
201 struct FreeTypeSubSetting
{
202 char font
[MAX_PATH
]; ///< The name of the font, or path to the font.
203 uint size
; ///< The (requested) size of the font.
204 bool aa
; ///< Whether to do anti aliasing or not.
207 /** Settings for the freetype fonts. */
208 struct FreeTypeSettings
{
209 FreeTypeSubSetting small
; ///< The smallest font; mostly used for zoomed out view.
210 FreeTypeSubSetting medium
; ///< The normal font size.
211 FreeTypeSubSetting large
; ///< The largest font; mostly used for newspapers.
212 FreeTypeSubSetting mono
; ///< The mono space font used for license/readme viewers.
215 extern FreeTypeSettings _freetype
;
217 #endif /* WITH_FREETYPE */
219 void InitFreeType(bool monospace
);
220 void UninitFreeType();
222 #endif /* FONTCACHE_H */