fontcache.h

Go to the documentation of this file.
00001 /* $Id: fontcache.h 25987 2013-11-13 21:53:40Z rubidium $ */
00002 
00003 /*
00004  * This file is part of OpenTTD.
00005  * OpenTTD is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, version 2.
00006  * OpenTTD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
00007  * See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with OpenTTD. If not, see <http://www.gnu.org/licenses/>.
00008  */
00009 
00012 #ifndef FONTCACHE_H
00013 #define FONTCACHE_H
00014 
00015 #include "string_type.h"
00016 #include "spritecache.h"
00017 
00019 typedef uint32 GlyphID;
00020 static const GlyphID SPRITE_GLYPH = 1U << 30;
00021 
00023 class FontCache {
00024 private:
00025   static FontCache *caches[FS_END]; 
00026 protected:
00027   FontCache *parent;                
00028   const FontSize fs;                
00029   int height;                       
00030   int ascender;                     
00031   int descender;                    
00032   int units_per_em;                 
00033 public:
00034   FontCache(FontSize fs);
00035   virtual ~FontCache();
00036 
00041   inline FontSize GetSize() const { return this->fs; }
00042 
00047   inline int GetHeight() const { return this->height; }
00048 
00053   inline int GetAscender() const { return this->ascender; }
00054 
00059   inline int GetDescender() const{ return this->descender; }
00060 
00065   inline int GetUnitsPerEM() const { return this->units_per_em; }
00066 
00072   virtual SpriteID GetUnicodeGlyph(WChar key) = 0;
00073 
00079   virtual void SetUnicodeGlyph(WChar key, SpriteID sprite) = 0;
00080 
00082   virtual void InitializeUnicodeGlyphMap() = 0;
00083 
00085   virtual void ClearFontCache() = 0;
00086 
00092   virtual const Sprite *GetGlyph(GlyphID key) = 0;
00093 
00099   virtual uint GetGlyphWidth(GlyphID key) = 0;
00100 
00105   virtual bool GetDrawGlyphShadow() = 0;
00106 
00112   virtual GlyphID MapCharToGlyph(WChar key) = 0;
00113 
00120   virtual const void *GetFontTable(uint32 tag, size_t &length) = 0;
00121 
00127   static inline FontCache *Get(FontSize fs)
00128   {
00129     assert(fs < FS_END);
00130     return FontCache::caches[fs];
00131   }
00132 
00136   inline bool HasParent()
00137   {
00138     return this->parent != NULL;
00139   }
00140 };
00141 
00143 static inline SpriteID GetUnicodeGlyph(FontSize size, WChar key)
00144 {
00145   return FontCache::Get(size)->GetUnicodeGlyph(key);
00146 }
00147 
00149 static inline void SetUnicodeGlyph(FontSize size, WChar key, SpriteID sprite)
00150 {
00151   FontCache::Get(size)->SetUnicodeGlyph(key, sprite);
00152 }
00153 
00155 static inline void InitializeUnicodeGlyphMap()
00156 {
00157   for (FontSize fs = FS_BEGIN; fs < FS_END; fs++) {
00158     FontCache::Get(fs)->InitializeUnicodeGlyphMap();
00159   }
00160 }
00161 
00162 static inline void ClearFontCache()
00163 {
00164   for (FontSize fs = FS_BEGIN; fs < FS_END; fs++) {
00165     FontCache::Get(fs)->ClearFontCache();
00166   }
00167 }
00168 
00170 static inline const Sprite *GetGlyph(FontSize size, WChar key)
00171 {
00172   FontCache *fc = FontCache::Get(size);
00173   return fc->GetGlyph(fc->MapCharToGlyph(key));
00174 }
00175 
00177 static inline uint GetGlyphWidth(FontSize size, WChar key)
00178 {
00179   FontCache *fc = FontCache::Get(size);
00180   return fc->GetGlyphWidth(fc->MapCharToGlyph(key));
00181 }
00182 
00183 static inline bool GetDrawGlyphShadow(FontSize size)
00184 {
00185   return FontCache::Get(size)->GetDrawGlyphShadow();
00186 }
00187 
00188 #ifdef WITH_FREETYPE
00189 
00191 struct FreeTypeSubSetting {
00192   char font[MAX_PATH]; 
00193   uint size;           
00194   bool aa;             
00195 };
00196 
00198 struct FreeTypeSettings {
00199   FreeTypeSubSetting small;  
00200   FreeTypeSubSetting medium; 
00201   FreeTypeSubSetting large;  
00202   FreeTypeSubSetting mono;   
00203 };
00204 
00205 extern FreeTypeSettings _freetype;
00206 
00207 #endif /* WITH_FREETYPE */
00208 
00209 void InitFreeType(bool monospace);
00210 void UninitFreeType();
00211 
00212 #endif /* FONTCACHE_H */