glyphs_manager.cpp 12.1 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"
Z
zhangguyuan 已提交
18
#include "gfx_utils/file.h"
W
wangtiantian 已提交
19
#include "gfx_utils/graphic_log.h"
M
mamingshuai 已提交
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47
#include "securec.h"

namespace OHOS {
GlyphsManager::GlyphsManager()
    : binHeader_{{0}},
      fontNum_(0),
      start_(0),
      fontHeaderSectionStart_(0),
      fontIndexSectionStart_(0),
      curFontIndexSectionStart_(0),
      glyphNodeSectionStart_(0),
      curGlyphNodeSectionStart_(0),
      bitMapSectionStart_(0),
      curBitMapSectionStart_(0),
      ramAddr_(nullptr),
      ramUsedLen_(0),
      fontHeaderCache_(nullptr),
      indexCache_(nullptr),
      curIndexCache_(nullptr),
      nodeCache_(nullptr),
      cacheStatus_(nullptr),
      fp_(-1),
      curFontHeader_(nullptr),
      curGlyphNode_(nullptr),
      isRamSet_(false),
      isFileSet_(false),
      isFontIdSet_(false)
{
W
wangtiantian 已提交
48
    fontId_ = UIFontBuilder::GetInstance()->GetBitmapFontIdMax();
M
mamingshuai 已提交
49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65
}
GlyphsManager::~GlyphsManager() {}

int8_t GlyphsManager::GlyphNodeCacheInit()
{
    uint32_t size = 0;
    for (int32_t i = 0; i < fontNum_; i++) {
        size += fontHeaderCache_[i].indexLen;
    }

    indexCache_ = ramAddr_ + ramUsedLen_;
    ramUsedLen_ += size;
    /* align up to 4 byte, power of 2 */
    ramUsedLen_ = AlignUp(ramUsedLen_, 2);

    int32_t ret = read(fp_, indexCache_, size);
    if (ret != static_cast<int32_t>(size)) {
W
wangtiantian 已提交
66
        GRAPHIC_LOGE("GlyphsManager::GlyphNodeCacheInit read failed");
M
mamingshuai 已提交
67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141
        return INVALID_RET_VALUE;
    }

    cacheStatus_ = reinterpret_cast<CacheState*>(ramAddr_ + ramUsedLen_);
    ramUsedLen_ += sizeof(CacheState);
    /* align up to 4 byte, power of 2 */
    ramUsedLen_ = AlignUp(ramUsedLen_, 2);
    for (int32_t font = 0; font < FONT_HASH_NR; font++) {
        for (int32_t uc = 0; uc < UNICODE_HASH_NR; uc++) {
            (*cacheStatus_)[font][uc] = 0;
        }
    }

    nodeCache_ = reinterpret_cast<CacheType*>(ramAddr_ + ramUsedLen_);
    ramUsedLen_ += sizeof(CacheType);
    /* align up to 4 byte, power of 2 */
    ramUsedLen_ = AlignUp(ramUsedLen_, 2);
    for (int32_t font = 0; font < FONT_HASH_NR; font++) {
        for (int32_t uc = 0; uc < UNICODE_HASH_NR; uc++) {
            for (int32_t node = 0; node < NODE_HASH_NR; node++) {
                (*nodeCache_)[font][uc][node].unicode = 0;
            }
        }
    }

    return RET_VALUE_OK;
}

GlyphNode* GlyphsManager::GetNodeFromCache(uint32_t unicode)
{
    GlyphNode* node = nullptr;

    uint8_t font = fontId_ & FONT_HASH_MASK;
    uint8_t uc = unicode & UNICODE_HASH_MASK;
    for (uint8_t i = 0; i < NODE_HASH_NR; i++) {
        GlyphNode* p = &((*nodeCache_)[font][uc][i]);
        if ((p->unicode == unicode) && (p->reserve == fontId_)) {
            node = p;
            break;
        }
    }
    return node;
}

GlyphNode* GlyphsManager::GetNodeCacheSpace(uint32_t unicode)
{
    uint8_t font, uc, i;
    GlyphNode* node = nullptr;

    font = fontId_ & FONT_HASH_MASK;
    uc = unicode & UNICODE_HASH_MASK;
    i = (*cacheStatus_)[font][uc];
    node = &((*nodeCache_)[font][uc][i]);

    i++;
    if (i >= NODE_HASH_NR) {
        i = 0;
    }
    (*cacheStatus_)[font][uc] = i;

    return node;
}

GlyphNode* GlyphsManager::GetNodeFromFile(uint32_t unicode)
{
    uint16_t idx = 0;
    uint8_t key;
    uint32_t offset;

    for (int32_t i = RADIX_SHIFT_START; i >= 0; i -= RADIX_TREE_BITS) {
        offset = idx * sizeof(IndexNode);
        key = static_cast<uint8_t>((unicode >> static_cast<uint8_t>(i)) & RADIX_TREE_MASK);
        offset += key * sizeof(uint16_t);
        idx = *(reinterpret_cast<uint16_t*>(curIndexCache_ + offset));
        if (idx == 0) {
W
wangtiantian 已提交
142
            GRAPHIC_LOGE("GlyphsManager::GetNodeFromFile unicode not found");
M
mamingshuai 已提交
143 144 145 146 147 148 149
            return nullptr;
        }
    }

    offset = curGlyphNodeSectionStart_ + (idx - 1) * sizeof(GlyphNode);
    int32_t ret = lseek(fp_, offset, SEEK_SET);
    if (ret != static_cast<int32_t>(offset)) {
W
wangtiantian 已提交
150
        GRAPHIC_LOGE("GlyphsManager::GetNodeFromFile lseek failed");
M
mamingshuai 已提交
151 152 153 154 155
        return nullptr;
    }
    GlyphNode* node = GetNodeCacheSpace(unicode);
    ret = read(fp_, node, sizeof(GlyphNode));
    if (ret < 0) {
W
wangtiantian 已提交
156
        GRAPHIC_LOGE("GlyphsManager::GetNodeFromFile read failed");
M
mamingshuai 已提交
157 158 159 160 161 162 163 164 165 166 167 168 169 170 171
        return nullptr;
    }

    return node;
}

void GlyphsManager::SetRamBuffer(uintptr_t ramAddr)
{
    ramAddr_ = reinterpret_cast<uint8_t*>(ramAddr);
    isRamSet_ = true;
}

int8_t GlyphsManager::SetFile(int32_t fp, uint32_t start)
{
    if (!isRamSet_) {
W
wangtiantian 已提交
172
        GRAPHIC_LOGE("GlyphsManager::SetFile Ram not set");
M
mamingshuai 已提交
173 174 175 176 177 178 179
        return INVALID_RET_VALUE;
    }

    fp_ = fp;
    start_ = start;
    int32_t ret = lseek(fp_, start_, SEEK_SET);
    if (ret < 0) {
W
wangtiantian 已提交
180
        GRAPHIC_LOGE("GlyphsManager::SetFile lseek failed");
M
mamingshuai 已提交
181 182 183 184
        return INVALID_RET_VALUE;
    }
    ret = read(fp_, &binHeader_, sizeof(binHeader_));
    if (ret != sizeof(binHeader_)) {
W
wangtiantian 已提交
185
        GRAPHIC_LOGE("GlyphsManager::SetFile read failed");
M
mamingshuai 已提交
186 187 188 189 190
        return INVALID_RET_VALUE;
    }
    if (strncmp(binHeader_.fontMagic, FONT_MAGIC_NUMBER, FONT_MAGIC_NUM_LEN) != 0) {
        return INVALID_RET_VALUE;
    }
W
wangtiantian 已提交
191
    if (binHeader_.fontNum > UIFontBuilder::GetInstance()->GetBitmapFontIdMax()) {
W
wangtiantian 已提交
192
        GRAPHIC_LOGE("GlyphsManager::SetFile data error, fontNum need less than max fontId");
M
mamingshuai 已提交
193 194 195 196 197 198 199 200 201 202 203 204 205 206
        return INVALID_RET_VALUE;
    }

    fontNum_ = binHeader_.fontNum;
    fontHeaderSectionStart_ = start_ + sizeof(binHeader_);
    int32_t size = sizeof(FontHeader) * fontNum_;
    fontIndexSectionStart_ = fontHeaderSectionStart_ + size;

    fontHeaderCache_ = reinterpret_cast<FontHeader*>(ramAddr_);
    /* align up to 4 byte, power of 2 */
    ramUsedLen_ = AlignUp(size, 2);

    ret = read(fp_, fontHeaderCache_, size);
    if (ret != size) {
W
wangtiantian 已提交
207
        GRAPHIC_LOGE("GlyphsManager::SetFile read failed");
M
mamingshuai 已提交
208 209 210 211 212 213 214 215 216 217 218 219 220 221
        return INVALID_RET_VALUE;
    }

    FontHeader* last = fontHeaderCache_ + fontNum_ - 1;
    size = last->indexOffset + last->indexLen;
    glyphNodeSectionStart_ = fontIndexSectionStart_ + size;

    size = 0;
    for (uint32_t i = 0; i < fontNum_; i++) {
        size += fontHeaderCache_[i].glyphNum * sizeof(GlyphNode);
    }
    bitMapSectionStart_ = glyphNodeSectionStart_ + size;
    ret = GlyphNodeCacheInit();
    if (ret == RET_VALUE_OK) {
W
wangtiantian 已提交
222
        GRAPHIC_LOGE("GlyphsManager::SetFile GlyphNodeCacheInit failed");
M
mamingshuai 已提交
223 224 225
        isFileSet_ = true;
    }

W
wangtiantian 已提交
226
    fontId_ = UIFontBuilder::GetInstance()->GetBitmapFontIdMax();
M
mamingshuai 已提交
227 228 229 230 231 232 233
    return ret;
}

int8_t GlyphsManager::SetCurrentFontId(uint8_t fontId)
{
    uint16_t fontIdx = 0;
    if (!isFileSet_) {
W
wangtiantian 已提交
234
        GRAPHIC_LOGE("GlyphsManager::SetCurrentFontId file not set");
M
mamingshuai 已提交
235 236
        return INVALID_RET_VALUE;
    }
W
wangtiantian 已提交
237
    if (fontId > UIFontBuilder::GetInstance()->GetBitmapFontIdMax()) {
W
wangtiantian 已提交
238
        GRAPHIC_LOGE("GlyphsManager::SetCurrentFontId fontId need less than max fontId");
M
mamingshuai 已提交
239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263
        return INVALID_RET_VALUE;
    }
    if (fontId_ == fontId) {
        return RET_VALUE_OK;
    }

    int32_t low = 0;
    int32_t high = binHeader_.fontNum - 1;
    bool found = false;

    while (low <= high) {
        int32_t mid = (low + high) / 2; // 2 means half
        if (fontHeaderCache_[mid].fontId == fontId) {
            fontIdx = mid;
            found = true;
            break;
        } else if (fontHeaderCache_[mid].fontId > fontId) {
            high = mid - 1;
        } else if (fontHeaderCache_[mid].fontId < fontId) {
            low = mid + 1;
        }
    }
    if (!found) {
        isFontIdSet_ = false;
        curFontHeader_ = nullptr;
W
wangtiantian 已提交
264
        fontId_ = UIFontBuilder::GetInstance()->GetBitmapFontIdMax();
W
wangtiantian 已提交
265
        GRAPHIC_LOGE("GlyphsManager::SetCurrentFontId fontId not found");
M
mamingshuai 已提交
266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287
        return INVALID_RET_VALUE;
    }

    uint32_t size = 0;
    fontId_ = fontId;
    curFontHeader_ = fontHeaderCache_ + fontIdx;
    curGlyphNode_ = nullptr;
    curFontIndexSectionStart_ = fontIndexSectionStart_ + curFontHeader_->indexOffset;
    for (uint32_t i = 0; i < fontIdx; i++) {
        size += fontHeaderCache_[i].glyphNum * sizeof(GlyphNode);
    }
    curGlyphNodeSectionStart_ = glyphNodeSectionStart_ + size;
    curBitMapSectionStart_ = bitMapSectionStart_ + curFontHeader_->glyphOffset;
    curIndexCache_ = indexCache_ + curFontHeader_->indexOffset;
    isFontIdSet_ = true;

    return RET_VALUE_OK;
}

int32_t GlyphsManager::GetRamUsedLen() const
{
    if (!isFileSet_) {
W
wangtiantian 已提交
288
        GRAPHIC_LOGE("GlyphsManager::GetRamUsedLen file not set");
M
mamingshuai 已提交
289 290 291 292 293 294 295 296
        return INVALID_RET_VALUE;
    }
    return ramUsedLen_;
}

int8_t GlyphsManager::GetFontVersion(char* version, uint8_t len) const
{
    if (!isFileSet_ || (version == nullptr) || (len > FONT_VERSION_LEN)) {
W
wangtiantian 已提交
297
        GRAPHIC_LOGE("GlyphsManager::GetFontVersion invalid parameters");
M
mamingshuai 已提交
298 299 300
        return INVALID_RET_VALUE;
    }
    if (memset_s(version, len, 0, len) != EOK) {
W
wangtiantian 已提交
301
        GRAPHIC_LOGE("GlyphsManager::GetFontVersion memset_s failed");
M
mamingshuai 已提交
302 303 304
        return INVALID_RET_VALUE;
    }
    if (strcpy_s(version, len, binHeader_.fontVersion) != EOK) {
W
wangtiantian 已提交
305
        GRAPHIC_LOGE("GlyphsManager::GetFontVersion strcpy_s failed");
M
mamingshuai 已提交
306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349
        return INVALID_RET_VALUE;
    }
    return RET_VALUE_OK;
}

const FontHeader* GlyphsManager::GetCurrentFontHeader() const
{
    if (!isFontIdSet_) {
        return nullptr;
    }

    if (curFontHeader_ == nullptr) {
        return nullptr;
    }

    return curFontHeader_;
}

const GlyphNode* GlyphsManager::GetGlyphNode(uint32_t unicode)
{
    if (!isFontIdSet_) {
        return nullptr;
    }

    if (curGlyphNode_ != nullptr) {
        if ((curGlyphNode_->unicode == unicode) && (curGlyphNode_->reserve == fontId_)) {
            return curGlyphNode_;
        }
    }
    GlyphNode* node = GetNodeFromCache(unicode);
    if (node == nullptr) {
        node = GetNodeFromFile(unicode);
        if (node != nullptr) {
            node->reserve = fontId_;
        }
    }

    curGlyphNode_ = node;
    return node;
}

int16_t GlyphsManager::GetFontHeight() const
{
    if (!isFontIdSet_) {
W
wangtiantian 已提交
350
        GRAPHIC_LOGE("GlyphsManager::GetFontHeight fontId not set");
M
mamingshuai 已提交
351 352 353 354
        return INVALID_RET_VALUE;
    }

    if (curFontHeader_ == nullptr) {
W
wangtiantian 已提交
355
        GRAPHIC_LOGE("GlyphsManager::GetFontHeight curFontHeader is nullptr");
M
mamingshuai 已提交
356 357 358 359 360 361 362 363 364 365 366
        return INVALID_RET_VALUE;
    }

    return curFontHeader_->fontHeight;
}

int16_t GlyphsManager::GetFontWidth(uint32_t unicode)
{
    const GlyphNode* node = nullptr;

    if (!isFontIdSet_) {
W
wangtiantian 已提交
367
        GRAPHIC_LOGE("GlyphsManager::GetFontWidth fontId not set");
M
mamingshuai 已提交
368 369 370 371
        return INVALID_RET_VALUE;
    }
    node = GetGlyphNode(unicode);
    if (node == nullptr) {
W
wangtiantian 已提交
372
        GRAPHIC_LOGE("GlyphsManager::GetFontWidth node not found");
M
mamingshuai 已提交
373 374 375 376 377 378 379 380
        return INVALID_RET_VALUE;
    }
    return node->advance;
}

int8_t GlyphsManager::GetBitmap(uint32_t unicode, uint8_t* bitmap)
{
    if (bitmap == nullptr) {
W
wangtiantian 已提交
381
        GRAPHIC_LOGE("GlyphsManager::GetBitmap invalid parameter");
M
mamingshuai 已提交
382 383 384
        return INVALID_RET_VALUE;
    }
    if (!isFontIdSet_) {
W
wangtiantian 已提交
385
        GRAPHIC_LOGE("GlyphsManager::GetBitmap fontId not set");
M
mamingshuai 已提交
386 387 388 389 390
        return INVALID_RET_VALUE;
    }

    const GlyphNode* node = GetGlyphNode(unicode);
    if (node == nullptr) {
W
wangtiantian 已提交
391
        GRAPHIC_LOGE("GlyphsManager::GetBitmap node not found");
M
mamingshuai 已提交
392 393 394 395 396 397 398
        return INVALID_RET_VALUE;
    }

    uint32_t offset = curBitMapSectionStart_ + node->dataOff;
    uint32_t size = node->kernOff - node->dataOff;
    int32_t ret = lseek(fp_, offset, SEEK_SET);
    if (ret != static_cast<int32_t>(offset)) {
W
wangtiantian 已提交
399
        GRAPHIC_LOGE("GlyphsManager::GetBitmap lseek failed");
M
mamingshuai 已提交
400 401 402 403 404
        return INVALID_RET_VALUE;
    }

    int32_t readSize = read(fp_, bitmap, size);
    if (readSize != static_cast<int32_t>(size)) {
W
wangtiantian 已提交
405
        GRAPHIC_LOGE("GlyphsManager::GetBitmap read failed");
M
mamingshuai 已提交
406 407 408 409 410 411
        return INVALID_RET_VALUE;
    }

    return RET_VALUE_OK;
}
} // namespace OHOS