Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
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** )
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__