public void DrawPath(IEnumerable<PathOperation> ops, Pen pen = null, BaseBrush brush = null) { using (var path = new Path()) { var bb = new BoundingBoxBuilder(); foreach (var op in ops) { var moveTo = op as MoveTo; if (moveTo != null) { var start = moveTo.Start; var end = moveTo.End; path.MoveTo((float) start.X, (float) start.Y); bb.Add(start); bb.Add(end); continue; } var lineTo = op as LineTo; if (lineTo != null) { var start = lineTo.Start; var end = lineTo.End; path.LineTo((float) start.X, (float) start.Y); path.LineTo((float) end.X, (float) end.Y); bb.Add(start); bb.Add(end); continue; } var at = op as ArcTo; if (at != null) { var p = at.Point; path.LineTo((float) p.X, (float) p.Y); bb.Add(p); continue; } var curveTo = op as CurveTo; if (curveTo != null) { var end = curveTo.End; var firstControlPoint = curveTo.FirstControlPoint; var secondControlPoint = curveTo.SecondControlPoint; path.CubicTo((float) firstControlPoint.X, (float) firstControlPoint.Y, (float) secondControlPoint.X, (float) secondControlPoint.Y, (float) end.X, (float) end.Y); bb.Add(firstControlPoint); bb.Add(secondControlPoint); bb.Add(end); continue; } var cp = op as ClosePath; if (cp != null) { path.Close(); continue; } throw new NotSupportedException("Path Op " + op); } var frame = bb.BoundingBox; if (brush != null) { var solidBrush = brush as SolidBrush; if (solidBrush != null) { path.SetFillType(GetPathFillType(((SolidBrush)brush).FillMode)); } var brushPaint = GetBrushPaint(brush, frame); graphics.DrawPath(path, brushPaint); } if (pen != null) { var penPaint = GetPenPaint(pen); graphics.DrawPath(path, penPaint); } } }
public void DrawPath(IEnumerable<PathOperation> ops, Pen pen = null, BaseBrush brush = null) { var bb = new BoundingBoxBuilder (); var s = new D2D1.PathGeometry (factories.D2DFactory); var figureDepth = 0; using (var sink = s.Open ()) { foreach (var op in ops) { if (op is MoveTo) { while (figureDepth > 0) { sink.EndFigure (D2D1.FigureEnd.Open); figureDepth--; } var mt = ((MoveTo)op); sink.BeginFigure (Conversions.ToVector2 (mt.Start), D2D1.FigureBegin.Filled); figureDepth++; bb.Add (mt.Start); } else if (op is LineTo) { var lt = ((LineTo)op); sink.AddLine (lt.Start.ToVector2()); sink.AddLine (lt.End.ToVector2()); bb.Add (lt.Start); bb.Add (lt.End); } else if (op is ArcTo) { var ar = ((ArcTo)op); // TODO: Direct2D Arcs //sink.AddArc (new D2D1.ArcSegment { // Size = Conversions.ToSize2F (ar.Radius), // Point = Conversions.ToVector2 (ar.Point), // SweepDirection = ar.SweepClockwise ? D2D1.SweepDirection.Clockwise : D2D1.SweepDirection.CounterClockwise, //}); sink.AddLine (Conversions.ToVector2 (ar.Point)); bb.Add (ar.Point); } else if (op is CurveTo) { var ct = ((CurveTo)op); sink.AddBezier (new D2D1.BezierSegment { Point1 = Conversions.ToVector2 (ct.FirstControlPoint), Point2 = Conversions.ToVector2 (ct.SecondControlPoint), Point3 = Conversions.ToVector2 (ct.End), }); bb.Add(ct.FirstControlPoint); bb.Add(ct.SecondControlPoint); bb.Add(ct.End); } else if (op is ClosePath) { sink.EndFigure (D2D1.FigureEnd.Closed); figureDepth--; } else { // TODO: More path operations } } while (figureDepth > 0) { sink.EndFigure (D2D1.FigureEnd.Open); figureDepth--; } sink.Close (); } var p = GetBrush (pen); var b = GetBrush (bb.BoundingBox, brush); if (b != null) { renderTarget.FillGeometry (s, b); } if (p != null) { renderTarget.DrawGeometry (s, p, (float)pen.Width, GetStrokeStyle (pen)); } }
public void DrawPath(IEnumerable<PathOperation> ops, Pen pen = null, BaseBrush baseBrush = null) { using (var path = new GraphicsPath()) { var bb = new BoundingBoxBuilder(); var position = Point.Zero; foreach (var op in ops) { var start = op as StartFigure; if (start != null) { path.StartFigure(); continue; } var moveTo = op as MoveTo; if (moveTo != null) { path.StartFigure(); continue; } var lineTo = op as LineTo; if (lineTo != null) { var p = lineTo.Start; path.AddLine(Conversions.GetPointF(lineTo.Start), Conversions.GetPointF(lineTo.End)); position = p; bb.Add(p); continue; } var arcTo = op as ArcTo; if (arcTo != null) { var p = arcTo.Point; path.AddLine(Conversions.GetPointF(position), Conversions.GetPointF(p)); position = p; bb.Add(p); continue; } var curveTo = op as CurveTo; if (curveTo != null) { path.AddBezier(Conversions.GetPointF(curveTo.Start), Conversions.GetPointF(curveTo.FirstControlPoint), Conversions.GetPointF(curveTo.SecondControlPoint), Conversions.GetPointF(curveTo.End)); bb.Add(curveTo.Start); bb.Add(curveTo.FirstControlPoint); bb.Add(curveTo.SecondControlPoint); bb.Add(curveTo.End); continue; } var closePath = op as ClosePath; if (closePath != null) { path.CloseFigure(); continue; } throw new NotSupportedException("Path Op " + op); } var frame = bb.BoundingBox; if (baseBrush != null) { graphics.FillPath(baseBrush.GetBrush(frame), path); } if (pen != null) { var r = Conversions.GetRectangleF(frame); graphics.DrawPath(pen.GetPen(), path); } } }