1 /*
2 * Copyright (c) 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 "js_path.h"
17
18 #include "native_value.h"
19
20 #include "js_drawing_utils.h"
21 #include "matrix_napi/js_matrix.h"
22 #include "path_iterator_napi/js_path_iterator.h"
23 #include "roundRect_napi/js_roundrect.h"
24 #include "utils/performanceCaculate.h"
25
26 #include "matrix_napi/js_matrix.h"
27
28 namespace OHOS::Rosen {
29 namespace Drawing {
30 thread_local napi_ref JsPath::constructor_ = nullptr;
31 const std::string CLASS_NAME = "Path";
32
33 static const napi_property_descriptor g_properties[] = {
34 DECLARE_NAPI_FUNCTION("moveTo", JsPath::MoveTo),
35 DECLARE_NAPI_FUNCTION("lineTo", JsPath::LineTo),
36 DECLARE_NAPI_FUNCTION("arcTo", JsPath::ArcTo),
37 DECLARE_NAPI_FUNCTION("quadTo", JsPath::QuadTo),
38 DECLARE_NAPI_FUNCTION("conicTo", JsPath::ConicTo),
39 DECLARE_NAPI_FUNCTION("cubicTo", JsPath::CubicTo),
40 DECLARE_NAPI_FUNCTION("rMoveTo", JsPath::RMoveTo),
41 DECLARE_NAPI_FUNCTION("rLineTo", JsPath::RLineTo),
42 DECLARE_NAPI_FUNCTION("rQuadTo", JsPath::RQuadTo),
43 DECLARE_NAPI_FUNCTION("rConicTo", JsPath::RConicTo),
44 DECLARE_NAPI_FUNCTION("rCubicTo", JsPath::RCubicTo),
45 DECLARE_NAPI_FUNCTION("addPolygon", JsPath::AddPolygon),
46 DECLARE_NAPI_FUNCTION("addOval", JsPath::AddOval),
47 DECLARE_NAPI_FUNCTION("addCircle", JsPath::AddCircle),
48 DECLARE_NAPI_FUNCTION("addArc", JsPath::AddArc),
49 DECLARE_NAPI_FUNCTION("addRect", JsPath::AddRect),
50 DECLARE_NAPI_FUNCTION("addRoundRect", JsPath::AddRoundRect),
51 DECLARE_NAPI_FUNCTION("addPath", JsPath::AddPath),
52 DECLARE_NAPI_FUNCTION("transform", JsPath::Transform),
53 DECLARE_NAPI_FUNCTION("contains", JsPath::Contains),
54 DECLARE_NAPI_FUNCTION("setFillType", JsPath::SetFillType),
55 DECLARE_NAPI_FUNCTION("getBounds", JsPath::GetBounds),
56 DECLARE_NAPI_FUNCTION("close", JsPath::Close),
57 DECLARE_NAPI_FUNCTION("offset", JsPath::Offset),
58 DECLARE_NAPI_FUNCTION("reset", JsPath::Reset),
59 DECLARE_NAPI_FUNCTION("op", JsPath::Op),
60 DECLARE_NAPI_FUNCTION("getLength", JsPath::GetLength),
61 DECLARE_NAPI_FUNCTION("getPositionAndTangent", JsPath::GetPositionAndTangent),
62 DECLARE_NAPI_FUNCTION("getSegment", JsPath::GetSegment),
63 DECLARE_NAPI_FUNCTION("getMatrix", JsPath::GetMatrix),
64 DECLARE_NAPI_FUNCTION("buildFromSvgString", JsPath::BuildFromSvgString),
65 DECLARE_NAPI_FUNCTION("isClosed", JsPath::IsClosed),
66 DECLARE_NAPI_FUNCTION("getPathIterator", JsPath::GetPathIterator),
67 };
68
Init(napi_env env,napi_value exportObj)69 napi_value JsPath::Init(napi_env env, napi_value exportObj)
70 {
71 napi_value constructor = nullptr;
72 napi_status status = napi_define_class(env, CLASS_NAME.c_str(), NAPI_AUTO_LENGTH, Constructor, nullptr,
73 sizeof(g_properties) / sizeof(g_properties[0]), g_properties, &constructor);
74 if (status != napi_ok) {
75 ROSEN_LOGE("Failed to define Path class");
76 return nullptr;
77 }
78
79 status = napi_create_reference(env, constructor, 1, &constructor_);
80 if (status != napi_ok) {
81 ROSEN_LOGE("Failed to create reference of constructor");
82 return nullptr;
83 }
84
85 status = napi_set_named_property(env, exportObj, CLASS_NAME.c_str(), constructor);
86 if (status != napi_ok) {
87 ROSEN_LOGE("Failed to set constructor");
88 return nullptr;
89 }
90
91 return exportObj;
92 }
93
Constructor(napi_env env,napi_callback_info info)94 napi_value JsPath::Constructor(napi_env env, napi_callback_info info)
95 {
96 size_t argc = ARGC_ONE;
97 napi_value argv[ARGC_ONE] = {nullptr};
98 napi_value jsThis = nullptr;
99 JsPath* jsPath = nullptr;
100 napi_status status = napi_get_cb_info(env, info, &argc, argv, &jsThis, nullptr);
101 if (status != napi_ok) {
102 ROSEN_LOGE("Path::Constructor Failed to napi_get_cb_info");
103 return nullptr;
104 }
105 if (argc == ARGC_ZERO) {
106 Path* path = new Path();
107 jsPath = new JsPath(path);
108 } else if (argc == ARGC_ONE) {
109 napi_valuetype valueType = napi_undefined;
110 if (argv[0] == nullptr || napi_typeof(env, argv[0], &valueType) != napi_ok || valueType != napi_object) {
111 return NapiThrowError(env, DrawingErrorCode::ERROR_INVALID_PARAM,
112 "JsPath::Constructor Argv[0] is invalid");
113 }
114 JsPath* path = nullptr;
115 GET_UNWRAP_PARAM(ARGC_ZERO, path);
116 Path* p = new Path(*path->GetPath());
117 jsPath = new JsPath(p);
118 } else {
119 return NapiThrowError(env, DrawingErrorCode::ERROR_INVALID_PARAM, "Incorrect number of parameters.");
120 }
121 if (!jsPath) {
122 ROSEN_LOGE("Failed to create JsPath");
123 return nullptr;
124 }
125 status = napi_wrap(env, jsThis, jsPath,
126 JsPath::Destructor, nullptr, nullptr);
127 if (status != napi_ok) {
128 delete jsPath;
129 ROSEN_LOGE("JsPath::Constructor Failed to wrap native instance");
130 return nullptr;
131 }
132 return jsThis;
133 }
134
Destructor(napi_env env,void * nativeObject,void * finalize)135 void JsPath::Destructor(napi_env env, void *nativeObject, void *finalize)
136 {
137 (void)finalize;
138 if (nativeObject != nullptr) {
139 JsPath *napi = reinterpret_cast<JsPath *>(nativeObject);
140 delete napi;
141 }
142 }
143
CreateJsPath(napi_env env,Path * path)144 napi_value JsPath::CreateJsPath(napi_env env, Path* path)
145 {
146 napi_value constructor = nullptr;
147 napi_status status = napi_get_reference_value(env, constructor_, &constructor);
148 if (status != napi_ok) {
149 delete path;
150 ROSEN_LOGE("JsPath::CreateJsPath failed to get reference value!");
151 return nullptr;
152 }
153 napi_value result = nullptr;
154 napi_create_object(env, &result);
155 if (result == nullptr) {
156 delete path;
157 ROSEN_LOGE("JsPath::CreateJsPath create object failed!");
158 return nullptr;
159 }
160 JsPath* jsPath = new JsPath(path);
161 status = napi_wrap(env, result, jsPath, JsPath::Destructor, nullptr, nullptr);
162 if (status != napi_ok) {
163 delete jsPath;
164 ROSEN_LOGE("JsPath::CreateJsPath failed to wrap native instance");
165 return nullptr;
166 }
167 napi_define_properties(env, result, sizeof(g_properties) / sizeof(g_properties[0]), g_properties);
168 return result;
169 }
170
~JsPath()171 JsPath::~JsPath()
172 {
173 if (m_path != nullptr) {
174 delete m_path;
175 m_path = nullptr;
176 }
177 }
178
MoveTo(napi_env env,napi_callback_info info)179 napi_value JsPath::MoveTo(napi_env env, napi_callback_info info)
180 {
181 JsPath* me = CheckParamsAndGetThis<JsPath>(env, info);
182 return (me != nullptr) ? me->OnMoveTo(env, info) : nullptr;
183 }
184
LineTo(napi_env env,napi_callback_info info)185 napi_value JsPath::LineTo(napi_env env, napi_callback_info info)
186 {
187 JsPath* me = CheckParamsAndGetThis<JsPath>(env, info);
188 return (me != nullptr) ? me->OnLineTo(env, info) : nullptr;
189 }
190
ArcTo(napi_env env,napi_callback_info info)191 napi_value JsPath::ArcTo(napi_env env, napi_callback_info info)
192 {
193 JsPath* me = CheckParamsAndGetThis<JsPath>(env, info);
194 return (me != nullptr) ? me->OnArcTo(env, info) : nullptr;
195 }
196
QuadTo(napi_env env,napi_callback_info info)197 napi_value JsPath::QuadTo(napi_env env, napi_callback_info info)
198 {
199 JsPath* me = CheckParamsAndGetThis<JsPath>(env, info);
200 return (me != nullptr) ? me->OnQuadTo(env, info) : nullptr;
201 }
202
ConicTo(napi_env env,napi_callback_info info)203 napi_value JsPath::ConicTo(napi_env env, napi_callback_info info)
204 {
205 JsPath* me = CheckParamsAndGetThis<JsPath>(env, info);
206 return (me != nullptr) ? me->OnConicTo(env, info) : nullptr;
207 }
208
CubicTo(napi_env env,napi_callback_info info)209 napi_value JsPath::CubicTo(napi_env env, napi_callback_info info)
210 {
211 JsPath* me = CheckParamsAndGetThis<JsPath>(env, info);
212 return (me != nullptr) ? me->OnCubicTo(env, info) : nullptr;
213 }
214
RMoveTo(napi_env env,napi_callback_info info)215 napi_value JsPath::RMoveTo(napi_env env, napi_callback_info info)
216 {
217 JsPath* me = CheckParamsAndGetThis<JsPath>(env, info);
218 return (me != nullptr) ? me->OnRMoveTo(env, info) : nullptr;
219 }
220
RLineTo(napi_env env,napi_callback_info info)221 napi_value JsPath::RLineTo(napi_env env, napi_callback_info info)
222 {
223 JsPath* me = CheckParamsAndGetThis<JsPath>(env, info);
224 return (me != nullptr) ? me->OnRLineTo(env, info) : nullptr;
225 }
226
RQuadTo(napi_env env,napi_callback_info info)227 napi_value JsPath::RQuadTo(napi_env env, napi_callback_info info)
228 {
229 JsPath* me = CheckParamsAndGetThis<JsPath>(env, info);
230 return (me != nullptr) ? me->OnRQuadTo(env, info) : nullptr;
231 }
232
RConicTo(napi_env env,napi_callback_info info)233 napi_value JsPath::RConicTo(napi_env env, napi_callback_info info)
234 {
235 JsPath* me = CheckParamsAndGetThis<JsPath>(env, info);
236 return (me != nullptr) ? me->OnRConicTo(env, info) : nullptr;
237 }
238
RCubicTo(napi_env env,napi_callback_info info)239 napi_value JsPath::RCubicTo(napi_env env, napi_callback_info info)
240 {
241 JsPath* me = CheckParamsAndGetThis<JsPath>(env, info);
242 return (me != nullptr) ? me->OnRCubicTo(env, info) : nullptr;
243 }
244
AddPolygon(napi_env env,napi_callback_info info)245 napi_value JsPath::AddPolygon(napi_env env, napi_callback_info info)
246 {
247 JsPath* me = CheckParamsAndGetThis<JsPath>(env, info);
248 return (me != nullptr) ? me->OnAddPolygon(env, info) : nullptr;
249 }
250
AddOval(napi_env env,napi_callback_info info)251 napi_value JsPath::AddOval(napi_env env, napi_callback_info info)
252 {
253 JsPath* me = CheckParamsAndGetThis<JsPath>(env, info);
254 return (me != nullptr) ? me->OnAddOval(env, info) : nullptr;
255 }
256
AddCircle(napi_env env,napi_callback_info info)257 napi_value JsPath::AddCircle(napi_env env, napi_callback_info info)
258 {
259 JsPath* me = CheckParamsAndGetThis<JsPath>(env, info);
260 return (me != nullptr) ? me->OnAddCircle(env, info) : nullptr;
261 }
262
AddArc(napi_env env,napi_callback_info info)263 napi_value JsPath::AddArc(napi_env env, napi_callback_info info)
264 {
265 JsPath* me = CheckParamsAndGetThis<JsPath>(env, info);
266 return (me != nullptr) ? me->OnAddArc(env, info) : nullptr;
267 }
268
AddRect(napi_env env,napi_callback_info info)269 napi_value JsPath::AddRect(napi_env env, napi_callback_info info)
270 {
271 JsPath* me = CheckParamsAndGetThis<JsPath>(env, info);
272 return (me != nullptr) ? me->OnAddRect(env, info) : nullptr;
273 }
274
AddRoundRect(napi_env env,napi_callback_info info)275 napi_value JsPath::AddRoundRect(napi_env env, napi_callback_info info)
276 {
277 JsPath* me = CheckParamsAndGetThis<JsPath>(env, info);
278 return (me != nullptr) ? me->OnAddRoundRect(env, info) : nullptr;
279 }
280
AddPath(napi_env env,napi_callback_info info)281 napi_value JsPath::AddPath(napi_env env, napi_callback_info info)
282 {
283 JsPath* me = CheckParamsAndGetThis<JsPath>(env, info);
284 return (me != nullptr) ? me->OnAddPath(env, info) : nullptr;
285 }
286
Transform(napi_env env,napi_callback_info info)287 napi_value JsPath::Transform(napi_env env, napi_callback_info info)
288 {
289 JsPath* me = CheckParamsAndGetThis<JsPath>(env, info);
290 return (me != nullptr) ? me->OnTransform(env, info) : nullptr;
291 }
292
Contains(napi_env env,napi_callback_info info)293 napi_value JsPath::Contains(napi_env env, napi_callback_info info)
294 {
295 JsPath* me = CheckParamsAndGetThis<JsPath>(env, info);
296 return (me != nullptr) ? me->OnContains(env, info) : nullptr;
297 }
298
SetFillType(napi_env env,napi_callback_info info)299 napi_value JsPath::SetFillType(napi_env env, napi_callback_info info)
300 {
301 JsPath* me = CheckParamsAndGetThis<JsPath>(env, info);
302 return (me != nullptr) ? me->OnSetFillType(env, info) : nullptr;
303 }
304
GetBounds(napi_env env,napi_callback_info info)305 napi_value JsPath::GetBounds(napi_env env, napi_callback_info info)
306 {
307 JsPath* me = CheckParamsAndGetThis<JsPath>(env, info);
308 return (me != nullptr) ? me->OnGetBounds(env, info) : nullptr;
309 }
310
Close(napi_env env,napi_callback_info info)311 napi_value JsPath::Close(napi_env env, napi_callback_info info)
312 {
313 JsPath* me = CheckParamsAndGetThis<JsPath>(env, info);
314 return (me != nullptr) ? me->OnClose(env, info) : nullptr;
315 }
316
Offset(napi_env env,napi_callback_info info)317 napi_value JsPath::Offset(napi_env env, napi_callback_info info)
318 {
319 JsPath* me = CheckParamsAndGetThis<JsPath>(env, info);
320 return (me != nullptr) ? me->OnOffset(env, info) : nullptr;
321 }
322
Reset(napi_env env,napi_callback_info info)323 napi_value JsPath::Reset(napi_env env, napi_callback_info info)
324 {
325 JsPath* me = CheckParamsAndGetThis<JsPath>(env, info);
326 return (me != nullptr) ? me->OnReset(env, info) : nullptr;
327 }
328
GetLength(napi_env env,napi_callback_info info)329 napi_value JsPath::GetLength(napi_env env, napi_callback_info info)
330 {
331 JsPath* me = CheckParamsAndGetThis<JsPath>(env, info);
332 return (me != nullptr) ? me->OnGetLength(env, info) : nullptr;
333 }
334
GetPositionAndTangent(napi_env env,napi_callback_info info)335 napi_value JsPath::GetPositionAndTangent(napi_env env, napi_callback_info info)
336 {
337 JsPath* me = CheckParamsAndGetThis<JsPath>(env, info);
338 return (me != nullptr) ? me->OnGetPositionAndTangent(env, info) : nullptr;
339 }
340
GetSegment(napi_env env,napi_callback_info info)341 napi_value JsPath::GetSegment(napi_env env, napi_callback_info info)
342 {
343 JsPath* me = CheckParamsAndGetThis<JsPath>(env, info);
344 return (me != nullptr) ? me->OnGetSegment(env, info) : nullptr;
345 }
346
GetMatrix(napi_env env,napi_callback_info info)347 napi_value JsPath::GetMatrix(napi_env env, napi_callback_info info)
348 {
349 JsPath* me = CheckParamsAndGetThis<JsPath>(env, info);
350 return (me != nullptr) ? me->OnGetMatrix(env, info) : nullptr;
351 }
352
BuildFromSvgString(napi_env env,napi_callback_info info)353 napi_value JsPath::BuildFromSvgString(napi_env env, napi_callback_info info)
354 {
355 JsPath* me = CheckParamsAndGetThis<JsPath>(env, info);
356 return (me != nullptr) ? me->OnBuildFromSvgString(env, info) : nullptr;
357 }
358
IsClosed(napi_env env,napi_callback_info info)359 napi_value JsPath::IsClosed(napi_env env, napi_callback_info info)
360 {
361 JsPath* me = CheckParamsAndGetThis<JsPath>(env, info);
362 return (me != nullptr) ? me->OnIsClosed(env, info) : nullptr;
363 }
364
Op(napi_env env,napi_callback_info info)365 napi_value JsPath::Op(napi_env env, napi_callback_info info)
366 {
367 JsPath* me = CheckParamsAndGetThis<JsPath>(env, info);
368 return (me != nullptr) ? me->OnOp(env, info) : nullptr;
369 }
370
GetPathIterator(napi_env env,napi_callback_info info)371 napi_value JsPath::GetPathIterator(napi_env env, napi_callback_info info)
372 {
373 DRAWING_PERFORMANCE_TEST_JS_RETURN(nullptr);
374 JsPath* me = CheckParamsAndGetThis<JsPath>(env, info);
375 return (me != nullptr) ? me->OnGetPathIterator(env, info) : nullptr;
376 }
377
OnMoveTo(napi_env env,napi_callback_info info)378 napi_value JsPath::OnMoveTo(napi_env env, napi_callback_info info)
379 {
380 if (m_path == nullptr) {
381 ROSEN_LOGE("JsPath::OnMoveTo path is nullptr");
382 return NapiThrowError(env, DrawingErrorCode::ERROR_INVALID_PARAM, "Invalid params.");
383 }
384
385 napi_value argv[ARGC_TWO] = {nullptr};
386 CHECK_PARAM_NUMBER_WITHOUT_OPTIONAL_PARAMS(argv, ARGC_TWO);
387
388 double x = 0.0;
389 GET_DOUBLE_PARAM(ARGC_ZERO, x);
390 double y = 0.0;
391 GET_DOUBLE_PARAM(ARGC_ONE, y);
392
393 JS_CALL_DRAWING_FUNC(m_path->MoveTo(x, y));
394 return nullptr;
395 }
396
OnLineTo(napi_env env,napi_callback_info info)397 napi_value JsPath::OnLineTo(napi_env env, napi_callback_info info)
398 {
399 if (m_path == nullptr) {
400 ROSEN_LOGE("JsPath::OnLineTo path is nullptr");
401 return NapiThrowError(env, DrawingErrorCode::ERROR_INVALID_PARAM, "Invalid params.");
402 }
403
404 napi_value argv[ARGC_TWO] = {nullptr};
405 CHECK_PARAM_NUMBER_WITHOUT_OPTIONAL_PARAMS(argv, ARGC_TWO);
406
407 double x = 0.0;
408 GET_DOUBLE_PARAM(ARGC_ZERO, x);
409 double y = 0.0;
410 GET_DOUBLE_PARAM(ARGC_ONE, y);
411
412 JS_CALL_DRAWING_FUNC(m_path->LineTo(x, y));
413 return nullptr;
414 }
415
OnArcTo(napi_env env,napi_callback_info info)416 napi_value JsPath::OnArcTo(napi_env env, napi_callback_info info)
417 {
418 if (m_path == nullptr) {
419 ROSEN_LOGE("JsPath::OnArcTo path is nullptr");
420 return NapiThrowError(env, DrawingErrorCode::ERROR_INVALID_PARAM, "Invalid params.");
421 }
422
423 napi_value argv[ARGC_SIX] = {nullptr};
424 CHECK_PARAM_NUMBER_WITHOUT_OPTIONAL_PARAMS(argv, ARGC_SIX);
425
426 double x1 = 0.0;
427 GET_DOUBLE_PARAM(ARGC_ZERO, x1);
428 double y1 = 0.0;
429 GET_DOUBLE_PARAM(ARGC_ONE, y1);
430 double x2 = 0.0;
431 GET_DOUBLE_PARAM(ARGC_TWO, x2);
432 double y2 = 0.0;
433 GET_DOUBLE_PARAM(ARGC_THREE, y2);
434 double startDeg = 0.0;
435 GET_DOUBLE_PARAM(ARGC_FOUR, startDeg);
436 double sweepDeg = 0.0;
437 GET_DOUBLE_PARAM(ARGC_FIVE, sweepDeg);
438
439 JS_CALL_DRAWING_FUNC(m_path->ArcTo(x1, y1, x2, y2, startDeg, sweepDeg));
440 return nullptr;
441 }
442
OnQuadTo(napi_env env,napi_callback_info info)443 napi_value JsPath::OnQuadTo(napi_env env, napi_callback_info info)
444 {
445 if (m_path == nullptr) {
446 ROSEN_LOGE("JsPath::OnQuadTo path is nullptr");
447 return NapiThrowError(env, DrawingErrorCode::ERROR_INVALID_PARAM, "Invalid params.");
448 }
449
450 napi_value argv[ARGC_FOUR] = {nullptr};
451 CHECK_PARAM_NUMBER_WITHOUT_OPTIONAL_PARAMS(argv, ARGC_FOUR);
452
453 double ctrlPtX = 0.0;
454 GET_DOUBLE_PARAM(ARGC_ZERO, ctrlPtX);
455 double ctrlPtY = 0.0;
456 GET_DOUBLE_PARAM(ARGC_ONE, ctrlPtY);
457 double endPtX = 0.0;
458 GET_DOUBLE_PARAM(ARGC_TWO, endPtX);
459 double endPtY = 0.0;
460 GET_DOUBLE_PARAM(ARGC_THREE, endPtY);
461
462 JS_CALL_DRAWING_FUNC(m_path->QuadTo(ctrlPtX, ctrlPtY, endPtX, endPtY));
463 return nullptr;
464 }
465
OnConicTo(napi_env env,napi_callback_info info)466 napi_value JsPath::OnConicTo(napi_env env, napi_callback_info info)
467 {
468 if (m_path == nullptr) {
469 ROSEN_LOGE("JsPath::OnConicTo path is nullptr");
470 return NapiThrowError(env, DrawingErrorCode::ERROR_INVALID_PARAM, "Invalid params.");
471 }
472
473 napi_value argv[ARGC_FIVE] = { nullptr };
474 CHECK_PARAM_NUMBER_WITHOUT_OPTIONAL_PARAMS(argv, ARGC_FIVE);
475
476 double ctrlPtX = 0.0;
477 GET_DOUBLE_PARAM(ARGC_ZERO, ctrlPtX);
478 double ctrlPtY = 0.0;
479 GET_DOUBLE_PARAM(ARGC_ONE, ctrlPtY);
480 double endPtX = 0.0;
481 GET_DOUBLE_PARAM(ARGC_TWO, endPtX);
482 double endPtY = 0.0;
483 GET_DOUBLE_PARAM(ARGC_THREE, endPtY);
484 double weight = 0.0;
485 GET_DOUBLE_PARAM(ARGC_FOUR, weight);
486
487 m_path->ConicTo(ctrlPtX, ctrlPtY, endPtX, endPtY, weight);
488 return nullptr;
489 }
490
OnCubicTo(napi_env env,napi_callback_info info)491 napi_value JsPath::OnCubicTo(napi_env env, napi_callback_info info)
492 {
493 if (m_path == nullptr) {
494 ROSEN_LOGE("JsPath::OnCubicTo path is nullptr");
495 return NapiThrowError(env, DrawingErrorCode::ERROR_INVALID_PARAM, "Invalid params.");
496 }
497
498 napi_value argv[ARGC_SIX] = {nullptr};
499 CHECK_PARAM_NUMBER_WITHOUT_OPTIONAL_PARAMS(argv, ARGC_SIX);
500
501 double px1 = 0.0;
502 GET_DOUBLE_PARAM(ARGC_ZERO, px1);
503 double py1 = 0.0;
504 GET_DOUBLE_PARAM(ARGC_ONE, py1);
505 double px2 = 0.0;
506 GET_DOUBLE_PARAM(ARGC_TWO, px2);
507 double py2 = 0.0;
508 GET_DOUBLE_PARAM(ARGC_THREE, py2);
509 double px3 = 0.0;
510 GET_DOUBLE_PARAM(ARGC_FOUR, px3);
511 double py3 = 0.0;
512 GET_DOUBLE_PARAM(ARGC_FIVE, py3);
513
514 JS_CALL_DRAWING_FUNC(m_path->CubicTo(Point(px1, py1), Point(px2, py2), Point(px3, py3)));
515 return nullptr;
516 }
517
OnRMoveTo(napi_env env,napi_callback_info info)518 napi_value JsPath::OnRMoveTo(napi_env env, napi_callback_info info)
519 {
520 if (m_path == nullptr) {
521 ROSEN_LOGE("JsPath::OnRMoveTo path is nullptr");
522 return NapiThrowError(env, DrawingErrorCode::ERROR_INVALID_PARAM, "Invalid params.");
523 }
524
525 napi_value argv[ARGC_TWO] = { nullptr };
526 CHECK_PARAM_NUMBER_WITHOUT_OPTIONAL_PARAMS(argv, ARGC_TWO);
527
528 double dx = 0.0;
529 GET_DOUBLE_PARAM(ARGC_ZERO, dx);
530 double dy = 0.0;
531 GET_DOUBLE_PARAM(ARGC_ONE, dy);
532
533 m_path->RMoveTo(dx, dy);
534 return nullptr;
535 }
536
OnRLineTo(napi_env env,napi_callback_info info)537 napi_value JsPath::OnRLineTo(napi_env env, napi_callback_info info)
538 {
539 if (m_path == nullptr) {
540 ROSEN_LOGE("JsPath::OnRLineTo path is nullptr");
541 return NapiThrowError(env, DrawingErrorCode::ERROR_INVALID_PARAM, "Invalid params.");
542 }
543
544 napi_value argv[ARGC_TWO] = { nullptr };
545 CHECK_PARAM_NUMBER_WITHOUT_OPTIONAL_PARAMS(argv, ARGC_TWO);
546
547 double dx = 0.0;
548 GET_DOUBLE_PARAM(ARGC_ZERO, dx);
549 double dy = 0.0;
550 GET_DOUBLE_PARAM(ARGC_ONE, dy);
551
552 m_path->RLineTo(dx, dy);
553 return nullptr;
554 }
555
OnRQuadTo(napi_env env,napi_callback_info info)556 napi_value JsPath::OnRQuadTo(napi_env env, napi_callback_info info)
557 {
558 if (m_path == nullptr) {
559 ROSEN_LOGE("JsPath::OnRQuadTo path is nullptr");
560 return NapiThrowError(env, DrawingErrorCode::ERROR_INVALID_PARAM, "Invalid params.");
561 }
562
563 napi_value argv[ARGC_FOUR] = { nullptr };
564 CHECK_PARAM_NUMBER_WITHOUT_OPTIONAL_PARAMS(argv, ARGC_FOUR);
565
566 double dx1 = 0.0;
567 GET_DOUBLE_PARAM(ARGC_ZERO, dx1);
568 double dy1 = 0.0;
569 GET_DOUBLE_PARAM(ARGC_ONE, dy1);
570 double dx2 = 0.0;
571 GET_DOUBLE_PARAM(ARGC_TWO, dx2);
572 double dy2 = 0.0;
573 GET_DOUBLE_PARAM(ARGC_THREE, dy2);
574
575 m_path->RQuadTo(dx1, dy1, dx2, dy2);
576 return nullptr;
577 }
578
OnRConicTo(napi_env env,napi_callback_info info)579 napi_value JsPath::OnRConicTo(napi_env env, napi_callback_info info)
580 {
581 if (m_path == nullptr) {
582 ROSEN_LOGE("JsPath::OnRConicTo path is nullptr");
583 return NapiThrowError(env, DrawingErrorCode::ERROR_INVALID_PARAM, "Invalid params.");
584 }
585
586 napi_value argv[ARGC_FIVE] = { nullptr };
587 CHECK_PARAM_NUMBER_WITHOUT_OPTIONAL_PARAMS(argv, ARGC_FIVE);
588
589 double ctrlPtX = 0.0;
590 GET_DOUBLE_PARAM(ARGC_ZERO, ctrlPtX);
591 double ctrlPtY = 0.0;
592 GET_DOUBLE_PARAM(ARGC_ONE, ctrlPtY);
593 double endPtX = 0.0;
594 GET_DOUBLE_PARAM(ARGC_TWO, endPtX);
595 double endPtY = 0.0;
596 GET_DOUBLE_PARAM(ARGC_THREE, endPtY);
597 double weight = 0.0;
598 GET_DOUBLE_PARAM(ARGC_FOUR, weight);
599
600 m_path->RConicTo(ctrlPtX, ctrlPtY, endPtX, endPtY, weight);
601 return nullptr;
602 }
603
OnRCubicTo(napi_env env,napi_callback_info info)604 napi_value JsPath::OnRCubicTo(napi_env env, napi_callback_info info)
605 {
606 if (m_path == nullptr) {
607 ROSEN_LOGE("JsPath::OnRCubicTo path is nullptr");
608 return NapiThrowError(env, DrawingErrorCode::ERROR_INVALID_PARAM, "Invalid params.");
609 }
610
611 napi_value argv[ARGC_SIX] = { nullptr };
612 CHECK_PARAM_NUMBER_WITHOUT_OPTIONAL_PARAMS(argv, ARGC_SIX);
613
614 double px1 = 0.0;
615 GET_DOUBLE_PARAM(ARGC_ZERO, px1);
616 double py1 = 0.0;
617 GET_DOUBLE_PARAM(ARGC_ONE, py1);
618 double px2 = 0.0;
619 GET_DOUBLE_PARAM(ARGC_TWO, px2);
620 double py2 = 0.0;
621 GET_DOUBLE_PARAM(ARGC_THREE, py2);
622 double px3 = 0.0;
623 GET_DOUBLE_PARAM(ARGC_FOUR, px3);
624 double py3 = 0.0;
625 GET_DOUBLE_PARAM(ARGC_FIVE, py3);
626
627 m_path->RCubicTo(px1, py1, px2, py2, px3, py3);
628 return nullptr;
629 }
630
OnAddPolygon(napi_env env,napi_callback_info info)631 napi_value JsPath::OnAddPolygon(napi_env env, napi_callback_info info)
632 {
633 napi_value argv[ARGC_TWO] = { nullptr };
634 CHECK_PARAM_NUMBER_WITHOUT_OPTIONAL_PARAMS(argv, ARGC_TWO);
635
636 napi_value array = argv[ARGC_ZERO];
637 uint32_t size = 0;
638 napi_get_array_length(env, array, &size);
639 if (size == 0) {
640 return nullptr;
641 }
642 std::vector<Point> points;
643 points.reserve(size);
644 for (uint32_t i = 0; i < size; i++) {
645 napi_value tempRunBuffer = nullptr;
646 napi_get_element(env, array, i, &tempRunBuffer);
647
648 napi_value tempValue = nullptr;
649 Point point = Point();
650 double positionX = 0.0;
651 double positionY = 0.0;
652 napi_get_named_property(env, tempRunBuffer, "x", &tempValue);
653 bool isPositionXOk = ConvertFromJsValue(env, tempValue, positionX);
654 napi_get_named_property(env, tempRunBuffer, "y", &tempValue);
655 bool isPositionYOk = ConvertFromJsValue(env, tempValue, positionY);
656 if (!(isPositionXOk && isPositionYOk)) {
657 ROSEN_LOGE("JsPath::OnAddPolygon Argv is invalid");
658 return napi_value();
659 }
660
661 point.SetX(positionX);
662 point.SetY(positionY);
663 points.emplace_back(point);
664 }
665 if (points.size() != size) {
666 ROSEN_LOGE("JsPath::OnAddPolygon Argv is invalid");
667 return nullptr;
668 }
669
670 bool close = false;
671 GET_BOOLEAN_PARAM(ARGC_ONE, close);
672
673 m_path->AddPoly(points, size, close);
674 return nullptr;
675 }
676
OnOp(napi_env env,napi_callback_info info)677 napi_value JsPath::OnOp(napi_env env, napi_callback_info info)
678 {
679 if (m_path == nullptr) {
680 ROSEN_LOGE("JsPath::OnOp path is nullptr");
681 return NapiThrowError(env, DrawingErrorCode::ERROR_INVALID_PARAM, "Invalid params.");
682 }
683
684 napi_value argv[ARGC_TWO] = {nullptr};
685 CHECK_PARAM_NUMBER_WITHOUT_OPTIONAL_PARAMS(argv, ARGC_TWO);
686
687 JsPath* jsPath = nullptr;
688 GET_UNWRAP_PARAM(ARGC_ZERO, jsPath);
689 if (jsPath->GetPath() == nullptr) {
690 ROSEN_LOGE("JsPath::OnOp path is nullptr");
691 return nullptr;
692 }
693
694 int32_t jsPathOp = 0;
695 GET_ENUM_PARAM(ARGC_ONE, jsPathOp, 0, static_cast<int32_t>(PathOp::REVERSE_DIFFERENCE));
696 return CreateJsValue(env, m_path->Op(*m_path, *jsPath->GetPath(), static_cast<PathOp>(jsPathOp)));
697 }
698
OnClose(napi_env env,napi_callback_info info)699 napi_value JsPath::OnClose(napi_env env, napi_callback_info info)
700 {
701 if (m_path == nullptr) {
702 ROSEN_LOGE("JsPath::OnClose path is nullptr");
703 return NapiThrowError(env, DrawingErrorCode::ERROR_INVALID_PARAM, "Invalid params.");
704 }
705
706 JS_CALL_DRAWING_FUNC(m_path->Close());
707 return nullptr;
708 }
709
OnOffset(napi_env env,napi_callback_info info)710 napi_value JsPath::OnOffset(napi_env env, napi_callback_info info)
711 {
712 if (m_path == nullptr) {
713 ROSEN_LOGE("JsPath::OnOffset path is nullptr");
714 return NapiThrowError(env, DrawingErrorCode::ERROR_INVALID_PARAM, "Invalid params.");
715 }
716
717 napi_value argv[ARGC_TWO] = { nullptr };
718 CHECK_PARAM_NUMBER_WITHOUT_OPTIONAL_PARAMS(argv, ARGC_TWO);
719
720 double dx = 0.0;
721 GET_DOUBLE_PARAM(ARGC_ZERO, dx);
722 double dy = 0.0;
723 GET_DOUBLE_PARAM(ARGC_ONE, dy);
724 Path* path = new Path();
725 m_path->Offset(path, dx, dy);
726 return CreateJsPath(env, path);
727 }
728
OnReset(napi_env env,napi_callback_info info)729 napi_value JsPath::OnReset(napi_env env, napi_callback_info info)
730 {
731 if (m_path == nullptr) {
732 ROSEN_LOGE("JsPath::OnReset path is nullptr");
733 return NapiThrowError(env, DrawingErrorCode::ERROR_INVALID_PARAM, "Invalid params.");
734 }
735
736 JS_CALL_DRAWING_FUNC(m_path->Reset());
737 return nullptr;
738 }
739
OnGetLength(napi_env env,napi_callback_info info)740 napi_value JsPath::OnGetLength(napi_env env, napi_callback_info info)
741 {
742 if (m_path == nullptr) {
743 ROSEN_LOGE("JsPath::OnGetLength path is nullptr");
744 return NapiThrowError(env, DrawingErrorCode::ERROR_INVALID_PARAM, "Invalid params.");
745 }
746 napi_value argv[ARGC_ONE] = {nullptr};
747 CHECK_PARAM_NUMBER_WITHOUT_OPTIONAL_PARAMS(argv, ARGC_ONE);
748
749 bool forceClosed = false;
750 GET_BOOLEAN_PARAM(ARGC_ZERO, forceClosed);
751
752 double len = m_path->GetLength(forceClosed);
753 return CreateJsNumber(env, len);
754 }
755
OnGetPositionAndTangent(napi_env env,napi_callback_info info)756 napi_value JsPath::OnGetPositionAndTangent(napi_env env, napi_callback_info info)
757 {
758 if (m_path == nullptr) {
759 ROSEN_LOGE("JsPath::OnGetPositionAndTangent path is nullptr");
760 return NapiThrowError(env, DrawingErrorCode::ERROR_INVALID_PARAM, "Invalid params.");
761 }
762
763 napi_value argv[ARGC_FOUR] = {nullptr};
764 CHECK_PARAM_NUMBER_WITHOUT_OPTIONAL_PARAMS(argv, ARGC_FOUR);
765
766 bool forceClosed = false;
767 GET_BOOLEAN_PARAM(ARGC_ZERO, forceClosed);
768
769 double distance = 0.0;
770 GET_DOUBLE_PARAM(ARGC_ONE, distance);
771
772 Drawing::Point position;
773 double startPoint[ARGC_TWO] = {0};
774 if (!ConvertFromJsPoint(env, argv[ARGC_TWO], startPoint, ARGC_TWO)) {
775 return NapiThrowError(env, DrawingErrorCode::ERROR_INVALID_PARAM,
776 "Incorrect parameter2 type. The type of x, y be number.");
777 }
778 position = Drawing::Point(startPoint[ARGC_ZERO], startPoint[ARGC_ONE]);
779
780 Drawing::Point tangent;
781 double endPoint[ARGC_TWO] = {0};
782 if (!ConvertFromJsPoint(env, argv[ARGC_THREE], endPoint, ARGC_TWO)) {
783 return NapiThrowError(env, DrawingErrorCode::ERROR_INVALID_PARAM,
784 "Incorrect parameter3 type. The type of x, y be number.");
785 }
786 tangent = Drawing::Point(endPoint[ARGC_ZERO], endPoint[ARGC_ONE]);
787
788 bool result = m_path->GetPositionAndTangent(distance, position, tangent, forceClosed);
789 if (napi_set_named_property(env, argv[ARGC_TWO], "x", CreateJsNumber(env, position.GetX())) != napi_ok ||
790 napi_set_named_property(env, argv[ARGC_TWO], "y", CreateJsNumber(env, position.GetY())) != napi_ok ||
791 napi_set_named_property(env, argv[ARGC_THREE], "x", CreateJsNumber(env, tangent.GetX())) != napi_ok ||
792 napi_set_named_property(env, argv[ARGC_THREE], "y", CreateJsNumber(env, tangent.GetY())) != napi_ok) {
793 return NapiThrowError(env, DrawingErrorCode::ERROR_INVALID_PARAM,
794 "JsPath::OnGetPositionAndTangent Cannot fill 'position' and 'tangent' Point type.");
795 }
796 return CreateJsValue(env, result);
797 }
798
OnGetSegment(napi_env env,napi_callback_info info)799 napi_value JsPath::OnGetSegment(napi_env env, napi_callback_info info)
800 {
801 if (m_path == nullptr) {
802 ROSEN_LOGE("JsPath::OnGetSegment path is nullptr");
803 return NapiThrowError(env, DrawingErrorCode::ERROR_INVALID_PARAM, "Invalid params.");
804 }
805 napi_value argv[ARGC_FIVE] = {nullptr};
806 CHECK_PARAM_NUMBER_WITHOUT_OPTIONAL_PARAMS(argv, ARGC_FIVE);
807
808 bool forceClosed = false;
809 GET_BOOLEAN_PARAM(ARGC_ZERO, forceClosed);
810 double start = 0.0;
811 GET_DOUBLE_PARAM(ARGC_ONE, start);
812 double stop = 0.0;
813 GET_DOUBLE_PARAM(ARGC_TWO, stop);
814 bool startWithMoveTo = false;
815 GET_BOOLEAN_PARAM(ARGC_THREE, startWithMoveTo);
816 JsPath* jsPath = nullptr;
817 GET_UNWRAP_PARAM(ARGC_FOUR, jsPath);
818
819 Path* dst = jsPath->GetPath();
820 if (dst == nullptr) {
821 ROSEN_LOGE("JsPath::OnGetSegment dst is nullptr");
822 return NapiThrowError(env, DrawingErrorCode::ERROR_INVALID_PARAM, "Incorrect OnGetSegment parameter3 type.");
823 }
824
825 double length = m_path->GetLength(forceClosed);
826 if (start < 0) {
827 start = 0;
828 }
829 if (stop > length) {
830 stop = length;
831 }
832 if (start >= stop) {
833 return CreateJsValue(env, false);
834 }
835 return CreateJsValue(env, m_path->GetSegment(start, stop, dst, startWithMoveTo, forceClosed));
836 }
837
OnGetMatrix(napi_env env,napi_callback_info info)838 napi_value JsPath::OnGetMatrix(napi_env env, napi_callback_info info)
839 {
840 if (m_path == nullptr) {
841 ROSEN_LOGE("JsPath::OnGetMatrix path is nullptr");
842 return NapiThrowError(env, DrawingErrorCode::ERROR_INVALID_PARAM, "Invalid params.");
843 }
844
845 napi_value argv[ARGC_FOUR] = {nullptr};
846 CHECK_PARAM_NUMBER_WITHOUT_OPTIONAL_PARAMS(argv, ARGC_FOUR);
847
848 bool forceClosed = false;
849 GET_BOOLEAN_PARAM(ARGC_ZERO, forceClosed);
850
851 double distance = 0.0;
852 GET_DOUBLE_PARAM(ARGC_ONE, distance);
853
854 JsMatrix* jsMatrix = nullptr;
855 GET_UNWRAP_PARAM(ARGC_TWO, jsMatrix);
856
857 if (jsMatrix->GetMatrix() == nullptr) {
858 ROSEN_LOGE("JsPath::OnGetMatrix jsMatrix is nullptr");
859 return NapiThrowError(env, DrawingErrorCode::ERROR_INVALID_PARAM, "Invalid params.");
860 }
861
862 int32_t flag = 0;
863 GET_ENUM_PARAM(ARGC_THREE, flag, 0,
864 static_cast<int32_t>(PathMeasureMatrixFlags::GET_POS_AND_TAN_MATRIX));
865 bool result = m_path->GetMatrix(
866 forceClosed,
867 distance,
868 jsMatrix->GetMatrix().get(),
869 static_cast<PathMeasureMatrixFlags>(flag));
870 return CreateJsValue(env, result);
871 }
872
OnBuildFromSvgString(napi_env env,napi_callback_info info)873 napi_value JsPath::OnBuildFromSvgString(napi_env env, napi_callback_info info)
874 {
875 if (m_path == nullptr) {
876 ROSEN_LOGE("JsPath::OnBuildFromSVGString path is nullptr");
877 return NapiThrowError(env, DrawingErrorCode::ERROR_INVALID_PARAM, "Invalid params.");
878 }
879
880 napi_value argv[ARGC_ONE] = { nullptr };
881 CHECK_PARAM_NUMBER_WITHOUT_OPTIONAL_PARAMS(argv, ARGC_ONE);
882
883 std::string str = {""};
884 if (!(ConvertFromJsValue(env, argv[ARGC_ZERO], str))) {
885 ROSEN_LOGE("JsPath::OnBuildFromSVGString Argv is invalid");
886 return NapiThrowError(env, DrawingErrorCode::ERROR_INVALID_PARAM, "Invalid params.");
887 }
888
889 bool result = m_path->BuildFromSVGString(str);
890 return CreateJsValue(env, result);
891 }
892
OnAddOval(napi_env env,napi_callback_info info)893 napi_value JsPath::OnAddOval(napi_env env, napi_callback_info info)
894 {
895 if (m_path == nullptr) {
896 ROSEN_LOGE("JsPath::OnAddOval path is nullptr");
897 return NapiThrowError(env, DrawingErrorCode::ERROR_INVALID_PARAM, "Invalid params.");
898 }
899
900 size_t argc = ARGC_THREE;
901 napi_value argv[ARGC_THREE] = {nullptr};
902 CHECK_PARAM_NUMBER_WITH_OPTIONAL_PARAMS(argv, argc, ARGC_TWO, ARGC_THREE);
903
904 Drawing::Rect drawingRect;
905 double ltrb[ARGC_FOUR] = {0};
906 if (!ConvertFromJsRect(env, argv[ARGC_ZERO], ltrb, ARGC_FOUR)) {
907 return NapiThrowError(env, DrawingErrorCode::ERROR_INVALID_PARAM,
908 "Incorrect parameter0 type. The type of left, top, right and bottom must be number.");
909 }
910 drawingRect = Drawing::Rect(ltrb[ARGC_ZERO], ltrb[ARGC_ONE], ltrb[ARGC_TWO], ltrb[ARGC_THREE]);
911
912 int32_t start = 0;
913 GET_INT32_CHECK_GE_ZERO_PARAM(ARGC_ONE, start);
914 if (argc == ARGC_TWO) {
915 JS_CALL_DRAWING_FUNC(m_path->AddOval(drawingRect, start,
916 static_cast<PathDirection>(PathDirection::CW_DIRECTION)));
917 return nullptr;
918 }
919 int32_t jsDirection = 0;
920 GET_ENUM_PARAM(ARGC_TWO, jsDirection, 0, static_cast<int32_t>(PathDirection::CCW_DIRECTION));
921
922 JS_CALL_DRAWING_FUNC(m_path->AddOval(drawingRect, start, static_cast<PathDirection>(jsDirection)));
923 return nullptr;
924 }
925
OnAddCircle(napi_env env,napi_callback_info info)926 napi_value JsPath::OnAddCircle(napi_env env, napi_callback_info info)
927 {
928 if (m_path == nullptr) {
929 ROSEN_LOGE("JsPath::OnAddCircle path is nullptr");
930 return NapiThrowError(env, DrawingErrorCode::ERROR_INVALID_PARAM, "Invalid params.");
931 }
932
933 size_t argc = ARGC_FOUR;
934 napi_value argv[ARGC_FOUR] = {nullptr};
935 CHECK_PARAM_NUMBER_WITH_OPTIONAL_PARAMS(argv, argc, ARGC_THREE, ARGC_FOUR);
936
937 double px1 = 0.0;
938 GET_DOUBLE_PARAM(ARGC_ZERO, px1);
939 double py1 = 0.0;
940 GET_DOUBLE_PARAM(ARGC_ONE, py1);
941 double radius = 0.0;
942 GET_DOUBLE_PARAM(ARGC_TWO, radius);
943 if (argc == ARGC_THREE) {
944 JS_CALL_DRAWING_FUNC(m_path->AddCircle(px1, py1, radius));
945 return nullptr;
946 }
947 int32_t jsDirection = 0;
948 GET_ENUM_PARAM(ARGC_THREE, jsDirection, 0, static_cast<int32_t>(PathDirection::CCW_DIRECTION));
949
950 JS_CALL_DRAWING_FUNC(m_path->AddCircle(px1, py1, radius, static_cast<PathDirection>(jsDirection)));
951 return nullptr;
952 }
953
OnAddArc(napi_env env,napi_callback_info info)954 napi_value JsPath::OnAddArc(napi_env env, napi_callback_info info)
955 {
956 if (m_path == nullptr) {
957 ROSEN_LOGE("JsPath::OnAddArc path is nullptr");
958 return NapiThrowError(env, DrawingErrorCode::ERROR_INVALID_PARAM, "Invalid params.");
959 }
960
961 napi_value argv[ARGC_THREE] = {nullptr};
962 CHECK_PARAM_NUMBER_WITHOUT_OPTIONAL_PARAMS(argv, ARGC_THREE);
963
964 Drawing::Rect drawingRect;
965 double ltrb[ARGC_FOUR] = {0};
966 if (!ConvertFromJsRect(env, argv[ARGC_ZERO], ltrb, ARGC_FOUR)) {
967 return NapiThrowError(env, DrawingErrorCode::ERROR_INVALID_PARAM,
968 "Incorrect parameter0 type. The type of left, top, right and bottom must be number.");
969 }
970 drawingRect = Drawing::Rect(ltrb[ARGC_ZERO], ltrb[ARGC_ONE], ltrb[ARGC_TWO], ltrb[ARGC_THREE]);
971
972 double startDeg = 0.0;
973 GET_DOUBLE_PARAM(ARGC_ONE, startDeg);
974 double sweepDeg = 0.0;
975 GET_DOUBLE_PARAM(ARGC_TWO, sweepDeg);
976
977 JS_CALL_DRAWING_FUNC(m_path->AddArc(drawingRect, startDeg, sweepDeg));
978 return nullptr;
979 }
980
OnAddRect(napi_env env,napi_callback_info info)981 napi_value JsPath::OnAddRect(napi_env env, napi_callback_info info)
982 {
983 if (m_path == nullptr) {
984 ROSEN_LOGE("JsPath::OnAddRect path is nullptr");
985 return NapiThrowError(env, DrawingErrorCode::ERROR_INVALID_PARAM, "Invalid params.");
986 }
987
988 size_t argc = ARGC_TWO;
989 napi_value argv[ARGC_TWO] = {nullptr};
990 CHECK_PARAM_NUMBER_WITH_OPTIONAL_PARAMS(argv, argc, ARGC_ONE, ARGC_TWO);
991
992 Drawing::Rect drawingRect;
993 double ltrb[ARGC_FOUR] = {0};
994 if (!ConvertFromJsRect(env, argv[ARGC_ZERO], ltrb, ARGC_FOUR)) {
995 return NapiThrowError(env, DrawingErrorCode::ERROR_INVALID_PARAM,
996 "Incorrect parameter0 type. The type of left, top, right and bottom must be number.");
997 }
998 drawingRect = Drawing::Rect(ltrb[ARGC_ZERO], ltrb[ARGC_ONE], ltrb[ARGC_TWO], ltrb[ARGC_THREE]);
999 if (argc == ARGC_ONE) {
1000 JS_CALL_DRAWING_FUNC(m_path->AddRect(drawingRect));
1001 return nullptr;
1002 }
1003 int32_t jsDirection = 0;
1004 GET_ENUM_PARAM(ARGC_ONE, jsDirection, 0, static_cast<int32_t>(PathDirection::CCW_DIRECTION));
1005
1006 JS_CALL_DRAWING_FUNC(m_path->AddRect(drawingRect, static_cast<PathDirection>(jsDirection)));
1007 return nullptr;
1008 }
1009
OnAddRoundRect(napi_env env,napi_callback_info info)1010 napi_value JsPath::OnAddRoundRect(napi_env env, napi_callback_info info)
1011 {
1012 if (m_path == nullptr) {
1013 ROSEN_LOGE("JsPath::OnAddRoundRect path is nullptr");
1014 return NapiThrowError(env, DrawingErrorCode::ERROR_INVALID_PARAM, "Invalid params.");
1015 }
1016
1017 size_t argc = ARGC_TWO;
1018 napi_value argv[ARGC_TWO] = {nullptr};
1019 CHECK_PARAM_NUMBER_WITH_OPTIONAL_PARAMS(argv, argc, ARGC_ONE, ARGC_TWO);
1020
1021 JsRoundRect* jsRoundRect = nullptr;
1022 GET_UNWRAP_PARAM(ARGC_ZERO, jsRoundRect);
1023 if (argc == ARGC_ONE) {
1024 JS_CALL_DRAWING_FUNC(m_path->AddRoundRect(jsRoundRect->GetRoundRect()));
1025 return nullptr;
1026 }
1027 int32_t jsDirection = 0;
1028 GET_ENUM_PARAM(ARGC_ONE, jsDirection, 0, static_cast<int32_t>(PathDirection::CCW_DIRECTION));
1029
1030 JS_CALL_DRAWING_FUNC(m_path->AddRoundRect(jsRoundRect->GetRoundRect(), static_cast<PathDirection>(jsDirection)));
1031 return nullptr;
1032 }
1033
OnAddPath(napi_env env,napi_callback_info info)1034 napi_value JsPath::OnAddPath(napi_env env, napi_callback_info info)
1035 {
1036 if (m_path == nullptr) {
1037 ROSEN_LOGE("JsPath::OnAddPath path is nullptr");
1038 return NapiThrowError(env, DrawingErrorCode::ERROR_INVALID_PARAM, "Invalid params.");
1039 }
1040
1041 size_t argc = ARGC_TWO;
1042 napi_value argv[ARGC_TWO] = {nullptr};
1043 CHECK_PARAM_NUMBER_WITH_OPTIONAL_PARAMS(argv, argc, ARGC_ONE, ARGC_TWO);
1044
1045 JsPath* jsPath = nullptr;
1046 GET_UNWRAP_PARAM(ARGC_ZERO, jsPath);
1047 if (jsPath->GetPath() == nullptr) {
1048 ROSEN_LOGE("JsPath::OnAddPath path is nullptr");
1049 return nullptr;
1050 }
1051 if (argc == ARGC_ONE) {
1052 JS_CALL_DRAWING_FUNC(m_path->AddPath(*jsPath->GetPath(), Drawing::Matrix()));
1053 return nullptr;
1054 }
1055 Drawing::Matrix* drawingMatrixPtr = nullptr;
1056 napi_valuetype valueType = napi_undefined;
1057 if (napi_typeof(env, argv[ARGC_ONE], &valueType) != napi_ok ||
1058 (valueType != napi_null && valueType != napi_object)) {
1059 return NapiThrowError(env, DrawingErrorCode::ERROR_INVALID_PARAM, "Incorrect OnAddPath parameter1 type.");
1060 }
1061 if (valueType == napi_object) {
1062 JsMatrix* jsMatrix = nullptr;
1063 GET_UNWRAP_PARAM(ARGC_ONE, jsMatrix);
1064 if (jsMatrix->GetMatrix() != nullptr) {
1065 drawingMatrixPtr = jsMatrix->GetMatrix().get();
1066 }
1067 }
1068 JS_CALL_DRAWING_FUNC(m_path->AddPath(*jsPath->GetPath(),
1069 drawingMatrixPtr ? *drawingMatrixPtr : Drawing::Matrix()));
1070 return nullptr;
1071 }
1072
OnTransform(napi_env env,napi_callback_info info)1073 napi_value JsPath::OnTransform(napi_env env, napi_callback_info info)
1074 {
1075 if (m_path == nullptr) {
1076 ROSEN_LOGE("JsPath::OnTransform path is nullptr");
1077 return NapiThrowError(env, DrawingErrorCode::ERROR_INVALID_PARAM, "Invalid params.");
1078 }
1079
1080 napi_value argv[ARGC_ONE] = {nullptr};
1081 CHECK_PARAM_NUMBER_WITHOUT_OPTIONAL_PARAMS(argv, ARGC_ONE);
1082
1083 JsMatrix* jsMatrix = nullptr;
1084 GET_UNWRAP_PARAM(ARGC_ZERO, jsMatrix);
1085 if (jsMatrix->GetMatrix() == nullptr) {
1086 ROSEN_LOGE("JsPath::OnAddPath Matrix is nullptr");
1087 return nullptr;
1088 }
1089
1090 JS_CALL_DRAWING_FUNC(m_path->Transform(*jsMatrix->GetMatrix()));
1091 return nullptr;
1092 }
1093
OnContains(napi_env env,napi_callback_info info)1094 napi_value JsPath::OnContains(napi_env env, napi_callback_info info)
1095 {
1096 if (m_path == nullptr) {
1097 ROSEN_LOGE("JsPath::OnContains path is nullptr");
1098 return NapiThrowError(env, DrawingErrorCode::ERROR_INVALID_PARAM, "Invalid params.");
1099 }
1100
1101 napi_value argv[ARGC_TWO] = {nullptr};
1102 CHECK_PARAM_NUMBER_WITHOUT_OPTIONAL_PARAMS(argv, ARGC_TWO);
1103
1104 double x = 0.0;
1105 GET_DOUBLE_PARAM(ARGC_ZERO, x);
1106 double y = 0.0;
1107 GET_DOUBLE_PARAM(ARGC_ONE, y);
1108
1109 return CreateJsValue(env, m_path->Contains(x, y));
1110 }
1111
OnSetFillType(napi_env env,napi_callback_info info)1112 napi_value JsPath::OnSetFillType(napi_env env, napi_callback_info info)
1113 {
1114 if (m_path == nullptr) {
1115 ROSEN_LOGE("JsPath::OnSetFillType path is nullptr");
1116 return NapiThrowError(env, DrawingErrorCode::ERROR_INVALID_PARAM, "Invalid params.");
1117 }
1118
1119 napi_value argv[ARGC_ONE] = {nullptr};
1120 CHECK_PARAM_NUMBER_WITHOUT_OPTIONAL_PARAMS(argv, ARGC_ONE);
1121
1122 int32_t jsFillType = 0;
1123 GET_ENUM_PARAM(ARGC_ZERO, jsFillType, 0, static_cast<int32_t>(PathFillType::INVERSE_EVENTODD));
1124
1125 JS_CALL_DRAWING_FUNC(m_path->SetFillStyle(static_cast<PathFillType>(jsFillType)));
1126 return nullptr;
1127 }
1128
OnGetBounds(napi_env env,napi_callback_info info)1129 napi_value JsPath::OnGetBounds(napi_env env, napi_callback_info info)
1130 {
1131 if (m_path == nullptr) {
1132 ROSEN_LOGE("JsPath::OnGetBounds path is nullptr");
1133 return NapiThrowError(env, DrawingErrorCode::ERROR_INVALID_PARAM, "Invalid params.");
1134 }
1135 auto bounds = m_path->GetBounds();
1136 std::shared_ptr<Rect> rect = std::make_shared<Rect>(bounds.GetLeft(),
1137 bounds.GetTop(), bounds.GetRight(), bounds.GetBottom());
1138
1139 return GetRectAndConvertToJsValue(env, rect);
1140 }
1141
OnIsClosed(napi_env env,napi_callback_info info)1142 napi_value JsPath::OnIsClosed(napi_env env, napi_callback_info info)
1143 {
1144 if (m_path == nullptr) {
1145 ROSEN_LOGE("JsPath::OnIsClosed path is nullptr");
1146 return NapiThrowError(env, DrawingErrorCode::ERROR_INVALID_PARAM, "Invalid params.");
1147 }
1148
1149 bool result = m_path->IsClosed(false);
1150 return CreateJsValue(env, result);
1151 }
1152
OnGetPathIterator(napi_env env,napi_callback_info info)1153 napi_value JsPath::OnGetPathIterator(napi_env env, napi_callback_info info)
1154 {
1155 if (m_path == nullptr) {
1156 ROSEN_LOGE("JsPath::OnGetPathIterator path is nullptr");
1157 return NapiThrowError(env, DrawingErrorCode::ERROR_INVALID_PARAM, "Invalid params.");
1158 }
1159
1160 DRAWING_PERFORMANCE_TEST_NAP_RETURN(nullptr);
1161 PathIterator *iter = new PathIterator(*m_path);
1162 return JsPathIterator::CreateJsPathIterator(env, iter);
1163 }
1164
GetPath()1165 Path* JsPath::GetPath()
1166 {
1167 return m_path;
1168 }
1169 } // namespace Drawing
1170 } // namespace OHOS::Rosen
1171