SvgPolygon.cs 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. using System;
  2. using System.Drawing.Drawing2D;
  3. using System.Diagnostics;
  4. using Svg.ExtensionMethods;
  5. #pragma warning disable
  6. namespace Svg
  7. {
  8. /// <summary>
  9. /// SvgPolygon defines a closed shape consisting of a set of connected straight line segments.
  10. /// </summary>
  11. [SvgElement("polygon")]
  12. public class SvgPolygon : SvgPathBasedElement
  13. {
  14. private GraphicsPath _path;
  15. /// <summary>
  16. /// The points that make up the SvgPolygon
  17. /// </summary>
  18. [SvgAttribute("points")]
  19. public SvgPointCollection Points
  20. {
  21. get { return this.Attributes["points"] as SvgPointCollection; }
  22. set { this.Attributes["points"] = value; this.IsPathDirty = true; }
  23. }
  24. /// <summary>
  25. /// Gets or sets the marker (end cap) of the path.
  26. /// </summary>
  27. [SvgAttribute("marker-end")]
  28. public virtual Uri MarkerEnd
  29. {
  30. get { return this.Attributes.GetAttribute<Uri>("marker-end").ReplaceWithNullIfNone(); }
  31. set { this.Attributes["marker-end"] = value; }
  32. }
  33. /// <summary>
  34. /// Gets or sets the marker (start cap) of the path.
  35. /// </summary>
  36. [SvgAttribute("marker-mid")]
  37. public virtual Uri MarkerMid
  38. {
  39. get { return this.Attributes.GetAttribute<Uri>("marker-mid").ReplaceWithNullIfNone(); }
  40. set { this.Attributes["marker-mid"] = value; }
  41. }
  42. /// <summary>
  43. /// Gets or sets the marker (start cap) of the path.
  44. /// </summary>
  45. [SvgAttribute("marker-start")]
  46. public virtual Uri MarkerStart
  47. {
  48. get { return this.Attributes.GetAttribute<Uri>("marker-start").ReplaceWithNullIfNone(); }
  49. set { this.Attributes["marker-start"] = value; }
  50. }
  51. public override GraphicsPath Path(ISvgRenderer renderer)
  52. {
  53. if (this._path == null || this.IsPathDirty)
  54. {
  55. this._path = new GraphicsPath();
  56. this._path.StartFigure();
  57. try
  58. {
  59. var points = this.Points;
  60. for (int i = 2; (i + 1) < points.Count; i += 2)
  61. {
  62. var endPoint = SvgUnit.GetDevicePoint(points[i], points[i + 1], renderer, this);
  63. // If it is to render, don't need to consider stroke width.
  64. // i.e stroke width only to be considered when calculating boundary
  65. if (renderer == null)
  66. {
  67. var radius = base.StrokeWidth / 2;
  68. _path.AddEllipse(endPoint.X - radius, endPoint.Y - radius, 2 * radius, 2 * radius);
  69. continue;
  70. }
  71. //first line
  72. if (_path.PointCount == 0)
  73. {
  74. _path.AddLine(SvgUnit.GetDevicePoint(points[i - 2], points[i - 1], renderer, this), endPoint);
  75. }
  76. else
  77. {
  78. _path.AddLine(_path.GetLastPoint(), endPoint);
  79. }
  80. }
  81. }
  82. catch
  83. {
  84. Trace.TraceError("Error parsing points");
  85. }
  86. this._path.CloseFigure();
  87. if (renderer != null)
  88. this.IsPathDirty = false;
  89. }
  90. return this._path;
  91. }
  92. /// <summary>
  93. /// Renders the stroke of the <see cref="SvgVisualElement"/> to the specified <see cref="ISvgRenderer"/>
  94. /// </summary>
  95. /// <param name="renderer">The <see cref="ISvgRenderer"/> object to render to.</param>
  96. protected internal override bool RenderStroke(ISvgRenderer renderer)
  97. {
  98. var result = base.RenderStroke(renderer);
  99. var path = this.Path(renderer);
  100. if (this.MarkerStart != null)
  101. {
  102. SvgMarker marker = this.OwnerDocument.GetElementById<SvgMarker>(this.MarkerStart.ToString());
  103. marker.RenderMarker(renderer, this, path.PathPoints[0], path.PathPoints[0], path.PathPoints[1]);
  104. }
  105. if (this.MarkerMid != null)
  106. {
  107. SvgMarker marker = this.OwnerDocument.GetElementById<SvgMarker>(this.MarkerMid.ToString());
  108. for (int i = 1; i <= path.PathPoints.Length - 2; i++)
  109. marker.RenderMarker(renderer, this, path.PathPoints[i], path.PathPoints[i - 1], path.PathPoints[i], path.PathPoints[i + 1]);
  110. }
  111. if (this.MarkerEnd != null)
  112. {
  113. SvgMarker marker = this.OwnerDocument.GetElementById<SvgMarker>(this.MarkerEnd.ToString());
  114. marker.RenderMarker(renderer, this, path.PathPoints[path.PathPoints.Length - 1], path.PathPoints[path.PathPoints.Length - 2], path.PathPoints[path.PathPoints.Length - 1]);
  115. }
  116. return result;
  117. }
  118. public override SvgElement DeepCopy()
  119. {
  120. return DeepCopy<SvgPolygon>();
  121. }
  122. public override SvgElement DeepCopy<T>()
  123. {
  124. var newObj = base.DeepCopy<T>() as SvgPolygon;
  125. newObj.Points = new SvgPointCollection();
  126. foreach (var pt in this.Points)
  127. newObj.Points.Add(pt);
  128. return newObj;
  129. }
  130. }
  131. }
  132. #pragma warning restore