Example #1
0
        public static AppKit.NSImage GetNSImage(this IImage image)
        {
            var c = (CGImageImage)image;

            return(new AppKit.NSImage(c.Image, Conversions.GetCGSize(c.Size)));
        }
        public void DrawPath(IEnumerable <PathOp> ops, Pen pen = null, Brush brush = null)
        {
            if (pen == null && brush == null)
            {
                return;
            }

            using (var path = new global::Android.Graphics.Path()) {
                var bb = new BoundingBoxBuilder();

                Point?prevPoint = null;

                foreach (var op in ops)
                {
                    var mt = op as MoveTo;
                    if (mt != null)
                    {
                        var p = mt.Point;
                        path.MoveTo((float)p.X, (float)p.Y);
                        bb.Add(p);
                        prevPoint = p;
                        continue;
                    }
                    var lt = op as LineTo;
                    if (lt != null)
                    {
                        var p = lt.Point;
                        path.LineTo((float)p.X, (float)p.Y);
                        bb.Add(p);
                        prevPoint = p;
                        continue;
                    }
                    var at = op as ArcTo;
                    if (at != null)
                    {
                        var p = at.Point;

                        if (!prevPoint.HasValue)
                        {
                            throw new NotSupportedException("Cannot begin path with Arc.");
                        }

                        var pp = prevPoint.Value;

                        Point c1, c2;
                        at.GetCircles(pp, out c1, out c2);

                        var circleCenter = at.LargeArc ^ !at.SweepClockwise ? c2 : c1;
                        var rect         = new Rect(circleCenter - at.Radius, at.Radius * 2);

                        var startAngle = Conversions.RadToDeg((float)Math.Atan2(pp.Y - circleCenter.Y, pp.X - circleCenter.X));
                        var endAngle   = Conversions.RadToDeg((float)Math.Atan2(p.Y - circleCenter.Y, p.X - circleCenter.X));

                        var sweepAngle = endAngle - startAngle;

                        if (at.SweepClockwise && sweepAngle < 0)
                        {
                            // If we want to go CW, sweepAngle needs to be positive
                            sweepAngle += 360.0f;
                        }
                        else if (!at.SweepClockwise && sweepAngle > 0)
                        {
                            // If we want to go CCW, sweepAngle needs to be negative
                            sweepAngle -= 360.0f;
                        }

                        path.AddArc(Conversions.GetRectF(rect), startAngle, sweepAngle);

                        bb.Add(p);
                        prevPoint = p;
                        continue;
                    }
                    var ct = op as CurveTo;
                    if (ct != null)
                    {
                        var p  = ct.Point;
                        var c1 = ct.Control1;
                        var c2 = ct.Control2;
                        path.CubicTo((float)c1.X, (float)c1.Y, (float)c2.X, (float)c2.Y, (float)p.X, (float)p.Y);
                        bb.Add(p);
                        bb.Add(c1);
                        bb.Add(c2);
                        prevPoint = p;
                        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 paint = GetBrushPaint(brush, frame);
                    graphics.DrawPath(path, paint);
                }
                if (pen != null)
                {
                    var paint = GetPenPaint(pen);
                    graphics.DrawPath(path, paint);
                }
            }
        }
Example #3
0
        public void DrawPath(IEnumerable <PathOp> ops, Pen pen = null, Brush brush = null)
        {
            if (pen == null && brush == null)
            {
                return;
            }

            DrawElement(() => {
                var bb = new BoundingBoxBuilder();

                foreach (var op in ops)
                {
                    var mt = op as MoveTo;
                    if (mt != null)
                    {
                        var p = mt.Point;
                        if (!IsValid(p.X) || !IsValid(p.Y))
                        {
                            continue;
                        }
                        context.MoveTo((nfloat)p.X, (nfloat)p.Y);
                        bb.Add(p);
                        continue;
                    }
                    var lt = op as LineTo;
                    if (lt != null)
                    {
                        var p = lt.Point;
                        if (!IsValid(p.X) || !IsValid(p.Y))
                        {
                            continue;
                        }
                        context.AddLineToPoint((nfloat)p.X, (nfloat)p.Y);
                        bb.Add(p);
                        continue;
                    }
                    var at = op as ArcTo;
                    if (at != null)
                    {
                        var p = at.Point;
                        if (!IsValid(p.X) || !IsValid(p.Y))
                        {
                            continue;
                        }
                        var pp = Conversions.GetPoint(context.GetPathCurrentPoint());
                        if (pp == p)
                        {
                            continue;
                        }
                        Point c1, c2;
                        at.GetCircles(pp, out c1, out c2);

                        var circleCenter = (at.LargeArc ^ at.SweepClockwise) ? c1 : c2;

                        var startAngle = (float)Math.Atan2(pp.Y - circleCenter.Y, pp.X - circleCenter.X);
                        var endAngle   = (float)Math.Atan2(p.Y - circleCenter.Y, p.X - circleCenter.X);

                        if (!IsValid(circleCenter.X) || !IsValid(circleCenter.Y) || !IsValid(startAngle) || !IsValid(endAngle))
                        {
                            context.MoveTo((nfloat)p.X, (nfloat)p.Y);
                            continue;
                        }

                        var clockwise = !at.SweepClockwise;

                        context.AddArc((nfloat)circleCenter.X, (nfloat)circleCenter.Y, (nfloat)at.Radius.Min, startAngle, endAngle, clockwise);

                        bb.Add(p);
                        continue;
                    }
                    var ct = op as CurveTo;
                    if (ct != null)
                    {
                        var p = ct.Point;
                        if (!IsValid(p.X) || !IsValid(p.Y))
                        {
                            continue;
                        }
                        var c1 = ct.Control1;
                        var c2 = ct.Control2;
                        if (!IsValid(c1.X) || !IsValid(c1.Y) || !IsValid(c2.X) || !IsValid(c2.Y))
                        {
                            context.MoveTo((nfloat)p.X, (nfloat)p.Y);
                            continue;
                        }
                        context.AddCurveToPoint((nfloat)c1.X, (nfloat)c1.Y, (nfloat)c2.X, (nfloat)c2.Y, (nfloat)p.X, (nfloat)p.Y);
                        bb.Add(p);
                        bb.Add(c1);
                        bb.Add(c2);
                        continue;
                    }
                    var cp = op as ClosePath;
                    if (cp != null)
                    {
                        context.ClosePath();
                        continue;
                    }

                    throw new NotSupportedException("Path Op " + op);
                }

                return(bb.BoundingBox);
            }, pen, brush);
        }
        public void DrawPath(IEnumerable <PathOp> ops, Pen pen = null, Brush brush = null)
        {
            using (var path = new GraphicsPath()) {
                var bb = new BoundingBoxBuilder();

                var position = Point.Zero;

                foreach (var op in ops)
                {
                    var mt = op as MoveTo;
                    if (mt != null)
                    {
                        var p = mt.Point;
                        position = p;
                        bb.Add(p);
                        continue;
                    }
                    var lt = op as LineTo;
                    if (lt != null)
                    {
                        var p = lt.Point;
                        path.AddLine(Conversions.GetPointF(position), Conversions.GetPointF(p));
                        position = p;
                        bb.Add(p);
                        continue;
                    }
                    var at = op as ArcTo;
                    if (at != null)
                    {
                        var p = at.Point;
                        path.AddLine(Conversions.GetPointF(position), Conversions.GetPointF(p));
                        position = p;
                        bb.Add(p);
                        continue;
                    }
                    var ct = op as CurveTo;
                    if (ct != null)
                    {
                        var p  = ct.Point;
                        var c1 = ct.Control1;
                        var c2 = ct.Control2;
                        path.AddBezier(Conversions.GetPointF(position), Conversions.GetPointF(c1),
                                       Conversions.GetPointF(c2), Conversions.GetPointF(p));
                        position = p;
                        bb.Add(p);
                        bb.Add(c1);
                        bb.Add(c2);
                        continue;
                    }
                    var cp = op as ClosePath;
                    if (cp != null)
                    {
                        path.CloseFigure();
                        continue;
                    }

                    throw new NotSupportedException("Path Op " + op);
                }

                var frame = bb.BoundingBox;
                if (brush != null)
                {
                    graphics.FillPath(brush.GetBrush(frame), path);
                }
                if (pen != null)
                {
                    graphics.DrawPath(pen.GetPen(), path);
                }
            }
        }