1 /*
2  * Copyright (c) 2022 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_NG_PATTERN_SHAPE_SHAPE_CONTAINER_PAINT_PROPERTY_H
17 #define FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_PATTERN_SHAPE_SHAPE_CONTAINER_PAINT_PROPERTY_H
18 
19 #include <string>
20 
21 #include "base/geometry/ng/image_mesh.h"
22 #include "core/components/shape/shape_component.h"
23 #include "core/components_ng/base/inspector_filter.h"
24 #include "core/components_ng/layout/layout_property.h"
25 #include "core/components_ng/pattern/shape/shape_paint_property.h"
26 #include "core/components_ng/pattern/shape/shape_view_box.h"
27 #include "core/components_ng/property/property.h"
28 #include "core/components_ng/render/paint_property.h"
29 
30 namespace OHOS::Ace::NG {
31 class ACE_EXPORT ShapeContainerPaintProperty : public ShapePaintProperty {
32     DECLARE_ACE_TYPE(ShapeContainerPaintProperty, ShapePaintProperty);
33 
34 public:
35     ShapeContainerPaintProperty() = default;
36     ~ShapeContainerPaintProperty() override = default;
Clone()37     RefPtr<PaintProperty> Clone() const override
38     {
39         auto value = MakeRefPtr<ShapeContainerPaintProperty>();
40         value->PaintProperty::UpdatePaintProperty(DynamicCast<PaintProperty>(this));
41         value->propImageMesh_ = CloneImageMesh();
42         value->propShapeViewBox_ = CloneShapeViewBox();
43         value->propFill_ = CloneFill();
44         value->propFillOpacity_ = CloneFillOpacity();
45         value->propStroke_ = CloneStroke();
46         value->propStrokeWidth_ = CloneStrokeWidth();
47         value->propStrokeOpacity_ = CloneStrokeOpacity();
48         value->propStrokeDashArray_ = CloneStrokeDashArray();
49         value->propStrokeDashOffset_ = CloneStrokeDashOffset();
50         value->propStrokeLineCap_ = CloneStrokeLineCap();
51         value->propStrokeLineJoin_ = CloneStrokeLineJoin();
52         value->propStrokeMiterLimit_ = CloneStrokeMiterLimit();
53         value->propAntiAlias_ = CloneAntiAlias();
54         return value;
55     }
56 
Reset()57     void Reset() override
58     {
59         ShapePaintProperty::Reset();
60         ResetImageMesh();
61         ResetShapeViewBox();
62     }
63 
ToJsonValue(std::unique_ptr<JsonValue> & json,const InspectorFilter & filter)64     void ToJsonValue(std::unique_ptr<JsonValue>& json, const InspectorFilter& filter) const override
65     {
66         ShapePaintProperty::ToJsonValue(json, filter);
67         /* no fixed attr below, just return */
68         if (filter.IsFastFilter()) {
69             return;
70         }
71         auto viewBoxJson = JsonUtil::Create(true);
72         if (propShapeViewBox_.has_value()) {
73             viewBoxJson->Put("x", propShapeViewBox_.value().Left().ToString().c_str());
74             viewBoxJson->Put("y", propShapeViewBox_.value().Top().ToString().c_str());
75             viewBoxJson->Put("width", propShapeViewBox_.value().Width().ToString().c_str());
76             viewBoxJson->Put("height", propShapeViewBox_.value().Height().ToString().c_str());
77         }
78         json->PutExtAttr("viewPort", viewBoxJson, filter);
79 
80         auto meshJson = JsonUtil::Create(true);
81         if (propImageMesh_.has_value() && meshJson) {
82             auto jsonValueArray = JsonUtil::CreateArray(true);
83             std::vector<double> array = propImageMesh_->GetMesh();
84             for (size_t i = 0; i < array.size(); i++) {
85                 auto index = std::to_string(i);
86                 auto value = std::to_string(array[i]);
87                 jsonValueArray->Put(index.c_str(), value.c_str());
88             }
89             meshJson->Put("value", jsonValueArray);
90             meshJson->Put("row", propImageMesh_->GetRow());
91             meshJson->Put("column", propImageMesh_->GetColumn());
92         }
93         json->PutExtAttr("mesh", meshJson, filter);
94     }
95 
96     ACE_DEFINE_PROPERTY_ITEM_WITHOUT_GROUP(ImageMesh, ImageMesh, PROPERTY_UPDATE_RENDER);
97     ACE_DEFINE_PROPERTY_ITEM_WITHOUT_GROUP(ShapeViewBox, ShapeViewBox, PROPERTY_UPDATE_RENDER);
98 
99 private:
100     ACE_DISALLOW_COPY_AND_MOVE(ShapeContainerPaintProperty);
101 };
102 } // namespace OHOS::Ace::NG
103 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_PATTERN_SHAPE_SHAPE_CONTAINER_PAINT_PROPERTY_H
104