Home | History | Annotate | Download | only in letest
      1 /*
      2  **********************************************************************
      3  *   Copyright (C) 2003-2008, International Business Machines
      4  *   Corporation and others.  All Rights Reserved.
      5  **********************************************************************
      6  */
      7 
      8 #include "layout/LETypes.h"
      9 
     10 #include "letest.h"
     11 #include "FontTableCache.h"
     12 
     13 #define TABLE_CACHE_INIT 5
     14 #define TABLE_CACHE_GROW 5
     15 
     16 struct FontTableCacheEntry
     17 {
     18     LETag tag;
     19     const void *table;
     20 };
     21 
     22 FontTableCache::FontTableCache()
     23     : fTableCacheCurr(0), fTableCacheSize(TABLE_CACHE_INIT)
     24 {
     25     fTableCache = NEW_ARRAY(FontTableCacheEntry, fTableCacheSize);
     26 
     27     if (fTableCache == NULL) {
     28         fTableCacheSize = 0;
     29         return;
     30     }
     31 
     32     for (int i = 0; i < fTableCacheSize; i += 1) {
     33         fTableCache[i].tag   = 0;
     34         fTableCache[i].table = NULL;
     35     }
     36 }
     37 
     38 FontTableCache::~FontTableCache()
     39 {
     40     for (int i = fTableCacheCurr - 1; i >= 0; i -= 1) {
     41         DELETE_ARRAY(fTableCache[i].table);
     42 
     43         fTableCache[i].tag   = 0;
     44         fTableCache[i].table = NULL;
     45     }
     46 
     47     fTableCacheCurr = 0;
     48 
     49     DELETE_ARRAY(fTableCache);
     50 }
     51 
     52 void FontTableCache::freeFontTable(const void *table) const
     53 {
     54     DELETE_ARRAY(table);
     55 }
     56 
     57 const void *FontTableCache::find(LETag tableTag) const
     58 {
     59     for (int i = 0; i < fTableCacheCurr; i += 1) {
     60         if (fTableCache[i].tag == tableTag) {
     61             return fTableCache[i].table;
     62         }
     63     }
     64 
     65     const void *table = readFontTable(tableTag);
     66 
     67     ((FontTableCache *) this)->add(tableTag, table);
     68 
     69     return table;
     70 }
     71 
     72 void FontTableCache::add(LETag tableTag, const void *table)
     73 {
     74     if (fTableCacheCurr >= fTableCacheSize) {
     75         le_int32 newSize = fTableCacheSize + TABLE_CACHE_GROW;
     76 
     77         fTableCache = (FontTableCacheEntry *) GROW_ARRAY(fTableCache, newSize);
     78 
     79         for (le_int32 i = fTableCacheSize; i < newSize; i += 1) {
     80             fTableCache[i].tag   = 0;
     81             fTableCache[i].table = NULL;
     82         }
     83 
     84         fTableCacheSize = newSize;
     85     }
     86 
     87     fTableCache[fTableCacheCurr].tag   = tableTag;
     88     fTableCache[fTableCacheCurr].table = table;
     89 
     90     fTableCacheCurr += 1;
     91 }
     92