OpenTTD

fontcache.h
Go to the documentation of this file.
1 /* $Id: fontcache.h 27004 2014-10-12 20:43:25Z peter1138 $ */
2
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 */
9
12 #ifndef FONTCACHE_H
13 #define FONTCACHE_H
14
15 #include "string_type.h"
16 #include "spritecache.h"
17
19 typedef uint32 GlyphID;
20 static const GlyphID SPRITE_GLYPH = 1U << 30;
21
23 class FontCache {
24 private:
25 static FontCache *caches[FS_END];
26 protected:
27 FontCache *parent;
28 const FontSize fs;
29 int height;
30 int ascender;
31 int descender;
32 int units_per_em;
33 public:
34 FontCache(FontSize fs);
35 virtual ~FontCache();
36
41 inline FontSize GetSize() const { return this->fs; }
42
47 virtual int GetHeight() const { return this->height; }
48
53 inline int GetAscender() const { return this->ascender; }
54
59 inline int GetDescender() const{ return this->descender; }
60
65 inline int GetUnitsPerEM() const { return this->units_per_em; }
66
72 virtual SpriteID GetUnicodeGlyph(WChar key) = 0;
73
79 virtual void SetUnicodeGlyph(WChar key, SpriteID sprite) = 0;
80
82 virtual void InitializeUnicodeGlyphMap() = 0;
83
85 virtual void ClearFontCache() = 0;
86
92 virtual const Sprite *GetGlyph(GlyphID key) = 0;
93
99 virtual uint GetGlyphWidth(GlyphID key) = 0;
100
105 virtual bool GetDrawGlyphShadow() = 0;
106
112 virtual GlyphID MapCharToGlyph(WChar key) = 0;
113
120 virtual const void *GetFontTable(uint32 tag, size_t &length) = 0;
121
126 virtual const char *GetFontName() = 0;
127
133 static inline FontCache *Get(FontSize fs)
134 {
135 assert(fs < FS_END);
136 return FontCache::caches[fs];
137 }
138
142 inline bool HasParent()
143 {
144 return this->parent != NULL;
145 }
146 };
147
149 static inline SpriteID GetUnicodeGlyph(FontSize size, WChar key)
150 {
151 return FontCache::Get(size)->GetUnicodeGlyph(key);
152 }
153
155 static inline void SetUnicodeGlyph(FontSize size, WChar key, SpriteID sprite)
156 {
157 FontCache::Get(size)->SetUnicodeGlyph(key, sprite);
158 }
159
161 static inline void InitializeUnicodeGlyphMap()
162 {
163 for (FontSize fs = FS_BEGIN; fs < FS_END; fs++) {
164 FontCache::Get(fs)->InitializeUnicodeGlyphMap();
165 }
166 }
167
168 static inline void ClearFontCache()
169 {
170 for (FontSize fs = FS_BEGIN; fs < FS_END; fs++) {
171 FontCache::Get(fs)->ClearFontCache();
172 }
173 }
174
176 static inline const Sprite *GetGlyph(FontSize size, WChar key)
177 {
178 FontCache *fc = FontCache::Get(size);
179 return fc->GetGlyph(fc->MapCharToGlyph(key));
180 }
181
183 static inline uint GetGlyphWidth(FontSize size, WChar key)
184 {
185 FontCache *fc = FontCache::Get(size);
186 return fc->GetGlyphWidth(fc->MapCharToGlyph(key));
187 }
188
189 static inline bool GetDrawGlyphShadow(FontSize size)
190 {
191 return FontCache::Get(size)->GetDrawGlyphShadow();
192 }
193
194 #ifdef WITH_FREETYPE
195
197 struct FreeTypeSubSetting {
198 char font[MAX_PATH];
199 uint size;
200 bool aa;
201 };
202
204 struct FreeTypeSettings {
205 FreeTypeSubSetting small;
206 FreeTypeSubSetting medium;
207 FreeTypeSubSetting large;
208 FreeTypeSubSetting mono;
209 };
210
211 extern FreeTypeSettings _freetype;
212
213 #endif /* WITH_FREETYPE */
214
215 void InitFreeType(bool monospace);
216 void UninitFreeType();
217
218 #endif /* FONTCACHE_H */
FontCache::GetFontName
virtual const char * GetFontName()=0
Get the name of this font.
FontCache::height
int height
The height of the font.
Definition: fontcache.h:29
InitFreeType
void InitFreeType(bool monospace)
(Re)initialize the freetype related things, i.e.
Definition: fontcache.cpp:643
FontCache::descender
int descender
The descender value of the font.
Definition: fontcache.h:31
FreeTypeSubSetting
Settings for a single freetype font.
Definition: fontcache.h:197
Sprite
Data structure describing a sprite.
Definition: spritecache.h:18
FreeTypeSettings::large
FreeTypeSubSetting large
The largest font; mostly used for newspapers.
Definition: fontcache.h:207
FontCache::units_per_em
int units_per_em
The units per EM value of the font.
Definition: fontcache.h:32
FontCache::SetUnicodeGlyph
virtual void SetUnicodeGlyph(WChar key, SpriteID sprite)=0
Map a SpriteID to the key.
FontCache::Get
static FontCache * Get(FontSize fs)
Get the font cache of a given font size.
Definition: fontcache.h:133
FontCache::GetAscender
int GetAscender() const
Get the ascender value of the font.
Definition: fontcache.h:53
FreeTypeSettings
Settings for the freetype fonts.
Definition: fontcache.h:204
FontCache::HasParent
bool HasParent()
Check whether the font cache has a parent.
Definition: fontcache.h:142
FreeTypeSubSetting::aa
bool aa
Whether to do anti aliasing or not.
Definition: fontcache.h:200
FS_BEGIN
First font.
Definition: gfx_type.h:210
FontCache::~FontCache
virtual ~FontCache()
Clean everything up.
Definition: fontcache.cpp:50
FreeTypeSettings::mono
FreeTypeSubSetting mono
The mono space font used for license/readme viewers.
Definition: fontcache.h:208
FontCache::FontCache
FontCache(FontSize fs)
Create a new font cache.
Definition: fontcache.cpp:40
FontCache::GetGlyphWidth
virtual uint GetGlyphWidth(GlyphID key)=0
Get the width of the glyph with the given key.
FontCache::GetUnitsPerEM
int GetUnitsPerEM() const
Get the units per EM value of the font.
Definition: fontcache.h:65
FontCache::GetHeight
virtual int GetHeight() const
Get the height of the font.
Definition: fontcache.h:47
FreeTypeSettings::medium
FreeTypeSubSetting medium
The normal font size.
Definition: fontcache.h:206
spritecache.h
Functions to cache sprites in memory.
FontCache::GetDescender
int GetDescender() const
Get the descender value of the font.
Definition: fontcache.h:59
FontCache::MapCharToGlyph
virtual GlyphID MapCharToGlyph(WChar key)=0
Map a character into a glyph.
FontCache
Font cache for basic fonts.
Definition: fontcache.h:23
FontCache::parent
FontCache * parent
The parent of this font cache.
Definition: fontcache.h:27
FreeTypeSubSetting::size
uint size
The (requested) size of the font.
Definition: fontcache.h:199
FontCache::GetFontTable
virtual const void * GetFontTable(uint32 tag, size_t &length)=0
Read a font table from the font.
SpriteID
uint32 SpriteID
The number of a sprite, without mapping bits and colourtables.
Definition: gfx_type.h:19
FontCache::caches
static FontCache * caches[FS_END]
All the font caches.
Definition: fontcache.h:25
FontCache::GetSize
FontSize GetSize() const
Get the FontSize of the font.
Definition: fontcache.h:41
FreeTypeSettings::small
FreeTypeSubSetting small
The smallest font; mostly used for zoomed out view.
Definition: fontcache.h:205
FontCache::GetGlyph
virtual const Sprite * GetGlyph(GlyphID key)=0
Get the glyph (sprite) of the given key.
FontSize
FontSize
Available font sizes.
Definition: gfx_type.h:203
FontCache::GetDrawGlyphShadow
virtual bool GetDrawGlyphShadow()=0
Do we need to draw a glyph shadow?
FontCache::InitializeUnicodeGlyphMap
virtual void InitializeUnicodeGlyphMap()=0
Initialize the glyph map.
FontCache::ascender
int ascender
The ascender value of the font.
Definition: fontcache.h:30
FontCache::fs
const FontSize fs
The size of the font.
Definition: fontcache.h:28
string_type.h
Types for strings.
UninitFreeType
void UninitFreeType()
Free everything allocated w.r.t.
Definition: fontcache.cpp:660
GlyphID
uint32 GlyphID
Glyphs are characters from a font.
Definition: fontcache.h:19
FontCache::ClearFontCache
virtual void ClearFontCache()=0
Clear the font cache.
WChar
uint32 WChar
Type for wide characters, i.e.
Definition: string_type.h:35
FontCache::GetUnicodeGlyph
virtual SpriteID GetUnicodeGlyph(WChar key)=0
Get the SpriteID mapped to the given key.