Commit ab3a7c58 authored by Axyonych's avatar Axyonych
Browse files

Fixed bitmap size calculation in SvgDocument.Draw(); Added guard on case when...

Fixed bitmap size calculation in SvgDocument.Draw(); Added guard on case when exception occured during rendering.
parent 3fd0b2ea
...@@ -3,12 +3,11 @@ using System.Collections.Generic; ...@@ -3,12 +3,11 @@ using System.Collections.Generic;
using System.ComponentModel; using System.ComponentModel;
using System.Diagnostics; using System.Diagnostics;
using System.Drawing; using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Text;
using System.IO; using System.IO;
using System.Linq;
using System.Text; using System.Text;
using System.Xml; using System.Xml;
using System.Drawing.Drawing2D;
using System.Xml.Schema;
namespace Svg namespace Svg
{ {
...@@ -17,22 +16,18 @@ namespace Svg ...@@ -17,22 +16,18 @@ namespace Svg
/// </summary> /// </summary>
public class SvgDocument : SvgFragment, ITypeDescriptorContext public class SvgDocument : SvgFragment, ITypeDescriptorContext
{ {
private SvgElementIdManager _idManager;
private int _ppi;
public static readonly int PPI = 96; public static readonly int PPI = 96;
/// <summary> /// <summary>
/// Gets a <see cref="string"/> containing the XLink namespace (http://www.w3.org/1999/xlink). /// Gets a <see cref="string"/> containing the XLink namespace (http://www.w3.org/1999/xlink).
/// </summary> /// </summary>
public static readonly string XLinkNamespace = "http://www.w3.org/1999/xlink"; public static readonly string XLinkNamespace = "http://www.w3.org/1999/xlink";
/// <summary> private SvgElementIdManager _idManager;
/// Retrieves the <see cref="SvgElement"/> with the specified ID.
/// </summary> public SvgDocument()
/// <param name="id">A <see cref="string"/> containing the ID of the element to find.</param>
/// <returns>An <see cref="SvgElement"/> of one exists with the specified ID; otherwise false.</returns>
public virtual SvgElement GetElementById(string id)
{ {
return this.IdManager.GetElementById(id); Ppi = 96;
} }
/// <summary> /// <summary>
...@@ -42,24 +37,57 @@ namespace Svg ...@@ -42,24 +37,57 @@ namespace Svg
{ {
get get
{ {
if (this._idManager == null) if (_idManager == null)
{ _idManager = new SvgElementIdManager(this);
this._idManager = new SvgElementIdManager(this);
}
return this._idManager; return _idManager;
} }
} }
public int Ppi public int Ppi { get; set; }
#region ITypeDescriptorContext Members
IContainer ITypeDescriptorContext.Container
{ {
get { return this._ppi; } get { throw new NotImplementedException(); }
set { this._ppi = value; }
} }
public SvgDocument() object ITypeDescriptorContext.Instance
{ {
this._ppi = 96; get { return this; }
}
void ITypeDescriptorContext.OnComponentChanged()
{
throw new NotImplementedException();
}
bool ITypeDescriptorContext.OnComponentChanging()
{
throw new NotImplementedException();
}
PropertyDescriptor ITypeDescriptorContext.PropertyDescriptor
{
get { throw new NotImplementedException(); }
}
object IServiceProvider.GetService(Type serviceType)
{
throw new NotImplementedException();
}
#endregion
/// <summary>
/// Retrieves the <see cref="SvgElement"/> with the specified ID.
/// </summary>
/// <param name="id">A <see cref="string"/> containing the ID of the element to find.</param>
/// <returns>An <see cref="SvgElement"/> of one exists with the specified ID; otherwise false.</returns>
public virtual SvgElement GetElementById(string id)
{
return IdManager.GetElementById(id);
} }
/// <summary> /// <summary>
...@@ -76,9 +104,7 @@ namespace Svg ...@@ -76,9 +104,7 @@ namespace Svg
public static SvgDocument Open(string path, Dictionary<string, string> entities) public static SvgDocument Open(string path, Dictionary<string, string> entities)
{ {
if (!File.Exists(path)) if (!File.Exists(path))
{
throw new FileNotFoundException("The specified document cannot be found.", path); throw new FileNotFoundException("The specified document cannot be found.", path);
}
return Open(File.OpenRead(path), entities); return Open(File.OpenRead(path), entities);
} }
...@@ -97,15 +123,14 @@ namespace Svg ...@@ -97,15 +123,14 @@ namespace Svg
{ {
Trace.TraceInformation("Begin Read"); Trace.TraceInformation("Begin Read");
using (SvgTextReader reader = new SvgTextReader(stream, entities)) using (var reader = new SvgTextReader(stream, entities))
{ {
Stack<SvgElement> elementStack = new Stack<SvgElement>(); var elementStack = new Stack<SvgElement>();
StringBuilder value = new StringBuilder(); var value = new StringBuilder();
SvgElement element = null; SvgElement element = null;
SvgElement parent = null; SvgElement parent;
SvgDocument svgDocument = null; SvgDocument svgDocument = null;
reader.XmlResolver = new SvgDtdResolver(); reader.XmlResolver = new SvgDtdResolver();
bool isEmpty;
reader.WhitespaceHandling = WhitespaceHandling.None; reader.WhitespaceHandling = WhitespaceHandling.None;
while (reader.Read()) while (reader.Read())
...@@ -117,16 +142,14 @@ namespace Svg ...@@ -117,16 +142,14 @@ namespace Svg
case XmlNodeType.Element: case XmlNodeType.Element:
// Does this element have a value or children // Does this element have a value or children
// (Must do this check here before we progress to another node) // (Must do this check here before we progress to another node)
isEmpty = reader.IsEmptyElement; bool isEmpty = reader.IsEmptyElement;
// Create element // Create element
if (elementStack.Count > 0) if (elementStack.Count > 0)
{
element = SvgElementFactory.CreateElement(reader, svgDocument); element = SvgElementFactory.CreateElement(reader, svgDocument);
}
else else
{ {
element = SvgElementFactory.CreateDocument(reader); element = SvgElementFactory.CreateDocument(reader);
svgDocument = (SvgDocument)element; svgDocument = (SvgDocument) element;
} }
if (element == null) if (element == null)
...@@ -144,9 +167,7 @@ namespace Svg ...@@ -144,9 +167,7 @@ namespace Svg
// Need to process if the element is empty // Need to process if the element is empty
if (isEmpty) if (isEmpty)
{
goto case XmlNodeType.EndElement; goto case XmlNodeType.EndElement;
}
break; break;
case XmlNodeType.EndElement: case XmlNodeType.EndElement:
...@@ -180,7 +201,7 @@ namespace Svg ...@@ -180,7 +201,7 @@ namespace Svg
} }
} }
public static SvgDocument Open(System.Xml.XmlDocument document) public static SvgDocument Open(XmlDocument document)
{ {
return null; return null;
} }
...@@ -190,36 +211,44 @@ namespace Svg ...@@ -190,36 +211,44 @@ namespace Svg
return null; return null;
} }
public static Bitmap OpenAsBitmap(System.Xml.XmlDocument document) public static Bitmap OpenAsBitmap(XmlDocument document)
{ {
return null; return null;
} }
public RectangleF GetDimensions() public RectangleF GetDimensions()
{ {
return new RectangleF(0, 0, this.Width.ToDeviceValue(), this.Height.ToDeviceValue()); return new RectangleF(0, 0, Width.ToDeviceValue(), Height.ToDeviceValue());
return new RectangleF();
} }
public void Draw(Graphics graphics) public void Draw(Graphics graphics)
{ {
this.Render(graphics); Render(graphics);
} }
public virtual Bitmap Draw() public virtual Bitmap Draw()
{ {
Trace.TraceInformation("Begin Render"); Trace.TraceInformation("Begin Render");
RectangleF size = this.GetDimensions(); var size = GetDimensions();
Bitmap bitmap = new Bitmap((int)this.Width.ToDeviceValue(), (int)this.Height.ToDeviceValue());
var bitmap = new Bitmap((int) Math.Ceiling(size.Width), (int) Math.Ceiling(size.Height));
using (Graphics g = Graphics.FromImage(bitmap)) try
{ {
g.TextRenderingHint = System.Drawing.Text.TextRenderingHint.AntiAlias; using (var g = Graphics.FromImage(bitmap))
g.TextContrast = 1; {
g.PixelOffsetMode = PixelOffsetMode.Half; g.TextRenderingHint = TextRenderingHint.AntiAlias;
this.Render(g); g.TextContrast = 1;
g.Save(); g.PixelOffsetMode = PixelOffsetMode.Half;
Render(g);
g.Save();
}
}
catch
{
bitmap.Dispose();
throw;
} }
Trace.TraceInformation("End Render"); Trace.TraceInformation("End Render");
...@@ -228,44 +257,14 @@ namespace Svg ...@@ -228,44 +257,14 @@ namespace Svg
public void Write(Stream stream) public void Write(Stream stream)
{ {
using (XmlTextWriter writer = new XmlTextWriter(stream, Encoding.UTF8)) using (new XmlTextWriter(stream, Encoding.UTF8))
{ {
} }
} }
public void Write(string path) public void Write(string path)
{ {
this.Write(File.Create(path)); Write(File.Create(path));
}
IContainer ITypeDescriptorContext.Container
{
get { throw new NotImplementedException(); }
}
object ITypeDescriptorContext.Instance
{
get { return this; }
}
void ITypeDescriptorContext.OnComponentChanged()
{
throw new NotImplementedException();
}
bool ITypeDescriptorContext.OnComponentChanging()
{
throw new NotImplementedException();
}
PropertyDescriptor ITypeDescriptorContext.PropertyDescriptor
{
get { throw new NotImplementedException(); }
}
object IServiceProvider.GetService(Type serviceType)
{
throw new NotImplementedException();
} }
} }
} }
\ No newline at end of file
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