Skip to content

Commit

Permalink
SVG: Added support for curves and removed flattening of the geometry
Browse files Browse the repository at this point in the history
  • Loading branch information
mpalmans committed Apr 2, 2017
1 parent a80c505 commit 1094d80
Show file tree
Hide file tree
Showing 2 changed files with 89 additions and 10 deletions.
3 changes: 1 addition & 2 deletions src/WpfMath/CharBox.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,7 @@ public override void RenderGeometry(GeometryGroup geometry, double scale, double
GlyphRun glyphRun = GetGlyphRun(scale, x, y);

GeometryGroup geoGroup = glyphRun.BuildGeometry() as GeometryGroup;
PathGeometry pg = geoGroup.GetFlattenedPathGeometry();
geometry.Children.Add(pg);
geometry.Children.Add(geoGroup);
}

public override int GetLastFontId()
Expand Down
96 changes: 88 additions & 8 deletions src/WpfMath/SVGConverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using System.Text;
using System.Windows.Media;
using System.Globalization;
using System.Diagnostics;

namespace WpfMath
{
Expand Down Expand Up @@ -48,6 +49,10 @@ private void AddGeometry(StringBuilder svgString, GeometryGroup group)
RectangleGeometry rectangle = (RectangleGeometry)geometry;
AddGeometry(svgString, rectangle);
}
else
{
Debug.Assert(false);
}
}
if (!group.Transform.Value.IsIdentity)
{
Expand All @@ -61,22 +66,97 @@ private void AddGeometry(StringBuilder svgString, GeometryGroup group)
private void AddGeometry(StringBuilder svgString, PathGeometry path)
{
svgString.Append("<path d=\"");

foreach (PathFigure pf in path.Figures)
{

svgString.Append("M ");
svgString.AppendFormat(CultureInfo.InvariantCulture, "{0} ", pf.StartPoint.X);
svgString.AppendFormat(CultureInfo.InvariantCulture, "{0} ", pf.StartPoint.Y);

foreach (PathSegment ps in pf.Segments)
{
PolyLineSegment seg = ps as PolyLineSegment;
svgString.Append("M ");
svgString.AppendFormat(CultureInfo.InvariantCulture, "{0} ", seg.Points[0].X);
svgString.AppendFormat(CultureInfo.InvariantCulture, "{0} ", seg.Points[0].Y);
for (int i = 1; i < seg.Points.Count; ++i)
if (ps is PolyLineSegment)
{
PolyLineSegment seg = ps as PolyLineSegment;
for (int i = 0; i < seg.Points.Count; ++i)
{
svgString.Append("L ");
svgString.AppendFormat(CultureInfo.InvariantCulture, "{0} ", seg.Points[i].X);
svgString.AppendFormat(CultureInfo.InvariantCulture, "{0} ", seg.Points[i].Y);
}
}
else if (ps is PolyBezierSegment)
{
PolyBezierSegment seg = ps as PolyBezierSegment;
for (int i = 0; i < seg.Points.Count; i += 3)
{
var point = seg.Points[i];
svgString.Append("C ");
svgString.AppendFormat(CultureInfo.InvariantCulture, "{0} ", seg.Points[i].X);
svgString.AppendFormat(CultureInfo.InvariantCulture, "{0} ", seg.Points[i].Y);

svgString.AppendFormat(CultureInfo.InvariantCulture, "{0} ", seg.Points[i + 1].X);
svgString.AppendFormat(CultureInfo.InvariantCulture, "{0} ", seg.Points[i + 1].Y);

svgString.AppendFormat(CultureInfo.InvariantCulture, "{0} ", seg.Points[i + 2].X);
svgString.AppendFormat(CultureInfo.InvariantCulture, "{0} ", seg.Points[i + 2].Y);
}
}
else if (ps is LineSegment)
{
LineSegment seg = ps as LineSegment;
svgString.Append("L ");
svgString.AppendFormat(CultureInfo.InvariantCulture, "{0} ", seg.Points[i].X);
svgString.AppendFormat(CultureInfo.InvariantCulture, "{0} ", seg.Points[i].Y);
svgString.AppendFormat(CultureInfo.InvariantCulture, "{0} ", seg.Point.X);
svgString.AppendFormat(CultureInfo.InvariantCulture, "{0} ", seg.Point.Y);
}
else if (ps is BezierSegment)
{
BezierSegment seg = ps as BezierSegment;
svgString.Append("C ");
svgString.AppendFormat(CultureInfo.InvariantCulture, "{0} ", seg.Point1.X);
svgString.AppendFormat(CultureInfo.InvariantCulture, "{0} ", seg.Point1.Y);

svgString.AppendFormat(CultureInfo.InvariantCulture, "{0} ", seg.Point2.X);
svgString.AppendFormat(CultureInfo.InvariantCulture, "{0} ", seg.Point2.Y);

svgString.AppendFormat(CultureInfo.InvariantCulture, "{0} ", seg.Point3.X);
svgString.AppendFormat(CultureInfo.InvariantCulture, "{0} ", seg.Point3.Y);
}
else if (ps is QuadraticBezierSegment)
{
//Untested: BuildGeometry converts quadratic bezier to cubic
QuadraticBezierSegment seg = ps as QuadraticBezierSegment;
svgString.Append("Q ");
svgString.AppendFormat(CultureInfo.InvariantCulture, "{0} ", seg.Point1.X);
svgString.AppendFormat(CultureInfo.InvariantCulture, "{0} ", seg.Point1.Y);

svgString.AppendFormat(CultureInfo.InvariantCulture, "{0} ", seg.Point2.X);
svgString.AppendFormat(CultureInfo.InvariantCulture, "{0} ", seg.Point2.Y);
}
else if (ps is PolyQuadraticBezierSegment)
{
//Untested: BuildGeometry converts quadratic bezier to cubic
PolyQuadraticBezierSegment seg = ps as PolyQuadraticBezierSegment;
for (int i = 0; i < seg.Points.Count; i += 2)
{
var point = seg.Points[i];
svgString.Append("Q ");
svgString.AppendFormat(CultureInfo.InvariantCulture, "{0} ", seg.Points[i].X);
svgString.AppendFormat(CultureInfo.InvariantCulture, "{0} ", seg.Points[i].Y);

svgString.AppendFormat(CultureInfo.InvariantCulture, "{0} ", seg.Points[i + 1].X);
svgString.AppendFormat(CultureInfo.InvariantCulture, "{0} ", seg.Points[i + 1].Y);
}
}
else
{
Debug.Assert(false); //If asserted, implement segment type
}
svgString.Append("Z ");
}

if (pf.IsClosed)
svgString.Append("Z ");
}
svgString.Append("\" fill = \"black\" />");
svgString.Append(Environment.NewLine);
Expand Down

0 comments on commit 1094d80

Please sign in to comment.