EnumConverters.cs 12 KB
Newer Older
1
2
using Svg.DataTypes;
using System;
3
4
5
6
7
using System.ComponentModel;
using System.Globalization;

namespace Svg
{
8
9
    //just overrrides canconvert and returns true
    public class BaseConverter : TypeConverter
10
    {
11
        
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
        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);
        }
    }
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
    
    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.");
            }

            // 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
        
        public override object ConvertTo(ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType)
57
58
59
        {
            if (destinationType == typeof(string))
            {
60
                return ((bool)value) ? "visible" : "hidden";
61
62
63
64
            }

            return base.ConvertTo(context, culture, value, destinationType);
        }	
65
66
67
68
    }
    
    //converts enums to lower case strings
    public class EnumBaseConverter<T> : BaseConverter
69
        where T : struct
70
    {
71
72
73
74
75
76
77
78
79
80
81
82
83
84
        /// <summary>If specified, upon conversion, the default value will result in 'null'.</summary>
        public T? DefaultValue { get; protected set;}

        /// <summary>Creates a new instance.</summary>
        public EnumBaseConverter() { }

        /// <summary>Creates a new instance.</summary>
        /// <param name="defaultValue">Specified the default value of the enum.</param>
        public EnumBaseConverter(T defaultValue)
        {
            this.DefaultValue = defaultValue;
        }

        /// <summary>Attempts to convert the provided value to <typeparamref name="T"/>.</summary>
85
86
87
88
        public override object ConvertFrom(ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value)
        {
            if (value == null)
            {
89
90
91
                if (this.DefaultValue.HasValue)
                    return this.DefaultValue.Value;
                
92
93
94
95
96
97
98
99
100
                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);
101
        }
102
        
103
        /// <summary>Attempts to convert the value to the destination type.</summary>
104
        public override object ConvertTo(ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType)
105
        {
106
            if (destinationType == typeof(string) && value is T)
107
            {
108
109
110
111
112
113
114
115
116
117
118
119
                //If the value id the default value, no need to write the attribute.
                if (this.DefaultValue.HasValue && Enum.Equals(value, this.DefaultValue.Value))
                    return null;
                else
                {
                    //SVG attributes should be camelCase.
                    string stringValue = ((T)value).ToString();

                    stringValue = string.Format("{0}{1}", stringValue[0].ToString().ToLower(), stringValue.Substring(1));

                    return stringValue;
                }
120
121
122
123
            }

            return base.ConvertTo(context, culture, value, destinationType);
        }	
124
125
    }
    
126
127
    public sealed class SvgFillRuleConverter : EnumBaseConverter<SvgFillRule>
    {
128
129
        public SvgFillRuleConverter() : base(SvgFillRule.NonZero) { }
    }
130
    
131
132
133
    public sealed class SvgColourInterpolationConverter : EnumBaseConverter<SvgColourInterpolation>
    {
        public SvgColourInterpolationConverter() : base(SvgColourInterpolation.SRGB) { }
134
135
136
137
    }
    
    public sealed class SvgClipRuleConverter : EnumBaseConverter<SvgClipRule>
    {
138
        public SvgClipRuleConverter() : base(SvgClipRule.NonZero) { }
139
140
141
142
    }
    
    public sealed class SvgTextAnchorConverter : EnumBaseConverter<SvgTextAnchor>
    {
143
        public SvgTextAnchorConverter() : base(SvgTextAnchor.Start) { }
144
145
146
147
    }
    
    public sealed class SvgStrokeLineCapConverter : EnumBaseConverter<SvgStrokeLineCap>
    {
148
        public SvgStrokeLineCapConverter() : base(SvgStrokeLineCap.Butt) { }
149
150
151
152
    }

    public sealed class SvgStrokeLineJoinConverter : EnumBaseConverter<SvgStrokeLineJoin>
    {
153
        public SvgStrokeLineJoinConverter() : base(SvgStrokeLineJoin.Miter) { }
154
    }
155

156
157
    public sealed class SvgMarkerUnitsConverter : EnumBaseConverter<SvgMarkerUnits>
    {
158
        public SvgMarkerUnitsConverter() : base(SvgMarkerUnits.StrokeWidth) { }
159
    }
160

161
162
    public sealed class SvgFontStyleConverter : EnumBaseConverter<SvgFontStyle>
    {
163
        public SvgFontStyleConverter() : base(SvgFontStyle.All) { }
164
    }
165

166
167
    public sealed class SvgOverflowConverter : EnumBaseConverter<SvgOverflow>
    {
168
        public SvgOverflowConverter() : base(SvgOverflow.Auto) { }
169
    }
170

171
172
    public sealed class SvgTextLengthAdjustConverter : EnumBaseConverter<SvgTextLengthAdjust>
    {
173
        public SvgTextLengthAdjustConverter() : base(SvgTextLengthAdjust.Spacing) { }
174
    }
175

176
177
    public sealed class SvgTextPathMethodConverter : EnumBaseConverter<SvgTextPathMethod>
    {
178
        public SvgTextPathMethodConverter() : base(SvgTextPathMethod.Align) { }
179
    }
180

181
182
    public sealed class SvgTextPathSpacingConverter : EnumBaseConverter<SvgTextPathSpacing>
    {
183
        public SvgTextPathSpacingConverter() : base(SvgTextPathSpacing.Exact) { }
184
185
186
187
188
189
    }
    
    public sealed class SvgShapeRenderingConverter : EnumBaseConverter<SvgShapeRendering>
    {
        public SvgShapeRenderingConverter() : base(SvgShapeRendering.Inherit) { }
    }
190

191
192
193
194
195
196
197
198
199
200
    public sealed class SvgTextRenderingConverter : EnumBaseConverter<SvgTextRendering>
    {
        public SvgTextRenderingConverter() : base(SvgTextRendering.Inherit) { }
    }

    public sealed class SvgImageRenderingConverter : EnumBaseConverter<SvgImageRendering>
    {
        public SvgImageRenderingConverter() : base(SvgImageRendering.Inherit) { }
    }
    
201
202
    public sealed class SvgFontVariantConverter : EnumBaseConverter<SvgFontVariant>
    {
203
204
        public SvgFontVariantConverter() : base(SvgFontVariant.Normal) { }

205
206
        public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object value)
        {
207
208
209
            if (value.ToString() == "small-caps")
                return SvgFontVariant.Smallcaps;
            
210
211
            return base.ConvertFrom(context, culture, value);
        }
212

213
214
        public override object ConvertTo(ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType)
        {
215
            if (destinationType == typeof(string) && value is SvgFontVariant && (SvgFontVariant)value == SvgFontVariant.Smallcaps)
216
217
218
            {
                return "small-caps";
            }
219
            
220
221
222
223
            return base.ConvertTo(context, culture, value, destinationType);
        }
    }

Eric Domke's avatar
Eric Domke committed
224
225
    public sealed class SvgCoordinateUnitsConverter : EnumBaseConverter<SvgCoordinateUnits>
    {
226
227
228
229
230
231
232
        //TODO Inherit is not actually valid. See TODO on SvgCoordinateUnits enum.
        public SvgCoordinateUnitsConverter() : base(SvgCoordinateUnits.Inherit) { }
    }

    public sealed class SvgGradientSpreadMethodConverter : EnumBaseConverter<SvgGradientSpreadMethod>
    {
        public SvgGradientSpreadMethodConverter() : base(SvgGradientSpreadMethod.Pad) { }
Eric Domke's avatar
Eric Domke committed
233
234
    }

Eric Domke's avatar
Eric Domke committed
235
236
    public sealed class SvgTextDecorationConverter : EnumBaseConverter<SvgTextDecoration>
    {
237
238
        public SvgTextDecorationConverter() : base(SvgTextDecoration.None) { }

Eric Domke's avatar
Eric Domke committed
239
240
        public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object value)
        {
241
242
243
            if (value.ToString() == "line-through") 
                return SvgTextDecoration.LineThrough;
            
Eric Domke's avatar
Eric Domke committed
244
245
            return base.ConvertFrom(context, culture, value);
        }
246

Eric Domke's avatar
Eric Domke committed
247
248
        public override object ConvertTo(ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType)
        {
249
            if (destinationType == typeof(string) && value is SvgTextDecoration && (SvgTextDecoration)value == SvgTextDecoration.LineThrough)
Eric Domke's avatar
Eric Domke committed
250
251
252
            {
                return "line-through";
            }
253

Eric Domke's avatar
Eric Domke committed
254
255
256
257
            return base.ConvertTo(context, culture, value, destinationType);
        }
    }

258
259
    public sealed class SvgFontWeightConverter : EnumBaseConverter<SvgFontWeight>
    {
260
261
262
        //TODO Defaulting to Normal although it should be All if this is used on a font face.
        public SvgFontWeightConverter() : base(SvgFontWeight.Normal) { }

263
264
265
266
267
268
        public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object value)
        {
            if (value is string)
            {
                switch ((string)value)
                {
269
270
271
272
273
274
275
276
277
                    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;
278
279
280
281
282
283
284
285
286
287
                }
            }
            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)
                {
288
289
290
291
292
293
294
295
296
                    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";
297
298
299
300
301
302
303
304
                }
            }
            return base.ConvertTo(context, culture, value, destinationType);
        }
    }

    public static class Enums 
    {
305
        [CLSCompliant(false)]
306
307
308
309
310
311
312
313
314
315
316
        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;
        }
    }
317
}