12e5b6d6dSopenharmony_ci/*
22e5b6d6dSopenharmony_ci *************************************************************************
32e5b6d6dSopenharmony_ci *   © 2016 and later: Unicode, Inc. and others.
42e5b6d6dSopenharmony_ci *   License & terms of use: http://www.unicode.org/copyright.html
52e5b6d6dSopenharmony_ci *************************************************************************
62e5b6d6dSopenharmony_ci *************************************************************************
72e5b6d6dSopenharmony_ci *   Copyright (C) 2003 - 2008, International Business Machines
82e5b6d6dSopenharmony_ci *   Corporation and others.  All Rights Reserved.
92e5b6d6dSopenharmony_ci *************************************************************************
102e5b6d6dSopenharmony_ci */
112e5b6d6dSopenharmony_ci
122e5b6d6dSopenharmony_ci#include "layout/LETypes.h"
132e5b6d6dSopenharmony_ci
142e5b6d6dSopenharmony_ci#include "FontTableCache.h"
152e5b6d6dSopenharmony_ci
162e5b6d6dSopenharmony_ci#define TABLE_CACHE_INIT 5
172e5b6d6dSopenharmony_ci#define TABLE_CACHE_GROW 5
182e5b6d6dSopenharmony_ci
192e5b6d6dSopenharmony_cistruct FontTableCacheEntry
202e5b6d6dSopenharmony_ci{
212e5b6d6dSopenharmony_ci    LETag tag;
222e5b6d6dSopenharmony_ci    const void *table;
232e5b6d6dSopenharmony_ci};
242e5b6d6dSopenharmony_ci
252e5b6d6dSopenharmony_ciFontTableCache::FontTableCache()
262e5b6d6dSopenharmony_ci    : fTableCacheCurr(0), fTableCacheSize(TABLE_CACHE_INIT)
272e5b6d6dSopenharmony_ci{
282e5b6d6dSopenharmony_ci    fTableCache = LE_NEW_ARRAY(FontTableCacheEntry, fTableCacheSize);
292e5b6d6dSopenharmony_ci
302e5b6d6dSopenharmony_ci    if (fTableCache == NULL) {
312e5b6d6dSopenharmony_ci        fTableCacheSize = 0;
322e5b6d6dSopenharmony_ci        return;
332e5b6d6dSopenharmony_ci    }
342e5b6d6dSopenharmony_ci
352e5b6d6dSopenharmony_ci    for (int i = 0; i < fTableCacheSize; i += 1) {
362e5b6d6dSopenharmony_ci        fTableCache[i].tag   = 0;
372e5b6d6dSopenharmony_ci        fTableCache[i].table = NULL;
382e5b6d6dSopenharmony_ci    }
392e5b6d6dSopenharmony_ci}
402e5b6d6dSopenharmony_ci
412e5b6d6dSopenharmony_ciFontTableCache::~FontTableCache()
422e5b6d6dSopenharmony_ci{
432e5b6d6dSopenharmony_ci    for (int i = fTableCacheCurr - 1; i >= 0; i -= 1) {
442e5b6d6dSopenharmony_ci        freeFontTable(fTableCache[i].table);
452e5b6d6dSopenharmony_ci
462e5b6d6dSopenharmony_ci        fTableCache[i].tag   = 0;
472e5b6d6dSopenharmony_ci        fTableCache[i].table = NULL;
482e5b6d6dSopenharmony_ci    }
492e5b6d6dSopenharmony_ci
502e5b6d6dSopenharmony_ci    fTableCacheCurr = 0;
512e5b6d6dSopenharmony_ci
522e5b6d6dSopenharmony_ci    LE_DELETE_ARRAY(fTableCache);
532e5b6d6dSopenharmony_ci    fTableCache = NULL;
542e5b6d6dSopenharmony_ci}
552e5b6d6dSopenharmony_ci
562e5b6d6dSopenharmony_civoid FontTableCache::freeFontTable(const void *table) const
572e5b6d6dSopenharmony_ci{
582e5b6d6dSopenharmony_ci    LE_DELETE_ARRAY(table);
592e5b6d6dSopenharmony_ci}
602e5b6d6dSopenharmony_ci
612e5b6d6dSopenharmony_ciconst void *FontTableCache::find(LETag tableTag) const
622e5b6d6dSopenharmony_ci{
632e5b6d6dSopenharmony_ci    for (int i = 0; i < fTableCacheCurr; i += 1) {
642e5b6d6dSopenharmony_ci        if (fTableCache[i].tag == tableTag) {
652e5b6d6dSopenharmony_ci            return fTableCache[i].table;
662e5b6d6dSopenharmony_ci        }
672e5b6d6dSopenharmony_ci    }
682e5b6d6dSopenharmony_ci
692e5b6d6dSopenharmony_ci    const void *table = readFontTable(tableTag);
702e5b6d6dSopenharmony_ci
712e5b6d6dSopenharmony_ci    ((FontTableCache *) this)->add(tableTag, table);
722e5b6d6dSopenharmony_ci
732e5b6d6dSopenharmony_ci    return table;
742e5b6d6dSopenharmony_ci}
752e5b6d6dSopenharmony_ci
762e5b6d6dSopenharmony_civoid FontTableCache::add(LETag tableTag, const void *table)
772e5b6d6dSopenharmony_ci{
782e5b6d6dSopenharmony_ci    if (fTableCacheCurr >= fTableCacheSize) {
792e5b6d6dSopenharmony_ci        le_int32 newSize = fTableCacheSize + TABLE_CACHE_GROW;
802e5b6d6dSopenharmony_ci
812e5b6d6dSopenharmony_ci        fTableCache = (FontTableCacheEntry *) LE_GROW_ARRAY(fTableCache, newSize);
822e5b6d6dSopenharmony_ci
832e5b6d6dSopenharmony_ci        for (le_int32 i = fTableCacheSize; i < newSize; i += 1) {
842e5b6d6dSopenharmony_ci            fTableCache[i].tag   = 0;
852e5b6d6dSopenharmony_ci            fTableCache[i].table = NULL;
862e5b6d6dSopenharmony_ci        }
872e5b6d6dSopenharmony_ci
882e5b6d6dSopenharmony_ci        fTableCacheSize = newSize;
892e5b6d6dSopenharmony_ci    }
902e5b6d6dSopenharmony_ci
912e5b6d6dSopenharmony_ci    fTableCache[fTableCacheCurr].tag   = tableTag;
922e5b6d6dSopenharmony_ci    fTableCache[fTableCacheCurr].table = table;
932e5b6d6dSopenharmony_ci
942e5b6d6dSopenharmony_ci    fTableCacheCurr += 1;
952e5b6d6dSopenharmony_ci}
96