"Source/git@s-devrep.domain.ft:importedprojects/SVG.git" did not exist on "11d63ae2ba5f5bd87524681de05c17cca5fda216"
SvgClipPath.cs 3.66 KB
Newer Older
davescriven's avatar
davescriven committed
1
2
3
4
5
6
7
8
using System;
using System.Collections.Generic;
using System.Text;
using System.Drawing;
using System.Drawing.Drawing2D;

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

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

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

31
        /// <summary>
32
        /// Gets this <see cref="SvgClipPath"/>'s region to be used as a clipping region.
33
        /// </summary>
34
35
        /// <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
36
        {
37
            var path = new GraphicsPath();
davescriven's avatar
davescriven committed
38

39
40
            if (this._pathDirty)
            {
davescriven's avatar
davescriven committed
41
                foreach (SvgElement element in this.Children)
42
                {
43
                    this.CombinePaths(path, element);
44
                }
davescriven's avatar
davescriven committed
45

46
                this._pathDirty = false;
davescriven's avatar
davescriven committed
47
48
            }

49
            return new Region(path);
davescriven's avatar
davescriven committed
50
51
        }

52
53
54
55
56
        /// <summary>
        /// 
        /// </summary>
        /// <param name="region"></param>
        /// <param name="element"></param>
57
        private void CombinePaths(GraphicsPath path, SvgElement element)
davescriven's avatar
davescriven committed
58
        {
59
            var graphicsElement = element as SvgVisualElement;
davescriven's avatar
davescriven committed
60

61
            if (graphicsElement != null && graphicsElement.Path != null)
62
            {
63
64
                path.FillMode = (graphicsElement.ClipRule == SvgClipRule.NonZero) ? FillMode.Winding : FillMode.Alternate;
                path.AddPath(graphicsElement.Path, false);
65
            }
davescriven's avatar
davescriven committed
66
67

            foreach (SvgElement child in element.Children)
68
            {
69
                this.CombinePaths(path, child);
70
            }
davescriven's avatar
davescriven committed
71
72
        }

73
74
75
76
77
78
        /// <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>
79
        protected override void AddElement(SvgElement child, int index)
davescriven's avatar
davescriven committed
80
        {
81
            base.AddElement(child, index);
davescriven's avatar
davescriven committed
82
83
84
            this._pathDirty = true;
        }

85
86
87
88
89
        /// <summary>
        /// Called by the underlying <see cref="SvgElement"/> when an element has been removed from the
        /// <see cref="Children"/> collection.
        /// </summary>
        /// <param name="child">The <see cref="SvgElement"/> that has been removed.</param>
90
        protected override void RemoveElement(SvgElement child)
davescriven's avatar
davescriven committed
91
        {
92
            base.RemoveElement(child);
davescriven's avatar
davescriven committed
93
94
95
            this._pathDirty = true;
        }

96
97
98
99
        /// <summary>
        /// Renders the <see cref="SvgElement"/> and contents to the specified <see cref="SvgRenderer"/> object.
        /// </summary>
        /// <param name="renderer">The <see cref="SvgRenderer"/> object to render to.</param>
100
        protected override void Render(SvgRenderer renderer)
davescriven's avatar
davescriven committed
101
102
103
104
105
        {
            // Do nothing
        }
    }
}