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 "include/core/SkPath.h"
19 #include "include/core/SkMatrix.h"
20 #include "include/core/SkPathMeasure.h"
21 #include "include/utils/SkParsePath.h"
22
23 #include "platform/common/rs_log.h"
24
25 namespace OHOS {
26 namespace Rosen {
CreateRSPath()27 std::shared_ptr<RSPath> RSPath::CreateRSPath()
28 {
29 return std::make_shared<RSPath>();
30 }
31
CreateRSPath(const SkPath & skPath)32 std::shared_ptr<RSPath> RSPath::CreateRSPath(const SkPath& skPath)
33 {
34 auto rsPath = std::make_shared<RSPath>();
35 rsPath->SetSkiaPath(skPath);
36 return rsPath;
37 }
38
CreateRSPath(const std::string & path)39 std::shared_ptr<RSPath> RSPath::CreateRSPath(const std::string& path)
40 {
41 SkPath skAnimationPath;
42 SkParsePath::FromSVGString(path.c_str(), &skAnimationPath);
43 return RSPath::CreateRSPath(skAnimationPath);
44 }
45
RSPath()46 RSPath::RSPath()
47 {
48 skPath_ = new SkPath();
49 }
50
~RSPath()51 RSPath::~RSPath()
52 {
53 if (skPath_) {
54 delete skPath_;
55 }
56 }
57
GetSkiaPath() const58 const SkPath& RSPath::GetSkiaPath() const
59 {
60 return *skPath_;
61 }
62
SetSkiaPath(const SkPath & skPath)63 void RSPath::SetSkiaPath(const SkPath& skPath)
64 {
65 if (skPath_) {
66 delete skPath_;
67 }
68 skPath_ = new SkPath(skPath);
69 }
70
Reverse()71 std::shared_ptr<RSPath> RSPath::Reverse()
72 {
73 SkPath path;
74 path.reverseAddPath(*skPath_);
75 return CreateRSPath(path);
76 }
77
GetDistance() const78 float RSPath::GetDistance() const
79 {
80 SkPathMeasure pathMeasure(*skPath_, false);
81 return pathMeasure.getLength();
82 }
83
84 template<>
GetPosTan(float distance,Vector2f & pos,float & degrees) const85 bool RSPath::GetPosTan(float distance, Vector2f& pos, float& degrees) const
86 {
87 SkPoint position;
88 SkVector tangent;
89 SkPathMeasure pathMeasure(*skPath_, false);
90 bool ret = pathMeasure.getPosTan(distance, &position, &tangent);
91 if (!ret) {
92 ROSEN_LOGE("SkPathMeasure get failed");
93 return false;
94 }
95 pos.data_[0] = position.x();
96 pos.data_[1] = position.y();
97 degrees = SkRadiansToDegrees(std::atan2(tangent.y(), tangent.x()));
98 return true;
99 }
100
101 template<>
GetPosTan(float distance,Vector4f & pos,float & degrees) const102 bool RSPath::GetPosTan(float distance, Vector4f& pos, float& degrees) const
103 {
104 Vector2f position;
105 bool res = GetPosTan(distance, position, degrees);
106 if (!res) {
107 ROSEN_LOGD("SkPathMeasure get failed");
108 return false;
109 }
110 pos[0] = position[0];
111 pos[1] = position[1];
112 return res;
113 }
114 } // namespace Rosen
115 } // namespace OHOS
116