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