• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2 **************************************************************************
3  *   © 2016 and later: Unicode, Inc. and others.
4  *   License & terms of use: http://www.unicode.org/copyright.html#License
5  *************************************************************************
6  *************************************************************************
7  *   Copyright (C) 2003-2013, International Business Machines
8  *   Corporation and others.  All Rights Reserved.
9  *************************************************************************
10  */
11 
12 #include "layout/LETypes.h"
13 
14 //#include "letest.h"
15 #include "FontTableCache.h"
16 
17 #define TABLE_CACHE_INIT 5
18 #define TABLE_CACHE_GROW 5
19 
20 struct FontTableCacheEntry
21 {
22   LETag tag;
23   const void *table;
24   size_t length;
25 };
26 
FontTableCache()27 FontTableCache::FontTableCache()
28     : fTableCacheCurr(0), fTableCacheSize(TABLE_CACHE_INIT)
29 {
30     fTableCache = LE_NEW_ARRAY(FontTableCacheEntry, fTableCacheSize);
31 
32     if (fTableCache == NULL) {
33         fTableCacheSize = 0;
34         return;
35     }
36 
37     for (int i = 0; i < fTableCacheSize; i += 1) {
38         fTableCache[i].tag   = 0;
39         fTableCache[i].table = NULL;
40         fTableCache[i].length = 0;
41     }
42 }
43 
~FontTableCache()44 FontTableCache::~FontTableCache()
45 {
46     for (int i = fTableCacheCurr - 1; i >= 0; i -= 1) {
47       LE_DELETE_ARRAY(fTableCache[i].table);
48 
49         fTableCache[i].tag   = 0;
50         fTableCache[i].table = NULL;
51         fTableCache[i].length = 0;
52     }
53 
54     fTableCacheCurr = 0;
55 
56     LE_DELETE_ARRAY(fTableCache);
57 }
58 
freeFontTable(const void * table) const59 void FontTableCache::freeFontTable(const void *table) const
60 {
61   LE_DELETE_ARRAY(table);
62 }
63 
find(LETag tableTag,size_t & length) const64 const void *FontTableCache::find(LETag tableTag, size_t &length) const
65 {
66     for (int i = 0; i < fTableCacheCurr; i += 1) {
67         if (fTableCache[i].tag == tableTag) {
68           length = fTableCache[i].length;
69           return fTableCache[i].table;
70         }
71     }
72 
73     const void *table = readFontTable(tableTag, length);
74 
75     ((FontTableCache *) this)->add(tableTag, table, length);
76 
77     return table;
78 }
79 
add(LETag tableTag,const void * table,size_t length)80 void FontTableCache::add(LETag tableTag, const void *table, size_t length)
81 {
82     if (fTableCacheCurr >= fTableCacheSize) {
83         le_int32 newSize = fTableCacheSize + TABLE_CACHE_GROW;
84 
85         fTableCache = (FontTableCacheEntry *) LE_GROW_ARRAY(fTableCache, newSize);
86 
87         for (le_int32 i = fTableCacheSize; i < newSize; i += 1) {
88             fTableCache[i].tag   = 0;
89             fTableCache[i].table = NULL;
90             fTableCache[i].length = 0;
91         }
92 
93         fTableCacheSize = newSize;
94     }
95 
96     fTableCache[fTableCacheCurr].tag   = tableTag;
97     fTableCache[fTableCacheCurr].table = table;
98     fTableCache[fTableCacheCurr].length = length;
99 
100     fTableCacheCurr += 1;
101 }
102