SvgPathBuilder.cs 10.2 KB
Newer Older
davescriven's avatar
davescriven committed
1
2
using System;
using System.Collections.Generic;
3
using System.ComponentModel;
davescriven's avatar
davescriven committed
4
using System.Diagnostics;
5
using System.Drawing;
6
using System.Globalization;
davescriven's avatar
davescriven committed
7
8
9
10
11
12
13
14
15
16
17
18

using Svg.Pathing;

namespace Svg
{
    internal class SvgPathBuilder : TypeConverter
    {
        public static SvgPathSegmentList Parse(string path)
        {
            if (string.IsNullOrEmpty(path))
                throw new ArgumentNullException("path");

19
            var segments = new SvgPathSegmentList();
davescriven's avatar
davescriven committed
20
21
22

            try
            {
23
                foreach (var commandSet in SplitCommands(path.TrimEnd(null)))
davescriven's avatar
davescriven committed
24
                {
25
26
27
                    var coords = new List<float>(ParseCoordinates(commandSet));
                    var command = commandSet[0];
                    var isRelative = char.IsLower(command);
davescriven's avatar
davescriven committed
28
29
30
31
32
33
                    // http://www.w3.org/TR/SVG11/paths.html#PathDataGeneralInformation

                    switch (command)
                    {
                        case 'm': // relative moveto
                        case 'M': // moveto
34
35
36
37
                            segments.Add(
                                new SvgMoveToSegment(ToAbsolute(coords[0], coords[1], segments, isRelative)));

                            for (var i = 2; i < coords.Count; i += 2)
davescriven's avatar
davescriven committed
38
                            {
39
40
                                segments.Add(new SvgLineSegment(segments.Last.End,
                                    ToAbsolute(coords[i], coords[i + 1], segments, isRelative)));
davescriven's avatar
davescriven committed
41
42
43
44
45
46
47
                            }
                            break;
                        case 'a':
                        case 'A':
                            throw new NotImplementedException("Arc segments are not yet implemented");
                        case 'l': // relative lineto
                        case 'L': // lineto
48
                            for (var i = 0; i < coords.Count; i += 2)
davescriven's avatar
davescriven committed
49
                            {
50
51
                                segments.Add(new SvgLineSegment(segments.Last.End,
                                    ToAbsolute(coords[i], coords[i + 1], segments, isRelative)));
davescriven's avatar
davescriven committed
52
53
54
55
                            }
                            break;
                        case 'H': // horizontal lineto
                        case 'h': // relative horizontal lineto
56
57
58
                            foreach (var value in coords)
                                segments.Add(new SvgLineSegment(segments.Last.End,
                                    ToAbsolute(value, segments.Last.End.Y, segments, isRelative, false)));
davescriven's avatar
davescriven committed
59
60
61
                            break;
                        case 'V': // vertical lineto
                        case 'v': // relative vertical lineto
62
63
64
                            foreach (var value in coords)
                                segments.Add(new SvgLineSegment(segments.Last.End,
                                    ToAbsolute(segments.Last.End.X, value, segments, false, isRelative)));
davescriven's avatar
davescriven committed
65
66
67
                            break;
                        case 'Q': // curveto
                        case 'q': // relative curveto
68
                            for (var i = 0; i < coords.Count; i += 4)
davescriven's avatar
davescriven committed
69
                            {
70
71
72
                                segments.Add(new SvgQuadraticCurveSegment(segments.Last.End,
                                    ToAbsolute(coords[i], coords[i + 1], segments, isRelative),
                                    ToAbsolute(coords[i + 2], coords[i + 3], segments, isRelative)));
davescriven's avatar
davescriven committed
73
74
75
76
                            }
                            break;
                        case 'T': // shorthand/smooth curveto
                        case 't': // relative shorthand/smooth curveto
77
                            for (var i = 0; i < coords.Count; i += 2)
davescriven's avatar
davescriven committed
78
                            {
79
                                var lastQuadCurve = segments.Last as SvgQuadraticCurveSegment;
davescriven's avatar
davescriven committed
80

81
82
83
                                var controlPoint = lastQuadCurve != null
                                    ? Reflect(lastQuadCurve.ControlPoint, segments.Last.End)
                                    : segments.Last.End;
davescriven's avatar
davescriven committed
84

85
86
                                segments.Add(new SvgQuadraticCurveSegment(segments.Last.End, controlPoint,
                                    ToAbsolute(coords[i], coords[i + 1], segments, isRelative)));
davescriven's avatar
davescriven committed
87
88
89
90
                            }
                            break;
                        case 'C': // curveto
                        case 'c': // relative curveto
91
                            for (var i = 0; i < coords.Count; i += 6)
davescriven's avatar
davescriven committed
92
                            {
93
94
95
96
                                segments.Add(new SvgCubicCurveSegment(segments.Last.End,
                                    ToAbsolute(coords[i], coords[i + 1], segments, isRelative),
                                    ToAbsolute(coords[i + 2], coords[i + 3], segments, isRelative),
                                    ToAbsolute(coords[i + 4], coords[i + 5], segments, isRelative)));
davescriven's avatar
davescriven committed
97
98
99
100
101
                            }
                            break;
                        case 'S': // shorthand/smooth curveto
                        case 's': // relative shorthand/smooth curveto

102
                            for (var i = 0; i < coords.Count; i += 4)
davescriven's avatar
davescriven committed
103
                            {
104
105
106
107
108
109
110
111
112
                                var lastCubicCurve = segments.Last as SvgCubicCurveSegment;

                                var controlPoint = lastCubicCurve != null
                                    ? Reflect(lastCubicCurve.SecondControlPoint, segments.Last.End)
                                    : segments.Last.End;

                                segments.Add(new SvgCubicCurveSegment(segments.Last.End, controlPoint,
                                    ToAbsolute(coords[i], coords[i + 1], segments, isRelative),
                                    ToAbsolute(coords[i + 2], coords[i + 3], segments, isRelative)));
davescriven's avatar
davescriven committed
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
                            }
                            break;
                        case 'Z': // closepath
                        case 'z': // relative closepath
                            segments.Add(new SvgClosePathSegment());
                            break;
                    }
                }
            }
            catch
            {
                Trace.TraceError("Error parsing path \"{0}\".", path);
            }

            return segments;
        }

        private static PointF Reflect(PointF point, PointF mirror)
        {
            // TODO: Only works left to right???
133
134
            var x = mirror.X + (mirror.X - point.X);
            var y = mirror.Y + (mirror.Y - point.Y);
davescriven's avatar
davescriven committed
135
136
137
138

            return new PointF(Math.Abs(x), Math.Abs(y));
        }

139
140
141
142
143
144
145
146
147
        /// <summary>
        /// Creates point with absolute coorindates.
        /// </summary>
        /// <param name="x">Raw X-coordinate value.</param>
        /// <param name="y">Raw Y-coordinate value.</param>
        /// <param name="segments">Current path segments.</param>
        /// <param name="isRelativeBoth"><b>true</b> if <paramref name="x"/> and <paramref name="y"/> contains relative coordinate values, otherwise <b>false</b>.</param>
        /// <returns><see cref="PointF"/> that contains absolute coordinates.</returns>
        private static PointF ToAbsolute(float x, float y, SvgPathSegmentList segments, bool isRelativeBoth)
davescriven's avatar
davescriven committed
148
        {
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
            return ToAbsolute(x, y, segments, isRelativeBoth, isRelativeBoth);
        }

        /// <summary>
        /// Creates point with absolute coorindates.
        /// </summary>
        /// <param name="x">Raw X-coordinate value.</param>
        /// <param name="y">Raw Y-coordinate value.</param>
        /// <param name="segments">Current path segments.</param>
        /// <param name="isRelativeX"><b>true</b> if <paramref name="x"/> contains relative coordinate value, otherwise <b>false</b>.</param>
        /// <param name="isRelativeY"><b>true</b> if <paramref name="y"/> contains relative coordinate value, otherwise <b>false</b>.</param>
        /// <returns><see cref="PointF"/> that contains absolute coordinates.</returns>
        private static PointF ToAbsolute(float x, float y, SvgPathSegmentList segments, bool isRelativeX, bool isRelativeY)
        {
            var point = new PointF(x, y);

            if ((isRelativeX || isRelativeY) && segments.Count > 0)
            {
                var lastSegment = segments.Last;

                if (isRelativeX)
                    point.X += lastSegment.End.X;

                if (isRelativeY)
                    point.Y += lastSegment.End.Y;
            }

            return point;
davescriven's avatar
davescriven committed
177
178
179
180
        }

        private static IEnumerable<string> SplitCommands(string path)
        {
181
            var commandStart = 0;
davescriven's avatar
davescriven committed
182

183
            for (var i = 0; i < path.Length; i++)
davescriven's avatar
davescriven committed
184
            {
185
                string command;
davescriven's avatar
davescriven committed
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
                if (char.IsLetter(path[i]))
                {
                    command = path.Substring(commandStart, i - commandStart).Trim();
                    commandStart = i;

                    if (!string.IsNullOrEmpty(command))
                        yield return command;

                    if (path.Length == i + 1)
                        yield return path[i].ToString();
                }
                else if (path.Length == i + 1)
                {
                    command = path.Substring(commandStart, i - commandStart + 1).Trim();

                    if (!string.IsNullOrEmpty(command))
                        yield return command;
                }
            }
        }

207
        private static IEnumerable<float> ParseCoordinates(string coords)
davescriven's avatar
davescriven committed
208
209
        {
            // TODO: Handle "1-1" (new PointF(1, -1);
210
211
            var parts = coords.Remove(0, 1).Replace("-", " -").Split(new[] {',', ' '},
                StringSplitOptions.RemoveEmptyEntries);
davescriven's avatar
davescriven committed
212

213
214
            for (var i = 0; i < parts.Length; i ++)
                yield return float.Parse(parts[i], NumberStyles.Float, CultureInfo.InvariantCulture);
davescriven's avatar
davescriven committed
215
216
        }

217
        public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object value)
davescriven's avatar
davescriven committed
218
219
        {
            if (value is string)
220
                return Parse((string) value);
davescriven's avatar
davescriven committed
221
222
223
224
225

            return base.ConvertFrom(context, culture, value);
        }
    }
}