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_v2/inspector/shape_container_composed_element.h"
20 #include "core/components_v2/inspector/utils.h"
21
22 namespace OHOS::Ace::V2 {
23 namespace {
24
25 const std::unordered_map<std::string, std::function<std::string(const ShapeComposedElement&)>> CREATE_JSON_MAP {
__anon055a88570202(const ShapeComposedElement& inspector) 26 { "fill", [](const ShapeComposedElement& inspector) { return inspector.GetFill(); } },
__anon055a88570302(const ShapeComposedElement& inspector) 27 { "fillOpacity", [](const ShapeComposedElement& inspector) { return inspector.GetFillOpacity(); } },
__anon055a88570402(const ShapeComposedElement& inspector) 28 { "stroke", [](const ShapeComposedElement& inspector) { return inspector.GetStroke(); } },
__anon055a88570502(const ShapeComposedElement& inspector) 29 { "strokeDashOffset", [](const ShapeComposedElement& inspector) { return inspector.GetStrokeDashOffset(); } },
__anon055a88570602(const ShapeComposedElement& inspector) 30 { "strokeLineCap", [](const ShapeComposedElement& inspector) { return inspector.GetStrokeLineCap(); } },
__anon055a88570702(const ShapeComposedElement& inspector) 31 { "strokeLineJoin", [](const ShapeComposedElement& inspector) { return inspector.GetStrokeLineJoin(); } },
__anon055a88570802(const ShapeComposedElement& inspector) 32 { "strokeMiterLimit", [](const ShapeComposedElement& inspector) { return inspector.GetStrokeMiterLimit(); } },
__anon055a88570902(const ShapeComposedElement& inspector) 33 { "strokeOpacity", [](const ShapeComposedElement& inspector) { return inspector.GetStrokeOpacity(); } },
__anon055a88570a02(const ShapeComposedElement& inspector) 34 { "strokeWidth", [](const ShapeComposedElement& inspector) { return inspector.GetStrokeWidth(); } },
__anon055a88570b02(const ShapeComposedElement& inspector) 35 { "antiAlias", [](const ShapeComposedElement& inspector) { return inspector.GetAntiAlias(); } },
__anon055a88570c02(const ShapeComposedElement& inspector) 36 { "commands", [](const ShapeComposedElement& inspector) { return inspector.GetCommands(); } }
37 };
38
39 using JsonRectType = std::function<std::unique_ptr<JsonValue>(const ShapeComposedElement&)>;
40 const std::unordered_map<std::string, JsonRectType> CREATE_JSON_JSON_VALUE_RECT_MAP {
__anon055a88570d02() 41 { "radius", [](const ShapeComposedElement& inspector) { return inspector.GetRadiusArray(); } },
__anon055a88570e02() 42 { "radiusWidth", [](const ShapeComposedElement& inspector) { return inspector.GetRadiusWidthArray(); } },
__anon055a88570f02() 43 { "radiusHeight", [](const ShapeComposedElement& inspector) { return inspector.GetRadiusHeightArray(); } }
44 };
45
46 using JsonFuncType = std::function<std::unique_ptr<JsonValue>(const ShapeComposedElement&)>;
47 const std::unordered_map<std::string, JsonFuncType> CREATE_JSON_JSON_VALUE_MAP {
__anon055a88571002() 48 { "strokeDashArray", [](const ShapeComposedElement& inspector) { return inspector.GetStrokeDashArray(); } }
49 };
50
51 using JsonPointType = std::function<std::unique_ptr<JsonValue>(const ShapeComposedElement&)>;
52 const std::unordered_map<std::string, JsonPointType> CREATE_JSON_JSON_VALUE_POINT_MAP {
__anon055a88571102() 53 { "startPoint", [](const ShapeComposedElement& inspector) { return inspector.GetStartPointArray(); } },
__anon055a88571202() 54 { "endPoint", [](const ShapeComposedElement& inspector) { return inspector.GetEndPointArray(); } },
55 };
56
57 using JsonPointsType = std::function<std::unique_ptr<JsonValue>(const ShapeComposedElement&)>;
58 const std::unordered_map<std::string, JsonPointsType> CREATE_JSON_JSON_VALUE_POINTS_MAP {
__anon055a88571302() 59 { "points", [](const ShapeComposedElement& inspector) { return inspector.GetPointsArray(); } }
60 };
61
62 } // namespace
63
Dump()64 void ShapeComposedElement::Dump()
65 {
66 InspectorComposedElement::Dump();
67 for (const auto& value : CREATE_JSON_MAP) {
68 DumpLog::GetInstance().AddDesc(std::string(value.first + ":").append(value.second(*this)));
69 }
70 }
71
GetWidth() const72 std::string ShapeComposedElement::GetWidth() const
73 {
74 auto renderShape = AceType::DynamicCast<RenderShape>(GetInspectorNode(ShapeElement::TypeId()));
75 if (renderShape) {
76 auto component = renderShape->GetShapeComponent();
77 if (component) {
78 return component->GetWidth().ToString();
79 }
80 }
81 return InspectorComposedElement::GetWidth();
82 }
83
GetHeight() const84 std::string ShapeComposedElement::GetHeight() const
85 {
86 auto renderShape = AceType::DynamicCast<RenderShape>(GetInspectorNode(ShapeElement::TypeId()));
87 if (renderShape) {
88 auto component = renderShape->GetShapeComponent();
89 if (component) {
90 return component->GetHeight().ToString();
91 }
92 }
93 return InspectorComposedElement::GetHeight();
94 }
95
ToJsonObject() const96 std::unique_ptr<JsonValue> ShapeComposedElement::ToJsonObject() const
97 {
98 auto resultJson = InspectorComposedElement::ToJsonObject();
99 auto shapeType = GetShapeType();
100 for (const auto& value : CREATE_JSON_MAP) {
101 resultJson->Put(value.first.c_str(), value.second(*this).c_str());
102 }
103
104 for (const auto& value : CREATE_JSON_JSON_VALUE_MAP) {
105 resultJson->Put(value.first.c_str(), value.second(*this));
106 }
107 if ((shapeType == "Polyline") || (shapeType == "Polygon")) {
108 for (const auto& value : CREATE_JSON_JSON_VALUE_POINTS_MAP) {
109 resultJson->Put(value.first.c_str(), value.second(*this));
110 }
111 }
112 if (shapeType == "Line") {
113 for (const auto& value : CREATE_JSON_JSON_VALUE_POINT_MAP) {
114 resultJson->Put(value.first.c_str(), value.second(*this));
115 }
116 }
117 if (shapeType == "Rect") {
118 for (const auto& value : CREATE_JSON_JSON_VALUE_RECT_MAP) {
119 resultJson->Put(value.first.c_str(), value.second(*this));
120 }
121 }
122 return resultJson;
123 }
124
GetShapeType() const125 std::string ShapeComposedElement::GetShapeType() const
126 {
127 auto render = GetContentRender<RenderShape>(ShapeElement::TypeId());
128 if (render) {
129 return SHAPE_TYPE_STRINGS[static_cast<int32_t>(render->GetShapeType())];
130 }
131 return "shape";
132 }
133
GetCommands() const134 std::string ShapeComposedElement::GetCommands() const
135 {
136 auto render = GetContentRender<RenderShape>(ShapeElement::TypeId());
137 if (render) {
138 return render->GetPathCmd();
139 }
140 return "";
141 }
142
GetAntiAlias() const143 std::string ShapeComposedElement::GetAntiAlias() const
144 {
145 auto render = GetContentRender<RenderShape>(ShapeElement::TypeId());
146 if (render) {
147 return ConvertBoolToString(render->GetAntiAlias());
148 }
149 return "";
150 }
151
GetFill() const152 std::string ShapeComposedElement::GetFill() const
153 {
154 auto render = GetContentRender<RenderShape>(ShapeElement::TypeId());
155 if (render) {
156 return render->GetFillState().GetColor().ColorToString();
157 }
158 return "";
159 }
160
GetFillOpacity() const161 std::string ShapeComposedElement::GetFillOpacity() const
162 {
163 auto render = GetContentRender<RenderShape>(ShapeElement::TypeId());
164 if (render) {
165 return std::to_string(render->GetFillState().GetOpacity().GetValue());
166 }
167 return "";
168 }
169
GetStroke() const170 std::string ShapeComposedElement::GetStroke() const
171 {
172 auto render = GetContentRender<RenderShape>(ShapeElement::TypeId());
173 if (render) {
174 return render->GetStrokeState().GetColor().ColorToString();
175 }
176 return "";
177 }
178
GetStrokeDashOffset() const179 std::string ShapeComposedElement::GetStrokeDashOffset() const
180 {
181 auto render = GetContentRender<RenderShape>(ShapeElement::TypeId());
182 if (render) {
183 return render->GetStrokeState().GetStrokeDashOffset().ToString();
184 }
185 return "";
186 }
187
GetStrokeDashArray() const188 std::unique_ptr<JsonValue> ShapeComposedElement::GetStrokeDashArray() const
189 {
190 auto render = GetContentRender<RenderShape>(ShapeElement::TypeId());
191 auto jsonDashArray = JsonUtil::CreateArray(true);
192 if (render) {
193 std::vector<Dimension> array = render->GetStrokeState().GetStrokeDashArray();
194 for (size_t i = 0; i < array.size(); i++) {
195 auto index = std::to_string(i);
196 auto value = array[i].ToString();
197 jsonDashArray->Put(index.c_str(), value.c_str());
198 }
199 }
200 return jsonDashArray;
201 }
202
GetStrokeLineCap() const203 std::string ShapeComposedElement::GetStrokeLineCap() const
204 {
205 auto render = GetContentRender<RenderShape>(ShapeElement::TypeId());
206 if (render) {
207 auto style = render->GetStrokeState().GetLineCap();
208 return ShapeContainerComposedElement::LineCapStyleToString(style);
209 }
210 return "";
211 }
212
GetStrokeLineJoin() const213 std::string ShapeComposedElement::GetStrokeLineJoin() const
214 {
215 auto render = GetContentRender<RenderShape>(ShapeElement::TypeId());
216 if (render) {
217 auto style = render->GetStrokeState().GetLineJoin();
218 return ShapeContainerComposedElement::LineJoinStyleToString(style);
219 }
220 return "";
221 }
222
GetStrokeMiterLimit() const223 std::string ShapeComposedElement::GetStrokeMiterLimit() const
224 {
225 auto render = GetContentRender<RenderShape>(ShapeElement::TypeId());
226 if (render) {
227 return std::to_string(render->GetStrokeState().GetMiterLimit());
228 }
229 return "";
230 }
231
GetStrokeOpacity() const232 std::string ShapeComposedElement::GetStrokeOpacity() const
233 {
234 auto render = GetContentRender<RenderShape>(ShapeElement::TypeId());
235 if (render) {
236 return std::to_string(render->GetStrokeState().GetOpacity().GetValue());
237 }
238 return "";
239 }
240
GetStrokeWidth() const241 std::string ShapeComposedElement::GetStrokeWidth() const
242 {
243 auto render = GetContentRender<RenderShape>(ShapeElement::TypeId());
244 if (render) {
245 return render->GetStrokeState().GetLineWidth().ToString();
246 }
247 return "";
248 }
249
GetRadiusArray() const250 std::unique_ptr<JsonValue> ShapeComposedElement::GetRadiusArray() const
251 {
252 auto render = GetContentRender<RenderShape>(ShapeElement::TypeId());
253 auto jsonRadiusArray = JsonUtil::CreateArray(true);
254 if (!render) {
255 jsonRadiusArray->Put("0", 0);
256 return jsonRadiusArray;
257 }
258 std::vector<Radius> rads = {
259 render->GetTopLeftRadius(), render->GetTopRightRadius(), render->GetBottomLeftRadius(),
260 render->GetBottomRightRadius()
261 };
262 for (size_t i = 0; i < rads.size(); i++) {
263 auto jsonObject = JsonUtil::CreateArray(true);
264 jsonObject->Put("0", rads[i].GetX().Value());
265 jsonObject->Put("1", rads[i].GetY().Value());
266 auto index = std::to_string(i);
267 jsonRadiusArray->Put(index.c_str(), jsonObject);
268 }
269 if (rads[0].GetX().Value() == -1) {
270 auto radiusArray = JsonUtil::CreateArray(true);
271 radiusArray->Put("0", 0);
272 return radiusArray;
273 }
274 return jsonRadiusArray;
275 }
276
GetRadiusHeightArray() const277 std::unique_ptr<JsonValue> ShapeComposedElement::GetRadiusHeightArray() const
278 {
279 auto render = GetContentRender<RenderShape>(ShapeElement::TypeId());
280 auto jsonRadiusArray = JsonUtil::CreateArray(true);
281 if (!render) {
282 jsonRadiusArray->Put("0", 0);
283 return jsonRadiusArray;
284 }
285 Radius topLeftRadius = render->GetTopLeftRadius();
286 Radius topRightRadius = render->GetTopRightRadius();
287 Radius bottomRightRadius = render->GetBottomRightRadius();
288 Radius bottomLeftRadius = render->GetBottomLeftRadius();
289 if (topLeftRadius.GetY().Value() == -1) {
290 jsonRadiusArray->Put("0", 0);
291 return jsonRadiusArray;
292 }
293 if ((topLeftRadius.GetY() == topRightRadius.GetY()) && (topLeftRadius.GetY() == bottomRightRadius.GetY()) &&
294 (topLeftRadius.GetY() == bottomLeftRadius.GetY())) {
295 jsonRadiusArray->Put("0", topLeftRadius.GetY().Value());
296 return jsonRadiusArray;
297 } else {
298 jsonRadiusArray->Put("0", topLeftRadius.GetY().Value());
299 jsonRadiusArray->Put("1", topRightRadius.GetY().Value());
300 jsonRadiusArray->Put("2", bottomLeftRadius.GetY().Value());
301 jsonRadiusArray->Put("3", bottomRightRadius.GetY().Value());
302 return jsonRadiusArray;
303 }
304 }
305
GetRadiusWidthArray() const306 std::unique_ptr<JsonValue> ShapeComposedElement::GetRadiusWidthArray() const
307 {
308 auto render = GetContentRender<RenderShape>(ShapeElement::TypeId());
309 auto jsonRadiusArray = JsonUtil::CreateArray(true);
310 if (!render) {
311 jsonRadiusArray->Put("0", 0);
312 return jsonRadiusArray;
313 }
314 Radius topLeftRadius = render->GetTopLeftRadius();
315 Radius topRightRadius = render->GetTopRightRadius();
316 Radius bottomRightRadius = render->GetBottomRightRadius();
317 Radius bottomLeftRadius = render->GetBottomLeftRadius();
318 if (topLeftRadius.GetX().Value() == -1) {
319 jsonRadiusArray->Put("0", 0);
320 return jsonRadiusArray;
321 }
322 if ((topLeftRadius.GetX() == topRightRadius.GetX()) && (topLeftRadius.GetX() == bottomRightRadius.GetX()) &&
323 (topLeftRadius.GetX() == bottomLeftRadius.GetX())) {
324 jsonRadiusArray->Put("0", topLeftRadius.GetX().Value());
325 return jsonRadiusArray;
326 } else {
327 jsonRadiusArray->Put("0", topLeftRadius.GetX().Value());
328 jsonRadiusArray->Put("1", topRightRadius.GetX().Value());
329 jsonRadiusArray->Put("2", bottomLeftRadius.GetX().Value());
330 jsonRadiusArray->Put("3", bottomRightRadius.GetX().Value());
331 return jsonRadiusArray;
332 }
333 }
334
GetStartPointArray() const335 std::unique_ptr<JsonValue> ShapeComposedElement::GetStartPointArray() const
336 {
337 auto render = GetContentRender<RenderShape>(ShapeElement::TypeId());
338 auto startPointArray = JsonUtil::CreateArray(true);
339 if (!render) {
340 startPointArray->Put("0", 0);
341 startPointArray->Put("1", 0);
342 return startPointArray;
343 }
344 ShapePoint startPoint = render->GetStartPoint();
345 startPointArray->Put("0", startPoint.first.Value());
346 startPointArray->Put("1", startPoint.second.Value());
347 return startPointArray;
348 }
349
GetEndPointArray() const350 std::unique_ptr<JsonValue> ShapeComposedElement::GetEndPointArray() const
351 {
352 auto render = GetContentRender<RenderShape>(ShapeElement::TypeId());
353 auto endPointArray = JsonUtil::CreateArray(true);
354 if (!render) {
355 endPointArray->Put("0", 0);
356 endPointArray->Put("1", 0);
357 return endPointArray;
358 }
359 ShapePoint endPoint = render->GetEndPoint();
360 endPointArray->Put("0", endPoint.first.Value());
361 endPointArray->Put("1", endPoint.second.Value());
362 return endPointArray;
363 }
364
GetPointsArray() const365 std::unique_ptr<JsonValue> ShapeComposedElement::GetPointsArray() const
366 {
367 auto render = GetContentRender<RenderShape>(ShapeElement::TypeId());
368 auto pointsArray = JsonUtil::CreateArray(true);
369 if (!render) {
370 return pointsArray;
371 }
372 std::vector<ShapePoint> points = render->GetPoints();
373 for (size_t i = 0; i < points.size(); i++) {
374 auto pointsObject = JsonUtil::CreateArray(true);
375 pointsObject->Put("0", points[i].first.Value());
376 pointsObject->Put("1", points[i].second.Value());
377 auto index = std::to_string(i);
378 pointsArray->Put(index.c_str(), pointsObject);
379 }
380 return pointsArray;
381 }
382
383 } // namespace OHOS::Ace::V2