2 *************************************************************************
3 * Copyright (C) 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 - 2008, International Business Machines
8 * Corporation and others. All Rights Reserved.
9 *************************************************************************
12 #include "layout/LETypes.h"
14 #include "FontTableCache.h"
16 #define TABLE_CACHE_INIT 5
17 #define TABLE_CACHE_GROW 5
19 struct FontTableCacheEntry
25 FontTableCache::FontTableCache()
26 : fTableCacheCurr(0), fTableCacheSize(TABLE_CACHE_INIT)
28 fTableCache = LE_NEW_ARRAY(FontTableCacheEntry, fTableCacheSize);
30 if (fTableCache == NULL) {
35 for (int i = 0; i < fTableCacheSize; i += 1) {
36 fTableCache[i].tag = 0;
37 fTableCache[i].table = NULL;
41 FontTableCache::~FontTableCache()
43 for (int i = fTableCacheCurr - 1; i >= 0; i -= 1) {
44 freeFontTable(fTableCache[i].table);
46 fTableCache[i].tag = 0;
47 fTableCache[i].table = NULL;
52 LE_DELETE_ARRAY(fTableCache);
56 void FontTableCache::freeFontTable(const void *table) const
58 LE_DELETE_ARRAY(table);
61 const void *FontTableCache::find(LETag tableTag) const
63 for (int i = 0; i < fTableCacheCurr; i += 1) {
64 if (fTableCache[i].tag == tableTag) {
65 return fTableCache[i].table;
69 const void *table = readFontTable(tableTag);
71 ((FontTableCache *) this)->add(tableTag, table);
76 void FontTableCache::add(LETag tableTag, const void *table)
78 if (fTableCacheCurr >= fTableCacheSize) {
79 le_int32 newSize = fTableCacheSize + TABLE_CACHE_GROW;
81 fTableCache = (FontTableCacheEntry *) LE_GROW_ARRAY(fTableCache, newSize);
83 for (le_int32 i = fTableCacheSize; i < newSize; i += 1) {
84 fTableCache[i].tag = 0;
85 fTableCache[i].table = NULL;
88 fTableCacheSize = newSize;
91 fTableCache[fTableCacheCurr].tag = tableTag;
92 fTableCache[fTableCacheCurr].table = table;