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_container_composed_element.h"
17
18 #include "base/log/dump_log.h"
19 #include "core/components_v2/inspector/utils.h"
20
21 namespace OHOS::Ace::V2 {
22 namespace {
23
24 using StrFuncType = std::function<std::string(const ShapeContainerComposedElement&)>;
25 const std::unordered_map<std::string, StrFuncType> CREATE_JSON_MAP {
__anoneae10cb90202() 26 { "fill", [](const ShapeContainerComposedElement& inspector) { return inspector.GetFill(); } },
__anoneae10cb90302() 27 { "fillOpacity", [](const ShapeContainerComposedElement& inspector) { return inspector.GetFillOpacity(); } },
__anoneae10cb90402() 28 { "stroke", [](const ShapeContainerComposedElement& inspector) { return inspector.GetStroke(); } },
29 { "strokeDashOffset",
__anoneae10cb90502() 30 [](const ShapeContainerComposedElement& inspector) { return inspector.GetStrokeDashOffset(); } },
__anoneae10cb90602() 31 { "strokeLineCap", [](const ShapeContainerComposedElement& inspector) { return inspector.GetStrokeLineCap(); } },
__anoneae10cb90702() 32 { "strokeLineJoin", [](const ShapeContainerComposedElement& inspector) { return inspector.GetStrokeLineJoin(); } },
33 { "strokeMiterLimit",
__anoneae10cb90802() 34 [](const ShapeContainerComposedElement& inspector) { return inspector.GetStrokeMiterLimit(); } },
__anoneae10cb90902() 35 { "strokeOpacity", [](const ShapeContainerComposedElement& inspector) { return inspector.GetStrokeOpacity(); } },
__anoneae10cb90a02() 36 { "strokeWidth", [](const ShapeContainerComposedElement& inspector) { return inspector.GetStrokeWidth(); } },
__anoneae10cb90b02() 37 { "antiAlias", [](const ShapeContainerComposedElement& inspector) { return inspector.GetAntiAlias(); } },
38 };
39
40 using JsonFuncType = std::function<std::unique_ptr<JsonValue>(const ShapeContainerComposedElement&)>;
41 const std::unordered_map<std::string, JsonFuncType> CREATE_JSON_JSON_VALUE_MAP {
42 { "strokeDashArray",
__anoneae10cb90c02() 43 [](const ShapeContainerComposedElement& inspector) { return inspector.GetStrokeDashArray(); } },
__anoneae10cb90d02() 44 { "viewPort", [](const ShapeContainerComposedElement& inspector) { return inspector.GetViewBox(); } },
__anoneae10cb90e02() 45 { "mesh", [](const ShapeContainerComposedElement& inspector) { return inspector.GetMesh(); } },
46 };
47
48 }
49
LineCapStyleToString(LineCapStyle lineCapStyle)50 std::string ShapeContainerComposedElement::LineCapStyleToString(LineCapStyle lineCapStyle)
51 {
52 static const std::unordered_map<LineCapStyle, std::string> STYLE_MAP {
53 { LineCapStyle::BUTT, "LineCapStyle.Butt" },
54 { LineCapStyle::ROUND, "LineCapStyle.Round" },
55 { LineCapStyle::SQUARE, "LineCapStyle.Square" },
56 };
57
58 auto pos = STYLE_MAP.find(lineCapStyle);
59 if (pos != STYLE_MAP.end()) {
60 return pos->second;
61 }
62 return "";
63 }
64
LineJoinStyleToString(LineJoinStyle lineJoinStyle)65 std::string ShapeContainerComposedElement::LineJoinStyleToString(LineJoinStyle lineJoinStyle)
66 {
67 static const std::unordered_map<LineJoinStyle, std::string> STYLE_MAP {
68 { LineJoinStyle::MITER, "LineJoinStyle.Miter" },
69 { LineJoinStyle::BEVEL, "LineJoinStyle.Bevel" },
70 { LineJoinStyle::ROUND, "LineJoinStyle.Round" },
71 };
72
73 auto pos = STYLE_MAP.find(lineJoinStyle);
74 if (pos != STYLE_MAP.end()) {
75 return pos->second;
76 }
77 return "";
78 }
79
Dump()80 void ShapeContainerComposedElement::Dump()
81 {
82 InspectorComposedElement::Dump();
83 for (const auto& value : CREATE_JSON_MAP) {
84 DumpLog::GetInstance().AddDesc(std::string(value.first + ": ").append(value.second(*this)));
85 }
86 }
87
ToJsonObject() const88 std::unique_ptr<JsonValue> ShapeContainerComposedElement::ToJsonObject() const
89 {
90 auto resultJson = InspectorComposedElement::ToJsonObject();
91 for (const auto& value : CREATE_JSON_MAP) {
92 resultJson->Put(value.first.c_str(), value.second(*this).c_str());
93 }
94 for (const auto& value : CREATE_JSON_JSON_VALUE_MAP) {
95 resultJson->Put(value.first.c_str(), value.second(*this));
96 }
97 return resultJson;
98 }
99
GetAntiAlias() const100 std::string ShapeContainerComposedElement::GetAntiAlias() const
101 {
102 auto render = GetContentRender<RenderShapeContainer>(ShapeContainerElement::TypeId());
103 if (render) {
104 return ConvertBoolToString(render->GetAntiAlias());
105 }
106 return "";
107 }
108
GetFill() const109 std::string ShapeContainerComposedElement::GetFill() const
110 {
111 auto render = GetContentRender<RenderShapeContainer>(ShapeContainerElement::TypeId());
112 if (render) {
113 return render->GetFillState().GetColor().ColorToString();
114 }
115 return "";
116 }
117
GetFillOpacity() const118 std::string ShapeContainerComposedElement::GetFillOpacity() const
119 {
120 auto render = GetContentRender<RenderShapeContainer>(ShapeContainerElement::TypeId());
121 if (render) {
122 return std::to_string(render->GetFillState().GetOpacity().GetValue());
123 }
124 return "";
125 }
126
GetStroke() const127 std::string ShapeContainerComposedElement::GetStroke() const
128 {
129 auto render = GetContentRender<RenderShapeContainer>(ShapeContainerElement::TypeId());
130 if (render) {
131 return render->GetStrokeState().GetColor().ColorToString();
132 }
133 return "";
134 }
135
GetStrokeDashOffset() const136 std::string ShapeContainerComposedElement::GetStrokeDashOffset() const
137 {
138 auto render = GetContentRender<RenderShapeContainer>(ShapeContainerElement::TypeId());
139 if (render) {
140 return render->GetStrokeState().GetStrokeDashOffset().ToString();
141 }
142 return "";
143 }
144
GetStrokeDashArray() const145 std::unique_ptr<JsonValue> ShapeContainerComposedElement::GetStrokeDashArray() const
146 {
147 auto render = GetContentRender<RenderShapeContainer>(ShapeContainerElement::TypeId());
148 auto jsonDashArray = JsonUtil::CreateArray(true);
149 if (render) {
150 std::vector<Dimension> array = render->GetStrokeState().GetStrokeDashArray();
151 for (size_t i = 0; i < array.size(); i++) {
152 auto index = std::to_string(i);
153 auto value = array[i].ToString();
154 jsonDashArray->Put(index.c_str(), value.c_str());
155 }
156 }
157 return jsonDashArray;
158 }
159
GetStrokeLineCap() const160 std::string ShapeContainerComposedElement::GetStrokeLineCap() const
161 {
162 auto render = GetContentRender<RenderShapeContainer>(ShapeContainerElement::TypeId());
163 if (render) {
164 auto style = render->GetStrokeState().GetLineCap();
165 return LineCapStyleToString(style);
166 }
167 return "";
168 }
169
GetStrokeLineJoin() const170 std::string ShapeContainerComposedElement::GetStrokeLineJoin() const
171 {
172 auto render = GetContentRender<RenderShapeContainer>(ShapeContainerElement::TypeId());
173 if (render) {
174 auto style = render->GetStrokeState().GetLineJoin();
175 return LineJoinStyleToString(style);
176 }
177 return "";
178 }
179
GetStrokeMiterLimit() const180 std::string ShapeContainerComposedElement::GetStrokeMiterLimit() const
181 {
182 auto render = GetContentRender<RenderShapeContainer>(ShapeContainerElement::TypeId());
183 if (render) {
184 return std::to_string(render->GetStrokeState().GetMiterLimit());
185 }
186 return "";
187 }
188
GetStrokeOpacity() const189 std::string ShapeContainerComposedElement::GetStrokeOpacity() const
190 {
191 auto render = GetContentRender<RenderShapeContainer>(ShapeContainerElement::TypeId());
192 if (render) {
193 return std::to_string(render->GetStrokeState().GetOpacity().GetValue());
194 }
195 return "";
196 }
197
GetStrokeWidth() const198 std::string ShapeContainerComposedElement::GetStrokeWidth() const
199 {
200 auto render = GetContentRender<RenderShapeContainer>(ShapeContainerElement::TypeId());
201 if (render) {
202 return render->GetStrokeState().GetLineWidth().ToString();
203 }
204 return "";
205 }
206
GetViewBox() const207 std::unique_ptr<JsonValue> ShapeContainerComposedElement::GetViewBox() const
208 {
209 auto render = GetContentRender<RenderShapeContainer>(ShapeContainerElement::TypeId());
210 auto jsonViewBox = JsonUtil::Create(true);
211 if (render) {
212 auto viewBox = render->GetShapeViewBox();
213 jsonViewBox->Put("x", viewBox.Left().ToString().c_str());
214 jsonViewBox->Put("y", viewBox.Top().ToString().c_str());
215 jsonViewBox->Put("width", viewBox.Width().ToString().c_str());
216 jsonViewBox->Put("height", viewBox.Height().ToString().c_str());
217 }
218 return jsonViewBox;
219 }
220
GetMesh() const221 std::unique_ptr<JsonValue> ShapeContainerComposedElement::GetMesh() const
222 {
223 auto render = GetContentRender<RenderShapeContainer>(ShapeContainerElement::TypeId());
224 auto jsonMesh = JsonUtil::Create(true);
225 if (render && jsonMesh) {
226 auto jsonValueArray = JsonUtil::CreateArray(true);
227 std::vector<double> array = render->GetMesh();
228 for (size_t i = 0; i < array.size(); i++) {
229 auto index = std::to_string(i);
230 auto value = std::to_string(array[i]);
231 jsonValueArray->Put(index.c_str(), value.c_str());
232 }
233 jsonMesh->Put("value", jsonValueArray);
234 jsonMesh->Put("column", std::to_string(render->GetColumn()).c_str());
235 jsonMesh->Put("row", std::to_string(render->GetRow()).c_str());
236 }
237 return jsonMesh;
238 }
239 } // namespace OHOS::Ace::V2
240