diff options
author | rubidium <rubidium@openttd.org> | 2013-06-23 15:24:36 +0000 |
---|---|---|
committer | rubidium <rubidium@openttd.org> | 2013-06-23 15:24:36 +0000 |
commit | e1a2a8275b4e4109912ae68b72b757cd92714daa (patch) | |
tree | cabc0b05c03ca32bfca957d5a2c17abd1645d06e /src/fontcache.h | |
parent | 94a5fe6b927da6e898968d240a3486dbe60d6be8 (diff) | |
download | openttd-e1a2a8275b4e4109912ae68b72b757cd92714daa.tar.xz |
(svn r25438) -Codechange: introduce classes for the FontCache
Diffstat (limited to 'src/fontcache.h')
-rw-r--r-- | src/fontcache.h | 138 |
1 files changed, 108 insertions, 30 deletions
diff --git a/src/fontcache.h b/src/fontcache.h index 7c1e8a843..2df587fc3 100644 --- a/src/fontcache.h +++ b/src/fontcache.h @@ -14,14 +14,119 @@ #include "spritecache.h" +/** Font cache for basic fonts. */ +class FontCache { +private: + static FontCache *caches[FS_END]; ///< All the font caches. +protected: + FontCache *parent; ///< The parent of this font cache. + const FontSize fs; ///< The size of the font. +public: + FontCache(FontSize fs); + virtual ~FontCache(); + + /** + * Get the SpriteID mapped to the given key + * @param key The key to get the sprite for. + * @return The sprite. + */ + virtual SpriteID GetUnicodeGlyph(uint32 key) = 0; + + /** + * Map a SpriteID to the key + * @param key The key to map to. + * @param sprite The sprite that is being mapped. + */ + virtual void SetUnicodeGlyph(uint32 key, SpriteID sprite) = 0; + + /** Initialize the glyph map */ + virtual void InitializeUnicodeGlyphMap() = 0; + + /** Clear the font cache. */ + virtual void ClearFontCache() = 0; + + /** + * Get the glyph (sprite) of the given key. + * @param key The key to look up. + * @return The sprite. + */ + virtual const Sprite *GetGlyph(uint32 key) = 0; + + /** + * Get the width of the glyph with the given key. + * @param key The key to look up. + * @return The width. + */ + virtual uint GetGlyphWidth(uint32 key) = 0; + + /** + * Do we need to draw a glyph shadow? + * @return True if it has to be done, otherwise false. + */ + virtual bool GetDrawGlyphShadow() = 0; + + /** + * Get the font cache of a given font size. + * @param fs The font size to look up. + * @return The font cache. + */ + static inline FontCache *Get(FontSize fs) + { + assert(fs < FS_END); + return FontCache::caches[fs]; + } + + /** + * Check whether the font cache has a parent. + */ + inline bool HasParent() + { + return this->parent != NULL; + } +}; + /** Get the SpriteID mapped to the given font size and key */ -SpriteID GetUnicodeGlyph(FontSize size, uint32 key); +static inline SpriteID GetUnicodeGlyph(FontSize size, uint32 key) +{ + return FontCache::Get(size)->GetUnicodeGlyph(key); +} /** Map a SpriteID to the font size and key */ -void SetUnicodeGlyph(FontSize size, uint32 key, SpriteID sprite); +static inline void SetUnicodeGlyph(FontSize size, uint32 key, SpriteID sprite) +{ + FontCache::Get(size)->SetUnicodeGlyph(key, sprite); +} /** Initialize the glyph map */ -void InitializeUnicodeGlyphMap(); +static inline void InitializeUnicodeGlyphMap() +{ + for (FontSize fs = FS_BEGIN; fs < FS_END; fs++) { + FontCache::Get(fs)->InitializeUnicodeGlyphMap(); + } +} + +static inline void ClearFontCache() { + for (FontSize fs = FS_BEGIN; fs < FS_END; fs++) { + FontCache::Get(fs)->ClearFontCache(); + } +} + +/** Get the Sprite for a glyph */ +static inline const Sprite *GetGlyph(FontSize size, uint32 key) +{ + return FontCache::Get(size)->GetGlyph(key); +} + +/** Get the width of a glyph */ +static inline uint GetGlyphWidth(FontSize size, uint32 key) +{ + return FontCache::Get(size)->GetGlyphWidth(key); +} + +static inline bool GetDrawGlyphShadow(FontSize size) +{ + return FontCache::Get(size)->GetDrawGlyphShadow(); +} #ifdef WITH_FREETYPE @@ -44,39 +149,12 @@ extern FreeTypeSettings _freetype; void InitFreeType(bool monospace); void UninitFreeType(); -void ClearFontCache(); -const Sprite *GetGlyph(FontSize size, uint32 key); -uint GetGlyphWidth(FontSize size, uint32 key); -bool GetDrawGlyphShadow(); #else /* Stub for initializiation */ static inline void InitFreeType(bool monospace) { extern void ResetFontSizes(bool monospace); ResetFontSizes(monospace); } static inline void UninitFreeType() {} -static inline void ClearFontCache() {} - -/** Get the Sprite for a glyph */ -static inline const Sprite *GetGlyph(FontSize size, uint32 key) -{ - SpriteID sprite = GetUnicodeGlyph(size, key); - if (sprite == 0) sprite = GetUnicodeGlyph(size, '?'); - return GetSprite(sprite, ST_FONT); -} - - -/** Get the width of a glyph */ -static inline uint GetGlyphWidth(FontSize size, uint32 key) -{ - SpriteID sprite = GetUnicodeGlyph(size, key); - if (sprite == 0) sprite = GetUnicodeGlyph(size, '?'); - return SpriteExists(sprite) ? GetSprite(sprite, ST_FONT)->width + (size != FS_NORMAL) : 0; -} - -static inline bool GetDrawGlyphShadow() -{ - return false; -} #endif /* WITH_FREETYPE */ |