text.h 14.3 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 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38
 * 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.
 */

/**
 * @addtogroup UI_Common
 * @{
 *
 * @brief Defines common UI capabilities, such as image and text processing.
 *
 * @since 1.0
 * @version 1.0
 */

/**
 * @file text.h
 *
 * @brief Declares the <b>Text</b> class that provides functions to set basic text attributes, such as the text
 *        direction and alignment mode.
 *
 * @since 1.0
 * @version 1.0
 */

#ifndef GRAPHIC_LITE_TEXT_H
#define GRAPHIC_LITE_TEXT_H

Z
zhangguyuan 已提交
39 40
#include "gfx_utils/geometry2d.h"
#include "gfx_utils/graphic_types.h"
X
xucheng57@huawei.com 已提交
41
#include "gfx_utils/list.h"
Z
zhangguyuan 已提交
42
#include "gfx_utils/style.h"
X
xucheng57@huawei.com 已提交
43
#include "gfx_utils/vector.h"
N
niulihua 已提交
44
#include "engines/gfx/gfx_engine_manager.h"
X
xucheng57@huawei.com 已提交
45
#include "font/ui_font_header.h"
L
Lizhiqi 已提交
46
#if defined(ENABLE_SPANNABLE_STRING) && ENABLE_SPANNABLE_STRING
X
xucheng57@huawei.com 已提交
47 48
#include "common/spannable_string.h"
#endif
M
mamingshuai 已提交
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

namespace OHOS {
/**
 * @brief Enumerates text alignment modes.
 */
enum UITextLanguageAlignment : uint8_t {
    /** Left-aligned */
    TEXT_ALIGNMENT_LEFT = 0,
    /** Right-aligned */
    TEXT_ALIGNMENT_RIGHT,
    /** Centered */
    TEXT_ALIGNMENT_CENTER,
    /** Top-aligned */
    TEXT_ALIGNMENT_TOP,
    /** Bottom-aligned */
    TEXT_ALIGNMENT_BOTTOM,
};

/**
 * @brief Enumerates text directions.
 */
enum UITextLanguageDirect : uint8_t {
    /** Left-to-right */
    TEXT_DIRECT_LTR = 0,
    /** Right-to-left */
    TEXT_DIRECT_RTL,
    TEXT_DIRECT_MIXED,
};

L
lancer 已提交
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
/**
 * @brief Stores the attribute information about this arc text to draw.
 */
struct ArcTextInfo {
    uint16_t radius;
    float startAngle;
    Point arcCenter;
    uint32_t lineStart;
    uint32_t lineEnd;
    UITextLanguageDirect direct;
    uint32_t* codePoints;
    uint16_t codePointsNum;
    uint8_t shapingFontId;
};

/**
 * @brief Enumerates text orientations.
 */
enum class TextOrientation : uint8_t {
    /** Inside */
    INSIDE,
    /** Outside */
    OUTSIDE,
};

X
xucheng57@huawei.com 已提交
103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123
struct BackgroundColor : public HeapBase {
    int16_t start;
    int16_t end;
    ColorType backgroundColor;
};

struct ForegroundColor : public HeapBase {
    int16_t start;
    int16_t end;
    ColorType fontColor;
};

struct LineBackgroundColor : public HeapBase {
    int16_t start;
    int16_t end;
    ColorType linebackgroundColor;
};

struct SizeSpan {
    bool isSizeSpan;
    uint8_t size;
124
    uint16_t fontId;
X
xucheng57@huawei.com 已提交
125 126 127
    int16_t height;
};

128 129
struct LabelLineInfo;

M
mamingshuai 已提交
130 131 132 133 134 135 136 137 138 139 140
/**
 * @brief Represents the base class of <b>Text</b>, providing the text attribute setting and text drawing
 *        capabilities for components that require font display.
 *
 * @since 1.0
 * @version 1.0
 */
class Text : public HeapBase {
public:
    /** Invalid value for the ellipsis position */
    static constexpr uint16_t TEXT_ELLIPSIS_END_INV = 0xFFFF;
141
    static constexpr uint16_t TEXT_ELLIPSIS_UNICODE = 0x2026;
M
mamingshuai 已提交
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

    /**
     * @brief A constructor used to create a <b>Text</b> instance.
     *
     * @since 1.0
     * @version 1.0
     */
    Text();

    /**
     * @brief A destructor used to delete the <b>Text</b> instance.
     *
     * @since 1.0
     * @version 1.0
     */
    virtual ~Text();

    /**
     * @brief Sets the content for this text.
     *
     * @param text Indicates the pointer to the text content.
     * @since 1.0
     * @version 1.0
     */
    virtual void SetText(const char* text);

L
Lizhiqi 已提交
168
#if defined(ENABLE_SPANNABLE_STRING) && ENABLE_SPANNABLE_STRING
X
xucheng57@huawei.com 已提交
169 170 171 172 173 174 175 176 177
    /**
     * @brief Sets the SpannableString for this text.
     *
     * @param text Indicates the pointer to the text content.
     * @since 1.0
     * @version 1.0
     */
    void SetSpannableString(const SpannableString* spannableString);
#endif
M
mamingshuai 已提交
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
    /**
     * @brief Obtains the content of this text.
     *
     * @return Returns the text content.
     * @since 1.0
     * @version 1.0
     */
    const char* GetText() const
    {
        return text_;
    }

    /**
     * @brief Sets the font name and size.
     *
     * @param name Indicates the pointer to the font name.
     * @param size Indicates the font size to set.
     * @since 1.0
     * @version 1.0
     */
    void SetFont(const char* name, uint8_t size);

    static void SetFont(const char* name, uint8_t size, char*& destName, uint8_t& destSize);

    /**
     * @brief Sets the font ID.
     *
     * @param fontId Indicates the font ID to set.
     * @since 1.0
     * @version 1.0
     */
209
    void SetFontId(uint16_t fontId);
M
mamingshuai 已提交
210 211 212 213 214 215 216 217

    /**
     * @brief Obtains the font ID.
     *
     * @return Returns the front ID.
     * @since 1.0
     * @version 1.0
     */
218
    uint16_t GetFontId() const
M
mamingshuai 已提交
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
    {
        return fontId_;
    }

    /**
     * @brief Obtains the font size.
     *
     * @return Returns the front size.
     * @since 1.0
     * @version 1.0
     */
    uint8_t GetFontSize() const
    {
        return fontSize_;
    }

    /**
     * @brief Sets the direction for this text.
     *
     * @param direct Indicates the text direction, as defined in {@link UITextLanguageDirect}.
     * @since 1.0
     * @version 1.0
     */
    void SetDirect(UITextLanguageDirect direct)
    {
        direct_ = direct;
    }

    /**
     * @brief Obtains the direction of this text.
     *
     * @return Returns the text direction, as defined in {@link UITextLanguageDirect}.
     * @since 1.0
     * @version 1.0
     */
    UITextLanguageDirect GetDirect() const
    {
        return static_cast<UITextLanguageDirect>(direct_);
    }

    /**
     * @brief Sets the alignment mode for this text.
     *
     * @param horizontalAlign Indicates the horizontal alignment mode to set,
     *                        which can be {@link TEXT_ALIGNMENT_LEFT},
     *                        {@link TEXT_ALIGNMENT_CENTER}, or {@link TEXT_ALIGNMENT_RIGHT}.
     * @param verticalAlign Indicates the vertical alignment mode to set, which can be
     *                      {@link TEXT_ALIGNMENT_TOP} (default mode), {@link TEXT_ALIGNMENT_CENTER},
     *                      or {@link TEXT_ALIGNMENT_BOTTOM}.
     * @since 1.0
     * @version 1.0
     */
    void SetAlign(UITextLanguageAlignment horizontalAlign, UITextLanguageAlignment verticalAlign = TEXT_ALIGNMENT_TOP)
    {
        if ((horizontalAlign_ != horizontalAlign) || (verticalAlign_ != verticalAlign)) {
            needRefresh_ = true;
            horizontalAlign_ = horizontalAlign;
            verticalAlign_ = verticalAlign;
        }
    }

    /**
     * @brief Obtains the horizontal alignment mode.
     *
     * @return Returns the horizontal alignment mode.
     * @since 1.0
     * @version 1.0
     */
    UITextLanguageAlignment GetHorAlign() const
    {
        return static_cast<UITextLanguageAlignment>(horizontalAlign_);
    }

    /**
     * @brief Obtains the vertical alignment mode.
     *
     * @return Returns the vertical alignment mode.
     * @since 1.0
     * @version 1.0
     */
    UITextLanguageAlignment GetVerAlign() const
    {
        return static_cast<UITextLanguageAlignment>(verticalAlign_);
    }

    /**
     * @brief Obtains the size of this text.
     *
     * @return Returns the text size.
     * @since 1.0
     * @version 1.0
     */
    Point GetTextSize() const
    {
        return textSize_;
    }

    virtual void ReMeasureTextSize(const Rect& textRect, const Style& style);

W
wanngtiantian 已提交
318 319
    void ReMeasureTextWidthInEllipsisMode(const Rect& textRect, const Style& style, uint16_t ellipsisIndex);

N
niulihua 已提交
320 321
    void OnDraw(BufferInfo& gfxDstBuffer,
                const Rect& invalidatedArea,
M
mamingshuai 已提交
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 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394
                const Rect& viewOrigRect,
                const Rect& textRect,
                int16_t offsetX,
                const Style& style,
                uint16_t ellipsisIndex,
                OpacityType opaScale);

    /**
     * @brief Sets whether to adapt the component width to this text.
     *
     * @param expand Specifies whether to adapt the component width to this text. The value <b>true</b> indicates
     *               that the component width will adapt to this text, and <b>false</b> indicates not.
     * @since 1.0
     * @version 1.0
     */
    void SetExpandWidth(bool expand)
    {
        expandWidth_ = expand;
    }

    /**
     * @brief Checks whether the component width adapts to this text.
     *
     * @return Returns <b>true</b> if the component width adapts to this text; returns <b>false</b> otherwise.
     * @since 1.0
     * @version 1.0
     */
    bool IsExpandWidth() const
    {
        return expandWidth_;
    }

    /**
     * @brief Sets whether to adapt the component height to this text.
     *
     * @param expand Specifies whether to adapt the component height to this text. The value <b>true</b> indicates
     *               that the component height will adapt to this text, and <b>false</b> indicates not.
     * @since 1.0
     * @version 1.0
     */
    void SetExpandHeight(bool expand)
    {
        expandHeight_ = expand;
    }

    /**
     * @brief Checks whether the component height adapts to this text.
     *
     * @return Returns <b>true</b> if the component height adapts to this text; returns <b>false</b> otherwise.
     * @since 1.0
     * @version 1.0
     */
    bool IsExpandHeight() const
    {
        return expandHeight_;
    }

    bool IsNeedRefresh() const
    {
        return needRefresh_;
    }

    /**
     * @brief Obtains the index of the character from where text will be replaced by ellipses based on
     *        the text rectangle and style.
     *
     * @param textRect Indicates the text rectangle.
     * @param style Indicates the text style.
     * @since 1.0
     * @version 1.0
     */
    uint16_t GetEllipsisIndex(const Rect& textRect, const Style& style);

L
lancer 已提交
395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424
    /**
     * @brief Get the GetShapingFontId of text
     *
     * @return Return ShapingFontId
     */
    virtual uint8_t GetShapingFontId() const
    {
        return 0;
    }

    /**
     * @brief Get the GetCodePointNum of text
     *
     * @return Return num of CodePoints
     */
    virtual uint16_t GetCodePointNum() const
    {
        return 0;
    }

    /**
     * @brief Get the GetCodePoints of text
     *
     * @return Return CodePoints of text
     */
    virtual uint32_t* GetCodePoints() const
    {
        return nullptr;
    }

425 426 427 428 429
    void SetSupportBaseLine(bool baseLine)
    {
        baseLine_ = baseLine;
    }

X
xucheng57@huawei.com 已提交
430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471
    void SetBackgroundColorSpan(ColorType backgroundColor, int16_t start, int16_t end)
    {
        BackgroundColor bgcolor;
        bgcolor.start = start;
        bgcolor.end = end;
        bgcolor.backgroundColor= backgroundColor;
        backgroundColor_.PushBack(bgcolor);
    }

    List<BackgroundColor> GetBackgroundColorSpan()
    {
        return backgroundColor_;
    }

    void SetForegroundColorSpan(ColorType fontColor, int16_t start, int16_t end)
    {
        ForegroundColor fColor;
        fColor.start = start;
        fColor.end = end;
        fColor.fontColor= fontColor;
        foregroundColor_.PushBack(fColor);
    }

    List<ForegroundColor> GetForegroundColorSpan()
    {
        return foregroundColor_;
    }

    void SetLineBackgroundSpan(ColorType linebackgroundColor, int16_t start, int16_t end)
    {
        LineBackgroundColor lineBackground;
        lineBackground.start = start;
        lineBackground.end = end;
        lineBackground.linebackgroundColor= linebackgroundColor;
        linebackgroundColor_.PushBack(lineBackground);
    }

    List<LineBackgroundColor> GetLineBackgroundSpan()
    {
        return linebackgroundColor_;
    }

L
liuyuxiang-bear 已提交
472
    void SetAbsoluteSizeSpan(uint16_t start, uint16_t end, uint8_t size);
L
liuyuxiang-bear 已提交
473
    void SetRelativeSizeSpan(uint16_t start, uint16_t end, float size);
X
xucheng57@huawei.com 已提交
474 475 476 477 478 479

    uint16_t GetSizeSpan()
    {
        return characterSize_;
    }

M
mamingshuai 已提交
480 481 482 483 484 485 486 487 488 489
protected:
    struct TextLine {
        uint16_t lineBytes;
        uint16_t linePixelWidth;
    };

    /** Maximum number of lines */
    static constexpr uint16_t MAX_LINE_COUNT = 50;
    static TextLine textLine_[MAX_LINE_COUNT];

490
    static constexpr const char* TEXT_ELLIPSIS = "…";
M
mamingshuai 已提交
491 492 493 494

    virtual uint32_t GetTextStrLen();

    virtual uint32_t
X
xucheng57@huawei.com 已提交
495 496
        GetTextLine(uint32_t begin, uint32_t textLen, int16_t width, uint16_t lineNum, uint8_t letterSpace,
                    uint16_t& letterIndex, SizeSpan* sizeSpans);
M
mamingshuai 已提交
497 498 499

    virtual uint16_t GetLetterIndexByPosition(const Rect& textRect, const Style& style, const Point& pos);

N
niulihua 已提交
500 501
    virtual void Draw(BufferInfo& gfxDstBuffer,
                      const Rect& mask,
M
mamingshuai 已提交
502 503 504 505 506 507 508 509 510
                      const Rect& coords,
                      const Style& style,
                      int16_t offsetX,
                      uint16_t ellipsisIndex,
                      OpacityType opaScale);

    uint16_t GetLine(int16_t width, uint8_t letterSpace, uint16_t ellipsisIndex, uint32_t& maxLineBytes);
    int16_t TextPositionY(const Rect& textRect, int16_t textHeight);
    int16_t LineStartPos(const Rect& textRect, uint16_t lineWidth);
X
xucheng57@huawei.com 已提交
511
    void DrawEllipsis(BufferInfo& gfxDstBuffer, LabelLineInfo& labelLine, uint16_t& letterIndex);
512
    uint32_t CalculateLineWithEllipsis(uint32_t begin, uint32_t textLen, int16_t width,
X
xucheng57@huawei.com 已提交
513 514 515
                                       uint8_t letterSpace, uint16_t& lineNum,
                                       uint16_t& letterIndex,
                                       SizeSpan* sizeSpans);
L
lancer 已提交
516
    uint16_t GetSpanFontIdBySize(uint8_t size);
X
xucheng57@huawei.com 已提交
517
    void InitSizeSpans();
L
Lizhiqi 已提交
518
#if defined(ENABLE_SPANNABLE_STRING) && ENABLE_SPANNABLE_STRING
X
xucheng57@huawei.com 已提交
519
    TextStyle* textStyles_;
L
Lizhiqi 已提交
520
#endif
M
mamingshuai 已提交
521
    char* text_;
522
    uint16_t fontId_;
M
mamingshuai 已提交
523 524 525 526 527
    uint8_t fontSize_; // Only the vector font library has a valid value.
    Point textSize_;
    bool needRefresh_ : 1;
    bool expandWidth_ : 1;
    bool expandHeight_ : 1;
528 529
    bool baseLine_ : 1;
    uint8_t direct_ : 4; // UITextLanguageDirect
X
xucheng57@huawei.com 已提交
530 531 532 533
    List<BackgroundColor> backgroundColor_;
    List<ForegroundColor> foregroundColor_;
    List<LineBackgroundColor> linebackgroundColor_;
    SizeSpan* sizeSpans_;
L
liuyuxiang-bear 已提交
534
    uint32_t characterSize_;
M
mamingshuai 已提交
535 536 537 538

private:
    uint8_t horizontalAlign_ : 4; // UITextLanguageAlignment
    uint8_t verticalAlign_ : 4;   // UITextLanguageAlignment
X
xucheng57@huawei.com 已提交
539 540

    static constexpr uint8_t FONT_ID_MAX = 0xFF;
M
mamingshuai 已提交
541 542
};
} // namespace OHOS
543
#endif // GRAPHIC_LITE_TEXT_H