MarkerEndTest.cs 3.13 KB
Newer Older
1
2
3
4
5
6
7
8
9
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Svg.DataTypes;
using System;
using System.Diagnostics;
using System.Drawing;
using System.IO;

namespace Svg.UnitTests
{
10
11
12
13
14
15
16
17
18

    /// <summary>
    /// Test Class of rendering SVGs with marker-end elements.
    /// Based on Issue 212.
    /// </summary>
    /// <remarks>
    /// Test use the following embedded resources:
    ///   - Issue212_MakerEnd\OperatingPlan.svg
    /// </remarks>
19
20
21
22
23
    [TestClass]
    public class MarkerEndTest : SvgTestHelper
    {

        protected override string TestResource { get { return GetFullResourceString("Issue212_MakerEnd.OperatingPlan.svg"); } }
24
        protected override int ExpectedSize { get { return 5000; } } //5321 //5410
25
26
27
28
29
30
31
32
33
34
35
36


        [TestMethod]
        public void TestOperatingPlanRendering()
        {
            LoadSvg(GetXMLDocFromResource());
        }


        [TestMethod]
        public void TestArrowCodeCreation()
        {
37
            // Sample code from Issue 212. Thanks to podostro.
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
            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();
        }

    }
}