SvgClipPath.cs 4.98 KB
Newer Older
davescriven's avatar
davescriven committed
1
2
3
4
5
using System;
using System.Collections.Generic;
using System.Text;
using System.Drawing;
using System.Drawing.Drawing2D;
6
using Svg.Transforms;
davescriven's avatar
davescriven committed
7
8
9

namespace Svg
{
10
    /// <summary>
11
    /// Defines a path that can be used by other <see cref="ISvgClipable"/> elements.
12
    /// </summary>
13
    [SvgElement("clipPath")]
davescriven's avatar
davescriven committed
14
15
    public sealed class SvgClipPath : SvgElement
    {
16
        private bool _pathDirty = true;
davescriven's avatar
davescriven committed
17

18
        /// <summary>
19
        /// Specifies the coordinate system for the clipping path.
20
        /// </summary>
davescriven's avatar
davescriven committed
21
        [SvgAttribute("clipPathUnits")]
22
        public SvgCoordinateUnits ClipPathUnits { get; set; }
davescriven's avatar
davescriven committed
23

24
25
26
        /// <summary>
        /// Initializes a new instance of the <see cref="SvgClipPath"/> class.
        /// </summary>
davescriven's avatar
davescriven committed
27
28
        public SvgClipPath()
        {
Eric Domke's avatar
Eric Domke committed
29
            this.ClipPathUnits = SvgCoordinateUnits.Inherit;
davescriven's avatar
davescriven committed
30
31
        }

32
33
        private GraphicsPath cachedClipPath = null;

34
        /// <summary>
35
        /// Gets this <see cref="SvgClipPath"/>'s region to be used as a clipping region.
36
        /// </summary>
37
38
        /// <returns>A new <see cref="Region"/> containing the <see cref="Region"/> to be used for clipping.</returns>
        public Region GetClipRegion(SvgVisualElement owner)
davescriven's avatar
davescriven committed
39
        {
40
            if (cachedClipPath == null || this._pathDirty)
41
            {
42
43
                cachedClipPath = new GraphicsPath();

davescriven's avatar
davescriven committed
44
                foreach (SvgElement element in this.Children)
45
                {
46
                    this.CombinePaths(cachedClipPath, element);
47
                }
davescriven's avatar
davescriven committed
48

49
                this._pathDirty = false;
davescriven's avatar
davescriven committed
50
51
            }

Eric Domke's avatar
Eric Domke committed
52
53
54
55
56
57
58
59
60
61
62
63
64
65
            var result = cachedClipPath;
            if (ClipPathUnits == SvgCoordinateUnits.ObjectBoundingBox)
            {
                result = (GraphicsPath)cachedClipPath.Clone();
                using (var transform = new Matrix())
                {
                    var bounds = owner.Bounds;
                    transform.Scale(bounds.Width, bounds.Height, MatrixOrder.Append);
                    transform.Translate(bounds.Left, bounds.Top, MatrixOrder.Append);
                    result.Transform(transform);
                }
            }

            return new Region(result);
davescriven's avatar
davescriven committed
66
67
        }

68
69
70
71
72
        /// <summary>
        /// 
        /// </summary>
        /// <param name="region"></param>
        /// <param name="element"></param>
73
        private void CombinePaths(GraphicsPath path, SvgElement element)
davescriven's avatar
davescriven committed
74
        {
75
            var graphicsElement = element as SvgVisualElement;
davescriven's avatar
davescriven committed
76

77
            if (graphicsElement != null && graphicsElement.Path(null) != null)
78
            {
79
                path.FillMode = (graphicsElement.ClipRule == SvgClipRule.NonZero) ? FillMode.Winding : FillMode.Alternate;
80

81
                GraphicsPath childPath = graphicsElement.Path(null);
82
83
84
85
86
87
88
89
90

                if (graphicsElement.Transforms != null)
                {
                    foreach (SvgTransform transform in graphicsElement.Transforms)
                    {
                        childPath.Transform(transform.Matrix);
                    }
                }

Eric Domke's avatar
Eric Domke committed
91
                if (childPath.PointCount > 0) path.AddPath(childPath, false);
92
            }
davescriven's avatar
davescriven committed
93
94

            foreach (SvgElement child in element.Children)
95
            {
96
                this.CombinePaths(path, child);
97
            }
davescriven's avatar
davescriven committed
98
99
        }

100
101
102
103
104
105
        /// <summary>
        /// Called by the underlying <see cref="SvgElement"/> when an element has been added to the
        /// <see cref="Children"/> collection.
        /// </summary>
        /// <param name="child">The <see cref="SvgElement"/> that has been added.</param>
        /// <param name="index">An <see cref="int"/> representing the index where the element was added to the collection.</param>
106
        protected override void AddElement(SvgElement child, int index)
davescriven's avatar
davescriven committed
107
        {
108
            base.AddElement(child, index);
davescriven's avatar
davescriven committed
109
110
111
            this._pathDirty = true;
        }

112
113
        /// <summary>
        /// Called by the underlying <see cref="SvgElement"/> when an element has been removed from the
114
        /// <see cref="SvgElement.Children"/> collection.
115
116
        /// </summary>
        /// <param name="child">The <see cref="SvgElement"/> that has been removed.</param>
117
        protected override void RemoveElement(SvgElement child)
davescriven's avatar
davescriven committed
118
        {
119
            base.RemoveElement(child);
davescriven's avatar
davescriven committed
120
121
122
            this._pathDirty = true;
        }

123
        /// <summary>
Eric Domke's avatar
Eric Domke committed
124
        /// Renders the <see cref="SvgElement"/> and contents to the specified <see cref="ISvgRenderer"/> object.
125
        /// </summary>
Eric Domke's avatar
Eric Domke committed
126
127
        /// <param name="renderer">The <see cref="ISvgRenderer"/> object to render to.</param>
        protected override void Render(ISvgRenderer renderer)
davescriven's avatar
davescriven committed
128
129
130
        {
            // Do nothing
        }
131
132
133
134
135
136
137
138
139
140
141
142
143


		public override SvgElement DeepCopy()
		{
			return DeepCopy<SvgClipPath>();
		}

		public override SvgElement DeepCopy<T>()
		{
			var newObj = base.DeepCopy<T>() as SvgClipPath;
			newObj.ClipPathUnits = this.ClipPathUnits;
			return newObj;
		}
davescriven's avatar
davescriven committed
144
145
    }
}