SvgMarker.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  1. using System;
  2. using System.Drawing;
  3. using System.Drawing.Drawing2D;
  4. using System.Linq;
  5. using Svg.DataTypes;
  6. #pragma warning disable
  7. namespace Svg
  8. {
  9. [SvgElement("marker")]
  10. public class SvgMarker : SvgVisualElement, ISvgViewPort
  11. {
  12. private SvgOrient _svgOrient = new SvgOrient();
  13. [SvgAttribute("refX")]
  14. public virtual SvgUnit RefX
  15. {
  16. get { return this.Attributes.GetAttribute<SvgUnit>("refX"); }
  17. set { this.Attributes["refX"] = value; }
  18. }
  19. [SvgAttribute("refY")]
  20. public virtual SvgUnit RefY
  21. {
  22. get { return this.Attributes.GetAttribute<SvgUnit>("refY"); }
  23. set { this.Attributes["refY"] = value; }
  24. }
  25. [SvgAttribute("orient")]
  26. public virtual SvgOrient Orient
  27. {
  28. get { return _svgOrient; }
  29. set { _svgOrient = value; }
  30. }
  31. [SvgAttribute("overflow")]
  32. public virtual SvgOverflow Overflow
  33. {
  34. get { return this.Attributes.GetAttribute<SvgOverflow>("overflow"); }
  35. set { this.Attributes["overflow"] = value; }
  36. }
  37. [SvgAttribute("viewBox")]
  38. public virtual SvgViewBox ViewBox
  39. {
  40. get { return this.Attributes.GetAttribute<SvgViewBox>("viewBox"); }
  41. set { this.Attributes["viewBox"] = value; }
  42. }
  43. [SvgAttribute("preserveAspectRatio")]
  44. public virtual SvgAspectRatio AspectRatio
  45. {
  46. get { return this.Attributes.GetAttribute<SvgAspectRatio>("preserveAspectRatio"); }
  47. set { this.Attributes["preserveAspectRatio"] = value; }
  48. }
  49. [SvgAttribute("markerWidth")]
  50. public virtual SvgUnit MarkerWidth
  51. {
  52. get { return this.Attributes.GetAttribute<SvgUnit>("markerWidth"); }
  53. set { this.Attributes["markerWidth"] = value; }
  54. }
  55. [SvgAttribute("markerHeight")]
  56. public virtual SvgUnit MarkerHeight
  57. {
  58. get { return this.Attributes.GetAttribute<SvgUnit>("markerHeight"); }
  59. set { this.Attributes["markerHeight"] = value; }
  60. }
  61. [SvgAttribute("markerUnits")]
  62. public virtual SvgMarkerUnits MarkerUnits
  63. {
  64. get { return this.Attributes.GetAttribute<SvgMarkerUnits>("markerUnits"); }
  65. set { this.Attributes["markerUnits"] = value; }
  66. }
  67. public SvgMarker()
  68. {
  69. MarkerUnits = SvgMarkerUnits.StrokeWidth;
  70. MarkerHeight = 3;
  71. MarkerWidth = 3;
  72. Overflow = SvgOverflow.Hidden;
  73. }
  74. public override System.Drawing.Drawing2D.GraphicsPath Path(ISvgRenderer renderer)
  75. {
  76. var path = this.Children.FirstOrDefault(x => x is SvgVisualElement);
  77. if (path != null)
  78. return (path as SvgVisualElement).Path(renderer);
  79. return null;
  80. }
  81. public override System.Drawing.RectangleF Bounds
  82. {
  83. get
  84. {
  85. var path = this.Path(null);
  86. if (path != null)
  87. return path.GetBounds();
  88. return new System.Drawing.RectangleF();
  89. }
  90. }
  91. public override SvgElement DeepCopy()
  92. {
  93. return DeepCopy<SvgMarker>();
  94. }
  95. public override SvgElement DeepCopy<T>()
  96. {
  97. var newObj = base.DeepCopy<T>() as SvgMarker;
  98. newObj.RefX = this.RefX;
  99. newObj.RefY = this.RefY;
  100. newObj.Orient = this.Orient;
  101. newObj.ViewBox = this.ViewBox;
  102. newObj.Overflow = this.Overflow;
  103. newObj.AspectRatio = this.AspectRatio;
  104. return newObj;
  105. }
  106. /// <summary>
  107. /// Render this marker using the slope of the given line segment
  108. /// </summary>
  109. /// <param name="pRenderer"></param>
  110. /// <param name="pOwner"></param>
  111. /// <param name="pMarkerPoint1"></param>
  112. /// <param name="pMarkerPoint2"></param>
  113. public void RenderMarker(ISvgRenderer pRenderer, SvgVisualElement pOwner, PointF pRefPoint, PointF pMarkerPoint1, PointF pMarkerPoint2)
  114. {
  115. float fAngle1 = 0f;
  116. if (Orient.IsAuto)
  117. {
  118. // Only calculate this if needed.
  119. float xDiff = pMarkerPoint2.X - pMarkerPoint1.X;
  120. float yDiff = pMarkerPoint2.Y - pMarkerPoint1.Y;
  121. fAngle1 = (float)(Math.Atan2(yDiff, xDiff) * 180.0 / Math.PI);
  122. }
  123. RenderPart2(fAngle1, pRenderer, pOwner, pRefPoint);
  124. }
  125. /// <summary>
  126. /// Render this marker using the average of the slopes of the two given line segments
  127. /// </summary>
  128. /// <param name="pRenderer"></param>
  129. /// <param name="pOwner"></param>
  130. /// <param name="pMarkerPoint1"></param>
  131. /// <param name="pMarkerPoint2"></param>
  132. /// <param name="pMarkerPoint3"></param>
  133. public void RenderMarker(ISvgRenderer pRenderer, SvgVisualElement pOwner, PointF pRefPoint, PointF pMarkerPoint1, PointF pMarkerPoint2, PointF pMarkerPoint3)
  134. {
  135. float xDiff = pMarkerPoint2.X - pMarkerPoint1.X;
  136. float yDiff = pMarkerPoint2.Y - pMarkerPoint1.Y;
  137. float fAngle1 = (float)(Math.Atan2(yDiff, xDiff) * 180.0 / Math.PI);
  138. xDiff = pMarkerPoint3.X - pMarkerPoint2.X;
  139. yDiff = pMarkerPoint3.Y - pMarkerPoint2.Y;
  140. float fAngle2 = (float)(Math.Atan2(yDiff, xDiff) * 180.0 / Math.PI);
  141. RenderPart2((fAngle1 + fAngle2) / 2, pRenderer, pOwner, pRefPoint);
  142. }
  143. /// <summary>
  144. /// Common code for rendering a marker once the orientation angle has been calculated
  145. /// </summary>
  146. /// <param name="fAngle"></param>
  147. /// <param name="pRenderer"></param>
  148. /// <param name="pOwner"></param>
  149. /// <param name="pMarkerPoint"></param>
  150. private void RenderPart2(float fAngle, ISvgRenderer pRenderer, SvgVisualElement pOwner, PointF pMarkerPoint)
  151. {
  152. using (var pRenderPen = CreatePen(pOwner, pRenderer))
  153. {
  154. using (var markerPath = GetClone(pOwner))
  155. {
  156. using (var transMatrix = new Matrix())
  157. {
  158. transMatrix.Translate(pMarkerPoint.X, pMarkerPoint.Y);
  159. if (Orient.IsAuto)
  160. transMatrix.Rotate(fAngle);
  161. else
  162. transMatrix.Rotate(Orient.Angle);
  163. switch (MarkerUnits)
  164. {
  165. case SvgMarkerUnits.StrokeWidth:
  166. if (ViewBox.Width > 0 && ViewBox.Height > 0)
  167. {
  168. transMatrix.Translate(AdjustForViewBoxWidth(-RefX.ToDeviceValue(pRenderer, UnitRenderingType.Horizontal, this) *
  169. pOwner.StrokeWidth.ToDeviceValue(pRenderer, UnitRenderingType.Other, this)),
  170. AdjustForViewBoxHeight(-RefY.ToDeviceValue(pRenderer, UnitRenderingType.Vertical, this) *
  171. pOwner.StrokeWidth.ToDeviceValue(pRenderer, UnitRenderingType.Other, this)));
  172. }
  173. else
  174. {
  175. // SvgMarkerUnits.UserSpaceOnUse
  176. // TODO: We know this isn't correct.
  177. // But use this until the TODOs from AdjustForViewBoxWidth and AdjustForViewBoxHeight are done.
  178. // MORE see Unit Test "MakerEndTest.TestArrowCodeCreation()"
  179. transMatrix.Translate(-RefX.ToDeviceValue(pRenderer, UnitRenderingType.Horizontal, this),
  180. -RefY.ToDeviceValue(pRenderer, UnitRenderingType.Vertical, this));
  181. }
  182. break;
  183. case SvgMarkerUnits.UserSpaceOnUse:
  184. transMatrix.Translate(-RefX.ToDeviceValue(pRenderer, UnitRenderingType.Horizontal, this),
  185. -RefY.ToDeviceValue(pRenderer, UnitRenderingType.Vertical, this));
  186. break;
  187. }
  188. markerPath.Transform(transMatrix);
  189. if (pRenderPen != null) pRenderer.DrawPath(pRenderPen, markerPath);
  190. SvgPaintServer pFill = this.Children.First().Fill;
  191. SvgFillRule pFillRule = FillRule; // TODO: What do we use the fill rule for?
  192. float fOpacity = FillOpacity;
  193. if (pFill != null)
  194. {
  195. using (var pBrush = pFill.GetBrush(this, pRenderer, fOpacity))
  196. {
  197. pRenderer.FillPath(pBrush, markerPath);
  198. }
  199. }
  200. }
  201. }
  202. }
  203. }
  204. /// <summary>
  205. /// Create a pen that can be used to render this marker
  206. /// </summary>
  207. /// <param name="pStroke"></param>
  208. /// <returns></returns>
  209. private Pen CreatePen(SvgVisualElement pPath, ISvgRenderer renderer)
  210. {
  211. if (pPath.Stroke == null) return null;
  212. Brush pBrush = pPath.Stroke.GetBrush(this, renderer, Opacity);
  213. switch (MarkerUnits)
  214. {
  215. case SvgMarkerUnits.StrokeWidth:
  216. return (new Pen(pBrush, StrokeWidth.ToDeviceValue(renderer, UnitRenderingType.Other, this) *
  217. pPath.StrokeWidth.ToDeviceValue(renderer, UnitRenderingType.Other, this)));
  218. case SvgMarkerUnits.UserSpaceOnUse:
  219. return (new Pen(pBrush, StrokeWidth.ToDeviceValue(renderer, UnitRenderingType.Other, this)));
  220. }
  221. return (new Pen(pBrush, StrokeWidth.ToDeviceValue(renderer, UnitRenderingType.Other, this)));
  222. }
  223. /// <summary>
  224. /// Get a clone of the current path, scaled for the stroke width
  225. /// </summary>
  226. /// <returns></returns>
  227. private GraphicsPath GetClone(SvgVisualElement pPath)
  228. {
  229. GraphicsPath pRet = Path(null).Clone() as GraphicsPath;
  230. switch (MarkerUnits)
  231. {
  232. case SvgMarkerUnits.StrokeWidth:
  233. using (var transMatrix = new Matrix())
  234. {
  235. transMatrix.Scale(AdjustForViewBoxWidth(pPath.StrokeWidth), AdjustForViewBoxHeight(pPath.StrokeWidth));
  236. pRet.Transform(transMatrix);
  237. }
  238. break;
  239. case SvgMarkerUnits.UserSpaceOnUse:
  240. break;
  241. }
  242. return (pRet);
  243. }
  244. /// <summary>
  245. /// Adjust the given value to account for the width of the viewbox in the viewport
  246. /// </summary>
  247. /// <param name="fWidth"></param>
  248. /// <returns></returns>
  249. private float AdjustForViewBoxWidth(float fWidth)
  250. {
  251. // TODO: We know this isn't correct
  252. return (ViewBox.Width <= 0 ? 1 : fWidth / ViewBox.Width);
  253. }
  254. /// <summary>
  255. /// Adjust the given value to account for the height of the viewbox in the viewport
  256. /// </summary>
  257. /// <param name="fWidth"></param>
  258. /// <returns></returns>
  259. private float AdjustForViewBoxHeight(float fHeight)
  260. {
  261. // TODO: We know this isn't correct
  262. return (ViewBox.Height <= 0 ? 1 : fHeight / ViewBox.Height);
  263. }
  264. }
  265. }
  266. #pragma warning restore