CrystalSpace

Public API Reference

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 protected:
00154 
00156   csArray<KnownFont*> knownFonts;
00157   csSet<csPtrKey<KnownFont> > purgeableFonts;
00158 
00160   LRUEntry* FindLRUEntry (KnownFont* font, utf32_char glyph);
00162   LRUEntry* FindLRUEntry (GlyphCacheData* cacheData);
00163 
00164   static int KnownFontArrayCompareItems (KnownFont* const& item1, 
00165     KnownFont* const& item2);
00166   static int KnownFontArrayCompareToKey (KnownFont* const& item1, 
00167     iFont* const& item2);
00168   static csArrayCmp<KnownFont*,iFont*> KnownFontArrayKeyFunctor(iFont* f)
00169     { return csArrayCmp<KnownFont*,iFont*>(f, KnownFontArrayCompareToKey); }
00170 
00172   virtual GlyphCacheData* InternalCacheGlyph (KnownFont* font,
00173     utf32_char glyph, uint flags);
00175   virtual void InternalUncacheGlyph (GlyphCacheData* cacheData);
00176 
00178   GlyphCacheData* CacheGlyphUnsafe (KnownFont* font, 
00179     utf32_char glyph, uint flags);
00181   void SetupCacheData (GlyphCacheData* cacheData,
00182     KnownFont* font, utf32_char glyph, uint flags);
00183 
00185   void AddCacheData (KnownFont* font, utf32_char glyph, GlyphCacheData* cacheData);
00187   void RemoveCacheData (GlyphCacheData* cacheData);
00189   void RemoveLRUEntry (LRUEntry* entry);
00191   GlyphCacheData* InternalGetCacheData (KnownFont* font, utf32_char glyph);
00192 
00196   struct FontDeleteNotify : public scfImplementation1<FontDeleteNotify, 
00197                                                       iFontDeleteNotify>
00198   {
00199     csFontCache* cache;
00200     
00201     FontDeleteNotify (csFontCache* cache);
00202     virtual ~FontDeleteNotify ();
00203 
00204     virtual void BeforeDelete (iFont* font);
00205   };
00206   FontDeleteNotify* deleteCallback;
00207 
00208   void CleanupCache ();
00209 public:
00210   csFontCache ();
00211   virtual ~csFontCache ();
00212   
00214   GlyphCacheData* CacheGlyph (KnownFont* font, utf32_char glyph,
00215     uint flags);
00217   void UncacheGlyph (GlyphCacheData* cacheData);
00218 
00220   KnownFont* GetCachedFont (iFont* font);
00222   KnownFont* CacheFont (iFont* font);
00224   void UncacheFont (iFont* font);
00226   GlyphCacheData* GetCacheData (KnownFont* font, utf32_char glyph, 
00227     uint flags);
00229   GlyphCacheData* GetLeastUsed ();
00230 
00232   void PurgeEmptyPlanes ();
00233 
00234   void SetClipRect (int x1, int y1, int x2, int y2)
00235   { 
00236     ClipX1 = x1; ClipY1 = y1; ClipX2 = x2; ClipY2 = y2; 
00237   }
00238 
00240 
00243   virtual void WriteString (iFont *font, int x, int y, int fg, int bg, 
00244     const void* text, bool isWide, uint flags);
00246 };
00247 
00250 #endif // __CS_CSPLUGINCOMMON_CANVAS_FONTCACHE_H__

Generated for Crystal Space 1.0.2 by doxygen 1.4.7