LinearGradientStyleValue.h 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  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/Angle.h>
  12. #include <LibWeb/CSS/Percentage.h>
  13. #include <LibWeb/CSS/StyleValues/AbstractImageStyleValue.h>
  14. #include <LibWeb/Painting/GradientPainting.h>
  15. namespace Web::CSS {
  16. // Note: The sides must be before the corners in this enum (as this order is used in parsing).
  17. enum class SideOrCorner {
  18. Top,
  19. Bottom,
  20. Left,
  21. Right,
  22. TopLeft,
  23. TopRight,
  24. BottomLeft,
  25. BottomRight
  26. };
  27. class LinearGradientStyleValue final : public AbstractImageStyleValue {
  28. public:
  29. using GradientDirection = Variant<Angle, SideOrCorner>;
  30. enum class GradientType {
  31. Standard,
  32. WebKit
  33. };
  34. static ValueComparingNonnullRefPtr<LinearGradientStyleValue> create(GradientDirection direction, Vector<LinearColorStopListElement> color_stop_list, GradientType type, GradientRepeating repeating)
  35. {
  36. VERIFY(color_stop_list.size() >= 2);
  37. return adopt_ref(*new LinearGradientStyleValue(direction, move(color_stop_list), type, repeating));
  38. }
  39. virtual ErrorOr<String> to_string() const override;
  40. virtual ~LinearGradientStyleValue() override = default;
  41. virtual bool equals(StyleValue const& other) const override;
  42. Vector<LinearColorStopListElement> const& color_stop_list() const
  43. {
  44. return m_properties.color_stop_list;
  45. }
  46. bool is_repeating() const { return m_properties.repeating == GradientRepeating::Yes; }
  47. float angle_degrees(CSSPixelSize gradient_size) const;
  48. void resolve_for_size(Layout::Node const&, CSSPixelSize) const override;
  49. bool is_paintable() const override { return true; }
  50. void paint(PaintContext& context, DevicePixelRect const& dest_rect, CSS::ImageRendering image_rendering) const override;
  51. private:
  52. LinearGradientStyleValue(GradientDirection direction, Vector<LinearColorStopListElement> color_stop_list, GradientType type, GradientRepeating repeating)
  53. : AbstractImageStyleValue(Type::LinearGradient)
  54. , m_properties { .direction = direction, .color_stop_list = move(color_stop_list), .gradient_type = type, .repeating = repeating }
  55. {
  56. }
  57. struct Properties {
  58. GradientDirection direction;
  59. Vector<LinearColorStopListElement> color_stop_list;
  60. GradientType gradient_type;
  61. GradientRepeating repeating;
  62. bool operator==(Properties const&) const = default;
  63. } m_properties;
  64. struct ResolvedData {
  65. Painting::LinearGradientData data;
  66. CSSPixelSize size;
  67. };
  68. mutable Optional<ResolvedData> m_resolved;
  69. };
  70. }