LinearGradientStyleValue.h 2.7 KB

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