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;
using System.Drawing; using System.Drawing;
using System.Drawing.Drawing2D; using System.Drawing.Drawing2D;
using System.Xml;
namespace Svg namespace Svg
{ {
...@@ -51,10 +52,10 @@ namespace Svg ...@@ -51,10 +52,10 @@ namespace Svg
get { return _x; } get { return _x; }
set set
{ {
if(_x != value) if (_x != value)
{ {
_x = value; _x = value;
OnAttributeChanged(new AttributeEventArgs{ Attribute = "x", Value = value }); OnAttributeChanged(new AttributeEventArgs { Attribute = "x", Value = value });
} }
} }
} }
...@@ -68,10 +69,10 @@ namespace Svg ...@@ -68,10 +69,10 @@ namespace Svg
get { return _y; } get { return _y; }
set set
{ {
if(_y != value) if (_y != value)
{ {
_y = value; _y = value;
OnAttributeChanged(new AttributeEventArgs{ Attribute = "y", Value = value }); OnAttributeChanged(new AttributeEventArgs { Attribute = "y", Value = value });
} }
} }
} }
...@@ -282,6 +283,20 @@ namespace Svg ...@@ -282,6 +283,20 @@ namespace Svg
return newObj; 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; ...@@ -11,6 +11,8 @@ using System.Xml;
using System.Linq; using System.Linq;
using ExCSS; using ExCSS;
using Svg.Css; using Svg.Css;
using System.Threading;
using System.Globalization;
namespace Svg namespace Svg
{ {
...@@ -488,6 +490,18 @@ namespace Svg ...@@ -488,6 +490,18 @@ namespace Svg
//Trace.TraceInformation("End Render"); //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) public void Write(Stream stream)
{ {
...@@ -499,7 +513,7 @@ namespace Svg ...@@ -499,7 +513,7 @@ namespace Svg
if (!String.IsNullOrEmpty(this.ExternalCSSHref)) if (!String.IsNullOrEmpty(this.ExternalCSSHref))
xmlWriter.WriteProcessingInstruction("xml-stylesheet", String.Format("type=\"text/css\" href=\"{0}\"", this.ExternalCSSHref)); xmlWriter.WriteProcessingInstruction("xml-stylesheet", String.Format("type=\"text/css\" href=\"{0}\"", this.ExternalCSSHref));
this.WriteElement(xmlWriter); this.Write(xmlWriter);
xmlWriter.Flush(); xmlWriter.Flush();
} }
......
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.ComponentModel; using System.ComponentModel;
using System.Drawing; using System.Drawing;
...@@ -520,16 +520,11 @@ namespace Svg ...@@ -520,16 +520,11 @@ namespace Svg
this.Render(renderer); 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 //Write any element who has a name.
var previousCulture = Thread.CurrentThread.CurrentCulture; return (this.ElementName != String.Empty);
Thread.CurrentThread.CurrentCulture = CultureInfo.InvariantCulture;
this.Write(writer);
//Switch culture back
Thread.CurrentThread.CurrentCulture = previousCulture;
} }
protected virtual void WriteStartElement(XmlTextWriter writer) protected virtual void WriteStartElement(XmlTextWriter writer)
...@@ -537,18 +532,8 @@ namespace Svg ...@@ -537,18 +532,8 @@ namespace Svg
if (this.ElementName != String.Empty) if (this.ElementName != String.Empty)
{ {
writer.WriteStartElement(this.ElementName); 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); this.WriteAttributes(writer);
} }
...@@ -577,7 +562,7 @@ namespace Svg ...@@ -577,7 +562,7 @@ namespace Svg
forceWrite = false; forceWrite = false;
writeStyle = (attr.Attribute.Name == "fill"); writeStyle = (attr.Attribute.Name == "fill");
if ((attr.Attribute.Name == "fill") && (Parent != null)) if (writeStyle && (Parent != null))
{ {
if(propertyValue == SvgColourServer.NotSet) continue; if(propertyValue == SvgColourServer.NotSet) continue;
...@@ -677,9 +662,9 @@ namespace Svg ...@@ -677,9 +662,9 @@ namespace Svg
return resolved; 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.WriteStartElement(writer);
this.WriteChildren(writer); this.WriteChildren(writer);
......
...@@ -54,7 +54,7 @@ namespace Svg ...@@ -54,7 +54,7 @@ namespace Svg
{ {
using (XmlTextWriter xml = new XmlTextWriter(str)) using (XmlTextWriter xml = new XmlTextWriter(str))
{ {
elem.WriteElement(xml); elem.Write(xml);
result = str.ToString(); result = str.ToString();
} }
......
...@@ -143,7 +143,7 @@ namespace Svg ...@@ -143,7 +143,7 @@ namespace Svg
{ {
this._rotate = value; this._rotate = value;
this._rotations.Clear(); 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; this.IsPathDirty = true;
OnAttributeChanged(new AttributeEventArgs { Attribute = "rotate", Value = value }); OnAttributeChanged(new AttributeEventArgs { Attribute = "rotate", Value = value });
} }
...@@ -297,7 +297,7 @@ namespace Svg ...@@ -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!) //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 && 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) if (_path == null || IsPathDirty || nodes.Count() == 1)
{ {
...@@ -914,5 +914,10 @@ namespace Svg ...@@ -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