fontservices/fontstore/src/OPENFONT.CPP
branchRCL_3
changeset 69 09b5fcf47b30
parent 60 dd58c6eee052
--- a/fontservices/fontstore/src/OPENFONT.CPP	Wed Sep 15 14:10:32 2010 +0300
+++ b/fontservices/fontstore/src/OPENFONT.CPP	Wed Oct 13 16:35:09 2010 +0300
@@ -26,7 +26,6 @@
 #include "linkedfontsprivate.h"
 #include <graphics/openfontrasterizer.h>
 #include <graphics/gdi/glyphsample.h>
-#include <e32math.h>
 
 const TInt KSessionCacheEntries = 512;
 const TInt KDefaultSlantFactor = 20480;
@@ -849,13 +848,13 @@
     iFileOffset = 0;
 	}
 
-COpenFontGlyphCache* COpenFont::GetGlyphCache() const
+COpenFontGlyphCache* COpenFont::GetGlyphCache()
 	{
     if (iGlyphCacheOffset == 0)
         {
         return NULL;
         }
-    return reinterpret_cast<COpenFontGlyphCache*>(PtrAdd(const_cast<COpenFont*>(this), iGlyphCacheOffset));
+    return reinterpret_cast<COpenFontGlyphCache*>(PtrAdd(this, iGlyphCacheOffset));
 	}
 
 const COpenFontGlyph* COpenFont::Glyph(TInt aSessionHandle, TInt aCode) const
@@ -894,7 +893,7 @@
 @param aCode The code for the glpyh to look for in the cache
 @return A pointer to the requested glyph if it was found in the glyph cache, NULL if it was not found.
 */
-const COpenFontGlyph* COpenFont::FontCacheGlyph(TInt aCode) const
+const COpenFontGlyph* COpenFont::FontCacheGlyph(TInt aCode)
 	{
 	if (COpenFontGlyphCache* glyphCache = GetGlyphCache())
 		{
@@ -1057,52 +1056,58 @@
     iEntryArray.Close(aHeap);
     }
 
-const COpenFontGlyph* COpenFontSessionCache::Glyph(const COpenFont* aFont, TInt aCode, TInt& aIndex) const
+const COpenFontGlyph* COpenFontSessionCache::Glyph(const COpenFont* aFont, TInt aCode, TInt& aIndex)
 	{
     aIndex = -1;
+    TInt oldest = KMaxTInt;
+    TInt oldest_index = 0;
     TInt numEntries = iEntryArray.Count();
     TInt index = GLYPH_CODE(aCode) % numEntries;   // simple hash function to shorten searches
     for (TInt i = 0; i < numEntries; ++i, ++index)
         {
         if (index >= numEntries)
-            {
             index = 0;
-            }
-        const COpenFontSessionCacheEntry* entry = iEntryArray[index];
+        COpenFontSessionCacheEntry* entry = iEntryArray[index];
         if (entry == NULL)
             {
-            if (aIndex < 0)
+            if (aIndex == -1)
+                aIndex = index;
+            }
+        else
+            { 
+            if (entry->Font() == aFont && entry->iCode == aCode)
                 {
-                aIndex = index;
+                entry->iLastAccess = iLastAccess++;
+                return entry;
+                }
+            if (entry->iLastAccess < oldest)
+                {
+                oldest = entry->iLastAccess;
+                oldest_index = index;
                 }
             }
-        else if (entry->Font() == aFont && entry->iCode == aCode)
-            {
-            return entry;
-            }
         }
+    if (aIndex == -1)
+        aIndex = oldest_index;
     return NULL;
 	}
 
 
 void COpenFontSessionCache::Insert(RHeap* aHeap, COpenFontSessionCacheEntry* aEntry, TInt aIndex)
 	{
-    if (aIndex >= iEntryArray.Count())
+    if (aIndex < 0 || aIndex >= iEntryArray.Count())
         {
         Panic(EFntSessionCacheIndexOutOfRange);
         }
-    if (aIndex < 0)
-        {
-        aIndex = Math::Rand(iRandomSeed) % iEntryArray.Count();
-        }
     COpenFontSessionCacheEntry::Delete(aHeap, iEntryArray[aIndex]);
     iEntryArray.SetAt(aIndex, aEntry);
+    aEntry->iLastAccess = iLastAccess++;
 	}
 
 COpenFontSessionCache::COpenFontSessionCache(TInt aSessionHandle):
     iSessionHandle(aSessionHandle),
-    iRandomSeed(0)
-    {
+    iLastAccess(0)
+    {    
     }
 
 TInt COpenFontSessionCacheList::AddCache(COpenFontSessionCache* aCache)