Main Page | Modules | Namespace List | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Namespace Members | Class Members | File Members | Related Pages
fontcache.h
Go to the documentation of this file.00001 /* 00002 Copyright (C) 2003 by Jorrit Tyberghein 00003 (C) 2003 by Frank Richter 00004 00005 This library is free software; you can redistribute it and/or 00006 modify it under the terms of the GNU Library General Public 00007 License as published by the Free Software Foundation; either 00008 version 2 of the License, or (at your option) any later version. 00009 00010 This library is distributed in the hope that it will be useful, 00011 but WITHOUT ANY WARRANTY; without even the implied warranty of 00012 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00013 Library General Public License for more details. 00014 00015 You should have received a copy of the GNU Library General Public 00016 License along with this library; if not, write to the Free 00017 Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. 00018 */ 00019 00020 #ifndef __CS_CSPLUGINCOMMON_CANVAS_FONTCACHE_H__ 00021 #define __CS_CSPLUGINCOMMON_CANVAS_FONTCACHE_H__ 00022 00027 #include "csextern.h" 00028 00029 #include "csutil/blockallocator.h" 00030 #include "csutil/csunicode.h" 00031 #include "csutil/set.h" 00032 #include "ivideo/fontserv.h" 00033 00034 00039 #define GLYPH_INDEX_UPPER_SHIFT 9 00040 #define GLYPH_INDEX_LOWER_COUNT 512 00041 #define GLYPH_INDEX_LOWER_MASK 0x1ff 00042 00043 #define RELEVANT_WRITE_FLAGS CS_WRITE_NOANTIALIAS 00044 00052 class CS_CRYSTALSPACE_EXPORT csFontCache 00053 { 00054 public: 00055 struct KnownFont; 00059 struct GlyphCacheData 00060 { 00062 KnownFont* font; 00064 utf32_char glyph; 00066 csGlyphMetrics glyphMetrics; 00068 bool hasGlyph; 00070 uint flags; 00071 }; 00072 00073 protected: 00077 struct LRUEntry 00078 { 00080 LRUEntry* next; 00082 LRUEntry* prev; 00083 00085 GlyphCacheData* cacheData; 00086 }; 00088 LRUEntry* head; 00090 LRUEntry* tail; 00091 00093 csBlockAllocator<LRUEntry> LRUAlloc; 00094 00101 struct PlaneGlyphs 00102 { 00104 LRUEntry* entries[GLYPH_INDEX_LOWER_COUNT]; 00105 int usedGlyphs; 00106 00107 PlaneGlyphs () 00108 { 00109 memset (entries, 0, sizeof (entries)); 00110 usedGlyphs = 0; 00111 } 00112 }; 00113 00114 class PlaneGlyphElementHandler : public csArrayElementHandler<PlaneGlyphs*> 00115 { 00116 public: 00117 static void Construct (PlaneGlyphs** address, PlaneGlyphs* const& src) 00118 { 00119 *address = src; 00120 } 00121 00122 static void Destroy (PlaneGlyphs** /*address*/) 00123 { 00124 } 00125 00126 static void InitRegion (PlaneGlyphs** address, size_t count) 00127 { 00128 memset (address, 0, count * sizeof (PlaneGlyphs*)); 00129 } 00130 }; 00131 00138 typedef csArray<PlaneGlyphs*, PlaneGlyphElementHandler> PlaneGlyphsArray; 00139 public: 00143 struct KnownFont 00144 { 00145 iFont* font; 00147 float fontSize; 00148 PlaneGlyphsArray planeGlyphs; 00149 }; 00151 int ClipX1, ClipY1, ClipX2, ClipY2; 00152 protected: 00153 00155 csArray<KnownFont*> knownFonts; 00156 csSet<csPtrKey<KnownFont> > purgeableFonts; 00157 00159 LRUEntry* FindLRUEntry (KnownFont* font, utf32_char glyph); 00161 LRUEntry* FindLRUEntry (GlyphCacheData* cacheData); 00162 00163 static int KnownFontArrayCompareItems (KnownFont* const& item1, 00164 KnownFont* const& item2); 00165 static int KnownFontArrayCompareToKey (KnownFont* const& item1, 00166 iFont* const& item2); 00167 static csArrayCmp<KnownFont*,iFont*> KnownFontArrayKeyFunctor(iFont* f) 00168 { return csArrayCmp<KnownFont*,iFont*>(f, KnownFontArrayCompareToKey); } 00169 00171 virtual GlyphCacheData* InternalCacheGlyph (KnownFont* font, 00172 utf32_char glyph, uint flags); 00174 virtual void InternalUncacheGlyph (GlyphCacheData* cacheData); 00175 00177 GlyphCacheData* CacheGlyphUnsafe (KnownFont* font, 00178 utf32_char glyph, uint flags); 00180 void SetupCacheData (GlyphCacheData* cacheData, 00181 KnownFont* font, utf32_char glyph, uint flags); 00182 00184 void AddCacheData (KnownFont* font, utf32_char glyph, GlyphCacheData* cacheData); 00186 void RemoveCacheData (GlyphCacheData* cacheData); 00188 void RemoveLRUEntry (LRUEntry* entry); 00190 GlyphCacheData* InternalGetCacheData (KnownFont* font, utf32_char glyph); 00191 00195 struct FontDeleteNotify : public iFontDeleteNotify 00196 { 00197 csFontCache* cache; 00198 SCF_DECLARE_IBASE; 00199 00200 FontDeleteNotify (csFontCache* cache); 00201 virtual ~FontDeleteNotify (); 00202 00203 virtual void BeforeDelete (iFont* font); 00204 }; 00205 FontDeleteNotify* deleteCallback; 00206 00207 void CleanupCache (); 00208 public: 00209 csFontCache (); 00210 virtual ~csFontCache (); 00211 00213 GlyphCacheData* CacheGlyph (KnownFont* font, utf32_char glyph, 00214 uint flags); 00216 void UncacheGlyph (GlyphCacheData* cacheData); 00217 00219 KnownFont* GetCachedFont (iFont* font); 00221 KnownFont* CacheFont (iFont* font); 00223 void UncacheFont (iFont* font); 00225 GlyphCacheData* GetCacheData (KnownFont* font, utf32_char glyph, 00226 uint flags); 00228 GlyphCacheData* GetLeastUsed (); 00229 00231 void PurgeEmptyPlanes (); 00232 00233 void SetClipRect (int x1, int y1, int x2, int y2) 00234 { 00235 ClipX1 = x1; ClipY1 = y1; ClipX2 = x2; ClipY2 = y2; 00236 } 00237 00241 virtual void WriteString (iFont *font, int x, int y, int fg, int bg, 00242 const utf8_char* text, uint flags); 00243 }; 00244 00247 #endif // __CS_CSPLUGINCOMMON_CANVAS_FONTCACHE_H__
Generated for Crystal Space by doxygen 1.4.4