OpenTTD Source  20240919-master-gdf0233f4c2
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 <http://www.gnu.org/licenses/>.
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 
24 TrueTypeFontCache::TrueTypeFontCache(FontSize fs, int pixels) : FontCache(fs), req_size(pixels)
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 
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 
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 }
TrueTypeFontCache::~TrueTypeFontCache
virtual ~TrueTypeFontCache()
Free everything that was allocated for this font cache.
Definition: truetypefontcache.cpp:31
TrueTypeFontCache::GlyphEntry
Container for information about a glyph.
Definition: truetypefontcache.h:31
Layouter::ResetFontCache
static void ResetFontCache(FontSize size)
Reset cached font information.
Definition: gfx_layout.cpp:353
TrueTypeFontCache::GetGlyphWidth
uint GetGlyphWidth(GlyphID key) override
Get the width of the glyph with the given key.
Definition: truetypefontcache.cpp:65
TrueTypeFontCache::GetDrawGlyphShadow
bool GetDrawGlyphShadow() override
Do we need to draw a glyph shadow?
Definition: truetypefontcache.cpp:60
TrueTypeFontCache::GlyphEntry::data
std::unique_ptr< uint8_t[]> data
The loaded sprite.
Definition: truetypefontcache.h:32
FS_NORMAL
@ FS_NORMAL
Index of the normal font in the font tables.
Definition: gfx_type.h:209
truetypefontcache.h
GlyphID
uint32_t GlyphID
Glyphs are characters from a font.
Definition: fontcache.h:17
FontCache::fs
const FontSize fs
The size of the font.
Definition: fontcache.h:25
FontCache::GetGlyphWidth
virtual uint GetGlyphWidth(GlyphID key)=0
Get the width of the glyph with the given key.
TrueTypeFontCache::GlyphEntry::width
uint8_t width
The width of the glyph.
Definition: truetypefontcache.h:33
FontCache
Font cache for basic fonts.
Definition: fontcache.h:21
FontCache::GetGlyph
virtual const Sprite * GetGlyph(GlyphID key)=0
Get the glyph (sprite) of the given key.
FontCache::parent
FontCache * parent
The parent of this font cache.
Definition: fontcache.h:24
TrueTypeFontCache::ClearFontCache
void ClearFontCache() override
Reset cached glyphs.
Definition: truetypefontcache.cpp:40
TrueTypeFontCache::TrueTypeFontCache
TrueTypeFontCache(FontSize fs, int pixels)
Create a new TrueTypeFontCache.
Definition: truetypefontcache.cpp:24
FontSize
FontSize
Available font sizes.
Definition: gfx_type.h:208
TrueTypeFontCache::GetGlyph
const Sprite * GetGlyph(GlyphID key) override
Get the glyph (sprite) of the given key.
Definition: truetypefontcache.cpp:78
Sprite
Data structure describing a sprite.
Definition: spritecache.h:17