SvgPaintServerFactory.cs 5.86 KB
Newer Older
davescriven's avatar
davescriven committed
1
2
using System;
using System.Collections.Generic;
Eric Domke's avatar
Eric Domke committed
3
using System.ComponentModel;
davescriven's avatar
davescriven committed
4
using System.Drawing;
5
using System.Globalization;
Eric Domke's avatar
Eric Domke committed
6
using System.Linq;
davescriven's avatar
davescriven committed
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21

namespace Svg
{
    internal class SvgPaintServerFactory : TypeConverter
    {
        private static readonly SvgColourConverter _colourConverter;

        static SvgPaintServerFactory()
        {
            _colourConverter = new SvgColourConverter();
        }

        public static SvgPaintServer Create(string value, SvgDocument document)
        {
            // If it's pointing to a paint server
22
            if (string.IsNullOrEmpty(value))
davescriven's avatar
davescriven committed
23
            {
24
                return SvgColourServer.NotSet;
davescriven's avatar
davescriven committed
25
            }
26
27
28
29
30
31
32
33
            else if (value == "inherit")
            {
                return SvgColourServer.Inherit;
            }
            else if (value == "currentColor")
            {
                return new SvgDeferredPaintServer(document, value);
            }
Eric Domke's avatar
Eric Domke committed
34
            else
Eric Domke's avatar
Eric Domke committed
35
            {
Eric Domke's avatar
Eric Domke committed
36
37
38
                var servers = new List<SvgPaintServer>();
                
                while (!string.IsNullOrEmpty(value))
Eric Domke's avatar
Eric Domke committed
39
                {
Eric Domke's avatar
Eric Domke committed
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
                    if (value.StartsWith("url(#"))
                    {
                        var leftParen = value.IndexOf(')', 5);
                        Uri id = new Uri(value.Substring(5, leftParen - 5), UriKind.Relative);
                        value = value.Substring(leftParen + 1).Trim();
                        servers.Add((SvgPaintServer)document.IdManager.GetElementById(id));
                    }
                    // If referenced to to a different (linear or radial) gradient
                    else if (document.IdManager.GetElementById(value) != null && document.IdManager.GetElementById(value).GetType().BaseType == typeof(SvgGradientServer))
                    {
                        return (SvgPaintServer)document.IdManager.GetElementById(value);
                    }
                    else if (value.StartsWith("#")) // Otherwise try and parse as colour
                    {
                        switch(CountHexDigits(value, 1))
                        {
                            case 3:
                                servers.Add(new SvgColourServer((Color)_colourConverter.ConvertFrom(value.Substring(0, 4))));
                                value = value.Substring(4).Trim();
                                break;
                            case 6:
                                servers.Add(new SvgColourServer((Color)_colourConverter.ConvertFrom(value.Substring(0, 7))));
                                value = value.Substring(7).Trim();
                                break;
                            default:
                                return new SvgDeferredPaintServer(document, value);
                        }
                    }
                    else
                    {
                        return new SvgColourServer((Color)_colourConverter.ConvertFrom(value.Trim()));
                    }
Eric Domke's avatar
Eric Domke committed
72
                }
Eric Domke's avatar
Eric Domke committed
73
74

                if (servers.Count > 1)
Eric Domke's avatar
Eric Domke committed
75
                {
Eric Domke's avatar
Eric Domke committed
76
                    return new SvgFallbackPaintServer(servers[0], servers.Skip(1));
Eric Domke's avatar
Eric Domke committed
77
                }
Eric Domke's avatar
Eric Domke committed
78
79
80
81
82
83
84
85
86
87
88
89
90
91
                return servers[0];
            } 
                
                
        }

        private static int CountHexDigits(string value, int start)
        {
            int i = Math.Max(start, 0);
            int count = 0;
            while (i < value.Length && 
                   ((value[i] >= '0' && value[i] <= '9') ||
                    (value[i] >= 'a' && value[i] <= 'f') ||
                    (value[i] >= 'A' && value[i] <= 'F')))
davescriven's avatar
davescriven committed
92
            {
Eric Domke's avatar
Eric Domke committed
93
94
                count++;
                i++;
davescriven's avatar
davescriven committed
95
            }
Eric Domke's avatar
Eric Domke committed
96
            return count;
davescriven's avatar
davescriven committed
97
        }
Eric Domke's avatar
Eric Domke committed
98
        
davescriven's avatar
davescriven committed
99
100
        public override object ConvertFrom(ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value)
        {
101
102
            if (value is string)
            {
Tebjan Halm's avatar
Tebjan Halm committed
103
            	var s = (string) value;
Eric Domke's avatar
Eric Domke committed
104
                if (String.Equals(s.Trim(), "none", StringComparison.OrdinalIgnoreCase) || string.IsNullOrEmpty(s) || s.Trim().Length < 1)
rokam's avatar
rokam committed
105
            		return SvgPaintServer.None;
Tebjan Halm's avatar
Tebjan Halm committed
106
107
            	else
                	return SvgPaintServerFactory.Create(s, (SvgDocument)context);
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
            }

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

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

            return base.CanConvertFrom(context, sourceType);
        }

        public override bool CanConvertTo(ITypeDescriptorContext context, Type destinationType)
        {
            if (destinationType == typeof(string))
            {
                return true;
            }

            return base.CanConvertTo(context, destinationType);
        }

        public override object ConvertTo(ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, Type destinationType)
        {
            if (destinationType == typeof(string))
            {
137
138
139
                //check for none
                if (value == SvgPaintServer.None) return "none";

140
141
142
143
144
145
146
147
148
149
150
                var colourServer = value as SvgColourServer;

                if (colourServer != null)
                {
                    return new SvgColourConverter().ConvertTo(colourServer.Colour, typeof(string));
                }

                if (value != null)
                {
                    return string.Format(CultureInfo.InvariantCulture, "url(#{0})", ((SvgPaintServer)value).ID);
                }
Tebjan Halm's avatar
Tebjan Halm committed
151
152
153
154
                else
                {
                	return "none";
                }
155
156
157
158
            }

            return base.ConvertTo(context, culture, value, destinationType);
        }
davescriven's avatar
davescriven committed
159
    }
Eric Domke's avatar
Eric Domke committed
160
}