• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2023 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_canvas_pattern.h"
17 
18 #include "bridge/declarative_frontend/jsview/canvas/js_matrix2d.h"
19 #include "bridge/declarative_frontend/jsview/js_view_common_def.h"
20 #include "core/components_ng/render/adapter/matrix2d.h"
21 
22 namespace OHOS::Ace::Framework {
23 constexpr int SET_TRANSFORM_PARAMETER_SIZE = 1;
Constructor(const JSCallbackInfo & info)24 void JSCanvasPattern::Constructor(const JSCallbackInfo& info)
25 {
26     auto canvasPattern = Referenced::MakeRefPtr<JSCanvasPattern>();
27     canvasPattern->IncRefCount();
28     info.SetReturnValue(Referenced::RawPtr(canvasPattern));
29 }
30 
Destructor(JSCanvasPattern * canvasPattern)31 void JSCanvasPattern::Destructor(JSCanvasPattern* canvasPattern)
32 {
33     if (canvasPattern != nullptr) {
34         canvasPattern->DecRefCount();
35     }
36 }
37 
JSBind(BindingTarget globalObj)38 void JSCanvasPattern::JSBind(BindingTarget globalObj)
39 {
40     JSClass<JSCanvasPattern>::Declare("CanvasPattern");
41     JSClass<JSCanvasPattern>::CustomMethod("setTransform", &JSCanvasPattern::JSSetTransform);
42     JSClass<JSCanvasPattern>::Bind(globalObj, JSCanvasPattern::Constructor, JSCanvasPattern::Destructor);
43 }
44 
JSSetTransform(const JSCallbackInfo & info)45 void JSCanvasPattern::JSSetTransform(const JSCallbackInfo& info)
46 {
47     if (info.Length() != SET_TRANSFORM_PARAMETER_SIZE || !info[0]->IsObject()) {
48         return;
49     }
50     CHECK_NULL_VOID(pattern_);
51     TransformParam transform;
52     if (Container::GreatOrEqualAPITargetVersion(PlatformVersion::VERSION_TEN)) {
53         auto* matrix = JSRef<JSObject>::Cast(info[0])->Unwrap<JSMatrix2d>();
54         CHECK_NULL_VOID(matrix);
55         transform = matrix->GetTransform();
56     } else {
57         JSRef<JSObject> jsObj = JSRef<JSObject>::Cast(info[0]);
58         transform = JSMatrix2d::GetTransformInfo(jsObj);
59     }
60     pattern_->SetScaleX(transform.scaleX);
61     pattern_->SetScaleY(transform.scaleY);
62     pattern_->SetSkewX(transform.skewX);
63     pattern_->SetSkewY(transform.skewY);
64     pattern_->SetTranslateX(transform.translateX);
65     pattern_->SetTranslateY(transform.translateY);
66 }
67 } // namespace OHOS::Ace::Framework
68 
69