SvgClipPath.cs 2.17 KB
Newer Older
davescriven's avatar
davescriven committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
using System;
using System.Collections.Generic;
using System.Text;
using System.Drawing;
using System.Drawing.Drawing2D;

namespace Svg
{
    public sealed class SvgClipPath : SvgElement
    {
        private SvgCoordinateSystem _clipPathUnits;
        private bool _pathDirty;
        private Region _region;

        [SvgAttribute("clipPathUnits")]
        public SvgCoordinateSystem ClipPathUnits
        {
            get { return this._clipPathUnits; }
            set { this._clipPathUnits = value; }
        }

        public SvgClipPath()
        {
            this._clipPathUnits = SvgCoordinateSystem.UserSpaceOnUse;
        }

        public override string ElementName
        {
            get { return "clipPath"; }
        }

        public override object Clone()
        {
            SvgClipPath path = new SvgClipPath();
            path._clipPathUnits = this._clipPathUnits;
            return path;
        }

        public Region GetClipRegion()
        {
            if (_region == null || _pathDirty)
            {
                _region = new Region();

                foreach (SvgElement element in this.Children)
                    ComplementRegion(_region, element);

                _pathDirty = false;
            }

            return _region;
        }

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

            if (graphicsElement != null)
                region.Complement(graphicsElement.Path);

            foreach (SvgElement child in element.Children)
                ComplementRegion(region, element);
        }

        protected override void AddedElement(SvgElement child, int index)
        {
            base.AddedElement(child, index);
            this._pathDirty = true;
        }

        protected override void RemovedElement(SvgElement child)
        {
            base.RemovedElement(child);
            this._pathDirty = true;
        }

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