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

#if ENABLE_ICU
W
wangtiantian 已提交
17
#include "common/typed_text.h"
X
xucheng57@huawei.com 已提交
18
#include "draw/draw_utils.h"
W
wangtiantian 已提交
19
#include "font/ui_font.h"
X
xucheng57@huawei.com 已提交
20
#include "font/ui_line_break.h"
21
#include "font/icu_umutex_stub.h"
W
wangtiantian 已提交
22 23
#include "rbbidata.h"
#include "ucmndata.h"
24
#include "unicode/ucptrie.h"
W
wangtiantian 已提交
25 26

using namespace U_ICU_NAMESPACE;
M
mamingshuai 已提交
27
namespace OHOS {
W
wangtiantian 已提交
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45
static void* MemAlloc(const void* context, size_t size)
{
    return UIMalloc(size);
}

static void MemFree(const void* context, void* mem)
{
    if (mem == nullptr) {
        return;
    }
    UIFree(mem);
}

static void* MemRealloc(const void* context, void* mem, size_t size)
{
    return UIRealloc(mem, size);
}

Z
zhdengc 已提交
46 47 48 49 50 51
UILineBreakEngine& UILineBreakEngine::GetInstance()
{
    static UILineBreakEngine instance;
    return instance;
}

W
wangtiantian 已提交
52 53
uint16_t UILineBreakEngine::GetNextBreakPos(UILineBreakProxy& record)
{
54
    const uint32_t* str = record.GetStr();
W
wangtiantian 已提交
55 56 57 58 59
    if ((str == nullptr) || !initSuccess_ || (lineBreakTrie_ == nullptr)) {
        return 0;
    }
    int32_t state = LINE_BREAK_STATE_START;
    const RBBIStateTable* rbbStateTable = reinterpret_cast<const RBBIStateTable*>(stateTbl_);
60 61 62
    const RBBIStateTableRow8* row =
        reinterpret_cast<const RBBIStateTableRow8*>(rbbStateTable->fTableData + rbbStateTable->fRowLen * state);
    UCPTrie* trie = reinterpret_cast<UCPTrie*>(lineBreakTrie_);
W
wangtiantian 已提交
63
    for (uint16_t index = 0; index < record.GetStrLen(); ++index) {
64
        uint16_t category = UCPTRIE_FAST_GET(trie, UCPTRIE_8, str[index]);
W
wangtiantian 已提交
65 66 67 68 69 70
        // 0x4000: remove the dictionary flag bit
        if ((category & 0x4000) != 0) {
            // 0x4000: remove the dictionary flag bit
            category &= ~0x4000;
        }
        state = row->fNextState[category];
71
        row = reinterpret_cast<const RBBIStateTableRow8*>(rbbStateTable->fTableData + rbbStateTable->fRowLen * state);
W
wangtiantian 已提交
72
        int16_t completedRule = row->fAccepting;
73
        if ((completedRule > 1) || (state == LINE_BREAK_STATE_STOP)) {
W
wangtiantian 已提交
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
            return index;
        }
    }
    return record.GetStrLen();
}

void UILineBreakEngine::LoadRule()
{
    if ((fp_ < 0) || (addr_ == nullptr)) {
        return;
    }
    UErrorCode status = U_ZERO_ERROR;
    u_setMemoryFunctions(nullptr, MemAlloc, MemRealloc, MemFree, &status);
    if (status != U_ZERO_ERROR) {
        return;
    }
    int32_t ret = lseek(fp_, offset_, SEEK_SET);
    if (ret != offset_) {
        return;
    }
    char* buf = addr_;
    ret = read(fp_, buf, size_);
    if (ret != size_) {
        return;
    }
    const char* dataInBytes = reinterpret_cast<const char*>(buf);
    const DataHeader* dh = reinterpret_cast<const DataHeader*>(buf);
    const RBBIDataHeader* rbbidh = reinterpret_cast<const RBBIDataHeader*>(dataInBytes + dh->dataHeader.headerSize);
    stateTbl_ = reinterpret_cast<const RBBIStateTable*>(reinterpret_cast<const char*>(rbbidh) + rbbidh->fFTable);
    status = U_ZERO_ERROR;
Z
Zhouyj_zju 已提交
104
    lineBreakTrie_ = reinterpret_cast<UCPTrie*>(ucptrie_openFromBinary(UCPTRIE_TYPE_FAST, UCPTRIE_VALUE_BITS_8,
Z
Zhouyj_zju 已提交
105 106 107
                                                                       reinterpret_cast<const uint8_t*>(rbbidh)
                                                                       + rbbidh->fTrie,
                                                                       rbbidh->fTrieLen, nullptr, &status));
W
wangtiantian 已提交
108 109 110 111 112 113
    if (status != U_ZERO_ERROR) {
        return;
    }
    initSuccess_ = true;
}

114
uint32_t UILineBreakEngine::GetNextLineAndWidth(const char* text,
115
                                                uint16_t fontId,
116
                                                uint8_t fontSize,
117 118 119
                                                int16_t space,
                                                bool allBreak,
                                                int16_t& maxWidth,
X
xucheng57@huawei.com 已提交
120 121 122
                                                int16_t& maxHeight,
                                                uint16_t& letterIndex,
                                                SizeSpan* sizeSpans,
M
mamingshuai 已提交
123 124
                                                uint16_t len)
{
W
wangtiantian 已提交
125 126 127 128 129 130 131 132 133 134 135
    if (text == nullptr) {
        return 0;
    }
    bool isAllCanBreak = allBreak;
    uint32_t byteIdx = 0;
    uint32_t preIndex = 0;
    int16_t lastWidth = 0;
    int16_t lastIndex = 0;
    int16_t curWidth = 0;
    int32_t state = LINE_BREAK_STATE_START;
    int16_t width = 0;
X
xucheng57@huawei.com 已提交
136
    int16_t height = 0;
Z
Zhouyj_zju 已提交
137
    while ((byteIdx < len) && (text[byteIdx] != '\0')) {
W
wangtiantian 已提交
138 139 140 141 142
        uint32_t unicode = TypedText::GetUTF8Next(text, preIndex, byteIdx);
        if (unicode == 0) {
            preIndex = byteIdx;
            continue;
        }
143
        if (isAllCanBreak || IsBreakPos(unicode, fontId, fontSize, state)) {
144
            state = LINE_BREAK_STATE_START;
W
wangtiantian 已提交
145
            // Accumulates the status value from the current character.
146
            IsBreakPos(unicode, fontId, fontSize, state);
W
wangtiantian 已提交
147 148 149
            lastIndex = preIndex;
            lastWidth = curWidth;
        }
150
        width = GetLetterWidth(unicode, letterIndex, height, fontId, fontSize, sizeSpans);
X
xucheng57@huawei.com 已提交
151 152 153 154
        letterIndex++;
        if (height > maxHeight) {
            maxHeight = height;
        }
W
wangtiantian 已提交
155 156
        int16_t nextWidth = (curWidth > 0 && width > 0) ? (curWidth + space + width) : (curWidth + width);
        if (nextWidth > maxWidth) {
X
xucheng57@huawei.com 已提交
157
            letterIndex--;
W
wangtiantian 已提交
158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173
            if (lastIndex == 0) {
                break;
            }
            maxWidth = lastWidth;
            return lastIndex;
        }
        curWidth = nextWidth;
        preIndex = byteIdx;
        if (byteIdx > 0 && ((text[byteIdx - 1] == '\r') || (text[byteIdx - 1] == '\n'))) {
            break;
        }
    }
    maxWidth = curWidth;
    return preIndex;
}

X
xucheng57@huawei.com 已提交
174
int16_t UILineBreakEngine::GetLetterWidth(uint32_t unicode, uint16_t& letterIndex, int16_t& height,
175
                                          uint16_t fontId, uint8_t fontSize, SizeSpan* sizeSpans)
X
xucheng57@huawei.com 已提交
176 177
{
    if (sizeSpans != nullptr && sizeSpans[letterIndex].isSizeSpan) {
178 179
        int16_t width = UIFont::GetInstance()->GetWidth(unicode, sizeSpans[letterIndex].fontId,
                                                        sizeSpans[letterIndex].size, 0);
X
xucheng57@huawei.com 已提交
180 181

        if (sizeSpans[letterIndex].height == 0) {
182 183
            height = UIFont::GetInstance()->GetHeight(sizeSpans[letterIndex].fontId,
                                                      sizeSpans[letterIndex].size);
X
xucheng57@huawei.com 已提交
184 185 186 187 188 189
            sizeSpans[letterIndex].height = height;
        } else {
            height = sizeSpans[letterIndex].height;
        }
        return width;
    } else {
190 191
        height = UIFont::GetInstance()->GetHeight(fontId, fontSize);
        return UIFont::GetInstance()->GetWidth(unicode, fontId, fontSize, 0);
X
xucheng57@huawei.com 已提交
192 193 194
    }
}

195
bool UILineBreakEngine::IsBreakPos(uint32_t unicode, uint16_t fontId, uint8_t fontSize, int32_t& state)
W
wangtiantian 已提交
196 197 198 199 200
{
    if ((unicode > TypedText::MAX_UINT16_HIGH_SCOPE) || (stateTbl_ == nullptr) || (lineBreakTrie_ == nullptr)) {
        return true;
    }
    const RBBIStateTable* rbbStateTable = reinterpret_cast<const RBBIStateTable*>(stateTbl_);
201 202
    const RBBIStateTableRow8* row =
        reinterpret_cast<const RBBIStateTableRow8*>(rbbStateTable->fTableData + rbbStateTable->fRowLen * state);
W
wangtiantian 已提交
203 204 205 206 207
    uint16_t utf16 = 0;
    if (unicode <= TypedText::MAX_UINT16_LOW_SCOPE) {
        utf16 = (unicode & TypedText::MAX_UINT16_LOW_SCOPE);
    } else if (unicode <= TypedText::MAX_UINT16_HIGH_SCOPE) {
        utf16 = static_cast<uint16_t>(TypedText::UTF16_LOW_PARAM + (unicode & TypedText::UTF16_LOW_MASK)); // low
208
        uint16_t category = UCPTRIE_FAST_GET(reinterpret_cast<UCPTrie*>(lineBreakTrie_), UCPTRIE_8, utf16);
W
wangtiantian 已提交
209 210 211 212 213 214
        // 0x4000: remove the dictionary flag bit
        if ((category & 0x4000) != 0) {
            // 0x4000: remove the dictionary flag bit
            category &= ~0x4000;
        }
        state = row->fNextState[category];
215
        row = reinterpret_cast<const RBBIStateTableRow8*>(rbbStateTable->fTableData + rbbStateTable->fRowLen * state);
216 217
        utf16 = static_cast<uint16_t>(TypedText::UTF16_HIGH_PARAM1 + (unicode >> TypedText::UTF16_HIGH_SHIFT) -
                                      TypedText::UTF16_HIGH_PARAM2); // high
W
wangtiantian 已提交
218
    }
219
    uint16_t category = UCPTRIE_FAST_GET(reinterpret_cast<UCPTrie*>(lineBreakTrie_), UCPTRIE_8, utf16);
W
wangtiantian 已提交
220 221 222 223 224 225
    // 0x4000: remove the dictionary flag bit
    if ((category & 0x4000) != 0) {
        // 0x4000: remove the dictionary flag bit
        category &= ~0x4000;
    }
    state = row->fNextState[category];
226 227
    row = reinterpret_cast<const RBBIStateTableRow8*>(rbbStateTable->fTableData + rbbStateTable->fRowLen * state);
    return (row->fAccepting > 1 || state == LINE_BREAK_STATE_STOP);
M
mamingshuai 已提交
228 229
}
} // namespace OHOS
W
wangtiantian 已提交
230
#endif // ENABLE_ICU