• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 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 "frameworks/core/components_ng/svg/parse/svg_path.h"
17 
18 #include "frameworks/core/components_ng/svg/parse/svg_constants.h"
19 
20 
21 namespace OHOS::Ace::NG {
22 
SvgPath()23 SvgPath::SvgPath() : SvgGraphic() {}
24 
Create()25 RefPtr<SvgNode> SvgPath::Create()
26 {
27     return AceType::MakeRefPtr<SvgPath>();
28 }
29 
ParseAndSetSpecializedAttr(const std::string & name,const std::string & value)30 bool SvgPath::ParseAndSetSpecializedAttr(const std::string& name, const std::string& value)
31 {
32     if (name == SVG_D) {
33         d_ = value;
34         return true;
35     }
36     return false;
37 }
38 
GetobjectBoundingBox(const SvgLengthScaleRule & lengthRule)39 Rect SvgPath::GetobjectBoundingBox(const SvgLengthScaleRule& lengthRule)
40 {
41     if (lengthRule.GetLengthScaleUnit() == SvgLengthScaleUnit::OBJECT_BOUNDING_BOX) {
42         LOGD("SvgPath::GetobjectBoundingBox : objectBoundingBox");
43         return lengthRule.GetContainerRect();
44     }
45     LOGD("SvgPath::GetobjectBoundingBox : userSpaceOnUse");
46     Rect objectBoundingBox(0, 0, 1, 1);
47     return objectBoundingBox;
48 }
49 
AsPath(const SvgLengthScaleRule & lengthRule)50 RSRecordingPath SvgPath::AsPath(const SvgLengthScaleRule& lengthRule)
51 {
52     /* re-generate the Path for pathTransform(true). AsPath come from clip-path */
53     if (path_.has_value() && !lengthRule.GetPathTransform()) {
54         return path_.value();
55     }
56     RSRecordingPath tmp;
57     RSRecordingPath out;
58     Rect objectBoundingBox = GetobjectBoundingBox(lengthRule);
59     RSMatrix matrix;
60     /* Setup matrix  for converting the points in path */
61     matrix.SetScaleTranslate(objectBoundingBox.Width(), objectBoundingBox.Height(), objectBoundingBox.Left(),
62         objectBoundingBox.Top());
63 
64     if (!d_.empty()) {
65         tmp.BuildFromSVGString(d_);
66         /* convert the points in Path with the matrixs */
67         tmp.TransformWithPerspectiveClip(matrix, &out, false);
68         if (attributes_.fillState.IsEvenodd()) {
69             out.SetFillStyle(RSPathFillType::EVENTODD);
70         }
71     }
72     /* Apply path transform for clip-path only */
73     if (lengthRule.GetPathTransform()) {
74         ApplyTransform(out);
75     }
76     return out;
77 }
78 
AsPath(const Size &) const79 RSRecordingPath SvgPath::AsPath(const Size& /* viewPort */) const
80 {
81     RSRecordingPath out;
82     if (!d_.empty()) {
83         out.BuildFromSVGString(d_);
84         if (attributes_.fillState.IsEvenodd()) {
85             out.SetFillStyle(RSPathFillType::EVENTODD);
86         }
87     }
88     return out;
89 }
90 
91 } // namespace OHOS::Ace::NG
92