SVGGradientElement.h 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. /*
  2. * Copyright (c) 2023, MacDue <macdue@dueutil.tech>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #pragma once
  7. #include <AK/IterationDecision.h>
  8. #include <LibGfx/PaintStyle.h>
  9. #include <LibWeb/SVG/AttributeParser.h>
  10. #include <LibWeb/SVG/SVGElement.h>
  11. #include <LibWeb/SVG/SVGStopElement.h>
  12. namespace Web::SVG {
  13. struct SVGPaintContext {
  14. Gfx::FloatRect viewport;
  15. Gfx::FloatRect path_bounding_box;
  16. Gfx::AffineTransform transform;
  17. };
  18. inline Gfx::SVGGradientPaintStyle::SpreadMethod to_gfx_spread_method(SpreadMethod spread_method)
  19. {
  20. switch (spread_method) {
  21. case SpreadMethod::Pad:
  22. return Gfx::SVGGradientPaintStyle::SpreadMethod::Pad;
  23. case SpreadMethod::Reflect:
  24. return Gfx::SVGGradientPaintStyle::SpreadMethod::Reflect;
  25. case SpreadMethod::Repeat:
  26. return Gfx::SVGGradientPaintStyle::SpreadMethod::Repeat;
  27. default:
  28. VERIFY_NOT_REACHED();
  29. }
  30. }
  31. class SVGGradientElement : public SVGElement {
  32. WEB_PLATFORM_OBJECT(SVGGradientElement, SVGElement);
  33. public:
  34. virtual ~SVGGradientElement() override = default;
  35. virtual void attribute_changed(FlyString const& name, Optional<String> const& value) override;
  36. virtual Optional<Gfx::PaintStyle const&> to_gfx_paint_style(SVGPaintContext const&) const = 0;
  37. GradientUnits gradient_units() const;
  38. SpreadMethod spread_method() const;
  39. Optional<Gfx::AffineTransform> gradient_transform() const;
  40. protected:
  41. SVGGradientElement(DOM::Document&, DOM::QualifiedName);
  42. virtual void initialize(JS::Realm&) override;
  43. JS::GCPtr<SVGGradientElement const> linked_gradient() const;
  44. Gfx::AffineTransform gradient_paint_transform(SVGPaintContext const&) const;
  45. template<VoidFunction<SVGStopElement> Callback>
  46. void for_each_color_stop(Callback const& callback) const
  47. {
  48. bool color_stops_found = false;
  49. for_each_child_of_type<SVG::SVGStopElement>([&](auto& stop) {
  50. color_stops_found = true;
  51. callback(stop);
  52. });
  53. if (!color_stops_found) {
  54. if (auto gradient = linked_gradient())
  55. gradient->for_each_color_stop(callback);
  56. }
  57. }
  58. void add_color_stops(Gfx::SVGGradientPaintStyle&) const;
  59. private:
  60. Optional<GradientUnits> m_gradient_units = {};
  61. Optional<SpreadMethod> m_spread_method = {};
  62. Optional<Gfx::AffineTransform> m_gradient_transform = {};
  63. };
  64. }