Skip to content

Commit

Permalink
Fix: xlink:href value format.
Browse files Browse the repository at this point in the history
- It doesn't work properly with certain IRIs, so fix it in another way.
- ref. svg-net#455.
  • Loading branch information
H1Gdev committed Aug 28, 2019
1 parent f73806e commit b56f08c
Show file tree
Hide file tree
Showing 6 changed files with 30 additions and 15 deletions.
7 changes: 3 additions & 4 deletions Source/Painting/SvgDeferredPaintServer.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.ComponentModel;
using System.Drawing;
using System.Linq;

Expand All @@ -8,6 +9,7 @@ namespace Svg
/// A wrapper for a paint server which isn't defined currently in the parse process,
/// but should be defined by the time the image needs to render.
/// </summary>
[TypeConverter(typeof(SvgDeferredPaintServerFactory))]
public class SvgDeferredPaintServer : SvgPaintServer
{
private bool _serverLoaded;
Expand Down Expand Up @@ -88,10 +90,7 @@ public override int GetHashCode()

public override string ToString()
{
if (DeferredId == "currentColor")
return DeferredId;
else
return string.Format("url({0})", DeferredId);
return string.IsNullOrEmpty(DeferredId) ? string.Empty : DeferredId;
}

public static T TryGet<T>(SvgPaintServer server, SvgElement parent) where T : SvgPaintServer
Expand Down
19 changes: 19 additions & 0 deletions Source/Painting/SvgDeferredPaintServerFactory.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
using System.ComponentModel;
using System.Globalization;

namespace Svg
{
internal class SvgDeferredPaintServerFactory : SvgPaintServerFactory
{
public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object value)
{
if (value is string)
{
var s = ((string)value).Trim();
if (!string.IsNullOrEmpty(s))
return new SvgDeferredPaintServer(s);
}
return base.ConvertFrom(context, culture, value);
}
}
}
4 changes: 2 additions & 2 deletions Source/Painting/SvgGradientServer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -75,9 +75,9 @@ public SvgCoordinateUnits GradientUnits
/// Gets or sets another gradient fill from which to inherit the stops from.
/// </summary>
[SvgAttribute("href", SvgAttributeAttribute.XLinkNamespace)]
public SvgPaintServer InheritGradient
public SvgDeferredPaintServer InheritGradient
{
get { return GetAttribute<SvgPaintServer>("href", false); }
get { return GetAttribute<SvgDeferredPaintServer>("href", false); }
set { Attributes["href"] = value; }
}

Expand Down
4 changes: 2 additions & 2 deletions Source/Painting/SvgPatternServer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -96,9 +96,9 @@ public SvgViewBox ViewBox
/// Gets or sets another gradient fill from which to inherit the stops from.
/// </summary>
[SvgAttribute("href", SvgAttributeAttribute.XLinkNamespace)]
public SvgPaintServer InheritGradient
public SvgDeferredPaintServer InheritGradient
{
get { return GetAttribute<SvgPaintServer>("href", false); }
get { return GetAttribute<SvgDeferredPaintServer>("href", false); }
set { Attributes["href"] = value; }
}

Expand Down
5 changes: 4 additions & 1 deletion Source/SvgAttributeCollection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,11 @@ public TAttributeType GetInheritedAttribute<TAttributeType>(string attributeName
if (ContainsKey(attributeName) && !IsInheritValue(base[attributeName]))
{
var result = (TAttributeType)base[attributeName];

var deferred = result as SvgDeferredPaintServer;
if (deferred != null) deferred.EnsureServer(_owner);
if (deferred != null)
deferred.EnsureServer(_owner);

return result;
}

Expand Down
6 changes: 0 additions & 6 deletions Source/SvgElement.cs
Original file line number Diff line number Diff line change
Expand Up @@ -694,12 +694,6 @@ protected virtual void WriteAttributes(XmlTextWriter writer)

if (propertyValue != null)
{
if (!string.IsNullOrEmpty(value))
{
if (attr.Attribute.NamespaceAndName == "xlink:href" && value.StartsWith("url("))
value = new StringBuilder(value).Remove(value.Length - 1, 1).Remove(0, 4).ToString();
}

//Only write the attribute's value if it is not the default value, not null/empty, or we're forcing the write.
if (forceWrite || (!string.IsNullOrEmpty(value) && !SvgDefaults.IsDefault(attr.Attribute.Name, attr.Property.ComponentType.Name, value)))
{
Expand Down

0 comments on commit b56f08c

Please sign in to comment.