HbFontCache.cpp 4.1 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
/*
 * Copyright (C) 2015 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#define LOG_TAG "Minikin"

19
#include "HbFontCache.h"
20

21 22 23
#include <android/log.h>
#include <utils/LruCache.h>

24
#include <hb.h>
25
#include <hb-ot.h>
26 27 28 29 30 31

#include <minikin/MinikinFont.h>
#include "MinikinInternal.h"

namespace android {

32
static hb_blob_t* referenceTable(hb_face_t* /* face */, hb_tag_t tag, void* userData) {
33
    MinikinFont* font = reinterpret_cast<MinikinFont*>(userData);
R
Raph Levien 已提交
34 35 36 37 38
    MinikinDestroyFunc destroy = 0;
    size_t size = 0;
    const void* buffer = font->GetTable(tag, &size, &destroy);
    if (buffer == nullptr) {
        return nullptr;
39 40
    }
#ifdef VERBOSE_DEBUG
R
Raph Levien 已提交
41 42
    ALOGD("referenceTable %c%c%c%c length=%zd",
        (tag >>24)&0xff, (tag>>16)&0xff, (tag>>8)&0xff, tag&0xff, size);
43
#endif
R
Raph Levien 已提交
44 45
    return hb_blob_create(reinterpret_cast<const char*>(buffer), size,
            HB_MEMORY_MODE_READONLY, const_cast<void*>(buffer), destroy);
46 47
}

48
class HbFontCache : private OnEntryRemoved<int32_t, hb_font_t*> {
49
public:
50
    HbFontCache() : mCache(kMaxEntries) {
51 52 53 54
        mCache.setOnEntryRemovedListener(this);
    }

    // callback for OnEntryRemoved
55 56
    void operator()(int32_t& /* key */, hb_font_t*& value) {
        hb_font_destroy(value);
57 58
    }

59
    hb_font_t* get(int32_t fontId) {
60 61 62
        return mCache.get(fontId);
    }

63 64
    void put(int32_t fontId, hb_font_t* font) {
        mCache.put(fontId, font);
65 66 67 68 69 70
    }

    void clear() {
        mCache.clear();
    }

71 72 73 74
    void remove(int32_t fontId) {
        mCache.remove(fontId);
    }

75 76 77
private:
    static const size_t kMaxEntries = 100;

78
    LruCache<int32_t, hb_font_t*> mCache;
79 80
};

81
HbFontCache* getFontCacheLocked() {
82
    assertMinikinLocked();
83
    static HbFontCache* cache = nullptr;
84
    if (cache == nullptr) {
85
        cache = new HbFontCache();
86 87 88 89
    }
    return cache;
}

90
void purgeHbFontCacheLocked() {
91
    assertMinikinLocked();
92
    getFontCacheLocked()->clear();
93 94
}

S
Seigo Nonaka 已提交
95 96
void purgeHbFontLocked(const MinikinFont* minikinFont) {
    assertMinikinLocked();
97 98 99 100
    const int32_t fontId = minikinFont->GetUniqueId();
    getFontCacheLocked()->remove(fontId);
}

R
Raph Levien 已提交
101 102
// Returns a new reference to a hb_font_t object, caller is
// responsible for calling hb_font_destroy() on it.
103
hb_font_t* getHbFontLocked(MinikinFont* minikinFont) {
104
    assertMinikinLocked();
R
Raph Levien 已提交
105
    // TODO: get rid of nullFaceFont
106
    static hb_font_t* nullFaceFont = nullptr;
107
    if (minikinFont == nullptr) {
108 109 110
        if (nullFaceFont == nullptr) {
            nullFaceFont = hb_font_create(nullptr);
        }
R
Raph Levien 已提交
111
        return hb_font_reference(nullFaceFont);
112 113
    }

114
    HbFontCache* fontCache = getFontCacheLocked();
115
    const int32_t fontId = minikinFont->GetUniqueId();
116 117
    hb_font_t* font = fontCache->get(fontId);
    if (font != nullptr) {
R
Raph Levien 已提交
118
        return hb_font_reference(font);
119 120
    }

R
Raph Levien 已提交
121 122 123 124 125 126 127 128 129 130 131
    hb_face_t* face;
    const void* buf = minikinFont->GetFontData();
    if (buf == nullptr) {
        face = hb_face_create_for_tables(referenceTable, minikinFont, nullptr);
    } else {
        size_t size = minikinFont->GetFontSize();
        hb_blob_t* blob = hb_blob_create(reinterpret_cast<const char*>(buf), size,
            HB_MEMORY_MODE_READONLY, nullptr, nullptr);
        face = hb_face_create(blob, minikinFont->GetFontIndex());
        hb_blob_destroy(blob);
    }
132 133 134 135 136 137 138 139 140 141
    hb_font_t* parent_font = hb_font_create(face);
    hb_ot_font_set_funcs(parent_font);

    unsigned int upem = hb_face_get_upem(face);
    hb_font_set_scale(parent_font, upem, upem);

    font = hb_font_create_sub_font(parent_font);
    hb_font_destroy(parent_font);
    hb_face_destroy(face);
    fontCache->put(fontId, font);
R
Raph Levien 已提交
142
    return hb_font_reference(font);
143 144 145
}

}  // namespace android