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