SvgClipPath.cs 2.07 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
        private Region GetClipRegion()
davescriven's avatar
davescriven committed
31
32
33
34
35
36
        {
            if (_region == null || _pathDirty)
            {
                _region = new Region();

                foreach (SvgElement element in this.Children)
37
                {
davescriven's avatar
davescriven committed
38
                    ComplementRegion(_region, element);
39
                }
davescriven's avatar
davescriven committed
40
41
42
43
44
45
46
47
48
49
50
51

                _pathDirty = false;
            }

            return _region;
        }

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

            if (graphicsElement != null)
52
            {
davescriven's avatar
davescriven committed
53
                region.Complement(graphicsElement.Path);
54
            }
davescriven's avatar
davescriven committed
55
56

            foreach (SvgElement child in element.Children)
57
            {
davescriven's avatar
davescriven committed
58
                ComplementRegion(region, element);
59
            }
davescriven's avatar
davescriven committed
60
61
        }

62
        protected override void AddElement(SvgElement child, int index)
davescriven's avatar
davescriven committed
63
        {
64
            base.AddElement(child, index);
davescriven's avatar
davescriven committed
65
66
67
            this._pathDirty = true;
        }

68
        protected override void RemoveElement(SvgElement child)
davescriven's avatar
davescriven committed
69
        {
70
            base.RemoveElement(child);
davescriven's avatar
davescriven committed
71
72
73
            this._pathDirty = true;
        }

74
        protected override void Render(SvgRenderer renderer)
davescriven's avatar
davescriven committed
75
76
77
78
79
        {
            // Do nothing
        }
    }
}