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/canvas/js_path2d.h"
17
18 #include "base/utils/utils.h"
19 #include "bridge/declarative_frontend/jsview/canvas/js_canvas_renderer.h"
20 #include "bridge/declarative_frontend/jsview/js_view_common_def.h"
21
22 namespace OHOS::Ace::Framework {
JSPath2D()23 JSPath2D::JSPath2D() {}
24
Constructor(const JSCallbackInfo & args)25 void JSPath2D::Constructor(const JSCallbackInfo& args)
26 {
27 auto jsPath2d = Referenced::MakeRefPtr<JSPath2D>();
28 jsPath2d->IncRefCount();
29 args.SetReturnValue(Referenced::RawPtr(jsPath2d));
30 int32_t unit = 0;
31 if (args.GetInt32Arg(std::abs(args.Length() - 1), unit) && (static_cast<CanvasUnit>(unit) == CanvasUnit::PX)) {
32 jsPath2d->SetUnit(CanvasUnit::PX);
33 }
34 jsPath2d->SetCanvasPath2d(JSCanvasRenderer::JsMakePath2D(args));
35 args.SetSize(sizeof(JSPath2D));
36 EcmaVM* vm = args.GetVm();
37 CHECK_NULL_VOID(vm);
38 Local<ObjectRef> pathCmdObj = ObjectRef::New(vm);
39 pathCmdObj->SetNativePointerFieldCount(vm, 1);
40 jsPath2d->pathCmdObj_ = panda::CopyableGlobal<panda::JSValueRef>(vm, pathCmdObj);
41 }
42
Destructor(JSPath2D * controller)43 void JSPath2D::Destructor(JSPath2D* controller)
44 {
45 if (controller != nullptr) {
46 controller->DecRefCount();
47 }
48 }
49
JSBind(BindingTarget globalObj)50 void JSPath2D::JSBind(BindingTarget globalObj)
51 {
52 JSClass<JSPath2D>::Declare("Path2D");
53 JSClass<JSPath2D>::CustomMethod("addPath", &JSPath2D::JsPath2DAddPath);
54 JSClass<JSPath2D>::CustomMethod("setTransform", &JSCanvasPath::JsPath2DSetTransform);
55 JSClass<JSPath2D>::CustomMethod("moveTo", &JSCanvasPath::JsPath2DMoveTo);
56 JSClass<JSPath2D>::CustomMethod("lineTo", &JSCanvasPath::JsPath2DLineTo);
57 JSClass<JSPath2D>::CustomMethod("arc", &JSCanvasPath::JsPath2DArc);
58 JSClass<JSPath2D>::CustomMethod("arcTo", &JSCanvasPath::JsPath2DArcTo);
59 JSClass<JSPath2D>::CustomMethod("quadraticCurveTo", &JSCanvasPath::JsPath2DQuadraticCurveTo);
60 JSClass<JSPath2D>::CustomMethod("bezierCurveTo", &JSCanvasPath::JsPath2DBezierCurveTo);
61 JSClass<JSPath2D>::CustomMethod("ellipse", &JSCanvasPath::JsPath2DEllipse);
62 JSClass<JSPath2D>::CustomMethod("rect", &JSCanvasPath::JsPath2DRect);
63 JSClass<JSPath2D>::CustomMethod("closePath", &JSCanvasPath::JsPath2DClosePath);
64 JSClass<JSPath2D>::Bind(globalObj, JSPath2D::Constructor, JSPath2D::Destructor);
65 }
66
67 // addPath(path: path2D, transform?:Matrix2D): void
JsPath2DAddPath(const JSCallbackInfo & args)68 void JSPath2D::JsPath2DAddPath(const JSCallbackInfo& args)
69 {
70 // one parameter
71 auto* jsPath2d = args.UnwrapArg<JSPath2D>(0);
72 CHECK_NULL_VOID(jsPath2d);
73 auto canvasPath2D = jsPath2d->GetCanvasPath2d();
74 path2d_->AddPath(canvasPath2D);
75 SetPathSize(args);
76
77 // two parameters
78 if (Container::GreatOrEqualAPITargetVersion(PlatformVersion::VERSION_TEN)) {
79 auto* jsMatrix2d = args.UnwrapArg<JSMatrix2d>(1);
80 if (jsMatrix2d) {
81 path2d_->SetTransform(jsMatrix2d->JsGetScaleX(), jsMatrix2d->JsGetRotateX(), jsMatrix2d->JsGetRotateY(),
82 jsMatrix2d->JsGetScaleY(), jsMatrix2d->JsGetTranslateX(), jsMatrix2d->JsGetTranslateY());
83 SetPathSize(args);
84 }
85 return;
86 }
87 if (args[1]->IsObject()) {
88 JSRef<JSObject> jsObj = JSRef<JSObject>::Cast(args[1]);
89 TransformParam param = JSMatrix2d::GetTransformInfo(jsObj);
90 double density = GetDensity();
91 param.translateX *= density;
92 param.translateY *= density;
93 path2d_->SetTransform(param.scaleX, param.skewX, param.skewY, param.scaleY, param.translateX, param.translateY);
94 SetPathSize(args);
95 }
96 }
97
98 } // namespace OHOS::Ace::Framework
99