text.cpp 20.9 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 17 18 19
 * 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
      direct_(TEXT_DIRECT_LTR),
X
xucheng57 已提交
38 39
      sizeSpans_(nullptr),
      characterSize_(0),
M
mamingshuai 已提交
40 41 42
      horizontalAlign_(TEXT_ALIGNMENT_LEFT),
      verticalAlign_(TEXT_ALIGNMENT_TOP)
{
L
Lizhiqi 已提交
43
#if defined(ENABLE_SPANNABLE_STRING) && ENABLE_SPANNABLE_STRING
X
xucheng57@huawei.com 已提交
44 45
    textStyles_ = nullptr;
#endif
M
mamingshuai 已提交
46 47 48 49 50 51 52 53 54
    SetFont(DEFAULT_VECTOR_FONT_FILENAME, DEFAULT_VECTOR_FONT_SIZE);
}

Text::~Text()
{
    if (text_ != nullptr) {
        UIFree(text_);
        text_ = nullptr;
    }
L
Lizhiqi 已提交
55
#if defined(ENABLE_SPANNABLE_STRING) && ENABLE_SPANNABLE_STRING
X
xucheng57@huawei.com 已提交
56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73
    if (textStyles_ != nullptr) {
        UIFree(textStyles_);
        textStyles_ = nullptr;
    }
#endif
    if (backgroundColor_.Size() > 0) {
        backgroundColor_.Clear();
    }
    if (linebackgroundColor_.Size() > 0) {
        linebackgroundColor_.Clear();
    }
    if (foregroundColor_.Size() > 0) {
        foregroundColor_.Clear();
    }
    if (sizeSpans_ != nullptr) {
        UIFree(sizeSpans_);
        sizeSpans_ = nullptr;
    }
M
mamingshuai 已提交
74 75
}

L
Lizhiqi 已提交
76
#if defined(ENABLE_SPANNABLE_STRING) && ENABLE_SPANNABLE_STRING
X
xucheng57@huawei.com 已提交
77 78 79 80 81 82 83 84 85 86 87 88 89 90 91
void Text::SetSpannableString(const SpannableString* spannableString)
{
    SetText(spannableString->text_);
    if (textStyles_ != nullptr) {
        UIFree(textStyles_);
        textStyles_ = nullptr;
    }
    if (spannableString->spanList_.IsEmpty()) {
        return;
    }
    uint32_t textLen = TypedText::GetUTF8CharacterSize(text_, GetTextStrLen());
    if (textLen == 0 || textLen > MAX_TEXT_LENGTH) {
        return;
    }
    textStyles_ = static_cast<TextStyle*>(UIMalloc(textLen));
Z
Zhouyj_zju 已提交
92 93 94 95
    if (textStyles_ == nullptr) {
        GRAPHIC_LOGE("Text::SetSpannableString invalid parameter");
        return;
    }
X
xucheng57@huawei.com 已提交
96 97 98 99 100 101 102 103 104 105 106 107
    ListNode<StyleSpan*>* node = spannableString->spanList_.Begin();
    while (node != spannableString->spanList_.End()) {
        for (uint32_t i = node->data_->start_; i < node->data_->end_; i++) {
            if (textLen > i) {
                textStyles_[i] = node->data_->textStyle_;
            }
        }
        node = node->next_;
    }
    needRefresh_ = true;
}
#endif
M
mamingshuai 已提交
108 109 110 111 112 113 114
void Text::SetText(const char* text)
{
    if (text == nullptr) {
        return;
    }
    uint32_t textLen = static_cast<uint32_t>(strlen(text));
    if (textLen > MAX_TEXT_LENGTH) {
P
pssea 已提交
115
        textLen = MAX_TEXT_LENGTH;
M
mamingshuai 已提交
116 117 118 119 120 121 122 123
    }
    if (text_ != nullptr) {
        if (strcmp(text, text_) == 0) {
            return;
        }
        UIFree(text_);
        text_ = nullptr;
    }
P
pssea 已提交
124
    text_ = static_cast<char*>(UIMalloc(textLen + 1));
M
mamingshuai 已提交
125 126 127
    if (text_ == nullptr) {
        return;
    }
P
pssea 已提交
128
    if (strncpy_s(text_, textLen + 1, text, textLen) != EOK) {
M
mamingshuai 已提交
129 130 131 132
        UIFree(text_);
        text_ = nullptr;
        return;
    }
L
Lizhiqi 已提交
133
#if defined(ENABLE_SPANNABLE_STRING) && ENABLE_SPANNABLE_STRING
X
xucheng57@huawei.com 已提交
134 135 136 137 138
    if (textStyles_ != nullptr) {
        UIFree(textStyles_);
        textStyles_ = nullptr;
    }
#endif
M
mamingshuai 已提交
139
    needRefresh_ = true;
X
xucheng57@huawei.com 已提交
140 141 142 143
    if (sizeSpans_ != nullptr) {
        UIFree(sizeSpans_);
        sizeSpans_ = nullptr;
    }
M
mamingshuai 已提交
144 145 146 147 148 149 150 151
}

void Text::SetFont(const char* name, uint8_t size)
{
    if (name == nullptr) {
        return;
    }
    if (UIFont::GetInstance()->IsVectorFont()) {
152
        uint16_t fontId = UIFont::GetInstance()->GetFontId(name);
W
wangtiantian 已提交
153 154
        if ((fontId != UIFontBuilder::GetInstance()->GetTotalFontId()) &&
            ((fontId_ != fontId) || (fontSize_ != size))) {
M
mamingshuai 已提交
155 156 157 158 159
            fontId_ = fontId;
            fontSize_ = size;
            needRefresh_ = true;
        }
    } else {
160
        uint16_t fontId = UIFont::GetInstance()->GetFontId(name, size);
M
mamingshuai 已提交
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
        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;
    }
}

197
void Text::SetFontId(uint16_t fontId)
M
mamingshuai 已提交
198
{
L
Lizhiqi 已提交
199
    if (fontId >= UIFontBuilder::GetInstance()->GetTotalFontId()) {
L
liuyuxiang-bear 已提交
200
        GRAPHIC_LOGE("Text::SetFontId invalid fontId(%hhd)", fontId);
M
mamingshuai 已提交
201 202
        return;
    }
L
Lizhiqi 已提交
203 204 205 206 207 208

    if ((fontId_ == fontId) && (fontSize_ != 0) && !UIFont::GetInstance()->IsVectorFont()) {
        GRAPHIC_LOGD("Text::SetFontId same font has already set");
        return;
    }

W
wangtiantian 已提交
209
    UITextLanguageFontParam* fontParam = UIFontBuilder::GetInstance()->GetTextLangFontsTable(fontId);
M
mamingshuai 已提交
210 211 212 213
    if (fontParam == nullptr) {
        return;
    }
    if (UIFont::GetInstance()->IsVectorFont()) {
214
        uint16_t fontId = UIFont::GetInstance()->GetFontId(fontParam->ttfName);
W
wangtiantian 已提交
215 216
        if ((fontId != UIFontBuilder::GetInstance()->GetTotalFontId()) && ((fontId_ != fontId) ||
            (fontSize_ != fontParam->size))) {
M
mamingshuai 已提交
217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234
            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;
    }
    int16_t maxWidth = (expandWidth_ ? COORD_MAX : textRect.GetWidth());
    if (maxWidth > 0) {
235 236
        textSize_ = TypedText::GetTextSize(text_, fontId_, fontSize_, style.letterSpace_, style.lineHeight_, maxWidth,
                                           style.lineSpace_, sizeSpans_);
237 238
        if (baseLine_) {
            FontHeader head;
239
            if (UIFont::GetInstance()->GetFontHeader(head, fontId_, fontSize_) != 0) {
240 241 242
                return;
            }
            textSize_.y += fontSize_ - head.ascender;
M
mamingshuai 已提交
243 244 245 246
        }
    }
}

W
wanngtiantian 已提交
247 248 249 250 251 252 253 254 255 256 257
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;
        }
    }
}
258

X
xucheng57@huawei.com 已提交
259
void Text::DrawEllipsis(BufferInfo& gfxDstBuffer, LabelLineInfo& labelLine, uint16_t& letterIndex)
260 261 262
{
    labelLine.offset.x = 0;
    labelLine.text = TEXT_ELLIPSIS;
263 264
    labelLine.lineLength = 1;
    labelLine.length = 1;
X
xucheng57@huawei.com 已提交
265
    DrawLabel::DrawTextOneLine(gfxDstBuffer, labelLine, letterIndex);
266 267
}

N
niulihua 已提交
268 269
void Text::OnDraw(BufferInfo& gfxDstBuffer,
                  const Rect& invalidatedArea,
M
mamingshuai 已提交
270 271 272 273 274 275 276 277 278 279 280 281 282
                  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;
    }
    Rect mask = invalidatedArea;

    if (mask.Intersect(mask, textRect)) {
N
niulihua 已提交
283
        Draw(gfxDstBuffer, mask, textRect, style, offsetX, ellipsisIndex, opaScale);
M
mamingshuai 已提交
284 285 286
    }
}

N
niulihua 已提交
287 288
void Text::Draw(BufferInfo& gfxDstBuffer,
                const Rect& mask,
M
mamingshuai 已提交
289 290 291 292 293 294 295 296 297 298 299
                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();
    uint16_t lineBegin = 0;
    uint32_t maxLineBytes = 0;
    uint16_t lineCount = GetLine(lineMaxWidth, style.letterSpace_, ellipsisIndex, maxLineBytes);
X
xucheng57@huawei.com 已提交
300
    int16_t lineHeight = style.lineHeight_;
301
    int16_t curLineHeight;
X
xucheng57@huawei.com 已提交
302
    if (lineHeight == 0) {
303 304 305 306
        lineHeight = UIFont::GetInstance()->GetHeight(fontId_, fontSize_);
        lineHeight += style.lineSpace_;
    }
    if ((style.lineSpace_ == 0) && (sizeSpans_ != nullptr)) {
X
xucheng57@huawei.com 已提交
307
        uint16_t letterIndex = 0;
308 309 310 311 312
        curLineHeight = UIFont::GetInstance()->GetLineMaxHeight(text_, textLine_[0].lineBytes, fontId_, fontSize_,
                                                                letterIndex, sizeSpans_);
        curLineHeight += style.lineSpace_;
    } else {
        curLineHeight = lineHeight;
X
xucheng57@huawei.com 已提交
313
    }
M
mamingshuai 已提交
314
    Point pos;
315 316 317 318 319
    if (lineHeight == style.lineHeight_) {
        pos.y = TextPositionY(coords, (lineCount * lineHeight));
    } else {
        pos.y = TextPositionY(coords, (lineCount * lineHeight - style.lineSpace_));
    }
M
mamingshuai 已提交
320
    OpacityType opa = DrawUtils::GetMixOpacity(opaScale, style.textOpa_);
X
xucheng57@huawei.com 已提交
321
    uint16_t letterIndex = 0;
322
    for (uint16_t i = 0; i < lineCount; i++) {
M
mamingshuai 已提交
323 324 325
        if (pos.y > mask.GetBottom()) {
            return;
        }
326
        int16_t nextLine = pos.y + curLineHeight;
327 328 329
        if (lineHeight != style.lineHeight_) {
            nextLine -= style.lineSpace_;
        }
X
xucheng57@huawei.com 已提交
330
        int16_t tempLetterIndex = letterIndex;
M
mamingshuai 已提交
331 332
        if (nextLine >= mask.GetTop()) {
            pos.x = LineStartPos(coords, textLine_[i].linePixelWidth);
333
            LabelLineInfo labelLine{pos, offset, mask, curLineHeight, textLine_[i].lineBytes,
334 335
                                    0, opa, style, &text_[lineBegin], textLine_[i].lineBytes,
                                    lineBegin, fontId_, fontSize_, 0, static_cast<UITextLanguageDirect>(direct_),
X
xucheng57@huawei.com 已提交
336
                                    nullptr, baseLine_,
L
Lizhiqi 已提交
337
#if defined(ENABLE_SPANNABLE_STRING) && ENABLE_SPANNABLE_STRING
X
xucheng57@huawei.com 已提交
338 339 340 341 342
                                    textStyles_,
#endif
                                    &backgroundColor_, &foregroundColor_, &linebackgroundColor_, sizeSpans_, 0};

            uint16_t ellipsisOssetY = DrawLabel::DrawTextOneLine(gfxDstBuffer, labelLine, letterIndex);
M
mamingshuai 已提交
343
            if ((i == (lineCount - 1)) && (ellipsisIndex != TEXT_ELLIPSIS_END_INV)) {
X
xucheng57@huawei.com 已提交
344 345
                labelLine.ellipsisOssetY = ellipsisOssetY;
                DrawEllipsis(gfxDstBuffer, labelLine, letterIndex);
M
mamingshuai 已提交
346
            }
X
xucheng57@huawei.com 已提交
347 348
        } else {
            letterIndex = TypedText::GetUTF8CharacterSize(text_, lineBegin + textLine_[i].lineBytes);
M
mamingshuai 已提交
349
        }
350 351 352 353 354 355 356
        if ((style.lineSpace_ == 0) && (sizeSpans_ != nullptr)) {
            curLineHeight = UIFont::GetInstance()->GetLineMaxHeight(&text_[lineBegin], textLine_[i].lineBytes, fontId_,
                                                                    fontSize_, tempLetterIndex, sizeSpans_);
            curLineHeight += style.lineSpace_;
        } else {
            curLineHeight = lineHeight;
        }
M
mamingshuai 已提交
357
        lineBegin += textLine_[i].lineBytes;
358
        pos.y += curLineHeight;
M
mamingshuai 已提交
359 360 361 362 363 364 365 366 367 368 369 370 371
    }
}

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;
        }
    }
372
    return textRect.GetY() + yOffset + EXTRA_HEIGHT / 2;   // 2:half extra height for first line
M
mamingshuai 已提交
373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399
}

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;
X
xucheng57@huawei.com 已提交
400
    uint16_t letterIndex = 0;
M
mamingshuai 已提交
401
    while ((begin < textLen) && (text_[begin] != '\0') && (lineNum < MAX_LINE_COUNT)) {
X
xucheng57@huawei.com 已提交
402
        begin += GetTextLine(begin, textLen, width, lineNum, letterSpace, letterIndex, sizeSpans_);
M
mamingshuai 已提交
403 404 405 406 407 408
        if (maxLineBytes < textLine_[lineNum].lineBytes) {
            maxLineBytes = textLine_[lineNum].lineBytes;
        }
        lineNum++;
    }
    if ((lineNum != 0) && (ellipsisIndex != TEXT_ELLIPSIS_END_INV)) {
409 410 411
        uint16_t ellipsisWidth =
            UIFont::GetInstance()->GetWidth(TEXT_ELLIPSIS_UNICODE, fontId_, fontSize_, 0) + letterSpace;
        textLine_[lineNum - 1].linePixelWidth += ellipsisWidth;
412
        if (textLine_[lineNum - 1].linePixelWidth > width) {
413
            int16_t newWidth = width - ellipsisWidth;
X
xucheng57@huawei.com 已提交
414 415
            maxLineBytes = CalculateLineWithEllipsis(begin, textLen, newWidth, letterSpace, lineNum, letterIndex,
                                                     sizeSpans_);
416
            textLine_[lineNum - 1].linePixelWidth += ellipsisWidth;
417
        }
M
mamingshuai 已提交
418 419 420 421
    }
    return lineNum;
}

422
uint32_t Text::CalculateLineWithEllipsis(uint32_t begin, uint32_t textLen, int16_t width,
X
xucheng57@huawei.com 已提交
423 424 425
                                         uint8_t letterSpace, uint16_t& lineNum,
                                         uint16_t& letterIndex,
                                         SizeSpan* sizeSpans)
426 427 428 429
{
    begin -= textLine_[lineNum - 1].lineBytes;
    lineNum--;
    while ((begin < textLen) && (text_[begin] != '\0') && (lineNum < MAX_LINE_COUNT)) {
X
xucheng57@huawei.com 已提交
430
        begin += GetTextLine(begin, textLen, width, lineNum, letterSpace, letterIndex, sizeSpans);
431 432 433 434 435 436 437 438 439 440 441
        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 已提交
442 443
uint32_t Text::GetTextStrLen()
{
444
    return (text_ != nullptr) ? (strlen(text_)) : 0;
M
mamingshuai 已提交
445 446
}

X
xucheng57@huawei.com 已提交
447 448
uint32_t Text::GetTextLine(uint32_t begin, uint32_t textLen, int16_t width, uint16_t lineNum, uint8_t letterSpace,
                           uint16_t& letterIndex, SizeSpan* sizeSpans)
M
mamingshuai 已提交
449
{
W
wangtiantian 已提交
450
    int16_t lineWidth = width;
X
xucheng57@huawei.com 已提交
451
    int16_t lineHeight = 0;
452 453 454
    uint16_t nextLineBytes = UIFontAdaptor::GetNextLineAndWidth(&text_[begin], fontId_, fontSize_, letterSpace,
                                                                lineWidth, lineHeight, letterIndex, sizeSpans, false,
                                                                textLen - begin);
M
mamingshuai 已提交
455 456 457 458
    if (nextLineBytes + begin > textLen) {
        nextLineBytes = textLen - begin;
    }
    textLine_[lineNum].lineBytes = nextLineBytes;
W
wangtiantian 已提交
459
    textLine_[lineNum].linePixelWidth = lineWidth;
M
mamingshuai 已提交
460 461 462 463 464
    return nextLineBytes;
}

uint16_t Text::GetEllipsisIndex(const Rect& textRect, const Style& style)
{
465
    if (textSize_.y <= textRect.GetHeight()) {
M
mamingshuai 已提交
466 467 468
        return TEXT_ELLIPSIS_END_INV;
    }
    UIFont* fontEngine = UIFont::GetInstance();
469
    int16_t letterWidth = fontEngine->GetWidth(TEXT_ELLIPSIS_UNICODE, fontId_, fontSize_, 0) + style.letterSpace_;
M
mamingshuai 已提交
470
    Point p;
471
    p.x = textRect.GetWidth() - letterWidth;
M
mamingshuai 已提交
472
    p.y = textRect.GetHeight();
W
wangtiantian 已提交
473 474
    int16_t height = style.lineHeight_;
    if (height == 0) {
475
        height = fontEngine->GetHeight(fontId_, fontSize_) + style.lineSpace_;
W
wangtiantian 已提交
476
    }
M
mamingshuai 已提交
477 478 479
    if (height) {
        p.y -= p.y % height;
    }
480 481 482
    if (height != style.lineHeight_) {
        p.y -= style.lineSpace_;
    }
M
mamingshuai 已提交
483 484 485 486 487 488 489 490 491 492
    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;
X
xucheng57@huawei.com 已提交
493
    int16_t lineHeight = style.lineHeight_;
494
    uint16_t letterHeight = UIFont::GetInstance()->GetHeight(fontId_, fontSize_);
W
wangtiantian 已提交
495
    if (lineHeight == 0) {
496 497 498 499 500 501 502
        lineHeight = letterHeight + style.lineSpace_;
    }
    uint16_t height = 0;
    if (lineHeight != style.lineHeight_) {
        height = letterHeight;
    } else {
        height = lineHeight;
W
wangtiantian 已提交
503
    }
M
mamingshuai 已提交
504 505
    int16_t y = 0;
    uint32_t textLen = static_cast<uint32_t>(strlen(text_));
W
wangtiantian 已提交
506
    int16_t width = 0;
X
xucheng57@huawei.com 已提交
507
    uint16_t letterIndex = 0;
M
mamingshuai 已提交
508
    while ((lineStart < textLen) && (text_[lineStart] != '\0')) {
W
wangtiantian 已提交
509
        width = textRect.GetWidth();
510 511
        nextLineStart += UIFontAdaptor::GetNextLineAndWidth(&text_[lineStart], fontId_, fontSize_, style.letterSpace_,
                                                            width, lineHeight, letterIndex, sizeSpans_);
W
wangtiantian 已提交
512
        if (nextLineStart == 0) {
M
mamingshuai 已提交
513 514
            break;
        }
515
        if (pos.y <= y + height) {
M
mamingshuai 已提交
516 517
            break;
        }
W
wangtiantian 已提交
518
        y += lineHeight;
W
wangtiantian 已提交
519
        lineStart = nextLineStart;
M
mamingshuai 已提交
520
    }
521 522 523
    if (nextLineStart == textLen) {
        return TEXT_ELLIPSIS_END_INV;
    }
W
wangtiantian 已提交
524 525
    /* Calculate the x coordinate */
    width = pos.x;
526 527 528
    lineStart +=
        UIFontAdaptor::GetNextLineAndWidth(&text_[lineStart], fontId_, fontSize_, style.letterSpace_, width, lineHeight,
                                           letterIndex, sizeSpans_, true);
W
wangtiantian 已提交
529
    return (lineStart < textLen) ? lineStart : TEXT_ELLIPSIS_END_INV;
M
mamingshuai 已提交
530
}
X
xucheng57@huawei.com 已提交
531

L
liuyuxiang-bear 已提交
532
void Text::SetAbsoluteSizeSpan(uint16_t start, uint16_t end, uint8_t size)
X
xucheng57@huawei.com 已提交
533
{
Y
youbing 已提交
534
#if defined(ENABLE_VECTOR_FONT) && ENABLE_VECTOR_FONT
X
xucheng57@huawei.com 已提交
535 536 537 538 539 540 541 542
    if (fontId_ == FONT_ID_MAX) {
        return;
    }
#else
    if (fontId_ == UIFontBuilder::GetInstance()->GetBitmapFontIdMax()) {
        return;
    }
#endif
543
    uint16_t fontId = GetSpanFontIdBySize(size);
Y
youbing 已提交
544
#if defined(ENABLE_VECTOR_FONT) && !ENABLE_VECTOR_FONT
X
xucheng57@huawei.com 已提交
545 546 547 548 549 550 551 552 553 554 555 556 557 558 559
    if (fontId == fontId_) {
        return;
    }
#endif
    if (text_ != nullptr && sizeSpans_ == nullptr) {
        characterSize_ = TypedText::GetUTF8CharacterSize(text_, GetTextStrLen());
        sizeSpans_ = static_cast<SizeSpan*>(UIMalloc(characterSize_ * sizeof(SizeSpan)));
        if (sizeSpans_ == nullptr) {
            GRAPHIC_LOGE("Text::SetAbsoluteSizeSpan invalid parameter");
            return;
        }
        InitSizeSpans();
    }

    if (sizeSpans_ != nullptr && start <= characterSize_) {
L
liuyuxiang-bear 已提交
560
        for (uint16_t i = start; i < end && i < characterSize_; i++) {
X
xucheng57@huawei.com 已提交
561 562 563 564 565 566 567
            sizeSpans_[i].fontId = fontId;
            sizeSpans_[i].size = size;
            sizeSpans_[i].isSizeSpan = true;
        }
    }
}

L
liuyuxiang-bear 已提交
568
void Text::SetRelativeSizeSpan(uint16_t start, uint16_t end, float size)
X
xucheng57@huawei.com 已提交
569 570
{
    uint8_t absoluteSize = 0;
Y
youbing 已提交
571
#if defined(ENABLE_VECTOR_FONT) && ENABLE_VECTOR_FONT
X
xucheng57@huawei.com 已提交
572 573 574
    absoluteSize = static_cast<uint8_t>(size * fontSize_);
#else
    UITextLanguageFontParam* fontParam = UIFontBuilder::GetInstance()->GetTextLangFontsTable(fontId_);
Z
Zhouyj_zju 已提交
575 576 577 578
    if (fontParam == nullptr) {
        GRAPHIC_LOGE("Text::SetRelativeSizeSpan invalid parameter");
        return;
    }
X
xucheng57@huawei.com 已提交
579 580 581 582 583
    absoluteSize = static_cast<uint8_t>(size * fontParam->size);
#endif
    SetAbsoluteSizeSpan(start, end, absoluteSize);
}

L
lancer 已提交
584
uint16_t Text::GetSpanFontIdBySize(uint8_t size)
X
xucheng57@huawei.com 已提交
585
{
Y
youbing 已提交
586
#if defined(ENABLE_VECTOR_FONT) && ENABLE_VECTOR_FONT
X
xucheng57@huawei.com 已提交
587 588 589 590 591 592 593 594
    return fontId_;
#else
    UITextLanguageFontParam* fontParam = UIFontBuilder::GetInstance()->GetTextLangFontsTable(fontId_);
    if (fontParam == nullptr) {
        return fontId_;
    }

    uint8_t ttfId = fontParam->ttfId;
595
    for (uint16_t fontId = 0; fontId < UIFontBuilder::GetInstance()->GetTotalFontId(); fontId++) {
X
xucheng57@huawei.com 已提交
596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616
        UITextLanguageFontParam* tempFontParam = UIFontBuilder::GetInstance()->GetTextLangFontsTable(fontId);
        if (tempFontParam == nullptr) {
            continue;
        }
        if (ttfId == tempFontParam->ttfId && size == tempFontParam->size) {
            return fontId;
        }
    }
    return fontId_;
#endif
}

void Text::InitSizeSpans()
{
    if (sizeSpans_ != nullptr) {
        for (uint32_t i = 0 ; i < TypedText::GetUTF8CharacterSize(text_, GetTextStrLen()); i++) {
            sizeSpans_[i].isSizeSpan = false;
            sizeSpans_[i].height = 0;
        }
    }
}
W
wanngtiantian 已提交
617
} // namespace OHOS