(svn r25442) -Codechange: move height and ascender information into the FontCache instances

This commit is contained in:
rubidium 2013-06-23 15:32:09 +00:00
parent eb4388f4f3
commit d724088a1c
4 changed files with 34 additions and 41 deletions

View File

@ -526,7 +526,7 @@ public:
uint Height(uint width) const uint Height(uint width) const
{ {
return max(FONT_HEIGHT_NORMAL, (byte)14); return max(FONT_HEIGHT_NORMAL, 14);
} }
bool Selectable() const bool Selectable() const

View File

@ -23,31 +23,14 @@
static const int ASCII_LETTERSTART = 32; ///< First printable ASCII letter. static const int ASCII_LETTERSTART = 32; ///< First printable ASCII letter.
static const int MAX_FONT_SIZE = 72; ///< Maximum font size. static const int MAX_FONT_SIZE = 72; ///< Maximum font size.
/** Semi-constant for the height of the different sizes of fonts. */
int _font_height[FS_END];
/** Default heights for the different sizes of fonts. */ /** Default heights for the different sizes of fonts. */
static const int _default_font_height[FS_END] = {10, 6, 18, 10}; static const int _default_font_height[FS_END] = {10, 6, 18, 10};
/**
* Reset the font sizes to the defaults of the sprite based fonts.
* @param monospace Whether to reset the monospace or regular fonts.
*/
void ResetFontSizes(bool monospace)
{
if (monospace) {
_font_height[FS_MONO] = _default_font_height[FS_MONO];
} else {
_font_height[FS_SMALL] = _default_font_height[FS_SMALL];
_font_height[FS_NORMAL] = _default_font_height[FS_NORMAL];
_font_height[FS_LARGE] = _default_font_height[FS_LARGE];
}
}
/** /**
* Create a new font cache. * Create a new font cache.
* @param fs The size of the font. * @param fs The size of the font.
*/ */
FontCache::FontCache(FontSize fs) : parent(FontCache::Get(fs)), fs(fs) FontCache::FontCache(FontSize fs) : parent(FontCache::Get(fs)), fs(fs), height(_default_font_height[fs])
{ {
assert(parent == NULL || this->fs == parent->fs); assert(parent == NULL || this->fs == parent->fs);
FontCache::caches[this->fs] = this; FontCache::caches[this->fs] = this;
@ -60,6 +43,18 @@ FontCache::~FontCache()
FontCache::caches[this->fs] = this->parent; FontCache::caches[this->fs] = this->parent;
} }
/**
* 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:
@ -142,6 +137,8 @@ bool SpriteFontCache::GetDrawGlyphShadow()
class FreeTypeFontCache : public FontCache { class FreeTypeFontCache : public FontCache {
private: private:
FT_Face face; ///< The font face associated with this font. FT_Face face; ///< The font face associated with this font.
int ascender; ///< The ascender value of this font.
int descender; ///< The descender value of this font.
public: public:
FreeTypeFontCache(FontSize fs, FT_Face face, int pixels); FreeTypeFontCache(FontSize fs, FT_Face face, int pixels);
~FreeTypeFontCache(); ~FreeTypeFontCache();
@ -155,7 +152,6 @@ public:
}; };
FT_Library _library = NULL; FT_Library _library = NULL;
static int _ascender[FS_END];
FreeTypeSettings _freetype; FreeTypeSettings _freetype;
@ -197,11 +193,9 @@ FreeTypeFontCache::FreeTypeFontCache(FontSize fs, FT_Face face, int pixels) : Fo
FT_Set_Pixel_Sizes(this->face, 0, n); FT_Set_Pixel_Sizes(this->face, 0, n);
} }
int asc = this->face->size->metrics.ascender >> 6; this->ascender = this->face->size->metrics.ascender >> 6;
int dec = this->face->size->metrics.descender >> 6; this->descender = this->face->size->metrics.descender >> 6;
this->height = this->ascender - this->descender;
_ascender[this->fs] = asc;
_font_height[this->fs] = asc - dec;
} }
/** /**
@ -271,9 +265,13 @@ FreeTypeFontCache::~FreeTypeFontCache()
*/ */
void InitFreeType(bool monospace) void InitFreeType(bool monospace)
{ {
ResetFontSizes(monospace);
ResetGlyphCache(monospace); ResetGlyphCache(monospace);
for (FontSize fs = FS_BEGIN; fs < FS_END; fs++) {
FontCache *fc = FontCache::Get(fs);
if (fc->HasParent()) delete fc;
}
if (StrEmpty(_freetype.small.font) && StrEmpty(_freetype.medium.font) && StrEmpty(_freetype.large.font) && StrEmpty(_freetype.mono.font)) { if (StrEmpty(_freetype.small.font) && StrEmpty(_freetype.medium.font) && StrEmpty(_freetype.large.font) && StrEmpty(_freetype.mono.font)) {
DEBUG(freetype, 1, "No font faces specified, using sprite fonts instead"); DEBUG(freetype, 1, "No font faces specified, using sprite fonts instead");
return; return;
@ -315,8 +313,6 @@ void InitFreeType(bool monospace)
*/ */
void UninitFreeType() void UninitFreeType()
{ {
ResetFontSizes(true);
ResetFontSizes(false);
ClearFontCache(); ClearFontCache();
for (FontSize fs = FS_BEGIN; fs < FS_END; fs++) { for (FontSize fs = FS_BEGIN; fs < FS_END; fs++) {
@ -506,7 +502,7 @@ const Sprite *FreeTypeFontCache::GetGlyph(WChar key)
sprite.width = width; sprite.width = width;
sprite.height = height; sprite.height = height;
sprite.x_offs = slot->bitmap_left; sprite.x_offs = slot->bitmap_left;
sprite.y_offs = _ascender[this->fs] - slot->bitmap_top; sprite.y_offs = this->ascender - slot->bitmap_top;
/* Draw shadow for medium size */ /* Draw shadow for medium size */
if (this->fs == FS_NORMAL && !aa) { if (this->fs == FS_NORMAL && !aa) {

View File

@ -21,10 +21,17 @@ private:
protected: protected:
FontCache *parent; ///< The parent of this font cache. FontCache *parent; ///< The parent of this font cache.
const FontSize fs; ///< The size of the font. const FontSize fs; ///< The size of the font.
int height; ///< The height of the font;
public: public:
FontCache(FontSize fs); FontCache(FontSize fs);
virtual ~FontCache(); virtual ~FontCache();
/**
* Get the height of the font.
* @return The height of the font.
*/
inline int GetHeight() { return this->height; }
/** /**
* Get the SpriteID mapped to the given key * Get the SpriteID mapped to the given key
* @param key The key to get the sprite for. * @param key The key to get the sprite for.
@ -153,7 +160,7 @@ void UninitFreeType();
#else #else
/* Stub for initializiation */ /* Stub for initializiation */
static inline void InitFreeType(bool monospace) { extern void ResetFontSizes(bool monospace); ResetFontSizes(monospace); } static inline void InitFreeType(bool monospace) {}
static inline void UninitFreeType() {} static inline void UninitFreeType() {}
#endif /* WITH_FREETYPE */ #endif /* WITH_FREETYPE */

View File

@ -153,17 +153,7 @@ 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);
* 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)
*/
static inline byte GetCharacterHeight(FontSize size)
{
assert(size < FS_END);
extern int _font_height[FS_END];
return _font_height[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))