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.
43 bool duplicate
; ///< Whether this glyph entry is a duplicate, i.e. may this be freed?
47 * The glyph cache. This is structured to reduce memory consumption.
48 * 1) There is a 'segment' table for each font size.
49 * 2) Each segment table is a discrete block of characters.
50 * 3) Each block contains 256 (aligned) characters sequential characters.
52 * The cache is accessed in the following way:
53 * For character 0x0041 ('A'): sprite_map[0x00][0x41]
54 * For character 0x20AC (Euro): sprite_map[0x20][0xAC]
56 * Currently only 256 segments are allocated, "limiting" us to 65536 characters.
57 * This can be simply changed in the two functions Get & SetGlyphPtr.
59 GlyphEntry
*sprite_map
[0x100];
61 typedef SmallMap
<uint32
, SmallPair
<size_t, const void*> > FontTable
; ///< Table with font table cache
63 FontTable font_tables
; ///< Cached font tables.
65 FT_Face face
; ///< The font face associated with this font.
67 #endif /* WITH_FREETYPE */
69 const FontSize fs
; ///< The size of the font.
70 int height
; ///< The height of the font.
71 int ascender
; ///< The ascender value of the font.
72 int descender
; ///< The descender value of the font.
73 int units_per_em
; ///< The units per EM value of the font.
75 byte widest_digit
; ///< Widest digit.
76 byte widest_digit_nonnull
; ///< Widest leading (non-null) digit.
77 byte digit_width
; ///< Width of the widest digit.
79 void ResetFontMetrics (void);
81 void ClearGlyphToSpriteMap (void);
83 SpriteID
GetGlyphSprite (GlyphID key
) const;
87 GlyphEntry
*SetGlyphPtr (GlyphID key
, Sprite
*sprite
, byte width
, bool duplicate
= false);
88 GlyphEntry
*GetGlyphPtr (GlyphID key
);
90 #endif /* WITH_FREETYPE */
92 FontCache(FontSize fs
);
97 * Get the height of the font.
98 * @return The height of the font.
100 inline int GetHeight() const { return this->height
; }
103 * Get the ascender value of the font.
104 * @return The ascender value of the font.
106 inline int GetAscender() const { return this->ascender
; }
109 * Get the descender value of the font.
110 * @return The descender value of the font.
112 inline int GetDescender() const{ return this->descender
; }
115 * Get the units per EM value of the font.
116 * @return The units per EM value of the font.
118 inline int GetUnitsPerEM() const { return this->units_per_em
; }
121 * Get the SpriteID mapped to the given key
122 * @param key The key to get the sprite for.
123 * @return The sprite.
125 SpriteID
GetUnicodeGlyph (WChar key
) const;
128 * Map a SpriteID to the key
129 * @param key The key to map to.
130 * @param sprite The sprite that is being mapped.
132 void SetUnicodeGlyph (WChar key
, SpriteID sprite
);
134 /** Initialize the glyph map */
135 void InitializeUnicodeGlyphMap (void);
139 /* Load the freetype font. */
140 void LoadFreeTypeFont (void);
142 /* Unload the freetype font. */
143 void UnloadFreeTypeFont (void);
145 #endif /* WITH_FREETYPE */
147 /** Clear the font cache. */
148 void ClearFontCache (void);
151 * Get the glyph (sprite) of the given key.
152 * @param key The key to look up.
153 * @return The sprite.
155 const Sprite
*GetGlyph (GlyphID key
);
158 * Get the width of the glyph with the given key.
159 * @param key The key to look up.
162 uint
GetGlyphWidth (GlyphID key
);
165 * Do we need to draw a glyph shadow?
166 * @return True if it has to be done, otherwise false.
168 bool GetDrawGlyphShadow (void) const;
171 * Map a character into a glyph.
172 * @param key The character.
173 * @return The glyph ID used to draw the character.
175 GlyphID
MapCharToGlyph (WChar key
) const;
178 * Get the glyph (sprite) for a given character.
179 * @param c The character to look up.
180 * @return The sprite.
182 const Sprite
*GetCharGlyph (WChar c
)
184 return this->GetGlyph (this->MapCharToGlyph (c
));
188 * Read a font table from the font.
189 * @param tag The of the table to load.
190 * @param length The length of the read data.
191 * @return The loaded table data.
193 const void *GetFontTable (uint32 tag
, size_t &length
);
196 * Get the name of this font.
197 * @return The name of the font.
199 const char *GetFontName (void) const;
202 * Return width of character glyph.
203 * @param size Font of the character
204 * @param key Character code glyph
205 * @return Width of the character glyph
207 byte
GetCharacterWidth (WChar key
)
209 /* Use cache if possible */
210 if (key
>= 32 && key
< 256) return this->glyph_widths
[key
- 32];
212 return this->GetGlyphWidth (this->MapCharToGlyph (key
));
216 * Return the maximum width of single digit.
217 * @param size Font of the digit
218 * @return Width of the digit.
220 byte
GetDigitWidth (void) const
222 return this->digit_width
;
225 /* Compute the broadest n-digit value in a given font size. */
226 uint64
GetBroadestValue (uint n
) const;
229 * Get the font cache of a given font size.
230 * @param fs The font size to look up.
231 * @return The font cache.
233 static inline FontCache
*Get(FontSize fs
)
236 return &FontCache::cache
[fs
];
240 /** Initialize the glyph map */
241 static inline void InitializeUnicodeGlyphMap()
243 for (FontSize fs
= FS_BEGIN
; fs
< FS_END
; fs
++) {
244 FontCache::Get(fs
)->InitializeUnicodeGlyphMap();
248 static inline void ClearFontCache()
250 for (FontSize fs
= FS_BEGIN
; fs
< FS_END
; fs
++) {
251 FontCache::Get(fs
)->ClearFontCache();
256 * Return the maximum width of single digit.
257 * @param size Font of the digit
258 * @return Width of the digit.
260 static inline byte
GetDigitWidth (FontSize size
= FS_NORMAL
)
262 return FontCache::Get(size
)->GetDigitWidth();
267 /** Settings for a single freetype font. */
268 struct FreeTypeSubSetting
{
269 char font
[MAX_PATH
]; ///< The name of the font, or path to the font.
270 uint size
; ///< The (requested) size of the font.
271 bool aa
; ///< Whether to do anti aliasing or not.
274 /** Settings for the freetype fonts. */
275 struct FreeTypeSettings
{
276 FreeTypeSubSetting small
; ///< The smallest font; mostly used for zoomed out view.
277 FreeTypeSubSetting medium
; ///< The normal font size.
278 FreeTypeSubSetting large
; ///< The largest font; mostly used for newspapers.
279 FreeTypeSubSetting mono
; ///< The mono space font used for license/readme viewers.
282 extern FreeTypeSettings _freetype
;
284 void InitFreeType(bool monospace
);
285 void UninitFreeType();
288 * We would like to have a fallback font as the current one
289 * doesn't contain all characters we need.
290 * This function must set all fonts of settings.
291 * @param settings the settings to overwrite the fontname of.
292 * @param language_isocode the language, e.g. en_GB.
293 * @param winlangid the language ID windows style.
294 * @param callback The function to call to check for missing glyphs.
295 * @return true if a font has been set, false otherwise.
297 bool SetFallbackFont (FreeTypeSettings
*settings
, const char *language_isocode
, int winlangid
, class MissingGlyphSearcher
*callback
);
299 #else /* WITH_FREETYPE */
301 static inline void InitFreeType (bool monospace
)
305 static inline void UninitFreeType (void)
309 #endif /* WITH_FREETYPE */