Commit c88ad05d authored by davescriven's avatar davescriven
Browse files

All current source code

parent 01b73c95
<Project DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003" ToolsVersion="3.5">
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
<ProductVersion>9.0.21022</ProductVersion>
<SchemaVersion>2.0</SchemaVersion>
<ProjectGuid>{886A98C5-37C0-4E8B-885E-30C1D2F98B47}</ProjectGuid>
<OutputType>Library</OutputType>
<AppDesignerFolder>Properties</AppDesignerFolder>
<RootNamespace>Svg</RootNamespace>
<AssemblyName>Svg</AssemblyName>
<RunPostBuildEvent>OnOutputUpdated</RunPostBuildEvent>
<FileUpgradeFlags>
</FileUpgradeFlags>
<OldToolsVersion>2.0</OldToolsVersion>
<UpgradeBackupLocation>
</UpgradeBackupLocation>
<TargetFrameworkVersion>v3.5</TargetFrameworkVersion>
<PublishUrl>publish\</PublishUrl>
<Install>true</Install>
<InstallFrom>Disk</InstallFrom>
<UpdateEnabled>false</UpdateEnabled>
<UpdateMode>Foreground</UpdateMode>
<UpdateInterval>7</UpdateInterval>
<UpdateIntervalUnits>Days</UpdateIntervalUnits>
<UpdatePeriodically>false</UpdatePeriodically>
<UpdateRequired>false</UpdateRequired>
<MapFileExtensions>true</MapFileExtensions>
<ApplicationRevision>0</ApplicationRevision>
<ApplicationVersion>1.0.0.%2a</ApplicationVersion>
<IsWebBootstrapper>false</IsWebBootstrapper>
<UseApplicationTrust>false</UseApplicationTrust>
<BootstrapperEnabled>true</BootstrapperEnabled>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<DebugSymbols>true</DebugSymbols>
<DebugType>full</DebugType>
<Optimize>false</Optimize>
<OutputPath>bin\Debug\</OutputPath>
<DefineConstants>DEBUG;TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<DebugType>pdbonly</DebugType>
<Optimize>true</Optimize>
<OutputPath>bin\Release\</OutputPath>
<DefineConstants>TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
<CodeAnalysisRules>
</CodeAnalysisRules>
</PropertyGroup>
<ItemGroup>
<Reference Include="Microsoft.JScript" />
<Reference Include="Microsoft.Vsa" />
<Reference Include="System" />
<Reference Include="System.Core">
<RequiredTargetFramework>3.5</RequiredTargetFramework>
</Reference>
<Reference Include="System.Data" />
<Reference Include="System.Drawing" />
<Reference Include="System.Web" />
<Reference Include="System.Xml" />
</ItemGroup>
<ItemGroup>
<Compile Include="Basic Shapes\SvgGraphicsElement.cs" />
<Compile Include="Basic Shapes\SvgGraphicsElementEvents.cs" />
<Compile Include="Basic Shapes\SvgCircle.cs" />
<Compile Include="Basic Shapes\SvgEllipse.cs" />
<Compile Include="Basic Shapes\SvgLine.cs" />
<Compile Include="Basic Shapes\SvgPolygon.cs" />
<Compile Include="Basic Shapes\SvgPolyline.cs" />
<Compile Include="DataTypes\ISvgViewPort.cs" />
<Compile Include="DataTypes\SvgElementStyle.cs" />
<Compile Include="DataTypes\SvgUnitCollection.cs" />
<Compile Include="DataTypes\SvgViewBox.cs" />
<Compile Include="Document Structure\SvgDefinitionList.cs" />
<Compile Include="Document Structure\SvgDescription.cs" />
<Compile Include="Document Structure\SvgGroup.cs" />
<Compile Include="Document Structure\SvgFragment.cs" />
<Compile Include="Document Structure\SvgUse.cs" />
<Compile Include="Filter Effects\ISvgFilter.cs" />
<Compile Include="Filter Effects\ISvgFilterable.cs" />
<Compile Include="Filter Effects\SvgFilter.cs" />
<Compile Include="Filter Effects\SvgFilterPrimitive.cs" />
<Compile Include="Filter Effects\SvgGaussianBlur.cs" />
<Compile Include="Filter Effects\SvgMerge.cs" />
<Compile Include="ISvgRenderer.cs" />
<Compile Include="Painting\SvgColourConverter.cs" />
<Compile Include="Painting\SvgGradientSpreadMethod.cs" />
<Compile Include="Painting\SvgGradientUnit.cs" />
<Compile Include="SvgDtdResolver.cs" />
<Compile Include="Exceptions\SvgException.cs" />
<Compile Include="Painting\SvgFillRule.cs" />
<Compile Include="Painting\SvgGradientServer.cs" />
<Compile Include="Painting\SvgGradientStop.cs" />
<Compile Include="Painting\ISvgStylable.cs" />
<Compile Include="Painting\SvgColourServer.cs" />
<Compile Include="Painting\SvgLinearGradientServer.cs" />
<Compile Include="Painting\SvgPaintServer.cs" />
<Compile Include="Painting\SvgPaintServerFactory.cs" />
<Compile Include="Painting\SvgPatternServer.cs" />
<Compile Include="Painting\SvgRadialGradientServer.cs" />
<Compile Include="Painting\SvgStrokeLineCap.cs" />
<Compile Include="Painting\SvgStrokeLineJoin.cs" />
<Compile Include="Basic Shapes\SvgGraphicsElementStyle.cs" />
<Compile Include="Paths\SvgArcSegment.cs" />
<Compile Include="Paths\SvgClosePathSegment.cs" />
<Compile Include="Paths\SvgCubicCurveSegment.cs" />
<Compile Include="Paths\SvgLineSegment.cs" />
<Compile Include="Paths\SvgMoveToSegment.cs" />
<Compile Include="Paths\SvgPath.cs" />
<Compile Include="Basic Shapes\SvgRectangle.cs" />
<Compile Include="Paths\SvgPathSegment.cs" />
<Compile Include="Paths\SvgPathSegmentList.cs" />
<Compile Include="Paths\SvgQuadraticCurveSegment.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="Document Structure\SvgDocument.cs" />
<Compile Include="SvgAttributeAttribute.cs" />
<Compile Include="SvgAttributeCollection.cs" />
<Compile Include="SvgElement.cs" />
<Compile Include="SvgElementCollection.cs" />
<Compile Include="SvgElementFactory.cs" />
<Compile Include="Paths\SvgPathBuilder.cs" />
<Compile Include="DataTypes\SvgPoint.cs" />
<Compile Include="SvgElementIdManager.cs" />
<Compile Include="DataTypes\SvgUnit.cs" />
<Compile Include="DataTypes\SvgUnitConverter.cs" />
<Compile Include="Text\SvgText.cs" />
<Compile Include="Text\SvgTextAnchor.cs" />
<Compile Include="Text\SvgTextSpan.cs" />
<Compile Include="Transforms\ISvgTransformable.cs" />
<Compile Include="Transforms\SvgRotate.cs" />
<Compile Include="Transforms\SvgScale.cs" />
<Compile Include="Transforms\SvgTransform.cs" />
<Compile Include="Transforms\SvgTransformCollection.cs" />
<Compile Include="Transforms\SvgTransformConverter.cs" />
<Compile Include="Transforms\SvgTranslate.cs" />
<Compile Include="Web\SvgHandler.cs" />
</ItemGroup>
<ItemGroup>
<BootstrapperPackage Include="Microsoft.Net.Framework.2.0">
<Visible>False</Visible>
<ProductName>.NET Framework 2.0 %28x86%29</ProductName>
<Install>true</Install>
</BootstrapperPackage>
<BootstrapperPackage Include="Microsoft.Net.Framework.3.0">
<Visible>False</Visible>
<ProductName>.NET Framework 3.0 %28x86%29</ProductName>
<Install>false</Install>
</BootstrapperPackage>
<BootstrapperPackage Include="Microsoft.Net.Framework.3.5">
<Visible>False</Visible>
<ProductName>.NET Framework 3.5</ProductName>
<Install>false</Install>
</BootstrapperPackage>
</ItemGroup>
<ItemGroup>
<EmbeddedResource Include="Resources\svg11.dtd" />
</ItemGroup>
<ItemGroup>
<Folder Include="Clipping and Masking\" />
<Folder Include="Web\Resources\" />
</ItemGroup>
<Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" />
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
Other similar extension points exist, see Microsoft.Common.targets.
<Target Name="BeforeBuild">
</Target>
<Target Name="AfterBuild">
</Target>
-->
<PropertyGroup>
<PostBuildEvent>
</PostBuildEvent>
</PropertyGroup>
</Project>
\ No newline at end of file
<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup>
<PublishUrlHistory>
</PublishUrlHistory>
<InstallUrlHistory>
</InstallUrlHistory>
<SupportUrlHistory>
</SupportUrlHistory>
<UpdateUrlHistory>
</UpdateUrlHistory>
<BootstrapperUrlHistory>
</BootstrapperUrlHistory>
<FallbackCulture>en-US</FallbackCulture>
<VerifyUploadedFiles>false</VerifyUploadedFiles>
<ProjectView>ProjectFiles</ProjectView>
</PropertyGroup>
</Project>
\ No newline at end of file
using System;
using System.Collections.Generic;
using System.Text;
using System.ComponentModel;
namespace Svg
{
public class SvgAttributeAttribute : System.Attribute
{
private const string SVG_NAMESPACE = "http://www.w3.org/2000/svg";
private string _name;
private string _namespace;
public override object TypeId
{
get
{
return base.TypeId;
}
}
public override bool Match(object obj)
{
SvgAttributeAttribute indicator = obj as SvgAttributeAttribute;
if (indicator == null)
return false;
// Always match if either value is String.Empty (wildcard)
if (indicator.Name == String.Empty)
return false;
return String.Compare(indicator.Name, this.Name) == 0;
}
public string Name
{
get { return this._name; }
}
public string NameSpace
{
get { return this._namespace; }
}
internal SvgAttributeAttribute()
{
this._name = String.Empty;
}
internal SvgAttributeAttribute(string name)
{
this._name = name;
this._namespace = SVG_NAMESPACE;
}
public SvgAttributeAttribute(string name, string nameSpace)
{
this._name = name;
this._namespace = nameSpace;
}
}
}
\ No newline at end of file
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Svg
{
/// <summary>
/// A collection of Scalable Vector Attributes that can be inherited from the owner elements ancestors.
/// </summary>
public sealed class SvgAttributeCollection : Dictionary<string, object>
{
private SvgElement _owner;
/// <summary>
/// Initialises a new instance of a <see cref="SvgAttributeCollection"/> with the given <see cref="SvgElement"/> as the owner.
/// </summary>
/// <param name="owner">The <see cref="SvgElement"/> owner of the collection.</param>
public SvgAttributeCollection(SvgElement owner)
{
this._owner = owner;
}
/// <summary>
/// Gets the attribute with the specified name.
/// </summary>
/// <typeparam name="TAttributeType">The type of the attribute value.</typeparam>
/// <param name="attributeName">A <see cref="string"/> containing the name of the attribute.</param>
/// <returns>The attribute value if available; otherwise the default value of <typeparamref name="TAttributeType"/>.</returns>
public TAttributeType GetAttribute<TAttributeType>(string attributeName)
{
if (this.ContainsKey(attributeName) && base[attributeName] != null)
{
return (TAttributeType)base[attributeName];
}
return default(TAttributeType);
}
/// <summary>
/// Gets the attribute with the specified name and inherits from ancestors if there is no attribute set.
/// </summary>
/// <typeparam name="TAttributeType">The type of the attribute value.</typeparam>
/// <param name="attributeName">A <see cref="string"/> containing the name of the attribute.</param>
/// <returns>The attribute value if available; otherwise the ancestors value for the same attribute; otherwise the default value of <typeparamref name="TAttributeType"/>.</returns>
public TAttributeType GetInheritedAttribute<TAttributeType>(string attributeName)
{
if (this.ContainsKey(attributeName) && base[attributeName] != null)
{
return (TAttributeType)base[attributeName];
}
if (this._owner.Parent != null)
{
if (this._owner.Parent.Attributes[attributeName] != null)
{
return (TAttributeType)this._owner.Parent.Attributes[attributeName];
}
}
return default(TAttributeType);
}
/// <summary>
/// Gets the attribute with the specified name.
/// </summary>
/// <param name="attributeName">A <see cref="string"/> containing the attribute name.</param>
/// <returns>The attribute value associated with the specified name; If there is no attribute the parent's value will be inherited.</returns>
public new object this[string attributeName]
{
get { return this.GetInheritedAttribute<object>(attributeName); }
set { base[attributeName] = value; }
}
}
}
\ No newline at end of file
using System;
using System.Resources;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Reflection;
using System.Xml;
namespace Svg
{
internal class SvgDtdResolver : XmlUrlResolver
{
/// <summary>
/// Maps a URI to an object containing the actual resource.
/// </summary>
/// <param name="absoluteUri">The URI returned from <see cref="M:System.Xml.XmlResolver.ResolveUri(System.Uri,System.String)"/></param>
/// <param name="role">The current implementation does not use this parameter when resolving URIs. This is provided for future extensibility purposes. For example, this can be mapped to the xlink:role and used as an implementation specific argument in other scenarios.</param>
/// <param name="ofObjectToReturn">The type of object to return. The current implementation only returns System.IO.Stream objects.</param>
/// <returns>
/// A System.IO.Stream object or null if a type other than stream is specified.
/// </returns>
/// <exception cref="T:System.Xml.XmlException">
/// <paramref name="ofObjectToReturn"/> is neither null nor a Stream type. </exception>
/// <exception cref="T:System.UriFormatException">The specified URI is not an absolute URI. </exception>
/// <exception cref="T:System.NullReferenceException">
/// <paramref name="absoluteUri"/> is null. </exception>
/// <exception cref="T:System.Exception">There is a runtime error (for example, an interrupted server connection). </exception>
public override object GetEntity(Uri absoluteUri, string role, Type ofObjectToReturn)
{
return Assembly.GetExecutingAssembly().GetManifestResourceStream("Svg.Resources.svg11.dtd");
}
}
}
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Xml;
using Svg.Transforms;
namespace Svg
{
/// <summary>
/// The base class of which all SVG elements are derived from.
/// </summary>
public abstract class SvgElement : ISvgElement, ISvgTransformable, ICloneable
{
internal SvgElement _parent;
private string _content;
private string _elementName;
private SvgAttributeCollection _attributes;
private EventHandlerList _eventHandlers;
private SvgElementCollection _children;
private static readonly object _loadEventKey = new object();
private Matrix _graphicsMatrix;
/// <summary>
/// Gets the name of the element.
/// </summary>
protected virtual string ElementName
{
get { return this._elementName; }
}
/// <summary>
/// Gets or sets the content of the element.
/// </summary>
public virtual string Content
{
get { return this._content; }
set { this._content = value; }
}
/// <summary>
/// Gets an <see cref="EventHandlerList"/> of all events belonging to the element.
/// </summary>
protected virtual EventHandlerList Events
{
get { return this._eventHandlers; }
}
/// <summary>
/// Occurs when the element is loaded.
/// </summary>
public event EventHandler Load
{
add { this.Events.AddHandler(_loadEventKey, value); }
remove { this.Events.RemoveHandler(_loadEventKey, value); }
}
/// <summary>
/// Gets a collection of all child <see cref="SvgElements"/>.
/// </summary>
public virtual SvgElementCollection Children
{
get { return this._children; }
}
/// <summary>
/// Gets a value to determine whether the element has children.
/// </summary>
/// <returns></returns>
public virtual bool HasChildren()
{
return (this.Children.Count > 0);
}
/// <summary>
/// Gets the parent <see cref="SvgElement"/>.
/// </summary>
/// <value>An <see cref="SvgElement"/> if one exists; otherwise null.</value>
public virtual SvgElement Parent
{
get { return this._parent; }
}
/// <summary>
/// Gets the owner <see cref="SvgDocument"/>.
/// </summary>
public virtual SvgDocument OwnerDocument
{
get
{
if (Parent == null)
{
if (this is SvgDocument)
return (SvgDocument)this;
else
return null;
}
else
return Parent.OwnerDocument;
}
}
/// <summary>
/// Gets a collection of element attributes.
/// </summary>
protected internal virtual SvgAttributeCollection Attributes
{
get
{
if (this._attributes == null)
{
this._attributes = new SvgAttributeCollection(this);
}
return this._attributes;
}
}
protected internal virtual void PushTransforms(Graphics graphics)
{
// Return if there are no transforms
if (this.Transforms == null || this.Transforms.Count == 0)
{
return;
}
_graphicsMatrix = graphics.Transform;
Matrix transformMatrix = new Matrix();
foreach (SvgTransform transformation in this.Transforms)
{
transformMatrix.Multiply(transformation.Matrix);
}
graphics.Transform = transformMatrix;
}
protected internal virtual void PopTransforms(Graphics graphics)
{
if (this.Transforms == null || this.Transforms.Count == 0 || _graphicsMatrix == null)
{
return;
}
graphics.Transform = _graphicsMatrix;
_graphicsMatrix = null;
}
void ISvgTransformable.PushTransforms(Graphics graphics)
{
this.PushTransforms(graphics);
}
void ISvgTransformable.PopTransforms(Graphics graphics)
{
this.PopTransforms(graphics);
}
/// <summary>
/// Gets or sets the element transforms.
/// </summary>
/// <value>The transforms.</value>
[SvgAttribute("transform")]
public SvgTransformCollection Transforms
{
get { return this.Attributes.GetAttribute<SvgTransformCollection>("Transforms"); }
set { this.Attributes["Transforms"] = value; }
}
/// <summary>
/// Gets or sets the ID of the element.
/// </summary>
/// <exception cref="SvgException">The ID is already used within the <see cref="SvgDocument"/>.</exception>
[SvgAttribute("id")]
public string ID
{
get { return this.Attributes.GetAttribute<string>("ID"); }
set
{
// Don't do anything if it hasn't changed
if (string.Compare(this.ID, value) == 0)
return;
if (this.OwnerDocument != null)
{
this.OwnerDocument.IdManager.Remove(this);
}
this.Attributes["ID"] = value;
if (this.OwnerDocument != null)
{
this.OwnerDocument.IdManager.Add(this);
}
}
}
protected virtual void ElementAdded(SvgElement child, int index)
{
}
internal void OnElementAdded(SvgElement child, int index)
{
this.ElementAdded(child, index);
}
protected virtual void ElementRemoved(SvgElement child)
{
}
internal void OnElementRemoved(SvgElement child)
{
this.ElementRemoved(child);
}
/// <summary>
/// Initializes a new instance of the <see cref="SvgElement"/> class.
/// </summary>
internal SvgElement()
{
this._children = new SvgElementCollection(this);
this._eventHandlers = new EventHandlerList();
this._elementName = string.Empty;
}
public void RenderElement(Graphics graphics)
{
this.Render(graphics);
}
public void WriteElement(XmlTextWriter writer)
{
this.Write(writer);
}
protected virtual void WriteStartElement(XmlTextWriter writer)
{
if (this.ElementName != String.Empty)
{
writer.WriteStartElement(this.ElementName);
}
this.WriteAttributes(writer);
}
protected virtual void WriteEndElement(XmlTextWriter writer)
{
if (this.ElementName != String.Empty)
{
writer.WriteEndElement();
}
}
protected virtual void WriteAttributes(XmlTextWriter writer)
{
}
protected virtual void Write(XmlTextWriter writer)
{
if (this.ElementName != String.Empty)
{
this.WriteStartElement(writer);
this.WriteChildren(writer);
this.WriteEndElement(writer);
}
}
protected virtual void WriteChildren(XmlTextWriter writer)
{
foreach (SvgElement child in this.Children)
{
child.Write(writer);
}
}
/// <summary>
/// Renders the <see cref="SvgElement"/> and contents to the specified <see cref="Graphics"/> object.
/// </summary>
/// <param name="graphics">The <see cref="Graphics"/> object to render to.</param>
protected virtual void Render(Graphics graphics)
{
this.PushTransforms(graphics);
this.RenderContents(graphics);
this.PopTransforms(graphics);
}
protected virtual void RenderContents(Graphics graphics)
{
foreach (SvgElement element in this.Children)
{
element.Render(graphics);
}
}
void ISvgElement.Render(Graphics graphics)
{
this.Render(graphics);
}
public virtual object Clone()
{
return this.MemberwiseClone();
}
}
internal interface ISvgElement
{
void Render(Graphics graphics);
}
}
\ No newline at end of file
using System;
using System.Collections.Generic;
using System.Text;
namespace Svg
{
/// <summary>
/// Represents a collection of <see cref="SvgElement"/>s.
/// </summary>
public sealed class SvgElementCollection : IList<SvgElement>
{
private List<SvgElement> _elements;
private SvgElement _owner;
/// <summary>
/// Initialises a new instance of an <see cref="SvgElementCollection"/> class.
/// </summary>
/// <param name="owner">The owner <see cref="SvgElement"/> of the collection.</param>
internal SvgElementCollection(SvgElement owner)
{
if (owner == null)
{
throw new ArgumentNullException("owner");
}
this._elements = new List<SvgElement>();
this._owner = owner;
}
/// <summary>
/// Returns the index of the specified <see cref="SvgElement"/> in the collection.
/// </summary>
/// <param name="item">The <see cref="SvgElement"/> to search for.</param>
/// <returns>The index of the element if it is present; otherwise -1.</returns>
public int IndexOf(SvgElement item)
{
return this._elements.IndexOf(item);
}
/// <summary>
/// Inserts the given <see cref="SvgElement"/> to the collection at the specified index.
/// </summary>
/// <param name="index">The index that the <paramref name="item"/> should be added at.</param>
/// <param name="item">The <see cref="SvgElement"/> to be added.</param>
public void Insert(int index, SvgElement item)
{
this._elements.Insert(index, item);
}
public void RemoveAt(int index)
{
SvgElement element = this[index];
if (element != null)
this.Remove(element);
}
public SvgElement this[int index]
{
get { return this._elements[index]; }
set { this._elements[index] = value; }
}
public void Add(SvgElement item)
{
if (this._owner.OwnerDocument != null)
{
this._owner.OwnerDocument.IdManager.Add(item);
}
this._elements.Add(item);
item._parent = this._owner;
item._parent.OnElementAdded(item, this.Count - 1);
}
public void Clear()
{
while (this.Count > 0)
{
SvgElement element = this[0];
this.Remove(element);
}
}
public bool Contains(SvgElement item)
{
return this._elements.Contains(item);
}
public void CopyTo(SvgElement[] array, int arrayIndex)
{
this._elements.CopyTo(array, arrayIndex);
}
public int Count
{
get { return this._elements.Count; }
}
public bool IsReadOnly
{
get { return false; }
}
public bool Remove(SvgElement item)
{
bool removed = this._elements.Remove(item);
if (removed)
{
this._owner.OnElementRemoved(item);
item._parent = null;
if (this._owner.OwnerDocument != null)
{
this._owner.OwnerDocument.IdManager.Remove(item);
}
}
return removed;
}
public IEnumerator<SvgElement> GetEnumerator()
{
return this._elements.GetEnumerator();
}
System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
{
return this._elements.GetEnumerator();
}
}
}
\ No newline at end of file
using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Collections;
using System.Text;
using System.Globalization;
using System.Text.RegularExpressions;
using System.Xml;
using System.ComponentModel;
using System.Reflection;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Diagnostics;
using System.Threading;
using Svg.Transforms;
namespace Svg
{
internal class SvgElementFactory
{
public static SvgDocument CreateDocument(XmlTextReader reader)
{
return (SvgDocument)CreateElement(reader, true, null);
}
public static SvgElement CreateElement(XmlTextReader reader, SvgDocument document)
{
return CreateElement(reader, false, document);
}
private static SvgElement CreateElement(XmlTextReader reader, bool fragmentIsDocument, SvgDocument document)
{
SvgElement createdElement = null;
SvgFragment fragment;
string elementName = reader.LocalName;
// Parse element
switch (elementName)
{
case "path":
createdElement = new SvgPath();
break;
case "linearGradient":
createdElement = new SvgLinearGradientServer();
break;
case "radialGradient":
createdElement = new SvgRadialGradientServer();
break;
case "pattern":
createdElement = new SvgPatternServer();
break;
case "defs":
createdElement = new SvgDefinitionList();
break;
case "stop":
createdElement = new SvgGradientStop();
break;
case "desc":
createdElement = new SvgDescription();
break;
case "svg":
if (!fragmentIsDocument)
fragment = new SvgFragment();
else
fragment = new SvgDocument();
createdElement = (fragmentIsDocument) ? (SvgDocument)fragment : fragment;
break;
case "circle":
createdElement = new SvgCircle();
break;
case "ellipse":
createdElement = new SvgEllipse();
break;
case "rect":
createdElement = new SvgRectangle();
break;
case "line":
createdElement = new SvgLine();
break;
case "polyline":
createdElement = new SvgPolyline();
break;
case "polygon":
createdElement = new SvgPolygon();
break;
case "g":
createdElement = new SvgGroup();
break;
case "use":
createdElement = new SvgUse();
break;
case "text":
createdElement = new SvgText();
break;
default:
// Do nothing - unsupported
return null;
}
SetAttributes(createdElement, reader, document);
return createdElement;
}
private static void SetAttributes(SvgElement element, XmlTextReader reader, SvgDocument document)
{
string[] styles = null;
string[] style = null;
int i = 0;
while (reader.MoveToNextAttribute())
{
// Special treatment for "style"
if (reader.LocalName.Equals("style"))
{
styles = reader.Value.Split(new char[] { ';' }, StringSplitOptions.RemoveEmptyEntries);
for (i = 0; i < styles.Length; i++)
{
if (!styles[i].Contains(":"))
{
continue;
}
style = styles[i].Split(new char[] { ':' }, StringSplitOptions.RemoveEmptyEntries);
SetPropertyValue(element, style[0].Trim(), style[1].Trim(), document);
}
continue;
}
SetPropertyValue(element, reader.LocalName, reader.Value, document);
}
}
private static void SetPropertyValue(SvgElement element, string attributeName, string attributeValue, SvgDocument document)
{
PropertyDescriptorCollection properties = TypeDescriptor.GetProperties(element.GetType(), new SvgAttributeAttribute[] { new SvgAttributeAttribute(attributeName) });
PropertyDescriptor descriptor = null;
TypeConverter converter = null;
if (properties.Count > 0)
{
descriptor = properties[0];
converter = (properties[0].Converter != null) ? properties[0].Converter : TypeDescriptor.GetConverter(descriptor.PropertyType);
try
{
descriptor.SetValue(element, converter.ConvertFrom(document, CultureInfo.InvariantCulture, attributeValue));
}
catch
{
Trace.TraceWarning(string.Format("Attribute '{0}' cannot be set - type '{1}' cannot convert from string '{2}'.", attributeName, descriptor.PropertyType.FullName, attributeValue));
}
}
}
//private static void SetAttributes(SvgElement element, Dictionary<string, string> attributes, SvgDocument document)
//{
// // Parse attributes
// foreach(KeyValuePair<string, string> keyValuePair in attributes)
// {
// string name = keyValuePair.Key;
// string value = keyValuePair.Value;
// switch (name)
// {
// case "id":
// if (!String.IsNullOrEmpty(value))
// SetProperty(element, name, value);
// break;
// case "style":
// string[] styles = value.Split(';');
// Dictionary<string, string> styleAttributes = new Dictionary<string, string>();
// foreach (string style in styles)
// {
// if (String.IsNullOrEmpty(style) || style.IndexOf(":") == -1)
// continue;
// string[] pair = style.Split(':');
// styleAttributes.Add(pair[0].Trim(), pair[1].Trim());
// }
// SetAttributes(element, styleAttributes, document);
// break;
// case "href":
// if (element is SvgUse)
// SetProperty(element, name, document.GetElementById(value));
// break;
// case "transform":
// SetProperty(element, name, _transformConverter.ConvertFrom(value));
// break;
// case "stroke":
// case "fill":
// SetProperty(element, name, SvgPaintServerFactory.Create(value, document));
// break;
// case "font":
// break;
// case "font-family":
// // TODO: create font family converter, loop through families list. return generic if it's not in the list
// try
// {
// SetProperty(element, name, new FontFamily(value));
// }
// catch
// {
// Trace.TraceWarning("\"{0}\" is not a recognised font.", value);
// SetProperty(element, name, FontFamily.GenericSansSerif);
// }
// break;
// case "font-weight":
// //SetProperty(createdElement, reader.LocalName, reader.Value);
// break;
// case "fill-opacity":
// case "stroke-opacity":
// case "stop-opacity":
// case "opacity":
// SetProperty(element, name, float.Parse(value));
// break;
// case "points":
// // TODO: TypeConverter for this?
// string points = value.Replace(",", " ").Trim();
// Regex spaceReplace = new Regex(@"\s+");
// points = spaceReplace.Replace(points, " ");
// string[] pts = points.Split(' ');
// List<SvgUnit> units = new List<SvgUnit>();
// foreach (string point in pts)
// units.Add((SvgUnit)_unitConverter.ConvertFrom(point));
// SetProperty(element, name, units);
// break;
// case "font-size":
// case "letter-spacing":
// case "word-spacing":
// case "r":
// case "width":
// case "height":
// case "ry":
// case "rx":
// case "x":
// case "y":
// case "x1":
// case "y1":
// case "x2":
// case "y2":
// case "cy":
// case "cx":
// case "offset":
// case "stroke-width":
// SetProperty(element, name, (SvgUnit)_unitConverter.ConvertFrom(value));
// break;
// case "stop-color":
// SetProperty(element, name, (Color)_colourConverter.ConvertFrom(value));
// break;
// case "d":
// SvgPathBuilder.Parse(value, ((SvgPath)element).PathData);
// break;
// case "pathLength":
// SetProperty(element, name, int.Parse(value));
// break;
// default:
// break;
// }
// }
//}
//private static void SetProperty(object element, string attributeName, object attributeValue)
//{
// string key = String.Format("{0}{1}", element.GetType().Name, attributeName);
// if (!_propertyDescriptorLookup.ContainsKey(key))
// {
// PropertyDescriptorCollection properties = TypeDescriptor.GetProperties(element.GetType(), new Attribute[] { new SvgAttributeAttribute(attributeName) });
// if (properties.Count == 0)
// return;
// _propertyDescriptorLookup.Add(key, properties[0]);
// }
// PropertyDescriptor property = _propertyDescriptorLookup[key];
// property.SetValue(element, attributeValue);
//}
}
}
\ No newline at end of file
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
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