OpenTTD Source: src/fontcache/truetypefontcache.h Source File

OpenTTD Source 15.3

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 <https://www.gnu.org/licenses/old-licenses/gpl-2.0>.
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
18static const uint8_t FACE_COLOUR = 1;
19static const uint8_t SHADOW_COLOUR = 2;
20
22 class TrueTypeFontCache : public FontCache {
23protected:
24 static constexpr int MAX_GLYPH_DIM = 256;
25 static constexpr uint MAX_FONT_MIN_REC_SIZE = 20u;
26
27 int req_size = 0;
28 int used_size = 0;
29
31 struct GlyphEntry {
32 std::unique_ptr<std::byte[]> 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
45public:
46 TrueTypeFontCache(FontSize fs, int pixels);
47 virtual ~TrueTypeFontCache();
48 int GetFontSize() const override { return this->used_size; }
49 const Sprite *GetGlyph(GlyphID key) override;
50 void ClearFontCache() override;
51 uint GetGlyphWidth(GlyphID key) override;
52 bool GetDrawGlyphShadow() override;
53 bool IsBuiltInFont() override { return false; }
54};
55
56#endif /* TRUETYPEFONTCACHE_H */
FontCache
Font cache for basic fonts.
Definition fontcache.h:22
FontCache::fs
const FontSize fs
The size of the font.
Definition fontcache.h:26
TrueTypeFontCache
Font cache for fonts that are based on a TrueType font.
Definition truetypefontcache.h:22
TrueTypeFontCache::MAX_GLYPH_DIM
static constexpr int MAX_GLYPH_DIM
Maximum glyph dimensions.
Definition truetypefontcache.h:24
TrueTypeFontCache::used_size
int used_size
Used font size.
Definition truetypefontcache.h:28
TrueTypeFontCache::GetGlyphWidth
uint GetGlyphWidth(GlyphID key) override
Get the width of the glyph with the given key.
Definition truetypefontcache.cpp:65
TrueTypeFontCache::GetGlyph
const Sprite * GetGlyph(GlyphID key) override
Get the glyph (sprite) of the given key.
Definition truetypefontcache.cpp:78
TrueTypeFontCache::GetDrawGlyphShadow
bool GetDrawGlyphShadow() override
Do we need to draw a glyph shadow?
Definition truetypefontcache.cpp:60
TrueTypeFontCache::req_size
int req_size
Requested font size.
Definition truetypefontcache.h:27
TrueTypeFontCache::GetFontSize
int GetFontSize() const override
Get the nominal font size of the font.
Definition truetypefontcache.h:48
TrueTypeFontCache::ClearFontCache
void ClearFontCache() override
Reset cached glyphs.
Definition truetypefontcache.cpp:40
TrueTypeFontCache::IsBuiltInFont
bool IsBuiltInFont() override
Is this a built-in sprite font?
Definition truetypefontcache.h:53
TrueTypeFontCache::~TrueTypeFontCache
virtual ~TrueTypeFontCache()
Free everything that was allocated for this font cache.
Definition truetypefontcache.cpp:31
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:18
FontSize
FontSize
Available font sizes.
Definition gfx_type.h:248
Sprite
Data structure describing a sprite.
Definition spritecache_type.h:16
TrueTypeFontCache::GlyphEntry
Container for information about a glyph.
Definition truetypefontcache.h:31
TrueTypeFontCache::GlyphEntry::data
std::unique_ptr< std::byte[]> data
The loaded sprite.
Definition truetypefontcache.h:32
TrueTypeFontCache::GlyphEntry::width
uint8_t width
The width of the glyph.
Definition truetypefontcache.h:33
MAX_FONT_SIZE
static const int MAX_FONT_SIZE
Maximum font size.
Definition truetypefontcache.h:16