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_path2d.h"
17 #include "bridge/declarative_frontend/jsview/js_canvas_renderer.h"
18 #include "bridge/declarative_frontend/jsview/js_view_common_def.h"
19
20 namespace OHOS::Ace::Framework {
21 constexpr int JS_PATH2D_PARAMETER_COUNTS = 2;
JSPath2D()22 JSPath2D::JSPath2D()
23 {
24 }
25
Constructor(const JSCallbackInfo & args)26 void JSPath2D::Constructor(const JSCallbackInfo& args)
27 {
28 auto jsPath2d = Referenced::MakeRefPtr<JSPath2D>();
29 jsPath2d->IncRefCount();
30 args.SetReturnValue(Referenced::RawPtr(jsPath2d));
31 jsPath2d->SetCanvasPath2d(JSCanvasRenderer::JsMakePath2D(args));
32 }
33
Destructor(JSPath2D * controller)34 void JSPath2D::Destructor(JSPath2D* controller)
35 {
36 if (controller != nullptr) {
37 controller->DecRefCount();
38 }
39 }
40
JSBind(BindingTarget globalObj)41 void JSPath2D::JSBind(BindingTarget globalObj)
42 {
43 JSClass<JSPath2D>::Declare("Path2D");
44 JSClass<JSPath2D>::CustomMethod("addPath", &JSPath2D::JsPath2DAddPath);
45 JSClass<JSPath2D>::CustomMethod("setTransform", &JSCanvasPath::JsPath2DSetTransform);
46 JSClass<JSPath2D>::CustomMethod("moveTo", &JSCanvasPath::JsPath2DMoveTo);
47 JSClass<JSPath2D>::CustomMethod("lineTo", &JSCanvasPath::JsPath2DLineTo);
48 JSClass<JSPath2D>::CustomMethod("arc", &JSCanvasPath::JsPath2DArc);
49 JSClass<JSPath2D>::CustomMethod("arcTo", &JSCanvasPath::JsPath2DArcTo);
50 JSClass<JSPath2D>::CustomMethod("quadraticCurveTo", &JSCanvasPath::JsPath2DQuadraticCurveTo);
51 JSClass<JSPath2D>::CustomMethod("bezierCurveTo", &JSCanvasPath::JsPath2DBezierCurveTo);
52 JSClass<JSPath2D>::CustomMethod("ellipse", &JSCanvasPath::JsPath2DEllipse);
53 JSClass<JSPath2D>::CustomMethod("rect", &JSCanvasPath::JsPath2DRect);
54 JSClass<JSPath2D>::CustomMethod("closePath", &JSCanvasPath::JsPath2DClosePath);
55 JSClass<JSPath2D>::Bind(globalObj, JSPath2D::Constructor, JSPath2D::Destructor);
56 }
57
JsPath2DAddPath(const JSCallbackInfo & args)58 void JSPath2D::JsPath2DAddPath(const JSCallbackInfo& args)
59 {
60 if (args.Length() < 1 || !args[0]->IsObject()) {
61 LOGE("The argument is wrong, it is supposed to have at least 1 arguments"
62 "and the first argument must be a object.");
63 return;
64 }
65 auto* jsPath2d = JSRef<JSObject>::Cast(args[0])->Unwrap<JSPath2D>();
66 if (jsPath2d == nullptr) {
67 return;
68 }
69 auto canvasPath2D = jsPath2d->GetCanvasPath2d();
70 path2d_->AddPath(canvasPath2D);
71 if (args.Length() == JS_PATH2D_PARAMETER_COUNTS) {
72 auto* jsMatrix2d = JSRef<JSObject>::Cast(args[1])->Unwrap<JSMatrix2d>();
73 if (jsMatrix2d != nullptr) {
74 path2d_->SetTransform(jsMatrix2d->JsGetScaleX(), jsMatrix2d->JsGetRotateX(), jsMatrix2d->JsGetRotateY(),
75 jsMatrix2d->JsGetScaleY(), jsMatrix2d->JsGetTranslateX(), jsMatrix2d->JsGetTranslateY());
76 }
77 }
78 }
79
80 } // namespace OHOS::Ace::Framework
81