Commit 1a003b2d authored by HeinrichAD's avatar HeinrichAD
Browse files

Maker rendering does not get right position.

As long as the functions AdjustForViewBoxWidth and
AdjustForViewBoxHeight are not fully developed pay attention that the
ViewBox width and height will not mismatch the positions of the marker
element then one of them are zero.

This changes are based on Issue #212 from mater.

Add: Quick solution to avoid  ViewBox width or height zero calculation.
Add: UnitTests
parent 18d4a960
......@@ -133,9 +133,14 @@ namespace Svg
/// <param name="pMarkerPoint2"></param>
public void RenderMarker(ISvgRenderer pRenderer, SvgVisualElement pOwner, PointF pRefPoint, PointF pMarkerPoint1, PointF pMarkerPoint2)
{
float fAngle1 = 0f;
if (Orient.IsAuto)
{
// Only calculate this if needed.
float xDiff = pMarkerPoint2.X - pMarkerPoint1.X;
float yDiff = pMarkerPoint2.Y - pMarkerPoint1.Y;
float fAngle1 = (float)(Math.Atan2(yDiff, xDiff) * 180.0 / Math.PI);
fAngle1 = (float)(Math.Atan2(yDiff, xDiff) * 180.0 / Math.PI);
}
RenderPart2(fAngle1, pRenderer, pOwner, pRefPoint);
}
......@@ -184,10 +189,22 @@ namespace Svg
switch (MarkerUnits)
{
case SvgMarkerUnits.StrokeWidth:
if (ViewBox.Width > 0 && ViewBox.Height > 0)
{
transMatrix.Translate(AdjustForViewBoxWidth(-RefX.ToDeviceValue(pRenderer, UnitRenderingType.Horizontal, this) *
pOwner.StrokeWidth.ToDeviceValue(pRenderer, UnitRenderingType.Other, this)),
AdjustForViewBoxHeight(-RefY.ToDeviceValue(pRenderer, UnitRenderingType.Vertical, this) *
pOwner.StrokeWidth.ToDeviceValue(pRenderer, UnitRenderingType.Other, this)));
}
else
{
// SvgMarkerUnits.UserSpaceOnUse
// TODO: We know this isn't correct.
// But use this until the TODOs from AdjustForViewBoxWidth and AdjustForViewBoxHeight are done.
// MORE see Unit Test "MakerEndTest.TestArrowCodeCreation()"
transMatrix.Translate(-RefX.ToDeviceValue(pRenderer, UnitRenderingType.Horizontal, this),
-RefY.ToDeviceValue(pRenderer, UnitRenderingType.Vertical, this));
}
break;
case SvgMarkerUnits.UserSpaceOnUse:
transMatrix.Translate(-RefX.ToDeviceValue(pRenderer, UnitRenderingType.Horizontal, this),
......
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Svg.DataTypes;
using System;
using System.Diagnostics;
using System.Drawing;
using System.IO;
namespace Svg.UnitTests
{
[TestClass]
public class MarkerEndTest : SvgTestHelper
{
protected override string TestResource { get { return GetFullResourceString("Issue212_MakerEnd.OperatingPlan.svg"); } }
protected override int ExpectedSize { get { return 5410; } }
[TestMethod]
public void TestOperatingPlanRendering()
{
LoadSvg(GetXMLDocFromResource());
}
[TestMethod]
public void TestArrowCodeCreation()
{
const int width = 50;
const int height = 50;
var document = new SvgDocument()
{
ID = "svgMap",
ViewBox = new SvgViewBox(0, 0, width, height)
};
var defsElement = new SvgDefinitionList() { ID = "defsMap" };
document.Children.Add(defsElement);
var groupElement = new SvgGroup() { ID = "gMap" };
document.Children.Add(groupElement);
var arrowPath = new SvgPath()
{
ID = "pathMarkerArrow",
Fill = new SvgColourServer(Color.Black),
PathData = SvgPathBuilder.Parse(@"M0,0 L4,2 L0,4 L1,2 z")
};
var arrowMarker = new SvgMarker()
{
ID = "markerArrow",
MarkerUnits = SvgMarkerUnits.StrokeWidth,
MarkerWidth = 5,
MarkerHeight = 5,
RefX = 3,
RefY = 2,
Orient = new SvgOrient() { IsAuto = true },
Children = { arrowPath }
};
defsElement.Children.Add(arrowMarker);
var line = new SvgLine()
{
ID = "lineLinkedPoint",
StartX = 0,
StartY = 15,
EndX = 35,
EndY = 35,
Stroke = new SvgColourServer(Color.Black),
StrokeWidth = 3,
MarkerEnd = new Uri(string.Format("url(#{0})", arrowMarker.ID), UriKind.Relative)
};
groupElement.Children.Add(line);
var svgXml = document.GetXML();
var img = document.Draw();
var file = Path.Combine(Path.GetTempPath(), Guid.NewGuid().ToString());
File.WriteAllText(file + ".svg", svgXml);
img.Save(file + ".png");
Debug.WriteLine(string.Format("Svg saved to '{0}'", file));
Debugger.Break();
// Remove
var svg = new FileInfo(file + ".svg");
if (svg.Exists) svg.Delete();
var png = new FileInfo(file + ".png");
if (png.Exists) png.Delete();
}
}
}
<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd">
<svg x="0" y="0" width="100%" height="100%" overflow="inherit" viewBox="0, 0, 265, 265" preserveAspectRatio="xMidYMid" font-size="0" id="svgmap" xml:space="default" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" xmlns:xml="http://www.w3.org/XML/1998/namespace" version="1.1">
<defs id="defsmap" xml:space="default">
<marker refX="3" refY="2" orient="auto" overflow="hidden" markerWidth="5" markerHeight="5" id="markerArrowLightBlue" xml:space="default">
<path d="M0 0 L4 2 L0 4 L1 2 z" id="pathMarkerArrowLightBlue" xml:space="default" style="fill:#ADD8E6;" />
</marker>
<marker refX="3" refY="2" orient="auto" overflow="hidden" markerWidth="5" markerHeight="5" id="markerArrowLightCoral" xml:space="default">
<path d="M0 0 L4 2 L0 4 L1 2 z" id="pathMarkerArrowLightCoral" xml:space="default" style="fill:#F08080;" />
</marker>
</defs>
<g id="gmap" xml:space="default">
<rect x="80" y="10" width="35" height="35" rx="7" ry="7" stroke="#ADD8E6" id="rectRoomX1Y0" xml:space="default" style="fill:#ADD8E6;" />
<line x1="78" y1="47" x2="49" y2="76" marker-end="url(#markerArrowLightBlue)" stroke="#ADD8E6" stroke-width="3" id="lineLinkedRoomX0Y1" xml:space="default" />
<rect x="150" y="10" width="35" height="35" rx="7" ry="7" stroke="#20B2AA" id="rectRoomX2Y0" xml:space="default" style="fill:#20B2AA;" />
<line x1="167.5" y1="47" x2="167.5" y2="76" marker-end="url(#markerArrowLightBlue)" stroke="#ADD8E6" stroke-width="3" id="lineLinkedRoomX2Y1" xml:space="default" />
<rect x="10" y="80" width="35" height="35" rx="7" ry="7" stroke="#ADD8E6" id="rectRoomX0Y1" xml:space="default" style="fill:#ADD8E6;" />
<line x1="47" y1="117" x2="76" y2="146" marker-end="url(#markerArrowLightBlue)" stroke="#ADD8E6" stroke-width="3" id="lineLinkedRoomX1Y2" xml:space="default" />
<rect x="80" y="80" width="35" height="35" rx="7" ry="7" stroke="#ADD8E6" id="rectRoomX1Y1" xml:space="default" style="fill:#ADD8E6;" />
<line x1="97.5" y1="78" x2="97.5" y2="49" marker-end="url(#markerArrowLightBlue)" stroke="#ADD8E6" stroke-width="3" id="lineLinkedRoomX1Y0" xml:space="default" />
<rect x="150" y="80" width="35" height="35" rx="7" ry="7" stroke="#ADD8E6" id="rectRoomX2Y1" xml:space="default" style="fill:#ADD8E6;" />
<line x1="148" y1="97.5" x2="119" y2="97.5" marker-end="url(#markerArrowLightBlue)" stroke="#ADD8E6" stroke-width="3" id="lineLinkedRoomX1Y1" xml:space="default" />
<rect x="220" y="80" width="35" height="35" rx="7" ry="7" stroke="#ADD8E6" id="rectRoomX3Y1" xml:space="default" style="fill:#ADD8E6;" />
<line x1="237.5" y1="117" x2="237.5" y2="146" marker-end="url(#markerArrowLightCoral)" stroke="#F08080" stroke-width="3" id="lineLinkedRoomX3Y2" xml:space="default" />
<rect x="10" y="150" width="35" height="35" rx="7" ry="7" stroke="#ADD8E6" id="rectRoomX0Y2" xml:space="default" style="fill:#ADD8E6;" />
<line x1="47" y1="187" x2="76" y2="216" marker-end="url(#markerArrowLightBlue)" stroke="#ADD8E6" stroke-width="3" id="lineLinkedRoomX1Y3" xml:space="default" />
<rect x="80" y="150" width="35" height="35" rx="7" ry="7" stroke="#ADD8E6" id="rectRoomX1Y2" xml:space="default" style="fill:#ADD8E6;" />
<line x1="78" y1="167.5" x2="49" y2="167.5" marker-end="url(#markerArrowLightBlue)" stroke="#ADD8E6" stroke-width="3" id="lineLinkedRoomX0Y2" xml:space="default" />
<rect x="150" y="150" width="35" height="35" rx="7" ry="7" stroke="#ADD8E6" id="rectRoomX2Y2" xml:space="default" style="fill:#ADD8E6;" />
<line x1="187" y1="148" x2="216" y2="119" marker-end="url(#markerArrowLightBlue)" stroke="#ADD8E6" stroke-width="3" id="lineLinkedRoomX3Y1" xml:space="default" />
<rect x="220" y="150" width="35" height="35" rx="7" ry="7" stroke="#F08080" id="rectRoomX3Y2" xml:space="default" style="fill:#F08080;" />
<rect x="80" y="220" width="35" height="35" rx="7" ry="7" stroke="#ADD8E6" id="rectRoomX1Y3" xml:space="default" style="fill:#ADD8E6;" />
<line x1="117" y1="218" x2="146" y2="189" marker-end="url(#markerArrowLightBlue)" stroke="#ADD8E6" stroke-width="3" id="lineLinkedRoomX2Y2" xml:space="default" />
</g>
</svg>
\ No newline at end of file
......@@ -55,6 +55,7 @@
</ItemGroup>
<ItemGroup>
<Compile Include="CssQueryTest.cs" />
<Compile Include="MarkerEndTest.cs" />
<Compile Include="MetafileRenderingTest.cs" />
<Compile Include="MultiThreadingTest.cs" />
<Compile Include="PrivateFontsTests.cs" />
......@@ -77,6 +78,9 @@
<ItemGroup>
<EmbeddedResource Include="Resources\Issue210_Metafile\3DSceneSnapshotBIG.svg" />
</ItemGroup>
<ItemGroup>
<EmbeddedResource Include="Resources\Issue212_MakerEnd\OperatingPlan.svg" />
</ItemGroup>
<Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" />
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
Other similar extension points exist, see Microsoft.Common.targets.
......
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