Commit 79e87a0a authored by Tebjan Halm's avatar Tebjan Halm
Browse files

Merge pull request #186 from pulsatrixbv/master

Fixed problems when running in multiple threads
parents 8a982713 3838e721
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.Text;
namespace Svg.Exceptions
{
[Serializable]
public class SvgMemoryException : Exception
{
public SvgMemoryException() {}
public SvgMemoryException(string message) : base(message) {}
public SvgMemoryException(string message, Exception inner) : base(message, inner) {}
protected SvgMemoryException(
SerializationInfo info,
StreamingContext context) : base(info, context) {}
}
}
......@@ -107,6 +107,7 @@
<Compile Include="DataTypes\SvgTextPathSpacing.cs" />
<Compile Include="DataTypes\XmlSpaceHandling.cs" />
<Compile Include="Document Structure\SvgSymbol.cs" />
<Compile Include="Exceptions\SvgMemoryException.cs" />
<Compile Include="ExtensionMethods\UriExtensions.cs" />
<Compile Include="Filter Effects\ImageBuffer.cs" />
<Compile Include="Painting\GenericBoundable.cs" />
......
......@@ -13,6 +13,7 @@ using ExCSS;
using Svg.Css;
using System.Threading;
using System.Globalization;
using Svg.Exceptions;
namespace Svg
{
......@@ -434,17 +435,27 @@ namespace Svg
this.Render(renderer);
}
/// <summary>
/// Renders the <see cref="SvgDocument"/> and returns the image as a <see cref="Bitmap"/>.
/// </summary>
/// <returns>A <see cref="Bitmap"/> containing the rendered document.</returns>
public virtual Bitmap Draw()
{
//Trace.TraceInformation("Begin Render");
var size = GetDimensions();
var bitmap = new Bitmap((int)Math.Round(size.Width), (int)Math.Round(size.Height));
// bitmap.SetResolution(300, 300);
/// <summary>
/// Renders the <see cref="SvgDocument"/> and returns the image as a <see cref="Bitmap"/>.
/// </summary>
/// <returns>A <see cref="Bitmap"/> containing the rendered document.</returns>
public virtual Bitmap Draw()
{
//Trace.TraceInformation("Begin Render");
var size = GetDimensions();
Bitmap bitmap = null;
try
{
bitmap = new Bitmap((int) Math.Round(size.Width), (int) Math.Round(size.Height));
}
catch (ArgumentException e)
{
//When processing too many files at one the system can run out of memory
throw new SvgMemoryException("Cannot process SVG file, cannot allocate the required memory", e);
}
// bitmap.SetResolution(300, 300);
try
{
Draw(bitmap);
......
......@@ -284,7 +284,7 @@ namespace Svg
get { return this._customAttributes; }
}
private static readonly Matrix _zeroMatrix = new Matrix(0, 0, 0, 0, 0, 0);
private readonly Matrix _zeroMatrix = new Matrix(0, 0, 0, 0, 0, 0);
/// <summary>
/// Applies the required transforms to <see cref="ISvgRenderer"/>.
......
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Drawing;
......@@ -58,15 +59,15 @@ namespace Svg
public SizeF MeasureString(ISvgRenderer renderer, string text)
{
var g = GetGraphics(renderer);
StringFormat format = StringFormat.GenericTypographic;
format.SetMeasurableCharacterRanges(new CharacterRange[] { new CharacterRange(0, text.Length) });
format.FormatFlags |= StringFormatFlags.MeasureTrailingSpaces;
Region[] r = g.MeasureCharacterRanges(text, _font, new Rectangle(0, 0, 1000, 1000), format);
RectangleF rect = r[0].GetBounds(g);
var g = GetGraphics(renderer);
StringFormat format = StringFormat.GenericTypographic.Clone() as StringFormat;
format.SetMeasurableCharacterRanges(new CharacterRange[] {new CharacterRange(0, text.Length)});
format.FormatFlags |= StringFormatFlags.MeasureTrailingSpaces;
Region[] r = g.MeasureCharacterRanges(text, _font, new Rectangle(0, 0, 1000, 1000), format);
RectangleF rect = r[0].GetBounds(g);
return new SizeF(rect.Width, Ascent(renderer));
}
return new SizeF(rect.Width, Ascent(renderer));
}
private Graphics _graphics;
private Graphics GetGraphics(object renderer)
......
......@@ -6,6 +6,7 @@ using System.IO;
using System.Threading.Tasks;
using System.Xml;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Svg.Exceptions;
namespace Svg.UnitTests
{
......@@ -34,13 +35,29 @@ namespace Svg.UnitTests
public void TestMultiThread()
{
bool valid = true;
Parallel.For(0, 3, (x) =>
Parallel.For(0, 10, (x) =>
{
LoadFile();
});
Assert.IsTrue(valid, "One or more of the runs was invalid");
});
Trace.WriteLine("Done");
}
[TestMethod]
[ExpectedException(typeof(SvgMemoryException))]
public void SVGGivesMemoryExceptionOnTooManyParallelTest()
{
try
{
Parallel.For(0, 50, (x) =>
{
LoadFile();
});
}
catch (AggregateException ex)
{
throw ex.InnerException;
}
}
private void LoadFile()
{
var xml = GetXMLDoc();
......
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