OpenTTD Source  1.11.0-beta2
fontcache_internal.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_INTERNAL_H
11 #define FONTCACHE_INTERNAL_H
12 
13 #include "core/smallmap_type.hpp"
14 #include "fontcache.h"
15 
16 
17 static const int MAX_FONT_SIZE = 72;
18 
19 static const byte FACE_COLOUR = 1;
20 static const byte SHADOW_COLOUR = 2;
21 
23 class TrueTypeFontCache : public FontCache {
24 protected:
25  static constexpr int MAX_GLYPH_DIM = 256;
26  static constexpr uint MAX_FONT_MIN_REC_SIZE = 20u;
27 
28  int req_size;
29  int used_size;
30 
33 
35  struct GlyphEntry {
37  byte width;
38  bool duplicate;
39  };
40 
55 
56  GlyphEntry *GetGlyphPtr(GlyphID key);
57  void SetGlyphPtr(GlyphID key, const GlyphEntry *glyph, bool duplicate = false);
58 
59  virtual const void *InternalGetFontTable(uint32 tag, size_t &length) = 0;
60  virtual const Sprite *InternalGetGlyph(GlyphID key, bool aa) = 0;
61 
62 public:
63  TrueTypeFontCache(FontSize fs, int pixels);
64  virtual ~TrueTypeFontCache();
65  int GetFontSize() const override { return this->used_size; }
66  SpriteID GetUnicodeGlyph(WChar key) override { return this->parent->GetUnicodeGlyph(key); }
67  void SetUnicodeGlyph(WChar key, SpriteID sprite) override { this->parent->SetUnicodeGlyph(key, sprite); }
69  const Sprite *GetGlyph(GlyphID key) override;
70  const void *GetFontTable(uint32 tag, size_t &length) override;
71  void ClearFontCache() override;
72  uint GetGlyphWidth(GlyphID key) override;
73  bool GetDrawGlyphShadow() override;
74  bool IsBuiltInFont() override { return false; }
75 };
76 
77 #endif /* FONTCACHE_INTERNAL_H */
GlyphID
uint32 GlyphID
Glyphs are characters from a font.
Definition: fontcache.h:17
TrueTypeFontCache::GlyphEntry::sprite
Sprite * sprite
The loaded sprite.
Definition: fontcache_internal.h:36
TrueTypeFontCache::font_tables
FontTable font_tables
Cached font tables.
Definition: fontcache_internal.h:32
FontCache::GetUnicodeGlyph
virtual SpriteID GetUnicodeGlyph(WChar key)=0
Get the SpriteID mapped to the given key.
TrueTypeFontCache::GetFontTable
const void * GetFontTable(uint32 tag, size_t &length) override
Read a font table from the font.
Definition: fontcache.cpp:372
TrueTypeFontCache::~TrueTypeFontCache
virtual ~TrueTypeFontCache()
Free everything that was allocated for this font cache.
Definition: fontcache.cpp:221
TrueTypeFontCache
Font cache for fonts that are based on a TrueType font.
Definition: fontcache_internal.h:23
TrueTypeFontCache::GlyphEntry::duplicate
bool duplicate
Whether this glyph entry is a duplicate, i.e. may this be freed?
Definition: fontcache_internal.h:38
WChar
char32_t WChar
Type for wide characters, i.e.
Definition: string_type.h:35
TrueTypeFontCache::glyph_to_sprite
GlyphEntry ** glyph_to_sprite
The glyph cache.
Definition: fontcache_internal.h:54
TrueTypeFontCache::GetUnicodeGlyph
SpriteID GetUnicodeGlyph(WChar key) override
Get the SpriteID mapped to the given key.
Definition: fontcache_internal.h:66
TrueTypeFontCache::GlyphEntry
Container for information about a glyph.
Definition: fontcache_internal.h:35
smallmap_type.hpp
TrueTypeFontCache::GetGlyphWidth
uint GetGlyphWidth(GlyphID key) override
Get the width of the glyph with the given key.
Definition: fontcache.cpp:301
TrueTypeFontCache::GetDrawGlyphShadow
bool GetDrawGlyphShadow() override
Do we need to draw a glyph shadow?
Definition: fontcache.cpp:296
SmallMap
Implementation of simple mapping class.
Definition: smallmap_type.hpp:26
SpriteID
uint32 SpriteID
The number of a sprite, without mapping bits and colourtables.
Definition: gfx_type.h:17
TrueTypeFontCache::GetFontSize
int GetFontSize() const override
Get the nominal font size of the font.
Definition: fontcache_internal.h:65
TrueTypeFontCache::MAX_GLYPH_DIM
static constexpr int MAX_GLYPH_DIM
Maximum glyph dimensions.
Definition: fontcache_internal.h:25
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: fontcache_internal.h:26
FontCache::fs
const FontSize fs
The size of the font.
Definition: fontcache.h:26
TrueTypeFontCache::FontTable
SmallMap< uint32, std::pair< size_t, const void * > > FontTable
Table with font table cache.
Definition: fontcache_internal.h:31
TrueTypeFontCache::req_size
int req_size
Requested font size.
Definition: fontcache_internal.h:28
FontCache
Font cache for basic fonts.
Definition: fontcache.h:21
MAX_FONT_SIZE
static const int MAX_FONT_SIZE
Maximum font size.
Definition: fontcache_internal.h:17
FontCache::parent
FontCache * parent
The parent of this font cache.
Definition: fontcache.h:25
TrueTypeFontCache::SetUnicodeGlyph
void SetUnicodeGlyph(WChar key, SpriteID sprite) override
Map a SpriteID to the key.
Definition: fontcache_internal.h:67
TrueTypeFontCache::ClearFontCache
void ClearFontCache() override
Reset cached glyphs.
Definition: fontcache.cpp:233
TrueTypeFontCache::used_size
int used_size
Used font size.
Definition: fontcache_internal.h:29
TrueTypeFontCache::TrueTypeFontCache
TrueTypeFontCache(FontSize fs, int pixels)
Create a new TrueTypeFontCache.
Definition: fontcache.cpp:214
TrueTypeFontCache::GlyphEntry::width
byte width
The width of the glyph.
Definition: fontcache_internal.h:37
TrueTypeFontCache::InitializeUnicodeGlyphMap
void InitializeUnicodeGlyphMap() override
Initialize the glyph map.
Definition: fontcache_internal.h:68
fontcache.h
FontSize
FontSize
Available font sizes.
Definition: gfx_type.h:206
TrueTypeFontCache::GetGlyph
const Sprite * GetGlyph(GlyphID key) override
Get the glyph (sprite) of the given key.
Definition: fontcache.cpp:314
FontCache::SetUnicodeGlyph
virtual void SetUnicodeGlyph(WChar key, SpriteID sprite)=0
Map a SpriteID to the key.
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: fontcache_internal.h:74