1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-05-31 10:48:11 +00:00

LibWeb: Resolve and paint SVG gradient fills

This bit is mostly ad-hoc for now. This simply turns fill: url(#grad1)
into document().get_element_by_id('grad1') then resolves the gradient.
This seems to do the trick for most use cases, but this is not
attempting to follow the spec yet to keep things simple.
This commit is contained in:
MacDue 2023-04-23 01:31:17 +01:00 committed by Andreas Kling
parent aa3464466e
commit afd355c135
6 changed files with 97 additions and 17 deletions

View file

@ -8,8 +8,10 @@
#include <LibWeb/Bindings/Intrinsics.h>
#include <LibWeb/CSS/Parser/Parser.h>
#include <LibWeb/DOM/Document.h>
#include <LibWeb/Layout/Node.h>
#include <LibWeb/SVG/AttributeParser.h>
#include <LibWeb/SVG/SVGGradientElement.h>
#include <LibWeb/SVG/SVGGraphicsElement.h>
#include <LibWeb/SVG/SVGSVGElement.h>
@ -40,6 +42,23 @@ void SVGGraphicsElement::parse_attribute(DeprecatedFlyString const& name, Deprec
}
}
Optional<Gfx::PaintStyle const&> SVGGraphicsElement::fill_paint_style(SVGPaintContext const& paint_context) const
{
// FIXME: This entire function is an ad-hoc hack:
if (!layout_node())
return {};
auto& fill = layout_node()->computed_values().fill();
if (!fill.has_value() || !fill->is_url())
return {};
auto& url = fill->as_url();
auto maybe_gradient = document().get_element_by_id(url.fragment());
if (is<SVG::SVGGradientElement>(*maybe_gradient)) {
auto& gradient = verify_cast<SVG::SVGGradientElement>(*maybe_gradient);
return gradient.to_gfx_paint_style(paint_context);
}
return {};
}
Gfx::AffineTransform transform_from_transform_list(ReadonlySpan<Transform> transform_list)
{
Gfx::AffineTransform affine_transform;
@ -111,8 +130,10 @@ Optional<Gfx::Color> SVGGraphicsElement::fill_color() const
return {};
// FIXME: In the working-draft spec, `fill` is intended to be a shorthand, with `fill-color`
// being what we actually want to use. But that's not final or widely supported yet.
return layout_node()->computed_values().fill().map([&](Gfx::Color color) {
return color.with_alpha(m_fill_opacity.value_or(1) * 255);
return layout_node()->computed_values().fill().map([&](auto& paint) -> Gfx::Color {
if (!paint.is_color())
return Color::Black;
return paint.as_color().with_alpha(m_fill_opacity.value_or(1) * 255);
});
}
@ -122,7 +143,11 @@ Optional<Gfx::Color> SVGGraphicsElement::stroke_color() const
return {};
// FIXME: In the working-draft spec, `stroke` is intended to be a shorthand, with `stroke-color`
// being what we actually want to use. But that's not final or widely supported yet.
return layout_node()->computed_values().stroke();
return layout_node()->computed_values().stroke().map([](auto& paint) -> Gfx::Color {
if (!paint.is_color())
return Color::Black;
return paint.as_color();
});
}
Optional<float> SVGGraphicsElement::stroke_width() const