fontservices/fontstore/inc/openfontsprivate.h
changeset 32 8b9155204a54
child 49 4d76f1414957
child 51 a7c938434754
equal deleted inserted replaced
31:b9ad20498fb4 32:8b9155204a54
       
     1 /*
       
     2 * Copyright (c) 2006-2009 Nokia Corporation and/or its subsidiary(-ies).
       
     3 * All rights reserved.
       
     4 * This component and the accompanying materials are made available
       
     5 * under the terms of "Eclipse Public License v1.0"
       
     6 * which accompanies this distribution, and is available
       
     7 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     8 *
       
     9 * Initial Contributors:
       
    10 * Nokia Corporation - initial contribution.
       
    11 *
       
    12 * Contributors:
       
    13 *
       
    14 * Description: 
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 #ifndef __OPENFONTS_PRIVATE_H__
       
    20 #define __OPENFONTS_PRIVATE_H__
       
    21 
       
    22 #include <e32hashtab.h>
       
    23 class COpenFontShaperCacheEntry;
       
    24 
       
    25 /* MSB is set to indicate a glyph code rather than a unicode value
       
    26    This is used for extracting the code value */
       
    27 #define GLYPH_CODE(code) (code & 0x7fffffff)
       
    28 
       
    29 /** COpenFontFile owned container for extra data. May be extended without affecting Binary Compatibility.
       
    30 
       
    31  @internalComponent
       
    32  */
       
    33 
       
    34 class TOpenFontFileData
       
    35 	{
       
    36 public:
       
    37 	CFontStore* iFontStore;			// pointer to the CFontStore instance that loaded the COpenFontFile
       
    38 	};
       
    39 
       
    40 /**
       
    41  @internalComponent
       
    42  */
       
    43 class COpenFontGlyph
       
    44 	{
       
    45 public:
       
    46 	COpenFontGlyph(TInt aCode, TInt aGlyphIndex, const TOpenFontCharMetrics& aMetrics):
       
    47 		iCode(aCode), iGlyphIndex(aGlyphIndex), iMetrics(aMetrics), iBitmapOffset(0) { }
       
    48 	
       
    49 	static COpenFontGlyph* NewL(RHeap* aHeap, TInt aCode, TInt aGlyphIndex,
       
    50 								const TOpenFontCharMetrics& aMetrics, const TDesC8& aBitmap);
       
    51 
       
    52 	static void Delete(RHeap* aHeap, COpenFontGlyph* aGlyph);	
       
    53 	TUint8* Bitmap() const;	
       
    54 
       
    55 	TInt iCode;						// the Unicode value of the character
       
    56 	TInt iGlyphIndex;				// the glyph index
       
    57 	TOpenFontCharMetrics iMetrics;	// the metrics	
       
    58 
       
    59 protected:
       
    60 	TBool SetBitmap(RHeap* aHeap, const TDesC8& aBitmap);
       
    61 	~COpenFontGlyph();
       
    62 	
       
    63 private:
       
    64 	// an offset from this COpenFontGlyph object to a pointer to the run-length-encoded bitmap, 
       
    65 	// calculated as (bitmapPointer)-(this)
       
    66 	TInt iBitmapOffset;				
       
    67 	};
       
    68 /**
       
    69  Binary tree of glyphs. Each glyph can have left and right child nodes which are added
       
    70  depending on the value of their glyph code relative to the parent's glyph code.
       
    71 
       
    72  @internalComponent
       
    73  */
       
    74 class COpenFontGlyphTreeEntry: public COpenFontGlyph
       
    75 	{
       
    76 	friend class COpenFont;
       
    77 	
       
    78 public:
       
    79 	static COpenFontGlyphTreeEntry* New(RHeap* aHeap, TInt aCode, TInt aGlyphIndex,
       
    80 										const TOpenFontCharMetrics& aMetrics, const TDesC8& aBitmap);
       
    81 
       
    82 private:
       
    83 	/** The left COpenFontGlyphTreeEntry from this entry. Represented by an offset from the
       
    84      current heap base. Use COpenFont::ThisOffsetToPointer() to convert to a valid 
       
    85      COpenFontGlyphTreeEntry pointer. */
       
    86     TInt iLeftOffset;
       
    87     /** The right COpenFontGlyphTreeEntry from this entry. Represented by an offset from the
       
    88      current heap base. Use COpenFont::ThisOffsetToPointer() to convert to a valid 
       
    89      COpenFontGlyphTreeEntry pointer. */
       
    90     TInt iRightOffset;
       
    91     /** Pointer to next glyph that was added to the glyph cache.  This enables 
       
    92      non-recursive deletion of the cache. This is only ever accessed from server
       
    93      process, so can be a direct pointer, not an offset. */
       
    94     COpenFontGlyphTreeEntry* iNext;
       
    95 
       
    96 private:
       
    97 	COpenFontGlyphTreeEntry(TInt aCode, TInt aGlyphIndex, const TOpenFontCharMetrics& aMetrics)
       
    98 	 :	COpenFontGlyph(aCode, aGlyphIndex, aMetrics), iLeftOffset(0), iRightOffset(0),iNext(NULL){}
       
    99 
       
   100 	~COpenFontGlyphTreeEntry();
       
   101 	};
       
   102 
       
   103 /**
       
   104  * Template for offset implementation of pointer to pointer
       
   105  @internalComponent
       
   106  */
       
   107 template<class T>
       
   108 class ROffsetArray
       
   109     {
       
   110 public:
       
   111     ROffsetArray();
       
   112     TInt Create(RHeap* aHeap, TInt aCount);
       
   113     void Close(RHeap* aHeap);
       
   114     TInt Count() const;
       
   115     T* operator[](TInt aIndex) const;
       
   116     void SetAt(TInt aIndex, T* aEntry);
       
   117 private:
       
   118     TInt iOffset;
       
   119     TInt iCount;
       
   120     };
       
   121 
       
   122 
       
   123 /**
       
   124  The per-font glyph cache. For now, just the members that used to be directly in
       
   125  COpenFont. Now it is a private class it can be elaborated to do character-to-glyph-index
       
   126  mapping when that is needed.
       
   127 
       
   128  @internalComponent
       
   129  */
       
   130 class COpenFontGlyphCache
       
   131 	{
       
   132 public:
       
   133 	COpenFontGlyphCache(): iGlyphTreeOffset(0), iGlyphCacheMemory(0),iGlyphList(NULL),iShaperCacheSentinel(NULL), iShapingInfoCacheMemory(0), iNumberOfShaperCacheEntries(0) { }
       
   134 	TShapeHeader* SearchShaperCache(TInt aSessionHandle, TFontShapeFunctionParameters*& aParams);
       
   135 	TShapeHeader* Insert(TInt aSessionHandle, RHeap* aHeap, CShaper::TInput aInput, TShapeHeader* aShapeHeader, TInt& aAddedBytes);
       
   136 	TInt DeleteLeastRecentlyUsedEntry(RHeap* aHeap);
       
   137 	TBool ShaperCacheIsEmpty();
       
   138 	
       
   139 public:
       
   140 	TInt iGlyphTreeOffset;									// an offset to root of the glyph cache; a binary tree
       
   141 	TInt iGlyphCacheMemory;									// memory used by the glyph tree in bytes
       
   142 	COpenFontGlyphTreeEntry* iGlyphList;                   // the glyphs, organized as a list
       
   143 	COpenFontShaperCacheEntry* iShaperCacheSentinel;
       
   144 	TInt iShapingInfoCacheMemory;
       
   145 	TInt iNumberOfShaperCacheEntries;	
       
   146 	};
       
   147 
       
   148 /**
       
   149  @internalComponent
       
   150  */
       
   151 
       
   152 class COpenFontSessionCacheEntry: public COpenFontGlyph
       
   153 	{
       
   154 public:
       
   155 	static COpenFontSessionCacheEntry* New(RHeap* aHeap, const COpenFont* aFont, TInt aCode, TInt aGlyphIndex,
       
   156 										   const TOpenFontCharMetrics& aMetrics, const TDesC8& aBitmap);
       
   157 	inline const COpenFont* Font()const;
       
   158 
       
   159 private:
       
   160 	inline COpenFontSessionCacheEntry(const COpenFont* aFont, TInt aCode, TInt aGlyphIndex, const TOpenFontCharMetrics& aMetrics);
       
   161 	~COpenFontSessionCacheEntry();
       
   162 public:
       
   163     TInt iLastAccess;               // serial number of the last access to the glyph
       
   164 
       
   165 private: 
       
   166     TInt iFontOffset;          // offset of the font that contains this glyph, (not owned by this class!)    
       
   167 	};
       
   168 
       
   169 /**
       
   170  A glyph cache for a particular session.
       
   171  Because session caches are not shared they can shrink as well as grow.
       
   172 
       
   173  @internalComponent
       
   174  */
       
   175 class COpenFontSessionCache
       
   176     {
       
   177 public:
       
   178     static COpenFontSessionCache* NewL(RHeap* aHeap, TInt aSessionHandle, TInt aEntries);
       
   179     void Delete(RHeap* aHeap);
       
   180     
       
   181     TInt SessionHandle() { return iSessionHandle; }
       
   182     const COpenFontGlyph* Glyph(const COpenFont* aFont, TInt aCode, TInt& aIndex);
       
   183     void Insert(RHeap* aHeap, COpenFontSessionCacheEntry* aEntry, TInt aIndex);
       
   184     
       
   185 private:
       
   186     COpenFontSessionCache(TInt aSessionHandle);
       
   187     ~COpenFontSessionCache();
       
   188 public:
       
   189     TInt iSessionHandle;    
       
   190     TInt iLastAccess;
       
   191     ROffsetArray<COpenFontSessionCacheEntry> iEntryArray;
       
   192     };
       
   193 
       
   194 /**
       
   195  @internalComponent
       
   196  */
       
   197 class COpenFontSessionCacheListItem
       
   198 	{
       
   199 public:
       
   200 	COpenFontSessionCacheListItem(COpenFontSessionCache* aCache);
       
   201 
       
   202 	COpenFontSessionCacheListItem* Next();
       
   203 	void SetNext(COpenFontSessionCacheListItem* aNext);
       
   204 	COpenFontSessionCache* Cache(); 
       
   205 
       
   206 	void Delete(RHeap* aHeap);
       
   207 private:
       
   208 	~COpenFontSessionCacheListItem();
       
   209 private:
       
   210 	TInt iNextOffset;
       
   211 	TInt iCacheOffset;
       
   212 	};
       
   213 
       
   214 class TFontTableGlyphOutlineCacheMemMonitor
       
   215     {
       
   216 public:
       
   217     TFontTableGlyphOutlineCacheMemMonitor();
       
   218     void Inc(TInt aBytes);
       
   219     void Dec(TInt aBytes);
       
   220     TInt GetMemUsage();
       
   221 private:
       
   222     TInt iBytes;
       
   223     };
       
   224 
       
   225 struct TCacheUserInfo {
       
   226     TInt iSessionHandle;
       
   227     TInt iRefCount;
       
   228     TCacheUserInfo(TInt aSessionHandle, TInt aRefCount = 0): 
       
   229             iSessionHandle(aSessionHandle), iRefCount(aRefCount) { } 
       
   230 };
       
   231 
       
   232 class CFontTableCache;
       
   233 
       
   234 class CFontTableCacheItem
       
   235     {
       
   236 friend class CFontTableCache ;
       
   237 
       
   238 public:
       
   239     CFontTableCacheItem(TUid &aFileUid, const TUint32 aTag, 
       
   240             TInt aOffset, TInt aLength);
       
   241     ~CFontTableCacheItem(); 
       
   242 
       
   243     TInt DecRefCount(TInt aSessionHandle);
       
   244     TInt IncRefCount(TInt aSessionHandle);
       
   245 
       
   246     TBool HasOutstandingRefCount();
       
   247     TInt FindUser(TInt aSessionHandle, TInt *id);
       
   248     
       
   249 #ifdef _DEBUG
       
   250     void SetUser(RPointerArray<TCacheUserInfo> users)
       
   251         {
       
   252         TInt len = users.Count();
       
   253         for( TInt i = 0; i < len ; i++ )
       
   254             {
       
   255             iUsers.Append(users[i]);
       
   256             }
       
   257         }    
       
   258 #endif
       
   259     
       
   260 private:
       
   261     CFontTableCacheItem(const CFontTableCacheItem &); // disallow copy construction.
       
   262     CFontTableCacheItem& operator =(const CFontTableCacheItem &); // disallow assignment.
       
   263     
       
   264     TUid iFileUid; 
       
   265     TUint32 iTag; 
       
   266  
       
   267 
       
   268     TInt iOffset;
       
   269     TInt iLength; 
       
   270     RPointerArray<TCacheUserInfo> iUsers;
       
   271     }; 
       
   272 
       
   273 
       
   274 class CFontTableCache 
       
   275     {
       
   276 public:
       
   277     CFontTableCache(RHeap* aHeap, TFontTableGlyphOutlineCacheMemMonitor& aMon);
       
   278     ~CFontTableCache();
       
   279     TInt Append(TUid aFileUid, TUint32 aTag, 
       
   280             TAny*& aContent, TInt aLength);
       
   281     TInt Find(TUid aFileUid, TUint32 aTag, TAny*& aContent, TInt& aLength, TInt* id);
       
   282     TInt IncRefCount(TUid FileUid, TUint32 aTag, TInt aSessionHandle);
       
   283     TInt DecRefCount(TUid aFileUid, TUint32 aTag, TInt aSessionHandle);
       
   284     TBool HasOutstandingRefCount();
       
   285     TBool HasOutstandingRefCountWithUid(TUid aFileUid);  
       
   286     void CleanupCacheOnFbsSessionTermination(TInt aSessionHandle);
       
   287     void CleanupCacheOnOpenFontFileRemoval(COpenFontFile*);
       
   288 #ifdef _DEBUG    
       
   289     void SetFontItem(RPointerArray<CFontTableCacheItem> cacheItems)
       
   290         {
       
   291         TInt len = cacheItems.Count();
       
   292         for(TInt i = 0; i < len; i++)
       
   293             {
       
   294             iCacheItems.Append(cacheItems[i]);
       
   295             }
       
   296         }
       
   297 #endif   
       
   298     
       
   299 private:
       
   300     CFontTableCache(const CFontTableCache &); // no copy construction.
       
   301     CFontTableCache& operator =(const CFontTableCache &); // no assignment.
       
   302 #ifdef _DEBUG
       
   303     TInt GetCacheState(const char *func);
       
   304 #endif
       
   305     
       
   306     TFontTableGlyphOutlineCacheMemMonitor &iCacheMemMon;
       
   307     RHeap *iHeap;
       
   308     RPointerArray<CFontTableCacheItem> iCacheItems;
       
   309     };
       
   310 
       
   311 
       
   312 class TUnhintedOutlineCache;
       
   313 
       
   314 class TUnhintedOutlineId 
       
   315     {
       
   316 public:
       
   317     TUnhintedOutlineId(TUid aFileUid, TInt aFaceIndex, TUint aId);
       
   318     TUid iFileUid;
       
   319     TInt iFaceIndex;
       
   320     TUint iId;
       
   321     };
       
   322 
       
   323 class COutlineCacheItem {
       
   324 friend class CUnhintedOutlineCache;
       
   325 friend class CHintedOutlineCache;
       
   326     
       
   327 public:
       
   328     COutlineCacheItem(TInt aOffset, TInt aLength);
       
   329     ~COutlineCacheItem() ;
       
   330 
       
   331     TInt DecRefCount(TInt aSessionHandle);
       
   332     TInt IncRefCount(TInt aSessionHandle);
       
   333 #ifdef _DEBUG
       
   334     void SetUser(RPointerArray<TCacheUserInfo> users)
       
   335         {
       
   336         TInt len = users.Count();
       
   337         for( TInt i = 0; i < len ; i++ )
       
   338             {
       
   339             iUsers.Append(users[i]);
       
   340             }
       
   341         }    
       
   342 #endif
       
   343  
       
   344 private:
       
   345     TInt FindUser(TInt aSessionHandle, TInt *id);
       
   346         
       
   347 
       
   348     TInt iOffset;
       
   349     TInt iLength;
       
   350     RPointerArray<TCacheUserInfo> iUsers;
       
   351 };    
       
   352 
       
   353 class CUnhintedOutlineCache {
       
   354 public:
       
   355     CUnhintedOutlineCache(RHeap* aHeap, TFontTableGlyphOutlineCacheMemMonitor& aMon);
       
   356     TInt Find(const TUnhintedOutlineId &aOutlineId, TAny*& aData, TInt& aLength);
       
   357     TInt IncRefCount(const TUnhintedOutlineId& aOutlineId, TInt aSessionHandle);
       
   358     TInt DecRefCount(const TUnhintedOutlineId& aOutlineId, TInt aSessionHandle);
       
   359     TInt CacheUnhintedOutline(const TUnhintedOutlineId& aOutlineId, 
       
   360             TAny * const aData, const TInt aLength, TAny*& aOutline, TInt &aLen);
       
   361     TInt CleanupCacheOnOpenFontFileRemoval(COpenFontFile* aFontFile);
       
   362     TInt CleanupCacheOnFbsSessionTermination(TInt aSessionHandle);
       
   363 #ifdef _DEBUG
       
   364     TInt GetCacheState(const char *func);
       
   365 #endif
       
   366     ~CUnhintedOutlineCache();
       
   367     
       
   368 #ifdef _DEBUG
       
   369     void SetUnHintedItem(TUnhintedOutlineId id, COutlineCacheItem* item)
       
   370         {
       
   371         iItemIdMap.Insert(id, item);
       
   372         }
       
   373 #endif
       
   374     
       
   375 private:
       
   376     // disallow assignment and copy-construction
       
   377     CUnhintedOutlineCache(const CUnhintedOutlineCache &); 
       
   378     CUnhintedOutlineCache& operator =(const CUnhintedOutlineCache &);
       
   379     
       
   380     static TUint32 IdHash(const TUnhintedOutlineId& aOutlineId);
       
   381     static TBool IdIdentity(const TUnhintedOutlineId& id1, const TUnhintedOutlineId& id2);
       
   382     
       
   383     TFontTableGlyphOutlineCacheMemMonitor& iCacheMemMon;
       
   384     RHeap* iHeap; 
       
   385     RHashMap<TUnhintedOutlineId, COutlineCacheItem*> iItemIdMap; // map the identity to an index in 'iCacheItems'.
       
   386 };
       
   387 
       
   388 class CHintedOutlineCache;
       
   389 
       
   390 class THintedOutlineId 
       
   391     {
       
   392 public:
       
   393     THintedOutlineId(COpenFont* aFont, TUint aId);
       
   394     COpenFont *iFont;
       
   395     TUint iId;
       
   396     };
       
   397 
       
   398 
       
   399 class CHintedOutlineCache {
       
   400 public:
       
   401     CHintedOutlineCache(RHeap* aHeap, TFontTableGlyphOutlineCacheMemMonitor& aMon);
       
   402     TInt Find(const THintedOutlineId& aOutlineId, TAny*& aData, TInt& aLength);
       
   403     TInt IncRefCount(const THintedOutlineId& aOutlineId, TInt aSessionHandle);
       
   404     TInt DecRefCount(const THintedOutlineId& aOutlineId, TInt aSessionHandle);
       
   405     TInt CacheHintedOutline(const THintedOutlineId& aOutlineId, 
       
   406             TAny* aData, TInt aLength, TAny*& aOutline, TInt& aLen);
       
   407     TInt CleanupCacheOnOpenFontRemoval(COpenFont* aFont);
       
   408     TInt CleanupCacheOnFbsSessionTermination(TInt aSessionHandle);
       
   409 #ifdef _DEBUG
       
   410     TInt GetCacheState(const char *func);
       
   411     void SetHintedItem(THintedOutlineId id, COutlineCacheItem* item)
       
   412         {
       
   413         iItemIdMap.Insert(id, item);
       
   414         }
       
   415     RHashMap<THintedOutlineId, COutlineCacheItem*> GetHintedMap()
       
   416     {
       
   417     return iItemIdMap;
       
   418     }
       
   419 #endif
       
   420     
       
   421 private:
       
   422     // disallow assignment and copy-construction
       
   423     CHintedOutlineCache(const CHintedOutlineCache &); 
       
   424     CHintedOutlineCache& operator =(const CHintedOutlineCache &);
       
   425     static TUint32 IdHash(const THintedOutlineId& aOutlineId);
       
   426     static TBool IdIdentity(const THintedOutlineId& id1, const THintedOutlineId& id2);
       
   427         
       
   428     TFontTableGlyphOutlineCacheMemMonitor& iCacheMemMon;
       
   429     RHeap* iHeap; 
       
   430     RHashMap<THintedOutlineId, COutlineCacheItem*> iItemIdMap; // map the identity to an index in 'iCacheItems'.
       
   431     };
       
   432 
       
   433 
       
   434 // inline functions
       
   435 inline COpenFontSessionCacheEntry::COpenFontSessionCacheEntry(const COpenFont* aFont, TInt aCode, TInt aGlyphIndex,const TOpenFontCharMetrics& aMetrics) :
       
   436     COpenFontGlyph(aCode, aGlyphIndex, aMetrics)
       
   437     {
       
   438     iFontOffset = aFont ? reinterpret_cast<TInt>(aFont) - reinterpret_cast<TInt>(this) : 0;
       
   439     }
       
   440 
       
   441 
       
   442 inline const COpenFont* COpenFontSessionCacheEntry::Font() const
       
   443     {
       
   444     if (iFontOffset)
       
   445         {
       
   446         return reinterpret_cast<const COpenFont*> (PtrAdd(this, iFontOffset));
       
   447         }
       
   448     else
       
   449         {
       
   450         return NULL;
       
   451         }
       
   452     }
       
   453 
       
   454 #endif	// __OPENFONTSPRIVATE_H__