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 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 explicit ImageSourceInfo(std::string imageSrc, Dimension width = Dimension(-1), 32 Dimension height = Dimension(-1), InternalResource::ResourceId resourceId = InternalResource::ResourceId::NO_ID, 33 const RefPtr<PixelMap>& pixmap = nullptr); ImageSourceInfo(const RefPtr<PixelMap> & pixmap)34 explicit ImageSourceInfo(const RefPtr<PixelMap>& pixmap) 35 : ImageSourceInfo("", Dimension(-1), Dimension(-1), InternalResource::ResourceId::NO_ID, pixmap) 36 {} 37 ImageSourceInfo() = default; 38 ~ImageSourceInfo() = default; 39 40 // static functions 41 static bool IsSVGSource(const std::string& imageSrc, InternalResource::ResourceId resourceId); 42 static bool IsPngSource(const std::string& src, InternalResource::ResourceId resourceId); 43 static SrcType ResolveURIType(const std::string& uri); 44 static bool IsValidBase64Head(const std::string& uri, const std::string& pattern); 45 static bool IsUriOfDataAbilityEncoded(const std::string& uri, const std::string& pattern); 46 47 // operators 48 bool operator==(const ImageSourceInfo& info) const; 49 bool operator!=(const ImageSourceInfo& info) const; 50 51 // interfaces to change [ImageSourceInfo] 52 void SetSrc(const std::string& src, std::optional<Color> fillColor = std::nullopt); 53 void SetResourceId(InternalResource::ResourceId id, std::optional<Color> fillColor = std::nullopt); 54 void SetPixMap(const RefPtr<PixelMap>& pixmap, std::optional<Color> fillColor = std::nullopt); 55 void SetDimension(Dimension width, Dimension Height); 56 void SetFillColor(const Color& color); 57 void Reset(); 58 59 // interfaces to get infomation from [ImageSourceInfo] 60 bool IsInternalResource() const; 61 bool IsValid() const; 62 bool IsPng() const; 63 bool IsSvg() const; 64 bool IsPixmap() const; 65 bool IsSourceDimensionValid() const; 66 std::string ToString() const; 67 InternalResource::ResourceId GetResourceId() const; 68 SrcType GetSrcType() const; 69 Size GetSourceSize() const; 70 const std::string& GetSrc() const; 71 const std::optional<Color>& GetFillColor() const; 72 const RefPtr<PixelMap>& GetPixmap() const; 73 std::string GetKey() const; 74 75 bool IsSupportCache() const; 76 77 private: 78 SrcType ResolveSrcType() const; 79 void GenerateCacheKey(); 80 81 std::string src_; 82 std::string cacheKey_; 83 Dimension sourceWidth_ = Dimension(-1); 84 Dimension sourceHeight_ = Dimension(-1); 85 InternalResource::ResourceId resourceId_ = InternalResource::ResourceId::NO_ID; 86 RefPtr<PixelMap> pixmap_; 87 bool isSvg_ = false; 88 bool isPng_ = false; 89 90 // only Svg will set it. 91 std::optional<Color> fillColor_; 92 93 // image source type for example:FILE, ASSET, NETWORK, MEMORY, BASE64, INTERNAL, RESOURCE or DATA_ABILITY, 94 SrcType srcType_ = SrcType::UNSUPPORTED; 95 }; 96 97 } // namespace OHOS::Ace 98 99 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_IMAGE_ACE_IMAGE_SOURCE_INFO_H 100