os/textandloc/fontservices/fontstore/inc/openfontsprivate.h
author sl
Tue, 10 Jun 2014 14:32:02 +0200
changeset 1 260cb5ec6c19
permissions -rw-r--r--
Update contrib.
     1 /*
     2 * Copyright (c) 2006-2010 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 <hextree.h>
    23 #include <e32hashtab.h>
    24 
    25 class COpenFontShaperCacheEntry;
    26 
    27 /* MSB is set to indicate a glyph code rather than a unicode value
    28    This is used for extracting the code value */
    29 #define GLYPH_CODE(code) (code & 0x7fffffff)
    30 
    31 /** COpenFontFile owned container for extra data. May be extended without affecting Binary Compatibility.
    32 
    33  @internalComponent
    34  */
    35 
    36 class TOpenFontFileData
    37 	{
    38 public:
    39 	CFontStore* iFontStore;			// pointer to the CFontStore instance that loaded the COpenFontFile
    40 	};
    41 
    42 /**
    43  Note: this class must be self-contained, since instances are added to an RHexTree,
    44  that is, it must be possible to destroy instances simply with RHeap::Free().
    45  @internalComponent
    46  */
    47 class COpenFontGlyph
    48 	{
    49 public:
    50     static COpenFontGlyph* New(RHeap* aHeap, TInt aCode, TInt aGlyphIndex,
    51                                const TOpenFontCharMetrics& aMetrics, const TDesC8& aBitmap);
    52 	inline static void Delete(RHeap* aHeap, COpenFontGlyph* aGlyph);
    53 	inline const TUint8* Bitmap() const;
    54 
    55 protected:
    56 	COpenFontGlyph(TInt aCode, TInt aGlyphIndex, const TOpenFontCharMetrics& aMetrics)
    57 		: iCode(aCode), iGlyphIndex(aGlyphIndex), iMetrics(aMetrics), iBitmapOffset(0) {}
    58 	~COpenFontGlyph() {}
    59 	void SetBitmap(const TAny* aBitmap);
    60 
    61 public:
    62 	const TInt iCode;						// the Unicode value of the character
    63 	const TInt iGlyphIndex;					// the glyph index
    64 	const TOpenFontCharMetrics iMetrics;	// the metrics
    65 
    66 private:
    67 	// an offset from this COpenFontGlyph object to a pointer to the run-length-encoded bitmap, 
    68 	// calculated as (bitmapPointer)-(this)
    69 	TInt iBitmapOffset;
    70 	};
    71 
    72 /**
    73  * Template for offset implementation of pointer array
    74  @internalComponent
    75  */
    76 template<class T>
    77 class ROffsetArray
    78     {
    79 public:
    80     ROffsetArray();
    81     TInt Create(RHeap* aHeap, TInt aCount);
    82     void Close(RHeap* aHeap);
    83     TInt Count() const;
    84     T* operator[](TInt aIndex) const;
    85     void SetAt(TInt aIndex, T* aEntry);
    86 private:
    87     TInt iOffset;
    88     TInt iCount;
    89     };
    90 
    91 
    92 /**
    93  The per-font glyph cache. For now, just the members that used to be directly in
    94  COpenFont. Now it is a private class it can be elaborated to do character-to-glyph-index
    95  mapping when that is needed.
    96 
    97  @internalComponent
    98  */
    99 class COpenFontGlyphCache
   100 	{
   101 public:
   102 	COpenFontGlyphCache(RHeap* aHeap)
   103 	  : iGlyphTreeById(aHeap),
   104 		iGlyphTreeByUnicode(aHeap),
   105 		iGlyphCacheMemory(0),
   106 		iShaperCacheSentinel(NULL),
   107 		iShapingInfoCacheMemory(0),
   108 		iNumberOfShaperCacheEntries(0)
   109 		{}
   110 	TShapeHeader* SearchShaperCache(TInt aSessionHandle, TFontShapeFunctionParameters*& aParams);
   111 	TShapeHeader* Insert(TInt aSessionHandle, RHeap* aHeap, CShaper::TInput aInput, TShapeHeader* aShapeHeader, TInt& aAddedBytes);
   112 	TInt DeleteLeastRecentlyUsedEntry(RHeap* aHeap);
   113 	TBool ShaperCacheIsEmpty();
   114 	
   115 public:
   116 	RHexTree<COpenFontGlyph> iGlyphTreeById;				// a hex tree of glyphs indexed by glyph ID
   117 	RHexTree<COpenFontGlyph> iGlyphTreeByUnicode;			// a hex tree of glyphs indexed by Unicode code point
   118 	TInt iGlyphCacheMemory;									// memory used by the glyph tree in bytes
   119 	COpenFontShaperCacheEntry* iShaperCacheSentinel;
   120 	TInt iShapingInfoCacheMemory;
   121 	TInt iNumberOfShaperCacheEntries;	
   122 	};
   123 
   124 /**
   125  @internalComponent
   126  */
   127 class COpenFontSessionCacheEntry: public COpenFontGlyph
   128 	{
   129 public:
   130 	static COpenFontSessionCacheEntry* New(RHeap* aHeap, const COpenFont* aFont, TInt aCode, TInt aGlyphIndex,
   131 										   const TOpenFontCharMetrics& aMetrics, const TDesC8& aBitmap);
   132 	inline static void Delete(RHeap* aHeap, COpenFontSessionCacheEntry* aEntry);
   133 	inline const COpenFont* Font()const;
   134 
   135 private:
   136 	inline COpenFontSessionCacheEntry(const COpenFont* aFont, TInt aCode, TInt aGlyphIndex, const TOpenFontCharMetrics& aMetrics);
   137 	~COpenFontSessionCacheEntry();
   138 
   139 private: 
   140     TInt iFontOffset;          // offset of the font that contains this glyph, (not owned by this class!)    
   141 	};
   142 
   143 /**
   144  A glyph cache for a particular session.
   145  Because session caches are not shared they can shrink as well as grow.
   146 
   147  @internalComponent
   148  */
   149 class COpenFontSessionCache
   150     {
   151 	friend class COpenFontSessionCacheList;
   152 public:
   153     static COpenFontSessionCache* NewL(RHeap* aHeap, TInt aSessionHandle, TInt aEntries);
   154     void Delete(RHeap* aHeap);
   155     
   156     TInt SessionHandle() { return iSessionHandle; }
   157     const COpenFontGlyph* Glyph(const COpenFont* aFont, TInt aCode, TInt& aIndex) const;
   158     void Insert(RHeap* aHeap, COpenFontSessionCacheEntry* aEntry, TInt aIndex);
   159     
   160 private:
   161     COpenFontSessionCache(TInt aSessionHandle);
   162     ~COpenFontSessionCache();
   163 private:
   164     TInt iSessionHandle;
   165     TInt64 iRandomSeed;
   166     ROffsetArray<COpenFontSessionCacheEntry> iEntryArray;
   167     };
   168 
   169 
   170 class TFontTableGlyphOutlineCacheMemMonitor
   171     {
   172 public:
   173     TFontTableGlyphOutlineCacheMemMonitor();
   174     void Inc(TInt aBytes);
   175     void Dec(TInt aBytes);
   176     TInt GetMemUsage();
   177 private:
   178     TInt iBytes;
   179     };
   180 
   181 struct TCacheUserInfo {
   182     TInt iSessionHandle;
   183     TInt iRefCount;
   184     TCacheUserInfo(TInt aSessionHandle, TInt aRefCount = 0): 
   185             iSessionHandle(aSessionHandle), iRefCount(aRefCount) { } 
   186 };
   187 
   188 class CFontTableCache;
   189 
   190 class CFontTableCacheItem
   191     {
   192 friend class CFontTableCache ;
   193 
   194 public:
   195     CFontTableCacheItem(TUid &aFileUid, const TUint32 aTag, 
   196             TInt aOffset, TInt aLength);
   197     ~CFontTableCacheItem(); 
   198 
   199     TInt DecRefCount(TInt aSessionHandle);
   200     TInt IncRefCount(TInt aSessionHandle);
   201 
   202     TBool HasOutstandingRefCount();
   203     TInt FindUser(TInt aSessionHandle, TInt *id);
   204     
   205 #ifdef _DEBUG
   206     void SetUser(RPointerArray<TCacheUserInfo> users)
   207         {
   208         TInt len = users.Count();
   209         for( TInt i = 0; i < len ; i++ )
   210             {
   211             iUsers.Append(users[i]);
   212             }
   213         }    
   214 #endif
   215     
   216 private:
   217     CFontTableCacheItem(const CFontTableCacheItem &); // disallow copy construction.
   218     CFontTableCacheItem& operator =(const CFontTableCacheItem &); // disallow assignment.
   219     
   220     TUid iFileUid; 
   221     TUint32 iTag; 
   222  
   223 
   224     TInt iOffset;
   225     TInt iLength; 
   226     RPointerArray<TCacheUserInfo> iUsers;
   227     }; 
   228 
   229 
   230 class CFontTableCache 
   231     {
   232 public:
   233     CFontTableCache(RHeap* aHeap, TFontTableGlyphOutlineCacheMemMonitor& aMon);
   234     ~CFontTableCache();
   235     TInt Append(TUid aFileUid, TUint32 aTag, 
   236             TAny*& aContent, TInt aLength);
   237     TInt Find(TUid aFileUid, TUint32 aTag, TAny*& aContent, TInt& aLength, TInt* id);
   238     TInt IncRefCount(TUid FileUid, TUint32 aTag, TInt aSessionHandle);
   239     TInt DecRefCount(TUid aFileUid, TUint32 aTag, TInt aSessionHandle);
   240     TBool HasOutstandingRefCount();
   241     TBool HasOutstandingRefCountWithUid(TUid aFileUid);  
   242     void CleanupCacheOnFbsSessionTermination(TInt aSessionHandle);
   243     void CleanupCacheOnOpenFontFileRemoval(COpenFontFile*);
   244 #ifdef _DEBUG    
   245     void SetFontItem(RPointerArray<CFontTableCacheItem> cacheItems)
   246         {
   247         TInt len = cacheItems.Count();
   248         for(TInt i = 0; i < len; i++)
   249             {
   250             iCacheItems.Append(cacheItems[i]);
   251             }
   252         }
   253 #endif   
   254     
   255 private:
   256     CFontTableCache(const CFontTableCache &); // no copy construction.
   257     CFontTableCache& operator =(const CFontTableCache &); // no assignment.
   258 #ifdef _DEBUG
   259     TInt GetCacheState(const char *func);
   260 #endif
   261     
   262     TFontTableGlyphOutlineCacheMemMonitor &iCacheMemMon;
   263     RHeap *iHeap;
   264     RPointerArray<CFontTableCacheItem> iCacheItems;
   265     };
   266 
   267 
   268 class TUnhintedOutlineCache;
   269 
   270 class TUnhintedOutlineId 
   271     {
   272 public:
   273     TUnhintedOutlineId(TUid aFileUid, TInt aFaceIndex, TUint aId);
   274     TUid iFileUid;
   275     TInt iFaceIndex;
   276     TUint iId;
   277     };
   278 
   279 class COutlineCacheItem {
   280 friend class CUnhintedOutlineCache;
   281 friend class CHintedOutlineCache;
   282     
   283 public:
   284     COutlineCacheItem(TInt aOffset, TInt aLength);
   285     ~COutlineCacheItem() ;
   286 
   287     TInt DecRefCount(TInt aSessionHandle);
   288     TInt IncRefCount(TInt aSessionHandle);
   289 #ifdef _DEBUG
   290     void SetUser(RPointerArray<TCacheUserInfo> users)
   291         {
   292         TInt len = users.Count();
   293         for( TInt i = 0; i < len ; i++ )
   294             {
   295             iUsers.Append(users[i]);
   296             }
   297         }    
   298 #endif
   299  
   300 private:
   301     TInt FindUser(TInt aSessionHandle, TInt *id);
   302         
   303 
   304     TInt iOffset;
   305     TInt iLength;
   306     RPointerArray<TCacheUserInfo> iUsers;
   307 };    
   308 
   309 class CUnhintedOutlineCache {
   310 public:
   311     CUnhintedOutlineCache(RHeap* aHeap, TFontTableGlyphOutlineCacheMemMonitor& aMon);
   312     TInt Find(const TUnhintedOutlineId &aOutlineId, TAny*& aData, TInt& aLength);
   313     TInt IncRefCount(const TUnhintedOutlineId& aOutlineId, TInt aSessionHandle);
   314     TInt DecRefCount(const TUnhintedOutlineId& aOutlineId, TInt aSessionHandle);
   315     TInt CacheUnhintedOutline(const TUnhintedOutlineId& aOutlineId, 
   316             TAny * const aData, const TInt aLength, TAny*& aOutline, TInt &aLen);
   317     TInt CleanupCacheOnOpenFontFileRemoval(COpenFontFile* aFontFile);
   318     TInt CleanupCacheOnFbsSessionTermination(TInt aSessionHandle);
   319 #ifdef _DEBUG
   320     TInt GetCacheState(const char *func);
   321 #endif
   322     ~CUnhintedOutlineCache();
   323     
   324 #ifdef _DEBUG
   325     void SetUnHintedItem(TUnhintedOutlineId id, COutlineCacheItem* item)
   326         {
   327         iItemIdMap.Insert(id, item);
   328         }
   329 #endif
   330     
   331 private:
   332     // disallow assignment and copy-construction
   333     CUnhintedOutlineCache(const CUnhintedOutlineCache &); 
   334     CUnhintedOutlineCache& operator =(const CUnhintedOutlineCache &);
   335     
   336     static TUint32 IdHash(const TUnhintedOutlineId& aOutlineId);
   337     static TBool IdIdentity(const TUnhintedOutlineId& id1, const TUnhintedOutlineId& id2);
   338     
   339     TFontTableGlyphOutlineCacheMemMonitor& iCacheMemMon;
   340     RHeap* iHeap; 
   341     RHashMap<TUnhintedOutlineId, COutlineCacheItem*> iItemIdMap; // map the identity to an index in 'iCacheItems'.
   342 };
   343 
   344 class CHintedOutlineCache;
   345 
   346 class THintedOutlineId 
   347     {
   348 public:
   349     THintedOutlineId(COpenFont* aFont, TUint aId);
   350     COpenFont *iFont;
   351     TUint iId;
   352     };
   353 
   354 
   355 class CHintedOutlineCache {
   356 public:
   357     CHintedOutlineCache(RHeap* aHeap, TFontTableGlyphOutlineCacheMemMonitor& aMon);
   358     TInt Find(const THintedOutlineId& aOutlineId, TAny*& aData, TInt& aLength);
   359     TInt IncRefCount(const THintedOutlineId& aOutlineId, TInt aSessionHandle);
   360     TInt DecRefCount(const THintedOutlineId& aOutlineId, TInt aSessionHandle);
   361     TInt CacheHintedOutline(const THintedOutlineId& aOutlineId, 
   362             TAny* aData, TInt aLength, TAny*& aOutline, TInt& aLen);
   363     TInt CleanupCacheOnOpenFontRemoval(COpenFont* aFont);
   364     TInt CleanupCacheOnFbsSessionTermination(TInt aSessionHandle);
   365 #ifdef _DEBUG
   366     TInt GetCacheState(const char *func);
   367     void SetHintedItem(THintedOutlineId id, COutlineCacheItem* item)
   368         {
   369         iItemIdMap.Insert(id, item);
   370         }
   371     RHashMap<THintedOutlineId, COutlineCacheItem*> GetHintedMap()
   372     {
   373     return iItemIdMap;
   374     }
   375 #endif
   376     
   377 private:
   378     // disallow assignment and copy-construction
   379     CHintedOutlineCache(const CHintedOutlineCache &); 
   380     CHintedOutlineCache& operator =(const CHintedOutlineCache &);
   381     static TUint32 IdHash(const THintedOutlineId& aOutlineId);
   382     static TBool IdIdentity(const THintedOutlineId& id1, const THintedOutlineId& id2);
   383         
   384     TFontTableGlyphOutlineCacheMemMonitor& iCacheMemMon;
   385     RHeap* iHeap; 
   386     RHashMap<THintedOutlineId, COutlineCacheItem*> iItemIdMap; // map the identity to an index in 'iCacheItems'.
   387     };
   388 
   389 
   390 // inline functions
   391 
   392 inline void COpenFontGlyph::Delete(RHeap* aHeap, COpenFontGlyph* aGlyph)
   393 	{
   394 	aHeap->Free(aGlyph);
   395 	}
   396 
   397 /**
   398 @return A pointer to the bitmap data stored with this glyph, or NULL
   399 if no bitmap has been stored with this glyph.
   400  */
   401 inline const TUint8* COpenFontGlyph::Bitmap() const
   402 	{
   403 	if (iBitmapOffset)
   404 		{
   405 		return reinterpret_cast<const TUint8*>(PtrAdd(this, iBitmapOffset));
   406 		}
   407 	return NULL;
   408 	}
   409 
   410 inline COpenFontSessionCacheEntry::COpenFontSessionCacheEntry(const COpenFont* aFont, TInt aCode, TInt aGlyphIndex,const TOpenFontCharMetrics& aMetrics) :
   411     COpenFontGlyph(aCode, aGlyphIndex, aMetrics)
   412     {
   413     iFontOffset = aFont ? reinterpret_cast<TInt>(aFont) - reinterpret_cast<TInt>(this) : 0;
   414     }
   415 
   416 inline void COpenFontSessionCacheEntry::Delete(RHeap* aHeap, COpenFontSessionCacheEntry* aEntry)
   417 	{
   418 	COpenFontGlyph::Delete(aHeap, aEntry);
   419 	}
   420 
   421 inline const COpenFont* COpenFontSessionCacheEntry::Font() const
   422     {
   423     if (iFontOffset)
   424         {
   425         return reinterpret_cast<const COpenFont*> (PtrAdd(this, iFontOffset));
   426         }
   427     return NULL;
   428     }
   429 
   430 #endif	// __OPENFONTSPRIVATE_H__