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_COMPONENTS_IMAGE_IMAGE_COMPONENT_H 17 #define FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_IMAGE_IMAGE_COMPONENT_H 18 19 #include <string> 20 21 #include "base/image/pixel_map.h" 22 #include "base/resource/internal_resource.h" 23 #include "base/utils/macros.h" 24 #include "base/utils/utils.h" 25 #include "core/components/box/drag_drop_event.h" 26 #include "core/components/common/layout/constants.h" 27 #include "core/components/common/properties/alignment.h" 28 #include "core/components/common/properties/border.h" 29 #include "core/components/common/properties/color.h" 30 #include "core/pipeline/base/component_group.h" 31 #include "core/pipeline/base/element.h" 32 #include "core/pipeline/base/measurable.h" 33 #include "core/pipeline/base/render_component.h" 34 35 namespace OHOS::Ace { 36 constexpr int32_t COLOR_FILTER_MATRIX_SIZE = 20; 37 constexpr int32_t IMAGE_SOURCE_SIZE = 2; 38 // A component can show image. 39 class ACE_EXPORT ImageComponent : public RenderComponent, public Measurable { 40 DECLARE_ACE_TYPE(ImageComponent, RenderComponent, Measurable); 41 42 public: 43 explicit ImageComponent(const std::string& src = std::string()); ImageComponent(InternalResource::ResourceId resourceId)44 explicit ImageComponent(InternalResource::ResourceId resourceId) : resourceId_(resourceId) {} 45 ~ImageComponent() override = default; 46 47 RefPtr<RenderNode> CreateRenderNode() override; 48 RefPtr<Element> CreateElement() override; 49 void SetSrc(const std::string& src); 50 void SetAlt(const std::string& alt); 51 void SetAlignment(const Alignment& alignment); 52 void SetColor(const std::optional<Color>& color); 53 void SetLoadSuccessEvent(const EventMarker& loadSuccessEvent); 54 void SetLoadFailEvent(const EventMarker& loadFailEvent); 55 void SetSvgAnimatorFinishEvent(const EventMarker& svgAnimatorFinishEvent); 56 void SetResourceId(InternalResource::ResourceId resourceId); 57 void SetBorder(const Border& border); 58 void SetFitMaxSize(bool fitMaxSize); 59 void SetMatchTextDirection(bool matchTextDirection); 60 void SetImageFill(const std::optional<Color>& color); 61 void SetImageFit(ImageFit imageFit); 62 void SetImageInterpolation(ImageInterpolation imageInterpolation); 63 void SetImageRenderMode(ImageRenderMode imageRenderMode); 64 void SetImageRepeat(ImageRepeat imageRepeat); 65 void SetImageSourceSize(const std::pair<Dimension, Dimension>& sourceSize); 66 void SetUseSkiaSvg(bool useSkiaSvg); 67 void SetPixmap(const RefPtr<PixelMap>& pixmap); 68 void SetAutoResize(bool autoResize); 69 void SetSyncMode(bool syncMode); 70 bool GetSyncMode(); 71 72 const std::string& GetAlt() const; 73 const Alignment& GetAlignment() const; 74 const std::string& GetSrc() const; 75 const std::optional<Color>& GetColor() const; 76 const Border& GetBorder() const; 77 const EventMarker& GetLoadSuccessEvent() const; 78 const EventMarker& GetLoadFailEvent() const; 79 const EventMarker& GetSvgAnimatorFinishEvent() const; 80 InternalResource::ResourceId GetResourceId() const; 81 bool GetFitMaxSize() const; 82 bool IsMatchTextDirection() const; 83 bool IsSrcSvgImage() const; 84 ImageFit GetImageFit() const; 85 ImageInterpolation GetImageInterpolation() const; 86 ImageRenderMode GetImageRenderMode() const; 87 ImageRepeat GetImageRepeat() const; 88 const std::pair<Dimension, Dimension>& GetImageSourceSize() const; 89 bool GetUseSkiaSvg() const; 90 bool GetAutoResize() const; 91 static bool IsSvgSuffix(const std::string& src); 92 const RefPtr<PixelMap>& GetPixmap() const; 93 void SetHasObjectPosition(bool hasObjectPosition); 94 bool GetHasObjectPosition() const; 95 void SetImageObjectPosition(const ImageObjectPosition& imageObjectPosition); 96 const ImageObjectPosition& GetImageObjectPosition() const; 97 const std::optional<Color>& GetImageFill() const; 98 99 static RefPtr<ImageComponent> MakeFromOtherWithoutSourceAndEvent(const RefPtr<ImageComponent>& other); 100 GetOnDragStartId()101 const OnDragFunc& GetOnDragStartId() const 102 { 103 return onDragStartId_; 104 } 105 SetOnDragStartId(const OnDragFunc & onDragStartId)106 void SetOnDragStartId(const OnDragFunc& onDragStartId) 107 { 108 onDragStartId_ = onDragStartId; 109 } 110 GetOnDragEnterId()111 const OnDropFunc& GetOnDragEnterId() const 112 { 113 return onDragEnterId_; 114 } 115 SetOnDragEnterId(const OnDropFunc & onDragEnterId)116 void SetOnDragEnterId(const OnDropFunc& onDragEnterId) 117 { 118 onDragEnterId_ = onDragEnterId; 119 } 120 GetOnDragMoveId()121 const OnDropFunc& GetOnDragMoveId() const 122 { 123 return onDragMoveId_; 124 } 125 SetOnDragMoveId(const OnDropFunc & onDragMoveId)126 void SetOnDragMoveId(const OnDropFunc& onDragMoveId) 127 { 128 onDragMoveId_ = onDragMoveId; 129 } 130 GetOnDragLeaveId()131 const OnDropFunc& GetOnDragLeaveId() const 132 { 133 return onDragLeaveId_; 134 } 135 SetOnDragLeaveId(const OnDropFunc & onDragLeaveId)136 void SetOnDragLeaveId(const OnDropFunc& onDragLeaveId) 137 { 138 onDragLeaveId_ = onDragLeaveId; 139 } 140 GetOnDropId()141 const OnDropFunc& GetOnDropId() const 142 { 143 return onDropId_; 144 } 145 SetOnDropId(const OnDropFunc & onDropId)146 void SetOnDropId(const OnDropFunc& onDropId) 147 { 148 onDropId_ = onDropId; 149 } 150 151 const CopyOptions& GetCopyOption() const; 152 void SetCopyOption(const CopyOptions& copyOption); 153 SetColorFilterMatrix(const std::vector<float> & colorfilter)154 void SetColorFilterMatrix(const std::vector<float>& colorfilter) 155 { 156 if (colorfilter.size() == COLOR_FILTER_MATRIX_SIZE) { 157 colorfilter_ = colorfilter; 158 } 159 } 160 GetColorFilterMatrix()161 const std::vector<float>& GetColorFilterMatrix() const 162 { 163 return colorfilter_; 164 } 165 GetFocusable()166 bool GetFocusable() const 167 { 168 return focusable_; 169 } 170 SetFocusable(bool focusable)171 void SetFocusable(bool focusable) 172 { 173 focusable_ = focusable; 174 } 175 GetBlurRadius()176 float GetBlurRadius() const 177 { 178 return blurRadius_; 179 } 180 SetBlur(float radius)181 void SetBlur(float radius) 182 { 183 blurRadius_ = radius; 184 } 185 SetBundleInfo(const std::string & bundleName,const std::string moduleName)186 void SetBundleInfo(const std::string& bundleName, const std::string moduleName) 187 { 188 bundleName_ = bundleName; 189 moduleName_ = moduleName; 190 } 191 GetBundleName()192 std::string GetBundleName() const 193 { 194 return bundleName_; 195 } 196 GetModuleName()197 std::string GetModuleName() const 198 { 199 return moduleName_; 200 } 201 202 private: 203 std::string src_; 204 std::string alt_; 205 // Interim programme 206 std::string bundleName_; 207 std::string moduleName_; 208 Alignment alignment_ = Alignment::CENTER; 209 ImageObjectPosition imageObjectPosition_; 210 211 std::optional<Color> color_; 212 std::optional<Color> fillColor_; // used for paint svg path. 213 std::vector<float> colorfilter_; 214 float blurRadius_ = 0.0f; 215 216 EventMarker loadSuccessEvent_; 217 EventMarker loadFailEvent_; 218 EventMarker svgAnimatorFinishEvent_; 219 InternalResource::ResourceId resourceId_ = InternalResource::ResourceId::NO_ID; 220 Border border_; 221 bool fitMaxSize_ = true; 222 bool hasObjectPosition_ = false; 223 bool matchTextDirection_ = false; 224 #ifndef USE_ROSEN_DRAWING 225 bool useSkiaSvg_ = true; 226 #else 227 bool useSkiaSvg_ = false; 228 #endif 229 bool autoResize_ = true; 230 bool focusable_ = true; 231 232 ImageFit imageFit_ = ImageFit::COVER; 233 // set default value to [ImageInterpolation::LOW] to keep consistent for the old frontend 234 ImageInterpolation imageInterpolation_ = ImageInterpolation::LOW; 235 ImageRenderMode imageRenderMode_ = ImageRenderMode::ORIGINAL; 236 ImageRepeat imageRepeat_ = ImageRepeat::NO_REPEAT; 237 std::pair<Dimension, Dimension> imageSourceSize_ = std::pair<Dimension, Dimension>(Dimension(-1), Dimension(-1)); 238 RefPtr<PixelMap> pixmap_ = nullptr; 239 bool syncMode_ = false; 240 OnDragFunc onDragStartId_; 241 OnDropFunc onDragEnterId_; 242 OnDropFunc onDragMoveId_; 243 OnDropFunc onDragLeaveId_; 244 OnDropFunc onDropId_; 245 246 CopyOptions copyOption_ = CopyOptions::None; 247 }; 248 249 } // namespace OHOS::Ace 250 251 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_IMAGE_IMAGE_COMPONENT_H 252