SvgCircle.cs 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. using System.Drawing.Drawing2D;
  2. #pragma warning disable
  3. namespace Svg
  4. {
  5. /// <summary>
  6. /// An SVG element to render circles to the document.
  7. /// </summary>
  8. [SvgElement("circle")]
  9. public class SvgCircle : SvgPathBasedElement
  10. {
  11. private GraphicsPath _path;
  12. private SvgUnit _radius;
  13. private SvgUnit _centerX;
  14. private SvgUnit _centerY;
  15. /// <summary>
  16. /// Gets the center point of the circle.
  17. /// </summary>
  18. /// <value>The center.</value>
  19. public SvgPoint Center
  20. {
  21. get { return new SvgPoint(this.CenterX, this.CenterY); }
  22. }
  23. [SvgAttribute("cx")]
  24. public virtual SvgUnit CenterX
  25. {
  26. get { return this._centerX; }
  27. set
  28. {
  29. if (_centerX != value)
  30. {
  31. this._centerX = value;
  32. this.IsPathDirty = true;
  33. OnAttributeChanged(new AttributeEventArgs { Attribute = "cx", Value = value });
  34. }
  35. }
  36. }
  37. [SvgAttribute("cy")]
  38. public virtual SvgUnit CenterY
  39. {
  40. get { return this._centerY; }
  41. set
  42. {
  43. if (_centerY != value)
  44. {
  45. this._centerY = value;
  46. this.IsPathDirty = true;
  47. OnAttributeChanged(new AttributeEventArgs { Attribute = "cy", Value = value });
  48. }
  49. }
  50. }
  51. [SvgAttribute("r")]
  52. public virtual SvgUnit Radius
  53. {
  54. get { return this._radius; }
  55. set
  56. {
  57. if (_radius != value)
  58. {
  59. this._radius = value;
  60. this.IsPathDirty = true;
  61. OnAttributeChanged(new AttributeEventArgs { Attribute = "r", Value = value });
  62. }
  63. }
  64. }
  65. /// <summary>
  66. /// Gets the <see cref="GraphicsPath"/> representing this element.
  67. /// </summary>
  68. public override GraphicsPath Path(ISvgRenderer renderer)
  69. {
  70. if (this._path == null || this.IsPathDirty)
  71. {
  72. float halfStrokeWidth = base.StrokeWidth / 2;
  73. // If it is to render, don't need to consider stroke width.
  74. // i.e stroke width only to be considered when calculating boundary
  75. if (renderer != null)
  76. {
  77. halfStrokeWidth = 0;
  78. this.IsPathDirty = false;
  79. }
  80. _path = new GraphicsPath();
  81. _path.StartFigure();
  82. var center = this.Center.ToDeviceValue(renderer, this);
  83. var radius = this.Radius.ToDeviceValue(renderer, UnitRenderingType.Other, this) + halfStrokeWidth;
  84. _path.AddEllipse(center.X - radius, center.Y - radius, 2 * radius, 2 * radius);
  85. _path.CloseFigure();
  86. }
  87. return _path;
  88. }
  89. /// <summary>
  90. /// Renders the circle to the specified <see cref="Graphics"/> object.
  91. /// </summary>
  92. /// <param name="graphics">The graphics object.</param>
  93. protected override void Render(ISvgRenderer renderer)
  94. {
  95. // Don't draw if there is no radius set
  96. if (this.Radius.Value > 0.0f)
  97. {
  98. base.Render(renderer);
  99. }
  100. }
  101. /// <summary>
  102. /// Initializes a new instance of the <see cref="SvgCircle"/> class.
  103. /// </summary>
  104. public SvgCircle()
  105. {
  106. CenterX = new SvgUnit(0.0f);
  107. CenterY = new SvgUnit(0.0f);
  108. }
  109. public override SvgElement DeepCopy()
  110. {
  111. return DeepCopy<SvgCircle>();
  112. }
  113. public override SvgElement DeepCopy<T>()
  114. {
  115. var newObj = base.DeepCopy<T>() as SvgCircle;
  116. newObj.CenterX = this.CenterX;
  117. newObj.CenterY = this.CenterY;
  118. newObj.Radius = this.Radius;
  119. return newObj;
  120. }
  121. }
  122. }
  123. #pragma warning restore