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:
28  const FontSize fs;
29  int height;
30  int ascender;
31  int descender;
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++) {
165  }
166 }
167 
168 static inline void ClearFontCache()
169 {
170  for (FontSize fs = FS_BEGIN; fs < FS_END; fs++) {
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 
198  char font[MAX_PATH];
199  uint size;
200  bool aa;
201 };
202 
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 */
virtual const char * GetFontName()=0
Get the name of this font.
int height
The height of the font.
Definition: fontcache.h:29
void InitFreeType(bool monospace)
(Re)initialize the freetype related things, i.e.
Definition: fontcache.cpp:643
int descender
The descender value of the font.
Definition: fontcache.h:31
Settings for a single freetype font.
Definition: fontcache.h:197
Data structure describing a sprite.
Definition: spritecache.h:18
FreeTypeSubSetting large
The largest font; mostly used for newspapers.
Definition: fontcache.h:207
int units_per_em
The units per EM value of the font.
Definition: fontcache.h:32
virtual void SetUnicodeGlyph(WChar key, SpriteID sprite)=0
Map a SpriteID to the key.
static FontCache * Get(FontSize fs)
Get the font cache of a given font size.
Definition: fontcache.h:133
int GetAscender() const
Get the ascender value of the font.
Definition: fontcache.h:53
Settings for the freetype fonts.
Definition: fontcache.h:204
bool HasParent()
Check whether the font cache has a parent.
Definition: fontcache.h:142
bool aa
Whether to do anti aliasing or not.
Definition: fontcache.h:200
First font.
Definition: gfx_type.h:210
virtual ~FontCache()
Clean everything up.
Definition: fontcache.cpp:50
FreeTypeSubSetting mono
The mono space font used for license/readme viewers.
Definition: fontcache.h:208
FontCache(FontSize fs)
Create a new font cache.
Definition: fontcache.cpp:40
virtual uint GetGlyphWidth(GlyphID key)=0
Get the width of the glyph with the given key.
int GetUnitsPerEM() const
Get the units per EM value of the font.
Definition: fontcache.h:65
virtual int GetHeight() const
Get the height of the font.
Definition: fontcache.h:47
FreeTypeSubSetting medium
The normal font size.
Definition: fontcache.h:206
Functions to cache sprites in memory.
int GetDescender() const
Get the descender value of the font.
Definition: fontcache.h:59
virtual GlyphID MapCharToGlyph(WChar key)=0
Map a character into a glyph.
Font cache for basic fonts.
Definition: fontcache.h:23
FontCache * parent
The parent of this font cache.
Definition: fontcache.h:27
uint size
The (requested) size of the font.
Definition: fontcache.h:199
virtual const void * GetFontTable(uint32 tag, size_t &length)=0
Read a font table from the font.
uint32 SpriteID
The number of a sprite, without mapping bits and colourtables.
Definition: gfx_type.h:19
static FontCache * caches[FS_END]
All the font caches.
Definition: fontcache.h:25
FontSize GetSize() const
Get the FontSize of the font.
Definition: fontcache.h:41
FreeTypeSubSetting small
The smallest font; mostly used for zoomed out view.
Definition: fontcache.h:205
virtual const Sprite * GetGlyph(GlyphID key)=0
Get the glyph (sprite) of the given key.
FontSize
Available font sizes.
Definition: gfx_type.h:203
virtual bool GetDrawGlyphShadow()=0
Do we need to draw a glyph shadow?
virtual void InitializeUnicodeGlyphMap()=0
Initialize the glyph map.
int ascender
The ascender value of the font.
Definition: fontcache.h:30
const FontSize fs
The size of the font.
Definition: fontcache.h:28
Types for strings.
void UninitFreeType()
Free everything allocated w.r.t.
Definition: fontcache.cpp:660
uint32 GlyphID
Glyphs are characters from a font.
Definition: fontcache.h:19
virtual void ClearFontCache()=0
Clear the font cache.
uint32 WChar
Type for wide characters, i.e.
Definition: string_type.h:35
virtual SpriteID GetUnicodeGlyph(WChar key)=0
Get the SpriteID mapped to the given key.