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 #include "core/components_v2/inspector/shape_composed_element.h"
17
18 #include "base/log/dump_log.h"
19 #include "core/components/shape/shape_element.h"
20 #include "core/components_v2/inspector/shape_container_composed_element.h"
21 #include "core/components_v2/inspector/utils.h"
22
23 namespace OHOS::Ace::V2 {
24 namespace {
25
26 const std::unordered_map<std::string, std::function<std::string(const ShapeComposedElement&)>> CREATE_JSON_MAP {
__anoncc0f25ed0202(const ShapeComposedElement& inspector) 27 { "fill", [](const ShapeComposedElement& inspector) { return inspector.GetFill(); } },
__anoncc0f25ed0302(const ShapeComposedElement& inspector) 28 { "fillOpacity", [](const ShapeComposedElement& inspector) { return inspector.GetFillOpacity(); } },
__anoncc0f25ed0402(const ShapeComposedElement& inspector) 29 { "stroke", [](const ShapeComposedElement& inspector) { return inspector.GetStroke(); } },
__anoncc0f25ed0502(const ShapeComposedElement& inspector) 30 { "strokeDashOffset", [](const ShapeComposedElement& inspector) { return inspector.GetStrokeDashOffset(); } },
__anoncc0f25ed0602(const ShapeComposedElement& inspector) 31 { "strokeLineCap", [](const ShapeComposedElement& inspector) { return inspector.GetStrokeLineCap(); } },
__anoncc0f25ed0702(const ShapeComposedElement& inspector) 32 { "strokeLineJoin", [](const ShapeComposedElement& inspector) { return inspector.GetStrokeLineJoin(); } },
__anoncc0f25ed0802(const ShapeComposedElement& inspector) 33 { "strokeMiterLimit", [](const ShapeComposedElement& inspector) { return inspector.GetStrokeMiterLimit(); } },
__anoncc0f25ed0902(const ShapeComposedElement& inspector) 34 { "strokeOpacity", [](const ShapeComposedElement& inspector) { return inspector.GetStrokeOpacity(); } },
__anoncc0f25ed0a02(const ShapeComposedElement& inspector) 35 { "strokeWidth", [](const ShapeComposedElement& inspector) { return inspector.GetStrokeWidth(); } },
__anoncc0f25ed0b02(const ShapeComposedElement& inspector) 36 { "antiAlias", [](const ShapeComposedElement& inspector) { return inspector.GetAntiAlias(); } },
__anoncc0f25ed0c02(const ShapeComposedElement& inspector) 37 { "commands", [](const ShapeComposedElement& inspector) { return inspector.GetCommands(); } }
38 };
39
40 using JsonRectType = std::function<std::unique_ptr<JsonValue>(const ShapeComposedElement&)>;
41 const std::unordered_map<std::string, JsonRectType> CREATE_JSON_JSON_VALUE_RECT_MAP {
__anoncc0f25ed0d02() 42 { "radius", [](const ShapeComposedElement& inspector) { return inspector.GetRadiusArray(); } },
__anoncc0f25ed0e02() 43 { "radiusWidth", [](const ShapeComposedElement& inspector) { return inspector.GetRadiusWidthArray(); } },
__anoncc0f25ed0f02() 44 { "radiusHeight", [](const ShapeComposedElement& inspector) { return inspector.GetRadiusHeightArray(); } }
45 };
46
47 using JsonFuncType = std::function<std::unique_ptr<JsonValue>(const ShapeComposedElement&)>;
48 const std::unordered_map<std::string, JsonFuncType> CREATE_JSON_JSON_VALUE_MAP {
__anoncc0f25ed1002() 49 { "strokeDashArray", [](const ShapeComposedElement& inspector) { return inspector.GetStrokeDashArray(); } }
50 };
51
52 using JsonPointType = std::function<std::unique_ptr<JsonValue>(const ShapeComposedElement&)>;
53 const std::unordered_map<std::string, JsonPointType> CREATE_JSON_JSON_VALUE_POINT_MAP {
__anoncc0f25ed1102() 54 { "startPoint", [](const ShapeComposedElement& inspector) { return inspector.GetStartPointArray(); } },
__anoncc0f25ed1202() 55 { "endPoint", [](const ShapeComposedElement& inspector) { return inspector.GetEndPointArray(); } },
56 };
57
58 using JsonPointsType = std::function<std::unique_ptr<JsonValue>(const ShapeComposedElement&)>;
59 const std::unordered_map<std::string, JsonPointsType> CREATE_JSON_JSON_VALUE_POINTS_MAP {
__anoncc0f25ed1302() 60 { "points", [](const ShapeComposedElement& inspector) { return inspector.GetPointsArray(); } }
61 };
62
63 } // namespace
64
Dump()65 void ShapeComposedElement::Dump()
66 {
67 InspectorComposedElement::Dump();
68 for (const auto& value : CREATE_JSON_MAP) {
69 DumpLog::GetInstance().AddDesc(std::string(value.first + ":").append(value.second(*this)));
70 }
71 }
72
GetWidth() const73 std::string ShapeComposedElement::GetWidth() const
74 {
75 auto renderShape = AceType::DynamicCast<RenderShape>(GetInspectorNode(ShapeElement::TypeId()));
76 if (renderShape) {
77 auto component = renderShape->GetShapeComponent();
78 if (component) {
79 return component->GetWidth().ToString();
80 }
81 }
82 return InspectorComposedElement::GetWidth();
83 }
84
GetHeight() const85 std::string ShapeComposedElement::GetHeight() const
86 {
87 auto renderShape = AceType::DynamicCast<RenderShape>(GetInspectorNode(ShapeElement::TypeId()));
88 if (renderShape) {
89 auto component = renderShape->GetShapeComponent();
90 if (component) {
91 return component->GetHeight().ToString();
92 }
93 }
94 return InspectorComposedElement::GetHeight();
95 }
96
ToJsonObject() const97 std::unique_ptr<JsonValue> ShapeComposedElement::ToJsonObject() const
98 {
99 auto resultJson = InspectorComposedElement::ToJsonObject();
100 auto shapeType = GetShapeType();
101 for (const auto& value : CREATE_JSON_MAP) {
102 resultJson->Put(value.first.c_str(), value.second(*this).c_str());
103 }
104
105 for (const auto& value : CREATE_JSON_JSON_VALUE_MAP) {
106 resultJson->Put(value.first.c_str(), value.second(*this));
107 }
108 if ((shapeType == "Polyline") || (shapeType == "Polygon")) {
109 for (const auto& value : CREATE_JSON_JSON_VALUE_POINTS_MAP) {
110 resultJson->Put(value.first.c_str(), value.second(*this));
111 }
112 }
113 if (shapeType == "Line") {
114 for (const auto& value : CREATE_JSON_JSON_VALUE_POINT_MAP) {
115 resultJson->Put(value.first.c_str(), value.second(*this));
116 }
117 }
118 if (shapeType == "Rect") {
119 for (const auto& value : CREATE_JSON_JSON_VALUE_RECT_MAP) {
120 resultJson->Put(value.first.c_str(), value.second(*this));
121 }
122 }
123 return resultJson;
124 }
125
GetShapeType() const126 std::string ShapeComposedElement::GetShapeType() const
127 {
128 auto render = GetContentRender<RenderShape>(ShapeElement::TypeId());
129 if (render) {
130 return SHAPE_TYPE_STRINGS[static_cast<int32_t>(render->GetShapeType())];
131 }
132 return "shape";
133 }
134
GetCommands() const135 std::string ShapeComposedElement::GetCommands() const
136 {
137 auto render = GetContentRender<RenderShape>(ShapeElement::TypeId());
138 if (render) {
139 return render->GetPathCmd();
140 }
141 return "";
142 }
143
GetAntiAlias() const144 std::string ShapeComposedElement::GetAntiAlias() const
145 {
146 auto render = GetContentRender<RenderShape>(ShapeElement::TypeId());
147 if (render) {
148 return ConvertBoolToString(render->GetAntiAlias());
149 }
150 return "";
151 }
152
GetFill() const153 std::string ShapeComposedElement::GetFill() const
154 {
155 auto render = GetContentRender<RenderShape>(ShapeElement::TypeId());
156 if (render) {
157 return render->GetFillState().GetColor().ColorToString();
158 }
159 return "";
160 }
161
GetFillOpacity() const162 std::string ShapeComposedElement::GetFillOpacity() const
163 {
164 auto render = GetContentRender<RenderShape>(ShapeElement::TypeId());
165 if (render) {
166 return std::to_string(render->GetFillState().GetOpacity().GetValue());
167 }
168 return "";
169 }
170
GetStroke() const171 std::string ShapeComposedElement::GetStroke() const
172 {
173 auto render = GetContentRender<RenderShape>(ShapeElement::TypeId());
174 if (render) {
175 return render->GetStrokeState().GetColor().ColorToString();
176 }
177 return "";
178 }
179
GetStrokeDashOffset() const180 std::string ShapeComposedElement::GetStrokeDashOffset() const
181 {
182 auto render = GetContentRender<RenderShape>(ShapeElement::TypeId());
183 if (render) {
184 return render->GetStrokeState().GetStrokeDashOffset().ToString();
185 }
186 return "";
187 }
188
GetStrokeDashArray() const189 std::unique_ptr<JsonValue> ShapeComposedElement::GetStrokeDashArray() const
190 {
191 auto render = GetContentRender<RenderShape>(ShapeElement::TypeId());
192 auto jsonDashArray = JsonUtil::CreateArray(true);
193 if (render) {
194 std::vector<Dimension> array = render->GetStrokeState().GetStrokeDashArray();
195 for (size_t i = 0; i < array.size(); i++) {
196 auto index = std::to_string(i);
197 auto value = array[i].ToString();
198 jsonDashArray->Put(index.c_str(), value.c_str());
199 }
200 }
201 return jsonDashArray;
202 }
203
GetStrokeLineCap() const204 std::string ShapeComposedElement::GetStrokeLineCap() const
205 {
206 auto render = GetContentRender<RenderShape>(ShapeElement::TypeId());
207 if (render) {
208 auto style = render->GetStrokeState().GetLineCap();
209 return ShapeContainerComposedElement::LineCapStyleToString(style);
210 }
211 return "";
212 }
213
GetStrokeLineJoin() const214 std::string ShapeComposedElement::GetStrokeLineJoin() const
215 {
216 auto render = GetContentRender<RenderShape>(ShapeElement::TypeId());
217 if (render) {
218 auto style = render->GetStrokeState().GetLineJoin();
219 return ShapeContainerComposedElement::LineJoinStyleToString(style);
220 }
221 return "";
222 }
223
GetStrokeMiterLimit() const224 std::string ShapeComposedElement::GetStrokeMiterLimit() const
225 {
226 auto render = GetContentRender<RenderShape>(ShapeElement::TypeId());
227 if (render) {
228 return std::to_string(render->GetStrokeState().GetMiterLimit());
229 }
230 return "";
231 }
232
GetStrokeOpacity() const233 std::string ShapeComposedElement::GetStrokeOpacity() const
234 {
235 auto render = GetContentRender<RenderShape>(ShapeElement::TypeId());
236 if (render) {
237 return std::to_string(render->GetStrokeState().GetOpacity().GetValue());
238 }
239 return "";
240 }
241
GetStrokeWidth() const242 std::string ShapeComposedElement::GetStrokeWidth() const
243 {
244 auto render = GetContentRender<RenderShape>(ShapeElement::TypeId());
245 if (render) {
246 return render->GetStrokeState().GetLineWidth().ToString();
247 }
248 return "";
249 }
250
GetRadiusArray() const251 std::unique_ptr<JsonValue> ShapeComposedElement::GetRadiusArray() const
252 {
253 auto render = GetContentRender<RenderShape>(ShapeElement::TypeId());
254 auto jsonRadiusArray = JsonUtil::CreateArray(true);
255 if (!render) {
256 jsonRadiusArray->Put("0", 0);
257 return jsonRadiusArray;
258 }
259 std::vector<Radius> rads = {
260 render->GetTopLeftRadius(), render->GetTopRightRadius(), render->GetBottomLeftRadius(),
261 render->GetBottomRightRadius()
262 };
263 for (size_t i = 0; i < rads.size(); i++) {
264 auto jsonObject = JsonUtil::CreateArray(true);
265 jsonObject->Put("0", rads[i].GetX().Value());
266 jsonObject->Put("1", rads[i].GetY().Value());
267 auto index = std::to_string(i);
268 jsonRadiusArray->Put(index.c_str(), jsonObject);
269 }
270 if (rads[0].GetX().Value() == -1) {
271 auto radiusArray = JsonUtil::CreateArray(true);
272 radiusArray->Put("0", 0);
273 return radiusArray;
274 }
275 return jsonRadiusArray;
276 }
277
GetRadiusHeightArray() const278 std::unique_ptr<JsonValue> ShapeComposedElement::GetRadiusHeightArray() const
279 {
280 auto render = GetContentRender<RenderShape>(ShapeElement::TypeId());
281 auto jsonRadiusArray = JsonUtil::CreateArray(true);
282 if (!render) {
283 jsonRadiusArray->Put("0", 0);
284 return jsonRadiusArray;
285 }
286 Radius topLeftRadius = render->GetTopLeftRadius();
287 Radius topRightRadius = render->GetTopRightRadius();
288 Radius bottomRightRadius = render->GetBottomRightRadius();
289 Radius bottomLeftRadius = render->GetBottomLeftRadius();
290 if (topLeftRadius.GetY().Value() == -1) {
291 jsonRadiusArray->Put("0", 0);
292 return jsonRadiusArray;
293 }
294 if ((topLeftRadius.GetY() == topRightRadius.GetY()) && (topLeftRadius.GetY() == bottomRightRadius.GetY()) &&
295 (topLeftRadius.GetY() == bottomLeftRadius.GetY())) {
296 jsonRadiusArray->Put("0", topLeftRadius.GetY().Value());
297 return jsonRadiusArray;
298 } else {
299 jsonRadiusArray->Put("0", topLeftRadius.GetY().Value());
300 jsonRadiusArray->Put("1", topRightRadius.GetY().Value());
301 jsonRadiusArray->Put("2", bottomLeftRadius.GetY().Value());
302 jsonRadiusArray->Put("3", bottomRightRadius.GetY().Value());
303 return jsonRadiusArray;
304 }
305 }
306
GetRadiusWidthArray() const307 std::unique_ptr<JsonValue> ShapeComposedElement::GetRadiusWidthArray() const
308 {
309 auto render = GetContentRender<RenderShape>(ShapeElement::TypeId());
310 auto jsonRadiusArray = JsonUtil::CreateArray(true);
311 if (!render) {
312 jsonRadiusArray->Put("0", 0);
313 return jsonRadiusArray;
314 }
315 Radius topLeftRadius = render->GetTopLeftRadius();
316 Radius topRightRadius = render->GetTopRightRadius();
317 Radius bottomRightRadius = render->GetBottomRightRadius();
318 Radius bottomLeftRadius = render->GetBottomLeftRadius();
319 if (topLeftRadius.GetX().Value() == -1) {
320 jsonRadiusArray->Put("0", 0);
321 return jsonRadiusArray;
322 }
323 if ((topLeftRadius.GetX() == topRightRadius.GetX()) && (topLeftRadius.GetX() == bottomRightRadius.GetX()) &&
324 (topLeftRadius.GetX() == bottomLeftRadius.GetX())) {
325 jsonRadiusArray->Put("0", topLeftRadius.GetX().Value());
326 return jsonRadiusArray;
327 } else {
328 jsonRadiusArray->Put("0", topLeftRadius.GetX().Value());
329 jsonRadiusArray->Put("1", topRightRadius.GetX().Value());
330 jsonRadiusArray->Put("2", bottomLeftRadius.GetX().Value());
331 jsonRadiusArray->Put("3", bottomRightRadius.GetX().Value());
332 return jsonRadiusArray;
333 }
334 }
335
GetStartPointArray() const336 std::unique_ptr<JsonValue> ShapeComposedElement::GetStartPointArray() const
337 {
338 auto render = GetContentRender<RenderShape>(ShapeElement::TypeId());
339 auto startPointArray = JsonUtil::CreateArray(true);
340 if (!render) {
341 startPointArray->Put("0", 0);
342 startPointArray->Put("1", 0);
343 return startPointArray;
344 }
345 ShapePoint startPoint = render->GetStartPoint();
346 startPointArray->Put("0", startPoint.first.Value());
347 startPointArray->Put("1", startPoint.second.Value());
348 return startPointArray;
349 }
350
GetEndPointArray() const351 std::unique_ptr<JsonValue> ShapeComposedElement::GetEndPointArray() const
352 {
353 auto render = GetContentRender<RenderShape>(ShapeElement::TypeId());
354 auto endPointArray = JsonUtil::CreateArray(true);
355 if (!render) {
356 endPointArray->Put("0", 0);
357 endPointArray->Put("1", 0);
358 return endPointArray;
359 }
360 ShapePoint endPoint = render->GetEndPoint();
361 endPointArray->Put("0", endPoint.first.Value());
362 endPointArray->Put("1", endPoint.second.Value());
363 return endPointArray;
364 }
365
GetPointsArray() const366 std::unique_ptr<JsonValue> ShapeComposedElement::GetPointsArray() const
367 {
368 auto render = GetContentRender<RenderShape>(ShapeElement::TypeId());
369 auto pointsArray = JsonUtil::CreateArray(true);
370 if (!render) {
371 return pointsArray;
372 }
373 std::vector<ShapePoint> points = render->GetPoints();
374 for (size_t i = 0; i < points.size(); i++) {
375 auto pointsObject = JsonUtil::CreateArray(true);
376 pointsObject->Put("0", points[i].first.Value());
377 pointsObject->Put("1", points[i].second.Value());
378 auto index = std::to_string(i);
379 pointsArray->Put(index.c_str(), pointsObject);
380 }
381 return pointsArray;
382 }
383
384 } // namespace OHOS::Ace::V2