/// <summary> /// Initializes a new instance of the <see cref="NGraphics.Custom.Models.Elements.Text"/> class. /// </summary> /// <param name="text">Text.</param> /// <param name="frame">Frame.</param> /// <param name="font">Font.</param> /// <param name="alignment">Alignment.</param> /// <param name="pen">Pen.</param> /// <param name="brush">Brush.</param> public Text(string text, Rect frame, Font font, TextAlignment alignment = TextAlignment.Left, Pen pen = null, BaseBrush brush = null) : base(pen, brush) { String = text; Frame = frame; Font = font; Alignment = alignment; }
private void AddElements(IList<IDrawable> list, IEnumerable<XElement> es, Pen inheritPen, BaseBrush inheritBaseBrush) { foreach (var e in es) AddElement(list, e, inheritPen, inheritBaseBrush); }
private void AddBrushPaint(Paint paint, BaseBrush brush, Rect frame) { paint.SetStyle(Paint.Style.Fill); var sb = brush as SolidBrush; if (sb != null) { paint.SetARGB(sb.Color.A, sb.Color.R, sb.Color.G, sb.Color.B); return; } var lgb = brush as LinearGradientBrush; if (lgb != null) { var n = lgb.Stops.Count; if (n >= 2) { var locs = new float[n]; var comps = new int[n]; for (var i = 0; i < n; i++) { var s = lgb.Stops[i]; locs[i] = (float) s.Offset; comps[i] = s.Color.Argb; } var p1 = lgb.Absolute ? lgb.Start : frame.Position + lgb.Start*frame.Size; var p2 = lgb.Absolute ? lgb.End : frame.Position + lgb.End*frame.Size; var lg = new LinearGradient( (float) p1.X, (float) p1.Y, (float) p2.X, (float) p2.Y, comps, locs, Shader.TileMode.Clamp); paint.SetShader(lg); } return; } var rgb = brush as RadialGradientBrush; if (rgb != null) { var n = rgb.Stops.Count; if (n >= 2) { var locs = new float[n]; var comps = new int[n]; for (var i = 0; i < n; i++) { var s = rgb.Stops[i]; locs[i] = (float) s.Offset; comps[i] = s.Color.Argb; } var p1 = rgb.GetAbsoluteCenter(frame); var r = rgb.GetAbsoluteRadius(frame); var rg = new RadialGradient( (float) p1.X, (float) p1.Y, (float) r.Max, comps, locs, Shader.TileMode.Clamp); paint.SetShader(rg); } return; } throw new NotSupportedException("Brush " + brush); }
public void DrawRectangle(Rect frame, Pen pen = null, BaseBrush brush = null) { if (brush != null) { var paint = GetBrushPaint(brush, frame); graphics.DrawRect((float) (frame.X), (float) (frame.Y), (float) (frame.X + frame.Width), (float) (frame.Y + frame.Height), paint); } if (pen != null) { var paint = GetPenPaint(pen); graphics.DrawRect((float) (frame.X), (float) (frame.Y), (float) (frame.X + frame.Width), (float) (frame.Y + frame.Height), paint); } }
public void DrawEllipse(Rect frame, Pen pen = null, BaseBrush brush = null) { if (brush != null) { var paint = GetBrushPaint(brush, frame); graphics.DrawOval(frame.GetRectF(), paint); } if (pen != null) { var paint = GetPenPaint(pen); graphics.DrawOval(frame.GetRectF(), paint); } }
public void DrawText(string text, Rect frame, Font font, TextAlignment alignment = TextAlignment.Left, Pen pen = null, BaseBrush brush = null) { }
public void DrawPath(IEnumerable<PathOperation> ops, Pen pen = null, BaseBrush brush = null) { }
void SetBrush(BaseBrush baseBrush) { var sb = baseBrush as SolidBrush; if (sb != null) { context.SetFillColor ((nfloat)sb.Color.Red, (nfloat)sb.Color.Green, (nfloat)sb.Color.Blue, (nfloat)sb.Color.Alpha); } }
public static void FillRectangle(this ICanvas canvas, double x, double y, double width, double height, BaseBrush brush) { canvas.DrawRectangle(new Rect(x, y, width, height), brush: brush); }
public static void FillPath(this ICanvas canvas, Action<Path> draw, BaseBrush brush) { var p = new Path(null, brush); draw(p); p.Draw(canvas); }
public static void FillPath(this ICanvas canvas, IEnumerable<PathOperation> ops, BaseBrush brush) { canvas.DrawPath(ops, brush: brush); }
public static void FillEllipse(this ICanvas canvas, Rect frame, BaseBrush brush) { canvas.DrawEllipse(frame, brush: brush); }
public static void FillEllipse(this ICanvas canvas, Point position, Size size, BaseBrush brush) { canvas.DrawEllipse(new Rect(position, size), null, brush); }
private void AddElement(IList<IDrawable> list, XElement e, Pen inheritPen, BaseBrush inheritBaseBrush) { Element element = null; var styleAttributedDictionary = e.Attributes().ToDictionary(k => k.Name.LocalName, v => v.Value); var pen = _stylesParser.GetPen(styleAttributedDictionary); var baseBrush = _stylesParser.GetBrush(styleAttributedDictionary,defs, pen); var style = ReadString(e.Attribute("style")); if (!string.IsNullOrWhiteSpace(style)) { ApplyStyle(style, ref pen, ref baseBrush); } pen = pen ?? inheritPen; baseBrush = baseBrush ?? inheritBaseBrush; //var id = ReadString (e.Attribute ("id")); // // Elements // switch (e.Name.LocalName) { case "text": { var x = _valuesParser.ReadNumber(e.Attribute("x")); var y = _valuesParser.ReadNumber(e.Attribute("y")); var text = e.Value.Trim(); var font = new Font(); element = new Text(text, new Rect(new Point(x, y), new Size(double.MaxValue, double.MaxValue)), font, TextAlignment.Left, pen, baseBrush); } break; case "rect": { var x = _valuesParser.ReadNumber(e.Attribute("x")); var y = _valuesParser.ReadNumber(e.Attribute("y")); var width = _valuesParser.ReadNumber(e.Attribute("width")); var height = _valuesParser.ReadNumber(e.Attribute("height")); element = new Rectangle(new Point(x, y), new Size(width, height), pen, baseBrush); } break; case "ellipse": { var cx = _valuesParser.ReadNumber(e.Attribute("cx")); var cy = _valuesParser.ReadNumber(e.Attribute("cy")); var rx = _valuesParser.ReadNumber(e.Attribute("rx")); var ry = _valuesParser.ReadNumber(e.Attribute("ry")); element = new Ellipse(new Point(cx - rx, cy - ry), new Size(2*rx, 2*ry), pen, baseBrush); } break; case "circle": { var cx = _valuesParser.ReadNumber(e.Attribute("cx")); var cy = _valuesParser.ReadNumber(e.Attribute("cy")); var rr = _valuesParser.ReadNumber(e.Attribute("r")); element = new Ellipse(new Point(cx - rr, cy - rr), new Size(2*rr, 2*rr), pen, baseBrush); } break; case "path": { var dA = e.Attribute("d"); if (dA != null && !string.IsNullOrWhiteSpace(dA.Value)) { var p = new Path(pen, baseBrush); SvgPathParser.Parse(p, dA.Value); element = p; } } break; case "g": { var g = new Group(); AddElements(g.Children, e.Elements(), pen, baseBrush); element = g; } break; case "use": { var href = ReadString(e.Attributes().FirstOrDefault(x => x.Name.LocalName == "href")); if (!string.IsNullOrWhiteSpace(href)) { XElement useE; if (defs.TryGetValue(href.Trim().Replace("#", ""), out useE)) { var useList = new List<IDrawable>(); AddElement(useList, useE, pen, baseBrush); element = useList.OfType<Element>().FirstOrDefault(); } } } break; case "title": Graphic.Title = ReadString(e); break; case "description": Graphic.Description = ReadString(e); break; case "defs": // Already read in earlier pass break; case "namedview": case "metadata": case "SVGTestCase": case "switch": break; default: throw new NotSupportedException("SVG element \"" + e.Name.LocalName + "\" is not supported"); } if (element != null) { element.Id = ReadString(e.Attribute("id")); } if (element != null) { element.Transform = ReadTransform(ReadString(e.Attribute("transform"))); list.Add(element); } }
private void ApplyStyle(string style, ref Pen pen, ref BaseBrush baseBrush) { var stylesDictionary = _stylesParser.ParseStyleValues(style); pen = _stylesParser.GetPen(stylesDictionary); baseBrush = _stylesParser.GetBrush(stylesDictionary, defs, pen); }
public void DrawText(string text, Rect frame, Font font, TextAlignment alignment = TextAlignment.Left, Pen pen = null, BaseBrush brush = null) { if (string.IsNullOrEmpty (text)) return; if (font == null) throw new ArgumentNullException ("font"); SetBrush (brush); string fontName = font.Name; Array availableFonts = #if __IOS__ UIKit.UIFont.FontNamesForFamilyName(fontName); #else AppKit.NSFontManager.SharedFontManager.AvailableMembersOfFontFamily (fontName).ToArray (); #endif if (availableFonts != null && availableFonts.Length > 0) context.SelectFont (font.Name, (nfloat)font.Size, CGTextEncoding.MacRoman); else context.SelectFont ("Georgia", (nfloat)font.Size, CGTextEncoding.MacRoman); context.ShowTextAtPoint ((nfloat)frame.X, (nfloat)frame.Y, text); // using (var atext = new NSMutableAttributedString (text)) { // // atext.AddAttributes (new CTStringAttributes { // ForegroundColor = new CGColor (1, 0, 0, 1), // }, new NSRange (0, text.Length)); // // using (var ct = new CTFramesetter (atext)) // using (var path = CGPath.FromRect (Conversions.GetCGRect (frame))) // using (var tframe = ct.GetFrame (new NSRange (0, atext.Length), path, null)) // tframe.Draw (context); // } }
void DrawElement(Func<Rect> add, Pen pen = null, BaseBrush brush = null) { if (pen == null && brush == null) return; var lgb = brush as LinearGradientBrush; if (lgb != null) { var cg = CreateGradient (lgb.Stops); context.SaveState (); var frame = add (); context.Clip (); CGGradientDrawingOptions options = CGGradientDrawingOptions.DrawsBeforeStartLocation | CGGradientDrawingOptions.DrawsAfterEndLocation; var size = frame.Size; var start = Conversions.GetCGPoint (lgb.Absolute ? lgb.Start : frame.Position + lgb.Start * size); var end = Conversions.GetCGPoint (lgb.Absolute ? lgb.End : frame.Position + lgb.End * size); context.DrawLinearGradient (cg, start, end, options); context.RestoreState (); brush = null; } var rgb = brush as RadialGradientBrush; if (rgb != null) { var cg = CreateGradient (rgb.Stops); context.SaveState (); var frame = add (); context.Clip (); CGGradientDrawingOptions options = CGGradientDrawingOptions.DrawsBeforeStartLocation | CGGradientDrawingOptions.DrawsAfterEndLocation; var size = frame.Size; var start = Conversions.GetCGPoint (rgb.GetAbsoluteCenter (frame)); var r = (nfloat)rgb.GetAbsoluteRadius (frame).Max; var end = Conversions.GetCGPoint (rgb.GetAbsoluteFocus (frame)); context.DrawRadialGradient (cg, start, 0, end, r, options); context.RestoreState (); brush = null; } if (pen != null || brush != null) { var mode = SetPenAndBrush (pen, brush); add (); context.DrawPath (mode); } }
public static void FillRectangle(this ICanvas canvas, Rect frame, BaseBrush brush) { canvas.DrawRectangle(frame, brush: brush); }
CGPathDrawingMode SetPenAndBrush(Pen pen, BaseBrush baseBrush) { var mode = CGPathDrawingMode.Fill; var sb = baseBrush as SolidBrush; if (sb != null) { if (sb.FillMode == FillMode.EvenOdd) { mode = CGPathDrawingMode.EOFill; } } if (baseBrush != null) { SetBrush (baseBrush); if (pen != null) { mode = CGPathDrawingMode.FillStroke; if (sb != null) { if (sb.FillMode == FillMode.EvenOdd) { mode = CGPathDrawingMode.EOFillStroke; } } } } if (pen != null) { SetPen (pen); if (baseBrush == null) mode = CGPathDrawingMode.Stroke; } return mode; }
public static void DrawPath(this ICanvas canvas, Action<Path> draw, Pen pen = null, BaseBrush brush = null) { var p = new Path(pen, brush); draw(p); p.Draw(canvas); }
public void DrawRectangle(Rect frame, Pen pen = null, BaseBrush brush = null) { }
public static void DrawRectangle(this ICanvas canvas, double x, double y, double width, double height, Pen pen = null, BaseBrush brush = null) { canvas.DrawRectangle(new Rect(x, y, width, height), pen, brush); }
public void DrawEllipse(Rect frame, Pen pen = null, BaseBrush brush = null) { }
public static void DrawRectangle(this ICanvas canvas, Point position, Size size, Pen pen = null, BaseBrush brush = null) { canvas.DrawRectangle(new Rect(position, size), pen, brush); }
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 baseBrush = null) { if (pen == null && baseBrush == null) return; DrawElement (() => { var bb = new BoundingBoxBuilder(); var lines = new List<CGPoint>(); foreach (var op in ops) { var moveTo = op as MoveTo; if (moveTo != null) { var start = moveTo.Start; var end = moveTo.End; context.MoveTo ((nfloat)start.X, (nfloat)start.Y); context.MoveTo ((nfloat)end.X, (nfloat)end.Y); bb.Add (start); bb.Add (end); continue; } var lt = op as LineTo; if (lt != null) { var start = lt.Start; var end = lt.End; context.AddLineToPoint((float)start.X, (float)start.Y); context.AddLineToPoint((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; var pp = Conversions.GetPoint (context.GetPathCurrentPoint ()); Point c1, c2; at.GetCircles (pp, out c1, out c2); context.AddLineToPoint ((nfloat)p.X, (nfloat)p.Y); bb.Add (p); continue; } var curveTo = op as CurveTo; if (curveTo != null) { var end = curveTo.End; var control1 = curveTo.FirstControlPoint; var control2 = curveTo.SecondControlPoint; context.AddCurveToPoint ((nfloat)control1.X, (nfloat)control1.Y, (nfloat)control2.X, (nfloat)control2.Y, (nfloat)end.X, (nfloat)end.Y); bb.Add (control1); bb.Add (control2); bb.Add (end); continue; } var cp = op as ClosePath; if (cp != null) { context.ClosePath (); continue; } throw new NotSupportedException ("Path Op " + op); } return bb.BoundingBox; }, pen, baseBrush); }
public void DrawText(string text, Rect frame, Font font, TextAlignment alignment = TextAlignment.Left, Pen pen = null, BaseBrush brush = null) { if (brush == null) return; var paint = GetFontPaint(font, alignment); var w = paint.MeasureText(text); var fm = paint.GetFontMetrics(); var h = fm.Ascent + fm.Descent; var point = frame.Position; var fr = new Rect(point, new Size(w, h)); AddBrushPaint(paint, brush, fr); graphics.DrawText(text, (float) point.X, (float) point.Y, paint); }
public void DrawRectangle(Rect frame, Pen pen = null, BaseBrush baseBrush = null) { if (pen == null && baseBrush == null) return; DrawElement (() => { context.AddRect (Conversions.GetCGRect (frame)); return frame; }, pen, baseBrush); }
private Paint GetBrushPaint(BaseBrush brush, Rect frame) { var paint = new Paint(PaintFlags.AntiAlias); AddBrushPaint(paint, brush, frame); return paint; }
/// <summary> /// Initializes a new instance of the <see cref="NGraphics.Custom.Models.Elements.Ellipse"/> class. /// </summary> /// <param name="position">Position.</param> /// <param name="size">Size.</param> /// <param name="pen">Pen.</param> /// <param name="baseBrush">Base brush.</param> public Ellipse(Point position, Size size, Pen pen = null, BaseBrush baseBrush = null) : this(new Rect(position, size), pen, baseBrush) { }