Commit 1a5baee0 authored by Gertjan van Heertum's avatar Gertjan van Heertum
Browse files

Partial solution for threading issues

parent 537fc79e
...@@ -9,9 +9,9 @@ namespace Svg.Css ...@@ -9,9 +9,9 @@ namespace Svg.Css
{ {
internal static class CssQuery 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); Fizzler.Parser.Parse(selector, generator);
return generator.Selector(Enumerable.Repeat(elem, 1)); return generator.Selector(Enumerable.Repeat(elem, 1));
} }
......
...@@ -8,10 +8,17 @@ namespace Svg.Css ...@@ -8,10 +8,17 @@ namespace Svg.Css
{ {
internal class SvgElementOps : IElementOps<SvgElement> internal class SvgElementOps : IElementOps<SvgElement>
{ {
private readonly SvgElementFactory _elementFactory;
public SvgElementOps(SvgElementFactory elementFactory)
{
_elementFactory = elementFactory;
}
public Selector<SvgElement> Type(NamespacePrefix prefix, string name) public Selector<SvgElement> Type(NamespacePrefix prefix, string name)
{ {
SvgElementFactory.ElementInfo type = null; 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); return nodes => nodes.Where(n => n.GetType() == type.ElementType);
} }
......
...@@ -243,6 +243,7 @@ namespace Svg ...@@ -243,6 +243,7 @@ namespace Svg
SvgElement element = null; SvgElement element = null;
SvgElement parent; SvgElement parent;
T svgDocument = null; T svgDocument = null;
var elementFactory = new SvgElementFactory();
var styles = new List<ISvgNode>(); var styles = new List<ISvgNode>();
...@@ -259,11 +260,11 @@ namespace Svg ...@@ -259,11 +260,11 @@ namespace Svg
// Create element // Create element
if (elementStack.Count > 0) if (elementStack.Count > 0)
{ {
element = SvgElementFactory.CreateElement(reader, svgDocument); element = elementFactory.CreateElement(reader, svgDocument);
} }
else else
{ {
svgDocument = SvgElementFactory.CreateDocument<T>(reader); svgDocument = elementFactory.CreateDocument<T>(reader);
element = svgDocument; element = svgDocument;
} }
...@@ -349,7 +350,7 @@ namespace Svg ...@@ -349,7 +350,7 @@ namespace Svg
foreach (var selector in selectors) 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 elem in elemsToStyle)
{ {
foreach (var decl in rule.Declarations) foreach (var decl in rule.Declarations)
......
...@@ -14,13 +14,13 @@ namespace Svg ...@@ -14,13 +14,13 @@ namespace Svg
/// </summary> /// </summary>
internal class SvgElementFactory internal class SvgElementFactory
{ {
private static Dictionary<string, ElementInfo> availableElements; private Dictionary<string, ElementInfo> availableElements;
private static Parser cssParser = new Parser(); private Parser cssParser = new Parser();
/// <summary> /// <summary>
/// Gets a list of available types that can be used when creating an <see cref="SvgElement"/>. /// Gets a list of available types that can be used when creating an <see cref="SvgElement"/>.
/// </summary> /// </summary>
public static Dictionary<string, ElementInfo> AvailableElements public Dictionary<string, ElementInfo> AvailableElements
{ {
get get
{ {
...@@ -47,7 +47,7 @@ namespace Svg ...@@ -47,7 +47,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 T CreateDocument<T>(XmlReader reader) where T : SvgDocument, new() public T CreateDocument<T>(XmlReader reader) where T : SvgDocument, new()
{ {
if (reader == null) if (reader == null)
{ {
...@@ -68,7 +68,7 @@ namespace Svg ...@@ -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="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> /// <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> /// <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) if (reader == null)
{ {
...@@ -78,7 +78,7 @@ namespace Svg ...@@ -78,7 +78,7 @@ namespace Svg
return CreateElement<SvgDocument>(reader, false, document); 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; SvgElement createdElement = null;
string elementName = reader.LocalName; string elementName = reader.LocalName;
...@@ -122,7 +122,7 @@ namespace Svg ...@@ -122,7 +122,7 @@ namespace Svg
return createdElement; 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"); //Trace.TraceInformation("Begin SetAttributes");
......
...@@ -68,8 +68,8 @@ namespace Svg ...@@ -68,8 +68,8 @@ namespace Svg
return new SizeF(rect.Width, Ascent(renderer)); return new SizeF(rect.Width, Ascent(renderer));
} }
private static Graphics _graphics; private Graphics _graphics;
private static Graphics GetGraphics(object renderer) private Graphics GetGraphics(object renderer)
{ {
var provider = renderer as IGraphicsProvider; var provider = renderer as IGraphicsProvider;
if (provider == null) if (provider == null)
......
...@@ -34,17 +34,9 @@ namespace Svg.UnitTests ...@@ -34,17 +34,9 @@ namespace Svg.UnitTests
public void TestMultiThread() public void TestMultiThread()
{ {
bool valid = true; bool valid = true;
Parallel.For(0, 10, (x) => Parallel.For(0, 3, (x) =>
{
try
{ {
LoadFile(); LoadFile();
}
catch (Exception e)
{
Trace.WriteLine("Run error in parallel: " + e.Message);
valid = false;
}
}); });
Assert.IsTrue(valid, "One or more of the runs was invalid"); Assert.IsTrue(valid, "One or more of the runs was invalid");
Trace.WriteLine("Done"); Trace.WriteLine("Done");
......
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