]>
git.saurik.com Git - apple/icu.git/blob - icuSources/test/letest/FontTableCache.cpp
1 // © 2016 and later: Unicode, Inc. and others.
2 // License & terms of use: http://www.unicode.org/copyright.html
4 **********************************************************************
5 * Copyright (C) 2003-2013, International Business Machines
6 * Corporation and others. All Rights Reserved.
7 **********************************************************************
10 #include "layout/LETypes.h"
13 #include "FontTableCache.h"
15 #define TABLE_CACHE_INIT 5
16 #define TABLE_CACHE_GROW 5
18 struct FontTableCacheEntry
25 FontTableCache::FontTableCache()
26 : fTableCacheCurr(0), fTableCacheSize(TABLE_CACHE_INIT
)
28 fTableCache
= LE_NEW_ARRAY(FontTableCacheEntry
, fTableCacheSize
);
30 if (fTableCache
== NULL
) {
35 for (int i
= 0; i
< fTableCacheSize
; i
+= 1) {
36 fTableCache
[i
].tag
= 0;
37 fTableCache
[i
].table
= NULL
;
38 fTableCache
[i
].length
= 0;
42 FontTableCache::~FontTableCache()
44 for (int i
= fTableCacheCurr
- 1; i
>= 0; i
-= 1) {
45 LE_DELETE_ARRAY(fTableCache
[i
].table
);
47 fTableCache
[i
].tag
= 0;
48 fTableCache
[i
].table
= NULL
;
49 fTableCache
[i
].length
= 0;
54 LE_DELETE_ARRAY(fTableCache
);
57 void FontTableCache::freeFontTable(const void *table
) const
59 LE_DELETE_ARRAY(table
);
62 const void *FontTableCache::find(LETag tableTag
, size_t &length
) const
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
;
71 const void *table
= readFontTable(tableTag
, length
);
73 ((FontTableCache
*) this)->add(tableTag
, table
, length
);
78 void FontTableCache::add(LETag tableTag
, const void *table
, size_t length
)
80 if (fTableCacheCurr
>= fTableCacheSize
) {
81 le_int32 newSize
= fTableCacheSize
+ TABLE_CACHE_GROW
;
83 fTableCache
= (FontTableCacheEntry
*) LE_GROW_ARRAY(fTableCache
, newSize
);
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;
91 fTableCacheSize
= newSize
;
94 fTableCache
[fTableCacheCurr
].tag
= tableTag
;
95 fTableCache
[fTableCacheCurr
].table
= table
;
96 fTableCache
[fTableCacheCurr
].length
= length
;