1 /*
2  * Copyright (c) 2021 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 #ifndef FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_BOX_MASK_H
17 #define FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_BOX_MASK_H
18 
19 #include <string>
20 
21 #include "base/geometry/rect.h"
22 #include "base/memory/ace_type.h"
23 #include "core/components/common/properties/clip_path.h"
24 #include "core/components/common/properties/decoration.h"
25 
26 namespace OHOS::Ace {
27 
28 enum class MaskImageType {
29     NONE = 0,
30     SVG,
31     COLOR,
32     PATH,
33 };
34 
35 class ACE_EXPORT Mask : public AceType {
36     DECLARE_ACE_TYPE(Mask, AceType);
37 
38 public:
39     Mask() = default;
40     ~Mask() = default;
41 
42     static RefPtr<Mask> Create();
43 
44     virtual void LoadMask(const WeakPtr<PipelineContext>& context, const RefPtr<RenderNode>& parent) = 0;
45 
SetMaskImage(const std::string & maskImage)46     void SetMaskImage(const std::string& maskImage)
47     {
48         maskImageType_ = MaskImageType::NONE;
49         maskImage_ = maskImage;
50 
51         if (maskImage.size() > 4) {
52             std::string::size_type start = maskImage.find("url(");
53             if (start != std::string::npos) {
54                 start += std::strlen("url(");
55                 std::string::size_type end = maskImage.find_first_of(')', start);
56                 if (end == std::string::npos) {
57                     return;
58                 }
59 
60                 file_ = maskImage.substr(start, end - start);
61                 if (file_.size() > 4 && file_.substr(file_.size() - 4) == ".svg") {
62                     maskImageType_ = MaskImageType::SVG;
63                 }
64                 return;
65             } else if (maskImage.substr(maskImage.size() - 4) == ".svg") {
66                 maskImageType_ = MaskImageType::SVG;
67                 file_ = maskImage;
68                 return;
69             }
70 
71             if (maskImage.find("Gradient") != std::string::npos) {
72                 maskImageType_ = MaskImageType::COLOR;
73             }
74         }
75     }
76 
SetMask(const RefPtr<MaskPath> & maskPath)77     void SetMask(const RefPtr<MaskPath>& maskPath)
78     {
79         maskImageType_ = MaskImageType::PATH;
80         maskPath_ = maskPath;
81     }
82 
GetMaskPath()83     RefPtr<MaskPath> GetMaskPath() const
84     {
85         return maskPath_;
86     }
87 
SetMaskPosition(const BackgroundImagePosition & position)88     void SetMaskPosition(const BackgroundImagePosition& position)
89     {
90         position_ = position;
91     }
92 
SetMaskSize(const BackgroundImageSize & size)93     void SetMaskSize(const BackgroundImageSize& size)
94     {
95         size_ = size;
96     }
97 
GetMaskPosition()98     const BackgroundImagePosition& GetMaskPosition() const
99     {
100         return position_;
101     }
102 
GetMaskSize()103     const BackgroundImageSize& GetMaskSize() const
104     {
105         return size_;
106     }
107 
IsSvgImage()108     bool IsSvgImage() const
109     {
110         return maskImageType_ == MaskImageType::SVG;
111     }
112 
IsLastSvgImage()113     bool IsLastSvgImage() const
114     {
115         return lastMaskImageType_ == MaskImageType::SVG;
116     }
117 
IsColorGradient()118     bool IsColorGradient() const
119     {
120         return maskImageType_ == MaskImageType::COLOR;
121     }
122 
IsLastColorGradient()123     bool IsLastColorGradient() const
124     {
125         return lastMaskImageType_ == MaskImageType::COLOR;
126     }
127 
IsPath()128     bool IsPath() const
129     {
130         return maskImageType_ == MaskImageType::PATH;
131     }
132 
IsValid()133     bool IsValid() const
134     {
135         return IsSvgImage() || IsColorGradient() || IsPath();
136     }
137 
138 protected:
139     std::string maskImage_;
140     std::string file_;
141     std::string lastFile_;
142     MaskImageType maskImageType_ = MaskImageType::NONE;
143     MaskImageType lastMaskImageType_ = MaskImageType::NONE;
144     BackgroundImagePosition position_;
145     BackgroundImageSize size_;
146     RefPtr<MaskPath> maskPath_;
147 };
148 
149 } // namespace OHOS::Ace
150 
151 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_BOX_MASK_H
152