Commit 5649b72e authored by Guymestef's avatar Guymestef Committed by Eric Domke
Browse files

Add support for unknown svg element and non svg element: useful for not...

Add support for unknown svg element and non svg element: useful for not removing unknown xml tag from document when loading a file
parent 6d1b5eb5
using System.Drawing;
using System.Drawing.Drawing2D;
namespace Svg
{
/// <summary>
/// The switch element evaluates the requiredFeatures, requiredExtensions and systemLanguage attributes on its direct child elements in order, and then processes and renders the first child for which these attributes evaluate to true
/// </summary>
[SvgElement("switch")]
public class SvgSwitch : SvgVisualElement
{
public SvgSwitch()
{
}
/// <summary>
/// Gets the <see cref="GraphicsPath"/> for this element.
/// </summary>
/// <value></value>
public override System.Drawing.Drawing2D.GraphicsPath Path
{
get
{
//var path = new GraphicsPath();
//AddPaths(this, path);
return GetPaths(this);
}
protected set
{ }
}
/// <summary>
/// Gets the bounds of the element.
/// </summary>
/// <value>The bounds.</value>
public override System.Drawing.RectangleF Bounds
{
get
{
var r = new RectangleF();
foreach (var c in this.Children)
{
if (c is SvgVisualElement)
{
// First it should check if rectangle is empty or it will return the wrong Bounds.
// This is because when the Rectangle is Empty, the Union method adds as if the first values where X=0, Y=0
if (r.IsEmpty)
{
r = ((SvgVisualElement)c).Bounds;
}
else
{
var childBounds = ((SvgVisualElement)c).Bounds;
if (!childBounds.IsEmpty)
{
r = RectangleF.Union(r, childBounds);
}
}
}
}
return r;
}
}
/// <summary>
/// Renders the <see cref="SvgElement"/> and contents to the specified <see cref="Graphics"/> object.
/// </summary>
/// <param name="renderer">The <see cref="Graphics"/> object to render to.</param>
protected override void Render(SvgRenderer renderer)
{
if (!Visible || !Displayable)
return;
this.PushTransforms(renderer);
this.SetClip(renderer);
base.RenderChildren(renderer);
this.ResetClip(renderer);
this.PopTransforms(renderer);
}
public override SvgElement DeepCopy()
{
return DeepCopy<SvgSwitch>();
}
public override SvgElement DeepCopy<T>()
{
var newObj = base.DeepCopy<T>() as SvgSwitch;
if (this.Fill != null)
newObj.Fill = this.Fill.DeepCopy() as SvgPaintServer;
return newObj;
}
}
}
using System.Drawing;
using System.Drawing.Drawing2D;
namespace Svg
{
/// <summary>
/// The foreignObject element allows for inclusion of a foreign namespace which has its graphical content drawn by a different user agent
/// </summary>
[SvgElement("foreignObject")]
public class SvgForeignObject : SvgVisualElement
{
public SvgForeignObject()
{
}
/// <summary>
/// Gets the <see cref="GraphicsPath"/> for this element.
/// </summary>
/// <value></value>
public override System.Drawing.Drawing2D.GraphicsPath Path
{
get
{
//var path = new GraphicsPath();
//AddPaths(this, path);
return GetPaths(this);
}
protected set
{ }
}
/// <summary>
/// Gets the bounds of the element.
/// </summary>
/// <value>The bounds.</value>
public override System.Drawing.RectangleF Bounds
{
get
{
var r = new RectangleF();
foreach (var c in this.Children)
{
if (c is SvgVisualElement)
{
// First it should check if rectangle is empty or it will return the wrong Bounds.
// This is because when the Rectangle is Empty, the Union method adds as if the first values where X=0, Y=0
if (r.IsEmpty)
{
r = ((SvgVisualElement)c).Bounds;
}
else
{
var childBounds = ((SvgVisualElement)c).Bounds;
if (!childBounds.IsEmpty)
{
r = RectangleF.Union(r, childBounds);
}
}
}
}
return r;
}
}
/// <summary>
/// Renders the <see cref="SvgElement"/> and contents to the specified <see cref="Graphics"/> object.
/// </summary>
/// <param name="renderer">The <see cref="Graphics"/> object to render to.</param>
protected override void Render(SvgRenderer renderer)
{
if (!Visible || !Displayable)
return;
this.PushTransforms(renderer);
this.SetClip(renderer);
base.RenderChildren(renderer);
this.ResetClip(renderer);
this.PopTransforms(renderer);
}
public override SvgElement DeepCopy()
{
return DeepCopy<SvgForeignObject>();
}
public override SvgElement DeepCopy<T>()
{
var newObj = base.DeepCopy<T>() as SvgForeignObject;
if (this.Fill != null)
newObj.Fill = this.Fill.DeepCopy() as SvgPaintServer;
return newObj;
}
}
}
namespace Svg
{
public class NonSvgElement : SvgElement
{
public NonSvgElement()
{
}
public NonSvgElement(string elementName)
{
this.ElementName = elementName;
}
public override SvgElement DeepCopy()
{
return DeepCopy<NonSvgElement>();
}
public override SvgElement DeepCopy<T>()
{
var newObj = base.DeepCopy<T>() as NonSvgElement;
return newObj;
}
}
}
\ No newline at end of file
...@@ -112,8 +112,10 @@ ...@@ -112,8 +112,10 @@
<Compile Include="DataTypes\SvgOverflow.cs" /> <Compile Include="DataTypes\SvgOverflow.cs" />
<Compile Include="DataTypes\SvgUnitCollection.cs" /> <Compile Include="DataTypes\SvgUnitCollection.cs" />
<Compile Include="DataTypes\SvgViewBox.cs" /> <Compile Include="DataTypes\SvgViewBox.cs" />
<Compile Include="Document Structure\SvgSwitch.cs" />
<Compile Include="Document Structure\SvgTitle.cs" /> <Compile Include="Document Structure\SvgTitle.cs" />
<Compile Include="Document Structure\SvgDocumentMetadata.cs" /> <Compile Include="Document Structure\SvgDocumentMetadata.cs" />
<Compile Include="Extensibility\SvgForeignObject.cs" />
<Compile Include="Painting\ISvgBoundable.cs" /> <Compile Include="Painting\ISvgBoundable.cs" />
<Compile Include="Painting\SvgMarker.cs" /> <Compile Include="Painting\SvgMarker.cs" />
<Compile Include="Document Structure\SvgDefinitionList.cs" /> <Compile Include="Document Structure\SvgDefinitionList.cs" />
...@@ -133,6 +135,8 @@ ...@@ -133,6 +135,8 @@
<Compile Include="Filter Effects\feMerge\SvgMerge.cs" /> <Compile Include="Filter Effects\feMerge\SvgMerge.cs" />
<Compile Include="Painting\EnumConverters.cs" /> <Compile Include="Painting\EnumConverters.cs" />
<Compile Include="SvgDefinitionDefaults.cs" /> <Compile Include="SvgDefinitionDefaults.cs" />
<Compile Include="NonSvgElement.cs" />
<Compile Include="SvgUnknownElement.cs" />
<Compile Include="SvgElementAttribute.cs" /> <Compile Include="SvgElementAttribute.cs" />
<Compile Include="SvgExtentions.cs" /> <Compile Include="SvgExtentions.cs" />
<Compile Include="SvgRenderer.cs" /> <Compile Include="SvgRenderer.cs" />
......
...@@ -93,7 +93,11 @@ namespace Svg ...@@ -93,7 +93,11 @@ namespace Svg
ElementInfo validType = AvailableElements.SingleOrDefault(e => e.ElementName == elementName); ElementInfo validType = AvailableElements.SingleOrDefault(e => e.ElementName == elementName);
if (validType != null) if (validType != null)
{ {
createdElement = (SvgElement)Activator.CreateInstance(validType.ElementType); createdElement = (SvgElement) Activator.CreateInstance(validType.ElementType);
}
else
{
createdElement = new SvgUnknownElement(elementName);
} }
} }
...@@ -102,6 +106,12 @@ namespace Svg ...@@ -102,6 +106,12 @@ namespace Svg
SetAttributes(createdElement, reader, document); SetAttributes(createdElement, reader, document);
} }
} }
else
{
// All non svg element (html, ...)
createdElement = new NonSvgElement(elementName);
SetAttributes(createdElement, reader, document);
}
//Trace.TraceInformation("End CreateElement"); //Trace.TraceInformation("End CreateElement");
...@@ -119,7 +129,7 @@ namespace Svg ...@@ -119,7 +129,7 @@ namespace Svg
while (reader.MoveToNextAttribute()) while (reader.MoveToNextAttribute())
{ {
// Special treatment for "style" // Special treatment for "style"
if (reader.LocalName.Equals("style")) if (reader.LocalName.Equals("style") && !(element is NonSvgElement))
{ {
styles = reader.Value.Split(new char[] { ';' }, StringSplitOptions.RemoveEmptyEntries); styles = reader.Value.Split(new char[] { ';' }, StringSplitOptions.RemoveEmptyEntries);
...@@ -139,7 +149,7 @@ namespace Svg ...@@ -139,7 +149,7 @@ namespace Svg
{ {
if (!styles.Contains("font-size") && document.CustomAttributes.ContainsKey("font-size") && document.CustomAttributes["font-size"] != null) if (!styles.Contains("font-size") && document.CustomAttributes.ContainsKey("font-size") && document.CustomAttributes["font-size"] != null)
{ {
SetPropertyValue(element, "font-size" , document.CustomAttributes["font-size"], document); SetPropertyValue(element, "font-size", document.CustomAttributes["font-size"], document);
} }
if (!styles.Contains("font-family") && document.CustomAttributes.ContainsKey("font-family") && document.CustomAttributes["font-family"] != null) if (!styles.Contains("font-family") && document.CustomAttributes.ContainsKey("font-family") && document.CustomAttributes["font-family"] != null)
{ {
......
namespace Svg
{
public class SvgUnknownElement : SvgElement
{
public SvgUnknownElement()
{
}
public SvgUnknownElement(string elementName)
{
this.ElementName = elementName;
}
public override SvgElement DeepCopy()
{
return DeepCopy<SvgUnknownElement>();
}
public override SvgElement DeepCopy<T>()
{
var newObj = base.DeepCopy<T>() as SvgUnknownElement;
return newObj;
}
}
}
\ 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