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/>.
8 /** @file truetypefontcache.h Common base definition for font file based font caches. */
10 #ifndef TRUETYPEFONTCACHE_H
11 #define TRUETYPEFONTCACHE_H
13 #include "../fontcache.h"
16 static const int MAX_FONT_SIZE
= 72; ///< Maximum font size.
18 static const byte FACE_COLOUR
= 1;
19 static const byte SHADOW_COLOUR
= 2;
21 /** Font cache for fonts that are based on a TrueType font. */
22 class TrueTypeFontCache
: public FontCache
{
24 static constexpr int MAX_GLYPH_DIM
= 256; ///< Maximum glyph dimensions.
25 static constexpr uint MAX_FONT_MIN_REC_SIZE
= 20u; ///< Upper limit for the recommended font size in case a font file contains nonsensical values.
27 int req_size
; ///< Requested font size.
28 int used_size
; ///< Used font size.
30 using FontTable
= std::map
<uint32_t, std::pair
<size_t, const void *>>; ///< Table with font table cache
31 FontTable font_tables
; ///< Cached font tables.
33 /** Container for information about a glyph. */
35 Sprite
*sprite
; ///< The loaded sprite.
36 byte width
; ///< The width of the glyph.
37 bool duplicate
; ///< Whether this glyph entry is a duplicate, i.e. may this be freed?
41 * The glyph cache. This is structured to reduce memory consumption.
42 * 1) There is a 'segment' table for each font size.
43 * 2) Each segment table is a discrete block of characters.
44 * 3) Each block contains 256 (aligned) characters sequential characters.
46 * The cache is accessed in the following way:
47 * For character 0x0041 ('A'): glyph_to_sprite[0x00][0x41]
48 * For character 0x20AC (Euro): glyph_to_sprite[0x20][0xAC]
50 * Currently only 256 segments are allocated, "limiting" us to 65536 characters.
51 * This can be simply changed in the two functions Get & SetGlyphPtr.
53 GlyphEntry
**glyph_to_sprite
;
55 GlyphEntry
*GetGlyphPtr(GlyphID key
);
56 void SetGlyphPtr(GlyphID key
, const GlyphEntry
*glyph
, bool duplicate
= false);
58 virtual const void *InternalGetFontTable(uint32_t tag
, size_t &length
) = 0;
59 virtual const Sprite
*InternalGetGlyph(GlyphID key
, bool aa
) = 0;
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; }
75 #endif /* TRUETYPEFONTCACHE_H */