1ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru/*
2ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru **********************************************************************
385bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho *   Copyright (C) 2003 - 2008, International Business Machines
4ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru *   Corporation and others.  All Rights Reserved.
5ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru **********************************************************************
6ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru */
7ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru
8ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru#include "layout/LETypes.h"
9ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru
10ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru#include "FontTableCache.h"
11ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru
12ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru#define TABLE_CACHE_INIT 5
13ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru#define TABLE_CACHE_GROW 5
14ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru
15ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Querustruct FontTableCacheEntry
16ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru{
17ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    LETag tag;
18ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    const void *table;
19ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru};
20ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru
21ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste QueruFontTableCache::FontTableCache()
22ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    : fTableCacheCurr(0), fTableCacheSize(TABLE_CACHE_INIT)
23ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru{
24ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    fTableCache = LE_NEW_ARRAY(FontTableCacheEntry, fTableCacheSize);
25ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru
26ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    if (fTableCache == NULL) {
27ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru        fTableCacheSize = 0;
28ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru        return;
29ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    }
30ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru
31ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    for (int i = 0; i < fTableCacheSize; i += 1) {
32ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru        fTableCache[i].tag   = 0;
33ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru        fTableCache[i].table = NULL;
34ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    }
35ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru}
36ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru
37ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste QueruFontTableCache::~FontTableCache()
38ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru{
39ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    for (int i = fTableCacheCurr - 1; i >= 0; i -= 1) {
4085bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho        freeFontTable(fTableCache[i].table);
41ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru
42ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru        fTableCache[i].tag   = 0;
43ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru        fTableCache[i].table = NULL;
44ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    }
45ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru
46ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    fTableCacheCurr = 0;
47ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru
48ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    LE_DELETE_ARRAY(fTableCache);
49ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    fTableCache = NULL;
50ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru}
51ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru
5285bf2e2fbc60a9f938064abc8127d61da7d19882Claire Hovoid FontTableCache::freeFontTable(const void *table) const
5385bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho{
5485bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho    LE_DELETE_ARRAY(table);
5585bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho}
5685bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho
57ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queruconst void *FontTableCache::find(LETag tableTag) const
58ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru{
59ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    for (int i = 0; i < fTableCacheCurr; i += 1) {
60ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru        if (fTableCache[i].tag == tableTag) {
61ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru            return fTableCache[i].table;
62ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru        }
63ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    }
64ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru
65ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    const void *table = readFontTable(tableTag);
66ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru
67ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    ((FontTableCache *) this)->add(tableTag, table);
68ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru
69ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    return table;
70ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru}
71ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru
72ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queruvoid FontTableCache::add(LETag tableTag, const void *table)
73ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru{
74ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    if (fTableCacheCurr >= fTableCacheSize) {
75ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru        le_int32 newSize = fTableCacheSize + TABLE_CACHE_GROW;
76ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru
77ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru        fTableCache = (FontTableCacheEntry *) LE_GROW_ARRAY(fTableCache, newSize);
78ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru
79ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru        for (le_int32 i = fTableCacheSize; i < newSize; i += 1) {
80ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru            fTableCache[i].tag   = 0;
81ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru            fTableCache[i].table = NULL;
82ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru        }
83ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru
84ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru        fTableCacheSize = newSize;
85ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    }
86ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru
87ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    fTableCache[fTableCacheCurr].tag   = tableTag;
88ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    fTableCache[fTableCacheCurr].table = table;
89ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru
90ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    fTableCacheCurr += 1;
91ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru}
92