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