• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_BASE_PROPERTIES_DECORATION_H
17 #define FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_BASE_PROPERTIES_DECORATION_H
18 
19 #include <memory>
20 #include <optional>
21 #include <regex>
22 #include <vector>
23 
24 #include "base/geometry/dimension.h"
25 #include "base/geometry/rect.h"
26 #include "base/memory/ace_type.h"
27 #include "base/utils/macros.h"
28 #include "core/components/common/properties/alignment.h"
29 #include "core/components/common/properties/animatable_color.h"
30 #include "core/components/common/properties/border.h"
31 #include "core/components/common/properties/color.h"
32 #include "core/components/common/properties/edge.h"
33 #include "core/components/common/properties/shadow.h"
34 #include "core/pipeline/pipeline_context.h"
35 #include "core/components/theme/theme_utils.h"
36 
37 namespace OHOS::Ace {
38 
39 constexpr double CENTER_OFFSET = 50.0;
40 constexpr double FULL_IMG_SIZE = 100.0;
41 constexpr double BOX_BEGIN_SIZE = 0.0;
42 constexpr double BOX_END_SIZE = 100.0;
43 constexpr double PERCENT_TRANSLATE = 100.0;
44 
45 enum class GradientDirection {
46     LEFT = 0,
47     TOP,
48     RIGHT,
49     BOTTOM,
50     LEFT_TOP,
51     LEFT_BOTTOM,
52     RIGHT_TOP,
53     RIGHT_BOTTOM,
54     NONE,
55     START_TO_END,
56     END_TO_START,
57 };
58 
59 enum class GradientType {
60     LINEAR,
61     RADIAL,
62     SWEEP,
63     CONIC,
64 };
65 
66 enum class RadialSizeType {
67     CLOSEST_SIDE,
68     CLOSEST_CORNER,
69     FARTHEST_SIDE,
70     FARTHEST_CORNER,
71     NONE,
72 };
73 
74 enum class RadialShapeType {
75     CIRCLE,
76     ELLIPSE,
77     NONE,
78 };
79 
80 enum class SpreadMethod {
81     PAD,
82     REFLECT,
83     REPEAT,
84 };
85 
86 struct LinearGradientInfo {
87     double x1 = 0.0;
88     double x2 = 0.0;
89     double y1 = 0.0;
90     double y2 = 0.0;
91 };
92 
93 struct RadialGradientInfo {
94     double r = 0.0;
95     double cx = 0.0;
96     double cy = 0.0;
97     double fx = 0.0;
98     double fy = 0.0;
99 };
100 
101 class GradientColor final {
102 public:
103     GradientColor() = default;
104     ~GradientColor() = default;
105 
GradientColor(const Color & color)106     explicit GradientColor(const Color& color)
107     {
108         color_ = color;
109     }
110 
111     void SetDimension(double value, DimensionUnit unit = DimensionUnit::PERCENT)
112     {
113         if (value < 0.0) {
114             return;
115         }
116         if (unit == DimensionUnit::PERCENT && value > BOX_END_SIZE) {
117             return;
118         }
119         dimension_ = Dimension(value, unit);
120     }
121 
SetDimension(const Dimension & dimension)122     void SetDimension(const Dimension& dimension)
123     {
124         if (dimension.Value() < 0.0) {
125             return;
126         }
127         if (dimension.Unit() == DimensionUnit::PERCENT && dimension.Value() > BOX_END_SIZE) {
128             return;
129         }
130         dimension_ = dimension;
131     }
132 
SetHasValue(bool hasValue)133     void SetHasValue(bool hasValue)
134     {
135         hasValue_ = hasValue;
136     }
137 
SetColor(const Color & color)138     void SetColor(const Color& color)
139     {
140         color_ = color;
141     }
142 
GetColor()143     const Color& GetColor() const
144     {
145         return color_;
146     }
147 
GetDimension()148     const Dimension& GetDimension() const
149     {
150         return dimension_;
151     }
152 
GetHasValue()153     bool GetHasValue() const
154     {
155         return hasValue_;
156     }
157 
SetOpacity(double opacity)158     void SetOpacity(double opacity)
159     {
160         opacity_ = opacity;
161     }
162 
GetOpacity()163     double GetOpacity() const
164     {
165         return opacity_;
166     }
167 
168 private:
169     bool hasValue_ = true;
170     Color color_ { Color::TRANSPARENT };
171     Dimension dimension_ { BOX_END_SIZE, DimensionUnit::PERCENT };
172     double opacity_ = 1.0;
173 };
174 
175 struct ACE_EXPORT RadialGradient {
176     // size type
177     std::optional<RadialSizeType> radialSizeType;
178     // shape circle or ellipse
179     std::optional<RadialShapeType> radialShape;
180     // size in x-axis
181     std::optional<AnimatableDimension> radialHorizontalSize;
182     // size in y-axis
183     std::optional<AnimatableDimension> radialVerticalSize;
184     // center of shape
185     std::optional<AnimatableDimension> radialCenterX;
186     std::optional<AnimatableDimension> radialCenterY;
187 
188     std::optional<Dimension> fRadialCenterX;
189     std::optional<Dimension> fRadialCenterY;
190 };
191 
192 struct ACE_EXPORT LinearGradient {
193     // direction in x-axis
194     std::optional<GradientDirection> linearX;
195     // direction in y-axis
196     std::optional<GradientDirection> linearY;
197     // angle of gradient line in bearing angle
198     std::optional<AnimatableDimension> angle;
199 
200     std::optional<Dimension> x1;
201     std::optional<Dimension> y1;
202     std::optional<Dimension> x2;
203     std::optional<Dimension> y2;
204 
205     // is direction in x-axis
IsXAxisLinearGradient206     static bool IsXAxis(GradientDirection direction)
207     {
208         return (direction == GradientDirection::LEFT || direction == GradientDirection::RIGHT ||
209                 direction == GradientDirection::START_TO_END || direction == GradientDirection::END_TO_START);
210     }
211 };
212 
213 struct ACE_EXPORT SweepGradient {
214     // center of x-axis
215     std::optional<AnimatableDimension> centerX;
216     // center of y-axis
217     std::optional<AnimatableDimension> centerY;
218     // startAngle in degree
219     std::optional<AnimatableDimension> startAngle;
220     // endAngle in degree
221     std::optional<AnimatableDimension> endAngle;
222     // rotation in degree
223     std::optional<AnimatableDimension> rotation;
224 };
225 
226 class ACE_EXPORT Gradient final {
227 public:
228     void AddColor(const GradientColor& color);
229 
230     void ClearColors();
231 
IsValid()232     bool IsValid() const
233     {
234         return colors_.size() > 1;
235     }
236 
SetRepeat(bool repeat)237     void SetRepeat(bool repeat)
238     {
239         repeat_ = repeat;
240     }
241 
GetRepeat()242     bool GetRepeat() const
243     {
244         return repeat_;
245     }
246 
GetColors()247     const std::vector<GradientColor>& GetColors() const
248     {
249         return colors_;
250     }
251 
GetBeginOffset()252     const Offset& GetBeginOffset() const
253     {
254         return beginOffset_;
255     }
256 
SetBeginOffset(const Offset & beginOffset)257     void SetBeginOffset(const Offset& beginOffset)
258     {
259         beginOffset_ = beginOffset;
260     }
261 
GetEndOffset()262     const Offset& GetEndOffset() const
263     {
264         return endOffset_;
265     }
266 
SetEndOffset(const Offset & endOffset)267     void SetEndOffset(const Offset& endOffset)
268     {
269         endOffset_ = endOffset;
270     }
271 
GetInnerRadius()272     double GetInnerRadius() const
273     {
274         return innerRadius_;
275     }
276 
SetInnerRadius(double innerRadius)277     void SetInnerRadius(double innerRadius)
278     {
279         innerRadius_ = innerRadius;
280     }
281 
GetOuterRadius()282     double GetOuterRadius() const
283     {
284         return outerRadius_;
285     }
286 
SetOuterRadius(double outerRadius)287     void SetOuterRadius(double outerRadius)
288     {
289         outerRadius_ = outerRadius;
290     }
291 
GetType()292     GradientType GetType() const
293     {
294         return type_;
295     }
296 
SetType(GradientType type)297     void SetType(GradientType type)
298     {
299         type_ = type;
300     }
301 
ToString()302     std::string ToString() const
303     {
304         return std::string("Gradient (")
305             .append(beginOffset_.ToString())
306             .append(",")
307             .append(std::to_string(innerRadius_))
308             .append(" --- ")
309             .append(endOffset_.ToString())
310             .append(",")
311             .append(std::to_string(outerRadius_))
312             .append(")");
313     }
314 
GetSweepGradient()315     SweepGradient& GetSweepGradient()
316     {
317         return sweepGradient_;
318     }
319 
GetSweepGradient()320     const SweepGradient& GetSweepGradient() const
321     {
322         return sweepGradient_;
323     }
324 
SetSweepGradient(const SweepGradient & sweepGradient)325     void SetSweepGradient(const SweepGradient& sweepGradient)
326     {
327         sweepGradient_ = sweepGradient;
328     }
329 
GetRadialGradient()330     RadialGradient& GetRadialGradient()
331     {
332         return radialGradient_;
333     }
334 
GetRadialGradient()335     const RadialGradient& GetRadialGradient() const
336     {
337         return radialGradient_;
338     }
339 
SetRadialGradient(const RadialGradient & radialGradient)340     void SetRadialGradient(const RadialGradient& radialGradient)
341     {
342         radialGradient_ = radialGradient;
343     }
344 
GetLinearGradient()345     LinearGradient& GetLinearGradient()
346     {
347         return linearGradient_;
348     }
349 
GetLinearGradient()350     const LinearGradient& GetLinearGradient() const
351     {
352         return linearGradient_;
353     }
354 
SetLinearGradient(const LinearGradient & linearGradient)355     void SetLinearGradient(const LinearGradient& linearGradient)
356     {
357         linearGradient_ = linearGradient;
358     }
359 
SetDirection(const GradientDirection & direction)360     void SetDirection(const GradientDirection& direction)
361     {
362         if (LinearGradient::IsXAxis(direction)) {
363             linearGradient_.linearX = direction;
364         } else {
365             linearGradient_.linearY = direction;
366         }
367     }
368 
SetSpreadMethod(SpreadMethod spreadMethod)369     void SetSpreadMethod(SpreadMethod spreadMethod)
370     {
371         spreadMethod_ = spreadMethod;
372     }
373 
SetGradientTransform(const std::string & gradientTransform)374     void SetGradientTransform(const std::string& gradientTransform)
375     {
376         gradientTransform_ = gradientTransform;
377     }
378 
GetSpreadMethod()379     SpreadMethod GetSpreadMethod() const
380     {
381         return spreadMethod_;
382     }
383 
GetGradientTransform()384     const std::string& GetGradientTransform() const
385     {
386         return gradientTransform_;
387     }
388 
GetRadialGradientInfo()389     const RadialGradientInfo& GetRadialGradientInfo() const
390     {
391         return radialGradientInfo_;
392     }
393 
SetRadialGradientInfo(const RadialGradientInfo & radialGradientInfo)394     void SetRadialGradientInfo(const RadialGradientInfo& radialGradientInfo)
395     {
396         radialGradientInfo_ = radialGradientInfo;
397     }
398 
GetLinearGradientInfo()399     const LinearGradientInfo& GetLinearGradientInfo() const
400     {
401         return linearGradientInfo_;
402     }
403 
SetLinearGradientInfo(const LinearGradientInfo & linearGradientInfo)404     void SetLinearGradientInfo(const LinearGradientInfo& linearGradientInfo)
405     {
406         linearGradientInfo_ = linearGradientInfo;
407     }
408 
409 private:
410     GradientType type_ = GradientType::LINEAR;
411     bool repeat_ = false;
412     std::vector<GradientColor> colors_;
413     // for RadialGradient
414     RadialGradient radialGradient_;
415     // for LinearGradient
416     LinearGradient linearGradient_;
417     // for SweepGradient
418     SweepGradient sweepGradient_;
419     // used for CanvasLinearGradient
420     Offset beginOffset_;
421     Offset endOffset_;
422     // used for CanvasRadialGradient
423     double innerRadius_ = 0.0;
424     double outerRadius_ = 0.0;
425     SpreadMethod spreadMethod_ = SpreadMethod::PAD;
426     std::string gradientTransform_;
427     LinearGradientInfo linearGradientInfo_;
428     RadialGradientInfo radialGradientInfo_;
429 };
430 
431 enum class ACE_EXPORT BackgroundImageSizeType {
432     CONTAIN = 0,
433     COVER,
434     AUTO,
435     LENGTH,
436     PERCENT,
437 };
438 
439 class ACE_EXPORT BackgroundImageSize final {
440 public:
441     BackgroundImageSize() = default;
BackgroundImageSize(BackgroundImageSizeType type,double value)442     BackgroundImageSize(BackgroundImageSizeType type, double value) : typeX_(type), valueX_(value) {}
BackgroundImageSize(BackgroundImageSizeType typeX,double valueX,BackgroundImageSizeType typeY,double valueY)443     BackgroundImageSize(BackgroundImageSizeType typeX, double valueX, BackgroundImageSizeType typeY, double valueY)
444         : typeX_(typeX), valueX_(valueX), typeY_(typeY), valueY_(valueY)
445     {}
446     ~BackgroundImageSize() = default;
447 
448     void SetSizeTypeX(BackgroundImageSizeType type);
449     void SetSizeTypeY(BackgroundImageSizeType type);
450     void SetSizeValueX(double value);
451     void SetSizeValueY(double value);
452     bool IsValid() const;
453     BackgroundImageSizeType GetSizeTypeX() const;
454     BackgroundImageSizeType GetSizeTypeY() const;
455     double GetSizeValueX() const;
456     double GetSizeValueY() const;
457 
458     BackgroundImageSize operator+(const BackgroundImageSize& size) const;
459     BackgroundImageSize operator-(const BackgroundImageSize& size) const;
460     BackgroundImageSize operator*(double value) const;
461 
462     bool operator==(const BackgroundImageSize& size) const;
463     bool operator!=(const BackgroundImageSize& size) const;
464 
465 private:
466     BackgroundImageSizeType typeX_ { BackgroundImageSizeType::AUTO };
467     double valueX_ = 0.0;
468     BackgroundImageSizeType typeY_ { BackgroundImageSizeType::AUTO };
469     double valueY_ = 0.0;
470 };
471 
472 enum class ACE_EXPORT BackgroundImagePositionType {
473     PERCENT = 0,
474     PX,
475 };
476 
477 class ACE_EXPORT BackgroundImagePosition {
478 public:
479     BackgroundImagePosition() = default;
480     ~BackgroundImagePosition() = default;
BackgroundImagePosition(BackgroundImagePositionType typeX,double valueX,BackgroundImagePositionType typeY,double valueY)481     BackgroundImagePosition(
482         BackgroundImagePositionType typeX, double valueX, BackgroundImagePositionType typeY, double valueY)
483         : typeX_(typeX), typeY_(typeY), valueX_(AnimatableDimension(valueX)), valueY_(AnimatableDimension(valueY))
484     {}
485 
SetContextAndCallback(const WeakPtr<PipelineContext> & context,const RenderNodeAnimationCallback & callback)486     void SetContextAndCallback(const WeakPtr<PipelineContext>& context, const RenderNodeAnimationCallback& callback)
487     {
488         valueX_.SetContextAndCallback(context, callback);
489         valueY_.SetContextAndCallback(context, callback);
490     }
491 
SetSizeTypeX(BackgroundImagePositionType type)492     void SetSizeTypeX(BackgroundImagePositionType type)
493     {
494         typeX_ = type;
495     }
496 
SetSizeX(const AnimatableDimension & sizeX)497     void SetSizeX(const AnimatableDimension& sizeX)
498     {
499         if (sizeX.Unit() == DimensionUnit::PERCENT) {
500             typeX_ = BackgroundImagePositionType::PERCENT;
501         } else {
502             typeX_ = BackgroundImagePositionType::PX;
503         }
504         valueX_ = sizeX;
505     }
506 
SetSizeTypeY(BackgroundImagePositionType type)507     void SetSizeTypeY(BackgroundImagePositionType type)
508     {
509         typeY_ = type;
510     }
511 
SetSizeY(const AnimatableDimension & sizeY)512     void SetSizeY(const AnimatableDimension& sizeY)
513     {
514         if (sizeY.Unit() == DimensionUnit::PERCENT) {
515             typeY_ = BackgroundImagePositionType::PERCENT;
516         } else {
517             typeY_ = BackgroundImagePositionType::PX;
518         }
519         valueY_ = sizeY;
520     }
521 
SetSizeValueX(double value)522     void SetSizeValueX(double value)
523     {
524         valueX_ = AnimatableDimension(value);
525     }
526 
SetSizeValueY(double value)527     void SetSizeValueY(double value)
528     {
529         valueY_ = AnimatableDimension(value);
530     }
531 
GetSizeTypeX()532     BackgroundImagePositionType GetSizeTypeX() const
533     {
534         return typeX_;
535     }
536 
GetSizeTypeY()537     BackgroundImagePositionType GetSizeTypeY() const
538     {
539         return typeY_;
540     }
541 
GetSizeX()542     const AnimatableDimension& GetSizeX() const
543     {
544         return valueX_;
545     }
546 
GetSizeY()547     const AnimatableDimension& GetSizeY() const
548     {
549         return valueY_;
550     }
551 
GetSizeValueX()552     double GetSizeValueX() const
553     {
554         return valueX_.Value();
555     }
556 
GetSizeValueY()557     double GetSizeValueY() const
558     {
559         return valueY_.Value();
560     }
561 
562     BackgroundImagePosition operator+(const BackgroundImagePosition& position) const;
563 
564     BackgroundImagePosition operator-(const BackgroundImagePosition& position) const;
565 
566     BackgroundImagePosition operator*(double value) const;
567 
568     bool operator==(const BackgroundImagePosition& backgroundImagePosition) const;
569 
570     bool operator!=(const BackgroundImagePosition& backgroundImagePosition) const;
571 
572 private:
573     BackgroundImagePositionType typeX_ { BackgroundImagePositionType::PX };
574     BackgroundImagePositionType typeY_ { BackgroundImagePositionType::PX };
575     AnimatableDimension valueX_ = AnimatableDimension(0.0);
576     AnimatableDimension valueY_ = AnimatableDimension(0.0);
577 };
578 
579 class ImageObjectPosition final : public BackgroundImagePosition {
580 
581 };
582 
583 class BackgroundImage final : public AceType {
584     DECLARE_ACE_TYPE(BackgroundImage, AceType);
585 
586 public:
587     BackgroundImage() = default;
588     ~BackgroundImage() override = default;
589 
GetImageSize()590     const BackgroundImageSize& GetImageSize() const
591     {
592         return imageSize_;
593     }
594 
GetImagePosition()595     const BackgroundImagePosition& GetImagePosition() const
596     {
597         return imagePosition_;
598     }
599 
GetImageRepeat()600     ImageRepeat GetImageRepeat() const
601     {
602         return imageRepeat_;
603     }
604 
GetSrc()605     const std::string& GetSrc() const
606     {
607         return src_;
608     }
609 
SetImageSize(BackgroundImageSize imageSize)610     void SetImageSize(BackgroundImageSize imageSize)
611     {
612         imageSize_ = imageSize;
613     }
614 
615     void SetImageSize(BackgroundImageSizeType type, double value = FULL_IMG_SIZE)
616     {
617         imageSize_ = BackgroundImageSize(type, value);
618     }
619 
SetImageSize(BackgroundImageSizeType typeX,double valueX,BackgroundImageSizeType typeY,double valueY)620     void SetImageSize(BackgroundImageSizeType typeX, double valueX, BackgroundImageSizeType typeY, double valueY)
621     {
622         imageSize_ = BackgroundImageSize(typeX, valueX, typeY, valueY);
623     }
624 
SetImagePosition(const BackgroundImagePosition & imagePosition)625     void SetImagePosition(const BackgroundImagePosition& imagePosition)
626     {
627         imagePosition_ = imagePosition;
628     }
629 
SetImagePosition(BackgroundImagePositionType typeX,double valueX,BackgroundImagePositionType typeY,double valueY)630     void SetImagePosition(
631         BackgroundImagePositionType typeX, double valueX, BackgroundImagePositionType typeY, double valueY)
632     {
633         imagePosition_ = BackgroundImagePosition(typeX, valueX, typeY, valueY);
634     }
635 
SetImageRepeat(const ImageRepeat & imageRepeat)636     void SetImageRepeat(const ImageRepeat& imageRepeat)
637     {
638         imageRepeat_ = imageRepeat;
639     }
640 
SetSrc(const std::string & src,const RefPtr<ThemeConstants> & themeConstants)641     void SetSrc(const std::string& src, const RefPtr<ThemeConstants>& themeConstants)
642     {
643         // If match the regex, src with the outer "url()" removed is returned.
644         // Otherwise return a copy of src directly.
645         auto imgSrc = std::regex_replace(src, std::regex(R"(^url\(\s*['"]?\s*([^()]+?)\s*['"]?\s*\)$)"), "$1");
646         src_ = ThemeUtils::ProcessImageSource(imgSrc, themeConstants);
647     }
648 
649     void SetParsedSrc(const std::string& src)
650     {
651         // src is processed by ParseJsMedia function
652         src_ = src;
653     }
654 
655     bool operator==(const BackgroundImage& image) const
656     {
657         bool fileName = src_ == image.GetSrc();
658         bool size = imageSize_ == image.GetImageSize();
659         bool position = imagePosition_ == image.GetImagePosition();
660         bool repeat = imageRepeat_ == image.GetImageRepeat();
661         return fileName && size && position && repeat;
662     }
663 
664     bool operator!=(const BackgroundImage& image) const
665     {
666         return !operator==(image);
667     }
668 
669 private:
670     std::string src_;
671     BackgroundImageSize imageSize_;
672     BackgroundImagePosition imagePosition_;
673     ImageRepeat imageRepeat_ { ImageRepeat::REPEAT };
674 };
675 
676 class BorderImage final : public AceType {
677     DECLARE_ACE_TYPE(BorderImage, AceType);
678 public:
679     BorderImage() = default;
680     explicit BorderImage(const std::string& src)
681     {
682         src_ = src;
683     }
684 
685     ~BorderImage() override = default;
686 
687     const std::string& GetSrc() const
688     {
689         return src_;
690     }
691 
692     void SetSrc(const std::string& src)
693     {
694         src_ = src;
695     }
696 
697     bool operator==(const BorderImage& image) const
698     {
699         return src_ == image.GetSrc();
700     }
701 
702     bool operator!=(const BorderImage& image) const
703     {
704         return !operator==(image);
705     }
706 
707 private:
708     std::string src_;
709 };
710 
711 class ArcBackground final : public AceType {
712     DECLARE_ACE_TYPE(ArcBackground, AceType);
713 
714 public:
715     ~ArcBackground() override = default;
716     ArcBackground(Point center, double radius)
717     {
718         SetCenter(center);
719         SetRadius(radius);
720     }
721 
722     const Point& GetCenter() const
723     {
724         return center_;
725     }
726 
727     double GetRadius() const
728     {
729         return radius_;
730     }
731 
732     void SetCenter(const Point& center)
733     {
734         center_ = center;
735     }
736 
737     void SetRadius(double radius)
738     {
739         radius_ = radius;
740     }
741 
742     void SetColor(const Color& color)
743     {
744         color_ = color;
745     }
746 
747     const Color& GetColor() const
748     {
749         return color_;
750     }
751 
752 private:
753     Point center_;
754     double radius_ = 0.0;
755     Color color_;
756 };
757 
758 class Decoration final : public AceType {
759     DECLARE_ACE_TYPE(Decoration, AceType);
760 
761 public:
762     Decoration() = default;
763     ~Decoration() override = default;
764 
765     void SetContextAndCallback(
766         const WeakPtr<PipelineContext>& context, const RenderNodeAnimationCallback& callback);
767 
768     void AddShadow(const Shadow& shadow);
769 
770     void ClearAllShadow();
771 
772     void SetBackgroundColor(const Color& backgroundColor, const AnimationOption& option = AnimationOption())
773     {
774         backgroundColor_ = AnimatableColor(backgroundColor, option);
775     }
776 
777     void SetBackgroundColor(const AnimatableColor& backgroundColor)
778     {
779         backgroundColor_ = backgroundColor;
780     }
781 
782     void SetAnimationColor(const Color& animationColor)
783     {
784         animationColor_ = animationColor;
785     }
786 
787     void SetGradient(
788         const Gradient& gradient, const WeakPtr<PipelineContext>& context = nullptr,
789         const RenderNodeAnimationCallback& callback = nullptr)
790     {
791         gradient_ = gradient;
792         if (callback) {
793             switch (gradient_.GetType()) {
794                 case GradientType::LINEAR:
795                     if (gradient_.GetLinearGradient().angle) {
796                         gradient_.GetLinearGradient().angle->SetContextAndCallbackAfterFirstAssign(context, callback);
797                     }
798                     break;
799                 case GradientType::SWEEP:
800                     if (gradient_.GetSweepGradient().centerX) {
801                         gradient_.GetSweepGradient().centerX->SetContextAndCallbackAfterFirstAssign(
802                             context, callback);
803                     }
804                     if (gradient_.GetSweepGradient().centerY) {
805                         gradient_.GetSweepGradient().centerY->SetContextAndCallbackAfterFirstAssign(context, callback);
806                     }
807                     if (gradient_.GetSweepGradient().startAngle) {
808                         gradient_.GetSweepGradient().startAngle->SetContextAndCallbackAfterFirstAssign(
809                             context, callback);
810                     }
811                     if (gradient_.GetSweepGradient().endAngle) {
812                         gradient_.GetSweepGradient().endAngle->SetContextAndCallbackAfterFirstAssign(context, callback);
813                     }
814                     if (gradient_.GetSweepGradient().rotation) {
815                         gradient_.GetSweepGradient().rotation->SetContextAndCallbackAfterFirstAssign(context, callback);
816                     }
817                     break;
818                 case GradientType::RADIAL:
819                     if (gradient_.GetRadialGradient().radialHorizontalSize) {
820                         gradient_.GetRadialGradient().radialHorizontalSize->SetContextAndCallbackAfterFirstAssign(
821                             context, callback);
822                     }
823                     if (gradient_.GetRadialGradient().radialVerticalSize) {
824                         gradient_.GetRadialGradient().radialVerticalSize->SetContextAndCallbackAfterFirstAssign(
825                             context, callback);
826                     }
827                     if (gradient_.GetRadialGradient().radialCenterX) {
828                         gradient_.GetRadialGradient().radialCenterX->SetContextAndCallbackAfterFirstAssign(
829                             context, callback);
830                     }
831                     if (gradient_.GetRadialGradient().radialCenterY) {
832                         gradient_.GetRadialGradient().radialCenterY->SetContextAndCallbackAfterFirstAssign(
833                             context, callback);
834                     }
835                     break;
836                 default:
837                     break;
838             }
839         }
840     }
841 
842     void SetGradientBorderImage(const Gradient& gradient)
843     {
844         gradientBorderImage_ = gradient;
845     }
846     void SetImage(const RefPtr<BackgroundImage>& image)
847     {
848         image_ = image;
849     }
850 
851     void SetBorderImage(const RefPtr<BorderImage>& borderImage)
852     {
853         borderImage_ = borderImage;
854     }
855 
856     void SetHasBorderImageSource(const bool tag)
857     {
858         hasBorderImageSource_ = tag;
859     }
860 
861     void SetHasBorderImageSlice(const bool tag)
862     {
863         hasBorderImageSlice_ = tag;
864     }
865 
866     void SetHasBorderImageWidth(const bool tag)
867     {
868         hasBorderImageWidth_ = tag;
869     }
870 
871     void SetHasBorderImageOutset(const bool tag)
872     {
873         hasBorderImageOutset_ = tag;
874     }
875 
876     void SetHasBorderImageRepeat(const bool tag)
877     {
878         hasBorderImageRepeat_ = tag;
879     }
880 
881     void SetHasBorderImageGradient(const bool tag)
882     {
883         hasBorderImageGradient_ = tag;
884     }
885 
886     void SetPadding(const Edge& padding)
887     {
888         padding_ = padding;
889     }
890 
891     void SetBorderRadius(const Radius& radius)
892     {
893         border_.SetBorderRadius(radius);
894     }
895 
896     void SetBorder(const Border& border)
897     {
898         border_ = border;
899     }
900 
901     void SetArcBackground(const RefPtr<ArcBackground>& arcBG)
902     {
903         arcBG_ = arcBG;
904     }
905 
906     void SetBlurRadius(const Dimension& radius)
907     {
908         blurRadius_ = radius;
909     }
910 
911     void SetBlurRadius(const AnimatableDimension& radius)
912     {
913         blurRadius_ = radius;
914     }
915 
916     const Color& GetColorBlend(void) const
917     {
918         return colorBlend;
919     }
920 
921     void SetColorBlend(const Color& color)
922     {
923         colorBlend = color;
924     }
925 
926     void SetWindowBlurProgress(float progress)
927     {
928         windowBlurProgress_ = progress;
929     }
930 
931     void SetWindowBlurStyle(WindowBlurStyle style)
932     {
933         windowBlurStyle_ = style;
934     }
935 
936     const Border& GetBorder() const
937     {
938         return border_;
939     }
940 
941     const Edge& GetPadding() const
942     {
943         return padding_;
944     }
945 
946     const RefPtr<BackgroundImage>& GetImage() const
947     {
948         return image_;
949     }
950 
951     const RefPtr<BorderImage>& GetBorderImage() const
952     {
953         return borderImage_;
954     }
955 
956     const Gradient& GetGradient() const
957     {
958         return gradient_;
959     }
960 
961     const Gradient& GetGradientBorderImage() const
962     {
963         return gradientBorderImage_;
964     }
965 
966     bool GetHasBorderImageSource()
967     {
968         return hasBorderImageSource_;
969     }
970 
971     bool GetHasBorderImageSlice()
972     {
973         return hasBorderImageSlice_;
974     }
975 
976     bool GetHasBorderImageWidth()
977     {
978         return hasBorderImageWidth_;
979     }
980 
981     bool GetHasBorderImageOutset()
982     {
983         return hasBorderImageOutset_;
984     }
985 
986     bool GetHasBorderImageRepeat()
987     {
988         return hasBorderImageRepeat_;
989     }
990 
991     bool GetHasBorderImageGradient()
992     {
993         return hasBorderImageGradient_;
994     }
995 
996     const AnimatableColor& GetBackgroundColor() const
997     {
998         return backgroundColor_;
999     }
1000 
1001     const Color& GetAnimationColor() const
1002     {
1003         return animationColor_;
1004     }
1005 
1006     const std::vector<Shadow>& GetShadows() const
1007     {
1008         return shadows_;
1009     }
1010 
1011     void SetShadows(const std::vector<Shadow>& shadows)
1012     {
1013         shadows_.assign(shadows.begin(), shadows.end());
1014     }
1015 
1016     const Dimension& GetGrayScale(void) const
1017     {
1018         return grayScale_;
1019     }
1020 
1021     void SetGrayScale(const Dimension& grayScale)
1022     {
1023         grayScale_ = grayScale;
1024     }
1025 
1026     void SetBrightness(const Dimension& brightness)
1027     {
1028         brightness_ = brightness;
1029     }
1030 
1031     const Dimension& GetBrightness() const
1032     {
1033         return brightness_;
1034     }
1035 
1036     const Dimension& GetContrast(void) const
1037     {
1038         return contrast_;
1039     }
1040 
1041     void SetContrast(const Dimension& contrast)
1042     {
1043         contrast_ = contrast;
1044     }
1045 
1046     const Dimension& GetSaturate(void) const
1047     {
1048         return saturate_;
1049     }
1050 
1051     void SetSaturate(const Dimension& saturate)
1052     {
1053         saturate_ = saturate;
1054     }
1055 
1056     const Dimension& GetSepia(void) const
1057     {
1058         return sepia_;
1059     }
1060 
1061     void SetSepia(const Dimension& sepia)
1062     {
1063         sepia_ = sepia;
1064     }
1065 
1066     void SetInvert(const Dimension& invert)
1067     {
1068         invert_ = invert;
1069     }
1070 
1071     const Dimension& GetInvert(void) const
1072     {
1073         return invert_;
1074     }
1075 
1076     float GetHueRotate(void) const
1077     {
1078         return hueRotate_;
1079     }
1080 
1081     void SetHueRotate(const float& hueRotate)
1082     {
1083         hueRotate_ = hueRotate;
1084     }
1085 
1086     const RefPtr<ArcBackground>& GetArcBackground() const
1087     {
1088         return arcBG_;
1089     }
1090 
1091     bool NeedReloadImage(const RefPtr<Decoration>& lastDecoration) const
1092     {
1093         if (!image_) {
1094             return false;
1095         }
1096 
1097         if (!lastDecoration || !(lastDecoration->GetImage())) {
1098             return true;
1099         }
1100 
1101         return (*image_) != (*(lastDecoration->GetImage()));
1102     }
1103 
1104     const AnimatableDimension& GetBlurRadius() const
1105     {
1106         return blurRadius_;
1107     }
1108 
1109     float GetWindowBlurProgress() const
1110     {
1111         return windowBlurProgress_;
1112     }
1113 
1114     WindowBlurStyle GetWindowBlurStyle() const
1115     {
1116         return windowBlurStyle_;
1117     }
1118 
1119     // Indicate how much size the decoration taken, excluding the content size.
1120     Size GetOccupiedSize(double dipScale) const;
1121     double HorizontalSpaceOccupied(double dipScale) const;
1122     double VerticalSpaceOccupied(double dipScale) const;
1123 
1124     Offset GetOffset(double dipScale) const;
1125 
1126 private:
1127     bool hasBorderImageSource_ = false;
1128     bool hasBorderImageSlice_ = false;
1129     bool hasBorderImageWidth_ = false;
1130     bool hasBorderImageOutset_ = false;
1131     bool hasBorderImageRepeat_ = false;
1132     bool hasBorderImageGradient_ = false;
1133 
1134     // padding is zero
1135     Edge padding_;
1136     // border contains black color and 1.0f thickness as default
1137     Border border_;
1138     // shadow vector is empty
1139     std::vector<Shadow> shadows_;
1140     Dimension grayScale_;
1141     // Brightness (1.0 as default), range = (0, 2)
1142     Dimension brightness_ = 1.0_px;
1143     // hueRotate
1144     float hueRotate_ = 0.0f;
1145     // Contrast (1.0 as default), complete gray at 0
1146     Dimension contrast_ = 1.0_px;
1147     // Saturate
1148     Dimension saturate_ = 1.0_px;
1149     // Sepia
1150     Dimension sepia_;
1151     // invert
1152     Dimension invert_;
1153     // color is transparent
1154     AnimatableColor backgroundColor_ { Color::TRANSPARENT };
1155     Color animationColor_ = Color::TRANSPARENT;
1156     // Gradient is not implemented
1157     Gradient gradient_ = Gradient();
1158     Gradient gradientBorderImage_ = Gradient();
1159     RefPtr<BackgroundImage> image_;
1160     RefPtr<BorderImage> borderImage_;
1161     RefPtr<ArcBackground> arcBG_;
1162     // Blur radius
1163     AnimatableDimension blurRadius_;
1164     // window blur progress
1165     float windowBlurProgress_ = 0.0f;
1166     // window blur style;
1167     WindowBlurStyle windowBlurStyle_ = WindowBlurStyle::STYLE_BACKGROUND_SMALL_LIGHT;
1168     Color colorBlend;
1169 };
1170 
1171 class Pattern final {
1172 public:
1173     bool IsValid() const
1174     {
1175         return !imgSrc_.empty();
1176     }
1177 
1178     const std::string& GetImgSrc() const
1179     {
1180         return imgSrc_;
1181     }
1182 
1183     void SetImgSrc(const std::string& imgSrc)
1184     {
1185         imgSrc_ = imgSrc;
1186     }
1187 
1188     const std::string& GetRepetition() const
1189     {
1190         return repetition_;
1191     }
1192 
1193     void SetRepetition(const std::string& repetition)
1194     {
1195         repetition_ = repetition;
1196     }
1197 
1198     double GetImageWidth() const
1199     {
1200         return imageWidth_;
1201     }
1202 
1203     void SetImageWidth(double imageWidth)
1204     {
1205         imageWidth_ = imageWidth;
1206     }
1207 
1208     double GetImageHeight() const
1209     {
1210         return imageHeight_;
1211     }
1212 
1213     void SetImageHeight(double imageHeight)
1214     {
1215         imageHeight_ = imageHeight;
1216     }
1217 
1218 private:
1219     double imageWidth_ = 0.0;
1220     double imageHeight_ = 0.0;
1221     std::string imgSrc_;
1222     std::string repetition_;
1223 };
1224 
1225 enum class PathCmd {
1226     CMDS,
1227     TRANSFORM,
1228     MOVE_TO,
1229     LINE_TO,
1230     ARC,
1231     ARC_TO,
1232     QUADRATIC_CURVE_TO,
1233     BEZIER_CURVE_TO,
1234     ELLIPSE,
1235     RECT,
1236     CLOSE_PATH,
1237 };
1238 
1239 struct PathArgs {
1240     std::string cmds;
1241     double para1 = 0.0;
1242     double para2 = 0.0;
1243     double para3 = 0.0;
1244     double para4 = 0.0;
1245     double para5 = 0.0;
1246     double para6 = 0.0;
1247     double para7 = 0.0;
1248     double para8 = 0.0;
1249 };
1250 
1251 class ACE_EXPORT CanvasPath2D : virtual public AceType {
1252     DECLARE_ACE_TYPE(CanvasPath2D, AceType)
1253 public:
1254     CanvasPath2D() = default;
1255     ~CanvasPath2D() = default;
1256     explicit CanvasPath2D(const std::string& cmds);
1257     explicit CanvasPath2D(const RefPtr<CanvasPath2D>& path);
1258     void AddPath(const RefPtr<CanvasPath2D>& path);
1259     void SetTransform(double a, double b, double c, double d, double e, double f);
1260     void MoveTo(double x, double y);
1261     void LineTo(double x, double y);
1262     void Arc(double x, double y, double radius, double startAngle, double endAngle, double ccw);
1263     void ArcTo(double x1, double y1, double x2, double y2, double radius);
1264     void QuadraticCurveTo(double cpx, double cpy, double x, double y);
1265     void BezierCurveTo(double cp1x, double cp1y, double cp2x, double cp2y, double x, double y);
1266     void Ellipse(double x, double y, double radiusX, double radiusY, double rotation, double startAngle,
1267         double endAngle, double ccw);
1268     void Rect(double x, double y, double width, double height);
1269     void ClosePath();
1270     const std::vector<std::pair<PathCmd, PathArgs>>& GetCaches() const;
1271     std::string ToString() const;
1272 
1273 private:
1274     std::vector<std::pair<PathCmd, PathArgs>> caches_;
1275 };
1276 
1277 } // namespace OHOS::Ace
1278 
1279 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_BASE_PROPERTIES_DECORATION_H
1280