]> git.saurik.com Git - apple/icu.git/blob - icuSources/samples/layout/FontTableCache.cpp
ICU-64243.0.1.tar.gz
[apple/icu.git] / icuSources / samples / layout / FontTableCache.cpp
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 - 2008, International Business Machines
8 * Corporation and others. All Rights Reserved.
9 *************************************************************************
10 */
11
12 #include "layout/LETypes.h"
13
14 #include "FontTableCache.h"
15
16 #define TABLE_CACHE_INIT 5
17 #define TABLE_CACHE_GROW 5
18
19 struct FontTableCacheEntry
20 {
21 LETag tag;
22 const void *table;
23 };
24
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 }
39 }
40
41 FontTableCache::~FontTableCache()
42 {
43 for (int i = fTableCacheCurr - 1; i >= 0; i -= 1) {
44 freeFontTable(fTableCache[i].table);
45
46 fTableCache[i].tag = 0;
47 fTableCache[i].table = NULL;
48 }
49
50 fTableCacheCurr = 0;
51
52 LE_DELETE_ARRAY(fTableCache);
53 fTableCache = NULL;
54 }
55
56 void FontTableCache::freeFontTable(const void *table) const
57 {
58 LE_DELETE_ARRAY(table);
59 }
60
61 const void *FontTableCache::find(LETag tableTag) const
62 {
63 for (int i = 0; i < fTableCacheCurr; i += 1) {
64 if (fTableCache[i].tag == tableTag) {
65 return fTableCache[i].table;
66 }
67 }
68
69 const void *table = readFontTable(tableTag);
70
71 ((FontTableCache *) this)->add(tableTag, table);
72
73 return table;
74 }
75
76 void FontTableCache::add(LETag tableTag, const void *table)
77 {
78 if (fTableCacheCurr >= fTableCacheSize) {
79 le_int32 newSize = fTableCacheSize + TABLE_CACHE_GROW;
80
81 fTableCache = (FontTableCacheEntry *) LE_GROW_ARRAY(fTableCache, newSize);
82
83 for (le_int32 i = fTableCacheSize; i < newSize; i += 1) {
84 fTableCache[i].tag = 0;
85 fTableCache[i].table = NULL;
86 }
87
88 fTableCacheSize = newSize;
89 }
90
91 fTableCache[fTableCacheCurr].tag = tableTag;
92 fTableCache[fTableCacheCurr].table = table;
93
94 fTableCacheCurr += 1;
95 }