From f220ed179dc259e35d39e027d9e456ed5e30a5cd Mon Sep 17 00:00:00 2001 From: Peter Nelson Date: Tue, 28 May 2024 19:34:03 +0100 Subject: [PATCH] Codechange: Use std::unordered_map for SpriteFontCache's glyph map. (#12724) Replaces a C-style malloc'd array and malloc'd contents, and no manual memory clean up is necessary. --- src/fontcache/spritefontcache.cpp | 42 +++++++++---------------------- src/fontcache/spritefontcache.h | 10 +++----- 2 files changed, 16 insertions(+), 36 deletions(-) diff --git a/src/fontcache/spritefontcache.cpp b/src/fontcache/spritefontcache.cpp index d9ad15d55e..9d945d394b 100644 --- a/src/fontcache/spritefontcache.cpp +++ b/src/fontcache/spritefontcache.cpp @@ -35,7 +35,7 @@ static int ScaleFontTrad(int value) * Create a new sprite font cache. * @param fs The font size to create the cache for. */ -SpriteFontCache::SpriteFontCache(FontSize fs) : FontCache(fs), glyph_to_spriteid_map(nullptr) +SpriteFontCache::SpriteFontCache(FontSize fs) : FontCache(fs) { this->InitializeUnicodeGlyphMap(); this->height = ScaleGUITrad(FontCache::GetDefaultFontHeight(this->fs)); @@ -43,30 +43,26 @@ SpriteFontCache::SpriteFontCache(FontSize fs) : FontCache(fs), glyph_to_spriteid } /** - * Free everything we allocated. + * Get SpriteID associated with a GlyphID. + * @param key Glyph to find. + * @return SpriteID of glyph, or 0 if not present. */ -SpriteFontCache::~SpriteFontCache() +SpriteID SpriteFontCache::GetUnicodeGlyph(GlyphID key) { - this->ClearGlyphToSpriteMap(); -} - -SpriteID SpriteFontCache::GetUnicodeGlyph(char32_t key) -{ - if (this->glyph_to_spriteid_map[GB(key, 8, 8)] == nullptr) return 0; - return this->glyph_to_spriteid_map[GB(key, 8, 8)][GB(key, 0, 8)]; + const auto found = this->glyph_to_spriteid_map.find(key & ~SPRITE_GLYPH); + if (found == std::end(this->glyph_to_spriteid_map)) return 0; + return found->second; } void SpriteFontCache::SetUnicodeGlyph(char32_t key, SpriteID sprite) { - if (this->glyph_to_spriteid_map == nullptr) this->glyph_to_spriteid_map = CallocT(256); - if (this->glyph_to_spriteid_map[GB(key, 8, 8)] == nullptr) this->glyph_to_spriteid_map[GB(key, 8, 8)] = CallocT(256); - this->glyph_to_spriteid_map[GB(key, 8, 8)][GB(key, 0, 8)] = sprite; + this->glyph_to_spriteid_map[key] = sprite; } void SpriteFontCache::InitializeUnicodeGlyphMap() { /* Clear out existing glyph map if it exists */ - this->ClearGlyphToSpriteMap(); + this->glyph_to_spriteid_map.clear(); SpriteID base; switch (this->fs) { @@ -98,20 +94,6 @@ void SpriteFontCache::InitializeUnicodeGlyphMap() } } -/** - * Clear the glyph to sprite mapping. - */ -void SpriteFontCache::ClearGlyphToSpriteMap() -{ - if (this->glyph_to_spriteid_map == nullptr) return; - - for (uint i = 0; i < 256; i++) { - free(this->glyph_to_spriteid_map[i]); - } - free(this->glyph_to_spriteid_map); - this->glyph_to_spriteid_map = nullptr; -} - void SpriteFontCache::ClearFontCache() { Layouter::ResetFontCache(this->fs); @@ -121,14 +103,14 @@ void SpriteFontCache::ClearFontCache() const Sprite *SpriteFontCache::GetGlyph(GlyphID key) { - SpriteID sprite = this->GetUnicodeGlyph(key); + SpriteID sprite = this->GetUnicodeGlyph(static_cast(key & ~SPRITE_GLYPH)); if (sprite == 0) sprite = this->GetUnicodeGlyph('?'); return GetSprite(sprite, SpriteType::Font); } uint SpriteFontCache::GetGlyphWidth(GlyphID key) { - SpriteID sprite = this->GetUnicodeGlyph(key); + SpriteID sprite = this->GetUnicodeGlyph(static_cast(key & ~SPRITE_GLYPH)); if (sprite == 0) sprite = this->GetUnicodeGlyph('?'); return SpriteExists(sprite) ? GetSprite(sprite, SpriteType::Font)->width + ScaleFontTrad(this->fs != FS_NORMAL ? 1 : 0) : 0; } diff --git a/src/fontcache/spritefontcache.h b/src/fontcache/spritefontcache.h index e176aeb9b1..4e43b8752c 100644 --- a/src/fontcache/spritefontcache.h +++ b/src/fontcache/spritefontcache.h @@ -15,14 +15,8 @@ /** Font cache for fonts that are based on a freetype font. */ class SpriteFontCache : public FontCache { -private: - SpriteID **glyph_to_spriteid_map; ///< Mapping of glyphs to sprite IDs. - SpriteID GetUnicodeGlyph(char32_t key); - - void ClearGlyphToSpriteMap(); public: SpriteFontCache(FontSize fs); - ~SpriteFontCache(); void SetUnicodeGlyph(char32_t key, SpriteID sprite) override; void InitializeUnicodeGlyphMap() override; void ClearFontCache() override; @@ -32,6 +26,10 @@ public: GlyphID MapCharToGlyph(char32_t key, [[maybe_unused]] bool allow_fallback = true) override { assert(IsPrintable(key)); return SPRITE_GLYPH | key; } std::string GetFontName() override { return "sprite"; } bool IsBuiltInFont() override { return true; } + +private: + std::unordered_map glyph_to_spriteid_map{}; ///< Mapping of glyphs to sprite IDs. + SpriteID GetUnicodeGlyph(GlyphID key); }; #endif /* SPRITEFONTCACHE_H */