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. */
17 #include FT_FREETYPE_H
19 #include FT_TRUETYPE_TABLES_H
20 #endif /* WITH_FREETYPE */
22 #include "core/smallmap_type.hpp"
24 #include "spritecache.h"
26 /** Glyphs are characters from a font. */
27 typedef uint32 GlyphID
;
28 static const GlyphID SPRITE_GLYPH
= 1U << 30;
30 /** Font cache for basic fonts. */
33 static FontCache cache
[FS_END
]; ///< All the font caches.
35 SpriteID
*spriteid_map
[0x100]; ///< Mapping of glyphs to sprite IDs.
37 byte glyph_widths
[256 - 32]; ///< Glyph widths of all ASCII characters.
41 /** Container for information about a FreeType glyph. */
43 Sprite
*sprite
; ///< The loaded sprite.
44 byte width
; ///< The width of the glyph.
45 bool duplicate
; ///< Whether this glyph entry is a duplicate, i.e. may this be freed?
49 * The glyph cache. This is structured to reduce memory consumption.
50 * 1) There is a 'segment' table for each font size.
51 * 2) Each segment table is a discrete block of characters.
52 * 3) Each block contains 256 (aligned) characters sequential characters.
54 * The cache is accessed in the following way:
55 * For character 0x0041 ('A'): sprite_map[0x00][0x41]
56 * For character 0x20AC (Euro): sprite_map[0x20][0xAC]
58 * Currently only 256 segments are allocated, "limiting" us to 65536 characters.
59 * This can be simply changed in the two functions Get & SetGlyphPtr.
61 GlyphEntry
*sprite_map
[0x100];
63 typedef SmallMap
<uint32
, SmallPair
<size_t, const void*> > FontTable
; ///< Table with font table cache
65 FontTable font_tables
; ///< Cached font tables.
67 FT_Face face
; ///< The font face associated with this font.
69 #endif /* WITH_FREETYPE */
71 const FontSize fs
; ///< The size of the font.
72 int height
; ///< The height of the font.
73 int ascender
; ///< The ascender value of the font.
74 int descender
; ///< The descender value of the font.
75 int units_per_em
; ///< The units per EM value of the font.
77 byte widest_digit
; ///< Widest digit.
78 byte widest_digit_nonnull
; ///< Widest leading (non-null) digit.
79 byte digit_width
; ///< Width of the widest digit.
81 void ResetFontMetrics (void);
83 void ClearGlyphToSpriteMap (void);
85 SpriteID
GetGlyphSprite (GlyphID key
) const;
89 GlyphEntry
*SetGlyphPtr (GlyphID key
, Sprite
*sprite
, byte width
, bool duplicate
= false);
90 GlyphEntry
*GetGlyphPtr (GlyphID key
);
92 #endif /* WITH_FREETYPE */
94 FontCache(FontSize fs
);
99 * Get the height of the font.
100 * @return The height of the font.
102 inline int GetHeight() const { return this->height
; }
105 * Get the ascender value of the font.
106 * @return The ascender value of the font.
108 inline int GetAscender() const { return this->ascender
; }
111 * Get the descender value of the font.
112 * @return The descender value of the font.
114 inline int GetDescender() const{ return this->descender
; }
117 * Get the units per EM value of the font.
118 * @return The units per EM value of the font.
120 inline int GetUnitsPerEM() const { return this->units_per_em
; }
123 * Get the SpriteID mapped to the given key
124 * @param key The key to get the sprite for.
125 * @return The sprite.
127 SpriteID
GetUnicodeGlyph (WChar key
) const;
130 * Map a SpriteID to the key
131 * @param key The key to map to.
132 * @param sprite The sprite that is being mapped.
134 void SetUnicodeGlyph (WChar key
, SpriteID sprite
);
136 /** Initialize the glyph map */
137 void InitializeUnicodeGlyphMap (void);
141 /* Load the freetype font. */
142 void LoadFreeTypeFont (void);
144 /* Unload the freetype font. */
145 void UnloadFreeTypeFont (void);
147 #endif /* WITH_FREETYPE */
149 /** Clear the font cache. */
150 void ClearFontCache (void);
153 * Get the glyph (sprite) of the given key.
154 * @param key The key to look up.
155 * @return The sprite.
157 const Sprite
*GetGlyph (GlyphID key
);
160 * Get the width of the glyph with the given key.
161 * @param key The key to look up.
164 uint
GetGlyphWidth (GlyphID key
);
167 * Do we need to draw a glyph shadow?
168 * @return True if it has to be done, otherwise false.
170 bool GetDrawGlyphShadow (void) const;
173 * Map a character into a glyph.
174 * @param key The character.
175 * @return The glyph ID used to draw the character.
177 GlyphID
MapCharToGlyph (WChar key
) const;
180 * Read a font table from the font.
181 * @param tag The of the table to load.
182 * @param length The length of the read data.
183 * @return The loaded table data.
185 const void *GetFontTable (uint32 tag
, size_t &length
);
188 * Get the name of this font.
189 * @return The name of the font.
191 const char *GetFontName (void) const;
194 * Return width of character glyph.
195 * @param size Font of the character
196 * @param key Character code glyph
197 * @return Width of the character glyph
199 byte
GetCharacterWidth (WChar key
)
201 /* Use cache if possible */
202 if (key
>= 32 && key
< 256) return this->glyph_widths
[key
- 32];
204 return this->GetGlyphWidth (this->MapCharToGlyph (key
));
208 * Return the maximum width of single digit.
209 * @param size Font of the digit
210 * @return Width of the digit.
212 byte
GetDigitWidth (void) const
214 return this->digit_width
;
217 /* Compute the broadest n-digit value in a given font size. */
218 uint64
GetBroadestValue (uint n
) const;
221 * Get the font cache of a given font size.
222 * @param fs The font size to look up.
223 * @return The font cache.
225 static inline FontCache
*Get(FontSize fs
)
228 return &FontCache::cache
[fs
];
232 /** Initialize the glyph map */
233 static inline void InitializeUnicodeGlyphMap()
235 for (FontSize fs
= FS_BEGIN
; fs
< FS_END
; fs
++) {
236 FontCache::Get(fs
)->InitializeUnicodeGlyphMap();
240 static inline void ClearFontCache()
242 for (FontSize fs
= FS_BEGIN
; fs
< FS_END
; fs
++) {
243 FontCache::Get(fs
)->ClearFontCache();
247 /** Get the Sprite for a glyph */
248 static inline const Sprite
*GetGlyph(FontSize size
, WChar key
)
250 FontCache
*fc
= FontCache::Get(size
);
251 return fc
->GetGlyph(fc
->MapCharToGlyph(key
));
255 * Return the maximum width of single digit.
256 * @param size Font of the digit
257 * @return Width of the digit.
259 static inline byte
GetDigitWidth (FontSize size
= FS_NORMAL
)
261 return FontCache::Get(size
)->GetDigitWidth();
266 /** Settings for a single freetype font. */
267 struct FreeTypeSubSetting
{
268 char font
[MAX_PATH
]; ///< The name of the font, or path to the font.
269 uint size
; ///< The (requested) size of the font.
270 bool aa
; ///< Whether to do anti aliasing or not.
273 /** Settings for the freetype fonts. */
274 struct FreeTypeSettings
{
275 FreeTypeSubSetting small
; ///< The smallest font; mostly used for zoomed out view.
276 FreeTypeSubSetting medium
; ///< The normal font size.
277 FreeTypeSubSetting large
; ///< The largest font; mostly used for newspapers.
278 FreeTypeSubSetting mono
; ///< The mono space font used for license/readme viewers.
281 extern FreeTypeSettings _freetype
;
283 void InitFreeType(bool monospace
);
284 void UninitFreeType();
286 #else /* WITH_FREETYPE */
288 static inline void InitFreeType (bool monospace
)
292 static inline void UninitFreeType (void)
296 #endif /* WITH_FREETYPE */
298 #endif /* FONTCACHE_H */