SvgPath.cs 3.03 KB
Newer Older
davescriven's avatar
davescriven committed
1
2
3
4
5
6
7
8
9
using System;
using System.Collections.Generic;
using System.Text;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Xml.Serialization;
using System.Xml;
using System.Diagnostics;
using Svg.Pathing;
owaits's avatar
owaits committed
10
using Svg.Transforms;
davescriven's avatar
davescriven committed
11
12
13

namespace Svg
{
14
15
16
    /// <summary>
    /// Represents an SVG path element.
    /// </summary>
17
    [SvgElement("path")]
18
    public class SvgPath : SvgVisualElement
davescriven's avatar
davescriven committed
19
20
21
22
23
    {
        private SvgPathSegmentList _pathData;
        private GraphicsPath _path;
        private int _pathLength;

24
25
26
        /// <summary>
        /// Gets or sets a <see cref="SvgPathSegmentList"/> of path data.
        /// </summary>
davescriven's avatar
davescriven committed
27
28
29
30
31
32
33
34
35
36
37
38
        [SvgAttribute("d")]
        public SvgPathSegmentList PathData
        {
            get { return this._pathData; }
            set
            {
                this._pathData = value;
                this._pathData._owner = this;
                this.IsPathDirty = true;
            }
        }

39
40
41
        /// <summary>
        /// Gets or sets the length of the path.
        /// </summary>
davescriven's avatar
davescriven committed
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
        [SvgAttribute("pathLength")]
        public int PathLength
        {
            get { return this._pathLength; }
            set { this._pathLength = value; }
        }

        /// <summary>
        /// Gets the <see cref="GraphicsPath"/> for this element.
        /// </summary>
        public override GraphicsPath Path
        {
            get
            {
                if (this._path == null || this.IsPathDirty)
                {
                    _path = new GraphicsPath();

                    foreach (SvgPathSegment segment in this.PathData)
                    {
                        segment.AddToPath(_path);
                    }

owaits's avatar
owaits committed
65
66
67
68
69
70
71
72
                    if (base.Transforms != null)
                    {
                        foreach (SvgTransform transform in base.Transforms)
                        {
                            _path.Transform(transform.Matrix);
                        }
                    }

davescriven's avatar
davescriven committed
73
74
75
76
77
78
79
80
81
82
83
                    this.IsPathDirty = false;
                }
                return _path;
            }
        }

        internal void OnPathUpdated()
        {
            this.IsPathDirty = true;
        }

84
85
86
        /// <summary>
        /// Gets or sets a value to determine if anti-aliasing should occur when the element is being rendered.
        /// </summary>
davescriven's avatar
davescriven committed
87
88
89
90
91
        protected override bool RequiresSmoothRendering
        {
            get { return true; }
        }

92
93
94
95
        /// <summary>
        /// Gets the bounds of the element.
        /// </summary>
        /// <value>The bounds.</value>
davescriven's avatar
davescriven committed
96
97
98
99
100
        public override System.Drawing.RectangleF Bounds
        {
            get { return this.Path.GetBounds(); }
        }

101
102
103
        /// <summary>
        /// Initializes a new instance of the <see cref="SvgPath"/> class.
        /// </summary>
davescriven's avatar
davescriven committed
104
105
106
107
108
109
110
        public SvgPath()
        {
            this._pathData = new SvgPathSegmentList();
            this._pathData._owner = this;
        }
    }
}