SvgClipPath.cs 2.83 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
10
11
    /// <summary>
    /// 
    /// </summary>
davescriven's avatar
davescriven committed
12
13
    public sealed class SvgClipPath : SvgElement
    {
14
        private SvgCoordinateUnits _clipPathUnits;
davescriven's avatar
davescriven committed
15
16
17
        private bool _pathDirty;
        private Region _region;

18
19
20
        /// <summary>
        /// 
        /// </summary>
davescriven's avatar
davescriven committed
21
        [SvgAttribute("clipPathUnits")]
22
        public SvgCoordinateUnits ClipPathUnits
davescriven's avatar
davescriven committed
23
24
25
26
27
        {
            get { return this._clipPathUnits; }
            set { this._clipPathUnits = value; }
        }

28
29
30
        /// <summary>
        /// Initializes a new instance of the <see cref="SvgClipPath"/> class.
        /// </summary>
davescriven's avatar
davescriven committed
31
32
        public SvgClipPath()
        {
33
            this._clipPathUnits = SvgCoordinateUnits.ObjectBoundingBox;
davescriven's avatar
davescriven committed
34
35
        }

36
37
38
39
40
        /// <summary>
        /// Gets this <see cref="SvgClipPath"/>'s region to be clipped.
        /// </summary>
        /// <returns>A new <see cref="Region"/> containing the area to be clipped.</returns>
        protected internal Region GetClipRegion()
davescriven's avatar
davescriven committed
41
42
43
44
45
46
        {
            if (_region == null || _pathDirty)
            {
                _region = new Region();

                foreach (SvgElement element in this.Children)
47
                {
davescriven's avatar
davescriven committed
48
                    ComplementRegion(_region, element);
49
                }
davescriven's avatar
davescriven committed
50
51
52
53
54
55
56

                _pathDirty = false;
            }

            return _region;
        }

57
58
59
60
61
        /// <summary>
        /// 
        /// </summary>
        /// <param name="region"></param>
        /// <param name="element"></param>
davescriven's avatar
davescriven committed
62
63
64
65
        private void ComplementRegion(Region region, SvgElement element)
        {
            SvgGraphicsElement graphicsElement = element as SvgGraphicsElement;

66
            if (graphicsElement != null && graphicsElement.Path != null)
67
            {
davescriven's avatar
davescriven committed
68
                region.Complement(graphicsElement.Path);
69
            }
davescriven's avatar
davescriven committed
70
71

            foreach (SvgElement child in element.Children)
72
            {
73
                ComplementRegion(region, child);
74
            }
davescriven's avatar
davescriven committed
75
76
        }

77
        protected override void AddElement(SvgElement child, int index)
davescriven's avatar
davescriven committed
78
        {
79
            base.AddElement(child, index);
davescriven's avatar
davescriven committed
80
81
82
            this._pathDirty = true;
        }

83
        protected override void RemoveElement(SvgElement child)
davescriven's avatar
davescriven committed
84
        {
85
            base.RemoveElement(child);
davescriven's avatar
davescriven committed
86
87
88
            this._pathDirty = true;
        }

89
90
91
92
        /// <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>
93
        protected override void Render(SvgRenderer renderer)
davescriven's avatar
davescriven committed
94
95
96
97
98
        {
            // Do nothing
        }
    }
}