(svn r25439) -Codechange: move the glyph to sprite ID mapping into the SpriteFontCache

This commit is contained in:
rubidium 2013-06-23 15:25:35 +00:00
parent e1a2a8275b
commit a285743d48
1 changed files with 43 additions and 16 deletions

View File

@ -62,8 +62,13 @@ FontCache::~FontCache()
/** Font cache for fonts that are based on a freetype font. */ /** Font cache for fonts that are based on a freetype font. */
class SpriteFontCache : public FontCache { class SpriteFontCache : public FontCache {
private:
SpriteID **glyph_to_spriteid_map; ///< Mapping of glyphs to sprite IDs.
void ClearGlyphToSpriteMap();
public: public:
SpriteFontCache(FontSize fs) : FontCache(fs) {} SpriteFontCache(FontSize fs);
~SpriteFontCache();
virtual SpriteID GetUnicodeGlyph(uint32 key); virtual SpriteID GetUnicodeGlyph(uint32 key);
virtual void SetUnicodeGlyph(uint32 key, SpriteID sprite); virtual void SetUnicodeGlyph(uint32 key, SpriteID sprite);
virtual void InitializeUnicodeGlyphMap(); virtual void InitializeUnicodeGlyphMap();
@ -73,6 +78,37 @@ public:
virtual bool GetDrawGlyphShadow(); virtual bool GetDrawGlyphShadow();
}; };
/**
* 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(NULL)
{
this->InitializeUnicodeGlyphMap();
}
/**
* Free everything we allocated.
*/
SpriteFontCache::~SpriteFontCache()
{
this->ClearGlyphToSpriteMap();
}
/**
* Clear the glyph to sprite mapping.
*/
void SpriteFontCache::ClearGlyphToSpriteMap()
{
if (this->glyph_to_spriteid_map == NULL) 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 = NULL;
}
void SpriteFontCache::ClearFontCache() {} void SpriteFontCache::ClearFontCache() {}
const Sprite *SpriteFontCache::GetGlyph(uint32 key) const Sprite *SpriteFontCache::GetGlyph(uint32 key)
@ -566,9 +602,6 @@ uint FreeTypeFontCache::GetGlyphWidth(WChar key)
#include "table/unicode.h" #include "table/unicode.h"
static SpriteID **_unicode_glyph_map[FS_END];
/** Get the SpriteID of the first glyph for the given font size */ /** Get the SpriteID of the first glyph for the given font size */
static SpriteID GetFontBase(FontSize size) static SpriteID GetFontBase(FontSize size)
{ {
@ -584,29 +617,23 @@ static SpriteID GetFontBase(FontSize size)
SpriteID SpriteFontCache::GetUnicodeGlyph(uint32 key) SpriteID SpriteFontCache::GetUnicodeGlyph(uint32 key)
{ {
if (_unicode_glyph_map[this->fs][GB(key, 8, 8)] == NULL) return 0; if (this->glyph_to_spriteid_map[GB(key, 8, 8)] == NULL) return 0;
return _unicode_glyph_map[this->fs][GB(key, 8, 8)][GB(key, 0, 8)]; return this->glyph_to_spriteid_map[GB(key, 8, 8)][GB(key, 0, 8)];
} }
void SpriteFontCache::SetUnicodeGlyph(uint32 key, SpriteID sprite) void SpriteFontCache::SetUnicodeGlyph(uint32 key, SpriteID sprite)
{ {
if (_unicode_glyph_map[this->fs] == NULL) _unicode_glyph_map[this->fs] = CallocT<SpriteID*>(256); if (this->glyph_to_spriteid_map == NULL) this->glyph_to_spriteid_map = CallocT<SpriteID*>(256);
if (_unicode_glyph_map[this->fs][GB(key, 8, 8)] == NULL) _unicode_glyph_map[this->fs][GB(key, 8, 8)] = CallocT<SpriteID>(256); if (this->glyph_to_spriteid_map[GB(key, 8, 8)] == NULL) this->glyph_to_spriteid_map[GB(key, 8, 8)] = CallocT<SpriteID>(256);
_unicode_glyph_map[this->fs][GB(key, 8, 8)][GB(key, 0, 8)] = sprite; this->glyph_to_spriteid_map[GB(key, 8, 8)][GB(key, 0, 8)] = sprite;
} }
void SpriteFontCache::InitializeUnicodeGlyphMap() void SpriteFontCache::InitializeUnicodeGlyphMap()
{ {
/* Clear out existing glyph map if it exists */ /* Clear out existing glyph map if it exists */
if (_unicode_glyph_map[this->fs] != NULL) { this->ClearGlyphToSpriteMap();
for (uint i = 0; i < 256; i++) {
free(_unicode_glyph_map[this->fs][i]);
}
free(_unicode_glyph_map[this->fs]);
_unicode_glyph_map[this->fs] = NULL;
}
SpriteID base = GetFontBase(this->fs); SpriteID base = GetFontBase(this->fs);