]> git.saurik.com Git - apple/icu.git/blame - icuSources/samples/layout/FontTableCache.cpp
ICU-8.11.tar.gz
[apple/icu.git] / icuSources / samples / layout / FontTableCache.cpp
CommitLineData
b75a7d8f
A
1/*
2 **********************************************************************
3 * Copyright (C) 2003, International Business Machines
4 * Corporation and others. All Rights Reserved.
5 **********************************************************************
6 */
7
8#include "layout/LETypes.h"
9
10#include "FontTableCache.h"
11
12#define TABLE_CACHE_INIT 5
13#define TABLE_CACHE_GROW 5
14
15struct FontTableCacheEntry
16{
17 LETag tag;
18 const void *table;
19};
20
21FontTableCache::FontTableCache()
22 : fTableCacheCurr(0), fTableCacheSize(TABLE_CACHE_INIT)
23{
24 fTableCache = LE_NEW_ARRAY(FontTableCacheEntry, fTableCacheSize);
25
26 if (fTableCache == NULL) {
27 fTableCacheSize = 0;
28 return;
29 }
30
31 for (int i = 0; i < fTableCacheSize; i += 1) {
32 fTableCache[i].tag = 0;
33 fTableCache[i].table = NULL;
34 }
35}
36
37FontTableCache::~FontTableCache()
38{
39 for (int i = fTableCacheCurr - 1; i >= 0; i -= 1) {
40 LE_DELETE_ARRAY(fTableCache[i].table);
41
42 fTableCache[i].tag = 0;
43 fTableCache[i].table = NULL;
44 }
45
46 fTableCacheCurr = 0;
47
48 LE_DELETE_ARRAY(fTableCache);
49 fTableCache = NULL;
50}
51
52const void *FontTableCache::find(LETag tableTag) const
53{
54 for (int i = 0; i < fTableCacheCurr; i += 1) {
55 if (fTableCache[i].tag == tableTag) {
56 return fTableCache[i].table;
57 }
58 }
59
60 const void *table = readFontTable(tableTag);
61
62 ((FontTableCache *) this)->add(tableTag, table);
63
64 return table;
65}
66
67void FontTableCache::add(LETag tableTag, const void *table)
68{
69 if (fTableCacheCurr >= fTableCacheSize) {
70 le_int32 newSize = fTableCacheSize + TABLE_CACHE_GROW;
71
72 fTableCache = (FontTableCacheEntry *) LE_GROW_ARRAY(fTableCache, newSize);
73
74 for (le_int32 i = fTableCacheSize; i < newSize; i += 1) {
75 fTableCache[i].tag = 0;
76 fTableCache[i].table = NULL;
77 }
78
79 fTableCacheSize = newSize;
80 }
81
82 fTableCache[fTableCacheCurr].tag = tableTag;
83 fTableCache[fTableCacheCurr].table = table;
84
85 fTableCacheCurr += 1;
86}