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 #include "bridge/declarative_frontend/jsview/models/shape_model_impl.h"
17 
18 #include "base/memory/referenced.h"
19 #include "bridge/declarative_frontend/jsview/models/shape_abstract_model_impl.h"
20 #include "bridge/declarative_frontend/view_stack_processor.h"
21 #include "core/components/shape/shape_container_component.h"
22 
23 namespace OHOS::Ace::Framework {
24 
Create()25 void ShapeModelImpl::Create()
26 {
27     std::list<RefPtr<OHOS::Ace::Component>> componentChildren;
28     RefPtr<OHOS::Ace::ShapeContainerComponent> component =
29         AceType::MakeRefPtr<OHOS::Ace::ShapeContainerComponent>(componentChildren);
30     ViewStackProcessor::GetInstance()->ClaimElementId(component);
31     ViewStackProcessor::GetInstance()->Push(component);
32 }
33 
SetBitmapMesh(std::vector<double> & mesh,int32_t column,int32_t row)34 void ShapeModelImpl::SetBitmapMesh(std::vector<double>& mesh, int32_t column, int32_t row)
35 {
36     auto* stack = ViewStackProcessor::GetInstance();
37     auto component = AceType::DynamicCast<OHOS::Ace::ShapeContainerComponent>(stack->GetMainComponent());
38     if (component) {
39         component->SetBitmapMesh(mesh, column, row);
40     }
41 }
42 
SetViewPort(const Dimension & dimLeft,const Dimension & dimTop,const Dimension & dimWidth,const Dimension & dimHeight)43 void ShapeModelImpl::SetViewPort(
44     const Dimension& dimLeft, const Dimension& dimTop, const Dimension& dimWidth, const Dimension& dimHeight)
45 {
46     auto* stack = ViewStackProcessor::GetInstance();
47     auto component = AceType::DynamicCast<OHOS::Ace::ShapeContainerComponent>(stack->GetMainComponent());
48     if (!component) {
49         LOGE("shape is null");
50         return;
51     }
52     AnimationOption option = stack->GetImplicitAnimationOption();
53     ShapeViewBox viewBox;
54     viewBox.SetLeft(dimLeft, option);
55     viewBox.SetTop(dimTop, option);
56     viewBox.SetWidth(dimWidth, option);
57     viewBox.SetHeight(dimHeight, option);
58     component->SetViewBox(viewBox);
59 }
60 
SetWidth()61 void ShapeModelImpl::SetWidth()
62 {
63     auto box = ViewStackProcessor::GetInstance()->GetBoxComponent();
64     if (!box) {
65         return;
66     }
67     if (!box->GetWidth().IsValid()) {
68         return;
69     }
70     auto* stack = ViewStackProcessor::GetInstance();
71     auto component = AceType::DynamicCast<OHOS::Ace::ShapeContainerComponent>(stack->GetMainComponent());
72     if (component) {
73         component->SetWidthFlag(true);
74     }
75 }
76 
SetHeight()77 void ShapeModelImpl::SetHeight()
78 {
79     auto box = ViewStackProcessor::GetInstance()->GetBoxComponent();
80     if (!box) {
81         return;
82     }
83     if (!box->GetHeight().IsValid()) {
84         return;
85     }
86     auto* stack = ViewStackProcessor::GetInstance();
87     auto component = AceType::DynamicCast<OHOS::Ace::ShapeContainerComponent>(stack->GetMainComponent());
88     if (component) {
89         component->SetHeightFlag(true);
90     }
91 }
92 
InitBox(RefPtr<PixelMap> & pixMap)93 void ShapeModelImpl::InitBox(RefPtr<PixelMap>& pixMap)
94 {
95     auto box = ViewStackProcessor::GetInstance()->GetBoxComponent();
96     box->SetOverflow(Overflow::FORCE_CLIP);
97     auto clipPath = AceType::MakeRefPtr<ClipPath>();
98     clipPath->SetGeometryBoxType(GeometryBoxType::BORDER_BOX);
99     box->SetClipPath(clipPath);
100     if (pixMap) {
101         box->SetPixelMap(pixMap);
102     }
103 }
104 
SetStroke(const Color & color)105 void ShapeModelImpl::SetStroke(const Color& color)
106 {
107     auto stack = ViewStackProcessor::GetInstance();
108     auto component = AceType::DynamicCast<OHOS::Ace::ShapeContainerComponent>(stack->GetMainComponent());
109     if (component) {
110         AnimationOption option = stack->GetImplicitAnimationOption();
111         component->SetStroke(color, option);
112     }
113 }
114 
SetFill(const Color & color)115 void ShapeModelImpl::SetFill(const Color& color)
116 {
117     auto stack = ViewStackProcessor::GetInstance();
118     auto component = AceType::DynamicCast<OHOS::Ace::ShapeContainerComponent>(stack->GetMainComponent());
119     if (!component) {
120         LOGE("component is null");
121         return;
122     }
123     AnimationOption option = stack->GetImplicitAnimationOption();
124     component->SetFill(color, option);
125 }
126 
SetStrokeDashOffset(const Ace::Dimension & dashOffset)127 void ShapeModelImpl::SetStrokeDashOffset(const Ace::Dimension& dashOffset)
128 {
129     auto stack = ViewStackProcessor::GetInstance();
130     auto component = AceType::DynamicCast<OHOS::Ace::ShapeContainerComponent>(stack->GetMainComponent());
131     if (component) {
132         AnimationOption option = stack->GetImplicitAnimationOption();
133         component->SetStrokeDashOffset(dashOffset, option);
134     }
135 }
136 
SetStrokeLineCap(int lineCapStyle)137 void ShapeModelImpl::SetStrokeLineCap(int lineCapStyle)
138 {
139     auto stack = ViewStackProcessor::GetInstance();
140     auto component = AceType::DynamicCast<OHOS::Ace::ShapeContainerComponent>(stack->GetMainComponent());
141     if (!component) {
142         LOGE("ShapeComponent is null");
143         return;
144     }
145     if (static_cast<int>(LineCapStyle::SQUARE) == lineCapStyle) {
146         component->SetStrokeLineCap(LineCapStyle::SQUARE);
147     } else if (static_cast<int>(LineCapStyle::ROUND) == lineCapStyle) {
148         component->SetStrokeLineCap(LineCapStyle::ROUND);
149     } else {
150         component->SetStrokeLineCap(LineCapStyle::BUTT);
151     }
152 }
153 
SetStrokeLineJoin(int lineJoinStyle)154 void ShapeModelImpl::SetStrokeLineJoin(int lineJoinStyle)
155 {
156     auto stack = ViewStackProcessor::GetInstance();
157     auto component = AceType::DynamicCast<OHOS::Ace::ShapeContainerComponent>(stack->GetMainComponent());
158     if (!component) {
159         LOGE("ShapeComponent is null");
160         return;
161     }
162     if (static_cast<int>(LineJoinStyle::BEVEL) == lineJoinStyle) {
163         component->SetStrokeLineJoin(LineJoinStyle::BEVEL);
164     } else if (static_cast<int>(LineJoinStyle::ROUND) == lineJoinStyle) {
165         component->SetStrokeLineJoin(LineJoinStyle::ROUND);
166     } else {
167         component->SetStrokeLineJoin(LineJoinStyle::MITER);
168     }
169 }
170 
SetStrokeMiterLimit(double miterLimit)171 void ShapeModelImpl::SetStrokeMiterLimit(double miterLimit)
172 {
173     auto stack = ViewStackProcessor::GetInstance();
174     auto component = AceType::DynamicCast<OHOS::Ace::ShapeContainerComponent>(stack->GetMainComponent());
175     if (!component) {
176         LOGE("ShapeComponent is null");
177         return;
178     }
179     component->SetStrokeMiterLimit(miterLimit);
180 }
181 
SetStrokeOpacity(double opacity)182 void ShapeModelImpl::SetStrokeOpacity(double opacity)
183 {
184     auto stack = ViewStackProcessor::GetInstance();
185     auto component = AceType::DynamicCast<OHOS::Ace::ShapeContainerComponent>(stack->GetMainComponent());
186     if (component) {
187         AnimationOption option = stack->GetImplicitAnimationOption();
188         component->SetStrokeOpacity(opacity, option);
189     }
190 }
191 
SetFillOpacity(double opacity)192 void ShapeModelImpl::SetFillOpacity(double opacity)
193 {
194     auto stack = ViewStackProcessor::GetInstance();
195     auto component = AceType::DynamicCast<OHOS::Ace::ShapeContainerComponent>(stack->GetMainComponent());
196     if (component) {
197         AnimationOption option = stack->GetImplicitAnimationOption();
198         component->SetFillOpacity(opacity, option);
199     }
200 }
201 
SetStrokeWidth(const Ace::Dimension & lineWidth)202 void ShapeModelImpl::SetStrokeWidth(const Ace::Dimension& lineWidth)
203 {
204     auto stack = ViewStackProcessor::GetInstance();
205     auto component = AceType::DynamicCast<OHOS::Ace::ShapeContainerComponent>(stack->GetMainComponent());
206     if (!component) {
207         LOGE("ShapeComponent is null");
208         return;
209     }
210     if (GreatOrEqual(lineWidth.Value(), 0.0)) {
211         AnimationOption option = stack->GetImplicitAnimationOption();
212         component->SetStrokeWidth(lineWidth, option);
213     }
214 }
215 
SetStrokeDashArray(const std::vector<Ace::Dimension> & dashArray)216 void ShapeModelImpl::SetStrokeDashArray(const std::vector<Ace::Dimension>& dashArray)
217 {
218     auto stack = ViewStackProcessor::GetInstance();
219     auto component = AceType::DynamicCast<OHOS::Ace::ShapeContainerComponent>(stack->GetMainComponent());
220     if (!component) {
221         LOGE("component is null");
222         return;
223     }
224     component->SetStrokeDashArray(dashArray);
225 }
226 
SetAntiAlias(bool antiAlias)227 void ShapeModelImpl::SetAntiAlias(bool antiAlias)
228 {
229     auto stack = ViewStackProcessor::GetInstance();
230     auto component = AceType::DynamicCast<OHOS::Ace::ShapeContainerComponent>(stack->GetMainComponent());
231     if (component) {
232         component->SetAntiAlias(antiAlias);
233     }
234 }
235 
236 } // namespace OHOS::Ace::Framework