OpenTTD Source 20260208-master-g43af8e94d0
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
9
10#include "../stdafx.h"
11#include "../debug.h"
12#include "../fontcache.h"
14#include "../gfx_layout.h"
15#include "truetypefontcache.h"
16
17#include "../safeguards.h"
18
25{
26}
27
32{
33 /* Virtual functions get called statically in destructors, so make it explicit to remove any confusion. */
35}
36
41{
42 this->glyph_to_sprite_map.clear();
44}
45
46
47TrueTypeFontCache::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
54TrueTypeFontCache::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
61{
62 return this->fs == FS_NORMAL && GetFontAAState();
63}
64
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
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}
Functions related to bit mathematics.
std::unique_ptr< FontCache > parent
The parent of this font cache.
Definition fontcache.h:25
const FontSize fs
The size of the font.
Definition fontcache.h:26
static void ResetFontCache(FontSize size)
Reset cached font information.
~TrueTypeFontCache() override
Free everything that was allocated for this font cache.
uint GetGlyphWidth(GlyphID key) override
Get the width of the glyph with the given key.
const Sprite * GetGlyph(GlyphID key) override
Get the glyph (sprite) of the given key.
bool GetDrawGlyphShadow() override
Do we need to draw a glyph shadow?
int req_size
Requested font size.
void ClearFontCache() override
Reset cached glyphs.
TrueTypeFontCache(FontSize fs, int pixels)
Create a new TrueTypeFontCache.
Functions related to debugging.
Functions to read fonts from files and cache them.
Functions related to laying out the texts.
FontSize
Available font sizes.
Definition gfx_type.h:248
@ FS_NORMAL
Index of the normal font in the font tables.
Definition gfx_type.h:249
A number of safeguards to prevent using unsafe methods.
Definition of base types and functions in a cross-platform compatible way.
Data structure describing a sprite.
Container for information about a glyph.
std::unique_ptr< std::byte[]> data
The loaded sprite.
uint8_t width
The width of the glyph.
Common base definition for font file based font caches.