1 /*
2 * Copyright (c) 2021-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/js_canvas_path.h"
17 #include "bridge/declarative_frontend/jsview/js_rendering_context.h"
18 #include "bridge/declarative_frontend/jsview/js_view_common_def.h"
19
20 namespace OHOS::Ace::Framework {
21
22 JSCanvasPath::JSCanvasPath() = default;
23
JsPath2DSetTransform(const JSCallbackInfo & info)24 void JSCanvasPath::JsPath2DSetTransform(const JSCallbackInfo& info)
25 {
26 if (info.Length() == 6) {
27 if (info[0]->IsNumber()) {
28 double scaleX = 0.0;
29 double skewX = 0.0;
30 double skewY = 0.0;
31 double scaleY = 0.0;
32 double translateX = 0.0;
33 double translateY = 0.0;
34 JSViewAbstract::ParseJsDouble(info[0], scaleX);
35 JSViewAbstract::ParseJsDouble(info[1], skewX);
36 JSViewAbstract::ParseJsDouble(info[2], skewY);
37 JSViewAbstract::ParseJsDouble(info[3], scaleY);
38 JSViewAbstract::ParseJsDouble(info[4], translateX);
39 JSViewAbstract::ParseJsDouble(info[5], translateY);
40 translateX = SystemProperties::Vp2Px(translateX);
41 translateY = SystemProperties::Vp2Px(translateY);
42 path2d_->SetTransform(scaleX, skewX, skewY, scaleY, translateX, translateY);
43 }
44 }
45 }
46
JsPath2DMoveTo(const JSCallbackInfo & info)47 void JSCanvasPath::JsPath2DMoveTo(const JSCallbackInfo& info)
48 {
49 if (info.Length() == 2) {
50 if (info[0]->IsNumber()) {
51 double X = 0.0;
52 double Y = 0.0;
53 JSViewAbstract::ParseJsDouble(info[0], X);
54 JSViewAbstract::ParseJsDouble(info[1], Y);
55 X = SystemProperties::Vp2Px(X);
56 Y = SystemProperties::Vp2Px(Y);
57 path2d_->MoveTo(X, Y);
58 }
59 }
60 }
61
JsPath2DLineTo(const JSCallbackInfo & info)62 void JSCanvasPath::JsPath2DLineTo(const JSCallbackInfo& info)
63 {
64 if (info.Length() == 2) {
65 if (info[0]->IsNumber()) {
66 double X = 0.0;
67 double Y = 0.0;
68 JSViewAbstract::ParseJsDouble(info[0], X);
69 JSViewAbstract::ParseJsDouble(info[1], Y);
70 X = SystemProperties::Vp2Px(X);
71 Y = SystemProperties::Vp2Px(Y);
72 path2d_->LineTo(X, Y);
73 }
74 }
75 }
76
JsPath2DArc(const JSCallbackInfo & info)77 void JSCanvasPath::JsPath2DArc(const JSCallbackInfo& info)
78 {
79 if (info.Length() == 5 || info.Length() == 6) {
80 if (info[0]->IsNumber()) {
81 double X = 0.0;
82 double Y = 0.0;
83 double radius = 0.0;
84 double startAngle = 0.0;
85 double endAngle = 0.0;
86 bool anticlockwise = false;
87 JSViewAbstract::ParseJsDouble(info[0], X);
88 JSViewAbstract::ParseJsDouble(info[1], Y);
89 JSViewAbstract::ParseJsDouble(info[2], radius);
90 JSViewAbstract::ParseJsDouble(info[3], startAngle);
91 JSViewAbstract::ParseJsDouble(info[4], endAngle);
92 X = SystemProperties::Vp2Px(X);
93 Y = SystemProperties::Vp2Px(Y);
94 radius = SystemProperties::Vp2Px(radius);
95 if (info.Length() == 6) {
96 JSViewAbstract::ParseJsBool(info[5], anticlockwise);
97 }
98 path2d_->Arc(X, Y, radius, startAngle, endAngle, anticlockwise);
99 }
100 }
101 }
102
JsPath2DArcTo(const JSCallbackInfo & info)103 void JSCanvasPath::JsPath2DArcTo(const JSCallbackInfo& info)
104 {
105 if (info.Length() == 5) {
106 if (info[0]->IsNumber()) {
107 double x1 = 0.0;
108 double y1 = 0.0;
109 double x2 = 0.0;
110 double y2 = 0.0;
111 double radius = 0.0;
112 JSViewAbstract::ParseJsDouble(info[0], x1);
113 JSViewAbstract::ParseJsDouble(info[1], y1);
114 JSViewAbstract::ParseJsDouble(info[2], x2);
115 JSViewAbstract::ParseJsDouble(info[3], y2);
116 JSViewAbstract::ParseJsDouble(info[4], radius);
117 x1 = SystemProperties::Vp2Px(x1);
118 y1 = SystemProperties::Vp2Px(y1);
119 x2 = SystemProperties::Vp2Px(x2);
120 y2 = SystemProperties::Vp2Px(y2);
121 radius = SystemProperties::Vp2Px(radius);
122 path2d_->ArcTo(x1, y1, x2, y2, radius);
123 }
124 }
125 }
126
JsPath2DQuadraticCurveTo(const JSCallbackInfo & info)127 void JSCanvasPath::JsPath2DQuadraticCurveTo(const JSCallbackInfo& info)
128 {
129 if (info[0]->IsNumber()) {
130 double cpx = 0.0;
131 double cpy = 0.0;
132 double x = 0.0;
133 double y = 0.0;
134 JSViewAbstract::ParseJsDouble(info[0], cpx);
135 JSViewAbstract::ParseJsDouble(info[1], cpy);
136 JSViewAbstract::ParseJsDouble(info[2], x);
137 JSViewAbstract::ParseJsDouble(info[3], y);
138 cpx = SystemProperties::Vp2Px(cpx);
139 cpy = SystemProperties::Vp2Px(cpy);
140 x = SystemProperties::Vp2Px(x);
141 y = SystemProperties::Vp2Px(y);
142 path2d_->QuadraticCurveTo(cpx, cpy, x, y);
143 }
144 }
145
JsPath2DBezierCurveTo(const JSCallbackInfo & info)146 void JSCanvasPath::JsPath2DBezierCurveTo(const JSCallbackInfo& info)
147 {
148 if (info[0]->IsNumber()) {
149 double cp1x = 0.0;
150 double cp1y = 0.0;
151 double cp2x = 0.0;
152 double cp2y = 0.0;
153 double x = 0.0;
154 double y = 0.0;
155 JSViewAbstract::ParseJsDouble(info[0], cp1x);
156 JSViewAbstract::ParseJsDouble(info[1], cp1y);
157 JSViewAbstract::ParseJsDouble(info[2], cp2x);
158 JSViewAbstract::ParseJsDouble(info[3], cp2y);
159 JSViewAbstract::ParseJsDouble(info[4], x);
160 JSViewAbstract::ParseJsDouble(info[5], y);
161 cp1x = SystemProperties::Vp2Px(cp1x);
162 cp1y = SystemProperties::Vp2Px(cp1y);
163 cp2x = SystemProperties::Vp2Px(cp2x);
164 cp2y = SystemProperties::Vp2Px(cp2y);
165 x = SystemProperties::Vp2Px(x);
166 y = SystemProperties::Vp2Px(y);
167 path2d_->BezierCurveTo(cp1x, cp1y, cp2x, cp2y, x, y);
168 }
169 }
170
JsPath2DEllipse(const JSCallbackInfo & info)171 void JSCanvasPath::JsPath2DEllipse(const JSCallbackInfo& info)
172 {
173 if (info[0]->IsNumber()) {
174 double X = 0.0;
175 double Y = 0.0;
176 double radiusX = 0.0;
177 double radiusY = 0.0;
178 double rotation = 0.0;
179 double startAngle = 0.0;
180 double endAngle = 0.0;
181
182 JSViewAbstract::ParseJsDouble(info[0], X);
183 JSViewAbstract::ParseJsDouble(info[1], Y);
184 JSViewAbstract::ParseJsDouble(info[2], radiusX);
185 JSViewAbstract::ParseJsDouble(info[3], radiusY);
186 JSViewAbstract::ParseJsDouble(info[4], rotation);
187 JSViewAbstract::ParseJsDouble(info[5], startAngle);
188 JSViewAbstract::ParseJsDouble(info[6], endAngle);
189 X = SystemProperties::Vp2Px(X);
190 Y = SystemProperties::Vp2Px(Y);
191 radiusX = SystemProperties::Vp2Px(radiusX);
192 radiusY = SystemProperties::Vp2Px(radiusY);
193
194 bool anticlockwise = false;
195
196 if (info.Length() == 8) {
197 JSViewAbstract::ParseJsBool(info[7], anticlockwise);
198 }
199
200 path2d_->Ellipse(X, Y, radiusX, radiusY, rotation, startAngle, endAngle, anticlockwise);
201 }
202 }
203
JsPath2DRect(const JSCallbackInfo & info)204 void JSCanvasPath::JsPath2DRect(const JSCallbackInfo& info)
205 {
206 if (info[0]->IsNumber()) {
207 double x = 0.0;
208 double y = 0.0;
209 double width = 0.0;
210 double height = 0.0;
211 JSViewAbstract::ParseJsDouble(info[0], x);
212 JSViewAbstract::ParseJsDouble(info[1], y);
213 JSViewAbstract::ParseJsDouble(info[2], width);
214 JSViewAbstract::ParseJsDouble(info[3], height);
215 x = SystemProperties::Vp2Px(x);
216 y = SystemProperties::Vp2Px(y);
217 width = SystemProperties::Vp2Px(width);
218 height = SystemProperties::Vp2Px(height);
219
220 path2d_->Rect(x, y, width, height);
221 }
222 }
223
JsPath2DClosePath(const JSCallbackInfo & info)224 void JSCanvasPath::JsPath2DClosePath(const JSCallbackInfo& info)
225 {
226 path2d_->ClosePath();
227 }
228
229 } // namespace OHOS::Ace::Framework