1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-27 04:17:35 +00:00

LibWeb: Implement SVGGradientElement

This is the base class for all SVG gradient types. This supports:

- The `gradientUnits` attribute
- The `gradientTransform` attribute
- And following `xlink:hrefs` for inheriting <stops>/attributes
This commit is contained in:
MacDue 2023-04-21 18:51:00 +01:00 committed by Andreas Kling
parent a5fa5e55ef
commit 71938550fa
7 changed files with 175 additions and 0 deletions

View file

@ -0,0 +1,59 @@
/*
* Copyright (c) 2023, MacDue <macdue@dueutil.tech>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#pragma once
#include <AK/IterationDecision.h>
#include <LibGfx/PaintStyle.h>
#include <LibWeb/SVG/AttributeParser.h>
#include <LibWeb/SVG/SVGElement.h>
#include <LibWeb/SVG/SVGStopElement.h>
namespace Web::SVG {
struct SVGPaintContext {
Gfx::FloatRect viewport;
Gfx::FloatRect path_bounding_box;
Gfx::AffineTransform transform;
};
class SVGGradientElement : public SVGElement {
WEB_PLATFORM_OBJECT(SVGGradientElement, SVGElement);
public:
virtual ~SVGGradientElement() override = default;
virtual void parse_attribute(DeprecatedFlyString const& name, DeprecatedString const& value) override;
virtual Optional<Gfx::PaintStyle const&> to_gfx_paint_style(SVGPaintContext const&) const = 0;
GradientUnits gradient_units() const;
Optional<Gfx::AffineTransform> gradient_transform() const;
protected:
SVGGradientElement(DOM::Document&, DOM::QualifiedName);
virtual JS::ThrowCompletionOr<void> initialize(JS::Realm&) override;
JS::GCPtr<SVGGradientElement const> xlink_href() const;
template<VoidFunction<SVGStopElement> Callback>
void for_each_color_stop(Callback const& callback) const
{
for_each_child_of_type<SVG::SVGStopElement>([&](auto& stop) {
callback(stop);
});
if (auto href = xlink_href())
href->for_each_color_stop(callback);
}
private:
Optional<GradientUnits> m_gradient_units = {};
Optional<Gfx::AffineTransform> m_gradient_transform = {};
};
}