1 /* 2 * Copyright (c) 2021 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 #include "core/components/common/properties/decoration.h" 35 36 namespace OHOS::Ace { 37 constexpr int32_t COLOR_FILTER_MATRIX_SIZE = 20; 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 186 private: 187 std::string src_; 188 std::string alt_; 189 Alignment alignment_ = Alignment::CENTER; 190 ImageObjectPosition imageObjectPosition_; 191 192 std::optional<Color> color_; 193 std::optional<Color> fillColor_; // used for paint svg path. 194 std::vector<float> colorfilter_; 195 float blurRadius_ = 0.0f; 196 197 EventMarker loadSuccessEvent_; 198 EventMarker loadFailEvent_; 199 EventMarker svgAnimatorFinishEvent_; 200 InternalResource::ResourceId resourceId_ = InternalResource::ResourceId::NO_ID; 201 Border border_; 202 bool fitMaxSize_ = true; 203 bool hasObjectPosition_ = false; 204 bool matchTextDirection_ = false; 205 bool useSkiaSvg_ = true; 206 bool autoResize_ = true; 207 bool focusable_ = true; 208 209 ImageFit imageFit_ = ImageFit::COVER; 210 // set default value to [ImageInterpolation::LOW] to keep consistent for the old frontend 211 ImageInterpolation imageInterpolation_ = ImageInterpolation::LOW; 212 ImageRenderMode imageRenderMode_ = ImageRenderMode::ORIGINAL; 213 ImageRepeat imageRepeat_ = ImageRepeat::NO_REPEAT; 214 std::pair<Dimension, Dimension> imageSourceSize_ = std::pair<Dimension, Dimension>(Dimension(-1), Dimension(-1)); 215 RefPtr<PixelMap> pixmap_ = nullptr; 216 bool syncMode_ = false; 217 OnDragFunc onDragStartId_; 218 OnDropFunc onDragEnterId_; 219 OnDropFunc onDragMoveId_; 220 OnDropFunc onDragLeaveId_; 221 OnDropFunc onDropId_; 222 223 CopyOptions copyOption_ = CopyOptions::None; 224 }; 225 226 } // namespace OHOS::Ace 227 228 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_IMAGE_IMAGE_COMPONENT_H 229