OpenTTD Source: src/fontcache/truetypefontcache.h Source File

OpenTTD Source 14.1

truetypefontcache.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 TRUETYPEFONTCACHE_H
11 #define TRUETYPEFONTCACHE_H
12
13 #include "../fontcache.h"
14
15
16 static const int MAX_FONT_SIZE = 72;
17
18 static const byte FACE_COLOUR = 1;
19 static const byte SHADOW_COLOUR = 2;
20
22 class TrueTypeFontCache : public FontCache {
23 protected:
24 static constexpr int MAX_GLYPH_DIM = 256;
25 static constexpr uint MAX_FONT_MIN_REC_SIZE = 20u;
26
27 int req_size;
28 int used_size;
29
30 using FontTable = std::map<uint32_t, std::pair<size_t, const void *>>;
31 FontTable font_tables;
32
34 struct GlyphEntry {
35 Sprite *sprite;
36 byte width;
37 bool duplicate;
38 };
39
53 GlyphEntry **glyph_to_sprite;
54
55 GlyphEntry *GetGlyphPtr(GlyphID key);
56 void SetGlyphPtr(GlyphID key, const GlyphEntry *glyph, bool duplicate = false);
57
58 virtual const void *InternalGetFontTable(uint32_t tag, size_t &length) = 0;
59 virtual const Sprite *InternalGetGlyph(GlyphID key, bool aa) = 0;
60
61 public:
62 TrueTypeFontCache(FontSize fs, int pixels);
63 virtual ~TrueTypeFontCache();
64 int GetFontSize() const override { return this->used_size; }
65 void SetUnicodeGlyph(char32_t key, SpriteID sprite) override { this->parent->SetUnicodeGlyph(key, sprite); }
66 void InitializeUnicodeGlyphMap() override { this->parent->InitializeUnicodeGlyphMap(); }
67 const Sprite *GetGlyph(GlyphID key) override;
68 const void *GetFontTable(uint32_t tag, size_t &length) override;
69 void ClearFontCache() override;
70 uint GetGlyphWidth(GlyphID key) override;
71 bool GetDrawGlyphShadow() override;
72 bool IsBuiltInFont() override { return false; }
73 };
74
75 #endif /* TRUETYPEFONTCACHE_H */
TrueTypeFontCache::GlyphEntry::sprite
Sprite * sprite
The loaded sprite.
Definition: truetypefontcache.h:35
TrueTypeFontCache::GetFontTable
const void * GetFontTable(uint32_t tag, size_t &length) override
Read a font table from the font.
Definition: truetypefontcache.cpp:168
TrueTypeFontCache::font_tables
FontTable font_tables
Cached font tables.
Definition: truetypefontcache.h:31
TrueTypeFontCache::~TrueTypeFontCache
virtual ~TrueTypeFontCache()
Free everything that was allocated for this font cache.
Definition: truetypefontcache.cpp:32
TrueTypeFontCache
Font cache for fonts that are based on a TrueType font.
Definition: truetypefontcache.h:22
TrueTypeFontCache::GlyphEntry::duplicate
bool duplicate
Whether this glyph entry is a duplicate, i.e. may this be freed?
Definition: truetypefontcache.h:37
FontCache::SetUnicodeGlyph
virtual void SetUnicodeGlyph(char32_t key, SpriteID sprite)=0
Map a SpriteID to the key.
TrueTypeFontCache::glyph_to_sprite
GlyphEntry ** glyph_to_sprite
The glyph cache.
Definition: truetypefontcache.h:53
TrueTypeFontCache::GlyphEntry
Container for information about a glyph.
Definition: truetypefontcache.h:34
TrueTypeFontCache::GetGlyphWidth
uint GetGlyphWidth(GlyphID key) override
Get the width of the glyph with the given key.
Definition: truetypefontcache.cpp:97
TrueTypeFontCache::GetDrawGlyphShadow
bool GetDrawGlyphShadow() override
Do we need to draw a glyph shadow?
Definition: truetypefontcache.cpp:92
TrueTypeFontCache::GetFontSize
int GetFontSize() const override
Get the nominal font size of the font.
Definition: truetypefontcache.h:64
TrueTypeFontCache::MAX_GLYPH_DIM
static constexpr int MAX_GLYPH_DIM
Maximum glyph dimensions.
Definition: truetypefontcache.h:24
TrueTypeFontCache::MAX_FONT_MIN_REC_SIZE
static constexpr uint MAX_FONT_MIN_REC_SIZE
Upper limit for the recommended font size in case a font file contains nonsensical values.
Definition: truetypefontcache.h:25
GlyphID
uint32_t GlyphID
Glyphs are characters from a font.
Definition: fontcache.h:17
FontCache::fs
const FontSize fs
The size of the font.
Definition: fontcache.h:25
SpriteID
uint32_t SpriteID
The number of a sprite, without mapping bits and colourtables.
Definition: gfx_type.h:17
TrueTypeFontCache::SetUnicodeGlyph
void SetUnicodeGlyph(char32_t key, SpriteID sprite) override
Map a SpriteID to the key.
Definition: truetypefontcache.h:65
TrueTypeFontCache::req_size
int req_size
Requested font size.
Definition: truetypefontcache.h:27
FontCache
Font cache for basic fonts.
Definition: fontcache.h:21
FontCache::parent
FontCache * parent
The parent of this font cache.
Definition: fontcache.h:24
TrueTypeFontCache::ClearFontCache
void ClearFontCache() override
Reset cached glyphs.
Definition: truetypefontcache.cpp:45
TrueTypeFontCache::used_size
int used_size
Used font size.
Definition: truetypefontcache.h:28
TrueTypeFontCache::TrueTypeFontCache
TrueTypeFontCache(FontSize fs, int pixels)
Create a new TrueTypeFontCache.
Definition: truetypefontcache.cpp:25
TrueTypeFontCache::FontTable
std::map< uint32_t, std::pair< size_t, const void * > > FontTable
Table with font table cache.
Definition: truetypefontcache.h:30
TrueTypeFontCache::GlyphEntry::width
byte width
The width of the glyph.
Definition: truetypefontcache.h:36
TrueTypeFontCache::InitializeUnicodeGlyphMap
void InitializeUnicodeGlyphMap() override
Initialize the glyph map.
Definition: truetypefontcache.h:66
FontSize
FontSize
Available font sizes.
Definition: gfx_type.h:202
TrueTypeFontCache::GetGlyph
const Sprite * GetGlyph(GlyphID key) override
Get the glyph (sprite) of the given key.
Definition: truetypefontcache.cpp:110
Sprite
Data structure describing a sprite.
Definition: spritecache.h:17
FontCache::InitializeUnicodeGlyphMap
virtual void InitializeUnicodeGlyphMap()=0
Initialize the glyph map.
TrueTypeFontCache::IsBuiltInFont
bool IsBuiltInFont() override
Is this a built-in sprite font?
Definition: truetypefontcache.h:72
MAX_FONT_SIZE
static const int MAX_FONT_SIZE
Maximum font size.
Definition: truetypefontcache.h:16