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