EnumConverters.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322
  1. using Svg.DataTypes;
  2. using System;
  3. using System.ComponentModel;
  4. using System.Globalization;
  5. #pragma warning disable
  6. namespace Svg
  7. {
  8. //just overrrides canconvert and returns true
  9. public class BaseConverter : TypeConverter
  10. {
  11. public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType)
  12. {
  13. if (sourceType == typeof(string))
  14. {
  15. return true;
  16. }
  17. return base.CanConvertFrom(context, sourceType);
  18. }
  19. public override bool CanConvertTo(ITypeDescriptorContext context, Type destinationType)
  20. {
  21. if (destinationType == typeof(string))
  22. {
  23. return true;
  24. }
  25. return base.CanConvertTo(context, destinationType);
  26. }
  27. }
  28. public sealed class SvgBoolConverter : BaseConverter
  29. {
  30. public override object ConvertFrom(ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value)
  31. {
  32. if (value == null)
  33. {
  34. return true;
  35. }
  36. if (!(value is string))
  37. {
  38. throw new ArgumentOutOfRangeException("value must be a string.");
  39. }
  40. // Note: currently only used by SvgVisualElement.Visible but if
  41. // conversion is used elsewhere these checks below will need to change
  42. string visibility = (string)value;
  43. if ((visibility == "hidden") || (visibility == "collapse"))
  44. return false;
  45. else
  46. return true;
  47. }
  48. public override object ConvertTo(ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType)
  49. {
  50. if (destinationType == typeof(string))
  51. {
  52. return ((bool)value) ? "visible" : "hidden";
  53. }
  54. return base.ConvertTo(context, culture, value, destinationType);
  55. }
  56. }
  57. //converts enums to lower case strings
  58. public class EnumBaseConverter<T> : BaseConverter
  59. where T : struct
  60. {
  61. /// <summary>If specified, upon conversion, the default value will result in 'null'.</summary>
  62. public T? DefaultValue { get; protected set;}
  63. /// <summary>Creates a new instance.</summary>
  64. public EnumBaseConverter() { }
  65. /// <summary>Creates a new instance.</summary>
  66. /// <param name="defaultValue">Specified the default value of the enum.</param>
  67. public EnumBaseConverter(T defaultValue)
  68. {
  69. this.DefaultValue = defaultValue;
  70. }
  71. /// <summary>Attempts to convert the provided value to <typeparamref name="T"/>.</summary>
  72. public override object ConvertFrom(ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value)
  73. {
  74. if (value == null)
  75. {
  76. if (this.DefaultValue.HasValue)
  77. return this.DefaultValue.Value;
  78. return Activator.CreateInstance(typeof(T));
  79. }
  80. if (!(value is string))
  81. {
  82. throw new ArgumentOutOfRangeException("value must be a string.");
  83. }
  84. return (T)Enum.Parse(typeof(T), (string)value, true);
  85. }
  86. /// <summary>Attempts to convert the value to the destination type.</summary>
  87. public override object ConvertTo(ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType)
  88. {
  89. if (destinationType == typeof(string) && value is T)
  90. {
  91. //If the value id the default value, no need to write the attribute.
  92. if (this.DefaultValue.HasValue && Enum.Equals(value, this.DefaultValue.Value))
  93. return null;
  94. else
  95. {
  96. //SVG attributes should be camelCase.
  97. string stringValue = ((T)value).ToString();
  98. stringValue = string.Format("{0}{1}", stringValue[0].ToString().ToLower(), stringValue.Substring(1));
  99. return stringValue;
  100. }
  101. }
  102. return base.ConvertTo(context, culture, value, destinationType);
  103. }
  104. }
  105. public sealed class SvgFillRuleConverter : EnumBaseConverter<SvgFillRule>
  106. {
  107. public SvgFillRuleConverter() : base(SvgFillRule.NonZero) { }
  108. }
  109. public sealed class SvgColourInterpolationConverter : EnumBaseConverter<SvgColourInterpolation>
  110. {
  111. public SvgColourInterpolationConverter() : base(SvgColourInterpolation.SRGB) { }
  112. }
  113. public sealed class SvgClipRuleConverter : EnumBaseConverter<SvgClipRule>
  114. {
  115. public SvgClipRuleConverter() : base(SvgClipRule.NonZero) { }
  116. }
  117. public sealed class SvgTextAnchorConverter : EnumBaseConverter<SvgTextAnchor>
  118. {
  119. public SvgTextAnchorConverter() : base(SvgTextAnchor.Start) { }
  120. }
  121. public sealed class SvgStrokeLineCapConverter : EnumBaseConverter<SvgStrokeLineCap>
  122. {
  123. public SvgStrokeLineCapConverter() : base(SvgStrokeLineCap.Butt) { }
  124. }
  125. public sealed class SvgStrokeLineJoinConverter : EnumBaseConverter<SvgStrokeLineJoin>
  126. {
  127. public SvgStrokeLineJoinConverter() : base(SvgStrokeLineJoin.Miter) { }
  128. }
  129. public sealed class SvgMarkerUnitsConverter : EnumBaseConverter<SvgMarkerUnits>
  130. {
  131. public SvgMarkerUnitsConverter() : base(SvgMarkerUnits.StrokeWidth) { }
  132. }
  133. public sealed class SvgFontStyleConverter : EnumBaseConverter<SvgFontStyle>
  134. {
  135. public SvgFontStyleConverter() : base(SvgFontStyle.All) { }
  136. }
  137. public sealed class SvgOverflowConverter : EnumBaseConverter<SvgOverflow>
  138. {
  139. public SvgOverflowConverter() : base(SvgOverflow.Auto) { }
  140. }
  141. public sealed class SvgTextLengthAdjustConverter : EnumBaseConverter<SvgTextLengthAdjust>
  142. {
  143. public SvgTextLengthAdjustConverter() : base(SvgTextLengthAdjust.Spacing) { }
  144. }
  145. public sealed class SvgTextPathMethodConverter : EnumBaseConverter<SvgTextPathMethod>
  146. {
  147. public SvgTextPathMethodConverter() : base(SvgTextPathMethod.Align) { }
  148. }
  149. public sealed class SvgTextPathSpacingConverter : EnumBaseConverter<SvgTextPathSpacing>
  150. {
  151. public SvgTextPathSpacingConverter() : base(SvgTextPathSpacing.Exact) { }
  152. }
  153. public sealed class SvgShapeRenderingConverter : EnumBaseConverter<SvgShapeRendering>
  154. {
  155. public SvgShapeRenderingConverter() : base(SvgShapeRendering.Inherit) { }
  156. }
  157. public sealed class SvgTextRenderingConverter : EnumBaseConverter<SvgTextRendering>
  158. {
  159. public SvgTextRenderingConverter() : base(SvgTextRendering.Inherit) { }
  160. }
  161. public sealed class SvgImageRenderingConverter : EnumBaseConverter<SvgImageRendering>
  162. {
  163. public SvgImageRenderingConverter() : base(SvgImageRendering.Inherit) { }
  164. }
  165. public sealed class SvgFontVariantConverter : EnumBaseConverter<SvgFontVariant>
  166. {
  167. public SvgFontVariantConverter() : base(SvgFontVariant.Normal) { }
  168. public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object value)
  169. {
  170. if (value.ToString() == "small-caps")
  171. return SvgFontVariant.Smallcaps;
  172. return base.ConvertFrom(context, culture, value);
  173. }
  174. public override object ConvertTo(ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType)
  175. {
  176. if (destinationType == typeof(string) && value is SvgFontVariant && (SvgFontVariant)value == SvgFontVariant.Smallcaps)
  177. {
  178. return "small-caps";
  179. }
  180. return base.ConvertTo(context, culture, value, destinationType);
  181. }
  182. }
  183. public sealed class SvgCoordinateUnitsConverter : EnumBaseConverter<SvgCoordinateUnits>
  184. {
  185. //TODO Inherit is not actually valid. See TODO on SvgCoordinateUnits enum.
  186. public SvgCoordinateUnitsConverter() : base(SvgCoordinateUnits.Inherit) { }
  187. }
  188. public sealed class SvgGradientSpreadMethodConverter : EnumBaseConverter<SvgGradientSpreadMethod>
  189. {
  190. public SvgGradientSpreadMethodConverter() : base(SvgGradientSpreadMethod.Pad) { }
  191. }
  192. public sealed class SvgTextDecorationConverter : EnumBaseConverter<SvgTextDecoration>
  193. {
  194. public SvgTextDecorationConverter() : base(SvgTextDecoration.None) { }
  195. public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object value)
  196. {
  197. if (value.ToString() == "line-through")
  198. return SvgTextDecoration.LineThrough;
  199. return base.ConvertFrom(context, culture, value);
  200. }
  201. public override object ConvertTo(ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType)
  202. {
  203. if (destinationType == typeof(string) && value is SvgTextDecoration && (SvgTextDecoration)value == SvgTextDecoration.LineThrough)
  204. {
  205. return "line-through";
  206. }
  207. return base.ConvertTo(context, culture, value, destinationType);
  208. }
  209. }
  210. public sealed class SvgFontWeightConverter : EnumBaseConverter<SvgFontWeight>
  211. {
  212. //TODO Defaulting to Normal although it should be All if this is used on a font face.
  213. public SvgFontWeightConverter() : base(SvgFontWeight.Normal) { }
  214. public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object value)
  215. {
  216. if (value is string)
  217. {
  218. switch ((string)value)
  219. {
  220. case "100": return SvgFontWeight.W100;
  221. case "200": return SvgFontWeight.W200;
  222. case "300": return SvgFontWeight.W300;
  223. case "400": return SvgFontWeight.W400;
  224. case "500": return SvgFontWeight.W500;
  225. case "600": return SvgFontWeight.W600;
  226. case "700": return SvgFontWeight.W700;
  227. case "800": return SvgFontWeight.W800;
  228. case "900": return SvgFontWeight.W900;
  229. }
  230. }
  231. return base.ConvertFrom(context, culture, value);
  232. }
  233. public override object ConvertTo(ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType)
  234. {
  235. if (destinationType == typeof(string) && value is SvgFontWeight)
  236. {
  237. switch ((SvgFontWeight)value)
  238. {
  239. case SvgFontWeight.W100: return "100";
  240. case SvgFontWeight.W200: return "200";
  241. case SvgFontWeight.W300: return "300";
  242. case SvgFontWeight.W400: return "400";
  243. case SvgFontWeight.W500: return "500";
  244. case SvgFontWeight.W600: return "600";
  245. case SvgFontWeight.W700: return "700";
  246. case SvgFontWeight.W800: return "800";
  247. case SvgFontWeight.W900: return "900";
  248. }
  249. }
  250. return base.ConvertTo(context, culture, value, destinationType);
  251. }
  252. }
  253. public static class Enums
  254. {
  255. [CLSCompliant(false)]
  256. public static bool TryParse<TEnum>(string value, out TEnum result) where TEnum : struct, IConvertible
  257. {
  258. var retValue = value == null ?
  259. false :
  260. Enum.IsDefined(typeof(TEnum), value);
  261. result = retValue ?
  262. (TEnum)Enum.Parse(typeof(TEnum), value) :
  263. default(TEnum);
  264. return retValue;
  265. }
  266. }
  267. }
  268. #pragma warning restore