diff --git a/src/fontcache.h b/src/fontcache.h index 20dbfff074..2ee0d240af 100644 --- a/src/fontcache.h +++ b/src/fontcache.h @@ -70,16 +70,6 @@ public: */ virtual int GetFontSize() const { return this->height; } - /** - * Map a SpriteID to the key - * @param key The key to map to. - * @param sprite The sprite that is being mapped. - */ - virtual void SetUnicodeGlyph(char32_t key, SpriteID sprite) = 0; - - /** Initialize the glyph map */ - virtual void InitializeUnicodeGlyphMap() = 0; - /** Clear the font cache. */ virtual void ClearFontCache() = 0; @@ -153,20 +143,6 @@ public: virtual bool IsBuiltInFont() = 0; }; -/** Map a SpriteID to the font size and key */ -inline void SetUnicodeGlyph(FontSize size, char32_t key, SpriteID sprite) -{ - FontCache::Get(size)->SetUnicodeGlyph(key, sprite); -} - -/** Initialize the glyph map */ -inline void InitializeUnicodeGlyphMap() -{ - for (FontSize fs = FS_BEGIN; fs < FS_END; fs++) { - FontCache::Get(fs)->InitializeUnicodeGlyphMap(); - } -} - inline void ClearFontCache(FontSizes fontsizes) { for (FontSize fs : fontsizes) { @@ -237,4 +213,8 @@ void UninitFontCache(); bool GetFontAAState(); void SetFont(FontSize fontsize, const std::string &font, uint size); +/* Implemented in spritefontcache.cpp */ +void InitializeUnicodeGlyphMap(); +void SetUnicodeGlyph(FontSize size, char32_t key, SpriteID sprite); + #endif /* FONTCACHE_H */ diff --git a/src/fontcache/spritefontcache.cpp b/src/fontcache/spritefontcache.cpp index cb49ad2817..e2d93189c2 100644 --- a/src/fontcache/spritefontcache.cpp +++ b/src/fontcache/spritefontcache.cpp @@ -10,6 +10,7 @@ #include "../stdafx.h" #include "../fontcache.h" #include "../gfx_layout.h" +#include "../string_func.h" #include "../zoom_func.h" #include "spritefontcache.h" @@ -31,41 +32,43 @@ static int ScaleFontTrad(int value) return UnScaleByZoom(value * ZOOM_BASE, _font_zoom); } -/** - * Create a new sprite font cache. - * @param fs The font size to create the cache for. - */ -SpriteFontCache::SpriteFontCache(FontSize fs) : FontCache(fs) -{ - this->InitializeUnicodeGlyphMap(); - this->height = ScaleGUITrad(FontCache::GetDefaultFontHeight(this->fs)); - this->ascender = (this->height - ScaleFontTrad(FontCache::GetDefaultFontHeight(this->fs))) / 2; -} +static std::array, FS_END> _char_maps{}; ///< Glyph map for each font size. /** * Get SpriteID associated with a character. * @param key Character to find. * @return SpriteID for character, or 0 if not present. */ -SpriteID SpriteFontCache::GetUnicodeGlyph(char32_t key) +static SpriteID GetUnicodeGlyph(FontSize fs, char32_t key) { - const auto found = this->char_map.find(key); - if (found == std::end(this->char_map)) return 0; - return found->second; + auto found = _char_maps[fs].find(key); + if (found != std::end(_char_maps[fs])) return found->second; + return 0; } -void SpriteFontCache::SetUnicodeGlyph(char32_t key, SpriteID sprite) +/** + * Set the SpriteID for a unicode character. + * @param fs Font size to set. + * @param key Unicode character to set. + * @param sprite SpriteID of character. + */ +void SetUnicodeGlyph(FontSize fs, char32_t key, SpriteID sprite) { - this->char_map[key] = sprite; + _char_maps[fs][key] = sprite; } -void SpriteFontCache::InitializeUnicodeGlyphMap() +/** + * Initialize the glyph map for a font size. + * This populates the glyph map with the baseset font sprites. + * @param fs Font size to initialize. + */ +void InitializeUnicodeGlyphMap(FontSize fs) { /* Clear out existing glyph map if it exists */ - this->char_map.clear(); + _char_maps[fs].clear(); SpriteID base; - switch (this->fs) { + switch (fs) { default: NOT_REACHED(); case FS_MONO: // Use normal as default for mono spaced font case FS_NORMAL: base = SPR_ASCII_SPACE; break; @@ -76,24 +79,45 @@ void SpriteFontCache::InitializeUnicodeGlyphMap() for (uint i = ASCII_LETTERSTART; i < 256; i++) { SpriteID sprite = base + i - ASCII_LETTERSTART; if (!SpriteExists(sprite)) continue; - this->SetUnicodeGlyph(i, sprite); - this->SetUnicodeGlyph(i + SCC_SPRITE_START, sprite); + SetUnicodeGlyph(fs, i, sprite); + SetUnicodeGlyph(fs, i + SCC_SPRITE_START, sprite); } + /* Modify map to move non-standard glyphs to a better unicode codepoint. */ for (const auto &unicode_map : _default_unicode_map) { uint8_t key = unicode_map.key; if (key == CLRA) { /* Clear the glyph. This happens if the glyph at this code point * is non-standard and should be accessed by an SCC_xxx enum * entry only. */ - this->SetUnicodeGlyph(unicode_map.code, 0); + SetUnicodeGlyph(fs, unicode_map.code, 0); } else { SpriteID sprite = base + key - ASCII_LETTERSTART; - this->SetUnicodeGlyph(unicode_map.code, sprite); + SetUnicodeGlyph(fs, unicode_map.code, sprite); } } } +/** + * Initialize the glyph map. + */ +void InitializeUnicodeGlyphMap() +{ + for (FontSize fs = FS_BEGIN; fs < FS_END; fs++) { + InitializeUnicodeGlyphMap(fs); + } +} + +/** + * Create a new sprite font cache. + * @param fs The font size to create the cache for. + */ +SpriteFontCache::SpriteFontCache(FontSize fs) : FontCache(fs) +{ + this->height = ScaleGUITrad(FontCache::GetDefaultFontHeight(this->fs)); + this->ascender = (this->height - ScaleFontTrad(FontCache::GetDefaultFontHeight(this->fs))) / 2; +} + void SpriteFontCache::ClearFontCache() { Layouter::ResetFontCache(this->fs); @@ -104,21 +128,21 @@ void SpriteFontCache::ClearFontCache() const Sprite *SpriteFontCache::GetGlyph(GlyphID key) { SpriteID sprite = static_cast(key & ~SPRITE_GLYPH); - if (sprite == 0) sprite = this->GetUnicodeGlyph('?'); + if (sprite == 0) sprite = GetUnicodeGlyph(this->fs, '?'); return GetSprite(sprite, SpriteType::Font); } uint SpriteFontCache::GetGlyphWidth(GlyphID key) { SpriteID sprite = static_cast(key & ~SPRITE_GLYPH); - if (sprite == 0) sprite = this->GetUnicodeGlyph('?'); + if (sprite == 0) sprite = GetUnicodeGlyph(this->fs, '?'); return SpriteExists(sprite) ? GetSprite(sprite, SpriteType::Font)->width + ScaleFontTrad(this->fs != FS_NORMAL ? 1 : 0) : 0; } GlyphID SpriteFontCache::MapCharToGlyph(char32_t key, [[maybe_unused]] bool allow_fallback) { assert(IsPrintable(key)); - SpriteID sprite = this->GetUnicodeGlyph(key); + SpriteID sprite = GetUnicodeGlyph(this->fs, key); if (sprite == 0) return 0; return SPRITE_GLYPH | sprite; } diff --git a/src/fontcache/spritefontcache.h b/src/fontcache/spritefontcache.h index 1f2a09a9c6..5403348cbe 100644 --- a/src/fontcache/spritefontcache.h +++ b/src/fontcache/spritefontcache.h @@ -10,15 +10,12 @@ #ifndef SPRITEFONTCACHE_H #define SPRITEFONTCACHE_H -#include "../string_func.h" #include "../fontcache.h" /** Font cache for fonts that are based on a freetype font. */ class SpriteFontCache : public FontCache { public: SpriteFontCache(FontSize fs); - void SetUnicodeGlyph(char32_t key, SpriteID sprite) override; - void InitializeUnicodeGlyphMap() override; void ClearFontCache() override; const Sprite *GetGlyph(GlyphID key) override; uint GetGlyphWidth(GlyphID key) override; @@ -26,10 +23,6 @@ public: GlyphID MapCharToGlyph(char32_t key, bool allow_fallback = true) override; std::string GetFontName() override { return "sprite"; } bool IsBuiltInFont() override { return true; } - -private: - std::unordered_map char_map{}; ///< Mapping of characters to sprite IDs. - SpriteID GetUnicodeGlyph(char32_t key); }; #endif /* SPRITEFONTCACHE_H */ diff --git a/src/fontcache/truetypefontcache.h b/src/fontcache/truetypefontcache.h index 2ce94c5d35..7714823305 100644 --- a/src/fontcache/truetypefontcache.h +++ b/src/fontcache/truetypefontcache.h @@ -46,8 +46,6 @@ public: TrueTypeFontCache(FontSize fs, int pixels); virtual ~TrueTypeFontCache(); int GetFontSize() const override { return this->used_size; } - void SetUnicodeGlyph(char32_t key, SpriteID sprite) override { this->parent->SetUnicodeGlyph(key, sprite); } - void InitializeUnicodeGlyphMap() override { this->parent->InitializeUnicodeGlyphMap(); } const Sprite *GetGlyph(GlyphID key) override; void ClearFontCache() override; uint GetGlyphWidth(GlyphID key) override; diff --git a/src/tests/mock_fontcache.h b/src/tests/mock_fontcache.h index 5ae4103f65..c159742a31 100644 --- a/src/tests/mock_fontcache.h +++ b/src/tests/mock_fontcache.h @@ -21,8 +21,6 @@ public: this->height = FontCache::GetDefaultFontHeight(this->fs); } - void SetUnicodeGlyph(char32_t, SpriteID) override {} - void InitializeUnicodeGlyphMap() override {} void ClearFontCache() override {} const Sprite *GetGlyph(GlyphID) override { return nullptr; } uint GetGlyphWidth(GlyphID) override { return this->height / 2; }