SvgEllipse.cs 4.4 KB

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