1a3e0fd82Sopenharmony_ci/*
2a3e0fd82Sopenharmony_ci * Copyright (c) 2020-2022 Huawei Device Co., Ltd.
3a3e0fd82Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
4a3e0fd82Sopenharmony_ci * you may not use this file except in compliance with the License.
5a3e0fd82Sopenharmony_ci * You may obtain a copy of the License at
6a3e0fd82Sopenharmony_ci *
7a3e0fd82Sopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
8a3e0fd82Sopenharmony_ci *
9a3e0fd82Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software
10a3e0fd82Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
11a3e0fd82Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12a3e0fd82Sopenharmony_ci * See the License for the specific language governing permissions and
13a3e0fd82Sopenharmony_ci * limitations under the License.
14a3e0fd82Sopenharmony_ci */
15a3e0fd82Sopenharmony_ci
16a3e0fd82Sopenharmony_ci/**
17a3e0fd82Sopenharmony_ci * @addtogroup UI_Common
18a3e0fd82Sopenharmony_ci * @{
19a3e0fd82Sopenharmony_ci *
20a3e0fd82Sopenharmony_ci * @brief Defines common UI capabilities, such as image and text processing.
21a3e0fd82Sopenharmony_ci *
22a3e0fd82Sopenharmony_ci * @since 1.0
23a3e0fd82Sopenharmony_ci * @version 1.0
24a3e0fd82Sopenharmony_ci */
25a3e0fd82Sopenharmony_ci
26a3e0fd82Sopenharmony_ci/**
27a3e0fd82Sopenharmony_ci * @file text.h
28a3e0fd82Sopenharmony_ci *
29a3e0fd82Sopenharmony_ci * @brief Declares the <b>Text</b> class that provides functions to set basic text attributes, such as the text
30a3e0fd82Sopenharmony_ci *        direction and alignment mode.
31a3e0fd82Sopenharmony_ci *
32a3e0fd82Sopenharmony_ci * @since 1.0
33a3e0fd82Sopenharmony_ci * @version 1.0
34a3e0fd82Sopenharmony_ci */
35a3e0fd82Sopenharmony_ci
36a3e0fd82Sopenharmony_ci#ifndef GRAPHIC_LITE_TEXT_H
37a3e0fd82Sopenharmony_ci#define GRAPHIC_LITE_TEXT_H
38a3e0fd82Sopenharmony_ci
39a3e0fd82Sopenharmony_ci#include <cstring>
40a3e0fd82Sopenharmony_ci#include "common/spannable_string.h"
41a3e0fd82Sopenharmony_ci#include "engines/gfx/gfx_engine_manager.h"
42a3e0fd82Sopenharmony_ci#include "font/ui_font_header.h"
43a3e0fd82Sopenharmony_ci
44a3e0fd82Sopenharmony_ci#include "gfx_utils/geometry2d.h"
45a3e0fd82Sopenharmony_ci#include "gfx_utils/graphic_types.h"
46a3e0fd82Sopenharmony_ci#include "gfx_utils/list.h"
47a3e0fd82Sopenharmony_ci#include "gfx_utils/style.h"
48a3e0fd82Sopenharmony_ci#include "gfx_utils/vector.h"
49a3e0fd82Sopenharmony_ci
50a3e0fd82Sopenharmony_cinamespace OHOS {
51a3e0fd82Sopenharmony_cinamespace {
52a3e0fd82Sopenharmony_ciconst std::string PASSWORD_DOT = "*"; // dot for password type
53a3e0fd82Sopenharmony_ciconstexpr uint16_t DEFAULT_TEXT_OFFSET = 5;
54a3e0fd82Sopenharmony_ciconstexpr uint16_t BOTH_SIDE_TEXT_OFFSET = DEFAULT_TEXT_OFFSET * 2; // 2: left and right space
55a3e0fd82Sopenharmony_ciconstexpr uint16_t DEFAULT_CURSOR_OFFSET = 1;
56a3e0fd82Sopenharmony_ciconstexpr uint16_t DEFAULT_CURSOR_WIDTH = 2;
57a3e0fd82Sopenharmony_ci} // namespace name
58a3e0fd82Sopenharmony_ci
59a3e0fd82Sopenharmony_ci/**
60a3e0fd82Sopenharmony_ci * @brief Enumerates text alignment modes.
61a3e0fd82Sopenharmony_ci */
62a3e0fd82Sopenharmony_cienum UITextLanguageAlignment : uint8_t {
63a3e0fd82Sopenharmony_ci    /** Left-aligned */
64a3e0fd82Sopenharmony_ci    TEXT_ALIGNMENT_LEFT = 0,
65a3e0fd82Sopenharmony_ci    /** Right-aligned */
66a3e0fd82Sopenharmony_ci    TEXT_ALIGNMENT_RIGHT,
67a3e0fd82Sopenharmony_ci    /** Centered */
68a3e0fd82Sopenharmony_ci    TEXT_ALIGNMENT_CENTER,
69a3e0fd82Sopenharmony_ci    /** Top-aligned */
70a3e0fd82Sopenharmony_ci    TEXT_ALIGNMENT_TOP,
71a3e0fd82Sopenharmony_ci    /** Bottom-aligned */
72a3e0fd82Sopenharmony_ci    TEXT_ALIGNMENT_BOTTOM,
73a3e0fd82Sopenharmony_ci};
74a3e0fd82Sopenharmony_ci
75a3e0fd82Sopenharmony_ci/**
76a3e0fd82Sopenharmony_ci * @brief Enumerates text directions.
77a3e0fd82Sopenharmony_ci */
78a3e0fd82Sopenharmony_cienum UITextLanguageDirect : uint8_t {
79a3e0fd82Sopenharmony_ci    /** Left-to-right */
80a3e0fd82Sopenharmony_ci    TEXT_DIRECT_LTR = 0,
81a3e0fd82Sopenharmony_ci    /** Right-to-left */
82a3e0fd82Sopenharmony_ci    TEXT_DIRECT_RTL,
83a3e0fd82Sopenharmony_ci    TEXT_DIRECT_MIXED,
84a3e0fd82Sopenharmony_ci};
85a3e0fd82Sopenharmony_ci
86a3e0fd82Sopenharmony_ci/**
87a3e0fd82Sopenharmony_ci * @brief Stores the attribute information about this arc text to draw.
88a3e0fd82Sopenharmony_ci */
89a3e0fd82Sopenharmony_cistruct ArcTextInfo {
90a3e0fd82Sopenharmony_ci    uint16_t radius;
91a3e0fd82Sopenharmony_ci    float startAngle;
92a3e0fd82Sopenharmony_ci    float endAngle;
93a3e0fd82Sopenharmony_ci    Point arcCenter;
94a3e0fd82Sopenharmony_ci    uint32_t lineStart;
95a3e0fd82Sopenharmony_ci    uint32_t lineEnd;
96a3e0fd82Sopenharmony_ci    UITextLanguageDirect direct;
97a3e0fd82Sopenharmony_ci    bool hasAnimator;
98a3e0fd82Sopenharmony_ci    uint32_t* codePoints;
99a3e0fd82Sopenharmony_ci    uint16_t codePointsNum;
100a3e0fd82Sopenharmony_ci    uint8_t shapingFontId;
101a3e0fd82Sopenharmony_ci};
102a3e0fd82Sopenharmony_ci
103a3e0fd82Sopenharmony_ci/**
104a3e0fd82Sopenharmony_ci * @brief Stores attribute information for this arc text to be drawn.
105a3e0fd82Sopenharmony_ci */
106a3e0fd82Sopenharmony_cistruct ArcLetterInfo {
107a3e0fd82Sopenharmony_ci    void InitData(uint16_t inFontId, uint8_t inFontSize, uint32_t inLetter, Point inPos,
108a3e0fd82Sopenharmony_ci        int16_t inRotateAngle, ColorType inColor, OpacityType inOpaScale,
109a3e0fd82Sopenharmony_ci        float inStartAngle, float inEndAngle, float inCurrentAngle, uint16_t inRadius,
110a3e0fd82Sopenharmony_ci        bool inCompatibilityMode, bool inDirectFlag, bool inOrientationFlag, bool inHasAnimator)
111a3e0fd82Sopenharmony_ci    {
112a3e0fd82Sopenharmony_ci        fontId = inFontId;
113a3e0fd82Sopenharmony_ci        fontSize = inFontSize;
114a3e0fd82Sopenharmony_ci        letter = inLetter;
115a3e0fd82Sopenharmony_ci        pos = inPos;
116a3e0fd82Sopenharmony_ci        rotateAngle = inRotateAngle;
117a3e0fd82Sopenharmony_ci        color = inColor;
118a3e0fd82Sopenharmony_ci        opaScale = inOpaScale;
119a3e0fd82Sopenharmony_ci        startAngle = inStartAngle;
120a3e0fd82Sopenharmony_ci        endAngle = inEndAngle;
121a3e0fd82Sopenharmony_ci        currentAngle = inCurrentAngle;
122a3e0fd82Sopenharmony_ci        radius = inRadius;
123a3e0fd82Sopenharmony_ci        compatibilityMode = inCompatibilityMode;
124a3e0fd82Sopenharmony_ci        directFlag = inDirectFlag;
125a3e0fd82Sopenharmony_ci        orientationFlag = inOrientationFlag;
126a3e0fd82Sopenharmony_ci        hasAnimator = inHasAnimator;
127a3e0fd82Sopenharmony_ci    }
128a3e0fd82Sopenharmony_ci    uint16_t fontId;
129a3e0fd82Sopenharmony_ci    uint8_t fontSize;
130a3e0fd82Sopenharmony_ci    uint32_t letter;
131a3e0fd82Sopenharmony_ci    Point pos;
132a3e0fd82Sopenharmony_ci    int16_t rotateAngle;
133a3e0fd82Sopenharmony_ci    ColorType color;
134a3e0fd82Sopenharmony_ci    OpacityType opaScale;
135a3e0fd82Sopenharmony_ci    float startAngle;
136a3e0fd82Sopenharmony_ci    float endAngle;
137a3e0fd82Sopenharmony_ci    float currentAngle;
138a3e0fd82Sopenharmony_ci    uint16_t radius;
139a3e0fd82Sopenharmony_ci    bool compatibilityMode;
140a3e0fd82Sopenharmony_ci    bool directFlag;
141a3e0fd82Sopenharmony_ci    bool orientationFlag;
142a3e0fd82Sopenharmony_ci    bool hasAnimator;
143a3e0fd82Sopenharmony_ci};
144a3e0fd82Sopenharmony_ci
145a3e0fd82Sopenharmony_ci/**
146a3e0fd82Sopenharmony_ci * @brief Enumerates text orientations.
147a3e0fd82Sopenharmony_ci */
148a3e0fd82Sopenharmony_cienum TextInRange {
149a3e0fd82Sopenharmony_ci    IN_RANGE,
150a3e0fd82Sopenharmony_ci    OUT_RANGE,
151a3e0fd82Sopenharmony_ci    NEED_CLIP
152a3e0fd82Sopenharmony_ci};
153a3e0fd82Sopenharmony_ci
154a3e0fd82Sopenharmony_ci/**
155a3e0fd82Sopenharmony_ci * @brief Enumerates text orientations.
156a3e0fd82Sopenharmony_ci */
157a3e0fd82Sopenharmony_cienum class TextOrientation : uint8_t {
158a3e0fd82Sopenharmony_ci    /** Inside */
159a3e0fd82Sopenharmony_ci    INSIDE,
160a3e0fd82Sopenharmony_ci    /** Outside */
161a3e0fd82Sopenharmony_ci    OUTSIDE,
162a3e0fd82Sopenharmony_ci};
163a3e0fd82Sopenharmony_ci
164a3e0fd82Sopenharmony_cistruct BackgroundColor : public HeapBase {
165a3e0fd82Sopenharmony_ci    int16_t start;
166a3e0fd82Sopenharmony_ci    int16_t end;
167a3e0fd82Sopenharmony_ci    ColorType backgroundColor;
168a3e0fd82Sopenharmony_ci};
169a3e0fd82Sopenharmony_ci
170a3e0fd82Sopenharmony_cistruct ForegroundColor : public HeapBase {
171a3e0fd82Sopenharmony_ci    int16_t start;
172a3e0fd82Sopenharmony_ci    int16_t end;
173a3e0fd82Sopenharmony_ci    ColorType fontColor;
174a3e0fd82Sopenharmony_ci};
175a3e0fd82Sopenharmony_ci
176a3e0fd82Sopenharmony_cistruct LineBackgroundColor : public HeapBase {
177a3e0fd82Sopenharmony_ci    int16_t start;
178a3e0fd82Sopenharmony_ci    int16_t end;
179a3e0fd82Sopenharmony_ci    ColorType linebackgroundColor;
180a3e0fd82Sopenharmony_ci};
181a3e0fd82Sopenharmony_ci
182a3e0fd82Sopenharmony_cistruct LabelLineInfo;
183a3e0fd82Sopenharmony_ci
184a3e0fd82Sopenharmony_ci/**
185a3e0fd82Sopenharmony_ci * @brief Represents the base class of <b>Text</b>, providing the text attribute setting and text drawing
186a3e0fd82Sopenharmony_ci *        capabilities for components that require font display.
187a3e0fd82Sopenharmony_ci *
188a3e0fd82Sopenharmony_ci * @since 1.0
189a3e0fd82Sopenharmony_ci * @version 1.0
190a3e0fd82Sopenharmony_ci */
191a3e0fd82Sopenharmony_ciclass Text : public HeapBase {
192a3e0fd82Sopenharmony_cipublic:
193a3e0fd82Sopenharmony_ci    /** Invalid value for the ellipsis position */
194a3e0fd82Sopenharmony_ci    static constexpr uint16_t TEXT_ELLIPSIS_END_INV = 0xFFFF;
195a3e0fd82Sopenharmony_ci    static constexpr uint16_t TEXT_ELLIPSIS_UNICODE = 0x2026;
196a3e0fd82Sopenharmony_ci
197a3e0fd82Sopenharmony_ci    /**
198a3e0fd82Sopenharmony_ci     * @brief A constructor used to create a <b>Text</b> instance.
199a3e0fd82Sopenharmony_ci     *
200a3e0fd82Sopenharmony_ci     * @since 1.0
201a3e0fd82Sopenharmony_ci     * @version 1.0
202a3e0fd82Sopenharmony_ci     */
203a3e0fd82Sopenharmony_ci    Text();
204a3e0fd82Sopenharmony_ci
205a3e0fd82Sopenharmony_ci    /**
206a3e0fd82Sopenharmony_ci     * @brief A destructor used to delete the <b>Text</b> instance.
207a3e0fd82Sopenharmony_ci     *
208a3e0fd82Sopenharmony_ci     * @since 1.0
209a3e0fd82Sopenharmony_ci     * @version 1.0
210a3e0fd82Sopenharmony_ci     */
211a3e0fd82Sopenharmony_ci    virtual ~Text();
212a3e0fd82Sopenharmony_ci
213a3e0fd82Sopenharmony_ci    /**
214a3e0fd82Sopenharmony_ci     * @brief Sets the content for this text.
215a3e0fd82Sopenharmony_ci     *
216a3e0fd82Sopenharmony_ci     * @param text Indicates the pointer to the text content.
217a3e0fd82Sopenharmony_ci     * @since 1.0
218a3e0fd82Sopenharmony_ci     * @version 1.0
219a3e0fd82Sopenharmony_ci     */
220a3e0fd82Sopenharmony_ci    virtual void SetText(const char* text);
221a3e0fd82Sopenharmony_ci
222a3e0fd82Sopenharmony_ci    /**
223a3e0fd82Sopenharmony_ci     * @brief Sets the SpannableString for this text.
224a3e0fd82Sopenharmony_ci     *
225a3e0fd82Sopenharmony_ci     * @param text Indicates the pointer to the text content.
226a3e0fd82Sopenharmony_ci     * @since 1.0
227a3e0fd82Sopenharmony_ci     * @version 1.0
228a3e0fd82Sopenharmony_ci     */
229a3e0fd82Sopenharmony_ci    void SetSpannableString(const SpannableString* spannableString);
230a3e0fd82Sopenharmony_ci
231a3e0fd82Sopenharmony_ci    /**
232a3e0fd82Sopenharmony_ci     * @brief Obtains the content of this text.
233a3e0fd82Sopenharmony_ci     *
234a3e0fd82Sopenharmony_ci     * @return Returns the text content.
235a3e0fd82Sopenharmony_ci     * @since 1.0
236a3e0fd82Sopenharmony_ci     * @version 1.0
237a3e0fd82Sopenharmony_ci     */
238a3e0fd82Sopenharmony_ci    const char* GetText() const
239a3e0fd82Sopenharmony_ci    {
240a3e0fd82Sopenharmony_ci        return text_;
241a3e0fd82Sopenharmony_ci    }
242a3e0fd82Sopenharmony_ci
243a3e0fd82Sopenharmony_ci    /**
244a3e0fd82Sopenharmony_ci     * @brief Sets the font name and size.
245a3e0fd82Sopenharmony_ci     *
246a3e0fd82Sopenharmony_ci     * @param name Indicates the pointer to the font name.
247a3e0fd82Sopenharmony_ci     * @param size Indicates the font size to set.
248a3e0fd82Sopenharmony_ci     * @since 1.0
249a3e0fd82Sopenharmony_ci     * @version 1.0
250a3e0fd82Sopenharmony_ci     */
251a3e0fd82Sopenharmony_ci    void SetFont(const char* name, uint8_t size);
252a3e0fd82Sopenharmony_ci
253a3e0fd82Sopenharmony_ci    static void SetFont(const char* name, uint8_t size, char*& destName, uint8_t& destSize);
254a3e0fd82Sopenharmony_ci
255a3e0fd82Sopenharmony_ci    /**
256a3e0fd82Sopenharmony_ci     * @brief Sets the font ID.
257a3e0fd82Sopenharmony_ci     *
258a3e0fd82Sopenharmony_ci     * @param fontId Indicates the font ID to set.
259a3e0fd82Sopenharmony_ci     * @since 1.0
260a3e0fd82Sopenharmony_ci     * @version 1.0
261a3e0fd82Sopenharmony_ci     */
262a3e0fd82Sopenharmony_ci    void SetFontId(uint16_t fontId);
263a3e0fd82Sopenharmony_ci
264a3e0fd82Sopenharmony_ci    /**
265a3e0fd82Sopenharmony_ci     * @brief Obtains the font ID.
266a3e0fd82Sopenharmony_ci     *
267a3e0fd82Sopenharmony_ci     * @return Returns the front ID.
268a3e0fd82Sopenharmony_ci     * @since 1.0
269a3e0fd82Sopenharmony_ci     * @version 1.0
270a3e0fd82Sopenharmony_ci     */
271a3e0fd82Sopenharmony_ci    uint16_t GetFontId() const
272a3e0fd82Sopenharmony_ci    {
273a3e0fd82Sopenharmony_ci        return fontId_;
274a3e0fd82Sopenharmony_ci    }
275a3e0fd82Sopenharmony_ci
276a3e0fd82Sopenharmony_ci    /**
277a3e0fd82Sopenharmony_ci     * @brief Obtains the font size.
278a3e0fd82Sopenharmony_ci     *
279a3e0fd82Sopenharmony_ci     * @return Returns the front size.
280a3e0fd82Sopenharmony_ci     * @since 1.0
281a3e0fd82Sopenharmony_ci     * @version 1.0
282a3e0fd82Sopenharmony_ci     */
283a3e0fd82Sopenharmony_ci    uint8_t GetFontSize() const
284a3e0fd82Sopenharmony_ci    {
285a3e0fd82Sopenharmony_ci        return fontSize_;
286a3e0fd82Sopenharmony_ci    }
287a3e0fd82Sopenharmony_ci
288a3e0fd82Sopenharmony_ci    /**
289a3e0fd82Sopenharmony_ci     * @brief Sets the direction for this text.
290a3e0fd82Sopenharmony_ci     *
291a3e0fd82Sopenharmony_ci     * @param direct Indicates the text direction, as defined in {@link UITextLanguageDirect}.
292a3e0fd82Sopenharmony_ci     * @since 1.0
293a3e0fd82Sopenharmony_ci     * @version 1.0
294a3e0fd82Sopenharmony_ci     */
295a3e0fd82Sopenharmony_ci    void SetDirect(UITextLanguageDirect direct)
296a3e0fd82Sopenharmony_ci    {
297a3e0fd82Sopenharmony_ci        direct_ = direct;
298a3e0fd82Sopenharmony_ci    }
299a3e0fd82Sopenharmony_ci
300a3e0fd82Sopenharmony_ci    /**
301a3e0fd82Sopenharmony_ci     * @brief Obtains the direction of this text.
302a3e0fd82Sopenharmony_ci     *
303a3e0fd82Sopenharmony_ci     * @return Returns the text direction, as defined in {@link UITextLanguageDirect}.
304a3e0fd82Sopenharmony_ci     * @since 1.0
305a3e0fd82Sopenharmony_ci     * @version 1.0
306a3e0fd82Sopenharmony_ci     */
307a3e0fd82Sopenharmony_ci    UITextLanguageDirect GetDirect() const
308a3e0fd82Sopenharmony_ci    {
309a3e0fd82Sopenharmony_ci        return static_cast<UITextLanguageDirect>(direct_);
310a3e0fd82Sopenharmony_ci    }
311a3e0fd82Sopenharmony_ci
312a3e0fd82Sopenharmony_ci    /**
313a3e0fd82Sopenharmony_ci     * @brief Sets the alignment mode for this text.
314a3e0fd82Sopenharmony_ci     *
315a3e0fd82Sopenharmony_ci     * @param horizontalAlign Indicates the horizontal alignment mode to set,
316a3e0fd82Sopenharmony_ci     *                        which can be {@link TEXT_ALIGNMENT_LEFT},
317a3e0fd82Sopenharmony_ci     *                        {@link TEXT_ALIGNMENT_CENTER}, or {@link TEXT_ALIGNMENT_RIGHT}.
318a3e0fd82Sopenharmony_ci     * @param verticalAlign Indicates the vertical alignment mode to set, which can be
319a3e0fd82Sopenharmony_ci     *                      {@link TEXT_ALIGNMENT_TOP} (default mode), {@link TEXT_ALIGNMENT_CENTER},
320a3e0fd82Sopenharmony_ci     *                      or {@link TEXT_ALIGNMENT_BOTTOM}.
321a3e0fd82Sopenharmony_ci     * @since 1.0
322a3e0fd82Sopenharmony_ci     * @version 1.0
323a3e0fd82Sopenharmony_ci     */
324a3e0fd82Sopenharmony_ci    void SetAlign(UITextLanguageAlignment horizontalAlign, UITextLanguageAlignment verticalAlign = TEXT_ALIGNMENT_TOP)
325a3e0fd82Sopenharmony_ci    {
326a3e0fd82Sopenharmony_ci        if ((horizontalAlign_ != horizontalAlign) || (verticalAlign_ != verticalAlign)) {
327a3e0fd82Sopenharmony_ci            needRefresh_ = true;
328a3e0fd82Sopenharmony_ci            horizontalAlign_ = horizontalAlign;
329a3e0fd82Sopenharmony_ci            verticalAlign_ = verticalAlign;
330a3e0fd82Sopenharmony_ci        }
331a3e0fd82Sopenharmony_ci    }
332a3e0fd82Sopenharmony_ci
333a3e0fd82Sopenharmony_ci    /**
334a3e0fd82Sopenharmony_ci     * @brief Obtains the horizontal alignment mode.
335a3e0fd82Sopenharmony_ci     *
336a3e0fd82Sopenharmony_ci     * @return Returns the horizontal alignment mode.
337a3e0fd82Sopenharmony_ci     * @since 1.0
338a3e0fd82Sopenharmony_ci     * @version 1.0
339a3e0fd82Sopenharmony_ci     */
340a3e0fd82Sopenharmony_ci    UITextLanguageAlignment GetHorAlign() const
341a3e0fd82Sopenharmony_ci    {
342a3e0fd82Sopenharmony_ci        return static_cast<UITextLanguageAlignment>(horizontalAlign_);
343a3e0fd82Sopenharmony_ci    }
344a3e0fd82Sopenharmony_ci
345a3e0fd82Sopenharmony_ci    /**
346a3e0fd82Sopenharmony_ci     * @brief Obtains the vertical alignment mode.
347a3e0fd82Sopenharmony_ci     *
348a3e0fd82Sopenharmony_ci     * @return Returns the vertical alignment mode.
349a3e0fd82Sopenharmony_ci     * @since 1.0
350a3e0fd82Sopenharmony_ci     * @version 1.0
351a3e0fd82Sopenharmony_ci     */
352a3e0fd82Sopenharmony_ci    UITextLanguageAlignment GetVerAlign() const
353a3e0fd82Sopenharmony_ci    {
354a3e0fd82Sopenharmony_ci        return static_cast<UITextLanguageAlignment>(verticalAlign_);
355a3e0fd82Sopenharmony_ci    }
356a3e0fd82Sopenharmony_ci
357a3e0fd82Sopenharmony_ci    /**
358a3e0fd82Sopenharmony_ci     * @brief Obtains the size of this text.
359a3e0fd82Sopenharmony_ci     *
360a3e0fd82Sopenharmony_ci     * @return Returns the text size.
361a3e0fd82Sopenharmony_ci     * @since 1.0
362a3e0fd82Sopenharmony_ci     * @version 1.0
363a3e0fd82Sopenharmony_ci     */
364a3e0fd82Sopenharmony_ci    Point GetTextSize() const
365a3e0fd82Sopenharmony_ci    {
366a3e0fd82Sopenharmony_ci        return textSize_;
367a3e0fd82Sopenharmony_ci    }
368a3e0fd82Sopenharmony_ci
369a3e0fd82Sopenharmony_ci    virtual void ReMeasureTextSize(const Rect& textRect, const Style& style);
370a3e0fd82Sopenharmony_ci
371a3e0fd82Sopenharmony_ci    void ReMeasureTextWidthInEllipsisMode(const Rect& textRect, const Style& style, uint16_t ellipsisIndex);
372a3e0fd82Sopenharmony_ci
373a3e0fd82Sopenharmony_ci    void OnDraw(BufferInfo& gfxDstBuffer,
374a3e0fd82Sopenharmony_ci                const Rect& invalidatedArea,
375a3e0fd82Sopenharmony_ci                const Rect& viewOrigRect,
376a3e0fd82Sopenharmony_ci                const Rect& textRect,
377a3e0fd82Sopenharmony_ci                int16_t offsetX,
378a3e0fd82Sopenharmony_ci                const Style& style,
379a3e0fd82Sopenharmony_ci                uint16_t ellipsisIndex,
380a3e0fd82Sopenharmony_ci                OpacityType opaScale);
381a3e0fd82Sopenharmony_ci
382a3e0fd82Sopenharmony_ci    /**
383a3e0fd82Sopenharmony_ci     * @brief Sets whether to adapt the component width to this text.
384a3e0fd82Sopenharmony_ci     *
385a3e0fd82Sopenharmony_ci     * @param expand Specifies whether to adapt the component width to this text. The value <b>true</b> indicates
386a3e0fd82Sopenharmony_ci     *               that the component width will adapt to this text, and <b>false</b> indicates not.
387a3e0fd82Sopenharmony_ci     * @since 1.0
388a3e0fd82Sopenharmony_ci     * @version 1.0
389a3e0fd82Sopenharmony_ci     */
390a3e0fd82Sopenharmony_ci    void SetExpandWidth(bool expand)
391a3e0fd82Sopenharmony_ci    {
392a3e0fd82Sopenharmony_ci        expandWidth_ = expand;
393a3e0fd82Sopenharmony_ci    }
394a3e0fd82Sopenharmony_ci
395a3e0fd82Sopenharmony_ci    /**
396a3e0fd82Sopenharmony_ci     * @brief Checks whether the component width adapts to this text.
397a3e0fd82Sopenharmony_ci     *
398a3e0fd82Sopenharmony_ci     * @return Returns <b>true</b> if the component width adapts to this text; returns <b>false</b> otherwise.
399a3e0fd82Sopenharmony_ci     * @since 1.0
400a3e0fd82Sopenharmony_ci     * @version 1.0
401a3e0fd82Sopenharmony_ci     */
402a3e0fd82Sopenharmony_ci    bool IsExpandWidth() const
403a3e0fd82Sopenharmony_ci    {
404a3e0fd82Sopenharmony_ci        return expandWidth_;
405a3e0fd82Sopenharmony_ci    }
406a3e0fd82Sopenharmony_ci
407a3e0fd82Sopenharmony_ci    /**
408a3e0fd82Sopenharmony_ci     * @brief Sets whether to adapt the component height to this text.
409a3e0fd82Sopenharmony_ci     *
410a3e0fd82Sopenharmony_ci     * @param expand Specifies whether to adapt the component height to this text. The value <b>true</b> indicates
411a3e0fd82Sopenharmony_ci     *               that the component height will adapt to this text, and <b>false</b> indicates not.
412a3e0fd82Sopenharmony_ci     * @since 1.0
413a3e0fd82Sopenharmony_ci     * @version 1.0
414a3e0fd82Sopenharmony_ci     */
415a3e0fd82Sopenharmony_ci    void SetExpandHeight(bool expand)
416a3e0fd82Sopenharmony_ci    {
417a3e0fd82Sopenharmony_ci        expandHeight_ = expand;
418a3e0fd82Sopenharmony_ci    }
419a3e0fd82Sopenharmony_ci
420a3e0fd82Sopenharmony_ci    /**
421a3e0fd82Sopenharmony_ci     * @brief Checks whether the component height adapts to this text.
422a3e0fd82Sopenharmony_ci     *
423a3e0fd82Sopenharmony_ci     * @return Returns <b>true</b> if the component height adapts to this text; returns <b>false</b> otherwise.
424a3e0fd82Sopenharmony_ci     * @since 1.0
425a3e0fd82Sopenharmony_ci     * @version 1.0
426a3e0fd82Sopenharmony_ci     */
427a3e0fd82Sopenharmony_ci    bool IsExpandHeight() const
428a3e0fd82Sopenharmony_ci    {
429a3e0fd82Sopenharmony_ci        return expandHeight_;
430a3e0fd82Sopenharmony_ci    }
431a3e0fd82Sopenharmony_ci
432a3e0fd82Sopenharmony_ci    bool IsNeedRefresh() const
433a3e0fd82Sopenharmony_ci    {
434a3e0fd82Sopenharmony_ci        return needRefresh_;
435a3e0fd82Sopenharmony_ci    }
436a3e0fd82Sopenharmony_ci
437a3e0fd82Sopenharmony_ci    /**
438a3e0fd82Sopenharmony_ci     * @brief Obtains the index of the character from where text will be replaced by ellipses based on
439a3e0fd82Sopenharmony_ci     *        the text rectangle and style.
440a3e0fd82Sopenharmony_ci     *
441a3e0fd82Sopenharmony_ci     * @param textRect Indicates the text rectangle.
442a3e0fd82Sopenharmony_ci     * @param style Indicates the text style.
443a3e0fd82Sopenharmony_ci     * @since 1.0
444a3e0fd82Sopenharmony_ci     * @version 1.0
445a3e0fd82Sopenharmony_ci     */
446a3e0fd82Sopenharmony_ci    uint16_t GetEllipsisIndex(const Rect& textRect, const Style& style);
447a3e0fd82Sopenharmony_ci
448a3e0fd82Sopenharmony_ci    /**
449a3e0fd82Sopenharmony_ci     * @brief Get the GetShapingFontId of text
450a3e0fd82Sopenharmony_ci     *
451a3e0fd82Sopenharmony_ci     * @return Return ShapingFontId
452a3e0fd82Sopenharmony_ci     */
453a3e0fd82Sopenharmony_ci    virtual uint8_t GetShapingFontId() const
454a3e0fd82Sopenharmony_ci    {
455a3e0fd82Sopenharmony_ci        return 0;
456a3e0fd82Sopenharmony_ci    }
457a3e0fd82Sopenharmony_ci
458a3e0fd82Sopenharmony_ci    /**
459a3e0fd82Sopenharmony_ci     * @brief Get the GetCodePointNum of text
460a3e0fd82Sopenharmony_ci     *
461a3e0fd82Sopenharmony_ci     * @return Return num of CodePoints
462a3e0fd82Sopenharmony_ci     */
463a3e0fd82Sopenharmony_ci    virtual uint16_t GetCodePointNum() const
464a3e0fd82Sopenharmony_ci    {
465a3e0fd82Sopenharmony_ci        return 0;
466a3e0fd82Sopenharmony_ci    }
467a3e0fd82Sopenharmony_ci
468a3e0fd82Sopenharmony_ci    /**
469a3e0fd82Sopenharmony_ci     * @brief Get the GetCodePoints of text
470a3e0fd82Sopenharmony_ci     *
471a3e0fd82Sopenharmony_ci     * @return Return CodePoints of text
472a3e0fd82Sopenharmony_ci     */
473a3e0fd82Sopenharmony_ci    virtual uint32_t* GetCodePoints() const
474a3e0fd82Sopenharmony_ci    {
475a3e0fd82Sopenharmony_ci        return nullptr;
476a3e0fd82Sopenharmony_ci    }
477a3e0fd82Sopenharmony_ci
478a3e0fd82Sopenharmony_ci    void SetSupportBaseLine(bool baseLine)
479a3e0fd82Sopenharmony_ci    {
480a3e0fd82Sopenharmony_ci        baseLine_ = baseLine;
481a3e0fd82Sopenharmony_ci    }
482a3e0fd82Sopenharmony_ci
483a3e0fd82Sopenharmony_ci    void SetBackgroundColorSpan(ColorType backgroundColor, int16_t start, int16_t end)
484a3e0fd82Sopenharmony_ci    {
485a3e0fd82Sopenharmony_ci        if (spannableString_ == nullptr) {
486a3e0fd82Sopenharmony_ci            spannableString_ = new SpannableString();
487a3e0fd82Sopenharmony_ci        }
488a3e0fd82Sopenharmony_ci        spannableString_->SetBackgroundColor(backgroundColor, (uint16_t)start, (uint16_t)end);
489a3e0fd82Sopenharmony_ci    }
490a3e0fd82Sopenharmony_ci
491a3e0fd82Sopenharmony_ci    List<BackgroundColor> GetBackgroundColorSpan()
492a3e0fd82Sopenharmony_ci    {
493a3e0fd82Sopenharmony_ci        return backgroundColor_;
494a3e0fd82Sopenharmony_ci    }
495a3e0fd82Sopenharmony_ci
496a3e0fd82Sopenharmony_ci    void SetForegroundColorSpan(ColorType fontColor, int16_t start, int16_t end)
497a3e0fd82Sopenharmony_ci    {
498a3e0fd82Sopenharmony_ci        if (spannableString_ == nullptr) {
499a3e0fd82Sopenharmony_ci            spannableString_ = new SpannableString();
500a3e0fd82Sopenharmony_ci        }
501a3e0fd82Sopenharmony_ci        spannableString_->SetForegroundColor(fontColor, (uint16_t)start, (uint16_t)end);
502a3e0fd82Sopenharmony_ci    }
503a3e0fd82Sopenharmony_ci
504a3e0fd82Sopenharmony_ci    List<ForegroundColor> GetForegroundColorSpan()
505a3e0fd82Sopenharmony_ci    {
506a3e0fd82Sopenharmony_ci        return foregroundColor_;
507a3e0fd82Sopenharmony_ci    }
508a3e0fd82Sopenharmony_ci
509a3e0fd82Sopenharmony_ci    void SetLineBackgroundSpan(ColorType linebackgroundColor, int16_t start, int16_t end)
510a3e0fd82Sopenharmony_ci    {
511a3e0fd82Sopenharmony_ci        if (spannableString_ == nullptr) {
512a3e0fd82Sopenharmony_ci            spannableString_ = new SpannableString();
513a3e0fd82Sopenharmony_ci        }
514a3e0fd82Sopenharmony_ci        spannableString_->SetLineBackgroundColor(linebackgroundColor, (uint16_t)start, (uint16_t)end);
515a3e0fd82Sopenharmony_ci    }
516a3e0fd82Sopenharmony_ci
517a3e0fd82Sopenharmony_ci    List<LineBackgroundColor> GetLineBackgroundSpan()
518a3e0fd82Sopenharmony_ci    {
519a3e0fd82Sopenharmony_ci        return linebackgroundColor_;
520a3e0fd82Sopenharmony_ci    }
521a3e0fd82Sopenharmony_ci
522a3e0fd82Sopenharmony_ci    void SetAbsoluteSizeSpan(uint16_t start, uint16_t end, uint8_t size);
523a3e0fd82Sopenharmony_ci    void SetRelativeSizeSpan(uint16_t start, uint16_t end, float size);
524a3e0fd82Sopenharmony_ci    virtual uint16_t GetLetterIndexByLinePosition(const Style& style, int16_t contentWidth,
525a3e0fd82Sopenharmony_ci                                                  const int16_t& posX, int16_t offsetX);
526a3e0fd82Sopenharmony_ci    virtual uint16_t GetPosXByLetterIndex(const Rect& textRect, const Style& style,
527a3e0fd82Sopenharmony_ci                                          uint16_t beginIndex, uint16_t count);
528a3e0fd82Sopenharmony_ci
529a3e0fd82Sopenharmony_ci    void SetEliminateTrailingSpaces(bool eliminateTrailingSpaces)
530a3e0fd82Sopenharmony_ci    {
531a3e0fd82Sopenharmony_ci        eliminateTrailingSpaces_ = eliminateTrailingSpaces;
532a3e0fd82Sopenharmony_ci    }
533a3e0fd82Sopenharmony_ci
534a3e0fd82Sopenharmony_ci    bool IsEliminateTrailingSpaces()
535a3e0fd82Sopenharmony_ci    {
536a3e0fd82Sopenharmony_ci        return eliminateTrailingSpaces_;
537a3e0fd82Sopenharmony_ci    }
538a3e0fd82Sopenharmony_ci    uint16_t GetSizeSpan()
539a3e0fd82Sopenharmony_ci    {
540a3e0fd82Sopenharmony_ci        return characterSize_;
541a3e0fd82Sopenharmony_ci    }
542a3e0fd82Sopenharmony_ci
543a3e0fd82Sopenharmony_ci    /**
544a3e0fd82Sopenharmony_ci     * @brief Get next character full dispaly offset.
545a3e0fd82Sopenharmony_ci     *
546a3e0fd82Sopenharmony_ci     * @param textRect Indicates size of input box.
547a3e0fd82Sopenharmony_ci     * @param style Indicates the style of text.
548a3e0fd82Sopenharmony_ci     * @param beginIndex Indicates index at the beginning of the text.
549a3e0fd82Sopenharmony_ci     * @param num Indicates num of text.
550a3e0fd82Sopenharmony_ci     *
551a3e0fd82Sopenharmony_ci     * @return Return text offset.
552a3e0fd82Sopenharmony_ci     *
553a3e0fd82Sopenharmony_ci     */
554a3e0fd82Sopenharmony_ci    virtual uint16_t GetNextCharacterFullDispalyOffset(const Rect& textRect,
555a3e0fd82Sopenharmony_ci        const Style& style, uint16_t beginIndex, uint16_t num);
556a3e0fd82Sopenharmony_ci
557a3e0fd82Sopenharmony_ci    /**
558a3e0fd82Sopenharmony_ci     * @brief Obtains the width of the meta text.
559a3e0fd82Sopenharmony_ci     *
560a3e0fd82Sopenharmony_ci     * @param style Indicates the style of text.
561a3e0fd82Sopenharmony_ci     *
562a3e0fd82Sopenharmony_ci     * @return Return meta text width.
563a3e0fd82Sopenharmony_ci     */
564a3e0fd82Sopenharmony_ci    virtual int16_t GetMetaTextWidth(const Style& style);
565a3e0fd82Sopenharmony_ci
566a3e0fd82Sopenharmony_ciprotected:
567a3e0fd82Sopenharmony_ci    struct TextLine {
568a3e0fd82Sopenharmony_ci        uint16_t lineBytes;
569a3e0fd82Sopenharmony_ci        uint16_t linePixelWidth;
570a3e0fd82Sopenharmony_ci    };
571a3e0fd82Sopenharmony_ci
572a3e0fd82Sopenharmony_ci    /** Maximum number of lines */
573a3e0fd82Sopenharmony_ci    static constexpr uint16_t MAX_LINE_COUNT = 50;
574a3e0fd82Sopenharmony_ci    static TextLine textLine_[MAX_LINE_COUNT];
575a3e0fd82Sopenharmony_ci
576a3e0fd82Sopenharmony_ci    static constexpr const char* TEXT_ELLIPSIS = "…";
577a3e0fd82Sopenharmony_ci
578a3e0fd82Sopenharmony_ci    virtual uint32_t GetTextStrLen();
579a3e0fd82Sopenharmony_ci
580a3e0fd82Sopenharmony_ci    virtual uint32_t
581a3e0fd82Sopenharmony_ci        GetTextLine(uint32_t begin, uint32_t textLen, int16_t width, uint16_t lineNum, uint8_t letterSpace,
582a3e0fd82Sopenharmony_ci                    uint16_t& letterIndex, SpannableString* spannableString, TextLine& textLine);
583a3e0fd82Sopenharmony_ci
584a3e0fd82Sopenharmony_ci    virtual uint16_t GetLetterIndexByPosition(const Rect& textRect, const Style& style, const Point& pos);
585a3e0fd82Sopenharmony_ci
586a3e0fd82Sopenharmony_ci    virtual void Draw(BufferInfo& gfxDstBuffer,
587a3e0fd82Sopenharmony_ci                      const Rect& mask,
588a3e0fd82Sopenharmony_ci                      const Rect& coords,
589a3e0fd82Sopenharmony_ci                      const Style& style,
590a3e0fd82Sopenharmony_ci                      int16_t offsetX,
591a3e0fd82Sopenharmony_ci                      uint16_t ellipsisIndex,
592a3e0fd82Sopenharmony_ci                      OpacityType opaScale);
593a3e0fd82Sopenharmony_ci
594a3e0fd82Sopenharmony_ci    uint16_t GetLine(int16_t width, uint8_t letterSpace, uint16_t ellipsisIndex, uint32_t& maxLineBytes);
595a3e0fd82Sopenharmony_ci    int16_t TextPositionY(const Rect& textRect, int16_t textHeight);
596a3e0fd82Sopenharmony_ci    int16_t LineStartPos(const Rect& textRect, uint16_t lineWidth);
597a3e0fd82Sopenharmony_ci    void DrawEllipsis(BufferInfo& gfxDstBuffer, LabelLineInfo& labelLine, uint16_t& letterIndex);
598a3e0fd82Sopenharmony_ci    uint32_t CalculateLineWithEllipsis(uint32_t begin, uint32_t textLen, int16_t width,
599a3e0fd82Sopenharmony_ci                                       uint8_t letterSpace, uint16_t& lineNum,
600a3e0fd82Sopenharmony_ci                                       uint16_t& letterIndex,
601a3e0fd82Sopenharmony_ci                                       SpannableString* spannableString);
602a3e0fd82Sopenharmony_ci    uint16_t GetSpanFontIdBySize(uint8_t size);
603a3e0fd82Sopenharmony_ci    SpannableString* CreateSpannableString();
604a3e0fd82Sopenharmony_ci#if defined(ENABLE_TEXT_STYLE) && ENABLE_TEXT_STYLE
605a3e0fd82Sopenharmony_ci    TextStyle* textStyles_;
606a3e0fd82Sopenharmony_ci#endif
607a3e0fd82Sopenharmony_ci    char* text_;
608a3e0fd82Sopenharmony_ci    uint16_t fontId_;
609a3e0fd82Sopenharmony_ci    uint8_t fontSize_; // Only the vector font library has a valid value.
610a3e0fd82Sopenharmony_ci    Point textSize_;
611a3e0fd82Sopenharmony_ci    bool needRefresh_ : 1;
612a3e0fd82Sopenharmony_ci    bool expandWidth_ : 1;
613a3e0fd82Sopenharmony_ci    bool expandHeight_ : 1;
614a3e0fd82Sopenharmony_ci    bool baseLine_ : 1;
615a3e0fd82Sopenharmony_ci    uint8_t direct_ : 4; // UITextLanguageDirect
616a3e0fd82Sopenharmony_ci    List<BackgroundColor> backgroundColor_;
617a3e0fd82Sopenharmony_ci    List<ForegroundColor> foregroundColor_;
618a3e0fd82Sopenharmony_ci    List<LineBackgroundColor> linebackgroundColor_;
619a3e0fd82Sopenharmony_ci    uint32_t characterSize_;
620a3e0fd82Sopenharmony_ci    SpannableString* spannableString_;
621a3e0fd82Sopenharmony_ci    TextLine preIndexLine_;
622a3e0fd82Sopenharmony_ci
623a3e0fd82Sopenharmony_ciprivate:
624a3e0fd82Sopenharmony_ci    uint8_t horizontalAlign_ : 4; // UITextLanguageAlignment
625a3e0fd82Sopenharmony_ci    uint8_t verticalAlign_ : 4;   // UITextLanguageAlignment
626a3e0fd82Sopenharmony_ci    bool eliminateTrailingSpaces_;
627a3e0fd82Sopenharmony_ci    static constexpr uint8_t FONT_ID_MAX = 0xFF;
628a3e0fd82Sopenharmony_ci#if defined(ENABLE_ICU) && ENABLE_ICU
629a3e0fd82Sopenharmony_ci    void SetLineBytes(uint16_t& lineBytes, uint16_t lineBegin);
630a3e0fd82Sopenharmony_ci#endif
631a3e0fd82Sopenharmony_ci    void CalculatedCurLineHeight(int16_t& lineHeight, int16_t& curLineHeight,
632a3e0fd82Sopenharmony_ci                                 uint16_t fontHeight, const Style& style, uint16_t lineMaxHeight);
633a3e0fd82Sopenharmony_ci    void SetNextLineBegin(const Style& style, uint16_t lineMaxHeight, int16_t& curLineHeight, Point& pos,
634a3e0fd82Sopenharmony_ci                          int16_t& tempLetterIndex, int16_t& lineHeight, uint16_t& lineBegin, uint16_t letterIndex);
635a3e0fd82Sopenharmony_ci    Point GetPos(int16_t& lineHeight, const Style& style, uint16_t& lineCount, const Rect& coords);
636a3e0fd82Sopenharmony_ci};
637a3e0fd82Sopenharmony_ci} // namespace OHOS
638a3e0fd82Sopenharmony_ci#endif // GRAPHIC_LITE_TEXT_H
639