• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "bridge/cj_frontend/cppview/canvas_path.h"
17 
18 #include "core/pipeline/pipeline_base.h"
19 namespace OHOS::Ace::Framework {
20 
NativeCanvasPath()21 NativeCanvasPath::NativeCanvasPath() : FFIData()
22 {
23     path2d_ = AceType::MakeRefPtr<CanvasPath2D>();
24 }
25 
NativeCanvasPath(const std::string & capStr)26 NativeCanvasPath::NativeCanvasPath(const std::string& capStr) : FFIData()
27 {
28     path2d_ = AceType::MakeRefPtr<CanvasPath2D>(capStr);
29 }
30 
~NativeCanvasPath()31 NativeCanvasPath::~NativeCanvasPath()
32 {
33     LOGI("Native CanvasPath Destroyed: %{public}" PRId64, GetID());
34 }
35 
AddPath(const sptr<NativeCanvasPath> & path)36 void NativeCanvasPath::AddPath(const sptr<NativeCanvasPath>& path)
37 {
38     auto toBeAdd = path->GetCanvasPath2d();
39     path2d_->AddPath(toBeAdd);
40 }
41 
AddPathWithMatrix(const sptr<NativeCanvasPath> & path,const sptr<NativeMatrix2d> & matrix2d)42 void NativeCanvasPath::AddPathWithMatrix(const sptr<NativeCanvasPath>& path, const sptr<NativeMatrix2d>& matrix2d)
43 {
44     auto toBeAdd = path->GetCanvasPath2d();
45     path2d_->AddPath(toBeAdd);
46 
47     path2d_->SetTransform(matrix2d->GetScaleX(), matrix2d->GetRotateX(), matrix2d->GetRotateY(), matrix2d->GetScaleY(),
48         matrix2d->GetTranslateX(), matrix2d->GetTranslateY());
49 }
50 
SetTransform(double scaleX,double skewX,double skewY,double scaleY,double translateX,double translateY)51 void NativeCanvasPath::SetTransform(
52     double scaleX, double skewX, double skewY, double scaleY, double translateX, double translateY)
53 {
54     double density = GetDensity();
55     path2d_->SetTransform(scaleX, skewX, skewY, scaleY, translateX * density, translateY * density);
56 }
57 
MoveTo(double x,double y)58 void NativeCanvasPath::MoveTo(double x, double y)
59 {
60     double density = GetDensity();
61     path2d_->MoveTo(x * density, y * density);
62 }
63 
LineTo(double x,double y)64 void NativeCanvasPath::LineTo(double x, double y)
65 {
66     double density = GetDensity();
67     path2d_->LineTo(x * density, y * density);
68 }
69 
Arc(double x,double y,double radius,double startAngle,double endAngle,bool anticlockwise)70 void NativeCanvasPath::Arc(double x, double y, double radius, double startAngle, double endAngle, bool anticlockwise)
71 {
72     double density = GetDensity();
73     path2d_->Arc(x * density, y * density, radius * density, startAngle, endAngle, anticlockwise);
74 }
75 
ArcTo(double x1,double y1,double x2,double y2,double radius)76 void NativeCanvasPath::ArcTo(double x1, double y1, double x2, double y2, double radius)
77 {
78     double density = GetDensity();
79     path2d_->ArcTo(x1 * density, y1 * density, x2 * density, y2 * density, radius * density);
80 }
81 
QuadraticCurveTo(double cpx,double cpy,double x,double y)82 void NativeCanvasPath::QuadraticCurveTo(double cpx, double cpy, double x, double y)
83 {
84     double density = GetDensity();
85     path2d_->QuadraticCurveTo(cpx * density, cpy * density, x * density, y * density);
86 }
87 
BezierCurveTo(double cp1x,double cp1y,double cp2x,double cp2y,double x,double y)88 void NativeCanvasPath::BezierCurveTo(double cp1x, double cp1y, double cp2x, double cp2y, double x, double y)
89 {
90     double density = GetDensity();
91     path2d_->BezierCurveTo(cp1x * density, cp1y * density, cp2x * density, cp2y * density, x * density, y * density);
92 }
93 
Ellipse(double x,double y,double radiusX,double radiusY,double rotation,double startAngle,double endAngle,bool anticlockwise)94 void NativeCanvasPath::Ellipse(double x, double y, double radiusX, double radiusY, double rotation, double startAngle,
95     double endAngle, bool anticlockwise)
96 {
97     double density = GetDensity();
98     path2d_->Ellipse(
99         x * density, y * density, radiusX * density, radiusY * density, rotation, startAngle, endAngle, anticlockwise);
100 }
101 
Rect(double x,double y,double width,double height)102 void NativeCanvasPath::Rect(double x, double y, double width, double height)
103 {
104     double density = GetDensity();
105     path2d_->Rect(x * density, y * density, width * density, height * density);
106 }
107 
ClosePath()108 void NativeCanvasPath::ClosePath()
109 {
110     path2d_->ClosePath();
111 }
112 
GetDensity()113 double NativeCanvasPath::GetDensity()
114 {
115     double density = PipelineBase::GetCurrentDensity();
116     return ((GetUnit() == CanvasUnit::DEFAULT) && !NearZero(density)) ? density : 1.0;
117 }
118 } // namespace OHOS::Ace::Framework