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

OpenTTD Source 15.0-RC2

truetypefontcache.cpp
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#include "../stdafx.h"
11#include "../debug.h"
12#include "../fontcache.h"
13#include "../core/bitmath_func.hpp"
14#include "../gfx_layout.h"
15#include "truetypefontcache.h"
16
17#include "../safeguards.h"
18
24TrueTypeFontCache::TrueTypeFontCache(FontSize fs, int pixels) : FontCache(fs), req_size(pixels)
25{
26}
27
31TrueTypeFontCache::~TrueTypeFontCache()
32{
33 /* Virtual functions get called statically in destructors, so make it explicit to remove any confusion. */
34 this->TrueTypeFontCache::ClearFontCache();
35}
36
40 void TrueTypeFontCache::ClearFontCache()
41{
42 this->glyph_to_sprite_map.clear();
43 Layouter::ResetFontCache(this->fs);
44}
45
46
47 TrueTypeFontCache::GlyphEntry *TrueTypeFontCache::GetGlyphPtr(GlyphID key)
48{
49 auto found = this->glyph_to_sprite_map.find(key);
50 if (found == std::end(this->glyph_to_sprite_map)) return nullptr;
51 return &found->second;
52}
53
54 TrueTypeFontCache::GlyphEntry &TrueTypeFontCache::SetGlyphPtr(GlyphID key, GlyphEntry &&glyph)
55{
56 this->glyph_to_sprite_map[key] = std::move(glyph);
57 return this->glyph_to_sprite_map[key];
58}
59
60 bool TrueTypeFontCache::GetDrawGlyphShadow()
61{
62 return this->fs == FS_NORMAL && GetFontAAState();
63}
64
65uint TrueTypeFontCache::GetGlyphWidth(GlyphID key)
66{
67 if ((key & SPRITE_GLYPH) != 0) return this->parent->GetGlyphWidth(key);
68
69 GlyphEntry *glyph = this->GetGlyphPtr(key);
70 if (glyph == nullptr || glyph->data == nullptr) {
71 this->GetGlyph(key);
72 glyph = this->GetGlyphPtr(key);
73 }
74
75 return glyph->width;
76}
77
78 const Sprite *TrueTypeFontCache::GetGlyph(GlyphID key)
79{
80 if ((key & SPRITE_GLYPH) != 0) return this->parent->GetGlyph(key);
81
82 /* Check for the glyph in our cache */
83 GlyphEntry *glyph = this->GetGlyphPtr(key);
84 if (glyph != nullptr && glyph->data != nullptr) return glyph->GetSprite();
85
86 return this->InternalGetGlyph(key, GetFontAAState());
87}
FontCache
Font cache for basic fonts.
Definition fontcache.h:22
FontCache::parent
std::unique_ptr< FontCache > parent
The parent of this font cache.
Definition fontcache.h:25
FontCache::fs
const FontSize fs
The size of the font.
Definition fontcache.h:26
Layouter::ResetFontCache
static void ResetFontCache(FontSize size)
Reset cached font information.
Definition gfx_layout.cpp:367
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::ClearFontCache
void ClearFontCache() override
Reset cached glyphs.
Definition truetypefontcache.cpp:40
TrueTypeFontCache::~TrueTypeFontCache
virtual ~TrueTypeFontCache()
Free everything that was allocated for this font cache.
Definition truetypefontcache.cpp:31
TrueTypeFontCache::TrueTypeFontCache
TrueTypeFontCache(FontSize fs, int pixels)
Create a new TrueTypeFontCache.
Definition truetypefontcache.cpp:24
GlyphID
uint32_t GlyphID
Glyphs are characters from a font.
Definition fontcache.h:18
FontSize
FontSize
Available font sizes.
Definition gfx_type.h:248
FS_NORMAL
@ FS_NORMAL
Index of the normal font in the font tables.
Definition gfx_type.h:249
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
truetypefontcache.h
Common base definition for font file based font caches.