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