Skip to content

Incorrect bounds of transformed SVG group #946

Open
@lukastrm

Description

@lukastrm

Description

SvgGroup.Bounds does not return the correct bounds if transformations have been applied to the SVG group.
The reason for this is that SvgGroup.Bounds first determines the correct bounds of all child elements and then applies its transformations to these bounds (via SvgElement.TransformedBounds(RectangleF bounds) instead of first applying its transformations to the child elements and determining the bounds afterwards. As a result the returned bounds are the bounds of a transformed rectangle but not the bounds of the contained elements.

Example data

In this example we see a circle in an SVG group. The SVG group has a rotation of 45 degrees around the center of the circle. The group bounds should be tight around the circle but instead the bounding rectangle of the circle is rotated 45 degrees and then the group bounds refer to that, thus making it larger than it acutally should be.

bounds

public void WrongBounds() {
    var svgCircle = new SvgCircle {
        CenterX = 200, CenterY = 200, Radius = 100, Fill = new SvgColourServer(Color.Gray)
    };

    var svgGroup = new SvgGroup {
        Children = {svgCircle}, Transforms = new SvgTransformCollection {new SvgRotate(45, 200, 200)}
    };

    var bounds = svgGroup.Bounds;

    var boundsSvgRectangle = new SvgRectangle {
        X = bounds.X,
        Y = bounds.Y,
        Width = bounds.Width,
        Height = bounds.Height,
        Stroke = new SvgColourServer(Color.Red),
        Fill = new SvgColourServer(Color.Transparent)
    };

    var document = new SvgDocument();
    document.Children.Add(svgGroup);
    document.Children.Add(boundsSvgRectangle);
    // ...
}

Used Versions

.NET 5.0
SVG.NET 3.4.0

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions