SVG icon indicating copy to clipboard operation
SVG copied to clipboard

Incorrect bounds of transformed SVG group

Open lukastrm opened this issue 3 years ago • 1 comments

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

lukastrm avatar Jan 24 '22 10:01 lukastrm

Please fix this bug. In my project i have many math operations on SVG, and many fails because bounds calculates incorrectly.

DmitrijOkeanij avatar Jan 11 '24 21:01 DmitrijOkeanij