csplugincommon/canvas/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/scf_implementation.h" 00032 #include "csutil/set.h" 00033 #include "ivideo/fontserv.h" 00034 00035 00040 #define GLYPH_INDEX_UPPER_SHIFT 9 00041 #define GLYPH_INDEX_LOWER_COUNT 512 00042 #define GLYPH_INDEX_LOWER_MASK 0x1ff 00043 00044 #define RELEVANT_WRITE_FLAGS CS_WRITE_NOANTIALIAS 00045 00053 class CS_CRYSTALSPACE_EXPORT csFontCache 00054 { 00055 public: 00056 struct KnownFont; 00060 struct GlyphCacheData 00061 { 00063 KnownFont* font; 00065 utf32_char glyph; 00067 csGlyphMetrics glyphMetrics; 00069 bool hasGlyph; 00071 uint flags; 00072 }; 00073 00074 protected: 00078 struct LRUEntry 00079 { 00081 LRUEntry* next; 00083 LRUEntry* prev; 00084 00086 GlyphCacheData* cacheData; 00087 }; 00089 LRUEntry* head; 00091 LRUEntry* tail; 00092 00094 csBlockAllocator<LRUEntry> LRUAlloc; 00095 00102 struct PlaneGlyphs 00103 { 00105 LRUEntry* entries[GLYPH_INDEX_LOWER_COUNT]; 00106 int usedGlyphs; 00107 00108 PlaneGlyphs () 00109 { 00110 memset (entries, 0, sizeof (entries)); 00111 usedGlyphs = 0; 00112 } 00113 }; 00114 00115 class PlaneGlyphElementHandler : public csArrayElementHandler<PlaneGlyphs*> 00116 { 00117 public: 00118 static void Construct (PlaneGlyphs** address, PlaneGlyphs* const& src) 00119 { 00120 *address = src; 00121 } 00122 00123 static void Destroy (PlaneGlyphs** /*address*/) 00124 { 00125 } 00126 00127 static void InitRegion (PlaneGlyphs** address, size_t count) 00128 { 00129 memset (address, 0, count * sizeof (PlaneGlyphs*)); 00130 } 00131 }; 00132 00139 typedef csArray<PlaneGlyphs*, PlaneGlyphElementHandler> PlaneGlyphsArray; 00140 public: 00144 struct KnownFont 00145 { 00146 iFont* font; 00148 float fontSize; 00149 PlaneGlyphsArray planeGlyphs; 00150 }; 00152 int ClipX1, ClipY1, ClipX2, ClipY2; 00153 int vpX, vpY; 00154 protected: 00155 00157 csArray<KnownFont*> knownFonts; 00158 csSet<csPtrKey<KnownFont> > purgeableFonts; 00159 00161 LRUEntry* FindLRUEntry (KnownFont* font, utf32_char glyph); 00163 LRUEntry* FindLRUEntry (GlyphCacheData* cacheData); 00164 00165 static int KnownFontArrayCompareItems (KnownFont* const& item1, 00166 KnownFont* const& item2); 00167 static int KnownFontArrayCompareToKey (KnownFont* const& item1, 00168 iFont* const& item2); 00169 static csArrayCmp<KnownFont*,iFont*> KnownFontArrayKeyFunctor(iFont* f) 00170 { return csArrayCmp<KnownFont*,iFont*>(f, KnownFontArrayCompareToKey); } 00171 00173 virtual GlyphCacheData* InternalCacheGlyph (KnownFont* font, 00174 utf32_char glyph, uint flags); 00176 virtual void InternalUncacheGlyph (GlyphCacheData* cacheData); 00177 00179 GlyphCacheData* CacheGlyphUnsafe (KnownFont* font, 00180 utf32_char glyph, uint flags); 00182 void SetupCacheData (GlyphCacheData* cacheData, 00183 KnownFont* font, utf32_char glyph, uint flags); 00184 00186 void AddCacheData (KnownFont* font, utf32_char glyph, GlyphCacheData* cacheData); 00188 void RemoveCacheData (GlyphCacheData* cacheData); 00190 void RemoveLRUEntry (LRUEntry* entry); 00192 GlyphCacheData* InternalGetCacheData (KnownFont* font, utf32_char glyph); 00193 00197 struct FontDeleteNotify : public scfImplementation1<FontDeleteNotify, 00198 iFontDeleteNotify> 00199 { 00200 csFontCache* cache; 00201 00202 FontDeleteNotify (csFontCache* cache); 00203 virtual ~FontDeleteNotify (); 00204 00205 virtual void BeforeDelete (iFont* font); 00206 }; 00207 FontDeleteNotify* deleteCallback; 00208 00209 void CleanupCache (); 00210 public: 00211 csFontCache (); 00212 virtual ~csFontCache (); 00213 00215 GlyphCacheData* CacheGlyph (KnownFont* font, utf32_char glyph, 00216 uint flags); 00218 void UncacheGlyph (GlyphCacheData* cacheData); 00219 00221 KnownFont* GetCachedFont (iFont* font); 00223 KnownFont* CacheFont (iFont* font); 00225 void UncacheFont (iFont* font); 00227 GlyphCacheData* GetCacheData (KnownFont* font, utf32_char glyph, 00228 uint flags); 00230 GlyphCacheData* GetLeastUsed (); 00231 00233 void PurgeEmptyPlanes (); 00234 00235 void SetClipRect (int x1, int y1, int x2, int y2) 00236 { 00237 ClipX1 = x1; ClipY1 = y1; ClipX2 = x2; ClipY2 = y2; 00238 } 00239 void SetViewportOfs (int vpX, int vpY) 00240 { 00241 this->vpX = vpX; this->vpY = vpY; 00242 } 00243 00245 00248 virtual void WriteString (iFont *font, int x, int y, int fg, int bg, 00249 const void* text, bool isWide, uint flags); 00251 }; 00252 00255 #endif // __CS_CSPLUGINCOMMON_CANVAS_FONTCACHE_H__
Generated for Crystal Space 2.0 by doxygen 1.6.1