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

namespace Svg
{
    public sealed class SvgClipPath : SvgElement
    {
11
        private SvgCoordinateUnits _clipPathUnits;
davescriven's avatar
davescriven committed
12
13
14
15
        private bool _pathDirty;
        private Region _region;

        [SvgAttribute("clipPathUnits")]
16
        public SvgCoordinateUnits ClipPathUnits
davescriven's avatar
davescriven committed
17
18
19
20
21
        {
            get { return this._clipPathUnits; }
            set { this._clipPathUnits = value; }
        }

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

30
31
32
33
        /// <summary>
        /// Gets the name of the element.
        /// </summary>
        protected override string ElementName
davescriven's avatar
davescriven committed
34
35
36
37
        {
            get { return "clipPath"; }
        }

38
        private Region GetClipRegion()
davescriven's avatar
davescriven committed
39
40
41
42
43
44
        {
            if (_region == null || _pathDirty)
            {
                _region = new Region();

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

                _pathDirty = false;
            }

            return _region;
        }

        private void ComplementRegion(Region region, SvgElement element)
        {
            SvgGraphicsElement graphicsElement = element as SvgGraphicsElement;

            if (graphicsElement != null)
60
            {
davescriven's avatar
davescriven committed
61
                region.Complement(graphicsElement.Path);
62
            }
davescriven's avatar
davescriven committed
63
64

            foreach (SvgElement child in element.Children)
65
            {
davescriven's avatar
davescriven committed
66
                ComplementRegion(region, element);
67
            }
davescriven's avatar
davescriven committed
68
69
        }

70
        protected override void ElementAdded(SvgElement child, int index)
davescriven's avatar
davescriven committed
71
        {
72
            base.ElementAdded(child, index);
davescriven's avatar
davescriven committed
73
74
75
            this._pathDirty = true;
        }

76
        protected override void ElementRemoved(SvgElement child)
davescriven's avatar
davescriven committed
77
        {
78
            base.ElementRemoved(child);
davescriven's avatar
davescriven committed
79
80
81
82
83
84
85
86
87
            this._pathDirty = true;
        }

        protected override void Render(System.Drawing.Graphics graphics)
        {
            // Do nothing
        }
    }
}