From 990c38ca813e8b066e47417d734dd005b1e83910 Mon Sep 17 00:00:00 2001 From: Nimetu Date: Tue, 5 Jan 2021 13:48:28 +0200 Subject: [PATCH] Change letter cache to map lookup, ryzom/ryzomcore#626 --- nel/include/nel/3d/texture_font.h | 59 +++++++++++++++++-------------- nel/src/3d/texture_font.cpp | 18 ++++------ 2 files changed, 39 insertions(+), 38 deletions(-) diff --git a/nel/include/nel/3d/texture_font.h b/nel/include/nel/3d/texture_font.h index b250fecd8..9ca423315 100644 --- a/nel/include/nel/3d/texture_font.h +++ b/nel/include/nel/3d/texture_font.h @@ -38,6 +38,36 @@ class CTextureFont : public ITexture public: + struct SLetterKey + { + u32char Char; + sint Size; + bool Embolden; + bool Oblique; + CFontGenerator *FontGenerator; + + SLetterKey() : Char(0), Size(0), Embolden(false), Oblique(false), FontGenerator(NULL) + { + } + + // Does not use FontGenerator in return value + inline uint64 getVal() const + { + return Char // 32 bits + | (uint64(Size & 0xFFFF) << 32) // 16 bits + | (uint64(Embolden) << (32+16)) // 1 bit + | (uint64(Oblique) << (32+16+1)); // 1 bit + } + + bool operator<(const SLetterKey &rhs) const + { + uint64 a = getVal(); + uint64 b = rhs.getVal(); + return (a < b) || ((a == b) && (FontGenerator < rhs.FontGenerator)); + } + }; + + // Holds info for glyphs rendered on atlas struct SGlyphInfo { @@ -70,14 +100,8 @@ public: }; // Holds info for glyphs displayed on screen - struct SLetterInfo + struct SLetterInfo : SLetterKey { - u32char Char; - sint Size; - bool Embolden; - bool Oblique; - CFontGenerator *FontGenerator; - uint32 GlyphIndex; uint32 CharWidth; // Displayed glyph height uint32 CharHeight; // Displayed glyph height @@ -88,29 +112,12 @@ public: SGlyphInfo* glyph; SLetterInfo() - : Char(0), Size(0), Embolden(false), Oblique(false), FontGenerator(NULL), - GlyphIndex(0), CharWidth(0), CharHeight(0), Top(0), Left(0), AdvX(0), + : GlyphIndex(0), CharWidth(0), CharHeight(0), Top(0), Left(0), AdvX(0), glyph(NULL) { } }; - struct SLetterKey - { - u32char Char; - sint Size; - bool Embolden; - bool Oblique; - CFontGenerator *FontGenerator; - - // Does not use FontGenerator in return value - uint32 getVal(); - - SLetterKey():Char(0), FontGenerator(NULL), Size(0), Embolden(false), Oblique(false) - { - } - }; - public: /** @@ -151,7 +158,7 @@ private: // Keep track of available space in main texture std::vector _AtlasNodes; - std::vector _Letters; + std::map _Letters; // lookup letter from letter cache or create new SLetterInfo* findLetter(SLetterKey& k, bool insert); diff --git a/nel/src/3d/texture_font.cpp b/nel/src/3d/texture_font.cpp index b40083516..dd966eb8d 100644 --- a/nel/src/3d/texture_font.cpp +++ b/nel/src/3d/texture_font.cpp @@ -112,9 +112,9 @@ void CTextureFont::clearAtlas() _Data[0].fill(0); // clear glyph cache - for(uint i = 0; i< _Letters.size(); ++i) + for(std::map::iterator it = _Letters.begin(); it != _Letters.end(); ++it) { - _Letters[i].glyph = NULL; + it->second.glyph = NULL; } _GlyphCache.clear(); @@ -473,21 +473,15 @@ CTextureFont::SGlyphInfo* CTextureFont::findLetterGlyph(SLetterInfo *letter, boo // --------------------------------------------------------------------------- CTextureFont::SLetterInfo* CTextureFont::findLetter(SLetterKey &k, bool insert) { - // TODO: use std::map - for(uint i = 0; i < _Letters.size(); ++i) + std::map::iterator it = _Letters.find(k); + if (it != _Letters.end()) { - if (_Letters[i].Char == k.Char && _Letters[i].Size == k.Size && - _Letters[i].Embolden == k.Embolden && _Letters[i].Oblique == k.Oblique && - _Letters[i].FontGenerator == k.FontGenerator) - { - return &_Letters[i]; - } + return &(it->second); } if (insert) { - _Letters.push_back(SLetterInfo()); - SLetterInfo* letter = &_Letters.back(); + SLetterInfo* letter = &_Letters[k]; // get metrics for requested size letter->Char = k.Char;