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 font.h Functions to read fonts from files and cache them. */
15 #include FT_FREETYPE_H
17 #include FT_TRUETYPE_TABLES_H
18 #endif /* WITH_FREETYPE */
20 #include "core/smallmap_type.hpp"
22 #include "spritecache.h"
24 /** Glyphs are characters from a font. */
25 typedef uint32 GlyphID
;
26 static const GlyphID SPRITE_GLYPH
= 1U << 30;
28 /** Font cache for basic fonts. */
31 static FontCache cache
[FS_END
]; ///< All the font caches.
33 SpriteID
*spriteid_map
[0x100]; ///< Mapping of glyphs to sprite IDs.
35 byte glyph_widths
[256 - 32]; ///< Glyph widths of all ASCII characters.
39 /** Container for information about a FreeType glyph. */
41 Sprite
*sprite
; ///< The loaded sprite.
42 byte width
; ///< The width of the glyph.
46 * The glyph cache. This is structured to reduce memory consumption.
47 * 1) There is a 'segment' table for each font size.
48 * 2) Each segment table is a discrete block of characters.
49 * 3) Each block contains 256 (aligned) characters sequential characters.
51 * The cache is accessed in the following way:
52 * For character 0x0041 ('A'): sprite_map[0x00][0x41]
53 * For character 0x20AC (Euro): sprite_map[0x20][0xAC]
55 * Currently only 256 segments are allocated, "limiting" us to 65536 characters.
56 * This can be simply changed in the function GetGlyphPtr.
58 GlyphEntry
*sprite_map
[0x100];
60 const GlyphEntry
*missing_sprite
; ///< Sprite for missing glyphs (question mark)
62 typedef SmallMap
<uint32
, SmallPair
<size_t, const void*> > FontTable
; ///< Table with font table cache
64 FontTable font_tables
; ///< Cached font tables.
66 FT_Face face
; ///< The font face associated with this font.
68 #endif /* WITH_FREETYPE */
70 const FontSize fs
; ///< The size of the font.
71 int height
; ///< The height of the font.
72 int ascender
; ///< The ascender value of the font.
73 int descender
; ///< The descender value of the font.
74 int units_per_em
; ///< The units per EM value of the font.
76 byte widest_digit
; ///< Widest digit.
77 byte widest_digit_nonnull
; ///< Widest leading (non-null) digit.
78 byte digit_width
; ///< Width of the widest digit.
80 void ResetFontMetrics (void);
81 void RebuildWidthCache (void);
83 void ClearGlyphToSpriteMap (void);
85 SpriteID
GetGlyphSprite (GlyphID key
) const;
89 const GlyphEntry
*GetGlyphPtr (GlyphID key
);
91 #endif /* WITH_FREETYPE */
93 FontCache(FontSize fs
);
98 * Get the height of the font.
99 * @return The height of the font.
101 inline int GetHeight() const { return this->height
; }
104 * Get the ascender value of the font.
105 * @return The ascender value of the font.
107 inline int GetAscender() const { return this->ascender
; }
110 * Get the descender value of the font.
111 * @return The descender value of the font.
113 inline int GetDescender() const{ return this->descender
; }
116 * Get the units per EM value of the font.
117 * @return The units per EM value of the font.
119 inline int GetUnitsPerEM() const { return this->units_per_em
; }
122 * Get the SpriteID mapped to the given key
123 * @param key The key to get the sprite for.
124 * @return The sprite.
126 SpriteID
GetUnicodeGlyph (WChar key
) const;
129 * Map a SpriteID to the key
130 * @param key The key to map to.
131 * @param sprite The sprite that is being mapped.
133 void SetUnicodeGlyph (WChar key
, SpriteID sprite
);
135 /** Initialize the glyph map */
136 void InitializeUnicodeGlyphMap (void);
140 /* Load the freetype font. */
141 void LoadFreeTypeFont (void);
143 /* Clear the glyph cache. */
144 void ClearGlyphCache (void);
146 /* Unload the freetype font. */
147 void UnloadFreeTypeFont (void);
149 #endif /* WITH_FREETYPE */
151 /** Clear the font cache. */
152 void ClearFontCache (void);
155 * Get the glyph (sprite) of the given key.
156 * @param key The key to look up.
157 * @return The sprite.
159 const Sprite
*GetGlyph (GlyphID key
);
162 * Get the width of the glyph with the given key.
163 * @param key The key to look up.
166 uint
GetGlyphWidth (GlyphID key
);
169 * Do we need to draw a glyph shadow?
170 * @return True if it has to be done, otherwise false.
172 bool GetDrawGlyphShadow (void) const;
175 * Map a character into a glyph.
176 * @param key The character.
177 * @return The glyph ID used to draw the character.
179 GlyphID
MapCharToGlyph (WChar key
) const;
182 * Get the glyph (sprite) for a given character.
183 * @param c The character to look up.
184 * @return The sprite.
186 const Sprite
*GetCharGlyph (WChar c
)
188 return this->GetGlyph (this->MapCharToGlyph (c
));
192 * Read a font table from the font.
193 * @param tag The of the table to load.
194 * @param length The length of the read data.
195 * @return The loaded table data.
197 const void *GetFontTable (uint32 tag
, size_t &length
);
200 * Get the name of this font.
201 * @return The name of the font.
203 const char *GetFontName (void) const;
206 * Return width of character glyph.
207 * @param size Font of the character
208 * @param key Character code glyph
209 * @return Width of the character glyph
211 byte
GetCharacterWidth (WChar key
)
213 /* Use cache if possible */
214 if (key
>= 32 && key
< 256) return this->glyph_widths
[key
- 32];
216 return this->GetGlyphWidth (this->MapCharToGlyph (key
));
220 * Return the maximum width of single digit.
221 * @param size Font of the digit
222 * @return Width of the digit.
224 byte
GetDigitWidth (void) const
226 return this->digit_width
;
229 /* Compute the broadest n-digit value in a given font size. */
230 uint64
GetBroadestValue (uint n
) const;
233 * Get the font cache of a given font size.
234 * @param fs The font size to look up.
235 * @return The font cache.
237 static inline FontCache
*Get(FontSize fs
)
240 return &FontCache::cache
[fs
];
244 /** Initialize the glyph map */
245 static inline void InitializeUnicodeGlyphMap()
247 for (FontSize fs
= FS_BEGIN
; fs
< FS_END
; fs
++) {
248 FontCache::Get(fs
)->InitializeUnicodeGlyphMap();
252 static inline void ClearFontCache()
254 for (FontSize fs
= FS_BEGIN
; fs
< FS_END
; fs
++) {
255 FontCache::Get(fs
)->ClearFontCache();
260 * Return the maximum width of single digit.
261 * @param size Font of the digit
262 * @return Width of the digit.
264 static inline byte
GetDigitWidth (FontSize size
= FS_NORMAL
)
266 return FontCache::Get(size
)->GetDigitWidth();
271 /** Settings for a single freetype font. */
272 struct FreeTypeSubSetting
{
273 char font
[MAX_PATH
]; ///< The name of the font, or path to the font.
274 uint size
; ///< The (requested) size of the font.
275 bool aa
; ///< Whether to do anti aliasing or not.
278 /** Settings for the freetype fonts. */
279 struct FreeTypeSettings
{
280 FreeTypeSubSetting small
; ///< The smallest font; mostly used for zoomed out view.
281 FreeTypeSubSetting medium
; ///< The normal font size.
282 FreeTypeSubSetting large
; ///< The largest font; mostly used for newspapers.
283 FreeTypeSubSetting mono
; ///< The mono space font used for license/readme viewers.
286 extern FreeTypeSettings _freetype
;
288 void InitFreeType(bool monospace
);
291 * We would like to have a fallback font as the current one
292 * doesn't contain all characters we need.
293 * This function must set all fonts of settings.
294 * @param settings the settings to overwrite the fontname of.
295 * @param language_isocode the language, e.g. en_GB.
296 * @param winlangid the language ID windows style.
297 * @param callback The function to call to check for missing glyphs.
298 * @return true if a font has been set, false otherwise.
300 bool SetFallbackFont (FreeTypeSettings
*settings
, const char *language_isocode
, int winlangid
, class MissingGlyphSearcher
*callback
);
302 #else /* WITH_FREETYPE */
304 static inline void InitFreeType (bool monospace
)
308 #endif /* WITH_FREETYPE */