EnumConverters.cs 8.17 KB
Newer Older
1
2
using Svg.DataTypes;
using System;
3
4
5
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
42
43
44
45
using System.ComponentModel;
using System.Globalization;

namespace Svg
{
	//just overrrides canconvert and returns true
	public class BaseConverter : TypeConverter
    {
     	
        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 sealed class SvgBoolConverter : BaseConverter
	{
		public override object ConvertFrom(ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value)
		{
			if (value == null)
			{
				return true;
			}
			
			if (!(value is string))
			{
				throw new ArgumentOutOfRangeException("value must be a string.");
			}
C Moore's avatar
C Moore committed
46
47
48
49
50
51
52
53

			// Note: currently only used by SvgVisualElement.Visible but if
			// conversion is used elsewhere these checks below will need to change
			string visibility = (string)value;
			if ((visibility == "hidden") || (visibility == "collapse"))
				return false;
			else
				return true;
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
        }
		
		public override object ConvertTo(ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType)
        {
            if (destinationType == typeof(string))
            {
            	return ((bool)value) ? "visible" : "hidden";
            }

            return base.ConvertTo(context, culture, value, destinationType);
        }	
	}
	
	//converts enums to lower case strings
	public class EnumBaseConverter<T> : BaseConverter
    {
		public override object ConvertFrom(ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value)
		{
			if (value == null)
			{
				return Activator.CreateInstance(typeof(T));
			}
			
			if (!(value is string))
			{
				throw new ArgumentOutOfRangeException("value must be a string.");
			}

			return (T)Enum.Parse(typeof(T), (string)value, true);
        }
		
		public override object ConvertTo(ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType)
        {
87
            if (destinationType == typeof(string))
88
            {
89
            	return ((T)value).ToString().ToLower();
90
91
92
93
94
95
            }

            return base.ConvertTo(context, culture, value, destinationType);
        }	
	}
	
96
	//implementation for fill-rule
97
98
99
100
    public sealed class SvgFillRuleConverter : EnumBaseConverter<SvgFillRule>
    {
    }
    
101
    //implementaton for clip rule
102
103
104
105
    public sealed class SvgClipRuleConverter : EnumBaseConverter<SvgClipRule>
    {
    }
    
106
    //implementaton for clip rule
107
108
109
110
111
112
113
114
115
116
117
    public sealed class SvgTextAnchorConverter : EnumBaseConverter<SvgTextAnchor>
    {
    }
    
    public sealed class SvgStrokeLineCapConverter : EnumBaseConverter<SvgStrokeLineCap>
    {
    }

    public sealed class SvgStrokeLineJoinConverter : EnumBaseConverter<SvgStrokeLineJoin>
    {
    }
118
119
120
121

	public sealed class SvgMarkerUnitsConverter : EnumBaseConverter<SvgMarkerUnits>
	{
	}
122
123
124
125
126

    public sealed class SvgFontVariantConverter : EnumBaseConverter<SvgFontVariant>
    {
        public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object value)
        {
127
            if (value.ToString() == "small-caps") return SvgFontVariant.smallcaps;
128
129
130
131
            return base.ConvertFrom(context, culture, value);
        }
        public override object ConvertTo(ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType)
        {
132
            if (destinationType == typeof(string) && value is SvgFontVariant && (SvgFontVariant)value == SvgFontVariant.smallcaps)
133
134
135
136
137
138
139
            {
                return "small-caps";
            }
            return base.ConvertTo(context, culture, value, destinationType);
        }
    }

Eric Domke's avatar
Eric Domke committed
140
141
    public sealed class SvgCoordinateUnitsConverter : EnumBaseConverter<SvgCoordinateUnits>
    {
142
143
144
145
146
147
148
149
150
151
152
153
154
        public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object value)
        {
            if (value == null || value.ToString() == "") return SvgCoordinateUnits.Inherit;
            return base.ConvertFrom(context, culture, value);
        }
        public override object ConvertTo(ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType)
        {
            if (destinationType == typeof(string) && value is SvgCoordinateUnits && (SvgCoordinateUnits)value == SvgCoordinateUnits.Inherit)
            {
                return null;
            }
            return base.ConvertTo(context, culture, value, destinationType);
        }
Eric Domke's avatar
Eric Domke committed
155
156
    }

Eric Domke's avatar
Eric Domke committed
157
158
159
160
    public sealed class SvgTextDecorationConverter : EnumBaseConverter<SvgTextDecoration>
    {
        public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object value)
        {
161
            if (value.ToString() == "line-through") return SvgTextDecoration.lineThrough;
Eric Domke's avatar
Eric Domke committed
162
163
164
165
            return base.ConvertFrom(context, culture, value);
        }
        public override object ConvertTo(ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType)
        {
166
            if (destinationType == typeof(string) && value is SvgTextDecoration && (SvgTextDecoration)value == SvgTextDecoration.lineThrough)
Eric Domke's avatar
Eric Domke committed
167
168
169
170
171
172
173
            {
                return "line-through";
            }
            return base.ConvertTo(context, culture, value, destinationType);
        }
    }

174
175
176
177
178
179
180
181
    public sealed class SvgFontWeightConverter : EnumBaseConverter<SvgFontWeight>
    {
        public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object value)
        {
            if (value is string)
            {
                switch ((string)value)
                {
182
183
184
185
186
187
188
189
190
                    case "100": return SvgFontWeight.w100;
                    case "200": return SvgFontWeight.w200;
                    case "300": return SvgFontWeight.w300;
                    case "400": return SvgFontWeight.w400;
                    case "500": return SvgFontWeight.w500;
                    case "600": return SvgFontWeight.w600;
                    case "700": return SvgFontWeight.w700;
                    case "800": return SvgFontWeight.w800;
                    case "900": return SvgFontWeight.w900;
191
192
193
194
195
196
197
198
199
200
                }
            }
            return base.ConvertFrom(context, culture, value);
        }
        public override object ConvertTo(ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType)
        {
            if (destinationType == typeof(string) && value is SvgFontWeight)
            {
                switch ((SvgFontWeight)value)
                {
201
202
203
204
205
206
207
208
209
                    case SvgFontWeight.w100: return "100";
                    case SvgFontWeight.w200: return "200";
                    case SvgFontWeight.w300: return "300";
                    case SvgFontWeight.w400: return "400";
                    case SvgFontWeight.w500: return "500";
                    case SvgFontWeight.w600: return "600";
                    case SvgFontWeight.w700: return "700";
                    case SvgFontWeight.w800: return "800";
                    case SvgFontWeight.w900: return "900";
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
                }
            }
            return base.ConvertTo(context, culture, value, destinationType);
        }
    }

    public static class Enums 
    {
        public static bool TryParse<TEnum>(string value, out TEnum result) where TEnum : struct, IConvertible
        {
            var retValue = value == null ?
                        false :
                        Enum.IsDefined(typeof(TEnum), value);
            result = retValue ?
                        (TEnum)Enum.Parse(typeof(TEnum), value) :
                        default(TEnum);
            return retValue;
        }
    }
229
}