1 /*
2  * Copyright (c) 2021-2022 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 "render/rs_path.h"
17 
18 #include "draw/path.h"
19 #include "utils/matrix.h"
20 #include "utils/scalar.h"
21 
22 #include "platform/common/rs_log.h"
23 
24 namespace OHOS {
25 namespace Rosen {
CreateRSPath()26 std::shared_ptr<RSPath> RSPath::CreateRSPath()
27 {
28     return std::make_shared<RSPath>();
29 }
30 
CreateRSPath(const Drawing::Path & path)31 std::shared_ptr<RSPath> RSPath::CreateRSPath(const Drawing::Path& path)
32 {
33     auto rsPath = std::make_shared<RSPath>();
34     rsPath->SetDrawingPath(path);
35     return rsPath;
36 }
37 
CreateRSPath(const std::string & path)38 std::shared_ptr<RSPath> RSPath::CreateRSPath(const std::string& path)
39 {
40     Drawing::Path drAnimationPath;
41     drAnimationPath.BuildFromSVGString(path);
42     return RSPath::CreateRSPath(drAnimationPath);
43 }
44 
RSPath()45 RSPath::RSPath()
46 {
47     drPath_ = new Drawing::Path();
48 }
49 
~RSPath()50 RSPath::~RSPath()
51 {
52     if (drPath_) {
53         delete drPath_;
54     }
55 }
56 
GetDrawingPath() const57 const Drawing::Path& RSPath::GetDrawingPath() const
58 {
59     return *drPath_;
60 }
61 
SetDrawingPath(const Drawing::Path & path)62 void RSPath::SetDrawingPath(const Drawing::Path& path)
63 {
64     if (drPath_) {
65         delete drPath_;
66     }
67     drPath_ = new Drawing::Path(path);
68 }
69 
Reverse()70 std::shared_ptr<RSPath> RSPath::Reverse()
71 {
72     Drawing::Path path;
73     path.ReverseAddPath(*drPath_);
74     return CreateRSPath(path);
75 }
76 
GetDistance() const77 float RSPath::GetDistance() const
78 {
79     return drPath_->GetLength(false);
80 }
81 
82 template<>
GetPosTan(float distance,Vector2f & pos,float & degrees) const83 bool RSPath::GetPosTan(float distance, Vector2f& pos, float& degrees) const
84 {
85     Drawing::Point position;
86     Drawing::Point tangent;
87     bool ret = drPath_->GetPositionAndTangent(distance, position, tangent, false);
88     if (!ret) {
89         ROSEN_LOGE("PathMeasure get failed");
90         return false;
91     }
92     pos.data_[0] = position.GetX();
93     pos.data_[1] = position.GetY();
94     degrees = Drawing::ConvertRadiansToDegrees(std::atan2(tangent.GetY(), tangent.GetX()));
95     return true;
96 }
97 
98 template<>
GetPosTan(float distance,Vector4f & pos,float & degrees) const99 bool RSPath::GetPosTan(float distance, Vector4f& pos, float& degrees) const
100 {
101     Vector2f position;
102     bool res = GetPosTan(distance, position, degrees);
103     if (!res) {
104         ROSEN_LOGD("PathMeasure get failed");
105         return false;
106     }
107     pos[0] = position[0];
108     pos[1] = position[1];
109     return res;
110 }
111 } // namespace Rosen
112 } // namespace OHOS
113