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/>.
8 /** @file truetypefontcache.cpp Common base implementation for font file based font caches. */
10 #include "../stdafx.h"
12 #include "../fontcache.h"
13 #include "../core/bitmath_func.hpp"
14 #include "../gfx_layout.h"
15 #include "truetypefontcache.h"
17 #include "../safeguards.h"
20 * Create a new TrueTypeFontCache.
21 * @param fs The font size that is going to be cached.
22 * @param pixels The number of pixels this font should be high.
24 TrueTypeFontCache::TrueTypeFontCache(FontSize fs
, int pixels
) : FontCache(fs
), req_size(pixels
)
29 * Free everything that was allocated for this font cache.
31 TrueTypeFontCache::~TrueTypeFontCache()
33 /* Virtual functions get called statically in destructors, so make it explicit to remove any confusion. */
34 this->TrueTypeFontCache::ClearFontCache();
38 * Reset cached glyphs.
40 void TrueTypeFontCache::ClearFontCache()
42 this->glyph_to_sprite_map
.clear();
43 Layouter::ResetFontCache(this->fs
);
47 TrueTypeFontCache::GlyphEntry
*TrueTypeFontCache::GetGlyphPtr(GlyphID key
)
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
;
54 TrueTypeFontCache::GlyphEntry
&TrueTypeFontCache::SetGlyphPtr(GlyphID key
, GlyphEntry
&&glyph
)
56 this->glyph_to_sprite_map
[key
] = std::move(glyph
);
57 return this->glyph_to_sprite_map
[key
];
60 bool TrueTypeFontCache::GetDrawGlyphShadow()
62 return this->fs
== FS_NORMAL
&& GetFontAAState();
65 uint
TrueTypeFontCache::GetGlyphWidth(GlyphID key
)
67 if ((key
& SPRITE_GLYPH
) != 0) return this->parent
->GetGlyphWidth(key
);
69 GlyphEntry
*glyph
= this->GetGlyphPtr(key
);
70 if (glyph
== nullptr || glyph
->data
== nullptr) {
72 glyph
= this->GetGlyphPtr(key
);
78 const Sprite
*TrueTypeFontCache::GetGlyph(GlyphID key
)
80 if ((key
& SPRITE_GLYPH
) != 0) return this->parent
->GetGlyph(key
);
82 /* Check for the glyph in our cache */
83 GlyphEntry
*glyph
= this->GetGlyphPtr(key
);
84 if (glyph
!= nullptr && glyph
->data
!= nullptr) return glyph
->GetSprite();
86 return this->InternalGetGlyph(key
, GetFontAAState());