SvgPolyline.cs 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4. using System.Drawing;
  5. using System.Drawing.Drawing2D;
  6. using System.Diagnostics;
  7. using Svg.ExtensionMethods;
  8. #pragma warning disable
  9. namespace Svg
  10. {
  11. /// <summary>
  12. /// SvgPolyline defines a set of connected straight line segments. Typically, <see cref="SvgPolyline"/> defines open shapes.
  13. /// </summary>
  14. [SvgElement("polyline")]
  15. public class SvgPolyline : SvgPolygon
  16. {
  17. /// <summary>
  18. /// Gets or sets the marker (end cap) of the path.
  19. /// </summary>
  20. [SvgAttribute("marker-end")]
  21. public override Uri MarkerEnd
  22. {
  23. get { return this.Attributes.GetAttribute<Uri>("marker-end").ReplaceWithNullIfNone(); }
  24. set { this.Attributes["marker-end"] = value; }
  25. }
  26. /// <summary>
  27. /// Gets or sets the marker (start cap) of the path.
  28. /// </summary>
  29. [SvgAttribute("marker-mid")]
  30. public override Uri MarkerMid
  31. {
  32. get { return this.Attributes.GetAttribute<Uri>("marker-mid").ReplaceWithNullIfNone(); }
  33. set { this.Attributes["marker-mid"] = value; }
  34. }
  35. /// <summary>
  36. /// Gets or sets the marker (start cap) of the path.
  37. /// </summary>
  38. [SvgAttribute("marker-start")]
  39. public override Uri MarkerStart
  40. {
  41. get { return this.Attributes.GetAttribute<Uri>("marker-start").ReplaceWithNullIfNone(); }
  42. set { this.Attributes["marker-start"] = value; }
  43. }
  44. private GraphicsPath _Path;
  45. public override GraphicsPath Path(ISvgRenderer renderer)
  46. {
  47. if ((_Path == null || this.IsPathDirty) && base.StrokeWidth > 0)
  48. {
  49. _Path = new GraphicsPath();
  50. try
  51. {
  52. for (int i = 0; (i + 1) < Points.Count; i += 2)
  53. {
  54. PointF endPoint = new PointF(Points[i].ToDeviceValue(renderer, UnitRenderingType.Horizontal, this),
  55. Points[i + 1].ToDeviceValue(renderer, UnitRenderingType.Vertical, this));
  56. if (renderer == null)
  57. {
  58. var radius = base.StrokeWidth / 2;
  59. _Path.AddEllipse(endPoint.X - radius, endPoint.Y - radius, 2 * radius, 2 * radius);
  60. continue;
  61. }
  62. // TODO: Remove unrequired first line
  63. if (_Path.PointCount == 0)
  64. {
  65. _Path.AddLine(endPoint, endPoint);
  66. }
  67. else
  68. {
  69. _Path.AddLine(_Path.GetLastPoint(), endPoint);
  70. }
  71. }
  72. }
  73. catch (Exception exc)
  74. {
  75. Trace.TraceError("Error rendering points: " + exc.Message);
  76. }
  77. if (renderer != null)
  78. this.IsPathDirty = false;
  79. }
  80. return _Path;
  81. }
  82. /// <summary>
  83. /// Renders the stroke of the <see cref="SvgVisualElement"/> to the specified <see cref="ISvgRenderer"/>
  84. /// </summary>
  85. /// <param name="renderer">The <see cref="ISvgRenderer"/> object to render to.</param>
  86. protected internal override bool RenderStroke(ISvgRenderer renderer)
  87. {
  88. var result = base.RenderStroke(renderer);
  89. var path = this.Path(renderer);
  90. if (this.MarkerStart != null)
  91. {
  92. SvgMarker marker = this.OwnerDocument.GetElementById<SvgMarker>(this.MarkerStart.ToString());
  93. marker.RenderMarker(renderer, this, path.PathPoints[0], path.PathPoints[0], path.PathPoints[1]);
  94. }
  95. if (this.MarkerMid != null)
  96. {
  97. SvgMarker marker = this.OwnerDocument.GetElementById<SvgMarker>(this.MarkerMid.ToString());
  98. for (int i = 1; i <= path.PathPoints.Length - 2; i++)
  99. marker.RenderMarker(renderer, this, path.PathPoints[i], path.PathPoints[i - 1], path.PathPoints[i], path.PathPoints[i + 1]);
  100. }
  101. if (this.MarkerEnd != null)
  102. {
  103. SvgMarker marker = this.OwnerDocument.GetElementById<SvgMarker>(this.MarkerEnd.ToString());
  104. marker.RenderMarker(renderer, this, path.PathPoints[path.PathPoints.Length - 1], path.PathPoints[path.PathPoints.Length - 2], path.PathPoints[path.PathPoints.Length - 1]);
  105. }
  106. return result;
  107. }
  108. }
  109. }
  110. #pragma warning restore