Skip to content

Commit

Permalink
Fixed handling of default values for radial gradients
Browse files Browse the repository at this point in the history
- added possibility for class-specific default attribute values
- fixes load/save of W3C example pservers-grad-06-b
- fixes #397
  • Loading branch information
mrbean-bremen committed Feb 11, 2019
1 parent e6d7d76 commit 1cc3508
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 31 deletions.
104 changes: 74 additions & 30 deletions Source/SvgDefinitionDefaults.cs
Expand Up @@ -10,49 +10,93 @@ namespace Svg
/// </summary>
public static class SvgDefaults
{
//internal dictionary for the defaults
private static readonly Dictionary<string, string> _defaults = new Dictionary<string, string>();
// defaults that are specific to some elements
private static Dictionary<string, Dictionary<string, string>> _propDefaults =
new Dictionary<string, Dictionary<string, string>>
{
{ "SvgRadialGradientServer", new Dictionary<string, string>
{ { "cx", "50%" }, { "cy", "50%" }, { "r", "50%" } } },
{ "SvgLinearGradientServer", new Dictionary<string, string>
{ { "x1", "0%" }, { "x2", "100%" }, { "y1", "0%" }, { "y2", "100%" } } },
};

// common defaults
private static readonly Dictionary<string, string> _defaults =
new Dictionary<string, string>()
{
{ "d", "" },
{ "viewBox", "0, 0, 0, 0" },
{ "visibility", "visible" },
{ "display", "inline" },
{ "enable-background", "accumulate" },
{ "opacity", "1" },
{ "clip", "auto" },
{ "clip-rule", "nonzero" },
{ "clipPathUnits", "userSpaceOnUse" },
{ "x", "0" },
{ "y", "0" },
{ "transform", "" },

// line
{ "x1", "0" },
{ "x2", "0" },
{ "y1", "0" },
{ "y2", "0" },

// circle, ellipse
{ "cx", "0" },
{ "cy", "0" },

{ "fill", "" },
{ "fill-opacity", "1" },
{ "fill-rule", "nonzero" },

{ "stop-color", "black" },
{ "stop-opacity", "1" },

{ "stroke", "none" },
{ "stroke-opacity", "1" },
{ "stroke-width", "1" },
{ "stroke-miterlimit", "4" },
{ "stroke-linecap", "butt" },
{ "stroke-linejoin", "miter" },
{ "stroke-dasharray", "none" },
{ "stroke-dashoffset", "0" },

// marker
{ "markerUnits", "strokeWidth" },
{ "refX", "0" },
{ "refY", "0" },
{ "markerWidth", "3" },
{ "markerHeight", "3" },
{ "orient", "0" }
};


static SvgDefaults()
{
_defaults["d"] = "";

_defaults["viewBox"] = "0, 0, 0, 0";

_defaults["visibility"] = "visible";
_defaults["opacity"] = "1";
_defaults["clip-rule"] = "nonzero";

_defaults["transform"] = "";
_defaults["rx"] = "0";
_defaults["ry"] = "0";
_defaults["cx"] = "0";
_defaults["cy"] = "0";

_defaults["fill"] = "";
_defaults["fill-opacity"] = "1";
_defaults["fill-rule"] = "nonzero";

_defaults["stroke"] = "none";
_defaults["stroke-opacity"] = "1";
_defaults["stroke-width"] = "1";
_defaults["stroke-miterlimit"] = "4";
_defaults["stroke-linecap"] = "butt";
_defaults["stroke-linejoin"] = "miter";
_defaults["stroke-dasharray"] = "none";
_defaults["stroke-dashoffset"] = "0";
}

/// <summary>
/// Checks whether the property value is the default value of the svg definition.
/// </summary>
/// <param name="attributeName">Name of the svg attribute</param>
/// <param name="componentType">Class name of the svg element</param>
/// <param name="value">.NET value of the attribute</param>
public static bool IsDefault(string attributeName, string value)
public static bool IsDefault(string attributeName, string componentType, string value)
{
Console.WriteLine(componentType + ": " + attributeName);
if (_propDefaults.ContainsKey(componentType))
{
if (_propDefaults[componentType].ContainsKey(attributeName))
{
return _propDefaults[componentType][attributeName] == value;
}
}

if (_defaults.ContainsKey(attributeName))
{
if (_defaults[attributeName] == value) return true;
return _defaults[attributeName] == value;
}
return false;
}
Expand Down
2 changes: 1 addition & 1 deletion Source/SvgElement.cs
Expand Up @@ -600,7 +600,7 @@ protected virtual void WriteAttributes(XmlTextWriter writer)
var type = propertyValue.GetType();

//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 ((!string.IsNullOrEmpty(value) && !SvgDefaults.IsDefault(attr.Attribute.Name, attr.Property.ComponentType.Name, value)) || forceWrite)
{
if (writeStyle)
{
Expand Down

0 comments on commit 1cc3508

Please sign in to comment.