1 /*
2 * Copyright (c) 2021 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_V2_INSPECTOR_UTILS_H
17 #define FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_V2_INSPECTOR_UTILS_H
18
19 #include <string>
20 #include <vector>
21
22 #include "core/components/common/layout/constants.h"
23 #include "core/components/common/layout/align_declaration.h"
24 #include "core/components/common/properties/text_style.h"
25 #include "core/components/text_overlay/text_overlay_manager.h"
26
27 namespace OHOS::Ace::V2 {
28
29 namespace {
30
31 const char* AXIS_TYPE[] = {
32 "Axis.VERTICAL",
33 "Axis.HORIZONTAL",
34 "Axis.FREE",
35 "Axis.NONE",
36 };
37
38 } // namespace
39
ConvertStackFitToString(StackFit stackFit)40 inline std::string ConvertStackFitToString(StackFit stackFit)
41 {
42 std::string result = "";
43 switch (stackFit) {
44 case StackFit::STRETCH:
45 result = "StackFit.Stretch";
46 break;
47 case StackFit::INHERIT:
48 result = "StackFit.Inherit";
49 break;
50 case StackFit::FIRST_CHILD:
51 result = "StackFit.FirstChild";
52 break;
53 case StackFit::KEEP:
54 default:
55 result = "StackFit.Keep";
56 break;
57 }
58 return result;
59 }
60
ConvertOverflowToString(Overflow overflow)61 inline std::string ConvertOverflowToString(Overflow overflow)
62 {
63 std::string result = "";
64 switch (overflow) {
65 case Overflow::CLIP:
66 result = "Overflow.Clip";
67 break;
68 case Overflow::SCROLL:
69 result = "Overflow.Scroll";
70 break;
71 case Overflow::FORCE_CLIP:
72 result = "Overflow.ForceClip";
73 break;
74 case Overflow::OBSERVABLE:
75 default:
76 result = "Overflow.Observable";
77 break;
78 }
79 return result;
80 }
81
ConvertBoolToString(bool flag)82 inline std::string ConvertBoolToString(bool flag)
83 {
84 return flag ? "true" : "false";
85 }
86
ConvertTabBarModeToString(TabBarMode barMode)87 inline std::string ConvertTabBarModeToString(TabBarMode barMode)
88 {
89 std::string result = "";
90 switch (barMode) {
91 case TabBarMode::SCROLLABLE:
92 result = "BarMode.Scrollable";
93 break;
94 case TabBarMode::FIXED_START:
95 result = "BarMode.FixedStart";
96 break;
97 case TabBarMode::FIXED:
98 default:
99 result = "BarMode.Fixed";
100 break;
101 }
102 return result;
103 }
104
ConvertBarPositionToString(BarPosition barPosition)105 inline std::string ConvertBarPositionToString(BarPosition barPosition)
106 {
107 std::string result = "";
108 switch (barPosition) {
109 case BarPosition::END:
110 result = "BarPosition.End";
111 break;
112 case BarPosition::START:
113 default:
114 result = "BarPosition.Start";
115 break;
116 }
117 return result;
118 }
119
ConvertFlexDirectionToStirng(FlexDirection direction)120 inline std::string ConvertFlexDirectionToStirng(FlexDirection direction)
121 {
122 if (direction == FlexDirection::ROW) {
123 return "FlexDirection.Row";
124 } else if (direction == FlexDirection::ROW_REVERSE) {
125 return "FlexDirection.RowReverse";
126 } else if (direction == FlexDirection::COLUMN) {
127 return "FlexDirection.Column";
128 } else {
129 return "FlexDirection.ColumnReverse";
130 }
131 }
132
ConvertWrapDirectionToStirng(WrapDirection direction)133 inline std::string ConvertWrapDirectionToStirng(WrapDirection direction)
134 {
135 if (direction == WrapDirection::HORIZONTAL) {
136 return "FlexDirection.Row";
137 } else if (direction == WrapDirection::HORIZONTAL_REVERSE) {
138 return "FlexDirection.RowReverse";
139 } else if (direction == WrapDirection::VERTICAL) {
140 return "FlexDirection.Column";
141 } else {
142 return "FlexDirection.ColumnReverse";
143 }
144 }
145
ConvertFlexAlignToStirng(FlexAlign align)146 inline std::string ConvertFlexAlignToStirng(FlexAlign align)
147 {
148 if (align == FlexAlign::FLEX_START) {
149 return "FlexAlign.Start";
150 } else if (align == FlexAlign::CENTER) {
151 return "FlexAlign.Center";
152 } else if (align == FlexAlign::FLEX_END) {
153 return "FlexAlign.End";
154 } else if (align == FlexAlign::SPACE_BETWEEN) {
155 return "FlexAlign.SpaceBetween";
156 } else if (align == FlexAlign::SPACE_AROUND) {
157 return "FlexAlign.SpaceAround";
158 } else if (align == FlexAlign::BASELINE) {
159 return "FlexAlign.Baseline";
160 } else if (align == FlexAlign::STRETCH) {
161 return "FlexAlign.Stretch";
162 } else {
163 return "FlexAlign.SpaceEvenly";
164 }
165 }
166
ConvertItemAlignToStirng(FlexAlign align)167 inline std::string ConvertItemAlignToStirng(FlexAlign align)
168 {
169 if (align == FlexAlign::FLEX_START) {
170 return "ItemAlign.Start";
171 }
172 if (align == FlexAlign::CENTER) {
173 return "ItemAlign.Center";
174 }
175 if (align == FlexAlign::FLEX_END) {
176 return "ItemAlign.End";
177 }
178 if (align == FlexAlign::BASELINE) {
179 return "ItemAlign.Baseline";
180 }
181 if (align == FlexAlign::STRETCH) {
182 return "ItemAlign.Stretch";
183 }
184 return "ItemAlign.Auto";
185 }
186
ConvertWrapAlignmentToStirng(WrapAlignment align)187 inline std::string ConvertWrapAlignmentToStirng(WrapAlignment align)
188 {
189 if (align == WrapAlignment::START) {
190 return "FlexAlign.Start";
191 } else if (align == WrapAlignment::CENTER) {
192 return "FlexAlign.Center";
193 } else if (align == WrapAlignment::END) {
194 return "FlexAlign.End";
195 } else if (align == WrapAlignment::SPACE_BETWEEN) {
196 return "FlexAlign.SpaceBetween";
197 } else if (align == WrapAlignment::SPACE_AROUND) {
198 return "FlexAlign.SpaceAround";
199 } else if (align == WrapAlignment::STRETCH) {
200 return "FlexAlign.Stretch";
201 } else if (align == WrapAlignment::BASELINE) {
202 return "FlexAlign.Baseline";
203 } else {
204 return "FlexAlign.SpaceEvenly";
205 }
206 }
207
ConvertWrapTextDecorationToStirng(TextDecoration decoration)208 inline std::string ConvertWrapTextDecorationToStirng(TextDecoration decoration)
209 {
210 static const LinearEnumMapNode<TextDecoration, std::string> decorationTable[] = {
211 { TextDecoration::NONE, "TextDecorationType.None" },
212 { TextDecoration::UNDERLINE, "TextDecorationType.Underline" },
213 { TextDecoration::OVERLINE, "TextDecorationType.Overline" },
214 { TextDecoration::LINE_THROUGH, "TextDecorationType.LineThrough" },
215 { TextDecoration::INHERIT, "TextDecorationType.Inherit" },
216 };
217
218 auto index = BinarySearchFindIndex(decorationTable, ArraySize(decorationTable), decoration);
219 return index < 0 ? "TextDecorationType.None" : decorationTable[index].value;
220 }
221
ConvertWrapTextCaseToStirng(TextCase textCase)222 inline std::string ConvertWrapTextCaseToStirng(TextCase textCase)
223 {
224 static const LinearEnumMapNode<TextCase, std::string> textCaseTable[] = {
225 { TextCase::NORMAL, "TextCase.Normal" },
226 { TextCase::LOWERCASE, "TextCase.LowerCase" },
227 { TextCase::UPPERCASE, "TextCase.UpperCase" },
228 };
229
230 auto index = BinarySearchFindIndex(textCaseTable, ArraySize(textCaseTable), textCase);
231 return index < 0 ? "TextCase.Normal" : textCaseTable[index].value;
232 }
233
ConvertWrapImageFitToString(ImageFit imageFit)234 inline std::string ConvertWrapImageFitToString(ImageFit imageFit)
235 {
236 static const LinearEnumMapNode<ImageFit, std::string> imageFitTable[] = {
237 { ImageFit::COVER, "ImageFit.Cover" },
238 { ImageFit::FILL, "ImageFit.Fill" },
239 { ImageFit::CONTAIN, "ImageFit.Contain" },
240 { ImageFit::FITWIDTH, "ImageFit.FitWidth" },
241 { ImageFit::FITHEIGHT, "ImageFit.FitHeight" },
242 { ImageFit::NONE, "ImageFit.None" },
243 { ImageFit::SCALE_DOWN, "ImageFit.ScaleDown" },
244 };
245
246 auto index = BinarySearchFindIndex(imageFitTable, ArraySize(imageFitTable), imageFit);
247 return index < 0 ? "ImageFit.Cover" : imageFitTable[index].value;
248 }
249
ConvertWrapImageRepeatToString(ImageRepeat repeat)250 inline std::string ConvertWrapImageRepeatToString(ImageRepeat repeat)
251 {
252 static const LinearEnumMapNode<ImageRepeat, std::string> imageRepeatTable[] = {
253 { ImageRepeat::NO_REPEAT, "ImageRepeat.NoRepeat" },
254 { ImageRepeat::REPEAT, "ImageRepeat.XY" },
255 { ImageRepeat::REPEAT_X, "ImageRepeat.X" },
256 { ImageRepeat::REPEAT_Y, "ImageRepeat.Y" },
257 };
258
259 auto index = BinarySearchFindIndex(imageRepeatTable, ArraySize(imageRepeatTable), repeat);
260 return index < 0 ? "ImageRepeat.NoRepeat" : imageRepeatTable[index].value;
261 }
262
ConvertWrapImageInterpolationToString(ImageInterpolation imageInterpolation)263 inline std::string ConvertWrapImageInterpolationToString(ImageInterpolation imageInterpolation)
264 {
265 static const LinearEnumMapNode<ImageInterpolation, std::string> imageInterpolationTable[] = {
266 { ImageInterpolation::NONE, "ImageInterpolation.None" },
267 { ImageInterpolation::LOW, "ImageInterpolation.Low" },
268 { ImageInterpolation::MEDIUM, "ImageInterpolation.Medium" },
269 { ImageInterpolation::HIGH, "ImageInterpolation.High" },
270 };
271
272 auto index =
273 BinarySearchFindIndex(imageInterpolationTable, ArraySize(imageInterpolationTable), imageInterpolation);
274 return index < 0 ? "ImageInterpolation.None" : imageInterpolationTable[index].value;
275 }
276
ConvertWrapImageRenderModeToString(ImageRenderMode imageRenderMode)277 inline std::string ConvertWrapImageRenderModeToString(ImageRenderMode imageRenderMode)
278 {
279 static const LinearEnumMapNode<ImageRenderMode, std::string> imageRenderModeTable[] = {
280 { ImageRenderMode::ORIGINAL, "ImageRenderMode.Original" },
281 { ImageRenderMode::TEMPLATE, "ImageRenderMode.Template" },
282 };
283
284 auto index = BinarySearchFindIndex(imageRenderModeTable, ArraySize(imageRenderModeTable), imageRenderMode);
285 return index < 0 ? "ImageRenderMode.Original" : imageRenderModeTable[index].value;
286 }
287
ConvertWrapTextAlignToString(TextAlign textAlign)288 inline std::string ConvertWrapTextAlignToString(TextAlign textAlign)
289 {
290 static const LinearEnumMapNode<TextAlign, std::string> textAlignTable[] = {
291 { TextAlign::LEFT, "TextAlign.Left" },
292 { TextAlign::RIGHT, "TextAlign.Right" },
293 { TextAlign::CENTER, "TextAlign.Center" },
294 { TextAlign::JUSTIFY, "TextAlign.Justify" },
295 { TextAlign::START, "TextAlign.Start" },
296 { TextAlign::END, "TextAlign.End" },
297 };
298
299 auto index = BinarySearchFindIndex(textAlignTable, ArraySize(textAlignTable), textAlign);
300 return index < 0 ? "TextAlign.Start" : textAlignTable[index].value;
301 }
302
ConvertWrapTextOverflowToString(TextOverflow textOverflow)303 inline std::string ConvertWrapTextOverflowToString(TextOverflow textOverflow)
304 {
305 static const LinearEnumMapNode<TextOverflow, std::string> textOverflowTable[] = {
306 { TextOverflow::CLIP, "TextOverflow.Clip" },
307 { TextOverflow::ELLIPSIS, "TextOverflow.Ellipsis" },
308 { TextOverflow::NONE, "TextOverflow.None" },
309 };
310
311 auto index = BinarySearchFindIndex(textOverflowTable, ArraySize(textOverflowTable), textOverflow);
312 return index < 0 ? "TextAlign.Start" : textOverflowTable[index].value;
313 }
314
ConvertWrapFontStyleToStirng(FontStyle fontStyle)315 inline std::string ConvertWrapFontStyleToStirng(FontStyle fontStyle)
316 {
317 static const LinearEnumMapNode<FontStyle, std::string> fontStyleTable[] = {
318 { FontStyle::NORMAL, "FontStyle.Normal" },
319 { FontStyle::ITALIC, "FontStyle.Italic" },
320 };
321
322 auto index = BinarySearchFindIndex(fontStyleTable, ArraySize(fontStyleTable), fontStyle);
323 return index < 0 ? "FontStyle.Normal" : fontStyleTable[index].value;
324 }
325
ConvertWrapFontWeightToStirng(FontWeight fontWeight)326 inline std::string ConvertWrapFontWeightToStirng(FontWeight fontWeight)
327 {
328 static const LinearEnumMapNode<FontWeight, std::string> fontWeightTable[] = {
329 { FontWeight::W100, "100" },
330 { FontWeight::W200, "200" },
331 { FontWeight::W300, "300" },
332 { FontWeight::W400, "400" },
333 { FontWeight::W500, "500" },
334 { FontWeight::W600, "600" },
335 { FontWeight::W700, "700" },
336 { FontWeight::W800, "800" },
337 { FontWeight::W900, "900" },
338 { FontWeight::BOLD, "FontWeight.Bold" },
339 { FontWeight::NORMAL, "FontWeight.Normal" },
340 { FontWeight::BOLDER, "FontWeight.Bolder" },
341 { FontWeight::LIGHTER, "FontWeight.Lighter" },
342 { FontWeight::MEDIUM, "FontWeight.Medium" },
343 { FontWeight::REGULAR, "FontWeight.Regular" },
344 };
345
346 auto index = BinarySearchFindIndex(fontWeightTable, ArraySize(fontWeightTable), fontWeight);
347 return index < 0 ? "FontWeight.Normal" : fontWeightTable[index].value;
348 }
349
ConvertWrapCopyOptionToString(CopyOptions copyOptions)350 inline std::string ConvertWrapCopyOptionToString(CopyOptions copyOptions)
351 {
352 static const LinearEnumMapNode<CopyOptions, std::string> copyOptionsTable[] = {
353 { CopyOptions::None, "CopyOptions::None" },
354 { CopyOptions::InApp, "CopyOptions:InApp" },
355 { CopyOptions::Local, "CopyOptions:Local" },
356 { CopyOptions::Distributed, "CopyOptions:Distributed" },
357 };
358
359 auto index = BinarySearchFindIndex(copyOptionsTable, ArraySize(copyOptionsTable), copyOptions);
360 return index < 0 ? "CopyOptions::None" : copyOptionsTable[index].value;
361 }
362
ConvertColorToString(Color color)363 inline std::string ConvertColorToString(Color color)
364 {
365 return color.ColorToString();
366 }
367
ConvertAxisToString(Axis axis)368 inline std::string ConvertAxisToString(Axis axis)
369 {
370 return AXIS_TYPE[static_cast<int32_t>(axis)];
371 }
372
ConvertSideToString(AlignDeclaration::Edge edge)373 inline std::string ConvertSideToString(AlignDeclaration::Edge edge)
374 {
375 if (edge == AlignDeclaration::Edge::TOP) {
376 return "Edge::Top";
377 } else if (edge == AlignDeclaration::Edge::CENTER) {
378 return "Edge::Center";
379 } else if (edge == AlignDeclaration::Edge::BOTTOM) {
380 return "Edge::Bottom";
381 } else if (edge == AlignDeclaration::Edge::BASELINE) {
382 return "Edge::Baseline";
383 } else if (edge == AlignDeclaration::Edge::START) {
384 return "Edge::Start";
385 } else if (edge == AlignDeclaration::Edge::MIDDLE) {
386 return "Edge::Middle";
387 } else if (edge == AlignDeclaration::Edge::END) {
388 return "Edge::End";
389 } else {
390 return "Edge::Center";
391 }
392 }
393
ConvertFontFamily(const std::vector<std::string> & fontFamily)394 inline std::string ConvertFontFamily(const std::vector<std::string>& fontFamily)
395 {
396 std::string result;
397 for (const auto& item : fontFamily) {
398 result += item;
399 result += ",";
400 }
401 result = result.substr(0, result.size() - 1);
402 return result;
403 }
404
405 std::string GetTextStyleInJson(const TextStyle& textStyle);
406
407 } // namespace OHOS::Ace::V2
408
409 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_V2_INSPECTOR_UTILS_H