1 /*
2  * Copyright (c) 2024 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 FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_PATTERNS_TEXT_SPAN_TLV_UTIL_H
17 #define FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_PATTERNS_TEXT_SPAN_TLV_UTIL_H
18 
19 #include <string>
20 #include <sstream>
21 #include <utility>
22 #include <vector>
23 
24 #include "base/geometry/dimension.h"
25 #include "base/image/pixel_map.h"
26 #include "core/components/common/properties/color.h"
27 #include "core/components/common/properties/shadow.h"
28 #include "core/components/common/properties/text_style.h"
29 #include "core/components_ng/pattern/text/span_node.h"
30 #include "core/components_ng/render/paragraph.h"
31 
32 namespace OHOS::Ace {
33 
34 constexpr Dimension ILLEGAL_DIMENSION_VALUE = Dimension(-100.0);
35 
36 constexpr uint8_t TLV_VARINT_BITS = 7;
37 constexpr uint8_t TLV_VARINT_MASK = 0x7F;
38 constexpr uint8_t TLV_VARINT_MORE = 0x80;
39 constexpr uint8_t TLV_END = 0x00;
40 constexpr uint8_t TLV_SPAN_STRING_CONTENT = 0x01;
41 constexpr uint8_t TLV_SPAN_STRING_SPANS = 0x02;
42 
43 constexpr uint8_t TLV_STRING_TAG = 0x20;
44 constexpr uint8_t TLV_DOUBLE_TAG = 0x21;
45 constexpr uint8_t TLV_COLOR_TAG = 0x22;
46 constexpr uint8_t TLV_TEXTSHADOW_TAG = 0x23;
47 constexpr uint8_t TLV_DIMENSION_TAG = 0x24;
48 constexpr uint8_t TLV_FONTFAMILIES_TAG = 0x25;
49 
50 constexpr uint8_t TLV_TEXTSHADOWS_TAG = 0x26;
51 constexpr uint8_t TLV_BORDERRADIUS_TAG = 0x27;
52 constexpr uint8_t TLV_PIXEL_MAP_TAG = 0x28;
53 constexpr uint8_t TLV_FONTFEATURE_TAG = 0x29;
54 constexpr uint8_t TLV_CALCDIMENSION_TAG = 0x2A;
55 constexpr uint8_t TLV_CALCLENGTH_TAG = 0x2B;
56 
57 constexpr uint8_t TLV_VERTICALALIGN_TAG = 0x2C;
58 constexpr uint8_t TLV_IMAGEFIT_TAG = 0x2D;
59 constexpr uint8_t TLV_FONTWEIGHT_TAG = 0x2E;
60 constexpr uint8_t TLV_ITALICSFONTSTYLE_TAG = 0x2F;
61 constexpr uint8_t TLV_TEXTDECORATION_TAG = 0x30;
62 constexpr uint8_t TLV_TEXTDECORATIONSTYLE_TAG = 0x31;
63 constexpr uint8_t TLV_TEXTCASE_TAG = 0x32;
64 constexpr uint8_t TLV_TEXTBASELINE_TAG = 0x33;
65 constexpr uint8_t TLV_TEXTOVERFLOW_TAG = 0x34;
66 constexpr uint8_t TLV_TEXTALIGN_TAG = 0x35;
67 constexpr uint8_t TLV_TEXTHEIGHTADAPTIVEPOLICY_TAG = 0x36;
68 constexpr uint8_t TLV_WORDBREAK_TAG = 0x37;
69 constexpr uint8_t TLV_LINEBREAKSTRATEGY_TAG = 0x38;
70 constexpr uint8_t TLV_ELLIPSISMODE_TAG = 0x39;
71 
72 constexpr uint8_t TLV_IMAGESPANATTRIBUTE_TAG = 0x3A;
73 constexpr uint8_t TLV_IMAGESPANATTRIBUTE_SIZE_TAG = 0x3B;
74 constexpr uint8_t TLV_IMAGESPANATTRIBUTE_VERTICALALIGN_TAG = 0x3C;
75 constexpr uint8_t TLV_IMAGESPANATTRIBUTE_OBJECTFIT_TAG = 0x3D;
76 constexpr uint8_t TLV_IMAGESPANATTRIBUTE_MARGINPROP_TAG = 0x3E;
77 constexpr uint8_t TLV_IMAGESPANATTRIBUTE_BORDERRADIUS_TAG = 0x3F;
78 constexpr uint8_t TLV_IMAGESPANATTRIBUTE_PADDINGPROP_TAG = 0x40;
79 constexpr uint8_t TLV_IMAGESPANATTRIBUTE_END_TAG = 0x41;
80 
81 constexpr uint8_t TLV_IMAGESPANSIZE_TAG = 0x42;
82 constexpr uint8_t TLV_IMAGESPANSIZE_WIDTH_TAG = 0x43;
83 constexpr uint8_t TLV_IMAGESPANSIZE_HEIGHT_TAG = 0x44;
84 constexpr uint8_t TLV_IMAGESPANSIZE_END_TAG = 0x45;
85 
86 constexpr uint8_t TLV_PADDINGPROPERTY_TAG = 0x46;
87 constexpr uint8_t TLV_PADDINGPROPERTY_TOP_TAG = 0x47;
88 constexpr uint8_t TLV_PADDINGPROPERTY_BOTTOM_TAG = 0x48;
89 constexpr uint8_t TLV_PADDINGPROPERTY_LEFT_TAG = 0x49;
90 constexpr uint8_t TLV_PADDINGPROPERTY_RIGHT_TAG = 0x4A;
91 constexpr uint8_t TLV_PADDINGPROPERTY_END_TAG = 0x4B;
92 
93 constexpr uint8_t TLV_LEADINGMARGIN_TAG = 0x4C;
94 constexpr uint8_t TLV_LEADINGMARGIN_HASPIXEL_TAG = 0x4D;
95 constexpr uint8_t TLV_LEADINGMARGIN_NOPIXEL_TAG = 0x4E;
96 
97 constexpr uint8_t TLV_SPANITEM_TAG = 0x4F;
98 constexpr uint8_t TLV_IMAGESPANITEM_TAG = 0x50;
99 constexpr uint8_t TLV_SPANITEM_END_TAG = 0x51;
100 constexpr uint8_t TLV_IMAGESPANOPTION_OFFSET_TAG = 0x52;
101 constexpr uint8_t TLV_IMAGESPANOPTION_IMAGE_TAG = 0x53;
102 constexpr uint8_t TLV_IMAGESPANOPTION_BUNDLENAME_TAG = 0x54;
103 constexpr uint8_t TLV_IMAGESPANOPTION_MODULENAME_TAG = 0x55;
104 constexpr uint8_t TLV_IMAGESPANOPTION_IMAGEPIXELMAP_TAG = 0x56;
105 constexpr uint8_t TLV_IMAGESPANOPTION_IMAGEATTRIBUTE_TAG = 0x57;
106 
107 constexpr uint8_t TLV_SPAN_FONT_STYLE_FONTSIZE = 0x58;
108 constexpr uint8_t TLV_SPAN_FONT_STYLE_TEXTCOLOR = 0x59;
109 constexpr uint8_t TLV_SPAN_FONT_STYLE_TEXTSHADOW = 0x5A;
110 constexpr uint8_t TLV_SPAN_FONT_STYLE_ITALICFONTSTYLE = 0x5B;
111 constexpr uint8_t TLV_SPAN_FONT_STYLE_FONTWEIGHT = 0x5C;
112 constexpr uint8_t TLV_SPAN_FONT_STYLE_FONTFAMILY = 0x5D;
113 constexpr uint8_t TLV_SPAN_FONT_STYLE_FONTFEATURE = 0x5E;
114 constexpr uint8_t TLV_SPAN_FONT_STYLE_TEXTDECORATION = 0x5F;
115 constexpr uint8_t TLV_SPAN_FONT_STYLE_TEXTDECORATIONCOLOR = 0x60;
116 constexpr uint8_t TLV_SPAN_FONT_STYLE_TEXTDECORATIONSTYLE = 0x61;
117 constexpr uint8_t TLV_SPAN_FONT_STYLE_TEXTCASE = 0x62;
118 constexpr uint8_t TLV_SPAN_FONT_STYLE_ADPATMINFONTSIZE = 0x63;
119 constexpr uint8_t TLV_SPAN_FONT_STYLE_ADPATMAXFONTSIZE = 0x64;
120 constexpr uint8_t TLV_SPAN_FONT_STYLE_LETTERSPACING = 0x65;
121 constexpr uint8_t TLV_SPAN_TEXT_LINE_STYLE_LINEHEIGHT = 0x66;
122 constexpr uint8_t TLV_SPAN_TEXT_LINE_STYLE_LINESPACING = 0x67;
123 constexpr uint8_t TLV_SPAN_TEXT_LINE_STYLE_TEXTBASELINE = 0x68;
124 constexpr uint8_t TLV_SPAN_TEXT_LINE_STYLE_BASELINEOFFSET = 0x69;
125 constexpr uint8_t TLV_SPAN_TEXT_LINE_STYLE_TEXTOVERFLOW = 0x6A;
126 constexpr uint8_t TLV_SPAN_TEXT_LINE_STYLE_TEXTALIGN = 0x6B;
127 constexpr uint8_t TLV_SPAN_TEXT_LINE_STYLE_MAXLENGTH = 0x6C;
128 constexpr uint8_t TLV_SPAN_TEXT_LINE_STYLE_MAXLINES = 0x6D;
129 constexpr uint8_t TLV_SPAN_TEXT_LINE_STYLE_HEIGHTADAPTIVEPOLICY = 0x6E;
130 constexpr uint8_t TLV_SPAN_TEXT_LINE_STYLE_TEXTINDENT = 0x6F;
131 constexpr uint8_t TLV_SPAN_TEXT_LINE_STYLE_LEADINGMARGIN = 0x90;
132 constexpr uint8_t TLV_SPAN_TEXT_LINE_STYLE_WORDBREAK = 0x91;
133 constexpr uint8_t TLV_SPAN_TEXT_LINE_STYLE_LINEBREAKSTRATEGY = 0x92;
134 constexpr uint8_t TLV_SPAN_TEXT_LINE_STYLE_ELLIPSISMODE = 0x93;
135 
136 constexpr uint8_t TLV_SPAN_BACKGROUND_BACKGROUNDCOLOR = 0x94;
137 constexpr uint8_t TLV_SPAN_BACKGROUND_BACKGROUNDRADIUS = 0x95;
138 constexpr uint8_t TLV_SPAN_BACKGROUND_GROUPID = 0x96;
139 
140 #define TLV_DEFINE_ENUM_TYPE(type, tag) \
141 public:                                                                     \
142     static void Write##type(std::vector<uint8_t>& buff, type value)         \
143     {                                                                       \
144         WriteUint8(buff, tag);                                              \
145         WriteInt32(buff, static_cast<int32_t>(value));                      \
146     }                                                                       \
147     static type Read##type(std::vector<uint8_t>& buff, int32_t& cursor)     \
148     {                                                                       \
149         if (ReadUint8(buff, cursor) != (tag)) {                             \
150             return static_cast<type>(0);                                    \
151         }                                                                   \
152         auto val = ReadInt32(buff, cursor);                                 \
153         return static_cast<type>(val);                                      \
154     }
155 
156 class TLVUtil {
157 public:
WriteUint8(std::vector<uint8_t> & buff,uint8_t value)158     static void WriteUint8(std::vector<uint8_t>& buff, uint8_t value)
159     {
160         buff.push_back(value);
161     }
162 
ReadUint8(std::vector<uint8_t> & buff,int32_t & cursor)163     static uint8_t ReadUint8(std::vector<uint8_t>& buff, int32_t& cursor)
164     {
165         if (static_cast<size_t>(cursor + 1) > buff.size()) {
166             return TLV_END;
167         }
168         return buff[cursor++];
169     }
170 
WriteInt32(std::vector<uint8_t> & buff,int32_t value)171     static void WriteInt32(std::vector<uint8_t>& buff, int32_t value)
172     {
173         while (value > TLV_VARINT_MASK) {
174             buff.push_back(TLV_VARINT_MORE | uint8_t(value & TLV_VARINT_MASK));
175             value >>= TLV_VARINT_BITS;
176         }
177         buff.push_back(uint8_t(value));
178     }
179 
ReadInt32(std::vector<uint8_t> & buff,int32_t & cursor)180     static int32_t ReadInt32(std::vector<uint8_t>& buff, int32_t& cursor)
181     {
182         int32_t value = 0;
183         uint8_t shift = 0;
184         int32_t item = 0;
185         do {
186             if (static_cast<size_t>(cursor + 1) > buff.size()) {
187                 return static_cast<int32_t>(TLV_END);
188             }
189             item = int32_t(buff[cursor++]);
190             value |= (item & TLV_VARINT_MASK) << shift;
191             shift += TLV_VARINT_BITS;
192         } while ((item & TLV_VARINT_MORE) != 0);
193         return value;
194     }
195 
196     TLV_DEFINE_ENUM_TYPE(FontStyle, TLV_ITALICSFONTSTYLE_TAG);
197     TLV_DEFINE_ENUM_TYPE(FontWeight, TLV_FONTWEIGHT_TAG);
198     TLV_DEFINE_ENUM_TYPE(TextDecoration, TLV_TEXTDECORATION_TAG);
199     TLV_DEFINE_ENUM_TYPE(TextDecorationStyle, TLV_TEXTDECORATIONSTYLE_TAG);
200     TLV_DEFINE_ENUM_TYPE(TextCase, TLV_TEXTCASE_TAG);
201     TLV_DEFINE_ENUM_TYPE(TextBaseline, TLV_TEXTBASELINE_TAG);
202     TLV_DEFINE_ENUM_TYPE(TextOverflow, TLV_TEXTOVERFLOW_TAG);
203     TLV_DEFINE_ENUM_TYPE(TextAlign, TLV_TEXTALIGN_TAG);
204     TLV_DEFINE_ENUM_TYPE(TextHeightAdaptivePolicy, TLV_TEXTHEIGHTADAPTIVEPOLICY_TAG);
205     TLV_DEFINE_ENUM_TYPE(WordBreak, TLV_WORDBREAK_TAG);
206     TLV_DEFINE_ENUM_TYPE(EllipsisMode, TLV_ELLIPSISMODE_TAG);
207     TLV_DEFINE_ENUM_TYPE(LineBreakStrategy, TLV_LINEBREAKSTRATEGY_TAG);
208     TLV_DEFINE_ENUM_TYPE(VerticalAlign, TLV_VERTICALALIGN_TAG);
209     TLV_DEFINE_ENUM_TYPE(ImageFit, TLV_IMAGEFIT_TAG);
210 
211     static void WriteString(std::vector<uint8_t>& buff, const std::string& value);
212     static std::string ReadString(std::vector<uint8_t>& buff, int32_t& cursor);
213     static void WriteDouble(std::vector<uint8_t>& buff, double value);
214     static double ReadDouble(std::vector<uint8_t>& buff, int32_t& cursor);
215     static void WriteColor(std::vector<uint8_t>& buff, Color& value);
216     static Color ReadColor(std::vector<uint8_t>& buff, int32_t& cursor);
217     static void WriteDimension(std::vector<uint8_t>& buff, const Dimension& value);
218     static Dimension ReadDimension(std::vector<uint8_t>& buff, int32_t& cursor);
219     static void WriteFontFamily(std::vector<uint8_t>& buff, std::vector<std::string>& value);
220     static std::vector<std::string> ReadFontFamily(std::vector<uint8_t>& buff, int32_t& cursor);
221     static void WriteTextShadow(std::vector<uint8_t>& buff, Shadow& value);
222     static Shadow ReadTextShadow(std::vector<uint8_t>& buff, int32_t& cursor);
223     static void WriteTextShadows(std::vector<uint8_t>& buff, std::vector<Shadow>& value);
224     static std::vector<Shadow> ReadTextShadows(std::vector<uint8_t>& buff, int32_t& cursor);
225     static void WriteFontFeature(std::vector<uint8_t>& buff, std::list<std::pair<std::string, int32_t>>& value);
226     static std::list<std::pair<std::string, int32_t>> ReadFontFeature(std::vector<uint8_t>& buff, int32_t& cursor);
227     static void WriteBorderRadiusProperty(std::vector<uint8_t>& buff, NG::BorderRadiusProperty& value);
228     static NG::BorderRadiusProperty ReadBorderRadiusProperty(std::vector<uint8_t>& buff, int32_t& cursor);
229     static void WritePixelMap(std::vector<uint8_t>& buff, RefPtr<Ace::PixelMap>& pixelMap);
230     static RefPtr<Ace::PixelMap> ReadPixelMap(std::vector<uint8_t>& buff, int32_t& cursor);
231     static void WriteCalcDimension(std::vector<uint8_t>& buff, CalcDimension& value);
232     static CalcDimension ReadCalcDimension(std::vector<uint8_t>& buff, int32_t& cursor);
233     static void WriteCalcLength(std::vector<uint8_t>& buff, NG::CalcLength& value);
234     static NG::CalcLength ReadCalcLength(std::vector<uint8_t>& buff, int32_t& cursor);
235     static void WriteImageSpanSize(std::vector<uint8_t>& buff, ImageSpanSize& value);
236     static ImageSpanSize ReadImageSpanSize(std::vector<uint8_t>& buff, int32_t& cursor);
237     static void WritePaddingProperty(std::vector<uint8_t>& buff, NG::PaddingProperty& value);
238     static NG::PaddingProperty ReadPaddingProperty(std::vector<uint8_t>& buff, int32_t& cursor);
239     static void WriteImageSpanAttribute(std::vector<uint8_t>& buff, ImageSpanAttribute& value);
240     static ImageSpanAttribute ReadImageSpanAttribute(std::vector<uint8_t>& buff, int32_t& cursor);
241     static void WriteLeadingMargin(std::vector<uint8_t>& buff, NG::LeadingMargin& value);
242     static NG::LeadingMargin ReadLeadingMargin(std::vector<uint8_t>& buff, int32_t& cursor);
243 };
244 } // namespace OHOS::Ace
245 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_PATTERNS_TEXT_SPAN_TLV_UTIL_H