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