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