text.cpp 11.9 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"
Z
zhangguyuan 已提交
20
#include "gfx_utils/graphic_log.h"
M
mamingshuai 已提交
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 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 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 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368
#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);
        if ((fontId != GetTotalFontId()) && ((fontId_ != fontId) || (fontSize_ != size))) {
            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)
{
    if ((fontId >= GetTotalFontId()) || (fontId_ == fontId)) {
        GRAPHIC_LOGE("Text::SetFontId invalid fontId(%d)", fontId);
        return;
    }
    UITextLanguageFontParam* fontParam = GetTextLangFontsTable(fontId);
    if (fontParam == nullptr) {
        return;
    }
    if (UIFont::GetInstance()->IsVectorFont()) {
        uint8_t fontId = UIFont::GetInstance()->GetFontId(fontParam->ttfName);
        if ((fontId != GetTotalFontId()) && ((fontId_ != fontId) || (fontSize_ != fontParam->size))) {
            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;
    }
}

void Text::OnDraw(const Rect& invalidatedArea,
                  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)) {
        Draw(mask, textRect, style, offsetX, ellipsisIndex, opaScale);
    }
}

void Text::Draw(const Rect& mask,
                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};
            DrawLabel::DrawTextOneLine(labelLine);
            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;
                DrawLabel::DrawTextOneLine(labelLine);
            }
        }
        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)
{
    uint16_t nextLineBytes = TypedText::GetNextLine(&text_[begin], letterSpace, width);
    if (nextLineBytes + begin > textLen) {
        nextLineBytes = textLen - begin;
    }
    textLine_[lineNum].lineBytes = nextLineBytes;
    textLine_[lineNum].linePixelWidth =
        static_cast<uint16_t>(TypedText::GetTextWidth(&text_[begin], nextLineBytes, letterSpace));
    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;

    uint16_t lineHeight = UIFont::GetInstance()->GetHeight();
    int16_t y = 0;
    uint32_t textLen = static_cast<uint32_t>(strlen(text_));
    while ((lineStart < textLen) && (text_[lineStart] != '\0')) {
        nextLineStart += TypedText::GetNextLine(&text_[lineStart], style.letterSpace_, textRect.GetWidth());
        if (pos.y <= y + lineHeight) {
            break;
        }
        y += lineHeight + style.lineSpace_;
        lineStart = nextLineStart;
    }
    /* Calculate the x coordinate */
    int16_t x = 0;
    uint32_t i = lineStart;
    uint32_t pre = i;
    while (i < nextLineStart - 1) {
        uint32_t letter = TypedText::GetUTF8Next(text_, i, i);
        x += UIFont::GetInstance()->GetWidth(letter, 0);
        if (pos.x < x) {
            i = pre;
            break;
        }
        x += style.letterSpace_;
        pre = i;
    }
    if (i >= (GetTextStrLen() - TEXT_ELLIPSIS_DOT_NUM)) {
        return TEXT_ELLIPSIS_END_INV;
    } else {
        return static_cast<uint16_t>(i);
    }
}
} // namespace OHOS