Commit 0fdceefc authored by Tebjan Halm's avatar Tebjan Halm
Browse files

document can load subclasses of SvgDocument, fill inheritance working

parent 5360393d
......@@ -88,7 +88,6 @@ namespace Svg
{
this._dirty = true;
this._requiresSmoothRendering = false;
this.Fill = new SvgColourServer(); //in case fill attribute is not set by xml, default fill is black
}
/// <summary>
......
......@@ -32,7 +32,7 @@ namespace Svg
[SvgAttribute("fill")]
public virtual SvgPaintServer Fill
{
get { return (this.Attributes["Fill"] == null) ? null : (SvgPaintServer)this.Attributes["Fill"]; }
get { return (this.Attributes["Fill"] == null) ? SvgColourServer.NotSet : (SvgPaintServer)this.Attributes["Fill"]; }
set { this.Attributes["Fill"] = value; }
}
......
......@@ -19,17 +19,6 @@ namespace Svg
{
}
/// <summary>
/// Gets or sets the fill.
/// </summary>
/// <value>The fill.</value>
[SvgAttribute("fill")]
public override SvgPaintServer Fill
{
get { return (this.Attributes["Fill"] == null) ? null : (SvgPaintServer)this.Attributes["Fill"]; }
set { this.Attributes["Fill"] = value; }
}
/// <summary>
/// Gets the <see cref="GraphicsPath"/> for this element.
/// </summary>
......
......@@ -7,6 +7,12 @@ namespace Svg
{
public sealed class SvgColourServer : SvgPaintServer
{
/// <summary>
/// An unspecified <see cref="SvgPaintServer"/>.
/// </summary>
public static readonly SvgPaintServer NotSet = new SvgColourServer();
public SvgColourServer() : this(Color.Black)
{
}
......@@ -34,6 +40,11 @@ namespace Svg
public override string ToString()
{
if(this == SvgPaintServer.None)
return "none";
else if(this == SvgColourServer.NotSet)
return "";
Color c = this.Colour;
// Return the name if it exists
......
......@@ -27,7 +27,7 @@ namespace Svg
_defaults["cx"] = "0";
_defaults["cy"] = "0";
_defaults["fill"] = "Black";
_defaults["fill"] = "";
_defaults["fill-opacity"] = "1";
_defaults["fill-rule"] = "nonzero";
......@@ -51,7 +51,6 @@ namespace Svg
if (_defaults.ContainsKey(attributeName))
{
if (_defaults[attributeName] == value) return true;
else if (attributeName == "fill") return value == "#000000";
}
return false;
}
......
......@@ -112,7 +112,18 @@ namespace Svg
/// <exception cref="FileNotFoundException">The document at the specified <paramref name="path"/> cannot be found.</exception>
public static SvgDocument Open(string path)
{
return Open(path, null);
return Open<SvgDocument>(path, null);
}
/// <summary>
/// Opens the document at the specified path and loads the SVG contents.
/// </summary>
/// <param name="path">A <see cref="string"/> containing the path of the file to open.</param>
/// <returns>An <see cref="SvgDocument"/> with the contents loaded.</returns>
/// <exception cref="FileNotFoundException">The document at the specified <paramref name="path"/> cannot be found.</exception>
public static T Open<T>(string path) where T : SvgDocument, new()
{
return Open<T>(path, null);
}
/// <summary>
......@@ -122,7 +133,7 @@ namespace Svg
/// <param name="entities">A dictionary of custom entity definitions to be used when resolving XML entities within the document.</param>
/// <returns>An <see cref="SvgDocument"/> with the contents loaded.</returns>
/// <exception cref="FileNotFoundException">The document at the specified <paramref name="path"/> cannot be found.</exception>
public static SvgDocument Open(string path, Dictionary<string, string> entities)
public static T Open<T>(string path, Dictionary<string, string> entities) where T : SvgDocument, new()
{
if (string.IsNullOrEmpty(path))
{
......@@ -134,16 +145,16 @@ namespace Svg
throw new FileNotFoundException("The specified document cannot be found.", path);
}
return Open(File.OpenRead(path), entities);
return Open<T>(File.OpenRead(path), entities);
}
/// <summary>
/// Attempts to open an SVG document from the specified <see cref="Stream"/>.
/// </summary>
/// <param name="stream">The <see cref="Stream"/> containing the SVG document to open.</param>
public static SvgDocument Open(Stream stream)
public static T Open<T>(Stream stream) where T : SvgDocument, new()
{
return Open(stream, null);
return Open<T>(stream, null);
}
/// <summary>
......@@ -152,7 +163,7 @@ namespace Svg
/// <param name="stream">The <see cref="Stream"/> containing the SVG document to open.</param>
/// <param name="entities">Custom entity definitions.</param>
/// <exception cref="ArgumentNullException">The <paramref name="stream"/> parameter cannot be <c>null</c>.</exception>
public static SvgDocument Open(Stream stream, Dictionary<string, string> entities)
public static T Open<T>(Stream stream, Dictionary<string, string> entities) where T : SvgDocument, new()
{
if (stream == null)
{
......@@ -168,7 +179,7 @@ namespace Svg
bool elementEmpty;
SvgElement element = null;
SvgElement parent;
SvgDocument svgDocument = null;
T svgDocument = null;
reader.XmlResolver = new SvgDtdResolver();
reader.WhitespaceHandling = WhitespaceHandling.None;
......@@ -189,8 +200,8 @@ namespace Svg
}
else
{
element = SvgElementFactory.CreateDocument(reader);
svgDocument = (SvgDocument)element;
svgDocument = SvgElementFactory.CreateDocument<T>(reader);
element = svgDocument;
}
if (element == null)
......@@ -262,7 +273,7 @@ namespace Svg
}
Stream stream = new MemoryStream(UTF8Encoding.Default.GetBytes(document.InnerXml));
return Open(stream, null);
return Open<SvgDocument>(stream, null);
}
public static Bitmap OpenAsBitmap(string path)
......
......@@ -383,6 +383,8 @@ namespace Svg
var forceWrite = false;
if ((attr.Attribute.Name == "fill") && (Parent != null))
{
if(propertyValue == SvgColourServer.NotSet) continue;
object parentValue;
if (TryResolveParentAttributeValue(attr.Attribute.Name, out parentValue))
{
......@@ -610,6 +612,13 @@ namespace Svg
newObj.Children.Add(child.DeepCopy());
}
if(this._customAttributes.Count > 0)
{
foreach (var element in _customAttributes)
{
newObj.CustomAttributes.Add(element.Key, element.Value);
}
}
return newObj;
}
......
......@@ -42,7 +42,7 @@ namespace Svg
/// <param name="reader">The <see cref="XmlTextReader"/> containing the node to parse into an <see cref="SvgDocument"/>.</param>
/// <exception cref="ArgumentNullException">The <paramref name="reader"/> parameter cannot be <c>null</c>.</exception>
/// <exception cref="InvalidOperationException">The CreateDocument method can only be used to parse root &lt;svg&gt; elements.</exception>
public static SvgDocument CreateDocument(XmlTextReader reader)
public static T CreateDocument<T>(XmlTextReader reader) where T : SvgDocument, new()
{
if (reader == null)
{
......@@ -54,7 +54,7 @@ namespace Svg
throw new InvalidOperationException("The CreateDocument method can only be used to parse root <svg> elements.");
}
return (SvgDocument)CreateElement(reader, true, null);
return (T)CreateElement<T>(reader, true, null);
}
/// <summary>
......@@ -70,15 +70,10 @@ namespace Svg
throw new ArgumentNullException("reader");
}
if (document == null)
{
throw new ArgumentNullException("document");
}
return CreateElement(reader, false, document);
return CreateElement<SvgDocument>(reader, false, document);
}
private static SvgElement CreateElement(XmlTextReader reader, bool fragmentIsDocument, SvgDocument document)
private static SvgElement CreateElement<T>(XmlTextReader reader, bool fragmentIsDocument, SvgDocument document) where T : SvgDocument, new()
{
SvgElement createdElement = null;
string elementName = reader.LocalName;
......@@ -87,7 +82,7 @@ namespace Svg
if (elementName == "svg")
{
createdElement = (fragmentIsDocument) ? new SvgDocument() : new SvgFragment();
createdElement = (fragmentIsDocument) ? new T() : new SvgFragment();
}
else
{
......
......@@ -121,7 +121,7 @@ namespace Svg.Web
entities.Add(queryString.Keys[i], queryString[i]);
}
SvgDocument document = SvgDocument.Open(this._state._context.Request.PhysicalPath, entities);
SvgDocument document = SvgDocument.Open<SvgDocument>(this._state._context.Request.PhysicalPath, entities);
using (Bitmap bitmap = document.Draw())
{
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment