OpenTTD Source  20241121-master-g67a0fccfad
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 }
Font cache for basic fonts.
Definition: fontcache.h:21
virtual const Sprite * GetGlyph(GlyphID key)=0
Get the glyph (sprite) of the given key.
FontCache * parent
The parent of this font cache.
Definition: fontcache.h:24
virtual uint GetGlyphWidth(GlyphID key)=0
Get the width of the glyph with the given key.
const FontSize fs
The size of the font.
Definition: fontcache.h:25
static void ResetFontCache(FontSize size)
Reset cached font information.
Definition: gfx_layout.cpp:353
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?
void ClearFontCache() override
Reset cached glyphs.
virtual ~TrueTypeFontCache()
Free everything that was allocated for this font cache.
TrueTypeFontCache(FontSize fs, int pixels)
Create a new TrueTypeFontCache.
uint32_t GlyphID
Glyphs are characters from a font.
Definition: fontcache.h:17
FontSize
Available font sizes.
Definition: gfx_type.h:208
@ FS_NORMAL
Index of the normal font in the font tables.
Definition: gfx_type.h:209
Data structure describing a sprite.
Definition: spritecache.h:17
Container for information about a glyph.
std::unique_ptr< uint8_t[]> data
The loaded sprite.
uint8_t width
The width of the glyph.
Common base definition for font file based font caches.