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