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 #include "core/components/image/image_component.h"
17 
18 #include "core/components/image/image_element.h"
19 
20 namespace OHOS::Ace {
21 
ImageComponent(const std::string & src)22 ImageComponent::ImageComponent(const std::string& src) : src_(src) {}
23 
CreateRenderNode()24 RefPtr<RenderNode> ImageComponent::CreateRenderNode()
25 {
26     return RenderImage::Create();
27 }
28 
CreateElement()29 RefPtr<Element> ImageComponent::CreateElement()
30 {
31     return AceType::MakeRefPtr<ImageElement>();
32 }
33 
SetSrc(const std::string & src)34 void ImageComponent::SetSrc(const std::string& src)
35 {
36     src_ = src;
37 }
38 
SetImageFill(const std::optional<Color> & color)39 void ImageComponent::SetImageFill(const std::optional<Color>& color)
40 {
41     fillColor_ = color;
42 }
43 
SetAlignment(const Alignment & alignment)44 void ImageComponent::SetAlignment(const Alignment& alignment)
45 {
46     alignment_ = alignment;
47 }
48 
GetAlignment() const49 const Alignment& ImageComponent::GetAlignment() const
50 {
51     return alignment_;
52 }
53 
GetBorder() const54 const Border& ImageComponent::GetBorder() const
55 {
56     return border_;
57 }
58 
GetSrc() const59 const std::string& ImageComponent::GetSrc() const
60 {
61     return src_;
62 }
63 
SetAlt(const std::string & alt)64 void ImageComponent::SetAlt(const std::string& alt)
65 {
66     alt_ = alt;
67 }
68 
GetAlt() const69 const std::string& ImageComponent::GetAlt() const
70 {
71     return alt_;
72 }
73 
SetColor(const std::optional<Color> & color)74 void ImageComponent::SetColor(const std::optional<Color>& color)
75 {
76     color_ = color;
77 }
78 
IsMatchTextDirection() const79 bool ImageComponent::IsMatchTextDirection() const
80 {
81     return matchTextDirection_;
82 }
83 
IsSrcSvgImage() const84 bool ImageComponent::IsSrcSvgImage() const
85 {
86     return IsSvgSuffix(src_);
87 }
88 
GetColor() const89 const std::optional<Color>& ImageComponent::GetColor() const
90 {
91     return color_;
92 }
93 
SetLoadSuccessEvent(const EventMarker & loadSuccessEvent)94 void ImageComponent::SetLoadSuccessEvent(const EventMarker& loadSuccessEvent)
95 {
96     loadSuccessEvent_ = loadSuccessEvent;
97 }
98 
GetLoadSuccessEvent() const99 const EventMarker& ImageComponent::GetLoadSuccessEvent() const
100 {
101     return loadSuccessEvent_;
102 }
103 
SetLoadFailEvent(const EventMarker & loadFailEvent)104 void ImageComponent::SetLoadFailEvent(const EventMarker& loadFailEvent)
105 {
106     loadFailEvent_ = loadFailEvent;
107 }
108 
GetLoadFailEvent() const109 const EventMarker& ImageComponent::GetLoadFailEvent() const
110 {
111     return loadFailEvent_;
112 }
113 
SetSvgAnimatorFinishEvent(const EventMarker & svgAnimatorFinishEvent)114 void ImageComponent::SetSvgAnimatorFinishEvent(const EventMarker& svgAnimatorFinishEvent)
115 {
116     svgAnimatorFinishEvent_ = svgAnimatorFinishEvent;
117 }
118 
GetSvgAnimatorFinishEvent() const119 const EventMarker& ImageComponent::GetSvgAnimatorFinishEvent() const
120 {
121     return svgAnimatorFinishEvent_;
122 }
123 
GetResourceId() const124 InternalResource::ResourceId ImageComponent::GetResourceId() const
125 {
126     return resourceId_;
127 }
128 
SetResourceId(InternalResource::ResourceId resourceId)129 void ImageComponent::SetResourceId(InternalResource::ResourceId resourceId)
130 {
131     resourceId_ = resourceId;
132 }
133 
SetBorder(const Border & border)134 void ImageComponent::SetBorder(const Border& border)
135 {
136     border_ = border;
137 }
138 
SetMatchTextDirection(bool matchTextDirection)139 void ImageComponent::SetMatchTextDirection(bool matchTextDirection)
140 {
141     matchTextDirection_ = matchTextDirection;
142 }
143 
SetFitMaxSize(bool fitMaxSize)144 void ImageComponent::SetFitMaxSize(bool fitMaxSize)
145 {
146     fitMaxSize_ = fitMaxSize;
147 }
148 
SetImageFit(ImageFit imageFit)149 void ImageComponent::SetImageFit(ImageFit imageFit)
150 {
151     imageFit_ = imageFit;
152 }
153 
SetImageInterpolation(ImageInterpolation imageInterpolation)154 void ImageComponent::SetImageInterpolation(ImageInterpolation imageInterpolation)
155 {
156     imageInterpolation_ = imageInterpolation;
157 }
158 
SetImageRenderMode(ImageRenderMode imageRenderMode)159 void ImageComponent::SetImageRenderMode(ImageRenderMode imageRenderMode)
160 {
161     imageRenderMode_ = imageRenderMode;
162 }
163 
SetImageRepeat(ImageRepeat imageRepeat)164 void ImageComponent::SetImageRepeat(ImageRepeat imageRepeat)
165 {
166     imageRepeat_ = imageRepeat;
167 }
168 
SetImageSourceSize(const std::pair<Dimension,Dimension> & sourceSize)169 void ImageComponent::SetImageSourceSize(const std::pair<Dimension, Dimension>& sourceSize)
170 {
171     imageSourceSize_ = sourceSize;
172 }
173 
SetUseSkiaSvg(bool useSkiaSvg)174 void ImageComponent::SetUseSkiaSvg(bool useSkiaSvg)
175 {
176 #ifndef USE_ROSEN_DRAWING
177     useSkiaSvg_ = useSkiaSvg;
178 #else
179     useSkiaSvg_ = false;
180 #endif
181 }
182 
SetAutoResize(bool autoResize)183 void ImageComponent::SetAutoResize(bool autoResize)
184 {
185     autoResize_ = autoResize;
186 }
187 
SetSyncMode(bool syncMode)188 void ImageComponent::SetSyncMode(bool syncMode)
189 {
190     syncMode_ = syncMode;
191 }
192 
GetSyncMode()193 bool ImageComponent::GetSyncMode()
194 {
195     return syncMode_;
196 }
197 
MakeFromOtherWithoutSourceAndEvent(const RefPtr<ImageComponent> & other)198 RefPtr<ImageComponent> ImageComponent::MakeFromOtherWithoutSourceAndEvent(const RefPtr<ImageComponent>& other)
199 {
200     RefPtr<ImageComponent> imageComponent = AceType::MakeRefPtr<ImageComponent>("");
201     imageComponent->SetAlignment(other->GetAlignment());
202     imageComponent->SetImageFill(other->GetImageFill());
203     imageComponent->SetColor(other->GetColor());
204     imageComponent->SetBorder(other->GetBorder());
205     imageComponent->SetFitMaxSize(other->GetFitMaxSize());
206     imageComponent->SetMatchTextDirection(other->IsMatchTextDirection());
207     imageComponent->SetImageFit(other->GetImageFit());
208     imageComponent->SetImageInterpolation(other->GetImageInterpolation());
209     imageComponent->SetImageRenderMode(other->GetImageRenderMode());
210     imageComponent->SetImageRepeat(other->GetImageRepeat());
211     imageComponent->SetImageSourceSize(other->GetImageSourceSize());
212     imageComponent->SetUseSkiaSvg(other->GetUseSkiaSvg());
213     imageComponent->SetAutoResize(other->GetAutoResize());
214     return imageComponent;
215 }
216 
GetImageFill() const217 const std::optional<Color>& ImageComponent::GetImageFill() const
218 {
219     return fillColor_;
220 }
221 
GetFitMaxSize() const222 bool ImageComponent::GetFitMaxSize() const
223 {
224     return fitMaxSize_;
225 }
226 
GetImageFit() const227 ImageFit ImageComponent::GetImageFit() const
228 {
229     return imageFit_;
230 }
231 
GetImageInterpolation() const232 ImageInterpolation ImageComponent::GetImageInterpolation() const
233 {
234     return imageInterpolation_;
235 }
236 
GetImageRenderMode() const237 ImageRenderMode ImageComponent::GetImageRenderMode() const
238 {
239     return imageRenderMode_;
240 }
241 
GetImageRepeat() const242 ImageRepeat ImageComponent::GetImageRepeat() const
243 {
244     return imageRepeat_;
245 }
246 
GetImageSourceSize() const247 const std::pair<Dimension, Dimension>& ImageComponent::GetImageSourceSize() const
248 {
249     return imageSourceSize_;
250 }
251 
GetUseSkiaSvg() const252 bool ImageComponent::GetUseSkiaSvg() const
253 {
254     return useSkiaSvg_;
255 }
256 
GetAutoResize() const257 bool ImageComponent::GetAutoResize() const
258 {
259     return autoResize_;
260 }
261 
IsSvgSuffix(const std::string & src)262 bool ImageComponent::IsSvgSuffix(const std::string& src)
263 {
264     // 4 is the length of ".svg".
265     return (src.size() > 4 && src.substr(src.size() - 4) == ".svg");
266 }
267 
SetPixmap(const RefPtr<PixelMap> & pixmap)268 void ImageComponent::SetPixmap(const RefPtr<PixelMap>& pixmap)
269 {
270     pixmap_ = pixmap;
271 }
272 
GetPixmap() const273 const RefPtr<PixelMap>& ImageComponent::GetPixmap() const
274 {
275     return pixmap_;
276 }
277 
SetHasObjectPosition(bool hasObjectPosition)278 void ImageComponent::SetHasObjectPosition(bool hasObjectPosition)
279 {
280     hasObjectPosition_ = hasObjectPosition;
281 }
282 
GetHasObjectPosition() const283 bool ImageComponent::GetHasObjectPosition() const
284 {
285     return hasObjectPosition_;
286 }
287 
SetImageObjectPosition(const ImageObjectPosition & imageObjectPosition)288 void ImageComponent::SetImageObjectPosition(const ImageObjectPosition& imageObjectPosition)
289 {
290     imageObjectPosition_ = imageObjectPosition;
291     SetHasObjectPosition(true);
292 }
293 
GetImageObjectPosition() const294 const ImageObjectPosition& ImageComponent::GetImageObjectPosition() const
295 {
296     return imageObjectPosition_;
297 }
298 
GetCopyOption() const299 const CopyOptions& ImageComponent::GetCopyOption() const
300 {
301     return copyOption_;
302 }
303 
SetCopyOption(const CopyOptions & copyOption)304 void ImageComponent::SetCopyOption(const CopyOptions& copyOption)
305 {
306     copyOption_ = copyOption;
307 }
308 
309 } // namespace OHOS::Ace
310