Commit 8ef8a97a authored by Tebjan Halm's avatar Tebjan Halm
Browse files

Merge pull request #183 from pulsatrixbv/master

Extended the Marker start/mid/end getters to return null if none
parents bcf5b85c 1a5baee0
Showing with 135 additions and 36 deletions
+135 -36
......@@ -4,6 +4,7 @@ using System.Text;
using System.ComponentModel;
using System.Drawing;
using System.Drawing.Drawing2D;
using Svg.ExtensionMethods;
namespace Svg
{
......@@ -85,7 +86,7 @@ namespace Svg
[SvgAttribute("marker-end")]
public Uri MarkerEnd
{
get { return this.Attributes.GetAttribute<Uri>("marker-end"); }
get { return this.Attributes.GetAttribute<Uri>("marker-end").ReplaceWithNullIfNone(); }
set { this.Attributes["marker-end"] = value; }
}
......@@ -96,7 +97,7 @@ namespace Svg
[SvgAttribute("marker-mid")]
public Uri MarkerMid
{
get { return this.Attributes.GetAttribute<Uri>("marker-mid"); }
get { return this.Attributes.GetAttribute<Uri>("marker-mid").ReplaceWithNullIfNone(); }
set { this.Attributes["marker-mid"] = value; }
}
......@@ -107,7 +108,7 @@ namespace Svg
[SvgAttribute("marker-start")]
public Uri MarkerStart
{
get { return this.Attributes.GetAttribute<Uri>("marker-start"); }
get { return this.Attributes.GetAttribute<Uri>("marker-start").ReplaceWithNullIfNone(); }
set { this.Attributes["marker-start"] = value; }
}
......
......@@ -4,6 +4,7 @@ using System.Text;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Diagnostics;
using Svg.ExtensionMethods;
using Svg.Pathing;
namespace Svg
......@@ -32,7 +33,7 @@ namespace Svg
[SvgAttribute("marker-end")]
public Uri MarkerEnd
{
get { return this.Attributes.GetAttribute<Uri>("marker-end"); }
get { return this.Attributes.GetAttribute<Uri>("marker-end").ReplaceWithNullIfNone(); }
set { this.Attributes["marker-end"] = value; }
}
......@@ -43,7 +44,7 @@ namespace Svg
[SvgAttribute("marker-mid")]
public Uri MarkerMid
{
get { return this.Attributes.GetAttribute<Uri>("marker-mid"); }
get { return this.Attributes.GetAttribute<Uri>("marker-mid").ReplaceWithNullIfNone(); }
set { this.Attributes["marker-mid"] = value; }
}
......@@ -54,7 +55,7 @@ namespace Svg
[SvgAttribute("marker-start")]
public Uri MarkerStart
{
get { return this.Attributes.GetAttribute<Uri>("marker-start"); }
get { return this.Attributes.GetAttribute<Uri>("marker-start").ReplaceWithNullIfNone(); }
set { this.Attributes["marker-start"] = value; }
}
......
......@@ -4,6 +4,7 @@ using System.Text;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Diagnostics;
using Svg.ExtensionMethods;
namespace Svg
{
......@@ -19,7 +20,7 @@ namespace Svg
[SvgAttribute("marker-end")]
public Uri MarkerEnd
{
get { return this.Attributes.GetAttribute<Uri>("marker-end"); }
get { return this.Attributes.GetAttribute<Uri>("marker-end").ReplaceWithNullIfNone(); }
set { this.Attributes["marker-end"] = value; }
}
......@@ -30,7 +31,7 @@ namespace Svg
[SvgAttribute("marker-mid")]
public Uri MarkerMid
{
get { return this.Attributes.GetAttribute<Uri>("marker-mid"); }
get { return this.Attributes.GetAttribute<Uri>("marker-mid").ReplaceWithNullIfNone(); }
set { this.Attributes["marker-mid"] = value; }
}
......@@ -41,7 +42,7 @@ namespace Svg
[SvgAttribute("marker-start")]
public Uri MarkerStart
{
get { return this.Attributes.GetAttribute<Uri>("marker-start"); }
get { return this.Attributes.GetAttribute<Uri>("marker-start").ReplaceWithNullIfNone(); }
set { this.Attributes["marker-start"] = value; }
}
......
......@@ -9,9 +9,9 @@ namespace Svg.Css
{
internal static class CssQuery
{
public static IEnumerable<SvgElement> QuerySelectorAll(this SvgElement elem, string selector)
public static IEnumerable<SvgElement> QuerySelectorAll(this SvgElement elem, string selector, SvgElementFactory elementFactory)
{
var generator = new SelectorGenerator<SvgElement>(new SvgElementOps());
var generator = new SelectorGenerator<SvgElement>(new SvgElementOps(elementFactory));
Fizzler.Parser.Parse(selector, generator);
return generator.Selector(Enumerable.Repeat(elem, 1));
}
......
......@@ -8,10 +8,17 @@ namespace Svg.Css
{
internal class SvgElementOps : IElementOps<SvgElement>
{
public Selector<SvgElement> Type(NamespacePrefix prefix, string name)
private readonly SvgElementFactory _elementFactory;
public SvgElementOps(SvgElementFactory elementFactory)
{
_elementFactory = elementFactory;
}
public Selector<SvgElement> Type(NamespacePrefix prefix, string name)
{
SvgElementFactory.ElementInfo type = null;
if (SvgElementFactory.AvailableElements.TryGetValue(name, out type))
if (_elementFactory.AvailableElements.TryGetValue(name, out type))
{
return nodes => nodes.Where(n => n.GetType() == type.ElementType);
}
......
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Svg.ExtensionMethods
{
public static class UriExtensions
{
public static Uri ReplaceWithNullIfNone(this Uri uri)
{
if(uri == null) { return null; }
return string.Equals(uri.ToString(), "none", StringComparison.OrdinalIgnoreCase) ? null : uri;
}
}
}
......@@ -6,6 +6,7 @@ using System.Drawing.Drawing2D;
using System.Xml.Serialization;
using System.Xml;
using System.Diagnostics;
using Svg.ExtensionMethods;
using Svg.Pathing;
using Svg.Transforms;
......@@ -51,7 +52,7 @@ namespace Svg
[SvgAttribute("marker-end", true)]
public Uri MarkerEnd
{
get { return this.Attributes.GetAttribute<Uri>("marker-end"); }
get { return this.Attributes.GetAttribute<Uri>("marker-end").ReplaceWithNullIfNone(); }
set { this.Attributes["marker-end"] = value; }
}
......@@ -62,7 +63,7 @@ namespace Svg
[SvgAttribute("marker-mid", true)]
public Uri MarkerMid
{
get { return this.Attributes.GetAttribute<Uri>("marker-mid"); }
get { return this.Attributes.GetAttribute<Uri>("marker-mid").ReplaceWithNullIfNone(); }
set { this.Attributes["marker-mid"] = value; }
}
......@@ -73,7 +74,7 @@ namespace Svg
[SvgAttribute("marker-start", true)]
public Uri MarkerStart
{
get { return this.Attributes.GetAttribute<Uri>("marker-start"); }
get { return this.Attributes.GetAttribute<Uri>("marker-start").ReplaceWithNullIfNone(); }
set { this.Attributes["marker-start"] = value; }
}
......
......@@ -107,6 +107,7 @@
<Compile Include="DataTypes\SvgTextPathSpacing.cs" />
<Compile Include="DataTypes\XmlSpaceHandling.cs" />
<Compile Include="Document Structure\SvgSymbol.cs" />
<Compile Include="ExtensionMethods\UriExtensions.cs" />
<Compile Include="Filter Effects\ImageBuffer.cs" />
<Compile Include="Painting\GenericBoundable.cs" />
<Compile Include="Painting\SvgFallbackPaintServer .cs" />
......@@ -352,6 +353,7 @@
</ItemGroup>
<ItemGroup>
<None Include="Basic Shapes\DOM.cd" />
<None Include="Svg.nuspec" />
<None Include="svgkey.snk" />
</ItemGroup>
<Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" />
......
<?xml version="1.0"?>
<package >
<metadata>
<id>$id$</id>
<version>$version$</version>
<title>$title$</title>
<authors>$author$</authors>
<owners>$author$</owners>
<description>TaxModel/PAW altered implementation of the Svg library</description>
</metadata>
</package>
\ No newline at end of file
......@@ -243,7 +243,8 @@ namespace Svg
SvgElement element = null;
SvgElement parent;
T svgDocument = null;
var elementFactory = new SvgElementFactory();
var styles = new List<ISvgNode>();
while (reader.Read())
......@@ -259,11 +260,11 @@ namespace Svg
// Create element
if (elementStack.Count > 0)
{
element = SvgElementFactory.CreateElement(reader, svgDocument);
element = elementFactory.CreateElement(reader, svgDocument);
}
else
{
svgDocument = SvgElementFactory.CreateDocument<T>(reader);
svgDocument = elementFactory.CreateDocument<T>(reader);
element = svgDocument;
}
......@@ -349,7 +350,7 @@ namespace Svg
foreach (var selector in selectors)
{
elemsToStyle = svgDocument.QuerySelectorAll(rule.Selector.ToString());
elemsToStyle = svgDocument.QuerySelectorAll(rule.Selector.ToString(), elementFactory);
foreach (var elem in elemsToStyle)
{
foreach (var decl in rule.Declarations)
......
......@@ -14,13 +14,13 @@ namespace Svg
/// </summary>
internal class SvgElementFactory
{
private static Dictionary<string, ElementInfo> availableElements;
private static Parser cssParser = new Parser();
private Dictionary<string, ElementInfo> availableElements;
private Parser cssParser = new Parser();
/// <summary>
/// Gets a list of available types that can be used when creating an <see cref="SvgElement"/>.
/// </summary>
public static Dictionary<string, ElementInfo> AvailableElements
public Dictionary<string, ElementInfo> AvailableElements
{
get
{
......@@ -47,7 +47,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 T CreateDocument<T>(XmlReader reader) where T : SvgDocument, new()
public T CreateDocument<T>(XmlReader reader) where T : SvgDocument, new()
{
if (reader == null)
{
......@@ -68,7 +68,7 @@ namespace Svg
/// <param name="reader">The <see cref="XmlTextReader"/> containing the node to parse into a subclass of <see cref="SvgElement"/>.</param>
/// <param name="document">The <see cref="SvgDocument"/> that the created element belongs to.</param>
/// <exception cref="ArgumentNullException">The <paramref name="reader"/> and <paramref name="document"/> parameters cannot be <c>null</c>.</exception>
public static SvgElement CreateElement(XmlReader reader, SvgDocument document)
public SvgElement CreateElement(XmlReader reader, SvgDocument document)
{
if (reader == null)
{
......@@ -78,7 +78,7 @@ namespace Svg
return CreateElement<SvgDocument>(reader, false, document);
}
private static SvgElement CreateElement<T>(XmlReader reader, bool fragmentIsDocument, SvgDocument document) where T : SvgDocument, new()
private SvgElement CreateElement<T>(XmlReader reader, bool fragmentIsDocument, SvgDocument document) where T : SvgDocument, new()
{
SvgElement createdElement = null;
string elementName = reader.LocalName;
......@@ -122,7 +122,7 @@ namespace Svg
return createdElement;
}
private static void SetAttributes(SvgElement element, XmlReader reader, SvgDocument document)
private void SetAttributes(SvgElement element, XmlReader reader, SvgDocument document)
{
//Trace.TraceInformation("Begin SetAttributes");
......
......@@ -68,8 +68,8 @@ namespace Svg
return new SizeF(rect.Width, Ascent(renderer));
}
private static Graphics _graphics;
private static Graphics GetGraphics(object renderer)
private Graphics _graphics;
private Graphics GetGraphics(object renderer)
{
var provider = renderer as IGraphicsProvider;
if (provider == null)
......
......@@ -5,13 +5,11 @@ using ExCSS;
namespace Svg.UnitTests
{
/// <summary>
///This is a test class for CssQueryTest and is intended
///to contain all CssQueryTest Unit Tests
///</summary>
[TestClass()]
/// <summary>
///This is a test class for CssQueryTest and is intended
///to contain all CssQueryTest Unit Tests
///</summary>
[TestClass()]
public class CssQueryTest
{
......
using System;
using System.Diagnostics;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Threading.Tasks;
using System.Xml;
using Microsoft.VisualStudio.TestTools.UnitTesting;
namespace Svg.UnitTests
{
[TestClass()]
public class MultiThreadingTest
{
private const string TestFile = @"d:\temp\test.svg";
private const int ExpectedSize = 600000;
private XmlDocument GetXMLDoc()
{
var xmlDoc = new XmlDocument();
if(!System.IO.File.Exists(TestFile)) { Assert.Inconclusive("Test file missing"); }
xmlDoc.LoadXml(System.IO.File.ReadAllText(TestFile));
return xmlDoc;
}
[TestMethod]
public void TestSingleThread()
{
LoadFile();
}
[TestMethod]
public void TestMultiThread()
{
bool valid = true;
Parallel.For(0, 3, (x) =>
{
LoadFile();
});
Assert.IsTrue(valid, "One or more of the runs was invalid");
Trace.WriteLine("Done");
}
private void LoadFile()
{
var xml = GetXMLDoc();
Trace.WriteLine("Reading and drawing file");
SvgDocument d = SvgDocument.Open(xml);
var b = d.Draw();
Trace.WriteLine("Done reading file");
using (var ms = new MemoryStream())
{
b.Save(ms, ImageFormat.Png);
ms.Flush();
Assert.IsTrue(ms.Length >= ExpectedSize, "File does not match expected minimum size");
}
}
}
}
\ No newline at end of file
......@@ -49,6 +49,7 @@
</ItemGroup>
<ItemGroup>
<Compile Include="CssQueryTest.cs" />
<Compile Include="MultiThreadingTest.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
</ItemGroup>
<ItemGroup>
......
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