• 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 Reset();
72 
73     // interfaces to get infomation from [ImageSourceInfo]
74     bool IsInternalResource() const;
75     bool IsValid() const;
76     bool IsPng() const;
77     bool IsSvg() const;
78     bool IsPixmap() const;
79     bool IsSourceDimensionValid() const;
80     const std::string& GetBundleName() const;
81     const std::string& GetModuleName() const;
82     std::string ToString() const;
83     InternalResource::ResourceId GetResourceId() const;
84     SrcType GetSrcType() const;
85     Size GetSourceSize() const;
86     const std::string& GetSrc() const;
87     const std::optional<Color>& GetFillColor() const;
88     const RefPtr<PixelMap>& GetPixmap() const;
89     std::string GetKey() const;
90 
91     bool SupportObjCache() const;
SetNeedCache(bool needCache)92     void SetNeedCache(bool needCache)
93     {
94         needCache_ = needCache;
95     }
96 
97 private:
98     SrcType ResolveSrcType() const;
99     void GenerateCacheKey();
100 
101     std::string src_;
102     std::string cacheKey_;
103     // Interim programme
104     std::string bundleName_;
105     std::string moduleName_;
106     Dimension sourceWidth_ = Dimension(-1);
107     Dimension sourceHeight_ = Dimension(-1);
108     InternalResource::ResourceId resourceId_ = InternalResource::ResourceId::NO_ID;
109     RefPtr<PixelMap> pixmap_;
110     bool isSvg_ = false;
111     bool isPng_ = false;
112     bool needCache_ = true;
113     [[deprecated("in NG")]]
114     std::optional<Color> fillColor_;
115 
116     // image source type for example:FILE, ASSET, NETWORK, MEMORY, BASE64, INTERNAL, RESOURCE or DATA_ABILITY,
117     SrcType srcType_ = SrcType::UNSUPPORTED;
118 };
119 
120 } // namespace OHOS::Ace
121 
122 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_IMAGE_ACE_IMAGE_SOURCE_INFO_H
123