SvgUse.cs 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4. using System.Web;
  5. using System.Xml;
  6. using System.Xml.Serialization;
  7. using System.Drawing.Drawing2D;
  8. #pragma warning disable
  9. namespace Svg
  10. {
  11. [SvgElement("use")]
  12. public class SvgUse : SvgVisualElement
  13. {
  14. private Uri _referencedElement;
  15. [SvgAttribute("href", SvgAttributeAttribute.XLinkNamespace)]
  16. public virtual Uri ReferencedElement
  17. {
  18. get { return this._referencedElement; }
  19. set { this._referencedElement = value; }
  20. }
  21. [SvgAttribute("x")]
  22. public virtual SvgUnit X
  23. {
  24. get { return this.Attributes.GetAttribute<SvgUnit>("x"); }
  25. set { this.Attributes["x"] = value; }
  26. }
  27. [SvgAttribute("y")]
  28. public virtual SvgUnit Y
  29. {
  30. get { return this.Attributes.GetAttribute<SvgUnit>("y"); }
  31. set { this.Attributes["y"] = value; }
  32. }
  33. /// <summary>
  34. /// Applies the required transforms to <see cref="ISvgRenderer"/>.
  35. /// </summary>
  36. /// <param name="renderer">The <see cref="ISvgRenderer"/> to be transformed.</param>
  37. protected internal override bool PushTransforms(ISvgRenderer renderer)
  38. {
  39. if (!base.PushTransforms(renderer)) return false;
  40. renderer.TranslateTransform(this.X.ToDeviceValue(renderer, UnitRenderingType.Horizontal, this),
  41. this.Y.ToDeviceValue(renderer, UnitRenderingType.Vertical, this),
  42. MatrixOrder.Prepend);
  43. return true;
  44. }
  45. /// <summary>
  46. /// Initializes a new instance of the <see cref="SvgUse"/> class.
  47. /// </summary>
  48. public SvgUse()
  49. {
  50. this.X = 0;
  51. this.Y = 0;
  52. }
  53. public override System.Drawing.Drawing2D.GraphicsPath Path(ISvgRenderer renderer)
  54. {
  55. SvgVisualElement element = (SvgVisualElement)this.OwnerDocument.IdManager.GetElementById(this.ReferencedElement);
  56. return (element != null) ? element.Path(renderer) : null;
  57. }
  58. public override System.Drawing.RectangleF Bounds
  59. {
  60. get { return new System.Drawing.RectangleF(); }
  61. }
  62. protected override bool Renderable { get { return false; } }
  63. protected override void Render(ISvgRenderer renderer)
  64. {
  65. if (this.Visible && this.Displayable && this.PushTransforms(renderer))
  66. {
  67. this.SetClip(renderer);
  68. var element = this.OwnerDocument.IdManager.GetElementById(this.ReferencedElement) as SvgVisualElement;
  69. if (element != null)
  70. {
  71. var origParent = element.Parent;
  72. element.Opacity = 0f;
  73. element._parent = this;
  74. // as the new parent may have other styles that are inherited,
  75. // we have to redraw the paths for the children
  76. element.InvalidateChildPaths();
  77. element.RenderElement(renderer);
  78. element._parent = origParent;
  79. }
  80. this.ResetClip(renderer);
  81. this.PopTransforms(renderer);
  82. }
  83. }
  84. public override SvgElement DeepCopy()
  85. {
  86. return DeepCopy<SvgUse>();
  87. }
  88. public override SvgElement DeepCopy<T>()
  89. {
  90. var newObj = base.DeepCopy<T>() as SvgUse;
  91. newObj.ReferencedElement = this.ReferencedElement;
  92. newObj.X = this.X;
  93. newObj.Y = this.Y;
  94. return newObj;
  95. }
  96. }
  97. }
  98. #pragma warning restore