OpenTTD
fontcache.h
Go to the documentation of this file.
1 /*
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/>.
6  */
7 
10 #ifndef FONTCACHE_H
11 #define FONTCACHE_H
12 
13 #include "string_type.h"
14 #include "spritecache.h"
15 
17 typedef uint32 GlyphID;
18 static const GlyphID SPRITE_GLYPH = 1U << 30;
19 
21 class FontCache {
22 private:
23  static FontCache *caches[FS_END];
24 protected:
26  const FontSize fs;
27  int height;
28  int ascender;
29  int descender;
31 public:
32  FontCache(FontSize fs);
33  virtual ~FontCache();
34 
39  inline FontSize GetSize() const { return this->fs; }
40 
45  virtual int GetHeight() const { return this->height; }
46 
51  inline int GetAscender() const { return this->ascender; }
52 
57  inline int GetDescender() const{ return this->descender; }
58 
63  inline int GetUnitsPerEM() const { return this->units_per_em; }
64 
69  virtual int GetFontSize() const { return this->height; }
70 
76  virtual SpriteID GetUnicodeGlyph(WChar key) = 0;
77 
83  virtual void SetUnicodeGlyph(WChar key, SpriteID sprite) = 0;
84 
86  virtual void InitializeUnicodeGlyphMap() = 0;
87 
89  virtual void ClearFontCache() = 0;
90 
96  virtual const Sprite *GetGlyph(GlyphID key) = 0;
97 
103  virtual uint GetGlyphWidth(GlyphID key) = 0;
104 
109  virtual bool GetDrawGlyphShadow() = 0;
110 
116  virtual GlyphID MapCharToGlyph(WChar key) = 0;
117 
124  virtual const void *GetFontTable(uint32 tag, size_t &length) = 0;
125 
130  virtual void *GetOSHandle()
131  {
132  return nullptr;
133  }
134 
139  virtual const char *GetFontName() = 0;
140 
146  static inline FontCache *Get(FontSize fs)
147  {
148  assert(fs < FS_END);
149  return FontCache::caches[fs];
150  }
151 
155  inline bool HasParent()
156  {
157  return this->parent != nullptr;
158  }
159 
163  virtual bool IsBuiltInFont() = 0;
164 };
165 
167 static inline SpriteID GetUnicodeGlyph(FontSize size, WChar key)
168 {
169  return FontCache::Get(size)->GetUnicodeGlyph(key);
170 }
171 
173 static inline void SetUnicodeGlyph(FontSize size, WChar key, SpriteID sprite)
174 {
175  FontCache::Get(size)->SetUnicodeGlyph(key, sprite);
176 }
177 
179 static inline void InitializeUnicodeGlyphMap()
180 {
181  for (FontSize fs = FS_BEGIN; fs < FS_END; fs++) {
183  }
184 }
185 
186 static inline void ClearFontCache()
187 {
188  for (FontSize fs = FS_BEGIN; fs < FS_END; fs++) {
190  }
191 }
192 
194 static inline const Sprite *GetGlyph(FontSize size, WChar key)
195 {
196  FontCache *fc = FontCache::Get(size);
197  return fc->GetGlyph(fc->MapCharToGlyph(key));
198 }
199 
201 static inline uint GetGlyphWidth(FontSize size, WChar key)
202 {
203  FontCache *fc = FontCache::Get(size);
204  return fc->GetGlyphWidth(fc->MapCharToGlyph(key));
205 }
206 
207 static inline bool GetDrawGlyphShadow(FontSize size)
208 {
209  return FontCache::Get(size)->GetDrawGlyphShadow();
210 }
211 
212 #if defined(WITH_FREETYPE) || defined(_WIN32)
213 
216  char font[MAX_PATH];
217  uint size;
218  bool aa;
219 
220  const void *os_handle = nullptr;
221 };
222 
229 };
230 
231 extern FreeTypeSettings _freetype;
232 
233 #endif /* defined(WITH_FREETYPE) || defined(_WIN32) */
234 
235 void InitFreeType(bool monospace);
236 void UninitFreeType();
237 
238 #endif /* FONTCACHE_H */
virtual const char * GetFontName()=0
Get the name of this font.
int height
The height of the font.
Definition: fontcache.h:27
virtual int GetFontSize() const
Get the nominal font size of the font.
Definition: fontcache.h:69
void InitFreeType(bool monospace)
(Re)initialize the freetype related things, i.e.
Definition: fontcache.cpp:1034
virtual void * GetOSHandle()
Get the native OS font handle, if there is one.
Definition: fontcache.h:130
int descender
The descender value of the font.
Definition: fontcache.h:29
Settings for a single freetype font.
Definition: fontcache.h:215
Data structure describing a sprite.
Definition: spritecache.h:16
FreeTypeSubSetting large
The largest font; mostly used for newspapers.
Definition: fontcache.h:227
int units_per_em
The units per EM value of the font.
Definition: fontcache.h:30
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:146
int GetAscender() const
Get the ascender value of the font.
Definition: fontcache.h:51
Settings for the freetype fonts.
Definition: fontcache.h:224
bool HasParent()
Check whether the font cache has a parent.
Definition: fontcache.h:155
bool aa
Whether to do anti aliasing or not.
Definition: fontcache.h:218
First font.
Definition: gfx_type.h:208
virtual ~FontCache()
Clean everything up.
Definition: fontcache.cpp:49
FreeTypeSubSetting mono
The mono space font used for license/readme viewers.
Definition: fontcache.h:228
FontCache(FontSize fs)
Create a new font cache.
Definition: fontcache.cpp:39
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:63
virtual int GetHeight() const
Get the height of the font.
Definition: fontcache.h:45
FreeTypeSubSetting medium
The normal font size.
Definition: fontcache.h:226
Functions to cache sprites in memory.
int GetDescender() const
Get the descender value of the font.
Definition: fontcache.h:57
virtual GlyphID MapCharToGlyph(WChar key)=0
Map a character into a glyph.
Font cache for basic fonts.
Definition: fontcache.h:21
FontCache * parent
The parent of this font cache.
Definition: fontcache.h:25
uint size
The (requested) size of the font.
Definition: fontcache.h:217
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:17
static FontCache * caches[FS_END]
All the font caches.
Definition: fontcache.h:23
FontSize GetSize() const
Get the FontSize of the font.
Definition: fontcache.h:39
FreeTypeSubSetting small
The smallest font; mostly used for zoomed out view.
Definition: fontcache.h:225
virtual const Sprite * GetGlyph(GlyphID key)=0
Get the glyph (sprite) of the given key.
FontSize
Available font sizes.
Definition: gfx_type.h:201
virtual bool GetDrawGlyphShadow()=0
Do we need to draw a glyph shadow?
virtual void InitializeUnicodeGlyphMap()=0
Initialize the glyph map.
virtual bool IsBuiltInFont()=0
Is this a built-in sprite font?
int ascender
The ascender value of the font.
Definition: fontcache.h:28
const FontSize fs
The size of the font.
Definition: fontcache.h:26
Types for strings.
void UninitFreeType()
Free everything allocated w.r.t.
Definition: fontcache.cpp:1053
uint32 GlyphID
Glyphs are characters from a font.
Definition: fontcache.h:17
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.