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