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