]> git.saurik.com Git - apple/icu.git/blob - icuSources/test/letest/FontTableCache.cpp
ICU-551.24.tar.gz
[apple/icu.git] / icuSources / test / letest / FontTableCache.cpp
1 /*
2 **********************************************************************
3 * Copyright (C) 2003-2013, 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 size_t length;
21 };
22
23 FontTableCache::FontTableCache()
24 : fTableCacheCurr(0), fTableCacheSize(TABLE_CACHE_INIT)
25 {
26 fTableCache = LE_NEW_ARRAY(FontTableCacheEntry, fTableCacheSize);
27
28 if (fTableCache == NULL) {
29 fTableCacheSize = 0;
30 return;
31 }
32
33 for (int i = 0; i < fTableCacheSize; i += 1) {
34 fTableCache[i].tag = 0;
35 fTableCache[i].table = NULL;
36 fTableCache[i].length = 0;
37 }
38 }
39
40 FontTableCache::~FontTableCache()
41 {
42 for (int i = fTableCacheCurr - 1; i >= 0; i -= 1) {
43 LE_DELETE_ARRAY(fTableCache[i].table);
44
45 fTableCache[i].tag = 0;
46 fTableCache[i].table = NULL;
47 fTableCache[i].length = 0;
48 }
49
50 fTableCacheCurr = 0;
51
52 LE_DELETE_ARRAY(fTableCache);
53 }
54
55 void FontTableCache::freeFontTable(const void *table) const
56 {
57 LE_DELETE_ARRAY(table);
58 }
59
60 const void *FontTableCache::find(LETag tableTag, size_t &length) const
61 {
62 for (int i = 0; i < fTableCacheCurr; i += 1) {
63 if (fTableCache[i].tag == tableTag) {
64 length = fTableCache[i].length;
65 return fTableCache[i].table;
66 }
67 }
68
69 const void *table = readFontTable(tableTag, length);
70
71 ((FontTableCache *) this)->add(tableTag, table, length);
72
73 return table;
74 }
75
76 void FontTableCache::add(LETag tableTag, const void *table, size_t length)
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 fTableCache[i].length = 0;
87 }
88
89 fTableCacheSize = newSize;
90 }
91
92 fTableCache[fTableCacheCurr].tag = tableTag;
93 fTableCache[fTableCacheCurr].table = table;
94 fTableCache[fTableCacheCurr].length = length;
95
96 fTableCacheCurr += 1;
97 }