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 #include "base/utils/utf_helper.h"
17 #include "core/components_ng/pattern/text/span/tlv_util.h"
18
19
20 namespace OHOS::Ace {
WriteString(std::vector<uint8_t> & buff,const std::string & value)21 void TLVUtil::WriteString(std::vector<uint8_t>& buff, const std::string& value)
22 {
23 WriteUint8(buff, TLV_STRING_TAG);
24 WriteInt32(buff, value.length());
25 for (char ch: value) {
26 WriteUint8(buff, static_cast<uint8_t>(ch));
27 }
28 }
29
ReadString(std::vector<uint8_t> & buff,int32_t & cursor)30 std::string TLVUtil::ReadString(std::vector<uint8_t>& buff, int32_t& cursor)
31 {
32 if (ReadUint8(buff, cursor) != TLV_STRING_TAG) {
33 return "";
34 }
35 int32_t strLen = ReadInt32(buff, cursor);
36 if (strLen == TLV_END || strLen <= 0) {
37 return "";
38 }
39 std::stringstream ss;
40 for (auto i = 0; i < strLen; ++i) {
41 uint8_t chVal = ReadUint8(buff, cursor);
42 if (chVal == TLV_END) {
43 return "";
44 }
45 ss << static_cast<char>(chVal);
46 }
47 return ss.str();
48 }
49
WriteU16String(std::vector<uint8_t> & buff,const std::u16string & value)50 void TLVUtil::WriteU16String(std::vector<uint8_t>& buff, const std::u16string& value)
51 {
52 WriteString(buff, UtfUtils::Str16DebugToStr8(value));
53 }
54
ReadU16String(std::vector<uint8_t> & buff,int32_t & cursor)55 std::u16string TLVUtil::ReadU16String(std::vector<uint8_t>& buff, int32_t& cursor)
56 {
57 return UtfUtils::Str8DebugToStr16(ReadString(buff, cursor));
58 }
59
WriteDouble(std::vector<uint8_t> & buff,double value)60 void TLVUtil::WriteDouble(std::vector<uint8_t>& buff, double value)
61 {
62 WriteUint8(buff, TLV_DOUBLE_TAG);
63 std::vector<uint8_t> bytes(sizeof(double));
64 auto valuePtr = reinterpret_cast<uint8_t*>(&value);
65 std::copy(valuePtr, valuePtr + sizeof(double), bytes.begin());
66 buff.insert(buff.end(), bytes.begin(), bytes.end());
67 }
68
ReadDouble(std::vector<uint8_t> & buff,int32_t & cursor)69 double TLVUtil::ReadDouble(std::vector<uint8_t>& buff, int32_t& cursor)
70 {
71 if (ReadUint8(buff, cursor) != TLV_DOUBLE_TAG) {
72 return 0.0;
73 }
74 auto start = buff.begin() + cursor;
75 auto end = start + sizeof(double);
76 double value;
77 std::copy(start, end, reinterpret_cast<uint8_t*>(&value));
78 cursor += sizeof(double);
79 return value;
80 }
81
WriteFloat(std::vector<uint8_t> & buff,float value)82 void TLVUtil::WriteFloat(std::vector<uint8_t>& buff, float value)
83 {
84 WriteUint8(buff, TLV_FLOAT_TAG);
85 std::vector<uint8_t> bytes(sizeof(float));
86 auto valuePtr = reinterpret_cast<uint8_t*>(&value);
87 std::copy(valuePtr, valuePtr + sizeof(float), bytes.begin());
88 buff.insert(buff.end(), bytes.begin(), bytes.end());
89 }
90
ReadFloat(std::vector<uint8_t> & buff,int32_t & cursor)91 float TLVUtil::ReadFloat(std::vector<uint8_t>& buff, int32_t& cursor)
92 {
93 if (ReadUint8(buff, cursor) != TLV_FLOAT_TAG) {
94 return 0.0;
95 }
96 auto start = buff.begin() + cursor;
97 auto end = start + sizeof(float);
98 float value;
99 std::copy(start, end, reinterpret_cast<uint8_t*>(&value));
100 cursor += sizeof(float);
101 return value;
102 }
103
WriteColor(std::vector<uint8_t> & buff,Color & value)104 void TLVUtil::WriteColor(std::vector<uint8_t>& buff, Color& value)
105 {
106 WriteUint8(buff, TLV_COLOR_TAG);
107 WriteUint8(buff, value.GetAlpha());
108 WriteUint8(buff, value.GetRed());
109 WriteUint8(buff, value.GetGreen());
110 WriteUint8(buff, value.GetBlue());
111 }
112
ReadColor(std::vector<uint8_t> & buff,int32_t & cursor)113 Color TLVUtil::ReadColor(std::vector<uint8_t>& buff, int32_t& cursor)
114 {
115 if (ReadUint8(buff, cursor) != TLV_COLOR_TAG) {
116 return Color();
117 }
118 auto alpha = ReadUint8(buff, cursor);
119 auto red = ReadUint8(buff, cursor);
120 auto green = ReadUint8(buff, cursor);
121 auto blue = ReadUint8(buff, cursor);
122 return Color::FromARGB(alpha, red, green, blue);
123 }
124
WriteDimension(std::vector<uint8_t> & buff,const Dimension & value)125 void TLVUtil::WriteDimension(std::vector<uint8_t>& buff, const Dimension& value)
126 {
127 WriteUint8(buff, TLV_DIMENSION_TAG);
128 WriteDouble(buff, value.Value());
129 WriteInt32(buff, static_cast<int32_t>(value.Unit()));
130 }
131
ReadDimension(std::vector<uint8_t> & buff,int32_t & cursor)132 Dimension TLVUtil::ReadDimension(std::vector<uint8_t>& buff, int32_t& cursor)
133 {
134 if (ReadUint8(buff, cursor) != TLV_DIMENSION_TAG) {
135 return Dimension();
136 }
137 auto val = ReadDouble(buff, cursor);
138 auto unit = static_cast<DimensionUnit>(ReadInt32(buff, cursor));
139 return Dimension(val, unit);
140 }
141
WriteFontFamily(std::vector<uint8_t> & buff,std::vector<std::string> & value)142 void TLVUtil::WriteFontFamily(std::vector<uint8_t>& buff, std::vector<std::string>& value)
143 {
144 WriteUint8(buff, TLV_FONTFAMILIES_TAG);
145 WriteInt32(buff, value.size());
146 for (auto& fontFamily: value) {
147 WriteString(buff, fontFamily);
148 }
149 }
150
ReadFontFamily(std::vector<uint8_t> & buff,int32_t & cursor)151 std::vector<std::string> TLVUtil::ReadFontFamily(std::vector<uint8_t>& buff, int32_t& cursor)
152 {
153 std::vector<std::string> fontFamilies;
154 if (ReadUint8(buff, cursor) != TLV_FONTFAMILIES_TAG) {
155 return fontFamilies;
156 }
157 int32_t fontFamilySize = ReadInt32(buff, cursor);
158 if (fontFamilySize < 0) {
159 return fontFamilies;
160 }
161 for (auto i = 0; i < fontFamilySize; i++) {
162 auto fontFamily = ReadString(buff, cursor);
163 fontFamilies.emplace_back(fontFamily);
164 }
165 return fontFamilies;
166 }
167
WriteTextShadow(std::vector<uint8_t> & buff,Shadow & value)168 void TLVUtil::WriteTextShadow(std::vector<uint8_t>& buff, Shadow& value)
169 {
170 WriteUint8(buff, TLV_TEXTSHADOW_TAG);
171 WriteDouble(buff, value.GetBlurRadius());
172 auto color = value.GetColor();
173 WriteColor(buff, color);
174 WriteInt32(buff, static_cast<int32_t>(value.GetShadowType()));
175 Offset offset = value.GetOffset();
176 WriteDouble(buff, offset.GetX());
177 WriteDouble(buff, offset.GetY());
178 }
179
ReadTextShadow(std::vector<uint8_t> & buff,int32_t & cursor)180 Shadow TLVUtil::ReadTextShadow(std::vector<uint8_t>& buff, int32_t& cursor)
181 {
182 Shadow shadow;
183 if (ReadUint8(buff, cursor) != TLV_TEXTSHADOW_TAG) {
184 return shadow;
185 }
186 double blurRadius = ReadDouble(buff, cursor);
187 Color color = ReadColor(buff, cursor);
188 ShadowType type = static_cast<ShadowType>(ReadInt32(buff, cursor));
189 double offsetX = ReadDouble(buff, cursor);
190 double offsetY = ReadDouble(buff, cursor);
191 shadow.SetBlurRadius(blurRadius);
192 shadow.SetColor(color);
193 shadow.SetShadowType(type);
194 shadow.SetOffset({offsetX, offsetY});
195 shadow.SetIsFilled(false);
196 return shadow;
197 }
198
WriteTextShadows(std::vector<uint8_t> & buff,std::vector<Shadow> & value)199 void TLVUtil::WriteTextShadows(std::vector<uint8_t>& buff, std::vector<Shadow>& value)
200 {
201 WriteUint8(buff, TLV_TEXTSHADOWS_TAG);
202 WriteInt32(buff, value.size());
203 for (auto& shadow: value) {
204 WriteTextShadow(buff, shadow);
205 }
206 }
207
ReadTextShadows(std::vector<uint8_t> & buff,int32_t & cursor)208 std::vector<Shadow> TLVUtil::ReadTextShadows(std::vector<uint8_t>& buff, int32_t& cursor)
209 {
210 std::vector<Shadow> shadows;
211 if (ReadUint8(buff, cursor) != TLV_TEXTSHADOWS_TAG) {
212 return shadows;
213 }
214 int32_t shadowSize = ReadInt32(buff, cursor);
215 if (shadowSize < 0) {
216 return shadows;
217 }
218 for (auto i = 0; i < shadowSize; i++) {
219 shadows.emplace_back(ReadTextShadow(buff, cursor));
220 }
221 return shadows;
222 }
223
WriteTextDecorations(std::vector<uint8_t> & buff,const std::vector<TextDecoration> & values)224 void TLVUtil::WriteTextDecorations(std::vector<uint8_t>& buff, const std::vector<TextDecoration>& values)
225 {
226 WriteUint8(buff, TLV_SPAN_FONT_STYLE_TEXTDECORATION);
227 WriteInt32(buff, values.size());
228 for (TextDecoration value: values) {
229 WriteInt32(buff, static_cast<int32_t>(value));
230 }
231 }
232
ReadTextDecorations(std::vector<uint8_t> & buff,int32_t & cursor)233 std::vector<TextDecoration> TLVUtil::ReadTextDecorations(std::vector<uint8_t>& buff, int32_t& cursor)
234 {
235 std::vector<TextDecoration> textDecorations;
236 int32_t size = ReadInt32(buff, cursor);
237 if (size < 0) {
238 return textDecorations;
239 }
240 for (auto i = 0; i < size; i++) {
241 int32_t value = ReadInt32(buff, cursor);
242 textDecorations.emplace_back(static_cast<TextDecoration>(value));
243 }
244 return textDecorations;
245 }
246
247
WriteFontFeature(std::vector<uint8_t> & buff,std::list<std::pair<std::string,int32_t>> & value)248 void TLVUtil::WriteFontFeature(std::vector<uint8_t>& buff, std::list<std::pair<std::string, int32_t>>& value)
249 {
250 WriteUint8(buff, TLV_FONTFEATURE_TAG);
251 WriteInt32(buff, value.size());
252 for (auto& fontFeature : value) {
253 WriteString(buff, fontFeature.first);
254 WriteInt32(buff, fontFeature.second);
255 }
256 }
257
ReadFontFeature(std::vector<uint8_t> & buff,int32_t & cursor)258 std::list<std::pair<std::string, int32_t>> TLVUtil::ReadFontFeature(std::vector<uint8_t>& buff, int32_t& cursor)
259 {
260 std::list<std::pair<std::string, int32_t>> fontFeatureList;
261 if (ReadUint8(buff, cursor) != TLV_FONTFEATURE_TAG) {
262 return fontFeatureList;
263 }
264 int32_t len = ReadInt32(buff, cursor);
265 if (len < 0) {
266 return fontFeatureList;
267 }
268 for (auto i = 0; i < len; i++) {
269 std::string first = ReadString(buff, cursor);
270 int32_t second = ReadInt32(buff, cursor);
271 fontFeatureList.push_back({first, second});
272 }
273 return fontFeatureList;
274 }
275
WriteBorderRadiusProperty(std::vector<uint8_t> & buff,NG::BorderRadiusProperty & value)276 void TLVUtil::WriteBorderRadiusProperty(std::vector<uint8_t>& buff, NG::BorderRadiusProperty& value)
277 {
278 WriteUint8(buff, TLV_BORDERRADIUS_TAG);
279
280 WriteDimension(buff, value.radiusTopLeft.value_or(ILLEGAL_DIMENSION_VALUE));
281 WriteDimension(buff, value.radiusTopRight.value_or(ILLEGAL_DIMENSION_VALUE));
282 WriteDimension(buff, value.radiusBottomLeft.value_or(ILLEGAL_DIMENSION_VALUE));
283 WriteDimension(buff, value.radiusBottomRight.value_or(ILLEGAL_DIMENSION_VALUE));
284 }
285
ReadBorderRadiusProperty(std::vector<uint8_t> & buff,int32_t & cursor)286 NG::BorderRadiusProperty TLVUtil::ReadBorderRadiusProperty(std::vector<uint8_t>& buff, int32_t& cursor)
287 {
288 NG::BorderRadiusProperty br;
289 if (ReadUint8(buff, cursor) != TLV_BORDERRADIUS_TAG) {
290 return br;
291 }
292
293 Dimension radiusTopLeft = ReadDimension(buff, cursor);
294 if (radiusTopLeft.IsNonNegative()) {
295 br.radiusTopLeft = radiusTopLeft;
296 }
297 Dimension radiusTopRight = ReadDimension(buff, cursor);
298 if (radiusTopRight.IsNonNegative()) {
299 br.radiusTopRight = radiusTopRight;
300 }
301 Dimension radiusBottomLeft = ReadDimension(buff, cursor);
302 if (radiusBottomLeft.IsNonNegative()) {
303 br.radiusBottomLeft = radiusBottomLeft;
304 }
305 Dimension radiusBottomRight = ReadDimension(buff, cursor);
306 if (radiusBottomRight.IsNonNegative()) {
307 br.radiusBottomRight = radiusBottomRight;
308 }
309 return br;
310 }
311
WritePixelMap(std::vector<uint8_t> & buff,RefPtr<Ace::PixelMap> & pixelMap)312 void TLVUtil::WritePixelMap(std::vector<uint8_t>& buff, RefPtr<Ace::PixelMap>& pixelMap)
313 {
314 WriteUint8(buff, TLV_PIXEL_MAP_TAG);
315 std::vector<uint8_t> tmpPixel;
316 if (pixelMap) {
317 pixelMap->EncodeTlv(tmpPixel);
318 WriteInt32(buff, static_cast<int32_t>(tmpPixel.size()));
319 buff.insert(buff.end(), tmpPixel.begin(), tmpPixel.end());
320 } else {
321 WriteInt32(buff, 0);
322 }
323 }
324
ReadPixelMap(std::vector<uint8_t> & buff,int32_t & cursor)325 RefPtr<Ace::PixelMap> TLVUtil::ReadPixelMap(std::vector<uint8_t>& buff, int32_t& cursor)
326 {
327 if (ReadUint8(buff, cursor) != TLV_PIXEL_MAP_TAG) {
328 return nullptr;
329 }
330 auto pixelMapLength = ReadInt32(buff, cursor);
331 if (pixelMapLength == 0) {
332 return nullptr;
333 }
334 std::vector<uint8_t> pixelMapSubVec(buff.begin() + cursor, buff.begin() + cursor + pixelMapLength);
335 RefPtr<Ace::PixelMap> p = Ace::PixelMap::DecodeTlv(pixelMapSubVec);
336 cursor += pixelMapLength;
337 return p;
338 }
339
WriteCalcDimension(std::vector<uint8_t> & buff,CalcDimension & value)340 void TLVUtil::WriteCalcDimension(std::vector<uint8_t>& buff, CalcDimension& value)
341 {
342 WriteUint8(buff, TLV_CALCDIMENSION_TAG);
343 WriteDimension(buff, value);
344 }
345
ReadCalcDimension(std::vector<uint8_t> & buff,int32_t & cursor)346 CalcDimension TLVUtil::ReadCalcDimension(std::vector<uint8_t>& buff, int32_t& cursor)
347 {
348 CalcDimension i;
349 if (ReadUint8(buff, cursor) != TLV_CALCDIMENSION_TAG) {
350 return i;
351 }
352 Dimension dim = ReadDimension(buff, cursor);
353 return CalcDimension(dim);
354 }
355
WriteCalcLength(std::vector<uint8_t> & buff,NG::CalcLength & value)356 void TLVUtil::WriteCalcLength(std::vector<uint8_t>& buff, NG::CalcLength& value)
357 {
358 WriteUint8(buff, TLV_CALCLENGTH_TAG);
359 WriteString(buff, value.CalcValue());
360 WriteDimension(buff, value.GetDimension());
361 }
362
ReadCalcLength(std::vector<uint8_t> & buff,int32_t & cursor)363 NG::CalcLength TLVUtil::ReadCalcLength(std::vector<uint8_t>& buff, int32_t& cursor)
364 {
365 NG::CalcLength calcL;
366 if (ReadUint8(buff, cursor) != TLV_CALCLENGTH_TAG) {
367 return calcL;
368 }
369 std::string calcValue = ReadString(buff, cursor);
370 Dimension dim = ReadDimension(buff, cursor);
371 calcL = NG::CalcLength(dim);
372 calcL.SetCalcValue(calcValue);
373 return calcL;
374 }
375
WriteImageSpanSize(std::vector<uint8_t> & buff,ImageSpanSize & value)376 void TLVUtil::WriteImageSpanSize(std::vector<uint8_t>& buff, ImageSpanSize& value)
377 {
378 WriteUint8(buff, TLV_IMAGESPANSIZE_TAG);
379 if (value.width.has_value()) {
380 WriteUint8(buff, TLV_IMAGESPANSIZE_WIDTH_TAG);
381 WriteCalcDimension(buff, value.width.value());
382 }
383 if (value.height.has_value()) {
384 WriteUint8(buff, TLV_IMAGESPANSIZE_HEIGHT_TAG);
385 WriteCalcDimension(buff, value.height.value());
386 }
387 WriteUint8(buff, TLV_IMAGESPANSIZE_END_TAG);
388 }
389
ReadImageSpanSize(std::vector<uint8_t> & buff,int32_t & cursor)390 ImageSpanSize TLVUtil::ReadImageSpanSize(std::vector<uint8_t>& buff, int32_t& cursor)
391 {
392 ImageSpanSize imageSpanSize;
393 if (ReadUint8(buff, cursor) != TLV_IMAGESPANSIZE_TAG) {
394 return imageSpanSize;
395 }
396 for (uint8_t tag = TLVUtil::ReadUint8(buff, cursor);
397 tag != TLV_IMAGESPANSIZE_END_TAG; tag = TLVUtil::ReadUint8(buff, cursor)) {
398 switch (tag) {
399 case TLV_IMAGESPANSIZE_WIDTH_TAG: {
400 imageSpanSize.width = ReadCalcDimension(buff, cursor);
401 break;
402 }
403 case TLV_IMAGESPANSIZE_HEIGHT_TAG: {
404 imageSpanSize.height = ReadCalcDimension(buff, cursor);
405 break;
406 }
407 default:
408 break;
409 }
410 }
411 return imageSpanSize;
412 }
413
WritePaddingProperty(std::vector<uint8_t> & buff,NG::PaddingProperty & value)414 void TLVUtil::WritePaddingProperty(std::vector<uint8_t>& buff, NG::PaddingProperty& value)
415 {
416 WriteUint8(buff, TLV_PADDINGPROPERTY_TAG);
417 if (value.left.has_value()) {
418 WriteUint8(buff, TLV_PADDINGPROPERTY_LEFT_TAG);
419 WriteCalcLength(buff, value.left.value());
420 }
421 if (value.right.has_value()) {
422 WriteUint8(buff, TLV_PADDINGPROPERTY_RIGHT_TAG);
423 WriteCalcLength(buff, value.right.value());
424 }
425 if (value.top.has_value()) {
426 WriteUint8(buff, TLV_PADDINGPROPERTY_TOP_TAG);
427 WriteCalcLength(buff, value.top.value());
428 }
429 if (value.bottom.has_value()) {
430 WriteUint8(buff, TLV_PADDINGPROPERTY_BOTTOM_TAG);
431 WriteCalcLength(buff, value.bottom.value());
432 }
433 WriteUint8(buff, TLV_PADDINGPROPERTY_END_TAG);
434 }
435
ReadPaddingProperty(std::vector<uint8_t> & buff,int32_t & cursor)436 NG::PaddingProperty TLVUtil::ReadPaddingProperty(std::vector<uint8_t>& buff, int32_t& cursor)
437 {
438 NG::PaddingProperty pad;
439 if (ReadUint8(buff, cursor) != TLV_PADDINGPROPERTY_TAG) {
440 return pad;
441 }
442 for (uint8_t tag = TLVUtil::ReadUint8(buff, cursor);
443 tag != TLV_PADDINGPROPERTY_END_TAG; tag = TLVUtil::ReadUint8(buff, cursor)) {
444 switch (tag) {
445 case TLV_PADDINGPROPERTY_LEFT_TAG: {
446 pad.left = ReadCalcLength(buff, cursor);
447 break;
448 }
449 case TLV_PADDINGPROPERTY_TOP_TAG: {
450 pad.top = ReadCalcLength(buff, cursor);
451 break;
452 }
453 case TLV_PADDINGPROPERTY_BOTTOM_TAG: {
454 pad.bottom = ReadCalcLength(buff, cursor);
455 break;
456 }
457 case TLV_PADDINGPROPERTY_RIGHT_TAG: {
458 pad.right = ReadCalcLength(buff, cursor);
459 break;
460 }
461 default:
462 break;
463 }
464 }
465 return pad;
466 }
467
WriteImageSpanAttribute(std::vector<uint8_t> & buff,ImageSpanAttribute & value)468 void TLVUtil::WriteImageSpanAttribute(std::vector<uint8_t>& buff, ImageSpanAttribute& value)
469 {
470 WriteUint8(buff, TLV_IMAGESPANATTRIBUTE_TAG);
471 if (value.size.has_value()) {
472 WriteUint8(buff, TLV_IMAGESPANATTRIBUTE_SIZE_TAG);
473 WriteImageSpanSize(buff, value.size.value());
474 }
475 if (value.verticalAlign.has_value()) {
476 WriteUint8(buff, TLV_IMAGESPANATTRIBUTE_VERTICALALIGN_TAG);
477 WriteVerticalAlign(buff, value.verticalAlign.value());
478 }
479 if (value.objectFit.has_value()) {
480 WriteUint8(buff, TLV_IMAGESPANATTRIBUTE_OBJECTFIT_TAG);
481 WriteImageFit(buff, value.objectFit.value());
482 }
483 if (value.marginProp.has_value()) {
484 WriteUint8(buff, TLV_IMAGESPANATTRIBUTE_MARGINPROP_TAG);
485 WritePaddingProperty(buff, value.marginProp.value());
486 }
487 if (value.borderRadius.has_value()) {
488 WriteUint8(buff, TLV_IMAGESPANATTRIBUTE_BORDERRADIUS_TAG);
489 WriteBorderRadiusProperty(buff, value.borderRadius.value());
490 }
491 if (value.paddingProp.has_value()) {
492 WriteUint8(buff, TLV_IMAGESPANATTRIBUTE_PADDINGPROP_TAG);
493 WritePaddingProperty(buff, value.paddingProp.value());
494 }
495 WriteUint8(buff, TLV_IMAGESPANATTRIBUTE_END_TAG);
496 }
497
ReadImageSpanAttribute(std::vector<uint8_t> & buff,int32_t & cursor)498 ImageSpanAttribute TLVUtil::ReadImageSpanAttribute(std::vector<uint8_t>& buff, int32_t& cursor)
499 {
500 ImageSpanAttribute l;
501 if (ReadUint8(buff, cursor) != TLV_IMAGESPANATTRIBUTE_TAG) {
502 return l;
503 }
504 for (uint8_t tag = TLVUtil::ReadUint8(buff, cursor);
505 tag != TLV_IMAGESPANATTRIBUTE_END_TAG; tag = TLVUtil::ReadUint8(buff, cursor)) {
506 switch (tag) {
507 case TLV_IMAGESPANATTRIBUTE_SIZE_TAG: {
508 l.size = ReadImageSpanSize(buff, cursor);
509 break;
510 }
511 case TLV_IMAGESPANATTRIBUTE_VERTICALALIGN_TAG: {
512 l.verticalAlign = ReadVerticalAlign(buff, cursor);
513 break;
514 }
515 case TLV_IMAGESPANATTRIBUTE_OBJECTFIT_TAG: {
516 l.objectFit = ReadImageFit(buff, cursor);
517 break;
518 }
519 case TLV_IMAGESPANATTRIBUTE_MARGINPROP_TAG: {
520 l.marginProp = ReadPaddingProperty(buff, cursor);
521 break;
522 }
523 case TLV_IMAGESPANATTRIBUTE_BORDERRADIUS_TAG: {
524 l.borderRadius = ReadBorderRadiusProperty(buff, cursor);
525 break;
526 }
527 case TLV_IMAGESPANATTRIBUTE_PADDINGPROP_TAG: {
528 l.paddingProp = ReadPaddingProperty(buff, cursor);
529 break;
530 }
531 default:
532 break;
533 }
534 }
535 if (!Container::GreatOrEqualAPITargetVersion(PlatformVersion::VERSION_TWENTY) &&
536 l.verticalAlign == VerticalAlign::FOLLOW_PARAGRAPH) {
537 l.verticalAlign = VerticalAlign::BOTTOM;
538 }
539 return l;
540 }
541
WriteLeadingMargin(std::vector<uint8_t> & buff,NG::LeadingMargin & value)542 void TLVUtil::WriteLeadingMargin(std::vector<uint8_t>& buff, NG::LeadingMargin& value)
543 {
544 WriteUint8(buff, TLV_LEADINGMARGIN_TAG);
545 WriteDimension(buff, value.size.Width());
546 WriteDimension(buff, value.size.Height());
547 if (value.pixmap) {
548 WriteUint8(buff, TLV_LEADINGMARGIN_HASPIXEL_TAG);
549 WritePixelMap(buff, value.pixmap);
550 } else {
551 WriteUint8(buff, TLV_LEADINGMARGIN_NOPIXEL_TAG);
552 }
553 }
554
ReadLeadingMargin(std::vector<uint8_t> & buff,int32_t & cursor)555 NG::LeadingMargin TLVUtil::ReadLeadingMargin(std::vector<uint8_t>& buff, int32_t& cursor)
556 {
557 NG::LeadingMargin l;
558 if (ReadUint8(buff, cursor) != TLV_LEADINGMARGIN_TAG) {
559 return l;
560 }
561 auto width = ReadDimension(buff, cursor);
562 auto height = ReadDimension(buff, cursor);
563 l.size = NG::LeadingMarginSize(width, height);
564 auto tag = ReadUint8(buff, cursor);
565 if (tag == TLV_LEADINGMARGIN_HASPIXEL_TAG) {
566 l.pixmap = ReadPixelMap(buff, cursor);
567 }
568 return l;
569 }
570 } // namespace OHOS::Ace