fontservices/textshaperplugin/source/FontTableCache.cpp
changeset 0 1fb32624e06b
equal deleted inserted replaced
-1:000000000000 0:1fb32624e06b
       
     1 /*
       
     2  **********************************************************************
       
     3  *   Copyright (C) 2003, International Business Machines
       
     4  *   Corporation and others.  All Rights Reserved.
       
     5  **********************************************************************
       
     6  */
       
     7 
       
     8 #include "layout/LETypes.h"
       
     9 
       
    10 #include "FontTableCache.h"
       
    11 
       
    12 #define TABLE_CACHE_INIT 5
       
    13 #define TABLE_CACHE_GROW 5
       
    14 
       
    15 struct FontTableCacheEntry
       
    16 {
       
    17     LETag tag;
       
    18     const void *table;
       
    19 };
       
    20 
       
    21 FontTableCache::FontTableCache()
       
    22     : fTableCacheCurr(0), fTableCacheSize(TABLE_CACHE_INIT)
       
    23 {
       
    24     fTableCache = LE_NEW_ARRAY(FontTableCacheEntry, fTableCacheSize);
       
    25 
       
    26     if (fTableCache == NULL) {
       
    27         fTableCacheSize = 0;
       
    28         return;
       
    29     }
       
    30 
       
    31     for (int i = 0; i < fTableCacheSize; i += 1) {
       
    32         fTableCache[i].tag   = 0;
       
    33         fTableCache[i].table = NULL;
       
    34     }
       
    35 }
       
    36 
       
    37 FontTableCache::~FontTableCache()
       
    38 {
       
    39     for (int i = fTableCacheCurr - 1; i >= 0; i -= 1) {
       
    40         LE_DELETE_ARRAY(fTableCache[i].table);
       
    41 
       
    42         fTableCache[i].tag   = 0;
       
    43         fTableCache[i].table = NULL;
       
    44     }
       
    45 
       
    46     fTableCacheCurr = 0;
       
    47     /* Nokia defect fix for memory leak - Jas: 23/06/2005 */
       
    48 	LE_DELETE_ARRAY(fTableCache);
       
    49 }
       
    50 
       
    51 const void *FontTableCache::find(LETag tableTag) const
       
    52 {
       
    53     for (int i = 0; i < fTableCacheCurr; i += 1) {
       
    54         if (fTableCache[i].tag == tableTag) {
       
    55             return fTableCache[i].table;
       
    56         }
       
    57     }
       
    58 
       
    59     const void *table = readFontTable(tableTag);
       
    60 
       
    61     if (table) {
       
    62         ((FontTableCache *) this)->add(tableTag, table);
       
    63     }
       
    64 
       
    65     return table;
       
    66 }
       
    67 
       
    68 void FontTableCache::add(LETag tableTag, const void *table)
       
    69 {
       
    70     if (fTableCacheCurr >= fTableCacheSize) {
       
    71         le_int32 newSize = fTableCacheSize + TABLE_CACHE_GROW;
       
    72 
       
    73         FontTableCacheEntry *newTableCache =
       
    74             (FontTableCacheEntry *) LE_GROW_ARRAY(fTableCache, newSize);
       
    75         if (!newTableCache) {
       
    76             // could not grow the cache, so we won't cache the table.
       
    77             return;
       
    78         }
       
    79         fTableCache = newTableCache;
       
    80 
       
    81         for (le_int32 i = fTableCacheSize; i < newSize; i += 1) {
       
    82             fTableCache[i].tag   = 0;
       
    83             fTableCache[i].table = NULL;
       
    84         }
       
    85 
       
    86         fTableCacheSize = newSize;
       
    87     }
       
    88 
       
    89     fTableCache[fTableCacheCurr].tag   = tableTag;
       
    90     fTableCache[fTableCacheCurr].table = table;
       
    91 
       
    92     fTableCacheCurr += 1;
       
    93 }