text.cpp 12.6 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 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
#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),
      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 已提交
86 87
        if ((fontId != UIFontBuilder::GetInstance()->GetTotalFontId()) &&
            ((fontId_ != fontId) || (fontSize_ != size))) {
M
mamingshuai 已提交
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
            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)
{
W
wangtiantian 已提交
132
    if ((fontId >= UIFontBuilder::GetInstance()->GetTotalFontId()) || (fontId_ == fontId)) {
M
mamingshuai 已提交
133 134 135
        GRAPHIC_LOGE("Text::SetFontId invalid fontId(%d)", fontId);
        return;
    }
W
wangtiantian 已提交
136
    UITextLanguageFontParam* fontParam = UIFontBuilder::GetInstance()->GetTextLangFontsTable(fontId);
M
mamingshuai 已提交
137 138 139 140 141
    if (fontParam == nullptr) {
        return;
    }
    if (UIFont::GetInstance()->IsVectorFont()) {
        uint8_t fontId = UIFont::GetInstance()->GetFontId(fontParam->ttfName);
W
wangtiantian 已提交
142 143
        if ((fontId != UIFontBuilder::GetInstance()->GetTotalFontId()) && ((fontId_ != fontId) ||
            (fontSize_ != fontParam->size))) {
M
mamingshuai 已提交
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
            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) {
        textSize_ = TypedText::GetTextSize(text_, style.letterSpace_, style.lineSpace_, maxWidth);
        FontHeader head;
        if (UIFont::GetInstance()->GetCurrentFontHeader(head) != 0) {
            return;
        }
        textSize_.y += fontSize_ - head.ascender;
    }
}

W
wanngtiantian 已提交
172 173 174 175 176 177 178 179 180 181 182
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;
        }
    }
}
N
niulihua 已提交
183 184
void Text::OnDraw(BufferInfo& gfxDstBuffer,
                  const Rect& invalidatedArea,
M
mamingshuai 已提交
185 186 187 188 189 190 191 192 193 194 195 196 197 198
                  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 已提交
199
        Draw(gfxDstBuffer, mask, textRect, style, offsetX, ellipsisIndex, opaScale);
M
mamingshuai 已提交
200 201 202
    }
}

N
niulihua 已提交
203 204
void Text::Draw(BufferInfo& gfxDstBuffer,
                const Rect& mask,
M
mamingshuai 已提交
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
                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();
    int16_t lineHeight = UIFont::GetInstance()->GetHeight() + style.lineSpace_;
    uint16_t lineBegin = 0;
    uint32_t maxLineBytes = 0;
    uint16_t lineCount = GetLine(lineMaxWidth, style.letterSpace_, ellipsisIndex, maxLineBytes);
    Point pos;
    pos.y = TextPositionY(coords, (lineCount * lineHeight - style.lineSpace_));
    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);
            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_),
                                    nullptr};
N
niulihua 已提交
243
            DrawLabel::DrawTextOneLine(gfxDstBuffer, labelLine);
M
mamingshuai 已提交
244 245 246 247 248
            if ((i == (lineCount - 1)) && (ellipsisIndex != TEXT_ELLIPSIS_END_INV)) {
                labelLine.offset.x = 0;
                labelLine.text = TEXT_ELLIPSIS;
                labelLine.lineLength = TEXT_ELLIPSIS_DOT_NUM;
                labelLine.length = TEXT_ELLIPSIS_DOT_NUM;
N
niulihua 已提交
249
                DrawLabel::DrawTextOneLine(gfxDstBuffer, labelLine);
M
mamingshuai 已提交
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 302 303 304 305 306 307 308 309 310 311 312 313 314 315
            }
        }
        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;
    }
    return lineNum;
}

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 已提交
316 317 318
    int16_t lineWidth = width;
    uint16_t nextLineBytes = UIFontAdaptor::GetNextLineAndWidth(&text_[begin], letterSpace, lineWidth, false,
                                                                textLen - begin);
M
mamingshuai 已提交
319 320 321 322
    if (nextLineBytes + begin > textLen) {
        nextLineBytes = textLen - begin;
    }
    textLine_[lineNum].lineBytes = nextLineBytes;
W
wangtiantian 已提交
323
    textLine_[lineNum].linePixelWidth = lineWidth;
M
mamingshuai 已提交
324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353
    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();
    int16_t height = fontEngine->GetHeight() + style.lineSpace_;
    if (height) {
        p.y -= p.y % height;
    }

    p.y -= style.lineSpace_;
    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 已提交
354
    uint16_t letterHeight = UIFont::GetInstance()->GetHeight();
M
mamingshuai 已提交
355 356
    int16_t y = 0;
    uint32_t textLen = static_cast<uint32_t>(strlen(text_));
W
wangtiantian 已提交
357
    int16_t width = 0;
M
mamingshuai 已提交
358
    while ((lineStart < textLen) && (text_[lineStart] != '\0')) {
W
wangtiantian 已提交
359 360 361
        width = textRect.GetWidth();
        nextLineStart += UIFontAdaptor::GetNextLineAndWidth(&text_[lineStart], style.letterSpace_, width);
        if (nextLineStart == 0) {
M
mamingshuai 已提交
362 363
            break;
        }
W
wangtiantian 已提交
364
        if (pos.y <= y + letterHeight) {
M
mamingshuai 已提交
365 366
            break;
        }
W
wangtiantian 已提交
367 368
        y += letterHeight + style.lineSpace_;
        lineStart = nextLineStart;
M
mamingshuai 已提交
369
    }
W
wangtiantian 已提交
370 371 372 373
    /* 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 已提交
374
}
W
wanngtiantian 已提交
375
} // namespace OHOS