public void BeginFigure_Adds_New_Figure() { var geometry = new XPathGeometry(); var target = new XPathGeometryContext(geometry); Assert.Equal(0, geometry.Figures.Length); target.BeginFigure(new XPoint()); Assert.Equal(1, geometry.Figures.Length); Assert.True(geometry.Figures[0].IsFilled); Assert.True(geometry.Figures[0].IsClosed); target.SetClosedState(false); Assert.False(geometry.Figures[0].IsClosed); }
public void CubicBezierTo_Adds_New_XCubicBezierSegment() { var geometry = new XPathGeometry(); var target = new XPathGeometryContext(geometry); target.BeginFigure(new XPoint()); Assert.Equal(0, geometry.Figures[0].Segments.Length); target.CubicBezierTo(new XPoint(), new XPoint(), new XPoint()); Assert.Equal(1, geometry.Figures[0].Segments.Length); var segment = geometry.Figures[0].Segments[0]; Assert.IsType <XCubicBezierSegment>(segment); Assert.True(segment.IsStroked); Assert.True(segment.IsSmoothJoin); }
public void PolyQuadraticBezierTo_Adds_New_XPolyQuadraticBezierSegment() { var geometry = new XPathGeometry(); var target = new XPathGeometryContext(geometry); target.BeginFigure(new XPoint()); Assert.Equal(0, geometry.Figures[0].Segments.Length); target.PolyQuadraticBezierTo(ImmutableArray.Create <XPoint>(new XPoint(), new XPoint(), new XPoint())); Assert.Equal(1, geometry.Figures[0].Segments.Length); var segment = geometry.Figures[0].Segments[0]; Assert.IsType <XPolyQuadraticBezierSegment>(segment); Assert.True(segment.IsStroked); Assert.True(segment.IsSmoothJoin); }
/// <summary> /// Parse a mini-language string representation of the <see cref="XPathGeometry"/>. /// </summary> /// <remarks> /// The path geometry syntax may start with a "wsp*Fwsp*(0|1)" which indicate the winding mode (F0 is EvenOdd while F1 is NonZero). /// </remarks> /// <param name="source">The string with geometry data.</param> /// <returns>The new instance of the <see cref="XPathGeometry"/> class.</returns> public static XPathGeometry Parse(string source) { var fillRule = XFillRule.EvenOdd; var geometry = XPathGeometry.Create(ImmutableArray.Create<XPathFigure>(), fillRule); if (source != null) { int curIndex = 0; while ((curIndex < source.Length) && Char.IsWhiteSpace(source, curIndex)) { curIndex++; } if (curIndex < source.Length) { if (source[curIndex] == 'F') { curIndex++; while ((curIndex < source.Length) && Char.IsWhiteSpace(source, curIndex)) { curIndex++; } if ((curIndex == source.Length) || ((source[curIndex] != '0') && (source[curIndex] != '1'))) { throw new FormatException("Illegal token."); } fillRule = source[curIndex] == '0' ? XFillRule.EvenOdd : XFillRule.Nonzero; curIndex++; } } var parser = new SvgToXPathGeometryParser(); var context = new XPathGeometryContext(geometry); parser.Parse(context, source, curIndex); } geometry.FillRule = fillRule; return geometry; }
/// <summary> /// Parse a mini-language string representation of the <see cref="XPathGeometry"/>. /// </summary> /// <remarks> /// The path geometry syntax may start with a "wsp*Fwsp*(0|1)" which indicate the winding mode (F0 is EvenOdd while F1 is NonZero). /// </remarks> /// <param name="source">The string with geometry data.</param> /// <returns>The new instance of the <see cref="XPathGeometry"/> class.</returns> public static XPathGeometry Parse(string source) { var fillRule = XFillRule.EvenOdd; var geometry = XPathGeometry.Create(ImmutableArray.Create <XPathFigure>(), fillRule); if (source != null) { int curIndex = 0; while ((curIndex < source.Length) && Char.IsWhiteSpace(source, curIndex)) { curIndex++; } if (curIndex < source.Length) { if (source[curIndex] == 'F') { curIndex++; while ((curIndex < source.Length) && Char.IsWhiteSpace(source, curIndex)) { curIndex++; } if ((curIndex == source.Length) || ((source[curIndex] != '0') && (source[curIndex] != '1'))) { throw new FormatException("Illegal token."); } fillRule = source[curIndex] == '0' ? XFillRule.EvenOdd : XFillRule.Nonzero; curIndex++; } } var parser = new SvgToXPathGeometryParser(); var context = new XPathGeometryContext(geometry); parser.Parse(context, source, curIndex); } geometry.FillRule = fillRule; return(geometry); }
/// <summary> /// /// </summary> /// <param name="pg"></param> /// <param name="dx"></param> /// <param name="dy"></param> /// <returns></returns> public static XPathGeometry ToXPathGeometry(this PathGeometry pg, double dx, double dy) { var geometry = XPathGeometry.Create( ImmutableArray.Create <XPathFigure>(), pg.FillRule == FillRule.EvenOdd ? XFillRule.EvenOdd : XFillRule.Nonzero); var context = new XPathGeometryContext(geometry); foreach (var pf in pg.Figures) { context.BeginFigure( XPoint.Create(pf.StartPoint.X + dx, pf.StartPoint.Y + dy), pf.IsFilled, pf.IsClosed); foreach (var segment in pf.Segments) { if (segment is ArcSegment) { var arcSegment = segment as ArcSegment; context.ArcTo( XPoint.Create(arcSegment.Point.X + dx, arcSegment.Point.Y + dy), XPathSize.Create(arcSegment.Size.Width, arcSegment.Size.Height), arcSegment.RotationAngle, arcSegment.IsLargeArc, arcSegment.SweepDirection == SweepDirection.Clockwise ? XSweepDirection.Clockwise : XSweepDirection.Counterclockwise, arcSegment.IsStroked, arcSegment.IsSmoothJoin); } else if (segment is BezierSegment) { var cubicBezierSegment = segment as BezierSegment; context.CubicBezierTo( XPoint.Create(cubicBezierSegment.Point1.X + dx, cubicBezierSegment.Point1.Y + dy), XPoint.Create(cubicBezierSegment.Point2.X + dx, cubicBezierSegment.Point2.Y + dy), XPoint.Create(cubicBezierSegment.Point3.X + dx, cubicBezierSegment.Point3.Y + dy), cubicBezierSegment.IsStroked, cubicBezierSegment.IsSmoothJoin); } else if (segment is LineSegment) { var lineSegment = segment as LineSegment; context.LineTo( XPoint.Create(lineSegment.Point.X + dx, lineSegment.Point.Y + dy), lineSegment.IsStroked, lineSegment.IsSmoothJoin); } else if (segment is PolyBezierSegment) { var polyCubicBezierSegment = segment as PolyBezierSegment; context.PolyCubicBezierTo( ToXPoints(polyCubicBezierSegment.Points, dx, dy), polyCubicBezierSegment.IsStroked, polyCubicBezierSegment.IsSmoothJoin); } else if (segment is PolyLineSegment) { var polyLineSegment = segment as PolyLineSegment; context.PolyLineTo( ToXPoints(polyLineSegment.Points, dx, dy), polyLineSegment.IsStroked, polyLineSegment.IsSmoothJoin); } else if (segment is PolyQuadraticBezierSegment) { var polyQuadraticSegment = segment as PolyQuadraticBezierSegment; context.PolyQuadraticBezierTo( ToXPoints(polyQuadraticSegment.Points, dx, dy), polyQuadraticSegment.IsStroked, polyQuadraticSegment.IsSmoothJoin); } else if (segment is QuadraticBezierSegment) { var quadraticBezierSegment = segment as QuadraticBezierSegment; context.QuadraticBezierTo( XPoint.Create(quadraticBezierSegment.Point1.X + dx, quadraticBezierSegment.Point1.Y + dy), XPoint.Create(quadraticBezierSegment.Point2.X + dx, quadraticBezierSegment.Point2.Y + dy), quadraticBezierSegment.IsStroked, quadraticBezierSegment.IsSmoothJoin); } else { throw new NotSupportedException("Not supported segment type: " + segment.GetType()); } } } return(geometry); }
public void Inherits_From_XGeometryContext() { var target = new XPathGeometryContext(new XPathGeometry()); Assert.True(target is XGeometryContext); }