OpenTTD Source  20240919-master-gdf0233f4c2
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 uint8_t FACE_COLOUR = 1;
19 static const uint8_t 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 
31  struct GlyphEntry {
32  std::unique_ptr<uint8_t[]> data;
33  uint8_t width = 0;
34 
35  Sprite *GetSprite() { return reinterpret_cast<Sprite *>(data.get()); }
36  };
37 
38  std::unordered_map<GlyphID, GlyphEntry> glyph_to_sprite_map{};
39 
40  GlyphEntry *GetGlyphPtr(GlyphID key);
41  GlyphEntry &SetGlyphPtr(GlyphID key, GlyphEntry &&glyph);
42 
43  virtual const Sprite *InternalGetGlyph(GlyphID key, bool aa) = 0;
44 
45 public:
46  TrueTypeFontCache(FontSize fs, int pixels);
47  virtual ~TrueTypeFontCache();
48  int GetFontSize() const override { return this->used_size; }
49  void SetUnicodeGlyph(char32_t key, SpriteID sprite) override { this->parent->SetUnicodeGlyph(key, sprite); }
51  const Sprite *GetGlyph(GlyphID key) override;
52  void ClearFontCache() override;
53  uint GetGlyphWidth(GlyphID key) override;
54  bool GetDrawGlyphShadow() override;
55  bool IsBuiltInFont() override { return false; }
56 };
57 
58 #endif /* TRUETYPEFONTCACHE_H */
TrueTypeFontCache::~TrueTypeFontCache
virtual ~TrueTypeFontCache()
Free everything that was allocated for this font cache.
Definition: truetypefontcache.cpp:31
TrueTypeFontCache
Font cache for fonts that are based on a TrueType font.
Definition: truetypefontcache.h:22
FontCache::SetUnicodeGlyph
virtual void SetUnicodeGlyph(char32_t key, SpriteID sprite)=0
Map a SpriteID to the key.
TrueTypeFontCache::GlyphEntry
Container for information about a glyph.
Definition: truetypefontcache.h:31
TrueTypeFontCache::GetGlyphWidth
uint GetGlyphWidth(GlyphID key) override
Get the width of the glyph with the given key.
Definition: truetypefontcache.cpp:65
TrueTypeFontCache::GetDrawGlyphShadow
bool GetDrawGlyphShadow() override
Do we need to draw a glyph shadow?
Definition: truetypefontcache.cpp:60
TrueTypeFontCache::GetFontSize
int GetFontSize() const override
Get the nominal font size of the font.
Definition: truetypefontcache.h:48
TrueTypeFontCache::GlyphEntry::data
std::unique_ptr< uint8_t[]> data
The loaded sprite.
Definition: truetypefontcache.h:32
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:18
TrueTypeFontCache::SetUnicodeGlyph
void SetUnicodeGlyph(char32_t key, SpriteID sprite) override
Map a SpriteID to the key.
Definition: truetypefontcache.h:49
TrueTypeFontCache::req_size
int req_size
Requested font size.
Definition: truetypefontcache.h:27
TrueTypeFontCache::GlyphEntry::width
uint8_t width
The width of the glyph.
Definition: truetypefontcache.h:33
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:40
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:24
TrueTypeFontCache::InitializeUnicodeGlyphMap
void InitializeUnicodeGlyphMap() override
Initialize the glyph map.
Definition: truetypefontcache.h:50
FontSize
FontSize
Available font sizes.
Definition: gfx_type.h:208
TrueTypeFontCache::GetGlyph
const Sprite * GetGlyph(GlyphID key) override
Get the glyph (sprite) of the given key.
Definition: truetypefontcache.cpp:78
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:55
MAX_FONT_SIZE
static const int MAX_FONT_SIZE
Maximum font size.
Definition: truetypefontcache.h:16