LinearGradientStyleValue.h 2.7 KB

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