RadialGradientStyleValue.h 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. /*
  2. * Copyright (c) 2018-2020, Andreas Kling <andreas@ladybird.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/StyleValues/AbstractImageStyleValue.h>
  13. #include <LibWeb/Painting/GradientPainting.h>
  14. namespace Web::CSS {
  15. class RadialGradientStyleValue final : public AbstractImageStyleValue {
  16. public:
  17. enum class EndingShape {
  18. Circle,
  19. Ellipse
  20. };
  21. enum class Extent {
  22. ClosestCorner,
  23. ClosestSide,
  24. FarthestCorner,
  25. FarthestSide
  26. };
  27. struct CircleSize {
  28. Length radius;
  29. bool operator==(CircleSize const&) const = default;
  30. };
  31. struct EllipseSize {
  32. LengthPercentage radius_a;
  33. LengthPercentage radius_b;
  34. bool operator==(EllipseSize const&) const = default;
  35. };
  36. using Size = Variant<Extent, CircleSize, EllipseSize>;
  37. static ValueComparingNonnullRefPtr<RadialGradientStyleValue> create(EndingShape ending_shape, Size size, ValueComparingNonnullRefPtr<PositionStyleValue> position, Vector<LinearColorStopListElement> color_stop_list, GradientRepeating repeating)
  38. {
  39. VERIFY(color_stop_list.size() >= 2);
  40. return adopt_ref(*new (nothrow) RadialGradientStyleValue(ending_shape, size, move(position), move(color_stop_list), repeating));
  41. }
  42. virtual String to_string() const override;
  43. void paint(PaintContext&, DevicePixelRect const& dest_rect, CSS::ImageRendering) const override;
  44. virtual bool equals(CSSStyleValue const& other) const override;
  45. Vector<LinearColorStopListElement> const& color_stop_list() const
  46. {
  47. return m_properties.color_stop_list;
  48. }
  49. bool is_paintable() const override { return true; }
  50. void resolve_for_size(Layout::NodeWithStyleAndBoxModelMetrics const&, CSSPixelSize) const override;
  51. CSSPixelSize resolve_size(Layout::Node const&, CSSPixelPoint, CSSPixelRect const&) const;
  52. bool is_repeating() const { return m_properties.repeating == GradientRepeating::Yes; }
  53. virtual ~RadialGradientStyleValue() override = default;
  54. private:
  55. RadialGradientStyleValue(EndingShape ending_shape, Size size, ValueComparingNonnullRefPtr<PositionStyleValue> position, Vector<LinearColorStopListElement> color_stop_list, GradientRepeating repeating)
  56. : AbstractImageStyleValue(Type::RadialGradient)
  57. , m_properties { .ending_shape = ending_shape, .size = size, .position = move(position), .color_stop_list = move(color_stop_list), .repeating = repeating }
  58. {
  59. }
  60. struct Properties {
  61. EndingShape ending_shape;
  62. Size size;
  63. ValueComparingNonnullRefPtr<PositionStyleValue> position;
  64. Vector<LinearColorStopListElement> color_stop_list;
  65. GradientRepeating repeating;
  66. bool operator==(Properties const&) const = default;
  67. } m_properties;
  68. struct ResolvedData {
  69. Painting::RadialGradientData data;
  70. CSSPixelSize gradient_size;
  71. CSSPixelPoint center;
  72. };
  73. mutable Optional<ResolvedData> m_resolved;
  74. };
  75. }