SVG icon indicating copy to clipboard operation
SVG copied to clipboard

Document Bounds with ViewBox?

Open hakakou opened this issue 3 years ago • 1 comments

Description

Shouldn't an element's Bounds be relative to the document's ViewBox? Consider the following test that fails:

Example data

        [Test]
        public void TestBounds()
        {
            var tem = new SvgDocument();
            tem.ViewBox = new SvgViewBox(1000, 0, 1000, 1000);
            tem.Width = 1000;
            tem.Height = 1000;

            var rect = new SvgRectangle()
            {
                X = 100,
                Y = 100,
                Width = 500,
                Height = 500,
                Fill = new SvgColourServer(Color.Black)
            };
            tem.Children.Add(rect);

            Assert.IsFalse(tem.Bounds.IntersectsWith(rect.Bounds));
        }

This produces the following SVG, so I would assume rect does not intersect with document's bounds.

image

Used Versions

3.3.0

hakakou avatar Oct 12 '21 09:10 hakakou

OK now I realized that Document.Bounds simply returns the bounds of the children elements. To get the bounds of the visible area as a rectangle, something like this is probably needed:

        public static RectangleF VisibleArea(this SvgDocument doc)
        {
            if (doc.ViewBox != null && doc.ViewBox.Width > 0 && doc.ViewBox.Height > 0)
                return new RectangleF(doc.ViewBox.MinX, doc.ViewBox.MinY, doc.ViewBox.Width, doc.ViewBox.Height);
            else
            {
                var size = new SizeF(SvgUnitToPixels(doc.Width, doc.Height);
                return new RectangleF(new PointF(0, 0), size);
            }
        }

hakakou avatar Oct 12 '21 11:10 hakakou