ui_font_bitmap.cpp 8.3 KB
Newer Older
W
wangtiantian 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 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 47 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 183 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 215 216 217 218 219 220 221 222 223 224 225 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 251 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
/*
 * 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/ui_font_bitmap.h"

#include "font/ui_font_adaptor.h"
#include "font/ui_font_builder.h"
#include "gfx_utils/file.h"
#include "graphic_config.h"
#if ENABLE_MULTI_FONT
#include "font/ui_multi_font_manager.h"
#endif
#if ENABLE_SHAPING
#include "font/ui_text_shaping.h"
#endif

namespace OHOS {
UIFontBitmap::UIFontBitmap() : dynamicFont_(), dynamicFontRamUsed_(0), dynamicFontFd_(-1)
{
    SetBaseFontId(UIFontBuilder::GetInstance()->GetBitmapFontIdMax());
    bitmapCache_ = nullptr;
    bitmapRamUsed_ = FONT_BITMAP_CACHE_SIZE;
}

UIFontBitmap:: ~UIFontBitmap()
{
    if (dynamicFontFd_ >= 0) {
        close(dynamicFontFd_);
    }
    if (bitmapCache_ != nullptr) {
        delete bitmapCache_;
        bitmapCache_ = nullptr;
    }
}

bool UIFontBitmap::IsVectorFont() const
{
    return false;
}

uint8_t UIFontBitmap::GetShapingFontId(char* text, uint8_t& ttfId, uint32_t& script, uint8_t fontId, uint8_t size) const
{
#if ENABLE_MULTI_FONT
    return UIMultiFontManager::GetInstance()->GetShapingFontId(text, fontId, ttfId, script);
#else
    UITextLanguageFontParam* fontParam = UIFontBuilder::GetInstance()->GetTextLangFontsTable(fontId);
    if (fontParam == nullptr) {
        return 0;
    }
    ttfId = fontParam->ttfId;
    return fontParam->shaping;
#endif
}

uint8_t UIFontBitmap::GetFontWeight(uint8_t fontId)
{
    UITextLanguageFontParam* fontParam = UIFontBuilder::GetInstance()->GetTextLangFontsTable(fontId);
    if (fontParam == nullptr) {
        return 0;
    }
    return fontParam->fontWeight;
}

int8_t UIFontBitmap::SetFontPath(const char* dpath, const char* spath)
{
    if (dpath == nullptr) {
        return INVALID_RET_VALUE;
    }
#ifdef _WIN32
    dynamicFontFd_ = open(dpath, O_RDONLY | O_BINARY);
#else 
    dynamicFontFd_ = open(dpath, O_RDONLY);
#endif
    if (dynamicFontFd_ < 0) {
        return INVALID_RET_VALUE;
    }
    dynamicFont_.SetRamBuffer(GetRamAddr());
    uint32_t start = 0;
    int32_t ret = dynamicFont_.SetFile(dynamicFontFd_, start);
    if (ret == INVALID_RET_VALUE) {
        close(dynamicFontFd_);
        dynamicFontFd_ = -1;
        return ret;
    }
    dynamicFontRamUsed_ = dynamicFont_.GetRamUsedLen();
    return RET_VALUE_OK;
}

int8_t UIFontBitmap::SetCurrentFontId(uint8_t fontId, uint8_t size)
{
    int8_t ret = SetDynamicFontId(fontId);
    if (ret == RET_VALUE_OK) {
        SetBaseFontId(fontId);
    }
    return ret;
}

uint16_t UIFontBitmap::GetHeight()
{
    int16_t ret = dynamicFont_.SetCurrentFontId(GetBaseFontId());
    if (ret == INVALID_RET_VALUE) {
        return ret;
    }
    return dynamicFont_.GetFontHeight();
}

uint8_t UIFontBitmap::GetFontId(const char* ttfName, uint8_t size) const
{
    if (ttfName == nullptr) {
        return UIFontBuilder::GetInstance()->GetBitmapFontIdMax();
    }
    uint8_t id;
    for (id = 0; id < UIFontBuilder::GetInstance()->GetBitmapFontIdMax(); ++id) {
        UITextLanguageFontParam* fontParam = UIFontBuilder::GetInstance()->GetTextLangFontsTable(id);
        if (fontParam != nullptr) {
            if ((fontParam->size == size) && (strncmp(fontParam->ttfName, ttfName, TTF_NAME_LEN_MAX) == 0)) {
                break;
            }
        }
    }
    return id;
}

int16_t UIFontBitmap::GetWidth(uint32_t unicode, uint8_t fontId)
{
    return GetWidthInFontId(unicode, fontId);
}

uint8_t* UIFontBitmap::GetBitmap(uint32_t unicode, GlyphNode& glyphNode, uint8_t fontId)
{
    return SearchInFont(unicode, glyphNode, fontId);
}

int8_t UIFontBitmap::GetCurrentFontHeader(FontHeader& fontHeader)
{
    int8_t ret = dynamicFont_.SetCurrentFontId(GetBaseFontId());
    if (ret == INVALID_RET_VALUE) {
        return ret;
    }
    const FontHeader* header = dynamicFont_.GetCurrentFontHeader();
    if (header != nullptr) {
        fontHeader = *header;
        return RET_VALUE_OK;
    }
    return INVALID_RET_VALUE;
}

int8_t UIFontBitmap::GetGlyphNode(uint32_t unicode, GlyphNode& glyphNode)
{
    int8_t ret = dynamicFont_.SetCurrentFontId(GetBaseFontId());
    if (ret == INVALID_RET_VALUE) {
        return ret;
    }
    const GlyphNode* node = dynamicFont_.GetGlyphNode(unicode);
    if (node != nullptr) {
        glyphNode = *node;
        return RET_VALUE_OK;
    }
    return INVALID_RET_VALUE;
}

int8_t UIFontBitmap::GetFontVersion(char* dVersion, uint8_t dLen, char* sVersion, uint8_t sLen) const
{
    return dynamicFont_.GetFontVersion(dVersion, dLen);
}

int8_t UIFontBitmap::SetCurrentLangId(uint8_t langId)
{
    if (bitmapCache_ == nullptr) {
        uint8_t* bitmapCacheAddr = reinterpret_cast<uint8_t*>(GetRamAddr() + dynamicFontRamUsed_);
        bitmapCache_ = new UIFontCache(bitmapCacheAddr, bitmapRamUsed_);
    }
    uint32_t total = dynamicFontRamUsed_ + bitmapRamUsed_;
    return (total <= GetRamLen()) ? RET_VALUE_OK : INVALID_RET_VALUE;
}

UITextLanguageFontParam* UIFontBitmap::GetFontInfo(uint8_t fontId) const
{
    return UIFontBuilder::GetInstance()->GetTextLangFontsTable(fontId);
}


uint32_t UIFontBitmap::GetBitmapRamUsed()
{
    return bitmapRamUsed_;
}

uint32_t UIFontBitmap::GetDynamicFontRamUsed()
{
    return dynamicFontRamUsed_;
}

uint32_t UIFontBitmap::GetRamUsedLen(uint32_t textManagerRamUsed, uint32_t langFontRamUsed)
{
    if (bitmapCache_ == nullptr) {
        uint8_t* bitmapCacheAddr = reinterpret_cast<uint8_t*>(GetRamAddr() + dynamicFontRamUsed_ + textManagerRamUsed);
        bitmapCache_ = new UIFontCache(bitmapCacheAddr, bitmapRamUsed_);
    }
    return dynamicFontRamUsed_ + textManagerRamUsed + bitmapRamUsed_ + langFontRamUsed;
}

int8_t UIFontBitmap::GetDynamicFotBitmap(uint32_t unicode, uint8_t* bitmap)
{
    return dynamicFont_.GetBitmap(unicode, bitmap);
}

uint8_t* UIFontBitmap::GetCacheBitmap(uint8_t fontId, uint32_t unicode)
{
    if (bitmapCache_ != nullptr) {
        return bitmapCache_->GetBitmap(fontId, unicode);
    }
    return nullptr;
}

uint8_t* UIFontBitmap::GetCacheSpace(uint8_t fontId, uint32_t unicode, uint32_t size)
{
    if (bitmapCache_ != nullptr) {
        return bitmapCache_->GetSpace(fontId, unicode, size);
    }
    return nullptr;
}

void UIFontBitmap::PutCacheSpace(uint8_t* addr)
{
    if (bitmapCache_ != nullptr) {
        bitmapCache_->PutSpace(addr);
    }
}

int8_t UIFontBitmap::SetDynamicFontId(uint8_t fontId)
{
    return dynamicFont_.SetCurrentFontId(fontId);
}

int16_t UIFontBitmap::GetDynamicFontWidth(uint32_t unicode, uint8_t fontId)
{
    int16_t ret = dynamicFont_.SetCurrentFontId(fontId);
    if (ret == INVALID_RET_VALUE) {
        return ret;
    }
    return dynamicFont_.GetFontWidth(unicode);
}

uint8_t* UIFontBitmap::SearchInFont(uint32_t unicode, GlyphNode& glyphNode, uint8_t fontId)
{
    if (!UIFontAdaptor::IsSameTTFId(fontId, unicode)) {
        return nullptr;
    }
    if (fontId != GetBaseFontId()) {
        SetCurrentFontId(fontId);
    }
    if (bitmapCache_ == nullptr) {
        return nullptr;
    }
    uint8_t* bitmap = bitmapCache_->GetBitmap(GetBaseFontId(), unicode);
    if (bitmap != nullptr) {
        GetGlyphNode(unicode, glyphNode);
        return bitmap;
    }

    int8_t ret = GetGlyphNode(unicode, glyphNode);
    if (ret != RET_VALUE_OK) {
        return nullptr;
    }

    if (glyphNode.kernOff <= glyphNode.dataOff) {
        return nullptr;
    }
    uint32_t bitmapSize = glyphNode.kernOff - glyphNode.dataOff;
    bitmap = bitmapCache_->GetSpace(GetBaseFontId(), unicode, bitmapSize);
    ret = dynamicFont_.GetBitmap(unicode, bitmap);
    if (ret == RET_VALUE_OK) {
        return bitmap;
    }
    bitmapCache_->PutSpace(bitmap);
    return nullptr;
}

int16_t UIFontBitmap::GetWidthInFontId(uint32_t unicode, uint8_t fontId)
{
    if (!UIFontAdaptor::IsSameTTFId(fontId, unicode)) {
        return INVALID_RET_VALUE;
    }
    if (fontId != GetBaseFontId()) {
        SetCurrentFontId(fontId);
    }
    return GetDynamicFontWidth(unicode, GetBaseFontId());
}
} // namespace