SvgMarker.cs 7.67 KB
Newer Older
1
2
3
4
5
6
7
8
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.Xml;
using System.Xml.Serialization;
using System.Drawing.Drawing2D;
9
10
using System.Drawing;
using Svg.DataTypes;
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
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

namespace Svg
{
    [SvgElement("marker")]
    public class SvgMarker : SvgVisualElement, ISvgViewPort
    {
    	private SvgOrient _svgOrient = new SvgOrient();

        [SvgAttribute("refX")]
        public virtual SvgUnit RefX
        {
            get { return this.Attributes.GetAttribute<SvgUnit>("refX"); }
            set { this.Attributes["refX"] = value; }
        }

        [SvgAttribute("refY")]
        public virtual SvgUnit RefY
        {
            get { return this.Attributes.GetAttribute<SvgUnit>("refY"); }
            set { this.Attributes["refY"] = value; }
        }


		[SvgAttribute("orient")]
		public virtual SvgOrient Orient
		{
			get { return _svgOrient; }
			set { _svgOrient = value; }
		}


		[SvgAttribute("overflow")]
		public virtual SvgOverflow Overflow
		{
			get { return this.Attributes.GetAttribute<SvgOverflow>("overflow"); }
			set { this.Attributes["overflow"] = value; }
		}


		[SvgAttribute("viewBox")]
		public virtual SvgViewBox ViewBox
		{
			get { return this.Attributes.GetAttribute<SvgViewBox>("viewBox"); }
			set { this.Attributes["viewBox"] = value; }
		}


		[SvgAttribute("preserveAspectRatio")]
		public virtual SvgAspectRatio AspectRatio
		{
			get { return this.Attributes.GetAttribute<SvgAspectRatio>("preserveAspectRatio"); }
			set { this.Attributes["preserveAspectRatio"] = value; }
		}


66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
		[SvgAttribute("markerWidth")]
		public virtual SvgUnit MarkerWidth
		{
			get { return this.Attributes.GetAttribute<SvgUnit>("markerWidth"); }
			set { this.Attributes["markerWidth"] = value; }
		}

		[SvgAttribute("markerHeight")]
		public virtual SvgUnit MarkerHeight
		{
			get { return this.Attributes.GetAttribute<SvgUnit>("markerHeight"); }
			set { this.Attributes["markerHeight"] = value; }
		}

		public SvgMarker()
		{
			MarkerUnits = SvgMarkerUnits.strokeWidth;
			MarkerHeight = 3;
			MarkerWidth = 3;
			Overflow = SvgOverflow.hidden;
		}

88
89
90
91
92
93
94
95
96
		public override System.Drawing.Drawing2D.GraphicsPath Path
        {
            get
            {
            	var path = this.Children.FirstOrDefault(x => x is SvgPath);
				if (path != null)
	            	return (path as SvgPath).Path;
				return null;
            }
97
98
99
100
            protected set
            {
                // No-op
            }
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
        }

        public override System.Drawing.RectangleF Bounds
        {
            get
            {
            	var path = this.Path;
				if (path != null)
					return path.GetBounds();
				return new System.Drawing.RectangleF();
            }
        }

		public override SvgElement DeepCopy()
		{
			return DeepCopy<SvgMarker>();
		}

		public override SvgElement DeepCopy<T>()
		{
			var newObj = base.DeepCopy<T>() as SvgMarker;
			newObj.RefX = this.RefX;
			newObj.RefY = this.RefY;
			newObj.Orient = this.Orient;
			newObj.ViewBox = this.ViewBox;
			newObj.Overflow = this.Overflow;
			newObj.AspectRatio = this.AspectRatio;

			return newObj;
		}
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274

		/// <summary>
		/// Render this marker using the slope of the given line segment
		/// </summary>
		/// <param name="pRenderer"></param>
		/// <param name="pOwner"></param>
		/// <param name="pMarkerPoint1"></param>
		/// <param name="pMarkerPoint2"></param>
		public void RenderMarker(SvgRenderer pRenderer, SvgPath pOwner, PointF pRefPoint, PointF pMarkerPoint1, PointF pMarkerPoint2)
		{
			float xDiff = pMarkerPoint2.X - pMarkerPoint1.X;
			float yDiff = pMarkerPoint2.Y - pMarkerPoint1.Y;
			float fAngle1 = (float)(Math.Atan2(yDiff, xDiff) * 180.0 / Math.PI);

			RenderPart2(fAngle1, pRenderer, pOwner, pRefPoint);
		}

		/// <summary>
		/// Render this marker using the average of the slopes of the two given line segments
		/// </summary>
		/// <param name="pRenderer"></param>
		/// <param name="pOwner"></param>
		/// <param name="pMarkerPoint1"></param>
		/// <param name="pMarkerPoint2"></param>
		/// <param name="pMarkerPoint3"></param>
		public void RenderMarker(SvgRenderer pRenderer, SvgPath pOwner, PointF pRefPoint, PointF pMarkerPoint1, PointF pMarkerPoint2, PointF pMarkerPoint3)
		{
			float xDiff = pMarkerPoint2.X - pMarkerPoint1.X;
			float yDiff = pMarkerPoint2.Y - pMarkerPoint1.Y;
			float fAngle1 = (float)(Math.Atan2(yDiff, xDiff) * 180.0 / Math.PI);

			xDiff = pMarkerPoint3.X - pMarkerPoint2.X;
			yDiff = pMarkerPoint3.Y - pMarkerPoint2.Y;
			float fAngle2 = (float)(Math.Atan2(yDiff, xDiff) * 180.0 / Math.PI);

			RenderPart2((fAngle1 + fAngle2) / 2, pRenderer, pOwner, pRefPoint);
		}

		/// <summary>
		/// Common code for rendering a marker once the orientation angle has been calculated
		/// </summary>
		/// <param name="fAngle"></param>
		/// <param name="pRenderer"></param>
		/// <param name="pOwner"></param>
		/// <param name="pMarkerPoint"></param>
		private void RenderPart2(float fAngle, SvgRenderer pRenderer, SvgPath pOwner, PointF pMarkerPoint)
		{
			Pen pRenderPen = CreatePen(pOwner);

			GraphicsPath markerPath = GetClone(pOwner);

			Matrix transMatrix = new Matrix();
			transMatrix.Translate(pMarkerPoint.X, pMarkerPoint.Y);
			if (Orient.IsAuto)
				transMatrix.Rotate(fAngle);
			else
				transMatrix.Rotate(Orient.Angle);
			switch (MarkerUnits)
			{
				case SvgMarkerUnits.strokeWidth:
					transMatrix.Translate(AdjustForViewBoxWidth(-RefX * pOwner.StrokeWidth), AdjustForViewBoxHeight(-RefY * pOwner.StrokeWidth));
					break;
				case SvgMarkerUnits.userSpaceOnUse:
					transMatrix.Translate(-RefX, -RefY);
					break;
			}
			markerPath.Transform(transMatrix);
			pRenderer.DrawPath(pRenderPen, markerPath);

			SvgPaintServer pFill = Fill;
			SvgFillRule pFillRule = FillRule;								// TODO: What do we use the fill rule for?
			float fOpacity = FillOpacity;

			if (pFill != null)
			{
				Brush pBrush = pFill.GetBrush(this, fOpacity);
				pRenderer.FillPath(pBrush, markerPath);
				pBrush.Dispose();
			}
			pRenderPen.Dispose();
			markerPath.Dispose();
			transMatrix.Dispose();
		}

		/// <summary>
		/// Create a pen that can be used to render this marker
		/// </summary>
		/// <param name="pStroke"></param>
		/// <returns></returns>
		private Pen CreatePen(SvgPath pPath)
		{
			Brush pBrush = pPath.Stroke.GetBrush(this, Opacity);
			switch (MarkerUnits)
			{
				case SvgMarkerUnits.strokeWidth:
					return (new Pen(pBrush, StrokeWidth * pPath.StrokeWidth));
				case SvgMarkerUnits.userSpaceOnUse:
					return (new Pen(pBrush, StrokeWidth));
			}
			return (new Pen(pBrush, StrokeWidth));
		}

		/// <summary>
		/// Get a clone of the current path, scaled for the stroke with
		/// </summary>
		/// <returns></returns>
		private GraphicsPath GetClone(SvgPath pPath)
		{
			GraphicsPath pRet = Path.Clone() as GraphicsPath;
			switch (MarkerUnits)
			{
				case SvgMarkerUnits.strokeWidth:
					Matrix transMatrix = new Matrix();
					transMatrix.Scale(AdjustForViewBoxWidth(pPath.StrokeWidth), AdjustForViewBoxHeight(pPath.StrokeWidth));
					pRet.Transform(transMatrix);
					break;
				case SvgMarkerUnits.userSpaceOnUse:
					break;
			}
			return (pRet);
		}

		/// <summary>
		/// Adjust the given value to account for the width of the viewbox in the viewport
		/// </summary>
		/// <param name="fWidth"></param>
		/// <returns></returns>
		private float AdjustForViewBoxWidth(float fWidth)
		{
			//	TODO: We know this isn't correct
			return (fWidth / ViewBox.Width);
		}

		/// <summary>
		/// Adjust the given value to account for the height of the viewbox in the viewport
		/// </summary>
		/// <param name="fWidth"></param>
		/// <returns></returns>
		private float AdjustForViewBoxHeight(float fHeight)
		{
			//	TODO: We know this isn't correct
			return (fHeight / ViewBox.Height);
		}
	}
275
}