glyphs_manager.cpp 5.0 KB
Newer Older
M
mamingshuai 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
/*
 * Copyright (c) 2020-2021 Huawei Device Co., Ltd.
 * 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.
 */

#include "font/glyphs_manager.h"
W
wangtiantian 已提交
17
#include "font/ui_font_builder.h"
L
Lizhiqi 已提交
18
#include "font/ui_font_cache_manager.h"
Z
zhangguyuan 已提交
19
#include "gfx_utils/file.h"
W
wangtiantian 已提交
20
#include "gfx_utils/graphic_log.h"
M
mamingshuai 已提交
21 22 23
#include "securec.h"

namespace OHOS {
L
Lizhiqi 已提交
24
GlyphsManager::GlyphsManager() : fileType_(0) {}
M
mamingshuai 已提交
25

26
GlyphsManager::~GlyphsManager()
M
mamingshuai 已提交
27
{
28 29 30
    for (uint16_t i = 0; i < glyphsFiles_.Size(); i++) {
        delete glyphsFiles_[i];
        glyphsFiles_[i] = nullptr;
M
mamingshuai 已提交
31
    }
32
}
M
mamingshuai 已提交
33

L
Lizhiqi 已提交
34
int8_t GlyphsManager::SetFile(const char* fontName, int32_t fp, uint32_t start, uint16_t fileType)
35 36 37 38 39
{
    for (uint16_t i = 0; i < glyphsFiles_.Size(); i++) {
        if (glyphsFiles_[i]->IsSameFile(fontName)) {
            GRAPHIC_LOGE("GlyphsManager::SetFile font name repeat");
            return INVALID_RET_VALUE;
M
mamingshuai 已提交
40 41 42
        }
    }

43 44 45 46
    GlyphsFile* instance = new GlyphsFile();
    if (instance == nullptr) {
        GRAPHIC_LOGE("GlyphsManager::SetFile new failed");
        return INVALID_RET_VALUE;
M
mamingshuai 已提交
47 48
    }

49 50 51 52
    if (instance->SetFile(fontName, fp, start) != RET_VALUE_OK) {
        delete instance;
        instance = nullptr;
        return INVALID_RET_VALUE;
M
mamingshuai 已提交
53 54
    }

55
    glyphsFiles_.PushBack(instance);
L
Lizhiqi 已提交
56
    fileType_ = fileType;
57
    return RET_VALUE_OK;
M
mamingshuai 已提交
58 59
}

60
int8_t GlyphsManager::GetFontVersion(const char* fontName, char* version, uint8_t len)
M
mamingshuai 已提交
61
{
62 63 64
    for (uint16_t i = 0; i < glyphsFiles_.Size(); i++) {
        if (glyphsFiles_[i]->GetFontVersion(fontName, version, len) == RET_VALUE_OK) {
            return RET_VALUE_OK;
M
mamingshuai 已提交
65 66
        }
    }
67
    return INVALID_RET_VALUE;
M
mamingshuai 已提交
68 69
}

L
Lizhiqi 已提交
70
const FontHeader* GlyphsManager::GetFontHeader(uint16_t fontId)
M
mamingshuai 已提交
71
{
72 73 74 75 76
    for (uint16_t i = 0; i < glyphsFiles_.Size(); i++) {
        const FontHeader* tmp = glyphsFiles_[i]->GetFontHeader(fontId);
        if (tmp != nullptr) {
            return tmp;
        }
M
mamingshuai 已提交
77 78
    }

79
    return nullptr;
M
mamingshuai 已提交
80 81
}

L
Lizhiqi 已提交
82
const GlyphNode* GlyphsManager::GetGlyphNodeFromFiles(uint32_t unicode, uint16_t fontId)
M
mamingshuai 已提交
83
{
84 85 86 87 88 89
    int8_t ret = INVALID_RET_VALUE;
    GlyphNode nodeInfo;
    for (uint16_t i = 0; i < glyphsFiles_.Size(); i++) {
        ret = glyphsFiles_[i]->GetNodeFromFile(unicode, fontId, nodeInfo);
        if (ret == RET_VALUE_OK) {
            nodeInfo.fontId = fontId;
M
mamingshuai 已提交
90 91 92 93
            break;
        }
    }

94
    if (ret != RET_VALUE_OK) {
M
mamingshuai 已提交
95 96 97
        return nullptr;
    }

L
Lizhiqi 已提交
98
    GlyphCacheNode* cacheNode = UIFontCacheManager::GetInstance()->GetNodeCacheSpace(unicode, fontId);
99 100
    if (cacheNode == nullptr) {
        return nullptr;
L
Lizhiqi 已提交
101
    }
102 103
    cacheNode->node = nodeInfo;
    cacheNode->cacheType = fileType_;
L
Lizhiqi 已提交
104 105 106 107 108 109 110 111 112 113

    return &(cacheNode->node);
}

const GlyphNode* GlyphsManager::GetGlyphNode(uint32_t unicode, uint16_t fontId)
{
    GlyphCacheNode* cacheNode =
        UIFontCacheManager::GetInstance()->GetNodeFromCache(unicode, fontId, GlyphCacheType::CACHE_TYPE_NONE);
    if (cacheNode != nullptr) {
        return &(cacheNode->node);
M
mamingshuai 已提交
114
    }
115

L
Lizhiqi 已提交
116 117 118 119 120
    const GlyphNode* node = const_cast<GlyphNode*>(GetGlyphNodeFromFiles(unicode, fontId));
    if (node != nullptr) {
        return node;
    }
    return nullptr;
M
mamingshuai 已提交
121 122
}

L
Lizhiqi 已提交
123
int16_t GlyphsManager::GetFontHeight(uint16_t fontId)
M
mamingshuai 已提交
124
{
125 126 127 128 129
    for (uint16_t i = 0; i < glyphsFiles_.Size(); i++) {
        int16_t height = glyphsFiles_[i]->GetFontHeight(fontId);
        if (height != INVALID_RET_VALUE) {
            return height;
        }
M
mamingshuai 已提交
130 131
    }

132
    return INVALID_RET_VALUE;
M
mamingshuai 已提交
133 134
}

L
Lizhiqi 已提交
135
int16_t GlyphsManager::GetFontWidth(uint32_t unicode, uint16_t fontId)
M
mamingshuai 已提交
136
{
137
    const GlyphNode* node = GetGlyphNode(unicode, fontId);
M
mamingshuai 已提交
138 139 140 141 142 143
    if (node == nullptr) {
        return INVALID_RET_VALUE;
    }
    return node->advance;
}

L
Lizhiqi 已提交
144
int8_t GlyphsManager::GetBitmap(uint32_t unicode, BufferInfo& bufInfo, uint16_t fontId)
M
mamingshuai 已提交
145
{
L
Lizhiqi 已提交
146 147 148 149 150 151 152 153 154 155 156
    GlyphCacheNode* cacheNode = UIFontCacheManager::GetInstance()->GetNodeFromCache(unicode, fontId, fileType_);
    if (cacheNode != nullptr && cacheNode->cacheType == fileType_) {
        for (uint16_t i = 0; i < glyphsFiles_.Size(); i++) {
            int8_t ret = glyphsFiles_[i]->GetBitmap(cacheNode->node, bufInfo);
            if (ret == RET_VALUE_OK) {
                return RET_VALUE_OK;
            }
        }
    }

    GlyphNode* node = const_cast<GlyphNode*>(GetGlyphNodeFromFiles(unicode, fontId));
M
mamingshuai 已提交
157
    if (node == nullptr) {
158
        GRAPHIC_LOGE("GlyphsManager::GetBitmap node not found");
M
mamingshuai 已提交
159 160 161
        return INVALID_RET_VALUE;
    }

162
    for (uint16_t i = 0; i < glyphsFiles_.Size(); i++) {
163
        int8_t ret = glyphsFiles_[i]->GetBitmap(*node, bufInfo);
164 165 166
        if (ret == RET_VALUE_OK) {
            return RET_VALUE_OK;
        }
M
mamingshuai 已提交
167
    }
168
    return INVALID_RET_VALUE;
M
mamingshuai 已提交
169 170
}
} // namespace OHOS