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:
@@ -43,6 +43,7 @@ FontCache::FontCache(FontSize fs) : parent(FontCache::Get(fs)), fs(fs), height(_
|
||||
{
|
||||
assert(parent == NULL || this->fs == parent->fs);
|
||||
FontCache::caches[this->fs] = this;
|
||||
font_height_cache[this->fs] = this->height;
|
||||
Layouter::ResetFontCache(this->fs);
|
||||
}
|
||||
|
||||
@@ -54,18 +55,6 @@ FontCache::~FontCache()
|
||||
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. */
|
||||
class SpriteFontCache : public FontCache {
|
||||
private:
|
||||
@@ -151,6 +140,7 @@ void SpriteFontCache::InitializeUnicodeGlyphMap()
|
||||
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) };
|
||||
int font_height_cache[FS_END];
|
||||
|
||||
#ifdef WITH_FREETYPE
|
||||
#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. */
|
||||
DEBUG(freetype, 0, "Font size selection failed. Using FontCache defaults.");
|
||||
}
|
||||
|
||||
font_height_cache[this->fs] = this->GetHeight();
|
||||
}
|
||||
|
||||
/**
|
||||
|
@@ -19,6 +19,8 @@
|
||||
typedef uint32 GlyphID;
|
||||
static const GlyphID SPRITE_GLYPH = 1U << 30;
|
||||
|
||||
extern int font_height_cache[FS_END]; ///< Cache of font heights
|
||||
|
||||
/** Font cache for basic fonts. */
|
||||
class FontCache {
|
||||
private:
|
||||
|
@@ -158,7 +158,17 @@ byte GetCharacterWidth(FontSize size, uint32 key);
|
||||
byte GetDigitWidth(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. */
|
||||
#define FONT_HEIGHT_SMALL (GetCharacterHeight(FS_SMALL))
|
||||
|
Reference in New Issue
Block a user