glyphs_manager.cpp 10.2 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"
M
mamingshuai 已提交
19 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
#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 已提交
47
    fontId_ = UIFontBuilder::GetInstance()->GetBitmapFontIdMax();
M
mamingshuai 已提交
48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 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 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182
}
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)) {
        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) {
            return nullptr;
        }
    }

    offset = curGlyphNodeSectionStart_ + (idx - 1) * sizeof(GlyphNode);
    int32_t ret = lseek(fp_, offset, SEEK_SET);
    if (ret != static_cast<int32_t>(offset)) {
        return nullptr;
    }
    GlyphNode* node = GetNodeCacheSpace(unicode);
    ret = read(fp_, node, sizeof(GlyphNode));
    if (ret < 0) {
        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_) {
        return INVALID_RET_VALUE;
    }

    fp_ = fp;
    start_ = start;
    int32_t ret = lseek(fp_, start_, SEEK_SET);
    if (ret < 0) {
        return INVALID_RET_VALUE;
    }
    ret = read(fp_, &binHeader_, sizeof(binHeader_));
    if (ret != sizeof(binHeader_)) {
        return INVALID_RET_VALUE;
    }
    if (strncmp(binHeader_.fontMagic, FONT_MAGIC_NUMBER, FONT_MAGIC_NUM_LEN) != 0) {
        return INVALID_RET_VALUE;
    }
W
wangtiantian 已提交
183
    if (binHeader_.fontNum > UIFontBuilder::GetInstance()->GetBitmapFontIdMax()) {
M
mamingshuai 已提交
184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214
        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) {
        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) {
        isFileSet_ = true;
    }

W
wangtiantian 已提交
215
    fontId_ = UIFontBuilder::GetInstance()->GetBitmapFontIdMax();
M
mamingshuai 已提交
216 217 218 219 220 221 222 223 224
    return ret;
}

int8_t GlyphsManager::SetCurrentFontId(uint8_t fontId)
{
    uint16_t fontIdx = 0;
    if (!isFileSet_) {
        return INVALID_RET_VALUE;
    }
W
wangtiantian 已提交
225
    if (fontId > UIFontBuilder::GetInstance()->GetBitmapFontIdMax()) {
M
mamingshuai 已提交
226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250
        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 已提交
251
        fontId_ = UIFontBuilder::GetInstance()->GetBitmapFontIdMax();
M
mamingshuai 已提交
252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 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 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384
        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_) {
        return INVALID_RET_VALUE;
    }
    return ramUsedLen_;
}

int8_t GlyphsManager::GetFontVersion(char* version, uint8_t len) const
{
    if (!isFileSet_ || (version == nullptr) || (len > FONT_VERSION_LEN)) {
        return INVALID_RET_VALUE;
    }
    if (memset_s(version, len, 0, len) != EOK) {
        return INVALID_RET_VALUE;
    }
    if (strcpy_s(version, len, binHeader_.fontVersion) != EOK) {
        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_) {
        return INVALID_RET_VALUE;
    }

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

    return curFontHeader_->fontHeight;
}

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

    if (!isFontIdSet_) {
        return INVALID_RET_VALUE;
    }
    node = GetGlyphNode(unicode);
    if (node == nullptr) {
        return INVALID_RET_VALUE;
    }
    return node->advance;
}

int8_t GlyphsManager::GetBitmap(uint32_t unicode, uint8_t* bitmap)
{
    if (bitmap == nullptr) {
        return INVALID_RET_VALUE;
    }
    if (!isFontIdSet_) {
        return INVALID_RET_VALUE;
    }

    const GlyphNode* node = GetGlyphNode(unicode);
    if (node == nullptr) {
        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)) {
        return INVALID_RET_VALUE;
    }

    int32_t readSize = read(fp_, bitmap, size);
    if (readSize != static_cast<int32_t>(size)) {
        return INVALID_RET_VALUE;
    }

    return RET_VALUE_OK;
}
} // namespace OHOS