00001 /* $Id: fontcache.h 26170 2013-12-22 17:46:27Z 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 00126 virtual const char *GetFontName() = 0; 00127 00133 static inline FontCache *Get(FontSize fs) 00134 { 00135 assert(fs < FS_END); 00136 return FontCache::caches[fs]; 00137 } 00138 00142 inline bool HasParent() 00143 { 00144 return this->parent != NULL; 00145 } 00146 }; 00147 00149 static inline SpriteID GetUnicodeGlyph(FontSize size, WChar key) 00150 { 00151 return FontCache::Get(size)->GetUnicodeGlyph(key); 00152 } 00153 00155 static inline void SetUnicodeGlyph(FontSize size, WChar key, SpriteID sprite) 00156 { 00157 FontCache::Get(size)->SetUnicodeGlyph(key, sprite); 00158 } 00159 00161 static inline void InitializeUnicodeGlyphMap() 00162 { 00163 for (FontSize fs = FS_BEGIN; fs < FS_END; fs++) { 00164 FontCache::Get(fs)->InitializeUnicodeGlyphMap(); 00165 } 00166 } 00167 00168 static inline void ClearFontCache() 00169 { 00170 for (FontSize fs = FS_BEGIN; fs < FS_END; fs++) { 00171 FontCache::Get(fs)->ClearFontCache(); 00172 } 00173 } 00174 00176 static inline const Sprite *GetGlyph(FontSize size, WChar key) 00177 { 00178 FontCache *fc = FontCache::Get(size); 00179 return fc->GetGlyph(fc->MapCharToGlyph(key)); 00180 } 00181 00183 static inline uint GetGlyphWidth(FontSize size, WChar key) 00184 { 00185 FontCache *fc = FontCache::Get(size); 00186 return fc->GetGlyphWidth(fc->MapCharToGlyph(key)); 00187 } 00188 00189 static inline bool GetDrawGlyphShadow(FontSize size) 00190 { 00191 return FontCache::Get(size)->GetDrawGlyphShadow(); 00192 } 00193 00194 #ifdef WITH_FREETYPE 00195 00197 struct FreeTypeSubSetting { 00198 char font[MAX_PATH]; 00199 uint size; 00200 bool aa; 00201 }; 00202 00204 struct FreeTypeSettings { 00205 FreeTypeSubSetting small; 00206 FreeTypeSubSetting medium; 00207 FreeTypeSubSetting large; 00208 FreeTypeSubSetting mono; 00209 }; 00210 00211 extern FreeTypeSettings _freetype; 00212 00213 #endif /* WITH_FREETYPE */ 00214 00215 void InitFreeType(bool monospace); 00216 void UninitFreeType(); 00217 00218 #endif /* FONTCACHE_H */