1 /*
2  * Copyright (c) 2020-2022 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #ifndef GRAPHIC_LITE_TYPE_TEXT_H
17 #define GRAPHIC_LITE_TYPE_TEXT_H
18 #include "common/text.h"
19 #include "graphic_config.h"
20 #ifndef _FONT_TOOL
21 #include "components/ui_arc_label.h"
22 #include "font/ui_font_header.h"
23 #include "gfx_utils/geometry2d.h"
24 #endif
25 
26 namespace OHOS {
27 #ifndef _FONT_TOOL
28 class TypedText : public HeapBase {
29 public:
30     static constexpr uint32_t MAX_UINT16_LOW_SCOPE = 0xFFFF;
31     static constexpr uint32_t MAX_UINT16_HIGH_SCOPE = 0xEFFFF;
32     static constexpr uint32_t UTF16_MASK = 0xFC00;
33     static constexpr uint32_t UTF16_LOW_MASK = 0x03FF;
34     static constexpr uint32_t UTF16_LOW_PARAM = 56320; // 0xDC00
35     static constexpr uint32_t UTF16_HIGH_PARAM1 = 55296; // 0xD800
36     static constexpr uint8_t UTF16_HIGH_PARAM2 = 64;
37     static constexpr uint8_t UTF16_HIGH_SHIFT = 10;
38     static constexpr uint8_t UTF8_TO_UNICODE_SHIFT1 = 6;
39     static constexpr uint8_t UTF8_TO_UNICODE_SHIFT2 = 12;
40     static constexpr uint8_t UTF8_TO_UNICODE_SHIFT3 = 18;
41 
42     static Point GetTextSize(const char* text,
43                              uint16_t fontId,
44                              uint8_t fontSize,
45                              int16_t letterSpace,
46                              int16_t lineHeight,
47                              int16_t maxWidth,
48                              int8_t lineSpace,
49                              SpannableString* spannableString = nullptr,
50                              bool eliminateTrailingSpaces = false);
51 
52     static uint32_t GetNextLine(const char* text,
53                                 uint16_t fontId,
54                                 uint8_t fontSize,
55                                 int16_t letterSpace,
56                                 int16_t maxWidth);
57 
58     static int16_t GetTextWidth(const char* text,
59                                 uint16_t fontId,
60                                 uint8_t fontSize,
61                                 uint16_t length,
62                                 int16_t letterSpace,
63                                 uint16_t beginUTF8Index = 0,
64                                 uint16_t count = UINT16_MAX);
65 
66     static Rect GetArcTextRect(const char* text,
67                                 uint16_t fontId,
68                                 uint8_t fontSize,
69                                const Point& arcCenter,
70                                int16_t letterSpace,
71                                TextOrientation orientation,
72                                const ArcTextInfo& arcTextInfo);
73 
74     static float GetAngleForArcLen(float len,
75                                    uint16_t height,
76                                    uint16_t radius,
77                                    UITextLanguageDirect direct,
78                                    TextOrientation orientation);
79 
80     static float GetAngleForArcLen(uint16_t letterWidth, int16_t letterSpace, uint16_t radius);
81 
82     static void GetArcLetterPos(const Point& arcCenter, uint16_t radius, float angle, float& posX, float& posY);
83 
84 #else  // _FONT_TOOL
85 class TypedText {
86 public:
87 #endif // _FONT_TOOL
88     static uint32_t GetUTF8Next(const char* text, uint32_t i, uint32_t& j);
89     static uint8_t GetUTF8OneCharacterSize(const char* str);
90     static uint32_t GetByteIndexFromUTF8Id(const char* text, uint32_t utf8Id);
91     static uint32_t GetUTF8CharacterSize(const char* text, uint32_t byteIndex = UINT32_MAX);
92     static void Utf8ToUtf16(const char* utf8Str, uint16_t* utf16Str, uint32_t len);
93     static uint16_t Utf16ToUtf32(const uint16_t* utf16Str, uint32_t* utf32Str, uint32_t len);
94     static int32_t Utf16ToUtf32Word(const uint16_t* src, uint32_t& des);
95     static uint32_t GetUtf16Cnt(const char* utf8Str, uint32_t maxLength = INT32_MAX);
96 
97     static bool IsEmoji(uint32_t codePoint);
98     static bool IsEmojiModifier(uint32_t codePoint);
99     static bool IsEmojiBase(uint32_t codePoint);
100 
101     static bool IsColourWord(uint32_t codePoint, uint16_t fontId, uint8_t fontSize);
102 
103 private:
104     static bool GetWrapPoint(const char* text, uint32_t& breakPoint);
105 };
106 } // namespace OHOS
107 #endif // GRAPHIC_LITE_TYPE_TEXT_H
108