Cache font heights in a static fixed array.

This is to avoid significant per-call overhead to get font heights,
as this is performed very frequently.
This commit is contained in:
Jonathan G Rennison
2016-01-01 21:47:34 +00:00
parent b51dd34fe4
commit 5aad0b51a3
3 changed files with 18 additions and 13 deletions

View File

@@ -43,6 +43,7 @@ FontCache::FontCache(FontSize fs) : parent(FontCache::Get(fs)), fs(fs), height(_
{ {
assert(parent == NULL || this->fs == parent->fs); assert(parent == NULL || this->fs == parent->fs);
FontCache::caches[this->fs] = this; FontCache::caches[this->fs] = this;
font_height_cache[this->fs] = this->height;
Layouter::ResetFontCache(this->fs); Layouter::ResetFontCache(this->fs);
} }
@@ -54,18 +55,6 @@ FontCache::~FontCache()
Layouter::ResetFontCache(this->fs); Layouter::ResetFontCache(this->fs);
} }
/**
* Get height of a character for a given font size.
* @param size Font size to get height of
* @return Height of characters in the given font (pixels)
*/
int GetCharacterHeight(FontSize size)
{
return FontCache::Get(size)->GetHeight();
}
/** 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: private:
@@ -151,6 +140,7 @@ void SpriteFontCache::InitializeUnicodeGlyphMap()
this->SetUnicodeGlyph(_default_unicode_map[i].code, sprite); this->SetUnicodeGlyph(_default_unicode_map[i].code, sprite);
} }
} }
font_height_cache[this->fs] = this->GetHeight();
} }
/** /**
@@ -197,6 +187,7 @@ bool SpriteFontCache::GetDrawGlyphShadow()
} }
/* static */ FontCache *FontCache::caches[FS_END] = { new SpriteFontCache(FS_NORMAL), new SpriteFontCache(FS_SMALL), new SpriteFontCache(FS_LARGE), new SpriteFontCache(FS_MONO) }; /* static */ FontCache *FontCache::caches[FS_END] = { new SpriteFontCache(FS_NORMAL), new SpriteFontCache(FS_SMALL), new SpriteFontCache(FS_LARGE), new SpriteFontCache(FS_MONO) };
int font_height_cache[FS_END];
#ifdef WITH_FREETYPE #ifdef WITH_FREETYPE
#include <ft2build.h> #include <ft2build.h>
@@ -312,6 +303,8 @@ FreeTypeFontCache::FreeTypeFontCache(FontSize fs, FT_Face face, int pixels) : Fo
/* Both FT_Set_Pixel_Sizes and FT_Select_Size failed. */ /* Both FT_Set_Pixel_Sizes and FT_Select_Size failed. */
DEBUG(freetype, 0, "Font size selection failed. Using FontCache defaults."); DEBUG(freetype, 0, "Font size selection failed. Using FontCache defaults.");
} }
font_height_cache[this->fs] = this->GetHeight();
} }
/** /**

View File

@@ -19,6 +19,8 @@
typedef uint32 GlyphID; typedef uint32 GlyphID;
static const GlyphID SPRITE_GLYPH = 1U << 30; static const GlyphID SPRITE_GLYPH = 1U << 30;
extern int font_height_cache[FS_END]; ///< Cache of font heights
/** Font cache for basic fonts. */ /** Font cache for basic fonts. */
class FontCache { class FontCache {
private: private:

View File

@@ -158,7 +158,17 @@ byte GetCharacterWidth(FontSize size, uint32 key);
byte GetDigitWidth(FontSize size = FS_NORMAL); byte GetDigitWidth(FontSize size = FS_NORMAL);
void GetBroadestDigit(uint *front, uint *next, FontSize size = FS_NORMAL); void GetBroadestDigit(uint *front, uint *next, FontSize size = FS_NORMAL);
int GetCharacterHeight(FontSize size); extern int font_height_cache[FS_END];
/**
* Get height of a character for a given font size.
* @param size Font size to get height of
* @return Height of characters in the given font (pixels)
*/
inline int GetCharacterHeight(FontSize size)
{
return font_height_cache[size];
}
/** Height of characters in the small (#FS_SMALL) font. */ /** Height of characters in the small (#FS_SMALL) font. */
#define FONT_HEIGHT_SMALL (GetCharacterHeight(FS_SMALL)) #define FONT_HEIGHT_SMALL (GetCharacterHeight(FS_SMALL))