using System;
using System.Collections.Generic;
using System.Text;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Xml.Serialization;
using System.Xml;
using System.Diagnostics;
using Svg.ExtensionMethods;
using Svg.Pathing;
using Svg.Transforms;
namespace Svg
{
///
/// Represents an SVG path element.
///
[SvgElement("path")]
public class SvgPath : SvgVisualElement
{
private GraphicsPath _path;
///
/// Gets or sets a of path data.
///
[SvgAttribute("d", true)]
public SvgPathSegmentList PathData
{
get { return this.Attributes.GetAttribute("d"); }
set
{
this.Attributes["d"] = value;
value._owner = this;
this.IsPathDirty = true;
}
}
///
/// Gets or sets the length of the path.
///
[SvgAttribute("pathLength", true)]
public float PathLength
{
get { return this.Attributes.GetAttribute("pathLength"); }
set { this.Attributes["pathLength"] = value; }
}
///
/// Gets or sets the marker (end cap) of the path.
///
[SvgAttribute("marker-end", true)]
public Uri MarkerEnd
{
get { return this.Attributes.GetAttribute("marker-end").ReplaceWithNullIfNone(); }
set { this.Attributes["marker-end"] = value; }
}
///
/// Gets or sets the marker (start cap) of the path.
///
[SvgAttribute("marker-mid", true)]
public Uri MarkerMid
{
get { return this.Attributes.GetAttribute("marker-mid").ReplaceWithNullIfNone(); }
set { this.Attributes["marker-mid"] = value; }
}
///
/// Gets or sets the marker (start cap) of the path.
///
[SvgAttribute("marker-start", true)]
public Uri MarkerStart
{
get { return this.Attributes.GetAttribute("marker-start").ReplaceWithNullIfNone(); }
set { this.Attributes["marker-start"] = value; }
}
///
/// Gets the for this element.
///
public override GraphicsPath Path(ISvgRenderer renderer)
{
if (this._path == null || this.IsPathDirty)
{
_path = new GraphicsPath();
foreach (SvgPathSegment segment in this.PathData)
{
segment.AddToPath(_path);
}
this.IsPathDirty = false;
}
return _path;
}
internal void OnPathUpdated()
{
this.IsPathDirty = true;
OnAttributeChanged(new AttributeEventArgs{ Attribute = "d", Value = this.Attributes.GetAttribute("d") });
}
///
/// Gets the bounds of the element.
///
/// The bounds.
public override System.Drawing.RectangleF Bounds
{
get { return this.Path(null).GetBounds(); }
}
///
/// Initializes a new instance of the class.
///
public SvgPath()
{
var pathData = new SvgPathSegmentList();
this.Attributes["d"] = pathData;
pathData._owner = this;
}
///
/// Renders the stroke of the to the specified
///
/// The object to render to.
protected internal override bool RenderStroke(ISvgRenderer renderer)
{
var result = base.RenderStroke(renderer);
var path = this.Path(renderer);
if (this.MarkerStart != null)
{
SvgMarker marker = this.OwnerDocument.GetElementById(this.MarkerStart.ToString());
marker.RenderMarker(renderer, this, path.PathPoints[0], path.PathPoints[0], path.PathPoints[1]);
}
if (this.MarkerMid != null)
{
SvgMarker marker = this.OwnerDocument.GetElementById(this.MarkerMid.ToString());
for (int i = 1; i <= path.PathPoints.Length - 2; i++)
marker.RenderMarker(renderer, this, path.PathPoints[i], path.PathPoints[i - 1], path.PathPoints[i], path.PathPoints[i + 1]);
}
if (this.MarkerEnd != null)
{
SvgMarker marker = this.OwnerDocument.GetElementById(this.MarkerEnd.ToString());
marker.RenderMarker(renderer, this, path.PathPoints[path.PathPoints.Length - 1], path.PathPoints[path.PathPoints.Length - 2], path.PathPoints[path.PathPoints.Length - 1]);
}
return result;
}
public override SvgElement DeepCopy()
{
return DeepCopy();
}
public override SvgElement DeepCopy()
{
var newObj = base.DeepCopy() as SvgPath;
foreach (var pathData in this.PathData)
newObj.PathData.Add(pathData.Clone());
newObj.PathLength = this.PathLength;
newObj.MarkerStart = this.MarkerStart;
newObj.MarkerEnd = this.MarkerEnd;
return newObj;
}
}
}