OpenTTD Source  20241108-master-g80f628063a
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 */
Font cache for basic fonts.
Definition: fontcache.h:21
FontCache * parent
The parent of this font cache.
Definition: fontcache.h:24
virtual void InitializeUnicodeGlyphMap()=0
Initialize the glyph map.
const FontSize fs
The size of the font.
Definition: fontcache.h:25
virtual void SetUnicodeGlyph(char32_t key, SpriteID sprite)=0
Map a SpriteID to the key.
Font cache for fonts that are based on a TrueType font.
static constexpr int MAX_GLYPH_DIM
Maximum glyph dimensions.
int used_size
Used font size.
uint GetGlyphWidth(GlyphID key) override
Get the width of the glyph with the given key.
void InitializeUnicodeGlyphMap() override
Initialize the glyph map.
void SetUnicodeGlyph(char32_t key, SpriteID sprite) override
Map a SpriteID to the key.
const Sprite * GetGlyph(GlyphID key) override
Get the glyph (sprite) of the given key.
bool GetDrawGlyphShadow() override
Do we need to draw a glyph shadow?
int req_size
Requested font size.
int GetFontSize() const override
Get the nominal font size of the font.
void ClearFontCache() override
Reset cached glyphs.
bool IsBuiltInFont() override
Is this a built-in sprite font?
virtual ~TrueTypeFontCache()
Free everything that was allocated for this font cache.
static constexpr uint MAX_FONT_MIN_REC_SIZE
Upper limit for the recommended font size in case a font file contains nonsensical values.
TrueTypeFontCache(FontSize fs, int pixels)
Create a new TrueTypeFontCache.
uint32_t GlyphID
Glyphs are characters from a font.
Definition: fontcache.h:17
uint32_t SpriteID
The number of a sprite, without mapping bits and colourtables.
Definition: gfx_type.h:18
FontSize
Available font sizes.
Definition: gfx_type.h:208
Data structure describing a sprite.
Definition: spritecache.h:17
Container for information about a glyph.
std::unique_ptr< uint8_t[]> data
The loaded sprite.
uint8_t width
The width of the glyph.
static const int MAX_FONT_SIZE
Maximum font size.