From 6b08837f577ea9ba17a049a4f77e46e9be7fcf9d Mon Sep 17 00:00:00 2001 From: Josh McCullough Date: Wed, 11 Feb 2015 13:07:37 -0500 Subject: [PATCH] Fixed many attribute enums. Now they will 1) output the proper case, 2) only output if non-null and non-default, 3) include more documentation, 4) are marked as Flags where appropriate. --- Source/DataTypes/SvgAspectRatio.cs | 1 + Source/DataTypes/SvgColourInterpolation.cs | 19 ++- Source/DataTypes/SvgCoordinateUnits.cs | 4 + Source/DataTypes/SvgFontStyle.cs | 19 ++- Source/DataTypes/SvgFontVariant.cs | 6 +- Source/DataTypes/SvgFontWeight.cs | 63 ++++++-- Source/DataTypes/SvgMarkerUnits.cs | 8 +- Source/DataTypes/SvgOverflow.cs | 35 +++- Source/DataTypes/SvgTextDecoration.cs | 25 ++- Source/DataTypes/SvgTextLengthAdjust.cs | 14 +- Source/DataTypes/SvgTextPathMethod.cs | 10 +- Source/DataTypes/SvgTextPathSpacing.cs | 10 +- Source/DataTypes/SvgUnitCollection.cs | 2 +- Source/Document Structure/SvgFragment.cs | 6 +- .../feColourMatrix/SvgColourMatrix.cs | 6 +- .../feColourMatrix/SvgColourMatrixType.cs | 10 +- Source/Painting/EnumConverters.cs | 152 +++++++++++++----- Source/Painting/SvgGradientSpreadMethod.cs | 12 ++ Source/Painting/SvgMarker.cs | 16 +- Source/Painting/SvgStrokeLineCap.cs | 12 +- Source/Painting/SvgStrokeLineJoin.cs | 9 ++ Source/SvgAttributeCollection.cs | 11 +- Source/SvgDocument.cs | 2 +- Source/SvgElement.cs | 9 +- Source/SvgElementStyle.cs | 28 ++-- Source/Text/SvgFontFace.cs | 6 +- Source/Text/SvgTextAnchor.cs | 3 +- Source/Text/SvgTextBase.cs | 6 +- Source/Text/SvgTextPath.cs | 4 +- 29 files changed, 368 insertions(+), 140 deletions(-) diff --git a/Source/DataTypes/SvgAspectRatio.cs b/Source/DataTypes/SvgAspectRatio.cs index 77ae817..4de7213 100644 --- a/Source/DataTypes/SvgAspectRatio.cs +++ b/Source/DataTypes/SvgAspectRatio.cs @@ -50,6 +50,7 @@ namespace Svg } + [TypeConverter(typeof(SvgPreserveAspectRatioConverter))] public enum SvgPreserveAspectRatio { xMidYMid, //default diff --git a/Source/DataTypes/SvgColourInterpolation.cs b/Source/DataTypes/SvgColourInterpolation.cs index 4d66653..ad17513 100644 --- a/Source/DataTypes/SvgColourInterpolation.cs +++ b/Source/DataTypes/SvgColourInterpolation.cs @@ -1,15 +1,26 @@ using System; using System.Collections.Generic; +using System.ComponentModel; using System.Linq; using System.Text; namespace Svg.DataTypes { + /// Specifies the color space for gradient interpolations, color animations and alpha compositing. + /// When a child element is blended into a background, the value of the ‘color-interpolation’ property on the child determines the type of blending, not the value of the ‘color-interpolation’ on the parent. For gradients which make use of the ‘xlink:href’ attribute to reference another gradient, the gradient uses the ‘color-interpolation’ property value from the gradient element which is directly referenced by the ‘fill’ or ‘stroke’ property. When animating colors, color interpolation is performed according to the value of the ‘color-interpolation’ property on the element being animated. + [TypeConverter(typeof(SvgColourInterpolationConverter))] public enum SvgColourInterpolation { - auto, - sRGB, - linearRGB, - inherit + /// Indicates that the user agent can choose either the sRGB or linearRGB spaces for color interpolation. This option indicates that the author doesn't require that color interpolation occur in a particular color space. + Auto, + + /// Indicates that color interpolation should occur in the sRGB color space. + SRGB, + + /// Indicates that color interpolation should occur in the linearized RGB color space as described above. + LinearRGB, + + /// The value is inherited from the parent element. + Inherit } } diff --git a/Source/DataTypes/SvgCoordinateUnits.cs b/Source/DataTypes/SvgCoordinateUnits.cs index a17c83e..e4b5a20 100644 --- a/Source/DataTypes/SvgCoordinateUnits.cs +++ b/Source/DataTypes/SvgCoordinateUnits.cs @@ -6,17 +6,21 @@ using System.ComponentModel; namespace Svg { + //TODO Need to split this enum into separate inherited enums for GradientCoordinateUnits, ClipPathCoordinateUnits, etc. as each should have its own converter since they have different defaults. /// /// Defines the various coordinate units certain SVG elements may use. /// [TypeConverter(typeof(SvgCoordinateUnitsConverter))] public enum SvgCoordinateUnits { + //TODO Inherit is not actually valid Inherit, + /// /// Indicates that the coordinate system of the owner element is to be used. /// ObjectBoundingBox, + /// /// Indicates that the coordinate system of the entire document is to be used. /// diff --git a/Source/DataTypes/SvgFontStyle.cs b/Source/DataTypes/SvgFontStyle.cs index 2153f6b..95708ab 100644 --- a/Source/DataTypes/SvgFontStyle.cs +++ b/Source/DataTypes/SvgFontStyle.cs @@ -1,15 +1,26 @@ using System; using System.Collections.Generic; +using System.ComponentModel; using System.Linq; using System.Text; namespace Svg { + /// This is the descriptor for the style of a font and takes the same values as the 'font-style' property, except that a comma-separated list is permitted. + [TypeConverter(typeof(SvgFontStyleConverter))] + [Flags] public enum SvgFontStyle { - normal, - italic, - oblique, - inherit + /// Indicates that the font-face supplies all styles (normal, oblique and italic). + All = (Normal | Oblique | Italic), + + /// Specifies a font that is classified as 'normal' in the UA's font database. + Normal = 1, + + /// Specifies a font that is classified as 'oblique' in the UA's font database. Fonts with Oblique, Slanted, or Incline in their names will typically be labeled 'oblique' in the font database. A font that is labeled 'oblique' in the UA's font database may actually have been generated by electronically slanting a normal font. + Oblique = 2, + + /// Specifies a font that is classified as 'italic' in the UA's font database, or, if that is not available, one labeled 'oblique'. Fonts with Italic, Cursive, or Kursiv in their names will typically be labeled 'italic' + Italic = 4 } } diff --git a/Source/DataTypes/SvgFontVariant.cs b/Source/DataTypes/SvgFontVariant.cs index 40971b8..f1f81ac 100644 --- a/Source/DataTypes/SvgFontVariant.cs +++ b/Source/DataTypes/SvgFontVariant.cs @@ -9,8 +9,8 @@ namespace Svg [TypeConverter(typeof(SvgFontVariantConverter))] public enum SvgFontVariant { - normal, - smallcaps, - inherit + Normal, + Smallcaps, + Inherit } } diff --git a/Source/DataTypes/SvgFontWeight.cs b/Source/DataTypes/SvgFontWeight.cs index 55001f4..31773f1 100644 --- a/Source/DataTypes/SvgFontWeight.cs +++ b/Source/DataTypes/SvgFontWeight.cs @@ -6,22 +6,57 @@ using System.ComponentModel; namespace Svg { + //TODO This should be split out to define an enum for the font face element and text element. + /// The weight of a face relative to others in the same font family. [TypeConverter(typeof(SvgFontWeightConverter))] + [Flags] public enum SvgFontWeight { - inherit, - normal, - bold, - bolder, - lighter, - w100, - w200, - w300, - w400, // same as normal - w500, - w600, - w700, // same as bold - w800, - w900 + //TODO All Is not valid for text elements, but is is for font face elements. + /// All font weights. + All = (W100 | W200 | W300 | W400 | W500 | W600 | W700 | W800 | W900), + + //TODO Inherit Is not valid for font face elements, but is is for text elements. + /// The value is inherited from the parent element. + Inherit = 0, + + /// Same as . + Normal = W400, + + /// Same as . + Bold = W700, + + /// One font weight darker than the parent element. + Bolder = 512, + + /// One font weight lighter than the parent element. + Lighter = 1024, + + /// + W100 = 1, + + /// + W200 = 2, + + /// + W300 = 4, + + /// Same as . + W400 = 8, + + /// + W500 = 16, + + /// + W600 = 32, + + /// Same as . + W700 = 64, + + /// + W800 = 128, + + /// + W900 = 256 } } diff --git a/Source/DataTypes/SvgMarkerUnits.cs b/Source/DataTypes/SvgMarkerUnits.cs index 9b0aa78..1603bbb 100644 --- a/Source/DataTypes/SvgMarkerUnits.cs +++ b/Source/DataTypes/SvgMarkerUnits.cs @@ -2,10 +2,14 @@ namespace Svg.DataTypes { + /// Defines the coordinate system for attributes ‘markerWidth’, ‘markerHeight’ and the contents of the ‘marker’. [TypeConverter(typeof(SvgMarkerUnitsConverter))] public enum SvgMarkerUnits { - strokeWidth, - userSpaceOnUse + /// If markerUnits="strokeWidth", ‘markerWidth’, ‘markerHeight’ and the contents of the ‘marker’ represent values in a coordinate system which has a single unit equal the size in user units of the current stroke width (see the ‘stroke-width’ property) in place for the graphic object referencing the marker. + StrokeWidth, + + /// If markerUnits="userSpaceOnUse", ‘markerWidth’, ‘markerHeight’ and the contents of the ‘marker’ represent values in the current user coordinate system in place for the graphic object referencing the marker (i.e., the user coordinate system for the element referencing the ‘marker’ element via a ‘marker’, ‘marker-start’, ‘marker-mid’ or ‘marker-end’ property). + UserSpaceOnUse } } diff --git a/Source/DataTypes/SvgOverflow.cs b/Source/DataTypes/SvgOverflow.cs index 1d7ae83..f5daa78 100644 --- a/Source/DataTypes/SvgOverflow.cs +++ b/Source/DataTypes/SvgOverflow.cs @@ -1,16 +1,39 @@ using System; using System.Collections.Generic; +using System.ComponentModel; using System.Linq; using System.Text; namespace Svg { + /// The ‘overflow’ property applies to elements that establish new viewports (e.g., ‘svg’ elements), ‘pattern’ elements and ‘marker’ elements. For all other elements, the property has no effect (i.e., a clipping rectangle is not created). + /// + /// The ‘overflow’ property has the same parameter values and has the same meaning as defined in CSS2 ([CSS2], section 11.1.1); however, the following additional points apply: + /// The ‘overflow’ property applies to elements that establish new viewports (e.g., ‘svg’ elements), ‘pattern’ elements and ‘marker’ elements. For all other elements, the property has no effect (i.e., a clipping rectangle is not created). + /// For those elements to which the ‘overflow’ property can apply, if the ‘overflow’ property has the value hidden or scroll, the effect is that a new clipping path in the shape of a rectangle is created. The result is equivalent to defining a ‘clipPath’ element whose content is a ‘rect’ element which defines the equivalent rectangle, and then specifying the of this ‘clipPath’ element on the ‘clip-path’ property for the given element. + /// If the ‘overflow’ property has a value other than hidden or scroll, the property has no effect (i.e., a clipping rectangle is not created). + /// Within SVG content, the value auto is equivalent to the value visible. + /// When an outermost svg element is embedded inline within a parent XML grammar which uses CSS layout ([CSS2], chapter 9) or XSL formatting [XSL], if the ‘overflow’ property has the value hidden or scroll, then the user agent will establish an initial clipping path equal to the bounds of the initial viewport; otherwise, the initial clipping path is set according to the clipping rules as defined in CSS2 ([CSS2], section 11.1.1). + /// When an outermost svg element is stand-alone or embedded inline within a parent XML grammar which does not use CSS layout or XSL formatting, the ‘overflow’ property on the outermost svg element is ignored for the purposes of visual rendering and the initial clipping path is set to the bounds of the initial viewport. + /// The initial value for ‘overflow’ as defined in [CSS2-overflow] is 'visible', and this applies also to the root ‘svg’ element; however, for child elements of an SVG document, SVG's user agent style sheet overrides this initial value and sets the ‘overflow’ property on elements that establish new viewports (e.g., ‘svg’ elements), ‘pattern’ elements and ‘marker’ elements to the value 'hidden'. + /// As a result of the above, the default behavior of SVG user agents is to establish a clipping path to the bounds of the initial viewport and to establish a new clipping path for each element which establishes a new viewport and each ‘pattern’ and ‘marker’ element. + /// + [TypeConverter(typeof(SvgOverflowConverter))] public enum SvgOverflow - { - inherit, - auto, - visible, - hidden, - scroll + { + /// The value is inherited from the parent element. + Inherit, + + /// The overflow is rendered - same as "visible". + Auto, + + /// Overflow is rendered. + Visible, + + /// Overflow is not rendered. + Hidden, + + /// Overflow causes a scrollbar to appear (horizontal, vertical or both). + Scroll } } diff --git a/Source/DataTypes/SvgTextDecoration.cs b/Source/DataTypes/SvgTextDecoration.cs index 50f9523..42b46fa 100644 --- a/Source/DataTypes/SvgTextDecoration.cs +++ b/Source/DataTypes/SvgTextDecoration.cs @@ -6,14 +6,27 @@ using System.ComponentModel; namespace Svg { + /// This property describes decorations that are added to the text of an element. Conforming SVG Viewers are not required to support the blink value. [TypeConverter(typeof(SvgTextDecorationConverter))] + [Flags] public enum SvgTextDecoration { - inherit, - none, - underline, - overline, - lineThrough, - blink + /// The value is inherited from the parent element. + Inherit = 0, + + /// The text is not decorated + None = 1, + + /// The text is underlined. + Underline = 2, + + /// The text is overlined. + Overline = 4, + + /// The text is struck through. + LineThrough = 8, + + /// The text will blink. + Blink = 16 } } diff --git a/Source/DataTypes/SvgTextLengthAdjust.cs b/Source/DataTypes/SvgTextLengthAdjust.cs index ab68d3a..7276e8c 100644 --- a/Source/DataTypes/SvgTextLengthAdjust.cs +++ b/Source/DataTypes/SvgTextLengthAdjust.cs @@ -1,13 +1,23 @@ using System; using System.Collections.Generic; +using System.ComponentModel; using System.Linq; using System.Text; namespace Svg { + /// Indicates the type of adjustments which the user agent shall make to make the rendered length of the text match the value specified on the ‘textLength’ attribute. + /// + /// The user agent is required to achieve correct start and end positions for the text strings, but the locations of intermediate glyphs are not predictable because user agents might employ advanced algorithms to stretch or compress text strings in order to balance correct start and end positioning with optimal typography. + /// Note that, for a text string that contains n characters, the adjustments to the advance values often occur only for n−1 characters (see description of attribute ‘textLength’), whereas stretching or compressing of the glyphs will be applied to all n characters. + /// + [TypeConverter(typeof(SvgTextLengthAdjustConverter))] public enum SvgTextLengthAdjust { - spacing, - spacingAndGlyphs + /// Indicates that only the advance values are adjusted. The glyphs themselves are not stretched or compressed. + Spacing, + + /// Indicates that the advance values are adjusted and the glyphs themselves stretched or compressed in one axis (i.e., a direction parallel to the inline-progression-direction). + SpacingAndGlyphs } } diff --git a/Source/DataTypes/SvgTextPathMethod.cs b/Source/DataTypes/SvgTextPathMethod.cs index 64aac32..a1ca800 100644 --- a/Source/DataTypes/SvgTextPathMethod.cs +++ b/Source/DataTypes/SvgTextPathMethod.cs @@ -1,13 +1,19 @@ using System; using System.Collections.Generic; +using System.ComponentModel; using System.Linq; using System.Text; namespace Svg { + /// Indicates the method by which text should be rendered along the path. + [TypeConverter(typeof(SvgTextPathMethodConverter))] public enum SvgTextPathMethod { - align, - stretch + /// Indicates that the glyphs should be rendered using simple 2x3 transformations such that there is no stretching/warping of the glyphs. Typically, supplemental rotation, scaling and translation transformations are done for each glyph to be rendered. As a result, with align, fonts where the glyphs are designed to be connected (e.g., cursive fonts), the connections may not align properly when text is rendered along a path. + Align, + + /// Indicates that the glyph outlines will be converted into paths, and then all end points and control points will be adjusted to be along the perpendicular vectors from the path, thereby stretching and possibly warping the glyphs. With this approach, connected glyphs, such as in cursive scripts, will maintain their connections. + Stretch } } diff --git a/Source/DataTypes/SvgTextPathSpacing.cs b/Source/DataTypes/SvgTextPathSpacing.cs index e53d8fa..b988c09 100644 --- a/Source/DataTypes/SvgTextPathSpacing.cs +++ b/Source/DataTypes/SvgTextPathSpacing.cs @@ -1,13 +1,19 @@ using System; using System.Collections.Generic; +using System.ComponentModel; using System.Linq; using System.Text; namespace Svg { + /// Indicates how the user agent should determine the spacing between glyphs that are to be rendered along a path. + [TypeConverter(typeof(SvgTextPathSpacingConverter))] public enum SvgTextPathSpacing { - exact, - auto + /// Indicates that the glyphs should be rendered exactly according to the spacing rules as specified in Text on a path layout rules. + Exact, + + /// Indicates that the user agent should use text-on-a-path layout algorithms to adjust the spacing between glyphs in order to achieve visually appealing results. + Auto } } diff --git a/Source/DataTypes/SvgUnitCollection.cs b/Source/DataTypes/SvgUnitCollection.cs index 4d831d5..866bb44 100644 --- a/Source/DataTypes/SvgUnitCollection.cs +++ b/Source/DataTypes/SvgUnitCollection.cs @@ -79,7 +79,7 @@ namespace Svg public override object ConvertTo(ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType) { - if (destinationType == typeof(string)) + if (value != null && destinationType == typeof(string)) { return ((SvgUnitCollection)value).ToString(); } diff --git a/Source/Document Structure/SvgFragment.cs b/Source/Document Structure/SvgFragment.cs index 6233669..c17ccda 100644 --- a/Source/Document Structure/SvgFragment.cs +++ b/Source/Document Structure/SvgFragment.cs @@ -162,9 +162,9 @@ namespace Svg { switch (this.Overflow) { - case SvgOverflow.auto: - case SvgOverflow.visible: - case SvgOverflow.scroll: + case SvgOverflow.Auto: + case SvgOverflow.Visible: + case SvgOverflow.Scroll: base.Render(renderer); break; default: diff --git a/Source/Filter Effects/feColourMatrix/SvgColourMatrix.cs b/Source/Filter Effects/feColourMatrix/SvgColourMatrix.cs index 18320c8..8f8804d 100644 --- a/Source/Filter Effects/feColourMatrix/SvgColourMatrix.cs +++ b/Source/Filter Effects/feColourMatrix/SvgColourMatrix.cs @@ -42,7 +42,7 @@ namespace Svg.FilterEffects float value; switch (this.Type) { - case SvgColourMatrixType.hueRotate: + case SvgColourMatrixType.HueRotate: value = (string.IsNullOrEmpty(this.Values) ? 0 : float.Parse(this.Values)); colorMatrixElements = new float[][] { new float[] {(float)(0.213 + Math.Cos(value) * +0.787 + Math.Sin(value) * -0.213), @@ -58,7 +58,7 @@ namespace Svg.FilterEffects new float[] {0, 0, 0, 0, 1} }; break; - case SvgColourMatrixType.luminanceToAlpha: + case SvgColourMatrixType.LuminanceToAlpha: colorMatrixElements = new float[][] { new float[] {0, 0, 0, 0, 0}, new float[] {0, 0, 0, 0, 0}, @@ -67,7 +67,7 @@ namespace Svg.FilterEffects new float[] {0, 0, 0, 0, 1} }; break; - case SvgColourMatrixType.saturate: + case SvgColourMatrixType.Saturate: value = (string.IsNullOrEmpty(this.Values) ? 1 : float.Parse(this.Values)); colorMatrixElements = new float[][] { new float[] {(float)(0.213+0.787*value), (float)(0.715-0.715*value), (float)(0.072-0.072*value), 0, 0}, diff --git a/Source/Filter Effects/feColourMatrix/SvgColourMatrixType.cs b/Source/Filter Effects/feColourMatrix/SvgColourMatrixType.cs index 9941f63..fe9694c 100644 --- a/Source/Filter Effects/feColourMatrix/SvgColourMatrixType.cs +++ b/Source/Filter Effects/feColourMatrix/SvgColourMatrixType.cs @@ -1,15 +1,17 @@ using System; using System.Collections.Generic; +using System.ComponentModel; using System.Linq; using System.Text; namespace Svg.FilterEffects { + [TypeConverter(typeof(EnumBaseConverter))] public enum SvgColourMatrixType { - matrix, - saturate, - hueRotate, - luminanceToAlpha + Matrix, + Saturate, + HueRotate, + LuminanceToAlpha } } diff --git a/Source/Painting/EnumConverters.cs b/Source/Painting/EnumConverters.cs index d048d14..a9e98df 100644 --- a/Source/Painting/EnumConverters.cs +++ b/Source/Painting/EnumConverters.cs @@ -66,11 +66,29 @@ namespace Svg //converts enums to lower case strings public class EnumBaseConverter : BaseConverter + where T : struct { + /// If specified, upon conversion, the default value will result in 'null'. + public T? DefaultValue { get; protected set;} + + /// Creates a new instance. + public EnumBaseConverter() { } + + /// Creates a new instance. + /// Specified the default value of the enum. + public EnumBaseConverter(T defaultValue) + { + this.DefaultValue = defaultValue; + } + + /// Attempts to convert the provided value to . public override object ConvertFrom(ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) { if (value == null) { + if (this.DefaultValue.HasValue) + return this.DefaultValue.Value; + return Activator.CreateInstance(typeof(T)); } @@ -82,112 +100,166 @@ namespace Svg return (T)Enum.Parse(typeof(T), (string)value, true); } + /// Attempts to convert the value to the destination type. public override object ConvertTo(ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType) { - if (destinationType == typeof(string)) + if (destinationType == typeof(string) && value is T) { - return ((T)value).ToString().ToLower(); + //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; + } } return base.ConvertTo(context, culture, value, destinationType); } } - //implementation for fill-rule public sealed class SvgFillRuleConverter : EnumBaseConverter { + public SvgFillRuleConverter() : base(SvgFillRule.NonZero) { } + } + + public sealed class SvgColourInterpolationConverter : EnumBaseConverter + { + public SvgColourInterpolationConverter() : base(SvgColourInterpolation.SRGB) { } } - //implementaton for clip rule public sealed class SvgClipRuleConverter : EnumBaseConverter { + public SvgClipRuleConverter() : base(SvgClipRule.NonZero) { } } - //implementaton for clip rule public sealed class SvgTextAnchorConverter : EnumBaseConverter { + public SvgTextAnchorConverter() : base(SvgTextAnchor.Start) { } } public sealed class SvgStrokeLineCapConverter : EnumBaseConverter { + public SvgStrokeLineCapConverter() : base(SvgStrokeLineCap.Butt) { } } public sealed class SvgStrokeLineJoinConverter : EnumBaseConverter { + public SvgStrokeLineJoinConverter() : base(SvgStrokeLineJoin.Miter) { } } public sealed class SvgMarkerUnitsConverter : EnumBaseConverter { + public SvgMarkerUnitsConverter() : base(SvgMarkerUnits.StrokeWidth) { } + } + + public sealed class SvgFontStyleConverter : EnumBaseConverter + { + public SvgFontStyleConverter() : base(SvgFontStyle.All) { } + } + + public sealed class SvgOverflowConverter : EnumBaseConverter + { + public SvgOverflowConverter() : base(SvgOverflow.Auto) { } + } + + public sealed class SvgTextLengthAdjustConverter : EnumBaseConverter + { + public SvgTextLengthAdjustConverter() : base(SvgTextLengthAdjust.Spacing) { } + } + + public sealed class SvgTextPathMethodConverter : EnumBaseConverter + { + public SvgTextPathMethodConverter() : base(SvgTextPathMethod.Align) { } + } + + public sealed class SvgTextPathSpacingConverter : EnumBaseConverter + { + public SvgTextPathSpacingConverter() : base(SvgTextPathSpacing.Exact) { } } public sealed class SvgFontVariantConverter : EnumBaseConverter { + public SvgFontVariantConverter() : base(SvgFontVariant.Normal) { } + public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object value) { - if (value.ToString() == "small-caps") return SvgFontVariant.smallcaps; + if (value.ToString() == "small-caps") + return SvgFontVariant.Smallcaps; + return base.ConvertFrom(context, culture, value); } + public override object ConvertTo(ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType) { - if (destinationType == typeof(string) && value is SvgFontVariant && (SvgFontVariant)value == SvgFontVariant.smallcaps) + if (destinationType == typeof(string) && value is SvgFontVariant && (SvgFontVariant)value == SvgFontVariant.Smallcaps) { return "small-caps"; } + return base.ConvertTo(context, culture, value, destinationType); } } public sealed class SvgCoordinateUnitsConverter : EnumBaseConverter { - 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); - } + //TODO Inherit is not actually valid. See TODO on SvgCoordinateUnits enum. + public SvgCoordinateUnitsConverter() : base(SvgCoordinateUnits.Inherit) { } + } + + public sealed class SvgGradientSpreadMethodConverter : EnumBaseConverter + { + public SvgGradientSpreadMethodConverter() : base(SvgGradientSpreadMethod.Pad) { } } public sealed class SvgTextDecorationConverter : EnumBaseConverter { + public SvgTextDecorationConverter() : base(SvgTextDecoration.None) { } + public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object value) { - if (value.ToString() == "line-through") return SvgTextDecoration.lineThrough; + if (value.ToString() == "line-through") + return SvgTextDecoration.LineThrough; + return base.ConvertFrom(context, culture, value); } + public override object ConvertTo(ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType) { - if (destinationType == typeof(string) && value is SvgTextDecoration && (SvgTextDecoration)value == SvgTextDecoration.lineThrough) + if (destinationType == typeof(string) && value is SvgTextDecoration && (SvgTextDecoration)value == SvgTextDecoration.LineThrough) { return "line-through"; } + return base.ConvertTo(context, culture, value, destinationType); } } public sealed class SvgFontWeightConverter : EnumBaseConverter { + //TODO Defaulting to Normal although it should be All if this is used on a font face. + public SvgFontWeightConverter() : base(SvgFontWeight.Normal) { } + public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object value) { if (value is string) { switch ((string)value) { - 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; + 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; } } return base.ConvertFrom(context, culture, value); @@ -198,15 +270,15 @@ namespace Svg { switch ((SvgFontWeight)value) { - 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"; + 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"; } } return base.ConvertTo(context, culture, value, destinationType); diff --git a/Source/Painting/SvgGradientSpreadMethod.cs b/Source/Painting/SvgGradientSpreadMethod.cs index 0041a6a..6c7453e 100644 --- a/Source/Painting/SvgGradientSpreadMethod.cs +++ b/Source/Painting/SvgGradientSpreadMethod.cs @@ -1,14 +1,26 @@ using System; using System.Collections.Generic; +using System.ComponentModel; using System.Linq; using System.Text; namespace Svg { + /// Indicates what happens if the gradient starts or ends inside the bounds of the target rectangle. + /// + /// Possible values are: 'pad', which says to use the terminal colors of the gradient to fill the remainder of the target region, 'reflect', which says to reflect the gradient pattern start-to-end, end-to-start, start-to-end, etc. continuously until the target rectangle is filled, and repeat, which says to repeat the gradient pattern start-to-end, start-to-end, start-to-end, etc. continuously until the target region is filled. + /// If the attribute is not specified, the effect is as if a value of 'pad' were specified. + /// + [TypeConverter(typeof(SvgGradientSpreadMethodConverter))] public enum SvgGradientSpreadMethod { + /// Use the terminal colors of the gradient to fill the remainder of the target region. Pad, + + /// Reflect the gradient pattern start-to-end, end-to-start, start-to-end, etc. continuously until the target rectangle is filled. Reflect, + + /// Repeat the gradient pattern start-to-end, start-to-end, start-to-end, etc. continuously until the target region is filled. Repeat } } \ No newline at end of file diff --git a/Source/Painting/SvgMarker.cs b/Source/Painting/SvgMarker.cs index dac9f1c..6d5de60 100644 --- a/Source/Painting/SvgMarker.cs +++ b/Source/Painting/SvgMarker.cs @@ -81,10 +81,10 @@ namespace Svg public SvgMarker() { - MarkerUnits = SvgMarkerUnits.strokeWidth; + MarkerUnits = SvgMarkerUnits.StrokeWidth; MarkerHeight = 3; MarkerWidth = 3; - Overflow = SvgOverflow.hidden; + Overflow = SvgOverflow.Hidden; } public override System.Drawing.Drawing2D.GraphicsPath Path(ISvgRenderer renderer) @@ -183,13 +183,13 @@ namespace Svg transMatrix.Rotate(Orient.Angle); switch (MarkerUnits) { - case SvgMarkerUnits.strokeWidth: + case SvgMarkerUnits.StrokeWidth: transMatrix.Translate(AdjustForViewBoxWidth(-RefX.ToDeviceValue(pRenderer, UnitRenderingType.Horizontal, this) * pOwner.StrokeWidth.ToDeviceValue(pRenderer, UnitRenderingType.Other, this)), AdjustForViewBoxHeight(-RefY.ToDeviceValue(pRenderer, UnitRenderingType.Vertical, this) * pOwner.StrokeWidth.ToDeviceValue(pRenderer, UnitRenderingType.Other, this))); break; - case SvgMarkerUnits.userSpaceOnUse: + case SvgMarkerUnits.UserSpaceOnUse: transMatrix.Translate(-RefX.ToDeviceValue(pRenderer, UnitRenderingType.Horizontal, this), -RefY.ToDeviceValue(pRenderer, UnitRenderingType.Vertical, this)); break; @@ -224,10 +224,10 @@ namespace Svg Brush pBrush = pPath.Stroke.GetBrush(this, renderer, Opacity); switch (MarkerUnits) { - case SvgMarkerUnits.strokeWidth: + case SvgMarkerUnits.StrokeWidth: return (new Pen(pBrush, StrokeWidth.ToDeviceValue(renderer, UnitRenderingType.Other, this) * pPath.StrokeWidth.ToDeviceValue(renderer, UnitRenderingType.Other, this))); - case SvgMarkerUnits.userSpaceOnUse: + case SvgMarkerUnits.UserSpaceOnUse: return (new Pen(pBrush, StrokeWidth.ToDeviceValue(renderer, UnitRenderingType.Other, this))); } return (new Pen(pBrush, StrokeWidth.ToDeviceValue(renderer, UnitRenderingType.Other, this))); @@ -242,14 +242,14 @@ namespace Svg GraphicsPath pRet = Path(null).Clone() as GraphicsPath; switch (MarkerUnits) { - case SvgMarkerUnits.strokeWidth: + case SvgMarkerUnits.StrokeWidth: using (var transMatrix = new Matrix()) { transMatrix.Scale(AdjustForViewBoxWidth(pPath.StrokeWidth), AdjustForViewBoxHeight(pPath.StrokeWidth)); pRet.Transform(transMatrix); } break; - case SvgMarkerUnits.userSpaceOnUse: + case SvgMarkerUnits.UserSpaceOnUse: break; } return (pRet); diff --git a/Source/Painting/SvgStrokeLineCap.cs b/Source/Painting/SvgStrokeLineCap.cs index f937e7f..5a7f75e 100644 --- a/Source/Painting/SvgStrokeLineCap.cs +++ b/Source/Painting/SvgStrokeLineCap.cs @@ -5,12 +5,20 @@ using System.ComponentModel; namespace Svg { + /// Specifies the shape to be used at the end of open subpaths when they are stroked. [TypeConverter(typeof(SvgStrokeLineCapConverter))] public enum SvgStrokeLineCap { + /// The value is inherited from the parent element. + Inherit, + + /// The ends of the subpaths are square but do not extend past the end of the subpath. Butt, + + /// The ends of the subpaths are rounded. Round, - Square, - Inherit + + /// The ends of the subpaths are square. + Square } } diff --git a/Source/Painting/SvgStrokeLineJoin.cs b/Source/Painting/SvgStrokeLineJoin.cs index 7af2288..e66730a 100644 --- a/Source/Painting/SvgStrokeLineJoin.cs +++ b/Source/Painting/SvgStrokeLineJoin.cs @@ -5,11 +5,20 @@ using System.ComponentModel; namespace Svg { + /// Specifies the shape to be used at the corners of paths or basic shapes when they are stroked. [TypeConverter(typeof(SvgStrokeLineJoinConverter))] public enum SvgStrokeLineJoin { + /// The value is inherited from the parent element. + Inherit, + + /// The corners of the paths are joined sharply. Miter, + + /// The corners of the paths are rounded off. Round, + + /// The corners of the paths are "flattened". Bevel } } diff --git a/Source/SvgAttributeCollection.cs b/Source/SvgAttributeCollection.cs index ac09091..a88c52b 100644 --- a/Source/SvgAttributeCollection.cs +++ b/Source/SvgAttributeCollection.cs @@ -85,13 +85,12 @@ namespace Svg private bool IsInheritValue(object value) { return (value == null || - (value is SvgFontStyle && (SvgFontStyle)value == SvgFontStyle.inherit) || - (value is SvgFontWeight && (SvgFontWeight)value == SvgFontWeight.inherit) || - (value is SvgTextAnchor && (SvgTextAnchor)value == SvgTextAnchor.inherit) || - (value is SvgFontVariant && (SvgFontVariant)value == SvgFontVariant.inherit) || - (value is SvgTextDecoration && (SvgTextDecoration)value == SvgTextDecoration.inherit) || + (value is SvgFontWeight && (SvgFontWeight)value == SvgFontWeight.Inherit) || + (value is SvgTextAnchor && (SvgTextAnchor)value == SvgTextAnchor.Inherit) || + (value is SvgFontVariant && (SvgFontVariant)value == SvgFontVariant.Inherit) || + (value is SvgTextDecoration && (SvgTextDecoration)value == SvgTextDecoration.Inherit) || (value is XmlSpaceHandling && (XmlSpaceHandling)value == XmlSpaceHandling.inherit) || - (value is SvgOverflow && (SvgOverflow)value == SvgOverflow.inherit) || + (value is SvgOverflow && (SvgOverflow)value == SvgOverflow.Inherit) || (value == SvgColourServer.Inherit) || (value is string && (string)value == "inherit") ); diff --git a/Source/SvgDocument.cs b/Source/SvgDocument.cs index b53e444..2bc106c 100644 --- a/Source/SvgDocument.cs +++ b/Source/SvgDocument.cs @@ -475,7 +475,7 @@ namespace Svg renderer.ScaleTransform(bitmap.Width / size.Width, bitmap.Height / size.Height); //EO, 2014-12-05: Requested to ensure proper zooming out (reduce size). Otherwise it clip the image. - this.Overflow = SvgOverflow.auto; + this.Overflow = SvgOverflow.Auto; this.Render(renderer); } diff --git a/Source/SvgElement.cs b/Source/SvgElement.cs index d988bee..5468028 100644 --- a/Source/SvgElement.cs +++ b/Source/SvgElement.cs @@ -572,9 +572,11 @@ namespace Svg (!attr.Attribute.InAttributeDictionary || _attributes.ContainsKey(attr.Attribute.Name))) { object propertyValue = attr.Property.GetValue(this); + string value = (string)attr.Property.Converter.ConvertTo(propertyValue, typeof(string)); forceWrite = false; writeStyle = (attr.Attribute.Name == "fill"); + if ((attr.Attribute.Name == "fill") && (Parent != null)) { if(propertyValue == SvgColourServer.NotSet) continue; @@ -593,9 +595,9 @@ namespace Svg if (propertyValue != null) { var type = propertyValue.GetType(); - string value = (string)attr.Property.Converter.ConvertTo(propertyValue, typeof(string)); - - if (!SvgDefaults.IsDefault(attr.Attribute.Name, value) || forceWrite) + + //Only write the attribute's value if it is not the default value, not null/empty, or we're forcing the write. + if ((!string.IsNullOrEmpty(value) && !SvgDefaults.IsDefault(attr.Attribute.Name, value)) || forceWrite) { if (writeStyle) { @@ -609,7 +611,6 @@ namespace Svg } else if(attr.Attribute.Name == "fill") //if fill equals null, write 'none' { - string value = (string)attr.Property.Converter.ConvertTo(propertyValue, typeof(string)); if (writeStyle) { styles[attr.Attribute.Name] = value; diff --git a/Source/SvgElementStyle.cs b/Source/SvgElementStyle.cs index 837c2c9..7ce672e 100644 --- a/Source/SvgElementStyle.cs +++ b/Source/SvgElementStyle.cs @@ -173,7 +173,7 @@ namespace Svg [SvgAttribute("font-style", true)] public virtual SvgFontStyle FontStyle { - get { return (this.Attributes["font-style"] == null) ? SvgFontStyle.inherit : (SvgFontStyle)this.Attributes["font-style"]; } + get { return (this.Attributes["font-style"] == null) ? SvgFontStyle.All : (SvgFontStyle)this.Attributes["font-style"]; } set { this.Attributes["font-style"] = value; this.IsPathDirty = true; } } @@ -183,7 +183,7 @@ namespace Svg [SvgAttribute("font-variant", true)] public virtual SvgFontVariant FontVariant { - get { return (this.Attributes["font-variant"] == null) ? SvgFontVariant.inherit : (SvgFontVariant)this.Attributes["font-variant"]; } + get { return (this.Attributes["font-variant"] == null) ? SvgFontVariant.Inherit : (SvgFontVariant)this.Attributes["font-variant"]; } set { this.Attributes["font-variant"] = value; this.IsPathDirty = true; } } @@ -193,7 +193,7 @@ namespace Svg [SvgAttribute("text-decoration", true)] public virtual SvgTextDecoration TextDecoration { - get { return (this.Attributes["text-decoration"] == null) ? SvgTextDecoration.inherit : (SvgTextDecoration)this.Attributes["text-decoration"]; } + get { return (this.Attributes["text-decoration"] == null) ? SvgTextDecoration.Inherit : (SvgTextDecoration)this.Attributes["text-decoration"]; } set { this.Attributes["text-decoration"] = value; this.IsPathDirty = true; } } @@ -203,7 +203,7 @@ namespace Svg [SvgAttribute("font-weight", true)] public virtual SvgFontWeight FontWeight { - get { return (this.Attributes["font-weight"] == null) ? SvgFontWeight.inherit : (SvgFontWeight)this.Attributes["font-weight"]; } + get { return (this.Attributes["font-weight"] == null) ? SvgFontWeight.Inherit : (SvgFontWeight)this.Attributes["font-weight"]; } set { this.Attributes["font-weight"] = value; this.IsPathDirty = true; } } @@ -328,12 +328,12 @@ namespace Svg // Get the font-weight switch (this.FontWeight) { - case SvgFontWeight.bold: - case SvgFontWeight.bolder: - case SvgFontWeight.w600: - case SvgFontWeight.w700: - case SvgFontWeight.w800: - case SvgFontWeight.w900: + //Note: Bold is not listed because it is = W700. + case SvgFontWeight.Bolder: + case SvgFontWeight.W600: + case SvgFontWeight.W700: + case SvgFontWeight.W800: + case SvgFontWeight.W900: fontStyle |= System.Drawing.FontStyle.Bold; break; } @@ -341,8 +341,8 @@ namespace Svg // Get the font-style switch (this.FontStyle) { - case SvgFontStyle.italic: - case SvgFontStyle.oblique: + case SvgFontStyle.Italic: + case SvgFontStyle.Oblique: fontStyle |= System.Drawing.FontStyle.Italic; break; } @@ -350,10 +350,10 @@ namespace Svg // Get the text-decoration switch (this.TextDecoration) { - case SvgTextDecoration.lineThrough: + case SvgTextDecoration.LineThrough: fontStyle |= System.Drawing.FontStyle.Strikeout; break; - case SvgTextDecoration.underline: + case SvgTextDecoration.Underline: fontStyle |= System.Drawing.FontStyle.Underline; break; } diff --git a/Source/Text/SvgFontFace.cs b/Source/Text/SvgFontFace.cs index e967729..1935280 100644 --- a/Source/Text/SvgFontFace.cs +++ b/Source/Text/SvgFontFace.cs @@ -84,7 +84,7 @@ namespace Svg [SvgAttribute("font-style")] public virtual SvgFontStyle FontStyle { - get { return (this.Attributes["font-style"] == null) ? SvgFontStyle.inherit : (SvgFontStyle)this.Attributes["font-style"]; } + get { return (this.Attributes["font-style"] == null) ? SvgFontStyle.All : (SvgFontStyle)this.Attributes["font-style"]; } set { this.Attributes["font-style"] = value; } } @@ -94,7 +94,7 @@ namespace Svg [SvgAttribute("font-variant")] public virtual SvgFontVariant FontVariant { - get { return (this.Attributes["font-variant"] == null) ? SvgFontVariant.inherit : (SvgFontVariant)this.Attributes["font-variant"]; } + get { return (this.Attributes["font-variant"] == null) ? SvgFontVariant.Inherit : (SvgFontVariant)this.Attributes["font-variant"]; } set { this.Attributes["font-variant"] = value; } } @@ -104,7 +104,7 @@ namespace Svg [SvgAttribute("font-weight")] public virtual SvgFontWeight FontWeight { - get { return (this.Attributes["font-weight"] == null) ? SvgFontWeight.inherit : (SvgFontWeight)this.Attributes["font-weight"]; } + get { return (this.Attributes["font-weight"] == null) ? SvgFontWeight.Inherit : (SvgFontWeight)this.Attributes["font-weight"]; } set { this.Attributes["font-weight"] = value; } } diff --git a/Source/Text/SvgTextAnchor.cs b/Source/Text/SvgTextAnchor.cs index 162b22a..f4dbf7d 100644 --- a/Source/Text/SvgTextAnchor.cs +++ b/Source/Text/SvgTextAnchor.cs @@ -12,7 +12,8 @@ namespace Svg [TypeConverter(typeof(SvgTextAnchorConverter))] public enum SvgTextAnchor { - inherit, + /// The value is inherited from the parent element. + Inherit, /// /// The rendered characters are aligned such that the start of the text string is at the initial current text position. /// diff --git a/Source/Text/SvgTextBase.cs b/Source/Text/SvgTextBase.cs index a572c52..9db224f 100644 --- a/Source/Text/SvgTextBase.cs +++ b/Source/Text/SvgTextBase.cs @@ -36,7 +36,7 @@ namespace Svg [SvgAttribute("text-anchor", true)] public virtual SvgTextAnchor TextAnchor { - get { return (this.Attributes["text-anchor"] == null) ? SvgTextAnchor.inherit : (SvgTextAnchor)this.Attributes["text-anchor"]; } + get { return (this.Attributes["text-anchor"] == null) ? SvgTextAnchor.Inherit : (SvgTextAnchor)this.Attributes["text-anchor"]; } set { this.Attributes["text-anchor"] = value; this.IsPathDirty = true; } } @@ -167,7 +167,7 @@ namespace Svg [SvgAttribute("lengthAdjust", true)] public virtual SvgTextLengthAdjust LengthAdjust { - get { return (this.Attributes["lengthAdjust"] == null) ? SvgTextLengthAdjust.spacing : (SvgTextLengthAdjust)this.Attributes["lengthAdjust"]; } + get { return (this.Attributes["lengthAdjust"] == null) ? SvgTextLengthAdjust.Spacing : (SvgTextLengthAdjust)this.Attributes["lengthAdjust"]; } set { this.Attributes["lengthAdjust"] = value; this.IsPathDirty = true; } } @@ -365,7 +365,7 @@ namespace Svg var diff = (actLength - specLength); if (Math.Abs(diff) > 1.5) { - if (this.LengthAdjust == SvgTextLengthAdjust.spacing) + if (this.LengthAdjust == SvgTextLengthAdjust.Spacing) { origState.LetterSpacingAdjust = -1 * diff / (state.NumChars - origState.NumChars - 1); SetPath(origState, false); diff --git a/Source/Text/SvgTextPath.cs b/Source/Text/SvgTextPath.cs index e80b866..d109a44 100644 --- a/Source/Text/SvgTextPath.cs +++ b/Source/Text/SvgTextPath.cs @@ -42,14 +42,14 @@ namespace Svg [SvgAttribute("method")] public virtual SvgTextPathMethod Method { - get { return (this.Attributes["method"] == null ? SvgTextPathMethod.align : (SvgTextPathMethod)this.Attributes["method"]); } + get { return (this.Attributes["method"] == null ? SvgTextPathMethod.Align : (SvgTextPathMethod)this.Attributes["method"]); } set { this.Attributes["method"] = value; } } [SvgAttribute("spacing")] public virtual SvgTextPathSpacing Spacing { - get { return (this.Attributes["spacing"] == null ? SvgTextPathSpacing.exact : (SvgTextPathSpacing)this.Attributes["spacing"]); } + get { return (this.Attributes["spacing"] == null ? SvgTextPathSpacing.Exact : (SvgTextPathSpacing)this.Attributes["spacing"]); } set { this.Attributes["spacing"] = value; } } -- GitLab