SvgUnitConverter.cs 2.69 KB
Newer Older
davescriven's avatar
davescriven committed
1
2
using System;
using System.Collections.Generic;
3
4
5
using System.ComponentModel;
using System.Diagnostics;
using System.Globalization;
davescriven's avatar
davescriven committed
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
using System.Text;
using System.Text.RegularExpressions;
using System.Web.UI.WebControls;

namespace Svg
{
    public sealed class SvgUnitConverter : TypeConverter
    {
        public override object ConvertFrom(ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value)
        {
            if (value == null)
            {
                return new SvgUnit(SvgUnitType.User, 0.0f);
            }

            if (!(value is string))
            {
                throw new ArgumentOutOfRangeException("value must be a string.");
            }

            // http://www.w3.org/TR/CSS21/syndata.html#values
            // http://www.w3.org/TR/SVG11/coords.html#Units

            string unit = value as string;
            int identifierIndex = -1;

            for (int i = 0; i < unit.Length; i++)
            {
                if (char.IsLetter(unit[i]) || unit[i] == '%')
                {
                    identifierIndex = i;
                    break;
                }
            }

            float val = 0.0f;
42
            float.TryParse((identifierIndex > -1) ? unit.Substring(0, identifierIndex) : unit, NumberStyles.Float, CultureInfo.InvariantCulture, out val);
davescriven's avatar
davescriven committed
43
44
45
46
47
48
49
50
51
52
53
54
55

            if (identifierIndex == -1)
            {
                return new SvgUnit(val);
            }

            switch (unit.Substring(identifierIndex).Trim().ToLower())
            {
                case "mm":
                    return new SvgUnit(SvgUnitType.Millimeter, val);
                case "cm":
                    return new SvgUnit(SvgUnitType.Centimeter, val);
                case "in":
56
                    return new SvgUnit(SvgUnitType.Inch, val);
davescriven's avatar
davescriven committed
57
58
59
60
61
62
63
64
                case "px":
                    return new SvgUnit(SvgUnitType.Pixel, val);
                case "pt":
                    return new SvgUnit(SvgUnitType.Point, val);
                case "pc":
                    return new SvgUnit(SvgUnitType.Pica, val);
                case "%":
                    return new SvgUnit(SvgUnitType.Percentage, val);
65
66
                case "em":
                    return new SvgUnit(SvgUnitType.Em, val);
davescriven's avatar
davescriven committed
67
68
69
70
71
72
73
74
75
76
77
78
79
80
                default:
                    throw new FormatException("Unit is in an invalid format '" + unit + "'.");
            }
        }

        public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType)
        {
            if (sourceType == typeof(string))
                return true;

            return base.CanConvertFrom(context, sourceType);
        }
    }
}