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

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

        static SvgPaintServerFactory()
        {
            _colourConverter = new SvgColourConverter();
            _urlRefPattern = new Regex(@"url\((#[^)]+)\)");
        }

        public static SvgPaintServer Create(string value, SvgDocument document)
        {
            // If it's pointing to a paint server
26
            if (string.IsNullOrEmpty(value))
davescriven's avatar
davescriven committed
27
            {
28
                return SvgColourServer.NotSet;
davescriven's avatar
davescriven committed
29
30
31
32
33
34
35
            }
            else if (value.IndexOf("url(#") > -1)
            {
                Match match = _urlRefPattern.Match(value);
                Uri id = new Uri(match.Groups[1].Value, UriKind.Relative);
                return (SvgPaintServer)document.IdManager.GetElementById(id);
            }
36
37
38
39
40
            // 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);
            }
41
42
43
44
45
46
47
48
49
50
51
52
            else if (value.StartsWith("#")) // Otherwise try and parse as colour
            {
                try
                {
                    return new SvgColourServer((Color)_colourConverter.ConvertFrom(value.Trim()));
                }
                catch
                {
                    return new SvgDeferredPaintServer(document, value);
                }
            }
            else
davescriven's avatar
davescriven committed
53
            {
54
                return new SvgColourServer((Color)_colourConverter.ConvertFrom(value.Trim()));
davescriven's avatar
davescriven committed
55
56
57
58
59
            }
        }

        public override object ConvertFrom(ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value)
        {
60
61
            if (value is string)
            {
Tebjan Halm's avatar
Tebjan Halm committed
62
            	var s = (string) value;
Eric Domke's avatar
Eric Domke committed
63
            	if(String.Equals( s.Trim(), "none", StringComparison.OrdinalIgnoreCase) || string.IsNullOrEmpty(s) || s.Trim().Length < 1)
rokam's avatar
rokam committed
64
            		return SvgPaintServer.None;
Tebjan Halm's avatar
Tebjan Halm committed
65
66
            	else
                	return SvgPaintServerFactory.Create(s, (SvgDocument)context);
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
            }

            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))
            {
96
97
98
                //check for none
                if (value == SvgPaintServer.None) return "none";

99
100
101
102
103
104
105
106
107
108
109
                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
110
111
112
113
                else
                {
                	return "none";
                }
114
115
116
117
            }

            return base.ConvertTo(context, culture, value, destinationType);
        }
davescriven's avatar
davescriven committed
118
119
    }
}