/// <summary> /// /// </summary> /// <param name="pg"></param> /// <param name="dx"></param> /// <param name="dy"></param> /// <returns></returns> public IPathGeometry ToPathGeometry(AM.PathGeometry pg, double dx, double dy) { var factory = _serviceProvider.GetService <IFactory>(); var geometry = factory.CreatePathGeometry( ImmutableArray.Create <IPathFigure>(), pg.FillRule == AM.FillRule.EvenOdd ? FillRule.EvenOdd : FillRule.Nonzero); var context = new PathGeometryContext(factory, geometry); foreach (var pf in pg.Figures) { context.BeginFigure( factory.CreatePointShape(pf.StartPoint.X + dx, pf.StartPoint.Y + dy), pf.IsFilled, pf.IsClosed); foreach (var segment in pf.Segments) { if (segment is AM.ArcSegment arcSegment) { context.ArcTo( factory.CreatePointShape(arcSegment.Point.X + dx, arcSegment.Point.Y + dy), factory.CreatePathSize(arcSegment.Size.Width, arcSegment.Size.Height), arcSegment.RotationAngle, arcSegment.IsLargeArc, arcSegment.SweepDirection == AM.SweepDirection.Clockwise ? SweepDirection.Clockwise : SweepDirection.Counterclockwise); } else if (segment is AM.BezierSegment cubicBezierSegment) { context.CubicBezierTo( factory.CreatePointShape(cubicBezierSegment.Point1.X + dx, cubicBezierSegment.Point1.Y + dy), factory.CreatePointShape(cubicBezierSegment.Point2.X + dx, cubicBezierSegment.Point2.Y + dy), factory.CreatePointShape(cubicBezierSegment.Point3.X + dx, cubicBezierSegment.Point3.Y + dy)); } else if (segment is AM.LineSegment lineSegment) { context.LineTo( factory.CreatePointShape(lineSegment.Point.X + dx, lineSegment.Point.Y + dy)); } else if (segment is AM.QuadraticBezierSegment quadraticBezierSegment) { context.QuadraticBezierTo( factory.CreatePointShape(quadraticBezierSegment.Point1.X + dx, quadraticBezierSegment.Point1.Y + dy), factory.CreatePointShape(quadraticBezierSegment.Point2.X + dx, quadraticBezierSegment.Point2.Y + dy)); } else { throw new NotSupportedException("Not supported segment type: " + segment.GetType()); } } } return(geometry); }
public void BeginFigure_Adds_New_Figure() { var geometry = new PathGeometry(); var target = new PathGeometryContext(geometry); Assert.Empty(geometry.Figures); target.BeginFigure(new PointShape()); Assert.NotNull(geometry.Figures[0]); 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_CubicBezierSegment() { var geometry = new PathGeometry(); var target = new PathGeometryContext(geometry); target.BeginFigure(new PointShape()); Assert.Empty(geometry.Figures[0].Segments); target.CubicBezierTo(new PointShape(), new PointShape(), new PointShape()); var segment = geometry.Figures[0].Segments[0]; Assert.IsType <CubicBezierSegment>(segment); Assert.True(segment.IsStroked); Assert.True(segment.IsSmoothJoin); }
public void PolyQuadraticBezierTo_Adds_New_XPolyQuadraticBezierSegment() { var geometry = new PathGeometry(); var target = new PathGeometryContext(geometry); target.BeginFigure(new PointShape()); Assert.Empty(geometry.Figures[0].Segments); target.PolyQuadraticBezierTo(ImmutableArray.Create <PointShape>(new PointShape(), new PointShape(), new PointShape())); var segment = geometry.Figures[0].Segments[0]; Assert.IsType <PolyQuadraticBezierSegment>(segment); Assert.True(segment.IsStroked); Assert.True(segment.IsSmoothJoin); }
public void ArcTo_Adds_New_ArcSegment() { var geometry = _factory.CreatePathGeometry(); var target = new PathGeometryContext(_factory, geometry); target.BeginFigure(_factory.CreatePointShape()); Assert.Empty(geometry.Figures[0].Segments); target.ArcTo(_factory.CreatePointShape(), _factory.CreatePathSize()); var segment = geometry.Figures[0].Segments[0]; Assert.IsType <ArcSegment>(segment); Assert.True(segment.IsStroked); Assert.True(segment.IsSmoothJoin); }
public void PolyLineTo_Adds_New_XPolyLineSegment() { var geometry = _factory.CreatePathGeometry(); var target = new PathGeometryContext(_factory, geometry); target.BeginFigure(_factory.CreatePointShape()); Assert.Empty(geometry.Figures[0].Segments); target.PolyLineTo(ImmutableArray.Create <IPointShape>(_factory.CreatePointShape(), _factory.CreatePointShape(), _factory.CreatePointShape())); var segment = geometry.Figures[0].Segments[0]; Assert.IsType <PolyLineSegment>(segment); Assert.True(segment.IsStroked); Assert.True(segment.IsSmoothJoin); }
/// <summary> /// /// </summary> /// <param name="pg"></param> /// <param name="dx"></param> /// <param name="dy"></param> /// <returns></returns> public static Path.PathGeometry ToPathGeometry(this System.Windows.Media.PathGeometry pg, double dx, double dy) { var geometry = Path.PathGeometry.Create( ImmutableArray.Create <Path.PathFigure>(), pg.FillRule == System.Windows.Media.FillRule.EvenOdd ? Path.FillRule.EvenOdd : Path.FillRule.Nonzero); var context = new PathGeometryContext(geometry); foreach (var pf in pg.Figures) { context.BeginFigure( PointShape.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( PointShape.Create(arcSegment.Point.X + dx, arcSegment.Point.Y + dy), PathSize.Create(arcSegment.Size.Width, arcSegment.Size.Height), arcSegment.RotationAngle, arcSegment.IsLargeArc, arcSegment.SweepDirection == System.Windows.Media.SweepDirection.Clockwise ? Path.SweepDirection.Clockwise : Path.SweepDirection.Counterclockwise, arcSegment.IsStroked, arcSegment.IsSmoothJoin); } else if (segment is BezierSegment) { var cubicBezierSegment = segment as BezierSegment; context.CubicBezierTo( PointShape.Create(cubicBezierSegment.Point1.X + dx, cubicBezierSegment.Point1.Y + dy), PointShape.Create(cubicBezierSegment.Point2.X + dx, cubicBezierSegment.Point2.Y + dy), PointShape.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( PointShape.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( ToPointShapes(polyCubicBezierSegment.Points, dx, dy), polyCubicBezierSegment.IsStroked, polyCubicBezierSegment.IsSmoothJoin); } else if (segment is PolyLineSegment) { var polyLineSegment = segment as PolyLineSegment; context.PolyLineTo( ToPointShapes(polyLineSegment.Points, dx, dy), polyLineSegment.IsStroked, polyLineSegment.IsSmoothJoin); } else if (segment is PolyQuadraticBezierSegment) { var polyQuadraticSegment = segment as PolyQuadraticBezierSegment; context.PolyQuadraticBezierTo( ToPointShapes(polyQuadraticSegment.Points, dx, dy), polyQuadraticSegment.IsStroked, polyQuadraticSegment.IsSmoothJoin); } else if (segment is QuadraticBezierSegment) { var quadraticBezierSegment = segment as QuadraticBezierSegment; context.QuadraticBezierTo( PointShape.Create(quadraticBezierSegment.Point1.X + dx, quadraticBezierSegment.Point1.Y + dy), PointShape.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); }