SvgSymbol.cs 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Drawing;
  6. #pragma warning disable
  7. namespace Svg.Document_Structure
  8. {
  9. /// <summary>
  10. /// An element used to group SVG shapes.
  11. /// </summary>
  12. [SvgElement("symbol")]
  13. public class SvgSymbol : SvgVisualElement
  14. {
  15. /// <summary>
  16. /// Gets or sets the viewport of the element.
  17. /// </summary>
  18. /// <value></value>
  19. [SvgAttribute("viewBox")]
  20. public SvgViewBox ViewBox
  21. {
  22. get { return this.Attributes.GetAttribute<SvgViewBox>("viewBox"); }
  23. set { this.Attributes["viewBox"] = value; }
  24. }
  25. /// <summary>
  26. /// Gets or sets the aspect of the viewport.
  27. /// </summary>
  28. /// <value></value>
  29. [SvgAttribute("preserveAspectRatio")]
  30. public SvgAspectRatio AspectRatio
  31. {
  32. get { return this.Attributes.GetAttribute<SvgAspectRatio>("preserveAspectRatio"); }
  33. set { this.Attributes["preserveAspectRatio"] = value; }
  34. }
  35. /// <summary>
  36. /// Gets the <see cref="GraphicsPath"/> for this element.
  37. /// </summary>
  38. /// <value></value>
  39. public override System.Drawing.Drawing2D.GraphicsPath Path(ISvgRenderer renderer)
  40. {
  41. return GetPaths(this, renderer);
  42. }
  43. /// <summary>
  44. /// Gets the bounds of the element.
  45. /// </summary>
  46. /// <value>The bounds.</value>
  47. public override System.Drawing.RectangleF Bounds
  48. {
  49. get
  50. {
  51. var r = new RectangleF();
  52. foreach (var c in this.Children)
  53. {
  54. if (c is SvgVisualElement)
  55. {
  56. // First it should check if rectangle is empty or it will return the wrong Bounds.
  57. // This is because when the Rectangle is Empty, the Union method adds as if the first values where X=0, Y=0
  58. if (r.IsEmpty)
  59. {
  60. r = ((SvgVisualElement)c).Bounds;
  61. }
  62. else
  63. {
  64. var childBounds = ((SvgVisualElement)c).Bounds;
  65. if (!childBounds.IsEmpty)
  66. {
  67. r = RectangleF.Union(r, childBounds);
  68. }
  69. }
  70. }
  71. }
  72. return r;
  73. }
  74. }
  75. protected override bool Renderable { get { return false; } }
  76. /// <summary>
  77. /// Applies the required transforms to <see cref="ISvgRenderer"/>.
  78. /// </summary>
  79. /// <param name="renderer">The <see cref="ISvgRenderer"/> to be transformed.</param>
  80. protected internal override bool PushTransforms(ISvgRenderer renderer)
  81. {
  82. if (!base.PushTransforms(renderer)) return false;
  83. this.ViewBox.AddViewBoxTransform(this.AspectRatio, renderer, null);
  84. return true;
  85. }
  86. // Only render if the parent is set to a Use element
  87. protected override void Render(ISvgRenderer renderer)
  88. {
  89. if (_parent is SvgUse) base.Render(renderer);
  90. }
  91. public override SvgElement DeepCopy()
  92. {
  93. return DeepCopy<SvgSymbol>();
  94. }
  95. public override SvgElement DeepCopy<T>()
  96. {
  97. var newObj = base.DeepCopy<T>() as SvgSymbol;
  98. if (this.Fill != null)
  99. newObj.Fill = this.Fill.DeepCopy() as SvgPaintServer;
  100. return newObj;
  101. }
  102. }
  103. }
  104. #pragma warning restore