Fix: CmdSetAutoReplace didn't validate group type and engine type match (#9950)
[openttd-github.git] / src / fontcache_internal.h
blobacae3d71de13b7e1d89b9a83597c18888594d904
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 */
8 /** @file fontcache_internal.h Support types and functions for platform-specific font support. */
10 #ifndef FONTCACHE_INTERNAL_H
11 #define FONTCACHE_INTERNAL_H
13 #include "core/smallmap_type.hpp"
14 #include "fontcache.h"
17 static const int MAX_FONT_SIZE = 72; ///< Maximum font size.
19 static const byte FACE_COLOUR = 1;
20 static const byte SHADOW_COLOUR = 2;
22 /** Font cache for fonts that are based on a TrueType font. */
23 class TrueTypeFontCache : public FontCache {
24 protected:
25 static constexpr int MAX_GLYPH_DIM = 256; ///< Maximum glyph dimensions.
26 static constexpr uint MAX_FONT_MIN_REC_SIZE = 20u; ///< Upper limit for the recommended font size in case a font file contains nonsensical values.
28 int req_size; ///< Requested font size.
29 int used_size; ///< Used font size.
31 typedef SmallMap<uint32, std::pair<size_t, const void *> > FontTable; ///< Table with font table cache
32 FontTable font_tables; ///< Cached font tables.
34 /** Container for information about a glyph. */
35 struct GlyphEntry {
36 Sprite *sprite; ///< The loaded sprite.
37 byte width; ///< The width of the glyph.
38 bool duplicate; ///< Whether this glyph entry is a duplicate, i.e. may this be freed?
41 /**
42 * The glyph cache. This is structured to reduce memory consumption.
43 * 1) There is a 'segment' table for each font size.
44 * 2) Each segment table is a discrete block of characters.
45 * 3) Each block contains 256 (aligned) characters sequential characters.
47 * The cache is accessed in the following way:
48 * For character 0x0041 ('A'): glyph_to_sprite[0x00][0x41]
49 * For character 0x20AC (Euro): glyph_to_sprite[0x20][0xAC]
51 * Currently only 256 segments are allocated, "limiting" us to 65536 characters.
52 * This can be simply changed in the two functions Get & SetGlyphPtr.
54 GlyphEntry **glyph_to_sprite;
56 GlyphEntry *GetGlyphPtr(GlyphID key);
57 void SetGlyphPtr(GlyphID key, const GlyphEntry *glyph, bool duplicate = false);
59 virtual const void *InternalGetFontTable(uint32 tag, size_t &length) = 0;
60 virtual const Sprite *InternalGetGlyph(GlyphID key, bool aa) = 0;
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); }
68 void InitializeUnicodeGlyphMap() override { this->parent->InitializeUnicodeGlyphMap(); }
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; }
77 #endif /* FONTCACHE_INTERNAL_H */