1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-02 23:42:13 +00:00

LibWeb: Apply SVG transform to path when painting (SVG) elements

This also combines the viewbox mapping into the same transform and
reuses some code by using Path::copy_transformed() rather than manually
mapping each segment of the path.
This commit is contained in:
MacDue 2023-04-10 12:32:55 +01:00 committed by Andreas Kling
parent 3484db0dc1
commit cf23a2b82d
3 changed files with 27 additions and 67 deletions

View file

@ -17,27 +17,6 @@ SVGGeometryBox::SVGGeometryBox(DOM::Document& document, SVG::SVGGeometryElement&
{
}
float SVGGeometryBox::viewbox_scaling() const
{
auto* svg_box = dom_node().first_ancestor_of_type<SVG::SVGSVGElement>();
if (!svg_box || !svg_box->view_box().has_value())
return 1;
auto view_box = svg_box->view_box().value();
bool has_specified_width = svg_box->has_attribute(HTML::AttributeNames::width);
auto specified_width = paint_box()->content_width().value();
bool has_specified_height = svg_box->has_attribute(HTML::AttributeNames::height);
auto specified_height = paint_box()->content_height().value();
auto scale_width = has_specified_width ? specified_width / view_box.width : 1;
auto scale_height = has_specified_height ? specified_height / view_box.height : 1;
return min(scale_width, scale_height);
}
CSSPixelPoint SVGGeometryBox::viewbox_origin() const
{
auto* svg_box = dom_node().first_ancestor_of_type<SVG::SVGSVGElement>();
@ -46,6 +25,28 @@ CSSPixelPoint SVGGeometryBox::viewbox_origin() const
return { svg_box->view_box().value().min_x, svg_box->view_box().value().min_y };
}
Gfx::AffineTransform SVGGeometryBox::layout_transform() const
{
auto& geometry_element = dom_node();
auto transform = geometry_element.get_transform();
auto* svg_box = geometry_element.first_ancestor_of_type<SVG::SVGSVGElement>();
float scaling = 1;
auto origin = viewbox_origin().to_type<float>();
Gfx::FloatPoint paint_offset = {};
if (svg_box && svg_box->view_box().has_value()) {
// Note: SVGFormattingContext has already done the scaling based on the viewbox,
// we now have to derive what it was from the original bounding box size.
// FIXME: It would be nice if we could store the transform from layout somewhere, so we don't have to solve for it here.
auto scaled_width = paint_box()->content_width().value();
auto scaled_height = paint_box()->content_height().value();
auto original_bounding_box = Gfx::AffineTransform {}.translate(-origin).multiply(transform).map(const_cast<SVG::SVGGeometryElement&>(geometry_element).get_path().bounding_box());
scaling = min(scaled_width / original_bounding_box.width(), scaled_height / original_bounding_box.height());
auto scaled_bounding_box = original_bounding_box.scaled(scaling, scaling);
paint_offset = (paint_box()->absolute_rect().location() - svg_box->paint_box()->absolute_rect().location()).to_type<float>() - scaled_bounding_box.location();
}
return Gfx::AffineTransform {}.translate(paint_offset).scale(scaling, scaling).translate(-origin).multiply(transform);
}
JS::GCPtr<Painting::Paintable> SVGGeometryBox::create_paintable() const
{
return Painting::SVGGeometryPaintable::create(*this);