tangxu
2024-12-29 72e75456f8b30ec5b6f355539d9c883b0f810d21
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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
// THIS FILE IS PART OF SVG PROJECT
// THE SVG PROJECT IS AN OPENSOURCE LIBRARY LICENSED UNDER THE MS-PL License.
// COPYRIGHT (C) svg-net. ALL RIGHTS RESERVED.
// GITHUB: https://github.com/svg-net/SVG
 
using AntdUI.Svg.Pathing;
using System.Drawing.Drawing2D;
 
namespace AntdUI.Svg
{
    /// <summary>
    /// Represents an SVG path element.
    /// </summary>
    public class SvgPath : SvgMarkerElement
    {
        public override string ClassName { get => "path"; }
 
        private GraphicsPath _path;
 
        /// <summary>
        /// Gets or sets a <see cref="SvgPathSegmentList"/> of path data.
        /// </summary>
        [SvgAttribute("d", true)]
        public SvgPathSegmentList PathData
        {
            get { return Attributes.GetAttribute<SvgPathSegmentList>("d"); }
            set
            {
                Attributes["d"] = value;
                value._owner = this;
                IsPathDirty = true;
            }
        }
 
        /// <summary>
        /// Gets or sets the length of the path.
        /// </summary>
        [SvgAttribute("pathLength", true)]
        public float PathLength
        {
            get { return Attributes.GetAttribute<float>("pathLength"); }
            set { Attributes["pathLength"] = value; }
        }
 
        /// <summary>
        /// Gets the <see cref="GraphicsPath"/> for this element.
        /// </summary>
        public override GraphicsPath Path(ISvgRenderer renderer)
        {
            if (_path == null || IsPathDirty)
            {
                _path = new GraphicsPath();
 
                foreach (var segment in PathData)
                {
                    segment.AddToPath(_path);
                }
 
                if (_path.PointCount == 0)
                {
                    if (PathData.Count > 0)
                    {
                        // special case with one move command only, see #223
                        var segment = PathData.Last;
                        _path.AddLine(segment.End, segment.End);
                        Fill = SvgPaintServer.None;
                    }
                    else
                    {
                        _path = null;
                    }
                }
                IsPathDirty = false;
            }
            return _path;
        }
 
        internal void OnPathUpdated()
        {
            IsPathDirty = true;
            OnAttributeChanged(new AttributeEventArgs { Attribute = "d", Value = Attributes.GetAttribute<SvgPathSegmentList>("d") });
        }
 
        /// <summary>
        /// Gets the bounds of the element.
        /// </summary>
        /// <value>The bounds.</value>
        public override System.Drawing.RectangleF Bounds
        {
            get { return Path(null).GetBounds(); }
        }
 
        /// <summary>
        /// Initializes a new instance of the <see cref="SvgPath"/> class.
        /// </summary>
        public SvgPath()
        {
            var pathData = new SvgPathSegmentList();
            Attributes["d"] = pathData;
            pathData._owner = this;
        }
    }
}