From d724088a1c346ee2e01e5cdec9c9ca4a92e8c12b Mon Sep 17 00:00:00 2001 From: rubidium Date: Sun, 23 Jun 2013 15:32:09 +0000 Subject: [PATCH] (svn r25442) -Codechange: move height and ascender information into the FontCache instances --- src/company_gui.cpp | 2 +- src/fontcache.cpp | 52 +++++++++++++++++++++------------------------ src/fontcache.h | 9 +++++++- src/gfx_func.h | 12 +---------- 4 files changed, 34 insertions(+), 41 deletions(-) diff --git a/src/company_gui.cpp b/src/company_gui.cpp index b763aa3630..1c75fc30ad 100644 --- a/src/company_gui.cpp +++ b/src/company_gui.cpp @@ -526,7 +526,7 @@ public: uint Height(uint width) const { - return max(FONT_HEIGHT_NORMAL, (byte)14); + return max(FONT_HEIGHT_NORMAL, 14); } bool Selectable() const diff --git a/src/fontcache.cpp b/src/fontcache.cpp index ae5416dd86..74603fce29 100644 --- a/src/fontcache.cpp +++ b/src/fontcache.cpp @@ -23,31 +23,14 @@ static const int ASCII_LETTERSTART = 32; ///< First printable ASCII letter. 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. */ 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. * @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); FontCache::caches[this->fs] = this; @@ -60,6 +43,18 @@ FontCache::~FontCache() 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. */ class SpriteFontCache : public FontCache { private: @@ -142,6 +137,8 @@ bool SpriteFontCache::GetDrawGlyphShadow() class FreeTypeFontCache : public FontCache { private: 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: FreeTypeFontCache(FontSize fs, FT_Face face, int pixels); ~FreeTypeFontCache(); @@ -155,7 +152,6 @@ public: }; FT_Library _library = NULL; -static int _ascender[FS_END]; FreeTypeSettings _freetype; @@ -197,11 +193,9 @@ FreeTypeFontCache::FreeTypeFontCache(FontSize fs, FT_Face face, int pixels) : Fo FT_Set_Pixel_Sizes(this->face, 0, n); } - int asc = this->face->size->metrics.ascender >> 6; - int dec = this->face->size->metrics.descender >> 6; - - _ascender[this->fs] = asc; - _font_height[this->fs] = asc - dec; + this->ascender = this->face->size->metrics.ascender >> 6; + this->descender = this->face->size->metrics.descender >> 6; + this->height = this->ascender - this->descender; } /** @@ -271,9 +265,13 @@ FreeTypeFontCache::~FreeTypeFontCache() */ void InitFreeType(bool monospace) { - ResetFontSizes(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)) { DEBUG(freetype, 1, "No font faces specified, using sprite fonts instead"); return; @@ -315,8 +313,6 @@ void InitFreeType(bool monospace) */ void UninitFreeType() { - ResetFontSizes(true); - ResetFontSizes(false); ClearFontCache(); for (FontSize fs = FS_BEGIN; fs < FS_END; fs++) { @@ -506,7 +502,7 @@ const Sprite *FreeTypeFontCache::GetGlyph(WChar key) sprite.width = width; sprite.height = height; 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 */ if (this->fs == FS_NORMAL && !aa) { diff --git a/src/fontcache.h b/src/fontcache.h index 2df587fc37..b26eaa2988 100644 --- a/src/fontcache.h +++ b/src/fontcache.h @@ -21,10 +21,17 @@ private: protected: FontCache *parent; ///< The parent of this font cache. const FontSize fs; ///< The size of the font. + int height; ///< The height of the font; public: FontCache(FontSize fs); 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 * @param key The key to get the sprite for. @@ -153,7 +160,7 @@ void UninitFreeType(); #else /* 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() {} #endif /* WITH_FREETYPE */ diff --git a/src/gfx_func.h b/src/gfx_func.h index 42c2b3a5e9..3dcba44975 100644 --- a/src/gfx_func.h +++ b/src/gfx_func.h @@ -153,17 +153,7 @@ byte GetCharacterWidth(FontSize size, uint32 key); byte GetDigitWidth(FontSize size = FS_NORMAL); void GetBroadestDigit(uint *front, uint *next, FontSize size = FS_NORMAL); -/** - * 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]; -} +int GetCharacterHeight(FontSize size); /** Height of characters in the small (#FS_SMALL) font. */ #define FONT_HEIGHT_SMALL (GetCharacterHeight(FS_SMALL))