SvgClipPath.cs 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4. using System.Drawing;
  5. using System.Drawing.Drawing2D;
  6. using Svg.Transforms;
  7. #pragma warning disable
  8. namespace Svg
  9. {
  10. /// <summary>
  11. /// Defines a path that can be used by other <see cref="ISvgClipable"/> elements.
  12. /// </summary>
  13. [SvgElement("clipPath")]
  14. public sealed class SvgClipPath : SvgElement
  15. {
  16. private bool _pathDirty = true;
  17. /// <summary>
  18. /// Specifies the coordinate system for the clipping path.
  19. /// </summary>
  20. [SvgAttribute("clipPathUnits")]
  21. public SvgCoordinateUnits ClipPathUnits { get; set; }
  22. /// <summary>
  23. /// Initializes a new instance of the <see cref="SvgClipPath"/> class.
  24. /// </summary>
  25. public SvgClipPath()
  26. {
  27. this.ClipPathUnits = SvgCoordinateUnits.Inherit;
  28. }
  29. private GraphicsPath cachedClipPath = null;
  30. /// <summary>
  31. /// Gets this <see cref="SvgClipPath"/>'s region to be used as a clipping region.
  32. /// </summary>
  33. /// <returns>A new <see cref="Region"/> containing the <see cref="Region"/> to be used for clipping.</returns>
  34. public Region GetClipRegion(SvgVisualElement owner)
  35. {
  36. if (cachedClipPath == null || this._pathDirty)
  37. {
  38. cachedClipPath = new GraphicsPath();
  39. foreach (SvgElement element in this.Children)
  40. {
  41. this.CombinePaths(cachedClipPath, element);
  42. }
  43. this._pathDirty = false;
  44. }
  45. var result = cachedClipPath;
  46. if (ClipPathUnits == SvgCoordinateUnits.ObjectBoundingBox)
  47. {
  48. result = (GraphicsPath)cachedClipPath.Clone();
  49. using (var transform = new Matrix())
  50. {
  51. var bounds = owner.Bounds;
  52. transform.Scale(bounds.Width, bounds.Height, MatrixOrder.Append);
  53. transform.Translate(bounds.Left, bounds.Top, MatrixOrder.Append);
  54. result.Transform(transform);
  55. }
  56. }
  57. return new Region(result);
  58. }
  59. /// <summary>
  60. ///
  61. /// </summary>
  62. /// <param name="region"></param>
  63. /// <param name="element"></param>
  64. private void CombinePaths(GraphicsPath path, SvgElement element)
  65. {
  66. var graphicsElement = element as SvgVisualElement;
  67. if (graphicsElement != null && graphicsElement.Path(null) != null)
  68. {
  69. path.FillMode = (graphicsElement.ClipRule == SvgClipRule.NonZero) ? FillMode.Winding : FillMode.Alternate;
  70. GraphicsPath childPath = graphicsElement.Path(null);
  71. if (graphicsElement.Transforms != null)
  72. {
  73. foreach (SvgTransform transform in graphicsElement.Transforms)
  74. {
  75. childPath.Transform(transform.Matrix);
  76. }
  77. }
  78. if (childPath.PointCount > 0) path.AddPath(childPath, false);
  79. }
  80. foreach (SvgElement child in element.Children)
  81. {
  82. this.CombinePaths(path, child);
  83. }
  84. }
  85. /// <summary>
  86. /// Called by the underlying <see cref="SvgElement"/> when an element has been added to the
  87. /// <see cref="Children"/> collection.
  88. /// </summary>
  89. /// <param name="child">The <see cref="SvgElement"/> that has been added.</param>
  90. /// <param name="index">An <see cref="int"/> representing the index where the element was added to the collection.</param>
  91. protected override void AddElement(SvgElement child, int index)
  92. {
  93. base.AddElement(child, index);
  94. this._pathDirty = true;
  95. }
  96. /// <summary>
  97. /// Called by the underlying <see cref="SvgElement"/> when an element has been removed from the
  98. /// <see cref="SvgElement.Children"/> collection.
  99. /// </summary>
  100. /// <param name="child">The <see cref="SvgElement"/> that has been removed.</param>
  101. protected override void RemoveElement(SvgElement child)
  102. {
  103. base.RemoveElement(child);
  104. this._pathDirty = true;
  105. }
  106. /// <summary>
  107. /// Renders the <see cref="SvgElement"/> and contents to the specified <see cref="ISvgRenderer"/> object.
  108. /// </summary>
  109. /// <param name="renderer">The <see cref="ISvgRenderer"/> object to render to.</param>
  110. protected override void Render(ISvgRenderer renderer)
  111. {
  112. // Do nothing
  113. }
  114. public override SvgElement DeepCopy()
  115. {
  116. return DeepCopy<SvgClipPath>();
  117. }
  118. public override SvgElement DeepCopy<T>()
  119. {
  120. var newObj = base.DeepCopy<T>() as SvgClipPath;
  121. newObj.ClipPathUnits = this.ClipPathUnits;
  122. return newObj;
  123. }
  124. }
  125. }
  126. #pragma warning restore