Skip to content

Commit

Permalink
bugfixes, work on code
Browse files Browse the repository at this point in the history
  • Loading branch information
jogibear9988 committed Feb 18, 2019
1 parent b9b4170 commit 9949243
Show file tree
Hide file tree
Showing 14 changed files with 104 additions and 56 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@
<ProjectGuid>{89EEDB4B-473C-4733-9FB9-A05DF3F67620}</ProjectGuid>
<OutputType>library</OutputType>
<AppDesignerFolder>Properties</AppDesignerFolder>
<RootNamespace>SVGImage.Design</RootNamespace>
<AssemblyName>SVGImage.Design</AssemblyName>
<RootNamespace>DotNetProjects.SVGImage.Design</RootNamespace>
<AssemblyName>DotNetProjects.SVGImage.Design</AssemblyName>
<TargetFrameworkVersion>v4.0</TargetFrameworkVersion>
<FileAlignment>512</FileAlignment>
<ProjectTypeGuids>{60dc8134-eba5-43b8-bcc9-bb4bc16c2548};{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}</ProjectTypeGuids>
Expand Down
5 changes: 4 additions & 1 deletion SVGImage/SVGImage.sln
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "DotNetProjects.SVGImage", "
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Example", "Example\Example.csproj", "{6BDADE27-F4F1-4E84-8BB0-4D5989B4380B}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SVGImage.Design", "SVGImage.Design\SVGImage.Design.csproj", "{89EEDB4B-473C-4733-9FB9-A05DF3F67620}"
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "DotNetProjects.SVGImage.Design", "SVGImage.Design\DotNetProjects.SVGImage.Design.csproj", "{89EEDB4B-473C-4733-9FB9-A05DF3F67620}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Expand All @@ -31,4 +31,7 @@ Global
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {299F3503-8850-481B-BB27-017B8485B0F6}
EndGlobalSection
EndGlobal
1 change: 1 addition & 0 deletions SVGImage/SVGImage/DotNetProjects.SVGImage.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@
<Compile Include="SVG\TextRender.cs" />
<Compile Include="SVG\TextStyle.cs" />
<Compile Include="SVG\Animation\AnimateTransform.cs" />
<Compile Include="SVG\Visibility.cs" />
<EmbeddedResource Include="Properties\Resources.resx">
<Generator>ResXFileCodeGenerator</Generator>
<LastGenOutput>Resources.Designer.cs</LastGenOutput>
Expand Down
3 changes: 1 addition & 2 deletions SVGImage/SVGImage/SVG/CSS/StyleParser.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using System;
using System.Collections.Generic;
using System.Collections.Generic;
using System.Text.RegularExpressions;

namespace SVGImage.SVG
Expand Down
2 changes: 1 addition & 1 deletion SVGImage/SVGImage/SVG/SVGRender.cs
Original file line number Diff line number Diff line change
Expand Up @@ -479,7 +479,7 @@ internal DrawingGroup LoadGroup(IList<Shape> elements, Rect? viewBox, bool isSwi
{
s.Point1 = lastPoint + (Vector)c.CtrlPoint1;

if (c.Command == 'q')
if (c.Command == 'q' && lastq != null) // fix for horse svg! needed ?? or is it wrong in SVG?
{
// first control point is a mirrored point of last end control point
//s.Point1 = lastPoint + new Vector(lastc.Point.X - dx, lastc.Point.Y - dy);
Expand Down
2 changes: 2 additions & 0 deletions SVGImage/SVGImage/SVG/SVGTags.cs
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@ class SVGTags
public const string sShapeUse = "use";
public const string sShapeImage = "image";

public const string sVisibility = "visibility";

public const string sSwitch = "switch";

public const string sRequiredFeatures = "requiredFeatures";
Expand Down
19 changes: 15 additions & 4 deletions SVGImage/SVGImage/SVG/ShapeUtil.cs
Original file line number Diff line number Diff line change
Expand Up @@ -215,13 +215,22 @@ static bool GetValueRespectingUnits(string inputstring, out double value, double
return true;
}
catch (FormatException)
{
}
{ }
}
return false;
}

public static double AttrValue(ShapeUtil.Attribute attr)

public static double GetDoubleValue(string value, double percentageMaximum = 1)
{
double result = 0;
if (GetValueRespectingUnits(value, out result, percentageMaximum))
{
return result;
}
return 0;
}

public static double AttrValue(ShapeUtil.Attribute attr)
{
double result = 0;
GetValueRespectingUnits(attr.Value, out result, 1);
Expand All @@ -241,6 +250,7 @@ public static double AttrValue(XmlNode node, string id, double defaultvalue, dou
}
return defaultvalue;
}

public static string AttrValue(XmlNode node, string id, string defaultvalue)
{
if (node.Attributes == null)
Expand All @@ -250,6 +260,7 @@ public static string AttrValue(XmlNode node, string id, string defaultvalue)
return attr.Value;
return defaultvalue;
}

public static string AttrValue(XmlNode node, string id)
{
return AttrValue(node, id, string.Empty);
Expand Down
16 changes: 2 additions & 14 deletions SVGImage/SVGImage/SVG/Shapes/Group.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using System.Collections.Generic;
using System.Text.RegularExpressions;
using System.Xml;
using DotNetProjects.SVGImage.SVG.Animation;
using DotNetProjects.SVGImage.SVG.Shapes.Filter;
Expand All @@ -10,22 +9,11 @@ public class Group : Shape
{
private List<Shape> m_elements = new List<Shape>();

public IList<Shape> Elements
{
get { return this.m_elements.AsReadOnly(); }
}
public IList<Shape> Elements => this.m_elements.AsReadOnly();

public bool IsSwitch { get; set; }

private Shape AddChild(Shape shape)
{
this.m_elements.Add(shape);
shape.Parent = this;
return shape;
}

public Group(SVG svg, XmlNode node, Shape parent)
: base(svg, node)
public Group(SVG svg, XmlNode node, Shape parent) : base(svg, node)
{
// parent on group must be set before children are added
var clp = XmlUtil.AttrValue(node, "clip-path", null);
Expand Down
14 changes: 3 additions & 11 deletions SVGImage/SVGImage/SVG/Shapes/ImageShape.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,9 @@ public class ImageShape : Shape

public double Height { get; set; }

public ImageSource ImageSource { get; private set; }
public ImageSource ImageSource { get; }

public ImageShape(SVG svg, XmlNode node)
: base(svg, node)
public ImageShape(SVG svg, XmlNode node) : base(svg, node)
{
this.X = XmlUtil.AttrValue(node, "x", 0, svg.Size.Width);
this.Y = XmlUtil.AttrValue(node, "y", 0, svg.Size.Height);
Expand All @@ -34,19 +33,12 @@ public ImageShape(SVG svg, XmlNode node)
b.BeginInit();
if (hRef.StartsWith("data:image/png;base64"))
{
b.StreamSource =
new System.IO.MemoryStream(
Convert.FromBase64String(hRef.Substring("data:image/png;base64,".Length)));
b.StreamSource = new MemoryStream(Convert.FromBase64String(hRef.Substring("data:image/png;base64,".Length)));
}
else
{
if (svg.ExternalFileLoader != null)
b.StreamSource = svg.ExternalFileLoader.LoadFile(hRef, svg.Filename);
// filename given must be relative to the location of the svg file
//string svgpath = System.IO.Path.GetDirectoryName(svg.Filename);
//string filename = System.IO.Path.Combine(svgpath, hRef);
//if (File.Exists(filename))
// b.UriSource = new Uri(filename, UriKind.RelativeOrAbsolute);
}

b.EndInit();
Expand Down
3 changes: 1 addition & 2 deletions SVGImage/SVGImage/SVG/Shapes/LineShape.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,7 @@ public class LineShape : Shape

public Point P2 { get; private set; }

public LineShape(SVG svg, XmlNode node)
: base(svg, node)
public LineShape(SVG svg, XmlNode node) : base(svg, node)
{
double x1 = XmlUtil.AttrValue(node, "x1", 0, svg.Size.Width);
double y1 = XmlUtil.AttrValue(node, "y1", 0, svg.Size.Height);
Expand Down
49 changes: 40 additions & 9 deletions SVGImage/SVGImage/SVG/Shapes/RectangleShape.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,21 +28,52 @@ public override Fill Fill

public double RY { get; set; }

public RectangleShape(SVG svg, XmlNode node)
: base(svg, node)
public RectangleShape(SVG svg, XmlNode node) : base(svg, node)
{
this.X = XmlUtil.AttrValue(node, "x", 0, svg.Size.Width);
this.Y = XmlUtil.AttrValue(node, "y", 0, svg.Size.Height);
this.Width = XmlUtil.AttrValue(node, "width", 0, svg.Size.Width);
this.Height = XmlUtil.AttrValue(node, "height", 0, svg.Size.Height);
this.RX = XmlUtil.AttrValue(node, "rx", 0, svg.Size.Width);
this.RY = XmlUtil.AttrValue(node, "ry", 0, svg.Size.Height);

if (DefaultFill == null)
{
DefaultFill = new Fill(svg);
DefaultFill.Color = svg.PaintServers.Parse("black");
}
}

protected override void Parse(SVG svg, string name, string value)
{
if (name.Contains(":"))
name = name.Split(':')[1];

if (name == "x")
{
this.X = XmlUtil.GetDoubleValue(value, svg.Size.Width);
return;
}
if (name == "y")
{
this.Y = XmlUtil.GetDoubleValue(value, svg.Size.Height);
return;
}
if (name == "width")
{
this.Width = XmlUtil.GetDoubleValue(value, svg.Size.Width);
return;
}
if (name == "height")
{
this.Height = XmlUtil.GetDoubleValue(value, svg.Size.Height);
return;
}
if (name == "rx")
{
this.RX = XmlUtil.GetDoubleValue(value, svg.Size.Width);
return;
}
if (name == "ry")
{
this.RY = XmlUtil.GetDoubleValue(value, svg.Size.Height);
return;
}

base.Parse(svg, name, value);
}
}
}
16 changes: 13 additions & 3 deletions SVGImage/SVGImage/SVG/Shapes/Shape.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ internal Clip Clip

public string RequiredFeatures { get; set; }

public Visibility Visibility { get; set; }

public virtual Stroke Stroke
{
get
Expand Down Expand Up @@ -80,19 +82,23 @@ public virtual TextStyle TextStyle
}
}

public double Opacity { get; set; }
public double Opacity
{
get => Visibility == Visibility.Visible ? m_opacity : 0;
set => m_opacity = value;
}

public virtual Transform Transform { get; private set; }

internal virtual Filter Filter { get; private set; }

//Used during render
internal Shape RealParent;
private double m_opacity;

public Shape Parent { get; set; }

public Shape(SVG svg, XmlNode node)
: this(svg, node, null)
public Shape(SVG svg, XmlNode node) : this(svg, node, null)
{
}

Expand Down Expand Up @@ -182,6 +188,10 @@ protected virtual void Parse(SVG svg, string name, string value)
this.Transform = ShapeUtil.ParseTransform(value.ToLower());
return;
}
if (name == SVGTags.sVisibility)
{
this.Visibility = value == "hidden" ? Visibility.Hidden : Visibility.Visible;
}
if (name == SVGTags.sStroke)
{
this.GetStroke(svg).Color = svg.PaintServers.Parse(value);
Expand Down
18 changes: 11 additions & 7 deletions SVGImage/SVGImage/SVG/Shapes/UseShape.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,7 @@ public class UseShape : Shape
public string hRef { get; set; }

public UseShape(SVG svg, XmlNode node) : base(svg, node)
{
this.X = XmlUtil.AttrValue(node, "x", 0, svg.Size.Width);
this.Y = XmlUtil.AttrValue(node, "y", 0, svg.Size.Height);
//this.hRef = XmlUtil.AttrValue(node, "xlink:href", string.Empty);
//if (this.hRef.StartsWith("#"))
// this.hRef = this.hRef.Substring(1);
}
{ }

protected override void Parse(SVG svg, string name, string value)
{
Expand All @@ -31,6 +25,16 @@ protected override void Parse(SVG svg, string name, string value)
this.hRef = this.hRef.Substring(1);
return;
}
if (name == "x")
{
this.X = XmlUtil.GetDoubleValue(value, svg.Size.Width);
return;
}
if (name == "y")
{
this.Y = XmlUtil.GetDoubleValue(value, svg.Size.Height);
return;
}

base.Parse(svg, name, value);
}
Expand Down
8 changes: 8 additions & 0 deletions SVGImage/SVGImage/SVG/Visibility.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
namespace SVGImage.SVG
{
public enum Visibility
{
Visible,
Hidden
}
}

0 comments on commit 9949243

Please sign in to comment.