Commit fa748a18 authored by mrbean-bremen's avatar mrbean-bremen Committed by mrbean-bremen
Browse files

Consider transformation for all svg element bounds

- fixes #331
parent 731a69e4
......@@ -88,9 +88,12 @@ namespace Svg
/// <value>The bounds.</value>
public override RectangleF Bounds
{
get { return new RectangleF(this.Location.ToDeviceValue(null, this),
new SizeF(this.Width.ToDeviceValue(null, UnitRenderingType.Horizontal, this),
this.Height.ToDeviceValue(null, UnitRenderingType.Vertical, this))); }
get
{
return TransformedBounds(new RectangleF(this.Location.ToDeviceValue(null, this),
new SizeF(this.Width.ToDeviceValue(null, UnitRenderingType.Horizontal, this),
this.Height.ToDeviceValue(null, UnitRenderingType.Vertical, this))));
}
}
/// <summary>
......
......@@ -238,7 +238,7 @@ namespace Svg
}
}
return bounds;
return TransformedBounds(bounds);
}
}
......
......@@ -47,8 +47,7 @@ namespace Svg
}
}
}
return r;
return TransformedBounds(r);
}
}
......
......@@ -52,7 +52,7 @@ namespace Svg
}
}
return r;
return TransformedBounds(r);
}
}
......
......@@ -74,7 +74,7 @@ namespace Svg.Document_Structure
}
}
return r;
return TransformedBounds(r);
}
}
......
......@@ -52,7 +52,7 @@ namespace Svg
}
}
return r;
return TransformedBounds(r);
}
}
......
......@@ -101,8 +101,14 @@ namespace Svg
{
var path = this.Path(null);
if (path != null)
{
if (Transforms != null && Transforms.Count > 0)
{
path.Transform(Transforms.GetMatrix());
}
return path.GetBounds();
return new System.Drawing.RectangleF();
}
return new RectangleF();
}
}
......
......@@ -362,6 +362,24 @@ namespace Svg
}
}
/// <summary>
/// Transforms the given rectangle with the set transformation, if any.
/// Can be applied to bounds calculated without considering the element transformation.
/// </summary>
/// <param name="bounds">The rectangle to be transformed.</param>
/// <returns>The transformed rectangle, or the original rectangle if no transformation exists.</returns>
protected RectangleF TransformedBounds(RectangleF bounds)
{
if (Transforms != null && Transforms.Count > 0)
{
var path = new GraphicsPath();
path.AddRectangle(bounds);
path.Transform(Transforms.GetMatrix());
return path.GetBounds();
}
return bounds;
}
/// <summary>
/// Gets or sets the ID of the element.
/// </summary>
......
......@@ -220,7 +220,11 @@ namespace Svg
{
path.AddPath(elem.Path(null), false);
}
return path.GetBounds();
if (Transforms != null && Transforms.Count > 0)
{
path.Transform(Transforms.GetMatrix());
}
return path.GetBounds();
}
}
......
......@@ -45,6 +45,24 @@ namespace Svg.UnitTests
AssertEqualBounds("rects", -50f, 9.5f, 80, 50.5f);
}
[TestMethod]
public void TestTranslatedGroupBounds()
{
AssertEqualBounds("lines-translated", -12.5f, -7, 31, 86);
}
[TestMethod]
public void TestScaledGroupBounds()
{
AssertEqualBounds("lines-scaled", 19, -81, 62, 172);
}
[TestMethod]
public void TestRotatedGroupBounds()
{
AssertEqualBounds("lines-rotated", -45.5f, 9.5f, 86, 31);
}
private void AssertEqualBounds(string elementId, float x, float y, float width, float height)
{
const float Epsilon = 0.01f;
......
......@@ -6,6 +6,21 @@
<line id="line-xlate" x1="10" x2="30" y1="20" y2="40" transform="translate(5, 5)" />
<line id="line-xform" x1="10" x2="30" y1="20" y2="40" transform="rotate(180), translate(-50, 0)" />
</g>
<g id="lines-translated" transform="translate(-22, 33.5)">
<line id="line" x1="10" x2="30" y1="20" y2="40" />
<line id="line-xlate" x1="10" x2="30" y1="20" y2="40" transform="translate(5, 5)" />
<line id="line-xform" x1="10" x2="30" y1="20" y2="40" transform="rotate(180), translate(-50, 0)" />
</g>
<g id="lines-rotated" transform="rotate(90)">
<line id="line" x1="10" x2="30" y1="20" y2="40" />
<line id="line-xlate" x1="10" x2="30" y1="20" y2="40" transform="translate(5, 5)" />
<line id="line-xform" x1="10" x2="30" y1="20" y2="40" transform="rotate(180), translate(-50, 0)" />
</g>
<g id="lines-scaled" transform="scale(2)">
<line id="line" x1="10" x2="30" y1="20" y2="40" />
<line id="line-xlate" x1="10" x2="30" y1="20" y2="40" transform="translate(5, 5)" />
<line id="line-xform" x1="10" x2="30" y1="20" y2="40" transform="rotate(180), translate(-50, 0)" />
</g>
<g id="rects">
<rect id="rect" x="10" y="30" width="10" height="20" />
<rect id="rect-xlate" x="10" y="30" width="10" height="20" transform="translate(10, 10)" />
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment