(svn r28004) -Update from Eints:
[openttd.git] / src / fontcache.h
blob8caf4f1bd3373c3eb851a44f6507883d6be3ff2e
1 /* $Id$ */
3 /*
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/>.
8 */
10 /** @file fontcache.h Functions to read fonts from files and cache them. */
12 #ifndef FONTCACHE_H
13 #define FONTCACHE_H
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 /** Font cache for basic fonts. */
23 class FontCache {
24 private:
25 static FontCache *caches[FS_END]; ///< All the font caches.
26 protected:
27 FontCache *parent; ///< The parent of this font cache.
28 const FontSize fs; ///< The size of the font.
29 int height; ///< The height of the font.
30 int ascender; ///< The ascender value of the font.
31 int descender; ///< The descender value of the font.
32 int units_per_em; ///< The units per EM value of the font.
33 public:
34 FontCache(FontSize fs);
35 virtual ~FontCache();
37 /**
38 * Get the FontSize of the font.
39 * @return The FontSize.
41 inline FontSize GetSize() const { return this->fs; }
43 /**
44 * Get the height of the font.
45 * @return The height of the font.
47 virtual int GetHeight() const { return this->height; }
49 /**
50 * Get the ascender value of the font.
51 * @return The ascender value of the font.
53 inline int GetAscender() const { return this->ascender; }
55 /**
56 * Get the descender value of the font.
57 * @return The descender value of the font.
59 inline int GetDescender() const{ return this->descender; }
61 /**
62 * Get the units per EM value of the font.
63 * @return The units per EM value of the font.
65 inline int GetUnitsPerEM() const { return this->units_per_em; }
67 /**
68 * Get the SpriteID mapped to the given key
69 * @param key The key to get the sprite for.
70 * @return The sprite.
72 virtual SpriteID GetUnicodeGlyph(WChar key) = 0;
74 /**
75 * Map a SpriteID to the key
76 * @param key The key to map to.
77 * @param sprite The sprite that is being mapped.
79 virtual void SetUnicodeGlyph(WChar key, SpriteID sprite) = 0;
81 /** Initialize the glyph map */
82 virtual void InitializeUnicodeGlyphMap() = 0;
84 /** Clear the font cache. */
85 virtual void ClearFontCache() = 0;
87 /**
88 * Get the glyph (sprite) of the given key.
89 * @param key The key to look up.
90 * @return The sprite.
92 virtual const Sprite *GetGlyph(GlyphID key) = 0;
94 /**
95 * Get the width of the glyph with the given key.
96 * @param key The key to look up.
97 * @return The width.
99 virtual uint GetGlyphWidth(GlyphID key) = 0;
102 * Do we need to draw a glyph shadow?
103 * @return True if it has to be done, otherwise false.
105 virtual bool GetDrawGlyphShadow() = 0;
108 * Map a character into a glyph.
109 * @param key The character.
110 * @return The glyph ID used to draw the character.
112 virtual GlyphID MapCharToGlyph(WChar key) = 0;
115 * Read a font table from the font.
116 * @param tag The of the table to load.
117 * @param length The length of the read data.
118 * @return The loaded table data.
120 virtual const void *GetFontTable(uint32 tag, size_t &length) = 0;
123 * Get the name of this font.
124 * @return The name of the font.
126 virtual const char *GetFontName() = 0;
129 * Get the font cache of a given font size.
130 * @param fs The font size to look up.
131 * @return The font cache.
133 static inline FontCache *Get(FontSize fs)
135 assert(fs < FS_END);
136 return FontCache::caches[fs];
140 * Check whether the font cache has a parent.
142 inline bool HasParent()
144 return this->parent != NULL;
148 /** Get the SpriteID mapped to the given font size and key */
149 static inline SpriteID GetUnicodeGlyph(FontSize size, WChar key)
151 return FontCache::Get(size)->GetUnicodeGlyph(key);
154 /** Map a SpriteID to the font size and key */
155 static inline void SetUnicodeGlyph(FontSize size, WChar key, SpriteID sprite)
157 FontCache::Get(size)->SetUnicodeGlyph(key, sprite);
160 /** Initialize the glyph map */
161 static inline void InitializeUnicodeGlyphMap()
163 for (FontSize fs = FS_BEGIN; fs < FS_END; fs++) {
164 FontCache::Get(fs)->InitializeUnicodeGlyphMap();
168 static inline void ClearFontCache()
170 for (FontSize fs = FS_BEGIN; fs < FS_END; fs++) {
171 FontCache::Get(fs)->ClearFontCache();
175 /** Get the Sprite for a glyph */
176 static inline const Sprite *GetGlyph(FontSize size, WChar key)
178 FontCache *fc = FontCache::Get(size);
179 return fc->GetGlyph(fc->MapCharToGlyph(key));
182 /** Get the width of a glyph */
183 static inline uint GetGlyphWidth(FontSize size, WChar key)
185 FontCache *fc = FontCache::Get(size);
186 return fc->GetGlyphWidth(fc->MapCharToGlyph(key));
189 static inline bool GetDrawGlyphShadow(FontSize size)
191 return FontCache::Get(size)->GetDrawGlyphShadow();
194 #ifdef WITH_FREETYPE
196 /** Settings for a single freetype font. */
197 struct FreeTypeSubSetting {
198 char font[MAX_PATH]; ///< The name of the font, or path to the font.
199 uint size; ///< The (requested) size of the font.
200 bool aa; ///< Whether to do anti aliasing or not.
203 /** Settings for the freetype fonts. */
204 struct FreeTypeSettings {
205 FreeTypeSubSetting small; ///< The smallest font; mostly used for zoomed out view.
206 FreeTypeSubSetting medium; ///< The normal font size.
207 FreeTypeSubSetting large; ///< The largest font; mostly used for newspapers.
208 FreeTypeSubSetting mono; ///< The mono space font used for license/readme viewers.
211 extern FreeTypeSettings _freetype;
213 #endif /* WITH_FREETYPE */
215 void InitFreeType(bool monospace);
216 void UninitFreeType();
218 #endif /* FONTCACHE_H */