Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SVG: Added support for curves and removed flattening of the geometry #79

Merged
merged 1 commit into from Apr 9, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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