glyphs_manager.cpp 12.5 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)) {
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
        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;
}

95
GlyphNode* GlyphsManager::GetNodeFromCache(uint32_t unicode, uint8_t fontId)
M
mamingshuai 已提交
96 97 98
{
    GlyphNode* node = nullptr;

99
    uint8_t font = fontId & FONT_HASH_MASK;
M
mamingshuai 已提交
100 101 102
    uint8_t uc = unicode & UNICODE_HASH_MASK;
    for (uint8_t i = 0; i < NODE_HASH_NR; i++) {
        GlyphNode* p = &((*nodeCache_)[font][uc][i]);
103
        if ((p->unicode == unicode) && (p->fontId == fontId)) {
M
mamingshuai 已提交
104 105 106 107 108 109 110
            node = p;
            break;
        }
    }
    return node;
}

111
GlyphNode* GlyphsManager::GetNodeCacheSpace(uint32_t unicode, uint8_t fontId)
M
mamingshuai 已提交
112 113 114 115
{
    uint8_t font, uc, i;
    GlyphNode* node = nullptr;

116
    font = fontId & FONT_HASH_MASK;
M
mamingshuai 已提交
117 118 119 120 121 122 123 124 125 126 127 128 129
    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;
}

130
GlyphNode* GlyphsManager::GetNodeFromFile(uint32_t unicode, uint8_t fontId)
M
mamingshuai 已提交
131 132 133 134
{
    uint16_t idx = 0;
    uint8_t key;
    uint32_t offset;
135 136 137
    uint8_t* tmpIndexCache = curIndexCache_;
    uint32_t tmpGlyphNodeSectionStart = curGlyphNodeSectionStart_;
    while (fontId_ != fontId) {
138
        if (SetCurrentFontId(fontId) == INVALID_RET_VALUE) {
139 140 141 142 143
            return nullptr;
        }
        tmpIndexCache = curIndexCache_;
        tmpGlyphNodeSectionStart = curGlyphNodeSectionStart_;
    }
M
mamingshuai 已提交
144 145 146 147
    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);
148
        idx = *(reinterpret_cast<uint16_t*>(tmpIndexCache + offset));
M
mamingshuai 已提交
149 150 151 152 153
        if (idx == 0) {
            return nullptr;
        }
    }

154
    offset = tmpGlyphNodeSectionStart + (idx - 1) * sizeof(GlyphNode);
M
mamingshuai 已提交
155 156
    int32_t ret = lseek(fp_, offset, SEEK_SET);
    if (ret != static_cast<int32_t>(offset)) {
157
        GRAPHIC_LOGE("GlyphsManager::GetNodeFromFile lseek failed");
M
mamingshuai 已提交
158 159
        return nullptr;
    }
160
    GlyphNode* node = GetNodeCacheSpace(unicode, fontId);
M
mamingshuai 已提交
161 162
    ret = read(fp_, node, sizeof(GlyphNode));
    if (ret < 0) {
163
        GRAPHIC_LOGE("GlyphsManager::GetNodeFromFile read failed");
M
mamingshuai 已提交
164 165 166 167 168 169 170 171 172 173 174 175 176 177 178
        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_) {
179
        GRAPHIC_LOGE("GlyphsManager::SetFile Ram not set");
M
mamingshuai 已提交
180 181 182 183 184 185 186
        return INVALID_RET_VALUE;
    }

    fp_ = fp;
    start_ = start;
    int32_t ret = lseek(fp_, start_, SEEK_SET);
    if (ret < 0) {
187
        GRAPHIC_LOGE("GlyphsManager::SetFile lseek failed");
M
mamingshuai 已提交
188 189 190 191
        return INVALID_RET_VALUE;
    }
    ret = read(fp_, &binHeader_, sizeof(binHeader_));
    if (ret != sizeof(binHeader_)) {
192
        GRAPHIC_LOGE("GlyphsManager::SetFile read failed");
M
mamingshuai 已提交
193 194 195 196 197
        return INVALID_RET_VALUE;
    }
    if (strncmp(binHeader_.fontMagic, FONT_MAGIC_NUMBER, FONT_MAGIC_NUM_LEN) != 0) {
        return INVALID_RET_VALUE;
    }
W
wangtiantian 已提交
198
    if (binHeader_.fontNum > UIFontBuilder::GetInstance()->GetBitmapFontIdMax()) {
199
        GRAPHIC_LOGE("GlyphsManager::SetFile data error, fontNum need less than max fontId");
M
mamingshuai 已提交
200 201 202 203 204 205 206 207 208 209 210 211 212 213
        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) {
214
        GRAPHIC_LOGE("GlyphsManager::SetFile read failed");
M
mamingshuai 已提交
215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231
        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 已提交
232
    fontId_ = UIFontBuilder::GetInstance()->GetBitmapFontIdMax();
M
mamingshuai 已提交
233 234 235 236 237 238
    return ret;
}

int8_t GlyphsManager::SetCurrentFontId(uint8_t fontId)
{
    uint16_t fontIdx = 0;
W
wangtiantian 已提交
239
    if (fontId > UIFontBuilder::GetInstance()->GetBitmapFontIdMax()) {
240
        GRAPHIC_LOGE("GlyphsManager::SetCurrentFontId fontId need less than max fontId");
M
mamingshuai 已提交
241 242
        return INVALID_RET_VALUE;
    }
243 244 245 246 247
    GraphicLockGuard guard(lock_);
    if (!isFileSet_) {
        GRAPHIC_LOGE("GlyphsManager::SetCurrentFontId file not set");
        return INVALID_RET_VALUE;
    }
M
mamingshuai 已提交
248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270
    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 已提交
271
        fontId_ = UIFontBuilder::GetInstance()->GetBitmapFontIdMax();
272
        GRAPHIC_LOGE("GlyphsManager::SetCurrentFontId fontId not found");
M
mamingshuai 已提交
273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294
        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_) {
295
        GRAPHIC_LOGE("GlyphsManager::GetRamUsedLen file not set");
M
mamingshuai 已提交
296 297 298 299 300 301 302 303
        return INVALID_RET_VALUE;
    }
    return ramUsedLen_;
}

int8_t GlyphsManager::GetFontVersion(char* version, uint8_t len) const
{
    if (!isFileSet_ || (version == nullptr) || (len > FONT_VERSION_LEN)) {
304
        GRAPHIC_LOGE("GlyphsManager::GetFontVersion invalid parameters");
M
mamingshuai 已提交
305 306 307
        return INVALID_RET_VALUE;
    }
    if (memset_s(version, len, 0, len) != EOK) {
308
        GRAPHIC_LOGE("GlyphsManager::GetFontVersion memset_s failed");
M
mamingshuai 已提交
309 310 311
        return INVALID_RET_VALUE;
    }
    if (strcpy_s(version, len, binHeader_.fontVersion) != EOK) {
312
        GRAPHIC_LOGE("GlyphsManager::GetFontVersion strcpy_s failed");
M
mamingshuai 已提交
313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335
        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;
    }
336
    uint8_t fontId = fontId_;
M
mamingshuai 已提交
337
    if (curGlyphNode_ != nullptr) {
338
        if ((curGlyphNode_->unicode == unicode) && (curGlyphNode_->fontId == fontId)) {
M
mamingshuai 已提交
339 340 341
            return curGlyphNode_;
        }
    }
342
    GlyphNode* node = GetNodeFromCache(unicode, fontId);
M
mamingshuai 已提交
343
    if (node == nullptr) {
344
        node = GetNodeFromFile(unicode, fontId);
M
mamingshuai 已提交
345
        if (node != nullptr) {
346
            node->fontId = fontId;
M
mamingshuai 已提交
347 348 349 350 351 352 353 354 355
        }
    }
    curGlyphNode_ = node;
    return node;
}

int16_t GlyphsManager::GetFontHeight() const
{
    if (!isFontIdSet_) {
356
        GRAPHIC_LOGE("GlyphsManager::GetFontHeight fontId not set");
M
mamingshuai 已提交
357 358 359 360
        return INVALID_RET_VALUE;
    }

    if (curFontHeader_ == nullptr) {
361
        GRAPHIC_LOGE("GlyphsManager::GetFontHeight curFontHeader is nullptr");
M
mamingshuai 已提交
362 363 364 365 366 367 368 369 370 371 372
        return INVALID_RET_VALUE;
    }

    return curFontHeader_->fontHeight;
}

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

    if (!isFontIdSet_) {
373
        GRAPHIC_LOGE("GlyphsManager::GetFontWidth fontId not set");
M
mamingshuai 已提交
374 375 376 377 378 379 380 381 382
        return INVALID_RET_VALUE;
    }
    node = GetGlyphNode(unicode);
    if (node == nullptr) {
        return INVALID_RET_VALUE;
    }
    return node->advance;
}

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

390
    GraphicLockGuard guard(lock_);
M
mamingshuai 已提交
391
    if (!isFontIdSet_) {
392
        GRAPHIC_LOGE("GlyphsManager::GetBitmap fontId not set");
M
mamingshuai 已提交
393 394
        return INVALID_RET_VALUE;
    }
395
    GlyphNode* node = const_cast<GlyphNode*>(GetGlyphNode(unicode));
396
    uint32_t tmpBitMapSectionStart = curBitMapSectionStart_;
397
    guard.Unlock();
M
mamingshuai 已提交
398
    if (node == nullptr) {
399
        GRAPHIC_LOGE("GlyphsManager::GetBitmap node not found");
M
mamingshuai 已提交
400 401
        return INVALID_RET_VALUE;
    }
402
    uint32_t offset = tmpBitMapSectionStart + node->dataOff;
M
mamingshuai 已提交
403 404 405
    uint32_t size = node->kernOff - node->dataOff;
    int32_t ret = lseek(fp_, offset, SEEK_SET);
    if (ret != static_cast<int32_t>(offset)) {
406
        GRAPHIC_LOGE("GlyphsManager::GetBitmap lseek failed");
M
mamingshuai 已提交
407 408 409 410 411
        return INVALID_RET_VALUE;
    }

    int32_t readSize = read(fp_, bitmap, size);
    if (readSize != static_cast<int32_t>(size)) {
412
        GRAPHIC_LOGE("GlyphsManager::GetBitmap read failed");
M
mamingshuai 已提交
413 414 415
        return INVALID_RET_VALUE;
    }

416
    node->dataFlag = fontId;
M
mamingshuai 已提交
417 418 419
    return RET_VALUE_OK;
}
} // namespace OHOS