• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_IMAGE_ACE_IMAGE_SOURCE_INFO_H
17 #define FOUNDATION_ACE_FRAMEWORKS_CORE_IMAGE_ACE_IMAGE_SOURCE_INFO_H
18 
19 #include <optional>
20 
21 #include "base/geometry/dimension.h"
22 #include "base/geometry/size.h"
23 #include "base/image/pixel_map.h"
24 #include "base/resource/internal_resource.h"
25 #include "core/components/common/layout/constants.h"
26 #include "core/components/common/properties/color.h"
27 
28 namespace OHOS::Ace {
29 class ImageSourceInfo {
30 public:
31     static bool IsSVGSource(const std::string& imageSrc, InternalResource::ResourceId resourceId);
32     static SrcType ResolveURIType(const std::string& uri);
33     static bool IsValidBase64Head(const std::string& uri, const std::string& pattern);
34     static bool IsUriOfDataAbilityEncoded(const std::string& uri, const std::string& pattern);
35 
36     explicit ImageSourceInfo(
37         const std::string& imageSrc,
38         Dimension width = Dimension(-1),
39         Dimension height = Dimension(-1),
40         InternalResource::ResourceId resourceId = InternalResource::ResourceId::NO_ID,
41         const RefPtr<PixelMap>& pixmap = nullptr);
42 
43     ImageSourceInfo() = default;
44     ~ImageSourceInfo() = default;
45 
46     bool operator==(const ImageSourceInfo& info) const
47     {
48         // only svg uses fillColor
49         if (isSvg_ && fillColor_ != info.fillColor_) {
50             return false;
51         }
52         return ((!pixmap_ && !info.pixmap_) || (pixmap_ && info.pixmap_ && pixmap_ == info.pixmap_)) &&
53         // TODO: Use GetModifyId to distinguish two PixelMap objects after Media provides it
54         src_ == info.src_ && resourceId_ == info.resourceId_;
55     }
56 
57     bool operator!=(const ImageSourceInfo& info) const
58     {
59         return !(operator==(info));
60     }
61 
62     void SetSrc(const std::string& src, std::optional<Color> fillColor = std::nullopt)
63     {
64         src_ = src;
65         resourceId_ = InternalResource::ResourceId::NO_ID;
66         isSvg_ = IsSVGSource(src_, resourceId_);
67         fillColor_ = fillColor;
68         pixmap_ = nullptr;
69     }
70 
GetSrc()71     const std::string& GetSrc() const
72     {
73         return src_;
74     }
75 
76     void SetResourceId(InternalResource::ResourceId id, std::optional<Color> fillColor = std::nullopt)
77     {
78         resourceId_ = id;
79         src_.clear();
80         isSvg_ = IsSVGSource(src_, resourceId_);
81         fillColor_ = fillColor;
82         pixmap_ = nullptr;
83     }
84 
GetResourceId()85     InternalResource::ResourceId GetResourceId() const
86     {
87         return resourceId_;
88     }
89 
IsInternalResource()90     bool IsInternalResource() const
91     {
92         return src_.empty() && resourceId_ != InternalResource::ResourceId::NO_ID && !pixmap_;
93     }
94 
IsValid()95     bool IsValid() const
96     {
97         return (src_.empty() && resourceId_ != InternalResource::ResourceId::NO_ID) ||
98                (!src_.empty() && resourceId_ == InternalResource::ResourceId::NO_ID) ||
99                pixmap_;
100     }
101 
IsSvg()102     bool IsSvg() const
103     {
104         return isSvg_;
105     }
106 
IsPixmap()107     bool IsPixmap() const
108     {
109         return pixmap_ != nullptr;
110     }
111 
GetSrcType()112     SrcType GetSrcType() const
113     {
114         return srcType_;
115     }
116 
ToString()117     std::string ToString() const
118     {
119         if (!src_.empty()) {
120             return src_ + std::string("w") + std::to_string(sourceWidth_.Value()) +
121                 std::string("h") + std::to_string(sourceHeight_.Value());
122         } else if (resourceId_ != InternalResource::ResourceId::NO_ID) {
123             return std::string("internal resource id: ") + std::to_string(static_cast<int32_t>(resourceId_));
124         } else if (pixmap_) {
125             return std::string("pixmapID: ") + pixmap_->GetId() +
126                 std::string(" -> modifyID: ") + pixmap_->GetModifyId();
127         } else {
128             return std::string("empty source");
129         }
130     }
131 
SetDimension(Dimension width,Dimension Height)132     void SetDimension(Dimension width, Dimension Height)
133     {
134         sourceWidth_ = width;
135         sourceHeight_ = Height;
136     }
137 
IsSourceDimensionValid()138     bool IsSourceDimensionValid() const
139     {
140         return sourceWidth_.IsValid() && sourceHeight_.IsValid();
141     }
142 
GetSourceSize()143     Size GetSourceSize() const
144     {
145         return Size(sourceWidth_.Value(), sourceHeight_.Value());
146     }
147 
Reset()148     void Reset()
149     {
150         src_.clear();
151         sourceWidth_ = Dimension(-1);
152         sourceHeight_ = Dimension(-1);
153         resourceId_ = InternalResource::ResourceId::NO_ID;
154         isSvg_ = false;
155         fillColor_.reset();
156         pixmap_ = nullptr;
157         cacheKey_.clear();
158     }
159 
160     void SetFillColor(const Color& color);
161 
GetFillColor()162     std::optional<Color> GetFillColor() const
163     {
164         return fillColor_;
165     }
166 
GetPixmap()167     const RefPtr<PixelMap>& GetPixmap() const
168     {
169         return pixmap_;
170     }
171 
GetCacheKey()172     const std::string& GetCacheKey() const
173     {
174         return cacheKey_;
175     }
176 
177 private:
178     SrcType ResolveSrcType() const;
179 
180     std::string src_;
181     std::string cacheKey_;
182     Dimension sourceWidth_ = Dimension(-1);
183     Dimension sourceHeight_ = Dimension(-1);
184     InternalResource::ResourceId resourceId_ = InternalResource::ResourceId::NO_ID;
185     RefPtr<PixelMap> pixmap_;
186     bool isSvg_ = false;
187 
188     // only Svg will set it.
189     std::optional<Color> fillColor_;
190 
191     // image source type for example:FILE, ASSET, NETWORK, MEMORY, BASE64, INTERNAL, RESOURCE or DATA_ABILITY,
192     SrcType srcType_ = SrcType::UNSUPPORTED;
193 };
194 
195 } // namespace OHOS::Ace
196 
197 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_IMAGE_ACE_IMAGE_SOURCE_INFO_H