• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021-2023 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 #include <utility>
21 
22 #include "base/geometry/dimension.h"
23 #include "base/geometry/size.h"
24 #include "base/image/pixel_map.h"
25 #include "base/resource/internal_resource.h"
26 #include "core/components/common/layout/constants.h"
27 #include "core/components/common/properties/color.h"
28 
29 namespace OHOS::Ace {
30 class ImageSourceInfo {
31 public:
32     // add constructor method for decompressed hap
33     ImageSourceInfo(std::string imageSrc, std::string bundleName, std::string moduleName,
34         Dimension width = Dimension(-1), Dimension height = Dimension(-1),
35         InternalResource::ResourceId resourceId = InternalResource::ResourceId::NO_ID,
36         const RefPtr<PixelMap>& pixmap = nullptr);
37 
38     explicit ImageSourceInfo(std::string imageSrc, Dimension width = Dimension(-1), Dimension height = Dimension(-1),
39         InternalResource::ResourceId resourceId = InternalResource::ResourceId::NO_ID,
40         const RefPtr<PixelMap>& pixmap = nullptr)
41         : ImageSourceInfo(std::move(imageSrc), "", "", width, height, resourceId, pixmap)
42     {}
43 
ImageSourceInfo(const RefPtr<PixelMap> & pixmap)44     explicit ImageSourceInfo(const RefPtr<PixelMap>& pixmap)
45         : ImageSourceInfo("", Dimension(-1), Dimension(-1), InternalResource::ResourceId::NO_ID, pixmap)
46     {}
47     ImageSourceInfo() = default;
48     ~ImageSourceInfo() = default;
49 
50     // static functions
51     static bool IsSVGSource(const std::string& imageSrc, InternalResource::ResourceId resourceId);
52     static bool IsPngSource(const std::string& src, InternalResource::ResourceId resourceId);
53     static SrcType ResolveURIType(const std::string& uri);
54     static bool IsValidBase64Head(const std::string& uri, const std::string& pattern);
55     static bool IsUriOfDataAbilityEncoded(const std::string& uri, const std::string& pattern);
56 
57     // operators
58     bool operator==(const ImageSourceInfo& info) const;
59     bool operator!=(const ImageSourceInfo& info) const;
60 
61     // interfaces to change [ImageSourceInfo]
62     void SetSrc(const std::string& src, std::optional<Color> fillColor = std::nullopt);
63     void SetResourceId(InternalResource::ResourceId id, std::optional<Color> fillColor = std::nullopt);
64     void SetPixMap(const RefPtr<PixelMap>& pixmap, std::optional<Color> fillColor = std::nullopt);
65     void SetDimension(Dimension width, Dimension Height);
66 
67     [[deprecated("use ImageRenderProperty::SetFillColor or SvgCanvasImage::SetFillColor")]]
68     void SetFillColor(const Color& color);
69     void SetBundleName(const std::string& bundleName);
70     void SetModuleName(const std::string& moduleName);
71     void SetIsUriPureNumber(bool isUriPureNumber = false)
72     {
73         isUriPureNumber_ = isUriPureNumber;
74     }
SetIsConfigurationChange(bool isConfigurationChange)75     void SetIsConfigurationChange(bool isConfigurationChange)
76     {
77         isConfigurationChange_ = isConfigurationChange;
78     }
79     void Reset();
80 
81     // interfaces to get infomation from [ImageSourceInfo]
82     bool IsInternalResource() const;
83     bool IsValid() const;
84     bool IsPng() const;
85     bool IsSvg() const;
86     bool IsPixmap() const;
87     bool IsSourceDimensionValid() const;
88     const std::string& GetBundleName() const;
89     const std::string& GetModuleName() const;
90     std::string ToString() const;
91     InternalResource::ResourceId GetResourceId() const;
92     SrcType GetSrcType() const;
93     Size GetSourceSize() const;
94     const std::string& GetSrc() const;
95     const std::optional<Color>& GetFillColor() const;
96     const RefPtr<PixelMap>& GetPixmap() const;
97     std::string GetKey() const;
GetIsUriPureNumber()98     bool GetIsUriPureNumber() const
99     {
100         return isUriPureNumber_;
101     }
GetIsConfigurationChange()102     bool GetIsConfigurationChange() const
103     {
104         return isConfigurationChange_;
105     }
106 
107     bool SupportObjCache() const;
SetNeedCache(bool needCache)108     void SetNeedCache(bool needCache)
109     {
110         needCache_ = needCache;
111     }
112 
113 private:
114     SrcType ResolveSrcType() const;
115     void GenerateCacheKey();
116 
117     std::string src_;
118     std::string cacheKey_;
119     // Interim programme
120     std::string bundleName_;
121     std::string moduleName_;
122     Dimension sourceWidth_ = Dimension(-1);
123     Dimension sourceHeight_ = Dimension(-1);
124     InternalResource::ResourceId resourceId_ = InternalResource::ResourceId::NO_ID;
125     RefPtr<PixelMap> pixmap_;
126     bool isSvg_ = false;
127     bool isPng_ = false;
128     bool needCache_ = true;
129     bool isConfigurationChange_ = false;
130     bool isUriPureNumber_ = false;
131     [[deprecated("in NG")]]
132     std::optional<Color> fillColor_;
133 
134     // image source type for example:FILE, ASSET, NETWORK, MEMORY, BASE64, INTERNAL, RESOURCE or DATA_ABILITY,
135     SrcType srcType_ = SrcType::UNSUPPORTED;
136 };
137 
138 } // namespace OHOS::Ace
139 
140 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_IMAGE_ACE_IMAGE_SOURCE_INFO_H
141