Skip to content

Commit

Permalink
Correctly handle embedded SVG fragments without viewbox
Browse files Browse the repository at this point in the history
- calculate svg element bounds considering child svg fragments
- consider svg fragment offset in bounds / dimensions
- do not create view box if none is defined
- fixes svg-net#244
  • Loading branch information
mrbean-bremen committed Mar 10, 2017
1 parent 2f426fa commit 52b7d32
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 4 deletions.
34 changes: 30 additions & 4 deletions Source/Document Structure/SvgFragment.cs
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,34 @@ public RectangleF Bounds
{
get
{
return this.Path.GetBounds();
var bounds = new RectangleF();
foreach (var child in this.Children)
{
RectangleF childBounds = new RectangleF();
if (child is SvgFragment)
{
childBounds = ((SvgFragment)child).Bounds;
childBounds.Offset(((SvgFragment)child).X, ((SvgFragment)child).Y);
}
else if (child is SvgVisualElement)
{
childBounds = ((SvgVisualElement)child).Bounds;
}

if (!childBounds.IsEmpty)
{
if (bounds.IsEmpty)
{
bounds = childBounds;
}
else
{
bounds = RectangleF.Union(bounds, childBounds);
}
}
}

return bounds;
}
}

Expand Down Expand Up @@ -244,21 +271,20 @@ public SizeF GetDimensions()
else
{
bounds = this.Bounds; //do just one call to the recursive bounds property
this.ViewBox = new SvgViewBox(bounds.X, bounds.Y, bounds.Width, bounds.Height);
}
}

if (isWidthperc)
{
w = (bounds.Width) * (Width.Value * 0.01f);
w = (bounds.Width + bounds.X) * (Width.Value * 0.01f);
}
else
{
w = Width.ToDeviceValue(null, UnitRenderingType.Horizontal, this);
}
if (isHeightperc)
{
h = (bounds.Height) * (Height.Value * 0.01f);
h = (bounds.Height + bounds.Y) * (Height.Value * 0.01f);
}
else
{
Expand Down
Binary file added Tests/W3CTestSuite/png/__issue-244-01.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified Tests/W3CTestSuite/png/__issue-279-01.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
11 changes: 11 additions & 0 deletions Tests/W3CTestSuite/svg/__issue-244-01.svg
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 52b7d32

Please sign in to comment.