SVGGradientElement.h 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  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. class SVGGradientElement : public SVGElement {
  19. WEB_PLATFORM_OBJECT(SVGGradientElement, SVGElement);
  20. public:
  21. virtual ~SVGGradientElement() override = default;
  22. virtual void attribute_changed(DeprecatedFlyString const& name, DeprecatedString const& value) override;
  23. virtual Optional<Gfx::PaintStyle const&> to_gfx_paint_style(SVGPaintContext const&) const = 0;
  24. GradientUnits gradient_units() const;
  25. Optional<Gfx::AffineTransform> gradient_transform() const;
  26. protected:
  27. SVGGradientElement(DOM::Document&, DOM::QualifiedName);
  28. virtual JS::ThrowCompletionOr<void> initialize(JS::Realm&) override;
  29. JS::GCPtr<SVGGradientElement const> xlink_href() const;
  30. Gfx::AffineTransform gradient_paint_transform(SVGPaintContext const&) const;
  31. template<VoidFunction<SVGStopElement> Callback>
  32. void for_each_color_stop(Callback const& callback) const
  33. {
  34. bool color_stops_found = false;
  35. for_each_child_of_type<SVG::SVGStopElement>([&](auto& stop) {
  36. color_stops_found = true;
  37. callback(stop);
  38. });
  39. if (!color_stops_found) {
  40. if (auto href = xlink_href())
  41. href->for_each_color_stop(callback);
  42. }
  43. }
  44. void add_color_stops(Gfx::SVGGradientPaintStyle&) const;
  45. private:
  46. Optional<GradientUnits> m_gradient_units = {};
  47. Optional<Gfx::AffineTransform> m_gradient_transform = {};
  48. };
  49. }