text.cpp 14.0 KB
Newer Older
M
mamingshuai 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
/*
 * 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 "common/text.h"
#include "common/typed_text.h"
#include "draw/draw_label.h"
#include "font/ui_font.h"
W
wangtiantian 已提交
20
#include "font/ui_font_adaptor.h"
W
wangtiantian 已提交
21
#include "font/ui_font_builder.h"
Z
zhangguyuan 已提交
22
#include "gfx_utils/graphic_log.h"
M
mamingshuai 已提交
23 24 25 26 27 28 29 30 31 32 33 34 35
#include "securec.h"

namespace OHOS {
Text::TextLine Text::textLine_[MAX_LINE_COUNT] = {{0}};

Text::Text()
    : text_(nullptr),
      fontId_(0),
      fontSize_(0),
      textSize_({0, 0}),
      needRefresh_(false),
      expandWidth_(false),
      expandHeight_(false),
36
      baseLine_(true),
M
mamingshuai 已提交
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
      direct_(TEXT_DIRECT_LTR),
      horizontalAlign_(TEXT_ALIGNMENT_LEFT),
      verticalAlign_(TEXT_ALIGNMENT_TOP)
{
    SetFont(DEFAULT_VECTOR_FONT_FILENAME, DEFAULT_VECTOR_FONT_SIZE);
}

Text::~Text()
{
    if (text_ != nullptr) {
        UIFree(text_);
        text_ = nullptr;
    }
}

void Text::SetText(const char* text)
{
    if (text == nullptr) {
        return;
    }
    uint32_t textLen = static_cast<uint32_t>(strlen(text));
    if (textLen > MAX_TEXT_LENGTH) {
        return;
    }
    if (text_ != nullptr) {
        if (strcmp(text, text_) == 0) {
            return;
        }
        UIFree(text_);
        text_ = nullptr;
    }
    text_ = static_cast<char*>(UIMalloc(++textLen));
    if (text_ == nullptr) {
        return;
    }
    if (memcpy_s(text_, textLen, text, textLen) != EOK) {
        UIFree(text_);
        text_ = nullptr;
        return;
    }
    needRefresh_ = true;
}

void Text::SetFont(const char* name, uint8_t size)
{
    if (name == nullptr) {
        return;
    }
    if (UIFont::GetInstance()->IsVectorFont()) {
        uint8_t fontId = UIFont::GetInstance()->GetFontId(name);
W
wangtiantian 已提交
87 88
        if ((fontId != UIFontBuilder::GetInstance()->GetTotalFontId()) &&
            ((fontId_ != fontId) || (fontSize_ != size))) {
M
mamingshuai 已提交
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
            fontId_ = fontId;
            fontSize_ = size;
            needRefresh_ = true;
        }
    } else {
        uint8_t fontId = UIFont::GetInstance()->GetFontId(name, size);
        SetFontId(fontId);
    }
}

void Text::SetFont(const char* name, uint8_t size, char*& destName, uint8_t& destSize)
{
    if (name == nullptr) {
        return;
    }
    uint32_t nameLen = static_cast<uint32_t>(strlen(name));
    if (nameLen > MAX_TEXT_LENGTH) {
        return;
    }
    if (destName != nullptr) {
        if (strcmp(destName, name) == 0) {
            destSize = size;
            return;
        }
        UIFree(destName);
        destName = nullptr;
    }
    if (nameLen != 0) {
        /* one more to store '\0' */
        destName = static_cast<char*>(UIMalloc(++nameLen));
        if (destName == nullptr) {
            return;
        }
        if (memcpy_s(destName, nameLen, name, nameLen) != EOK) {
            UIFree(destName);
            destName = nullptr;
            return;
        }
        destSize = size;
    }
}

void Text::SetFontId(uint8_t fontId)
{
133
    if ((fontId >= UIFontBuilder::GetInstance()->GetTotalFontId()) || ((fontId_ == fontId) && (fontSize_ != 0))) {
134
        GRAPHIC_LOGE("Text::SetFontId invalid fontId(%d)", fontId);
M
mamingshuai 已提交
135 136
        return;
    }
W
wangtiantian 已提交
137
    UITextLanguageFontParam* fontParam = UIFontBuilder::GetInstance()->GetTextLangFontsTable(fontId);
M
mamingshuai 已提交
138 139 140 141 142
    if (fontParam == nullptr) {
        return;
    }
    if (UIFont::GetInstance()->IsVectorFont()) {
        uint8_t fontId = UIFont::GetInstance()->GetFontId(fontParam->ttfName);
W
wangtiantian 已提交
143 144
        if ((fontId != UIFontBuilder::GetInstance()->GetTotalFontId()) && ((fontId_ != fontId) ||
            (fontSize_ != fontParam->size))) {
M
mamingshuai 已提交
145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163
            fontId_ = fontId;
            fontSize_ = fontParam->size;
            needRefresh_ = true;
        }
    } else {
        fontId_ = fontId;
        fontSize_ = fontParam->size;
        needRefresh_ = true;
    }
}

void Text::ReMeasureTextSize(const Rect& textRect, const Style& style)
{
    if (fontSize_ == 0) {
        return;
    }
    UIFont::GetInstance()->SetCurrentFontId(fontId_, fontSize_);
    int16_t maxWidth = (expandWidth_ ? COORD_MAX : textRect.GetWidth());
    if (maxWidth > 0) {
164
        textSize_ = TypedText::GetTextSize(text_, style.letterSpace_, style.lineHeight_, maxWidth, style.lineSpace_);
165 166 167 168 169 170
        if (baseLine_) {
            FontHeader head;
            if (UIFont::GetInstance()->GetCurrentFontHeader(head) != 0) {
                return;
            }
            textSize_.y += fontSize_ - head.ascender;
M
mamingshuai 已提交
171 172 173 174
        }
    }
}

W
wanngtiantian 已提交
175 176 177 178 179 180 181 182 183 184 185
void Text::ReMeasureTextWidthInEllipsisMode(const Rect& textRect, const Style& style, uint16_t ellipsisIndex)
{
    if (ellipsisIndex != TEXT_ELLIPSIS_END_INV) {
        int16_t lineMaxWidth  = expandWidth_ ? textSize_.x : textRect.GetWidth();
        uint32_t maxLineBytes = 0;
        uint16_t lineCount = GetLine(lineMaxWidth, style.letterSpace_, ellipsisIndex, maxLineBytes);
        if ((lineCount > 0) && (textSize_.x < textLine_[lineCount - 1].linePixelWidth)) {
            textSize_.x = textLine_[lineCount - 1].linePixelWidth;
        }
    }
}
186 187 188 189 190 191 192 193 194 195

void Text::DrawEllipsis(BufferInfo& gfxDstBuffer, LabelLineInfo& labelLine)
{
    labelLine.offset.x = 0;
    labelLine.text = TEXT_ELLIPSIS;
    labelLine.lineLength = TEXT_ELLIPSIS_DOT_NUM;
    labelLine.length = TEXT_ELLIPSIS_DOT_NUM;
    DrawLabel::DrawTextOneLine(gfxDstBuffer, labelLine);
}

N
niulihua 已提交
196 197
void Text::OnDraw(BufferInfo& gfxDstBuffer,
                  const Rect& invalidatedArea,
M
mamingshuai 已提交
198 199 200 201 202 203 204 205 206 207 208 209 210 211
                  const Rect& viewOrigRect,
                  const Rect& textRect,
                  int16_t offsetX,
                  const Style& style,
                  uint16_t ellipsisIndex,
                  OpacityType opaScale)
{
    if ((text_ == nullptr) || (strlen(text_) == 0) || (fontSize_ == 0)) {
        return;
    }
    UIFont::GetInstance()->SetCurrentFontId(fontId_, fontSize_);
    Rect mask = invalidatedArea;

    if (mask.Intersect(mask, textRect)) {
N
niulihua 已提交
212
        Draw(gfxDstBuffer, mask, textRect, style, offsetX, ellipsisIndex, opaScale);
M
mamingshuai 已提交
213 214 215
    }
}

N
niulihua 已提交
216 217
void Text::Draw(BufferInfo& gfxDstBuffer,
                const Rect& mask,
M
mamingshuai 已提交
218 219 220 221 222 223 224 225
                const Rect& coords,
                const Style& style,
                int16_t offsetX,
                uint16_t ellipsisIndex,
                OpacityType opaScale)
{
    Point offset = {offsetX, 0};
    int16_t lineMaxWidth = expandWidth_ ? textSize_.x : coords.GetWidth();
W
wangtiantian 已提交
226 227
    int16_t lineHeight = style.lineHeight_;
    if (lineHeight == 0) {
228
        lineHeight = UIFont::GetInstance()->GetHeight() + style.lineSpace_;
W
wangtiantian 已提交
229
    }
M
mamingshuai 已提交
230 231 232 233
    uint16_t lineBegin = 0;
    uint32_t maxLineBytes = 0;
    uint16_t lineCount = GetLine(lineMaxWidth, style.letterSpace_, ellipsisIndex, maxLineBytes);
    Point pos;
234 235 236 237 238
    if (lineHeight == style.lineHeight_) {
        pos.y = TextPositionY(coords, (lineCount * lineHeight));
    } else {
        pos.y = TextPositionY(coords, (lineCount * lineHeight - style.lineSpace_));
    }
M
mamingshuai 已提交
239 240 241 242 243 244 245 246
    OpacityType opa = DrawUtils::GetMixOpacity(opaScale, style.textOpa_);
    for (int i = 0; i < lineCount; i++) {
        if (pos.y > mask.GetBottom()) {
            return;
        }
        int16_t nextLine = pos.y + lineHeight;
        if (nextLine >= mask.GetTop()) {
            pos.x = LineStartPos(coords, textLine_[i].linePixelWidth);
247 248 249
            LabelLineInfo labelLine{pos, offset, mask, lineHeight, textLine_[i].lineBytes,
                                    0, opa, style, &text_[lineBegin], textLine_[i].lineBytes,
                                    lineBegin, fontId_, fontSize_, 0, static_cast<UITextLanguageDirect>(direct_),
250
                                    nullptr, baseLine_};
N
niulihua 已提交
251
            DrawLabel::DrawTextOneLine(gfxDstBuffer, labelLine);
M
mamingshuai 已提交
252
            if ((i == (lineCount - 1)) && (ellipsisIndex != TEXT_ELLIPSIS_END_INV)) {
253
                DrawEllipsis(gfxDstBuffer, labelLine);
M
mamingshuai 已提交
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
            }
        }
        lineBegin += textLine_[i].lineBytes;
        pos.y = nextLine;
    }
}

int16_t Text::TextPositionY(const Rect& textRect, int16_t textHeight)
{
    int16_t yOffset = 0;
    if (!expandHeight_ && (verticalAlign_ != TEXT_ALIGNMENT_TOP) && (textRect.GetHeight() > textHeight)) {
        if (verticalAlign_ == TEXT_ALIGNMENT_CENTER) {
            yOffset = (textRect.GetHeight() - textHeight) >> 1;
        } else if (verticalAlign_ == TEXT_ALIGNMENT_BOTTOM) {
            yOffset = textRect.GetHeight() - textHeight;
        }
    }
    return textRect.GetY() + yOffset;
}

int16_t Text::LineStartPos(const Rect& textRect, uint16_t lineWidth)
{
    int16_t xOffset = 0;
    int16_t rectWidth = textRect.GetWidth();
    if (horizontalAlign_ == TEXT_ALIGNMENT_CENTER) {
        xOffset = (direct_ == TEXT_DIRECT_RTL) ? ((rectWidth + lineWidth + 1) >> 1) : ((rectWidth - lineWidth) >> 1);
    } else if (horizontalAlign_ == TEXT_ALIGNMENT_RIGHT) {
        xOffset = (direct_ == TEXT_DIRECT_RTL) ? rectWidth : (rectWidth - lineWidth);
    } else {
        xOffset = (direct_ == TEXT_DIRECT_RTL) ? lineWidth : 0;
    }
    return textRect.GetX() + xOffset;
}

uint16_t Text::GetLine(int16_t width, uint8_t letterSpace, uint16_t ellipsisIndex, uint32_t& maxLineBytes)
{
    if (text_ == nullptr) {
        return 0;
    }
    uint16_t lineNum = 0;
    uint32_t textLen = GetTextStrLen();
    if ((ellipsisIndex != TEXT_ELLIPSIS_END_INV) && (ellipsisIndex < textLen)) {
        textLen = ellipsisIndex;
    }
    uint32_t begin = 0;
    while ((begin < textLen) && (text_[begin] != '\0') && (lineNum < MAX_LINE_COUNT)) {
        begin += GetTextLine(begin, textLen, width, lineNum, letterSpace);
        if (maxLineBytes < textLine_[lineNum].lineBytes) {
            maxLineBytes = textLine_[lineNum].lineBytes;
        }
        lineNum++;
    }
    if ((lineNum != 0) && (ellipsisIndex != TEXT_ELLIPSIS_END_INV)) {
        uint16_t ellipsisWidth = UIFont::GetInstance()->GetWidth('.', 0) + letterSpace;
        textLine_[lineNum - 1].linePixelWidth += ellipsisWidth * TEXT_ELLIPSIS_DOT_NUM;
309 310 311 312 313
        if (textLine_[lineNum - 1].linePixelWidth > width) {
            int16_t newWidth = width - ellipsisWidth * TEXT_ELLIPSIS_DOT_NUM;
            maxLineBytes = CalculateLineWithEllipsis(begin, textLen, newWidth, letterSpace, lineNum);
            textLine_[lineNum - 1].linePixelWidth += ellipsisWidth * TEXT_ELLIPSIS_DOT_NUM;
        }
M
mamingshuai 已提交
314 315 316 317
    }
    return lineNum;
}

318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335
uint32_t Text::CalculateLineWithEllipsis(uint32_t begin, uint32_t textLen, int16_t width,
                                         uint8_t letterSpace, uint16_t& lineNum)
{
    begin -= textLine_[lineNum - 1].lineBytes;
    lineNum--;
    while ((begin < textLen) && (text_[begin] != '\0') && (lineNum < MAX_LINE_COUNT)) {
        begin += GetTextLine(begin, textLen, width, lineNum, letterSpace);
        lineNum++;
    }
    uint32_t maxLineBytes = 0;
    for (uint16_t i = 0; i < lineNum; i++) {
        if (maxLineBytes < textLine_[i].lineBytes) {
            maxLineBytes = textLine_[i].lineBytes;
        }
    }
    return maxLineBytes;
}

M
mamingshuai 已提交
336 337 338 339 340 341 342
uint32_t Text::GetTextStrLen()
{
    return strlen(text_);
}

uint32_t Text::GetTextLine(uint32_t begin, uint32_t textLen, int16_t width, uint16_t lineNum, uint8_t letterSpace)
{
W
wangtiantian 已提交
343 344 345
    int16_t lineWidth = width;
    uint16_t nextLineBytes = UIFontAdaptor::GetNextLineAndWidth(&text_[begin], letterSpace, lineWidth, false,
                                                                textLen - begin);
M
mamingshuai 已提交
346 347 348 349
    if (nextLineBytes + begin > textLen) {
        nextLineBytes = textLen - begin;
    }
    textLine_[lineNum].lineBytes = nextLineBytes;
W
wangtiantian 已提交
350
    textLine_[lineNum].linePixelWidth = lineWidth;
M
mamingshuai 已提交
351 352 353 354 355 356 357 358 359 360 361 362 363 364
    return nextLineBytes;
}

uint16_t Text::GetEllipsisIndex(const Rect& textRect, const Style& style)
{
    if ((textSize_.y <= textRect.GetHeight()) || (TypedText::GetUTF8CharacterSize(text_) <= TEXT_ELLIPSIS_DOT_NUM)) {
        return TEXT_ELLIPSIS_END_INV;
    }
    UIFont* fontEngine = UIFont::GetInstance();
    fontEngine->SetCurrentFontId(fontId_, fontSize_);
    int16_t letterWidth = fontEngine->GetWidth('.', 0) + style.letterSpace_;
    Point p;
    p.x = textRect.GetWidth() - letterWidth * TEXT_ELLIPSIS_DOT_NUM;
    p.y = textRect.GetHeight();
W
wangtiantian 已提交
365 366
    int16_t height = style.lineHeight_;
    if (height == 0) {
367
        height = fontEngine->GetHeight() + style.lineSpace_;
W
wangtiantian 已提交
368
    }
M
mamingshuai 已提交
369 370 371
    if (height) {
        p.y -= p.y % height;
    }
372 373 374
    if (height != style.lineHeight_) {
        p.y -= style.lineSpace_;
    }
M
mamingshuai 已提交
375 376 377 378 379 380 381 382 383 384
    return GetLetterIndexByPosition(textRect, style, p);
}

uint16_t Text::GetLetterIndexByPosition(const Rect& textRect, const Style& style, const Point& pos)
{
    if (text_ == nullptr) {
        return 0;
    }
    uint32_t lineStart = 0;
    uint32_t nextLineStart = 0;
W
wangtiantian 已提交
385
    uint16_t lineHeight = style.lineHeight_;
386
    uint16_t letterHeight = UIFont::GetInstance()->GetHeight();
W
wangtiantian 已提交
387
    if (lineHeight == 0) {
388 389 390 391 392 393 394
        lineHeight = letterHeight + style.lineSpace_;
    }
    uint16_t height = 0;
    if (lineHeight != style.lineHeight_) {
        height = letterHeight;
    } else {
        height = lineHeight;
W
wangtiantian 已提交
395
    }
M
mamingshuai 已提交
396 397
    int16_t y = 0;
    uint32_t textLen = static_cast<uint32_t>(strlen(text_));
W
wangtiantian 已提交
398
    int16_t width = 0;
M
mamingshuai 已提交
399
    while ((lineStart < textLen) && (text_[lineStart] != '\0')) {
W
wangtiantian 已提交
400 401 402
        width = textRect.GetWidth();
        nextLineStart += UIFontAdaptor::GetNextLineAndWidth(&text_[lineStart], style.letterSpace_, width);
        if (nextLineStart == 0) {
M
mamingshuai 已提交
403 404
            break;
        }
405
        if (pos.y <= y + height) {
M
mamingshuai 已提交
406 407
            break;
        }
W
wangtiantian 已提交
408
        y += lineHeight;
W
wangtiantian 已提交
409
        lineStart = nextLineStart;
M
mamingshuai 已提交
410
    }
411 412 413
    if (nextLineStart == textLen) {
        return TEXT_ELLIPSIS_END_INV;
    }
W
wangtiantian 已提交
414 415 416 417
    /* Calculate the x coordinate */
    width = pos.x;
    lineStart += UIFontAdaptor::GetNextLineAndWidth(&text_[lineStart], style.letterSpace_, width, true);
    return (lineStart < textLen) ? lineStart : TEXT_ELLIPSIS_END_INV;
M
mamingshuai 已提交
418
}
W
wanngtiantian 已提交
419
} // namespace OHOS