Exemplo n.º 1
0
 private static void DefineRectangle(StreamGeometryContext context, Thickness th, CornerRadiusExt r, double w, double h)
 {
     context.BeginFigure(new Point(th.Left, th.Top + r.TopLeftLeft), true, true);
     context.QuadraticBezierTo(new Point(th.Left, th.Top), new Point(th.Left + r.TopLeftTop, th.Top), false, false);
     context.LineTo(new Point(w - th.Right - r.TopRightTop, th.Top), false, false);
     context.QuadraticBezierTo(new Point(w - th.Right, th.Top), new Point(w - th.Right, th.Top + r.TopRightRight), false, false);
     context.LineTo(new Point(w - th.Right, h - th.Bottom - r.BottomRightRight), false, false);
     context.QuadraticBezierTo(new Point(w - th.Right, h - th.Bottom), new Point(w - th.Right - r.BottomRightBottom, h - th.Bottom), false, false);
     context.LineTo(new Point(th.Left + r.BottomLeftBottom, h - th.Bottom), false, false);
     context.QuadraticBezierTo(new Point(th.Left, h - th.Bottom), new Point(th.Left, h - th.Bottom - r.BottomLeftLeft), false, false);
 }
Exemplo n.º 2
0
        private void drawLine(Point p1, Point p2)
        {
            List <Point> controlPoints = BezierHelper.getControlPoints(0.4, p1, p2);

            if (isHaveLastControlPoint == false)
            {
                lastControlPoint       = p1;
                isHaveLastControlPoint = true;
            }
            //先画bezier,再画直线
            //                        QuadraticBezierSegment bezierSegment = new QuadraticBezierSegment(p1, controlPoints[0],  true);
            //
            //                        lastControlPoint = controlPoints[1];
            //                        pathFigure.Segments.Add(bezierSegment);
            //            LineSegment lineSegment = new LineSegment(controlPoints[1], true);
            //            pathFigure.Segments.Add(lineSegment);



            StreamGeometry streamGeometry = new StreamGeometry();

            using (StreamGeometryContext ctx = streamGeometry.Open())
            {
                ctx.BeginFigure(lastControlPoint, false, false);
                ctx.QuadraticBezierTo(p1, controlPoints[0], true, true);
                ctx.LineTo(controlPoints[1], true, true);
            }

            lastControlPoint = controlPoints[1];
            geometryGroup.Children.Add(streamGeometry);
        }
Exemplo n.º 3
0
        public override void Draw(DrawingContext ctx, Matrix matrix)
        {
            if (!this.IsValid)
            {
                return;
            }
            PathFigureEditor      pathFigureEditor      = new PathFigureEditor(this.PathGeometry.Figures[this.FigureIndex]);
            StreamGeometry        streamGeometry        = new StreamGeometry();
            StreamGeometryContext streamGeometryContext = streamGeometry.Open();

            switch (this.PathPointKind)
            {
            case PathPointKind.Start:
                throw new InvalidOperationException(ExceptionStringTable.PathSegmentAdornerIsolatedPointSegment);

            case PathPointKind.Arc:
                ArcSegment arcSegment = (ArcSegment)pathFigureEditor.PathFigure.Segments[this.SegmentIndex];
                streamGeometryContext.BeginFigure(this.GetPoint(pathFigureEditor, -1), false, false);
                streamGeometryContext.ArcTo(arcSegment.Point, arcSegment.Size, arcSegment.RotationAngle, arcSegment.IsLargeArc, arcSegment.SweepDirection, true, false);
                break;

            case PathPointKind.Line:
                streamGeometryContext.BeginFigure(this.GetPoint(pathFigureEditor, -1), false, false);
                streamGeometryContext.LineTo(this.GetPoint(pathFigureEditor, 0), true, false);
                break;

            case PathPointKind.Quadratic:
                streamGeometryContext.BeginFigure(this.GetPoint(pathFigureEditor, -2), false, false);
                streamGeometryContext.QuadraticBezierTo(this.GetPoint(pathFigureEditor, -1), this.GetPoint(pathFigureEditor, 0), true, false);
                break;

            case PathPointKind.Cubic:
                streamGeometryContext.BeginFigure(this.GetPoint(pathFigureEditor, -3), false, false);
                streamGeometryContext.BezierTo(this.GetPoint(pathFigureEditor, -2), this.GetPoint(pathFigureEditor, -1), this.GetPoint(pathFigureEditor, 0), true, false);
                break;

            case PathPointKind.BezierHandle:
                throw new InvalidOperationException(ExceptionStringTable.PathSegmentAdornerLastPointIsBezier);

            default:
                throw new NotImplementedException(ExceptionStringTable.PathSegmentAdornerUnknownPathPoint);
            }
            streamGeometryContext.Close();
            MatrixTransform matrixTransform = new MatrixTransform(matrix);

            matrixTransform.Freeze();
            streamGeometry.Transform = (Transform)matrixTransform;
            streamGeometry.Freeze();
            Pen pen = this.IsActive ? this.ThickPathSegmentPen : this.ThinPathSegmentPen;

            ctx.DrawGeometry((Brush)null, pen, (System.Windows.Media.Geometry)streamGeometry);
            ctx.DrawGeometry((Brush)null, PathSegmentAdorner.HitTestPen, (System.Windows.Media.Geometry)streamGeometry);
        }
Exemplo n.º 4
0
        private static void DeserializeQuadraticBezierTo(BinaryReader br, byte firstByte, StreamGeometryContext sc)
        {
            Point point1;
            Point point2 = new Point();
            bool  isStroked;
            bool  isSmoothJoin;

            DeserializePointAndTwoBools(br, firstByte, out point1, out isStroked, out isSmoothJoin);

            point2.X = XamlSerializationHelper.ReadDouble(br);
            point2.Y = XamlSerializationHelper.ReadDouble(br);

            sc.QuadraticBezierTo(point1, point2, isStroked, isSmoothJoin);
        }
Exemplo n.º 5
0
        public static void DrawFigure(this StreamGeometryContext ctx, PathFigure figure)
        {
            ctx.BeginFigure(figure.StartPoint, figure.IsFilled, figure.IsClosed);
            foreach (var segment in figure.Segments)
            {
                if (segment is LineSegment lineSegment)
                {
                    ctx.LineTo(lineSegment.Point, lineSegment.IsStroked, lineSegment.IsSmoothJoin);
                    continue;
                }

                if (segment is BezierSegment bezierSegment)
                {
                    ctx.BezierTo(bezierSegment.Point1, bezierSegment.Point2, bezierSegment.Point3, bezierSegment.IsStroked, bezierSegment.IsSmoothJoin);
                    continue;
                }

                if (segment is QuadraticBezierSegment quadraticSegment)
                {
                    ctx.QuadraticBezierTo(quadraticSegment.Point1, quadraticSegment.Point2, quadraticSegment.IsStroked, quadraticSegment.IsSmoothJoin);
                    continue;
                }

                if (segment is PolyLineSegment polyLineSegment)
                {
                    ctx.PolyLineTo(polyLineSegment.Points, polyLineSegment.IsStroked, polyLineSegment.IsSmoothJoin);
                    continue;
                }

                if (segment is PolyBezierSegment polyBezierSegment)
                {
                    ctx.PolyBezierTo(polyBezierSegment.Points, polyBezierSegment.IsStroked, polyBezierSegment.IsSmoothJoin);
                    continue;
                }

                if (segment is PolyQuadraticBezierSegment polyQuadraticSegment)
                {
                    ctx.PolyQuadraticBezierTo(polyQuadraticSegment.Points, polyQuadraticSegment.IsStroked, polyQuadraticSegment.IsSmoothJoin);
                    continue;
                }

                if (segment is ArcSegment arcSegment)
                {
                    ctx.ArcTo(arcSegment.Point, arcSegment.Size, arcSegment.RotationAngle, arcSegment.IsLargeArc, arcSegment.SweepDirection, arcSegment.IsStroked, arcSegment.IsSmoothJoin);
                    continue;
                }
            }
        }
Exemplo n.º 6
0
        public static void DrawFigure(StreamGeometryContext ctx, PathFigure figure)
        {
            ctx.BeginFigure(figure.StartPoint, figure.IsFilled, figure.IsClosed);
            foreach (var segment in figure.Segments)
            {
                var lineSegment = segment as WpfLineSegment;
                if (lineSegment != null)
                {
                    ctx.LineTo(lineSegment.Point, lineSegment.IsStroked, lineSegment.IsSmoothJoin); continue;
                }

                var bezierSegment = segment as BezierSegment;
                if (bezierSegment != null)
                {
                    ctx.BezierTo(bezierSegment.Point1, bezierSegment.Point2, bezierSegment.Point3, bezierSegment.IsStroked, bezierSegment.IsSmoothJoin); continue;
                }

                var quadraticSegment = segment as QuadraticBezierSegment;
                if (quadraticSegment != null)
                {
                    ctx.QuadraticBezierTo(quadraticSegment.Point1, quadraticSegment.Point2, quadraticSegment.IsStroked, quadraticSegment.IsSmoothJoin); continue;
                }

                var polyLineSegment = segment as PolyLineSegment;
                if (polyLineSegment != null)
                {
                    ctx.PolyLineTo(polyLineSegment.Points, polyLineSegment.IsStroked, polyLineSegment.IsSmoothJoin); continue;
                }

                var polyBezierSegment = segment as PolyBezierSegment;
                if (polyBezierSegment != null)
                {
                    ctx.PolyBezierTo(polyBezierSegment.Points, polyBezierSegment.IsStroked, polyBezierSegment.IsSmoothJoin); continue;
                }

                var polyQuadraticSegment = segment as PolyQuadraticBezierSegment;
                if (polyQuadraticSegment != null)
                {
                    ctx.PolyQuadraticBezierTo(polyQuadraticSegment.Points, polyQuadraticSegment.IsStroked, polyQuadraticSegment.IsSmoothJoin); continue;
                }

                var arcSegment = segment as ArcSegment;
                if (arcSegment != null)
                {
                    ctx.ArcTo(arcSegment.Point, arcSegment.Size, arcSegment.RotationAngle, arcSegment.IsLargeArc, arcSegment.SweepDirection, arcSegment.IsStroked, arcSegment.IsSmoothJoin); continue;
                }
            }
        }
        /// <summary>
        /// Generates a WPF geometry for a single graphic path geometry
        /// </summary>
        public static Geometry GenerateGeometry(GraphicPathGeometry graphicPathGeometry)
        {
            if (graphicPathGeometry == null)
            {
                return(null);
            }

            StreamGeometry geometry = new StreamGeometry();

            geometry.FillRule = ConvertFillRule(graphicPathGeometry.FillRule);
            StreamGeometryContext ctx = geometry.Open();

            foreach (var segment in graphicPathGeometry.Segments)
            {
                switch (segment)
                {
                case GraphicMoveSegment graphicMove:
                {
                    ctx.BeginFigure(graphicMove.StartPoint, true, graphicMove.IsClosed);
                    break;
                }

                case GraphicLineSegment graphicLineTo:
                {
                    ctx.LineTo(graphicLineTo.To, true, true);
                    break;
                }

                case GraphicCubicBezierSegment graphicCubicBezier:
                {
                    ctx.BezierTo(graphicCubicBezier.ControlPoint1, graphicCubicBezier.ControlPoint2, graphicCubicBezier.EndPoint, true, true);
                    break;
                }

                case GraphicQuadraticBezierSegment graphicQuadraticBezier:
                {
                    ctx.QuadraticBezierTo(graphicQuadraticBezier.ControlPoint, graphicQuadraticBezier.EndPoint, true, true);
                    break;
                }
                }
            }

            ctx.Close();
            geometry.Freeze();

            return(geometry);
        }
Exemplo n.º 8
0
        private void internalGeometryDraw(StreamGeometryContext context)
        {
            makeOrthogonalPoints();

            context.BeginFigure(StartPoint, true, false);


            foreach (FromTo fromTo in PointList)
            {
                if (fromTo.Intersection.Count > 0)
                {
                    foreach (IntersectPoint interPoint in fromTo.Intersection)
                    {
                        switch (fromTo.DirSort)
                        {
                        case DirSort.X_ASC:
                            if (interPoint.Mode == IntersectPointMode.NormalIntersect)
                            {
                                context.LineTo(new Point(interPoint.Point.X - offset, interPoint.Point.Y), true, true);
                                context.QuadraticBezierTo(new Point(interPoint.Point.X, interPoint.Point.Y - offset), new Point(interPoint.Point.X + offset, interPoint.Point.Y), true, true);
                            }
                            else if (interPoint.Mode == IntersectPointMode.InnerIntersect)
                            {
                                context.LineTo(new Point(interPoint.Point.X - 2.5, interPoint.Point.Y), true, true);
                                context.QuadraticBezierTo(new Point(interPoint.Point.X, interPoint.Point.Y - 3.5), new Point(interPoint.Point.X + 2.5, interPoint.Point.Y), true, true);
                                context.QuadraticBezierTo(new Point(interPoint.Point.X, interPoint.Point.Y + 3.5), new Point(interPoint.Point.X - 2.5, interPoint.Point.Y), true, true);
                            }
                            break;

                        case DirSort.X_DESC:
                            if (interPoint.Mode == IntersectPointMode.NormalIntersect)
                            {
                                context.LineTo(new Point(interPoint.Point.X + offset, interPoint.Point.Y), true, true);
                                context.QuadraticBezierTo(new Point(interPoint.Point.X, interPoint.Point.Y - offset), new Point(interPoint.Point.X - offset, interPoint.Point.Y), true, true);
                            }
                            else if (interPoint.Mode == IntersectPointMode.InnerIntersect)
                            {
                                context.LineTo(new Point(interPoint.Point.X + 2.5, interPoint.Point.Y), true, true);
                                context.QuadraticBezierTo(new Point(interPoint.Point.X, interPoint.Point.Y - 3.5), new Point(interPoint.Point.X - 2.5, interPoint.Point.Y), true, true);
                                context.QuadraticBezierTo(new Point(interPoint.Point.X, interPoint.Point.Y + 3.5), new Point(interPoint.Point.X + 2.5, interPoint.Point.Y), true, true);
                            }
                            break;
                        }
                    }
                    context.LineTo(fromTo.To, true, true);
                }
                else
                {
                    context.LineTo(fromTo.To, true, true);
                }
            }
        }
Exemplo n.º 9
0
        public void DrawBezier(Point[] pts, DrawingContext dc, Brush brush)
        {
            foreach (Point pt in pts)//绘制点
            {
                dc.DrawEllipse(brush, null, pt, 5, 5);
            }
            StreamGeometry        sg  = new StreamGeometry();
            StreamGeometryContext sgc = sg.Open();

            sgc.BeginFigure(pts[0], true, false);
            sgc.QuadraticBezierTo(pts[1], pts[2], true, true);
            sgc.BeginFigure(pts[0], true, false);
            sgc.PolyLineTo(pts, true, true);
            sgc.Close();
            Pen pen = new Pen(brush, 1);

            dc.DrawGeometry(null, pen, sg);
        }
        public StreamGeometryQuadraticBezierToExample()
        {
            // Create a path to draw a geometry with.
            Path myPath = new Path();

            myPath.Stroke          = Brushes.Black;
            myPath.StrokeThickness = 1;

            // Create a StreamGeometry to use to specify myPath.
            StreamGeometry geometry = new StreamGeometry();

            // Open a StreamGeometryContext that can be used to describe this StreamGeometry
            // object's contents.
            using (StreamGeometryContext ctx = geometry.Open())
            {
                // Set the begin point of the shape.
                ctx.BeginFigure(new Point(10, 100), true /* is filled */, false /* is closed */);

                // Create a Quadratic Bezier curve using the 2 specifed points. The first point
                // specifies the control point while the second point specifies the end point
                // of the curve.
                ctx.QuadraticBezierTo(new Point(100, 0), new Point(200, 200), true /* is stroked */,
                                      false /* is smooth join */);
            }

            // Freeze the geometry (make it unmodifiable)
            // for additional performance benefits.
            geometry.Freeze();

            // specify the shape (quadratic Bezier curve) of the path using the StreamGeometry.
            myPath.Data = geometry;

            // Add path shape to the UI.
            StackPanel mainPanel = new StackPanel();

            mainPanel.Children.Add(myPath);
            this.Content = mainPanel;
        }
Exemplo n.º 11
0
        public static void Write(this StreamGeometryContext ctx, PathSegment pathSegment)
        {
            switch (pathSegment)
            {
            case ArcSegment arc:
                ctx.ArcTo(arc.Point, arc.Size, arc.RotationAngle, arc.IsLargeArc, arc.SweepDirection, false, false);
                break;

            case BezierSegment bezier:
                ctx.BezierTo(bezier.Point1, bezier.Point2, bezier.Point3, false, false);
                break;

            case PolyBezierSegment polyBezier:
                ctx.PolyBezierTo(polyBezier.Points, false, false);
                break;

            case PolyLineSegment polyLine:
                ctx.PolyLineTo(polyLine.Points, false, false);
                break;

            case QuadraticBezierSegment quadraticBezier:
                ctx.QuadraticBezierTo(quadraticBezier.Point1, quadraticBezier.Point2, false, false);
                break;

            case PolyQuadraticBezierSegment polyQuadraticBezier:
                ctx.PolyQuadraticBezierTo(polyQuadraticBezier.Points, false, false);
                break;

            case LineSegment line:
                ctx.LineTo(line.Point, false, false);
                break;

            default:
                break;
            }
        }
Exemplo n.º 12
0
 protected internal override void ApplyTo(StreamGeometryContext ctx)
 {
     ctx.QuadraticBezierTo(Point1, Point2);
 }
Exemplo n.º 13
0
 public override void Curve3(float x1, float y1, float x2, float y2) =>
 _context.QuadraticBezierTo(new Point(x1, y1), new Point(x2, y2));
Exemplo n.º 14
0
        private static void DeserializeQuadraticBezierTo(BinaryReader br, byte firstByte, StreamGeometryContext sc)
        {
            Point point1;
            Point point2 = new Point();
            bool isStroked;
            bool isSmoothJoin;

            DeserializePointAndTwoBools(br, firstByte, out point1, out isStroked, out isSmoothJoin);

            point2.X = XamlSerializationHelper.ReadDouble(br);
            point2.Y = XamlSerializationHelper.ReadDouble(br);

            sc.QuadraticBezierTo(point1, point2, isStroked, isSmoothJoin);
        }
Exemplo n.º 15
0
 protected internal override void ApplyTo(StreamGeometryContext ctx)
 {
     ctx.QuadraticBezierTo(Point1, Point2);
 }
Exemplo n.º 16
0
        /// <summary>
        /// Parse a PathFigureCollection string
        /// </summary>
        internal void ParseToGeometryContext(
            StreamGeometryContext context,
            string pathString,
            int startIndex)
        {
            // [BreakingChange] Dev10 Bug #453199
            // We really should throw an ArgumentNullException here for context and pathString.

            // From original code
            // This is only used in call to Double.Parse
            _formatProvider = System.Globalization.CultureInfo.InvariantCulture;

            _context    = context;
            _pathString = pathString;
            _pathLength = pathString.Length;
            _curIndex   = startIndex;

            _secondLastPoint = new Point(0, 0);
            _lastPoint       = new Point(0, 0);
            _lastStart       = new Point(0, 0);

            _figureStarted = false;

            bool first = true;

            char last_cmd = ' ';

            while (ReadToken()) // Empty path is allowed in XAML
            {
                char cmd = _token;

                if (first)
                {
                    if ((cmd != 'M') && (cmd != 'm'))  // Path starts with M|m
                    {
                        ThrowBadToken();
                    }

                    first = false;
                }

                switch (cmd)
                {
                case 'm':
                case 'M':
                    // XAML allows multiple points after M/m
                    _lastPoint = ReadPoint(cmd, !AllowComma);

                    context.BeginFigure(_lastPoint, IsFilled, !IsClosed);
                    _figureStarted = true;
                    _lastStart     = _lastPoint;
                    last_cmd       = 'M';

                    while (IsNumber(AllowComma))
                    {
                        _lastPoint = ReadPoint(cmd, !AllowComma);

                        context.LineTo(_lastPoint, IsStroked, !IsSmoothJoin);
                        last_cmd = 'L';
                    }
                    break;

                case 'l':
                case 'L':
                case 'h':
                case 'H':
                case 'v':
                case 'V':
                    EnsureFigure();

                    do
                    {
                        switch (cmd)
                        {
                        case 'l': _lastPoint = ReadPoint(cmd, !AllowComma); break;

                        case 'L': _lastPoint = ReadPoint(cmd, !AllowComma); break;

                        case 'h': _lastPoint.X += ReadNumber(!AllowComma); break;

                        case 'H': _lastPoint.X = ReadNumber(!AllowComma); break;

                        case 'v': _lastPoint.Y += ReadNumber(!AllowComma); break;

                        case 'V': _lastPoint.Y = ReadNumber(!AllowComma); break;
                        }

                        context.LineTo(_lastPoint, IsStroked, !IsSmoothJoin);
                    }while (IsNumber(AllowComma));

                    last_cmd = 'L';
                    break;

                case 'c':
                case 'C':           // cubic Bezier
                case 's':
                case 'S':           // smooth cublic Bezier
                    EnsureFigure();

                    do
                    {
                        Point p;

                        if ((cmd == 's') || (cmd == 'S'))
                        {
                            if (last_cmd == 'C')
                            {
                                p = Reflect();
                            }
                            else
                            {
                                p = _lastPoint;
                            }

                            _secondLastPoint = ReadPoint(cmd, !AllowComma);
                        }
                        else
                        {
                            p = ReadPoint(cmd, !AllowComma);

                            _secondLastPoint = ReadPoint(cmd, AllowComma);
                        }

                        _lastPoint = ReadPoint(cmd, AllowComma);

                        context.BezierTo(p, _secondLastPoint, _lastPoint, IsStroked, !IsSmoothJoin);

                        last_cmd = 'C';
                    }while (IsNumber(AllowComma));

                    break;

                case 'q':
                case 'Q':           // quadratic Bezier
                case 't':
                case 'T':           // smooth quadratic Bezier
                    EnsureFigure();

                    do
                    {
                        if ((cmd == 't') || (cmd == 'T'))
                        {
                            if (last_cmd == 'Q')
                            {
                                _secondLastPoint = Reflect();
                            }
                            else
                            {
                                _secondLastPoint = _lastPoint;
                            }

                            _lastPoint = ReadPoint(cmd, !AllowComma);
                        }
                        else
                        {
                            _secondLastPoint = ReadPoint(cmd, !AllowComma);
                            _lastPoint       = ReadPoint(cmd, AllowComma);
                        }

                        context.QuadraticBezierTo(_secondLastPoint, _lastPoint, IsStroked, !IsSmoothJoin);

                        last_cmd = 'Q';
                    }while (IsNumber(AllowComma));

                    break;

                case 'a':
                case 'A':
                    EnsureFigure();

                    do
                    {
                        // A 3,4 5, 0, 0, 6,7
                        double w        = ReadNumber(!AllowComma);
                        double h        = ReadNumber(AllowComma);
                        double rotation = ReadNumber(AllowComma);
                        bool   large    = ReadBool();
                        bool   sweep    = ReadBool();

                        _lastPoint = ReadPoint(cmd, AllowComma);

                        context.ArcTo(
                            _lastPoint,
                            new Size(w, h),
                            rotation,
                            large,
#if PBTCOMPILER
                            sweep,
#else
                            sweep ? SweepDirection.Clockwise : SweepDirection.Counterclockwise,
#endif
                            IsStroked,
                            !IsSmoothJoin
                            );
                    }while (IsNumber(AllowComma));

                    last_cmd = 'A';
                    break;

                case 'z':
                case 'Z':
                    EnsureFigure();
                    context.SetClosedState(IsClosed);

                    _figureStarted = false;
                    last_cmd       = 'Z';

                    _lastPoint = _lastStart; // Set reference point to be first point of current figure
                    break;

                default:
                    ThrowBadToken();
                    break;
                }
            }
        }
Exemplo n.º 17
0
        /// <summary>
        /// Parse a PathFigureCollection string
        /// </summary>
        internal void ParseToGeometryContext(StreamGeometryContext context, string pathString, int startIndex)
        {
            // [BreakingChange] Dev10 Bug #453199
            // We really should throw an ArgumentNullException here for context and pathString.

            // From original code
            // This is only used in call to Double.Parse
            this.formatProvider = CultureInfo.InvariantCulture;

            this.context    = context;
            this.pathString = pathString;
            this.pathLength = pathString.Length;
            this.curIndex   = startIndex;

            this.secondLastPoint = new Point(0, 0);
            this.lastPoint       = new Point(0, 0);
            this.lastStart       = new Point(0, 0);

            this.figureStarted = false;

            bool first = true;

            char lastCmd = ' ';

            while (this.ReadToken()) // Empty path is allowed in XAML
            {
                char cmd = this.token;

                if (first)
                {
                    if ((cmd != 'M') && (cmd != 'm')) // Path starts with M|m
                    {
                        this.ThrowBadToken();
                    }

                    first = false;
                }

                switch (cmd)
                {
                case 'm':
                case 'M':
                    // XAML allows multiple points after M/m
                    this.lastPoint = this.ReadPoint(cmd, !this.allowComma);

                    context.BeginFigure(this.lastPoint, this.isFilled, !this.isClosed);
                    this.figureStarted = true;
                    this.lastStart     = this.lastPoint;
                    lastCmd            = 'M';

                    while (this.IsNumber(this.allowComma))
                    {
                        this.lastPoint = this.ReadPoint(cmd, !this.allowComma);

                        context.LineTo(this.lastPoint, this.isStroked, !this.isSmoothJoin);
                        lastCmd = 'L';
                    }
                    break;

                case 'l':
                case 'L':
                case 'h':
                case 'H':
                case 'v':
                case 'V':
                    this.EnsureFigure();

                    do
                    {
                        switch (cmd)
                        {
                        case 'l':
                            this.lastPoint = this.ReadPoint(cmd, !this.allowComma);
                            break;

                        case 'L':
                            this.lastPoint = this.ReadPoint(cmd, !this.allowComma);
                            break;

                        case 'h':
                            this.lastPoint.X += this.ReadNumber(!this.allowComma);
                            break;

                        case 'H':
                            this.lastPoint.X = this.ReadNumber(!this.allowComma);
                            break;

                        case 'v':
                            this.lastPoint.Y += this.ReadNumber(!this.allowComma);
                            break;

                        case 'V':
                            this.lastPoint.Y = this.ReadNumber(!this.allowComma);
                            break;
                        }

                        context.LineTo(this.lastPoint, this.isStroked, !this.isSmoothJoin);
                    }while (this.IsNumber(this.allowComma));

                    lastCmd = 'L';
                    break;

                case 'c':
                case 'C': // cubic Bezier
                case 's':
                case 'S': // smooth cublic Bezier
                    this.EnsureFigure();

                    do
                    {
                        Point p;

                        if ((cmd == 's') || (cmd == 'S'))
                        {
                            if (lastCmd == 'C')
                            {
                                p = this.Reflect();
                            }
                            else
                            {
                                p = this.lastPoint;
                            }

                            this.secondLastPoint = this.ReadPoint(cmd, !this.allowComma);
                        }
                        else
                        {
                            p = this.ReadPoint(cmd, !this.allowComma);

                            this.secondLastPoint = this.ReadPoint(cmd, this.allowComma);
                        }

                        this.lastPoint = this.ReadPoint(cmd, this.allowComma);

                        context.BezierTo(p, this.secondLastPoint, this.lastPoint, this.isStroked, !this.isSmoothJoin);

                        lastCmd = 'C';
                    }while (this.IsNumber(this.allowComma));

                    break;

                case 'q':
                case 'Q': // quadratic Bezier
                case 't':
                case 'T': // smooth quadratic Bezier
                    this.EnsureFigure();

                    do
                    {
                        if ((cmd == 't') || (cmd == 'T'))
                        {
                            if (lastCmd == 'Q')
                            {
                                this.secondLastPoint = this.Reflect();
                            }
                            else
                            {
                                this.secondLastPoint = this.lastPoint;
                            }

                            this.lastPoint = this.ReadPoint(cmd, !this.allowComma);
                        }
                        else
                        {
                            this.secondLastPoint = this.ReadPoint(cmd, !this.allowComma);
                            this.lastPoint       = this.ReadPoint(cmd, this.allowComma);
                        }

                        context.QuadraticBezierTo(this.secondLastPoint, this.lastPoint, this.isStroked, !this.isSmoothJoin);

                        lastCmd = 'Q';
                    }while (this.IsNumber(this.allowComma));

                    break;

                case 'a':
                case 'A':
                    this.EnsureFigure();

                    do
                    {
                        // A 3,4 5, 0, 0, 6,7
                        double w        = this.ReadNumber(!this.allowComma);
                        double h        = this.ReadNumber(this.allowComma);
                        double rotation = this.ReadNumber(this.allowComma);
                        bool   large    = this.ReadBool();
                        bool   sweep    = this.ReadBool();

                        this.lastPoint = this.ReadPoint(cmd, this.allowComma);

                        context.ArcTo(
                            this.lastPoint,
                            new Size(w, h),
                            rotation,
                            large,
#if PBTCOMPILER
                            sweep,
#else
                            sweep ? SweepDirection.Clockwise : SweepDirection.Counterclockwise,
#endif
                            this.isStroked,
                            !this.isSmoothJoin);
                    }while (this.IsNumber(this.allowComma));

                    lastCmd = 'A';
                    break;

                case 'z':
                case 'Z':
                    this.EnsureFigure();
                    //context.SetClosedState(isClosed);
                    var mi = context.GetType().GetMethod("SetClosedState", BindingFlags.Instance | BindingFlags.NonPublic);
                    if (mi != null)
                    {
                        var pia = mi.GetParameters();
                        if (pia != null && pia.Length == 1 && pia[0].ParameterType == typeof(bool))
                        {
                            mi.Invoke(context, new object[] { this.isClosed });
                        }
                    }

                    this.figureStarted = false;
                    lastCmd            = 'Z';

                    this.lastPoint = this.lastStart; // Set reference point to be first point of current figure
                    break;

                default:
                    this.ThrowBadToken();
                    break;
                }
            }
        }
Exemplo n.º 18
0
        private static void Draw(StreamGeometryContext dc, IPathCommand element, IPathCommand previous, Vector startOffset)
        {
            if (element is LineTo)
            {
                dc.LineTo(new System.Windows.Point((element as LineTo).X + startOffset.X, (element as LineTo).Y + startOffset.Y), true, true);
            }
            else if (element is MoveTo)
            {
                dc.LineTo(new System.Windows.Point((element as MoveTo).X + startOffset.X, (element as MoveTo).Y + startOffset.Y), false, true);
            }
            else if (element is CurveTo)
            {
                Point controlStart = Point.Add((element as CurveTo).ControlStart.ToWinPoint(), startOffset);
                Point controlEnd   = Point.Add((element as CurveTo).ControlEnd.ToWinPoint(), startOffset);
                Point end          = Point.Add((element as CurveTo).End.ToWinPoint(), startOffset);
                dc.BezierTo(controlStart, controlEnd, end, true, true);
            }
            else if (element is EllipticalArcTo)
            {
                dc.ArcTo(Point.Add((element as EllipticalArcTo).End.ToWinPoint(), startOffset), (element as EllipticalArcTo).Size.ToWinSize(), (element as EllipticalArcTo).RotationAngle, (element as EllipticalArcTo).IsLargeArc, (element as EllipticalArcTo).SweepDirection == Path.SweepDirection.Clockwise ? System.Windows.Media.SweepDirection.Clockwise : System.Windows.Media.SweepDirection.Counterclockwise, true, true);
            }
            else if (element is SmoothCurveTo)
            {
                SmoothCurveTo item = element as SmoothCurveTo;

                // If previous command is not S or C, then control points are the same
                Point controlStart = item.ControlEnd.ToWinPoint();

                // Else reflect ControlEnd of previous command in StartPoint
                if (previous is SmoothCurveTo)
                {
                    controlStart = ReflectPointIn((previous as SmoothCurveTo).ControlEnd.ToWinPoint(), (previous as SmoothCurveTo).End.ToWinPoint());
                }
                else if (previous is CurveTo)
                {
                    controlStart = ReflectPointIn((previous as CurveTo).ControlEnd.ToWinPoint(), (previous as CurveTo).End.ToWinPoint());
                }

                dc.BezierTo(Point.Add(controlStart, startOffset), Point.Add(item.ControlEnd.ToWinPoint(), startOffset), Point.Add(item.End.ToWinPoint(), startOffset), true, true);
            }
            else if (element is SmoothQuadraticBeizerCurveTo)
            {
                SmoothQuadraticBeizerCurveTo item = element as SmoothQuadraticBeizerCurveTo;

                if (previous is SmoothQuadraticBeizerCurveTo)
                {
                    throw new NotSupportedException();
                }
                else if (previous is QuadraticBeizerCurveTo)
                {
                    throw new NotSupportedException();
                }
                else
                {
                    // If previous command is not Q or T, then draw a line
                    dc.LineTo(item.End.ToWinPoint(), true, true);
                }
            }
            else if (element is QuadraticBeizerCurveTo)
            {
                QuadraticBeizerCurveTo item = element as QuadraticBeizerCurveTo;
                dc.QuadraticBezierTo(Point.Add(item.Control.ToWinPoint(), startOffset), Point.Add(item.End.ToWinPoint(), startOffset), true, true);
            }
        }