RadialGradientStyleValue.h 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. /*
  2. * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
  3. * Copyright (c) 2021, Tobias Christiansen <tobyase@serenityos.org>
  4. * Copyright (c) 2021-2023, Sam Atkins <atkinssj@serenityos.org>
  5. * Copyright (c) 2022-2023, MacDue <macdue@dueutil.tech>
  6. *
  7. * SPDX-License-Identifier: BSD-2-Clause
  8. */
  9. #pragma once
  10. #include <AK/Vector.h>
  11. #include <LibWeb/CSS/Enums.h>
  12. #include <LibWeb/CSS/Position.h>
  13. #include <LibWeb/CSS/StyleValues/AbstractImageStyleValue.h>
  14. #include <LibWeb/Painting/GradientPainting.h>
  15. namespace Web::CSS {
  16. class RadialGradientStyleValue final : public AbstractImageStyleValue {
  17. public:
  18. enum class EndingShape {
  19. Circle,
  20. Ellipse
  21. };
  22. enum class Extent {
  23. ClosestCorner,
  24. ClosestSide,
  25. FarthestCorner,
  26. FarthestSide
  27. };
  28. struct CircleSize {
  29. Length radius;
  30. bool operator==(CircleSize const&) const = default;
  31. };
  32. struct EllipseSize {
  33. LengthPercentage radius_a;
  34. LengthPercentage radius_b;
  35. bool operator==(EllipseSize const&) const = default;
  36. };
  37. using Size = Variant<Extent, CircleSize, EllipseSize>;
  38. static ValueComparingNonnullRefPtr<RadialGradientStyleValue> create(EndingShape ending_shape, Size size, PositionValue position, Vector<LinearColorStopListElement> color_stop_list, GradientRepeating repeating)
  39. {
  40. VERIFY(color_stop_list.size() >= 2);
  41. return adopt_ref(*new (nothrow) RadialGradientStyleValue(ending_shape, size, position, move(color_stop_list), repeating));
  42. }
  43. virtual String to_string() const override;
  44. void paint(PaintContext&, DevicePixelRect const& dest_rect, CSS::ImageRendering) const override;
  45. virtual bool equals(StyleValue const& other) const override;
  46. Vector<LinearColorStopListElement> const& color_stop_list() const
  47. {
  48. return m_properties.color_stop_list;
  49. }
  50. bool is_paintable() const override { return true; }
  51. void resolve_for_size(Layout::NodeWithStyleAndBoxModelMetrics const&, CSSPixelSize) const override;
  52. CSSPixelSize resolve_size(Layout::Node const&, CSSPixelPoint, CSSPixelRect const&) const;
  53. bool is_repeating() const { return m_properties.repeating == GradientRepeating::Yes; }
  54. virtual ~RadialGradientStyleValue() override = default;
  55. private:
  56. RadialGradientStyleValue(EndingShape ending_shape, Size size, PositionValue position, Vector<LinearColorStopListElement> color_stop_list, GradientRepeating repeating)
  57. : AbstractImageStyleValue(Type::RadialGradient)
  58. , m_properties { .ending_shape = ending_shape, .size = size, .position = position, .color_stop_list = move(color_stop_list), .repeating = repeating }
  59. {
  60. }
  61. struct Properties {
  62. EndingShape ending_shape;
  63. Size size;
  64. PositionValue position;
  65. Vector<LinearColorStopListElement> color_stop_list;
  66. GradientRepeating repeating;
  67. bool operator==(Properties const&) const = default;
  68. } m_properties;
  69. struct ResolvedData {
  70. Painting::RadialGradientData data;
  71. CSSPixelSize gradient_size;
  72. CSSPixelPoint center;
  73. };
  74. mutable Optional<ResolvedData> m_resolved;
  75. };
  76. }