SvgLine.cs 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. using System;
  2. using System.Drawing;
  3. using System.Drawing.Drawing2D;
  4. using Svg.ExtensionMethods;
  5. #pragma warning disable
  6. namespace Svg
  7. {
  8. /// <summary>
  9. /// Represents and SVG line element.
  10. /// </summary>
  11. [SvgElement("line")]
  12. public class SvgLine : SvgPathBasedElement
  13. {
  14. private SvgUnit _startX;
  15. private SvgUnit _startY;
  16. private SvgUnit _endX;
  17. private SvgUnit _endY;
  18. private GraphicsPath _path;
  19. [SvgAttribute("x1")]
  20. public SvgUnit StartX
  21. {
  22. get { return this._startX; }
  23. set
  24. {
  25. if (_startX != value)
  26. {
  27. this._startX = value;
  28. this.IsPathDirty = true;
  29. OnAttributeChanged(new AttributeEventArgs { Attribute = "x1", Value = value });
  30. }
  31. }
  32. }
  33. [SvgAttribute("y1")]
  34. public SvgUnit StartY
  35. {
  36. get { return this._startY; }
  37. set
  38. {
  39. if (_startY != value)
  40. {
  41. this._startY = value;
  42. this.IsPathDirty = true;
  43. OnAttributeChanged(new AttributeEventArgs { Attribute = "y1", Value = value });
  44. }
  45. }
  46. }
  47. [SvgAttribute("x2")]
  48. public SvgUnit EndX
  49. {
  50. get { return this._endX; }
  51. set
  52. {
  53. if (_endX != value)
  54. {
  55. this._endX = value;
  56. this.IsPathDirty = true;
  57. OnAttributeChanged(new AttributeEventArgs { Attribute = "x2", Value = value });
  58. }
  59. }
  60. }
  61. [SvgAttribute("y2")]
  62. public SvgUnit EndY
  63. {
  64. get { return this._endY; }
  65. set
  66. {
  67. if (_endY != value)
  68. {
  69. this._endY = value;
  70. this.IsPathDirty = true;
  71. OnAttributeChanged(new AttributeEventArgs { Attribute = "y2", Value = value });
  72. }
  73. }
  74. }
  75. /// <summary>
  76. /// Gets or sets the marker (end cap) of the path.
  77. /// </summary>
  78. [SvgAttribute("marker-end")]
  79. public Uri MarkerEnd
  80. {
  81. get { return this.Attributes.GetAttribute<Uri>("marker-end").ReplaceWithNullIfNone(); }
  82. set { this.Attributes["marker-end"] = value; }
  83. }
  84. /// <summary>
  85. /// Gets or sets the marker (start cap) of the path.
  86. /// </summary>
  87. [SvgAttribute("marker-mid")]
  88. public Uri MarkerMid
  89. {
  90. get { return this.Attributes.GetAttribute<Uri>("marker-mid").ReplaceWithNullIfNone(); }
  91. set { this.Attributes["marker-mid"] = value; }
  92. }
  93. /// <summary>
  94. /// Gets or sets the marker (start cap) of the path.
  95. /// </summary>
  96. [SvgAttribute("marker-start")]
  97. public Uri MarkerStart
  98. {
  99. get { return this.Attributes.GetAttribute<Uri>("marker-start").ReplaceWithNullIfNone(); }
  100. set { this.Attributes["marker-start"] = value; }
  101. }
  102. public override SvgPaintServer Fill
  103. {
  104. get { return null; /* Line can't have a fill */ }
  105. set
  106. {
  107. // Do nothing
  108. }
  109. }
  110. public SvgLine()
  111. {
  112. }
  113. public override System.Drawing.Drawing2D.GraphicsPath Path(ISvgRenderer renderer)
  114. {
  115. if ((this._path == null || this.IsPathDirty) && base.StrokeWidth > 0)
  116. {
  117. PointF start = new PointF(this.StartX.ToDeviceValue(renderer, UnitRenderingType.Horizontal, this),
  118. this.StartY.ToDeviceValue(renderer, UnitRenderingType.Vertical, this));
  119. PointF end = new PointF(this.EndX.ToDeviceValue(renderer, UnitRenderingType.Horizontal, this),
  120. this.EndY.ToDeviceValue(renderer, UnitRenderingType.Vertical, this));
  121. this._path = new GraphicsPath();
  122. // If it is to render, don't need to consider stroke width.
  123. // i.e stroke width only to be considered when calculating boundary
  124. if (renderer != null)
  125. {
  126. this._path.AddLine(start, end);
  127. this.IsPathDirty = false;
  128. }
  129. else
  130. { // only when calculating boundary
  131. _path.StartFigure();
  132. var radius = base.StrokeWidth / 2;
  133. _path.AddEllipse(start.X - radius, start.Y - radius, 2 * radius, 2 * radius);
  134. _path.AddEllipse(end.X - radius, end.Y - radius, 2 * radius, 2 * radius);
  135. _path.CloseFigure();
  136. }
  137. }
  138. return this._path;
  139. }
  140. /// <summary>
  141. /// Renders the stroke of the <see cref="SvgVisualElement"/> to the specified <see cref="ISvgRenderer"/>
  142. /// </summary>
  143. /// <param name="renderer">The <see cref="ISvgRenderer"/> object to render to.</param>
  144. protected internal override bool RenderStroke(ISvgRenderer renderer)
  145. {
  146. var result = base.RenderStroke(renderer);
  147. var path = this.Path(renderer);
  148. if (this.MarkerStart != null)
  149. {
  150. SvgMarker marker = this.OwnerDocument.GetElementById<SvgMarker>(this.MarkerStart.ToString());
  151. marker.RenderMarker(renderer, this, path.PathPoints[0], path.PathPoints[0], path.PathPoints[1]);
  152. }
  153. if (this.MarkerMid != null)
  154. {
  155. SvgMarker marker = this.OwnerDocument.GetElementById<SvgMarker>(this.MarkerMid.ToString());
  156. for (int i = 1; i <= path.PathPoints.Length - 2; i++)
  157. marker.RenderMarker(renderer, this, path.PathPoints[i], path.PathPoints[i - 1], path.PathPoints[i], path.PathPoints[i + 1]);
  158. }
  159. if (this.MarkerEnd != null)
  160. {
  161. SvgMarker marker = this.OwnerDocument.GetElementById<SvgMarker>(this.MarkerEnd.ToString());
  162. marker.RenderMarker(renderer, this, path.PathPoints[path.PathPoints.Length - 1], path.PathPoints[path.PathPoints.Length - 2], path.PathPoints[path.PathPoints.Length - 1]);
  163. }
  164. return result;
  165. }
  166. public override SvgElement DeepCopy()
  167. {
  168. return DeepCopy<SvgLine>();
  169. }
  170. public override SvgElement DeepCopy<T>()
  171. {
  172. var newObj = base.DeepCopy<T>() as SvgLine;
  173. newObj.StartX = this.StartX;
  174. newObj.EndX = this.EndX;
  175. newObj.StartY = this.StartY;
  176. newObj.EndY = this.EndY;
  177. if (this.Fill != null)
  178. newObj.Fill = this.Fill.DeepCopy() as SvgPaintServer;
  179. return newObj;
  180. }
  181. }
  182. }
  183. #pragma warning restore