Exemple #1
0
        public void DrawText(string text, Rect frame, Font font, TextAlignment alignment = TextAlignment.Left, Pen pen = null, Brush brush = null)
        {
            if (brush == null)
            {
                return;
            }
            var sdfont = new System.Drawing.Font(font.Family, (float)font.Size, FontStyle.Regular, GraphicsUnit.Pixel);
            var sz     = graphics.MeasureString(text, sdfont);
            var point  = frame.Position;
            var fr     = new Rect(point, new Size(sz.Width, sz.Height));

            graphics.DrawString(text, sdfont, Conversions.GetBrush(brush, fr), Conversions.GetPointF(point - new Point(0, sdfont.Height)));
        }
        public void DrawText(string text, Rect frame, Font font, TextAlignment alignment = TextAlignment.Left, Pen pen = null, Brush brush = null)
        {
            var brushToUse = brush ?? Brushes.Black;
            var netFont    = new System.Drawing.Font(font.Family, (float)font.Size, FontStyle.Regular, GraphicsUnit.Pixel);
            var sz         = graphics.MeasureString(text, netFont);
            var asc        = netFont.FontFamily.GetCellAscent(netFont.Style);
            var desc       = netFont.FontFamily.GetCellDescent(netFont.Style);
            var ascale     = sz.Height / (asc + desc);
            var point      = frame.Position;
            var fr         = new Rect(point, new Size(sz.Width, sz.Height));

            graphics.DrawString(text, netFont, Conversions.GetBrush(brushToUse, fr), Conversions.GetPointF(point - new Point(0, sz.Height - desc * ascale)));
        }
Exemple #3
0
        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);
                }
            }
        }
Exemple #4
0
        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;

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

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

                        var startAngle = Conversions.RadToDeg((float)Math.Atan2(position.Y - circleCenter.Y, position.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.GetRectangleF(rect), startAngle, sweepAngle);
                        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);
                }
            }
        }