Commit 0f51d060 authored by tebjan's avatar tebjan
Browse files

Merge branch 'simplify-element-writing' of...

Merge branch 'simplify-element-writing' of https://github.com/JoshMcCullough/SVG into JoshMcCullough-simplify-element-writing

Conflicts:
	Source/SvgElement.cs
parents 67946913 796f6eba
using System;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Xml;
namespace Svg
{
......@@ -51,10 +52,10 @@ namespace Svg
get { return _x; }
set
{
if(_x != value)
if (_x != value)
{
_x = value;
OnAttributeChanged(new AttributeEventArgs{ Attribute = "x", Value = value });
OnAttributeChanged(new AttributeEventArgs { Attribute = "x", Value = value });
}
}
}
......@@ -68,10 +69,10 @@ namespace Svg
get { return _y; }
set
{
if(_y != value)
if (_y != value)
{
_y = value;
OnAttributeChanged(new AttributeEventArgs{ Attribute = "y", Value = value });
OnAttributeChanged(new AttributeEventArgs { Attribute = "y", Value = value });
}
}
}
......@@ -282,6 +283,20 @@ namespace Svg
return newObj;
}
//Override the default behavior, writing out the namespaces.
protected override void WriteStartElement(XmlTextWriter writer)
{
base.WriteStartElement(writer);
foreach (var ns in SvgAttributeAttribute.Namespaces)
{
if (string.IsNullOrEmpty(ns.Key))
writer.WriteAttributeString("xmlns", ns.Value);
else
writer.WriteAttributeString("xmlns:" + ns.Key, ns.Value);
}
writer.WriteAttributeString("version", "1.1");
}
}
}
\ No newline at end of file
......@@ -11,6 +11,8 @@ using System.Xml;
using System.Linq;
using ExCSS;
using Svg.Css;
using System.Threading;
using System.Globalization;
namespace Svg
{
......@@ -488,6 +490,18 @@ namespace Svg
//Trace.TraceInformation("End Render");
}
public override void Write(XmlTextWriter writer)
{
//Save previous culture and switch to invariant for writing
var previousCulture = Thread.CurrentThread.CurrentCulture;
Thread.CurrentThread.CurrentCulture = CultureInfo.InvariantCulture;
base.Write(writer);
//Switch culture back
Thread.CurrentThread.CurrentCulture = previousCulture;
}
public void Write(Stream stream)
{
......@@ -499,7 +513,7 @@ namespace Svg
if (!String.IsNullOrEmpty(this.ExternalCSSHref))
xmlWriter.WriteProcessingInstruction("xml-stylesheet", String.Format("type=\"text/css\" href=\"{0}\"", this.ExternalCSSHref));
this.WriteElement(xmlWriter);
this.Write(xmlWriter);
xmlWriter.Flush();
}
......
using System;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
......@@ -520,16 +520,11 @@ namespace Svg
this.Render(renderer);
}
public void WriteElement(XmlTextWriter writer)
/// <summary>Derrived classes may decide that the element should not be written. For example, the text element shouldn't be written if it's empty.</summary>
public virtual bool ShouldWriteElement()
{
//Save previous culture and switch to invariant for writing
var previousCulture = Thread.CurrentThread.CurrentCulture;
Thread.CurrentThread.CurrentCulture = CultureInfo.InvariantCulture;
this.Write(writer);
//Switch culture back
Thread.CurrentThread.CurrentCulture = previousCulture;
//Write any element who has a name.
return (this.ElementName != String.Empty);
}
protected virtual void WriteStartElement(XmlTextWriter writer)
......@@ -537,18 +532,8 @@ namespace Svg
if (this.ElementName != String.Empty)
{
writer.WriteStartElement(this.ElementName);
if (this.ElementName == "svg")
{
foreach (var ns in SvgAttributeAttribute.Namespaces)
{
if (string.IsNullOrEmpty(ns.Key))
writer.WriteAttributeString("xmlns", ns.Value);
else
writer.WriteAttributeString("xmlns:" + ns.Key, ns.Value);
}
writer.WriteAttributeString("version", "1.1");
}
}
this.WriteAttributes(writer);
}
......@@ -577,7 +562,7 @@ namespace Svg
forceWrite = false;
writeStyle = (attr.Attribute.Name == "fill");
if ((attr.Attribute.Name == "fill") && (Parent != null))
if (writeStyle && (Parent != null))
{
if(propertyValue == SvgColourServer.NotSet) continue;
......@@ -677,9 +662,9 @@ namespace Svg
return resolved;
}
protected virtual void Write(XmlTextWriter writer)
public virtual void Write(XmlTextWriter writer)
{
if (this.ElementName != String.Empty)
if (ShouldWriteElement())
{
this.WriteStartElement(writer);
this.WriteChildren(writer);
......
......@@ -54,7 +54,7 @@ namespace Svg
{
using (XmlTextWriter xml = new XmlTextWriter(str))
{
elem.WriteElement(xml);
elem.Write(xml);
result = str.ToString();
}
......
......@@ -143,7 +143,7 @@ namespace Svg
{
this._rotate = value;
this._rotations.Clear();
this._rotations.AddRange(from r in _rotate.Split(new char[] {',', ' ', '\r', '\n', '\t'}, StringSplitOptions.RemoveEmptyEntries) select float.Parse(r));
this._rotations.AddRange(from r in _rotate.Split(new char[] { ',', ' ', '\r', '\n', '\t' }, StringSplitOptions.RemoveEmptyEntries) select float.Parse(r));
this.IsPathDirty = true;
OnAttributeChanged(new AttributeEventArgs { Attribute = "rotate", Value = value });
}
......@@ -297,7 +297,7 @@ namespace Svg
{
//if there is a TSpan inside of this text element then path should not be null (even if this text is empty!)
var nodes = GetContentNodes().Where(x => x is SvgContentNode &&
string.IsNullOrEmpty(x.Content.Trim(new[] {'\r', '\n', '\t'})));
string.IsNullOrEmpty(x.Content.Trim(new[] { '\r', '\n', '\t' })));
if (_path == null || IsPathDirty || nodes.Count() == 1)
{
......@@ -914,5 +914,10 @@ namespace Svg
}
}
/// <summary>Empty text elements are not legal - only write this element if it has children.</summary>
public override bool ShouldWriteElement()
{
return (this.HasChildren() || this.Nodes.Count > 0);
}
}
}
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