コード例 #1
0
        public void DrawPath(IEnumerable <PathOp> ops, Pen pen = null, Brush 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.Point), D2D1.FigureBegin.Filled);
                        figureDepth++;
                        bb.Add(mt.Point);
                    }
                    else if (op is LineTo)
                    {
                        var lt = ((LineTo)op);
                        sink.AddLine(Conversions.ToVector2(lt.Point));
                        bb.Add(lt.Point);
                    }
                    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.Control1),
                            Point2 = Conversions.ToVector2(ct.Control2),
                            Point3 = Conversions.ToVector2(ct.Point),
                        });
                        bb.Add(ct.Point);
                        bb.Add(ct.Control1);
                        bb.Add(ct.Control2);
                    }
                    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));
            }
        }