SvgTransformConverter.cs 4.52 KB
Newer Older
davescriven's avatar
davescriven committed
1
2
3
4
5
6
7
using System;
using System.Collections.Generic;
using System.Text;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.ComponentModel;
using System.Text.RegularExpressions;
8
using System.Globalization;
davescriven's avatar
davescriven committed
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

namespace Svg.Transforms
{
    public class SvgTransformConverter : TypeConverter
    {
        private static IEnumerable<string> SplitTransforms(string transforms)
        {
            int transformEnd = 0;

            for (int i = 0; i < transforms.Length; i++)
            {
                if (transforms[i] == ')')
                {
                    yield return transforms.Substring(transformEnd, i - transformEnd + 1).Trim();
                    transformEnd = i+1;
                }
            }
        }

        /// <summary>
        /// Converts the given object to the type of this converter, using the specified context and culture information.
        /// </summary>
        /// <param name="context">An <see cref="T:System.ComponentModel.ITypeDescriptorContext"/> that provides a format context.</param>
        /// <param name="culture">The <see cref="T:System.Globalization.CultureInfo"/> to use as the current culture.</param>
        /// <param name="value">The <see cref="T:System.Object"/> to convert.</param>
        /// <returns>
        /// An <see cref="T:System.Object"/> that represents the converted value.
        /// </returns>
        /// <exception cref="T:System.NotSupportedException">The conversion cannot be performed. </exception>
        public override object ConvertFrom(ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value)
        {
            if (value is string)
            {
                SvgTransformCollection transformList = new SvgTransformCollection();

                string[] parts;
                string contents;
                string transformName;

                foreach (string transform in SvgTransformConverter.SplitTransforms((string)value))
                {
                    if (string.IsNullOrEmpty(transform))
                        continue;

                    parts = transform.Split('(', ')');
                    transformName = parts[0].Trim();
                    contents = parts[1].Trim();

                    switch (transformName)
                    {
                        case "translate":
                            string[] coords = contents.Split(new char[]{',', ' '}, StringSplitOptions.RemoveEmptyEntries);
61
62
63
64
65
66

                            if (coords.Length != 2)
                            {
                                throw new FormatException("Translate transforms must be in the format 'translate(x, y)'");
                            }

67
68
                            float x = float.Parse(coords[0].Trim(), NumberStyles.Float, CultureInfo.InvariantCulture);
                            float y = float.Parse(coords[1].Trim(), NumberStyles.Float, CultureInfo.InvariantCulture);
davescriven's avatar
davescriven committed
69
70
71
                            transformList.Add(new SvgTranslate(x, y));
                            break;
                        case "rotate":
72
                            float angle = float.Parse(contents, NumberStyles.Float, CultureInfo.InvariantCulture);
davescriven's avatar
davescriven committed
73
74
75
                            transformList.Add(new SvgRotate(angle));
                            break;
                        case "scale":
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
                            string[] scales = contents.Split(new char[] { ',', ' ' }, StringSplitOptions.RemoveEmptyEntries);

                            if (scales.Length == 0 || scales.Length > 2)
                            {
                                throw new FormatException("Scale transforms must be in the format 'scale(x [,y])'");
                            }

                            float sx = float.Parse(scales[0].Trim(), NumberStyles.Float, CultureInfo.InvariantCulture);

                            if (scales.Length > 1)
                            {
                                float sy = float.Parse(scales[1].Trim(), NumberStyles.Float, CultureInfo.InvariantCulture);
                                transformList.Add(new SvgScale(sx, sy));
                            }
                            else
                            {
                                transformList.Add(new SvgScale(sx));
                            }

davescriven's avatar
davescriven committed
95
96
97
98
99
100
101
102
103
104
105
                            break;
                    }
                }

                return transformList;
            }

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