/// <summary> /// Draws text /// </summary> /// <param name="text"></param> /// <param name="frame"></param> /// <param name="font"></param> /// <param name="alignment"></param> /// <param name="pen"></param> /// <param name="brush"></param> public void DrawText(string text, Rect frame, Font font, TextAlignment alignment = TextAlignment.Left, Pen pen = null, NGraphics.Brush brush = null) { var textBlock = new TextBlock(); textBlock.Text = text; Canvas.SetLeft(textBlock, frame.X); Canvas.SetTop(textBlock, frame.Y); textBlock.FontFamily = new FontFamily(font.Family); textBlock.FontSize = font.Size; if (pen != null) { textBlock.Foreground = new SolidColorBrush(new System.Windows.Media.Color { A = pen.Color.A, R = pen.Color.R, G = pen.Color.G, B = pen.Color.B }); } _canvas.Children.Add(textBlock); }
/// <summary> /// Draws text /// </summary> /// <param name="text"></param> /// <param name="frame"></param> /// <param name="font"></param> /// <param name="alignment"></param> /// <param name="pen"></param> /// <param name="brush"></param> public void DrawText(string text, Rect frame, Font font, TextAlignment alignment = TextAlignment.Left, Pen pen = null, NGraphics.Brush brush = null) { var textBlock = new TextBlock(); textBlock.Text = text; textBlock.Width = frame.Width; textBlock.Height = frame.Height; Canvas.SetLeft(textBlock, frame.X); Canvas.SetTop(textBlock, frame.Y); textBlock.FontFamily = new FontFamily(font.Family); textBlock.FontSize = font.Size; switch (alignment) { case TextAlignment.Left: textBlock.TextAlignment = System.Windows.TextAlignment.Left; break; case TextAlignment.Center: textBlock.TextAlignment = System.Windows.TextAlignment.Center; break; case TextAlignment.Right: textBlock.TextAlignment = System.Windows.TextAlignment.Right; break; } if (pen != null) { textBlock.Foreground = new SolidColorBrush(new System.Windows.Media.Color { A = pen.Color.A, R = pen.Color.R, G = pen.Color.G, B = pen.Color.B }); } textBlock.RenderTransform = Conversions.GetTransform(CurrentTransform); _canvas.Children.Add(textBlock); }
/// <summary> /// Draws an ellipse /// </summary> /// <param name="frame"></param> /// <param name="pen"></param> /// <param name="brush"></param> public void DrawEllipse(Rect frame, Pen pen = null, NGraphics.Brush brush = null) { var ellipseEl = new System.Windows.Shapes.Ellipse(); ellipseEl.Width = frame.Width; ellipseEl.Height = frame.Height; if (brush != null) { ellipseEl.Fill = GetBrush(brush); } if (pen != null) { ellipseEl.Stroke = GetStroke(pen); ellipseEl.StrokeThickness = pen.Width; } _canvas.Children.Add(ellipseEl); Canvas.SetLeft(ellipseEl, frame.X); Canvas.SetTop(ellipseEl, frame.Y); }
/// <summary> /// Draws an ellipse /// </summary> /// <param name="frame"></param> /// <param name="pen"></param> /// <param name="brush"></param> public void DrawEllipse(Rect frame, Pen pen = null, NGraphics.Brush brush = null) { var ellipseEl = new System.Windows.Shapes.Ellipse(); var offset = pen != null ? pen.Width : 0.0; ellipseEl.Width = frame.Width + offset; ellipseEl.Height = frame.Height + offset; if (brush != null) { ellipseEl.Fill = GetBrush(brush); } if (pen != null) { ellipseEl.Stroke = GetStroke(pen); ellipseEl.StrokeThickness = pen.Width; } ellipseEl.RenderTransform = Conversions.GetTransform(CurrentTransform); _canvas.Children.Add(ellipseEl); Canvas.SetLeft(ellipseEl, frame.X - offset / 2.0); Canvas.SetTop(ellipseEl, frame.Y - offset / 2.0); }
void ApplyStyle(string style, ref Pen pen, out bool hasPen, ref Brush brush, out bool hasBrush) { var d = ParseStyle(style); ApplyStyle(d, ref pen, out hasPen, ref brush, out hasBrush); }
public SvgReader(string svgString, double pixelsPerInch = 160.0, Brush defaultBrush = null) { PixelsPerInch = pixelsPerInch; Read(XDocument.Parse(svgString), defaultBrush); }
public Text(Rect frame, Font font, TextAlignment alignment = TextAlignment.Left, Pen pen = null, Brush brush = null) : base(pen, brush) { Frame = frame; Font = font; Alignment = alignment; Spans = new List <TextSpan> (); }
/// <summary> /// Returns a Windows brush from the NGraphics brush /// </summary> /// <param name="fromBrush"></param> /// <returns></returns> private Windows.UI.Xaml.Media.Brush GetBrush(NGraphics.Brush fromBrush) { var sb = fromBrush as SolidBrush; if (sb != null) { // Solid brush return(new SolidColorBrush(new Windows.UI.Color { A = sb.Color.A, R = sb.Color.R, G = sb.Color.G, B = sb.Color.B })); } var lb = fromBrush as NGraphics.LinearGradientBrush; if (lb != null) { // Linear gradient var gradStops = new GradientStopCollection(); var n = lb.Stops.Count; if (n >= 2) { var locs = new float[n]; var comps = new int[n]; for (var i = 0; i < n; i++) { var s = lb.Stops[i]; gradStops.Add(new Windows.UI.Xaml.Media.GradientStop { Color = new Windows.UI.Color { A = s.Color.A, R = s.Color.R, B = s.Color.B, G = s.Color.G, }, Offset = s.Offset, }); } } var grad = new Windows.UI.Xaml.Media.LinearGradientBrush(gradStops, 90); return(grad); } var rb = fromBrush as NGraphics.RadialGradientBrush; if (rb != null) { // Radial gradient throw new NotSupportedException("RadialGradientBrush is not supported for Windws Store Apps."); //var grad = new Windows.UI.Xaml.Media.RadialGradientBrush(); //var n = rb.Stops.Count; //if (n >= 2) //{ // var locs = new float[n]; // var comps = new int[n]; // for (var i = 0; i < n; i++) // { // var s = rb.Stops[i]; // grad.GradientStops.Add(new Windows.UI.Xaml.Media.GradientStop // { // Color = new Windows.UI.Color // { // A = s.Color.A, // R = s.Color.R, // B = s.Color.B, // G = s.Color.G, // }, // Offset = s.Offset, // }); // } //} //return grad; } return(null); }
public Path(IEnumerable <PathOp> operations, Pen pen = null, Brush brush = null) : base(pen, brush) { Operations.AddRange(operations); }
public virtual void DrawEllipse(Rect frame, Pen pen = null, Brush brush = null) { NextCanvas.DrawEllipse(frame, GetPen(pen), GetBrush(brush)); }
public virtual void DrawRectangle(Rect frame, Size corner, Pen pen = null, Brush brush = null) { NextCanvas.DrawRectangle(frame, corner, GetPen(pen), GetBrush(brush)); }
public virtual void DrawPath(System.Collections.Generic.IEnumerable <PathOp> ops, Pen pen = null, Brush brush = null) { NextCanvas.DrawPath(ops, GetPen(pen), GetBrush(brush)); }
public virtual void DrawText(string text, Rect frame, Font font, TextAlignment alignment = TextAlignment.Left, Pen pen = null, Brush brush = null) { NextCanvas.DrawText(text, frame, font, alignment, GetPen(pen), GetBrush(brush)); }
public virtual Brush GetBrush(Brush brush) { return(brush); }
void ApplyStyle(Dictionary <string, string> style, ref Pen pen, out bool hasPen, ref Brush brush, out bool hasBrush) { // // Pen attributes // var strokeWidth = GetString(style, "stroke-width"); if (!string.IsNullOrWhiteSpace(strokeWidth)) { if (pen == null) { pen = new Pen(); } pen.Width = ReadNumber(strokeWidth); } var strokeOpacity = GetString(style, "stroke-opacity"); if (string.IsNullOrWhiteSpace(strokeOpacity)) { strokeOpacity = GetString(style, "opacity"); } if (!string.IsNullOrWhiteSpace(strokeOpacity)) { if (pen == null) { pen = new Pen(); } pen.Color = pen.Color.WithAlpha(ReadNumber(strokeOpacity)); } // // Pen // var stroke = GetString(style, "stroke").Trim(); if (string.IsNullOrEmpty(stroke)) { // No change hasPen = false; } else if (stroke.Equals("none", StringComparison.OrdinalIgnoreCase)) { hasPen = true; pen = null; } else { hasPen = true; if (pen == null) { pen = new Pen(); } Color color; if (Colors.TryParse(stroke, out color)) { if (pen.Color.Alpha == 1) { pen.Color = color; } else { pen.Color = color.WithAlpha(pen.Color.Alpha); } } } // // Brush attributes // var fillOpacity = GetString(style, "fill-opacity"); if (string.IsNullOrWhiteSpace(fillOpacity)) { fillOpacity = GetString(style, "opacity"); } if (!string.IsNullOrWhiteSpace(fillOpacity)) { if (brush == null) { brush = new SolidBrush(); } var sb = brush as SolidBrush; if (sb != null) { sb.Color = sb.Color.WithAlpha(ReadNumber(fillOpacity)); } } // // Brush // var fill = GetString(style, "fill").Trim(); if (string.IsNullOrEmpty(fill)) { // No change hasBrush = false; } else if (fill.Equals("none", StringComparison.OrdinalIgnoreCase)) { hasBrush = true; brush = null; } else { hasBrush = true; Color color; if (Colors.TryParse(fill, out color)) { var sb = brush as SolidBrush; if (sb == null) { brush = new SolidBrush(color); } else { if (sb.Color.Alpha == 1) { sb.Color = color; } else { sb.Color = color.WithAlpha(sb.Color.Alpha); } } } else { var urlM = fillUrlRe.Match(fill); if (urlM.Success) { var id = urlM.Groups [1].Value.Trim(); brush = GetGradientBrush(id, null); } else { throw new NotSupportedException("Fill " + fill); } } } }
void AddElements(IList <Element> list, IEnumerable <XElement> es, Pen inheritPen, Brush inheritBrush) { foreach (var e in es) { AddElement(list, e, inheritPen, inheritBrush); } }
public Path(Pen pen = null, Brush brush = null) : base(pen, brush) { }
public void DrawRectangle(Rect frame, Size corner, Pen pen = null, NGraphics.Brush brush = null) { DrawRectangle(frame, pen, brush); }
void ICanvas.DrawRectangle(Rect frame, Size corner, Pen pen, NGraphics.Brush brush) { DrawRectangle(frame, pen, brush); }
void GetPenAndBrush(XElement e, Pen inheritPen, Brush inheritBrush, out Pen pen, out Brush brush) { bool hasPen, hasBrush; pen = null; brush = null; ApplyStyle(e.Attributes().ToDictionary(k => k.Name.LocalName, v => v.Value), ref pen, out hasPen, ref brush, out hasBrush); var style = ReadString(e.Attribute("style")); if (!string.IsNullOrWhiteSpace(style)) { ApplyStyle(style, ref pen, out hasPen, ref brush, out hasBrush); } pen = hasPen ? pen : inheritPen; brush = hasBrush ? brush : inheritBrush; }
void AddElement(IList <Element> list, XElement e, Pen inheritPen, Brush inheritBrush) { // // Style // Element r = null; GetPenAndBrush(e, inheritPen, inheritBrush, out var pen, out var brush); //var id = ReadString (e.Attribute ("id")); // // Elements // switch (e.Name.LocalName) { case "text": { var x = ReadNumber(e.Attribute("x")); var y = ReadNumber(e.Attribute("y")); var font = new Font(); var fontFamily = ReadTextFontFamily(e); if (!string.IsNullOrEmpty(fontFamily)) { font.Family = fontFamily; } var fontSize = ReadTextFontSize(e); if (fontSize >= 0) { font.Size = fontSize; } TextAlignment textAlignment = ReadTextAlignment(e); var txt = new Text(new Rect(new Point(x, y), new Size(double.MaxValue, double.MaxValue)), font, textAlignment, pen, brush); ReadTextSpans(txt, e, pen, brush); r = txt; } break; case "rect": { var x = ReadNumber(e.Attribute("x")); var y = ReadNumber(e.Attribute("y")); var width = ReadNumber(e.Attribute("width")); var height = ReadNumber(e.Attribute("height")); var rx = ReadNumber(e.Attribute("rx")); var ry = ReadNumber(e.Attribute("ry")); if (ry == 0) { ry = rx; } r = new Rectangle(new Rect(new Point(x, y), new Size(width, height)), new Size(rx, ry), pen, brush); } break; case "ellipse": { var cx = ReadNumber(e.Attribute("cx")); var cy = ReadNumber(e.Attribute("cy")); var rx = ReadNumber(e.Attribute("rx")); var ry = ReadNumber(e.Attribute("ry")); r = new Ellipse(new Point(cx - rx, cy - ry), new Size(2 * rx, 2 * ry), pen, brush); } break; case "circle": { var cx = ReadNumber(e.Attribute("cx")); var cy = ReadNumber(e.Attribute("cy")); var rr = ReadNumber(e.Attribute("r")); r = new Ellipse(new Point(cx - rr, cy - rr), new Size(2 * rr, 2 * rr), pen, brush); } break; case "path": { var dA = e.Attribute("d"); if (dA != null && !string.IsNullOrWhiteSpace(dA.Value)) { var p = new Path(pen, brush); ReadPath(p, dA.Value); r = p; } } break; case "polygon": { var pA = e.Attribute("points"); if (pA != null && !string.IsNullOrWhiteSpace(pA.Value)) { var path = new Path(pen, brush); ReadPoints(path, pA.Value, true); r = path; } } break; case "polyline": { var pA = e.Attribute("points"); if (pA != null && !string.IsNullOrWhiteSpace(pA.Value)) { var path = new Path(pen, brush); ReadPoints(path, pA.Value, false); r = path; } } break; case "g": { var g = new Group(); var groupId = e.Attribute("id"); if (groupId != null && !string.IsNullOrEmpty(groupId.Value)) { g.Id = groupId.Value; } var groupOpacity = e.Attribute("opacity"); if (groupOpacity != null && !string.IsNullOrEmpty(groupOpacity.Value)) { g.Opacity = ReadNumber(groupOpacity); } AddElements(g.Children, e.Elements(), pen, brush); r = 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 <Element> (); AddElement(useList, useE, pen, brush); r = useList.FirstOrDefault(); } } } break; case "title": Graphic.Title = ReadString(e); break; case "desc": case "description": Graphic.Description = ReadString(e); break; case "defs": // Already read in earlier pass break; case "namedview": case "metadata": case "image": // Ignore break; case "line": { var x1 = ReadNumber(e.Attribute("x1")); var x2 = ReadNumber(e.Attribute("x2")); var y1 = ReadNumber(e.Attribute("y1")); var y2 = ReadNumber(e.Attribute("y2")); var p = new Path(pen, null); p.MoveTo(x1, y1); p.LineTo(x2, y2); r = p; } break; case "foreignObject": { var x = ReadNumber(e.Attribute("x")); var y = ReadNumber(e.Attribute("y")); var width = ReadNumber(e.Attribute("width")); var height = ReadNumber(e.Attribute("height")); r = new ForeignObject(new Point(x, y), new Size(width, height)); } break; case "pgf": { var id = e.Attribute("id"); System.Diagnostics.Debug.WriteLine("Ignoring pgf element" + (id != null ? ": '" + id.Value + "'" : "")); } break; case "switch": { // Evaluate requiredFeatures, requiredExtensions and systemLanguage foreach (var ee in e.Elements()) { var requiredFeatures = ee.Attribute("requiredFeatures"); var requiredExtensions = ee.Attribute("requiredExtensions"); var systemLanguage = ee.Attribute("systemLanguage"); // currently no support for any of these restrictions if (requiredFeatures == null && requiredExtensions == null && systemLanguage == null) { AddElement(list, ee, pen, brush); } } } break; // color definition that can be referred to by other elements case "linearGradient": break; case "mask": break; default: throw new NotSupportedException("SVG element \"" + e.Name.LocalName + "\" is not supported"); } if (r != null) { r.Transform = ReadTransform(ReadString(e.Attribute("transform"))); var ida = e.Attribute("id"); if (ida != null && !string.IsNullOrEmpty(ida.Value)) { r.Id = ida.Value.Trim(); } list.Add(r); } }
/// <summary> /// Draws a path /// </summary> /// <param name="ops"></param> /// <param name="pen"></param> /// <param name="brush"></param> public void DrawPath(IEnumerable <PathOp> ops, Pen pen = null, NGraphics.Brush brush = null) { if (pen == null && brush == null) { return; } var pathEl = new System.Windows.Shapes.Path(); if (brush != null) { pathEl.Fill = GetBrush(brush); } if (pen != null) { pathEl.Stroke = GetStroke(pen); pathEl.StrokeThickness = pen.Width; } var geo = new StringBuilder(); foreach (var op in ops) { var mt = op as MoveTo; if (mt != null) { geo.AppendFormat(CultureInfo.InvariantCulture, " M {0},{1}", mt.Point.X, mt.Point.Y); continue; } var lt = op as LineTo; if (lt != null) { geo.AppendFormat(CultureInfo.InvariantCulture, " L {0},{1}", lt.Point.X, lt.Point.Y); continue; } var at = op as ArcTo; if (at != null) { var p = at.Point; var r = at.Radius; geo.AppendFormat(CultureInfo.InvariantCulture, " A {0},{1} 0 {2} {3} {4},{5}", r.Width, r.Height, at.LargeArc ? 1 : 0, at.SweepClockwise ? 1 : 0, p.X, p.Y); continue; } var ct = op as CurveTo; if (ct != null) { var p = ct.Point; var c1 = ct.Control1; var c2 = ct.Control2; geo.AppendFormat(CultureInfo.InvariantCulture, " C {0},{1} {2},{3} {4},{5}", c1.X, c1.Y, c2.X, c2.Y, p.X, p.Y); continue; } var cp = op as ClosePath; if (cp != null) { geo.Append(" z"); continue; } } // Convert path string to geometry var b = new Binding { Source = geo.ToString() }; BindingOperations.SetBinding(pathEl, System.Windows.Shapes.Path.DataProperty, b); pathEl.RenderTransform = Conversions.GetTransform(CurrentTransform); _canvas.Children.Add(pathEl); }
// readonly XNamespace ns; public SvgReader(System.IO.TextReader reader, double pixelsPerInch = 160.0, Brush defaultBrush = null) { defaultBrush = defaultBrush ?? Brushes.Black; PixelsPerInch = pixelsPerInch; Read(XDocument.Load(reader), defaultBrush); }
/// <summary> /// Returns a Windows brush from the NGraphics brush /// </summary> /// <param name="fromBrush"></param> /// <returns></returns> private System.Windows.Media.Brush GetBrush(NGraphics.Brush fromBrush) { var sb = fromBrush as SolidBrush; if (sb != null) { // Solid brush return(new SolidColorBrush(new System.Windows.Media.Color { A = sb.Color.A, R = sb.Color.R, G = sb.Color.G, B = sb.Color.B })); } var lb = fromBrush as NGraphics.LinearGradientBrush; if (lb != null) { // Linear gradient var gradStops = new GradientStopCollection(); var n = lb.Stops.Count; if (n >= 2) { var locs = new float[n]; var comps = new int[n]; for (var i = 0; i < n; i++) { var s = lb.Stops[i]; gradStops.Add(new System.Windows.Media.GradientStop { Color = new System.Windows.Media.Color { A = s.Color.A, R = s.Color.R, B = s.Color.B, G = s.Color.G, }, Offset = s.Offset, }); } } var grad = new System.Windows.Media.LinearGradientBrush(gradStops, 90); return(grad); } var rb = fromBrush as NGraphics.RadialGradientBrush; if (rb != null) { // Radial gradient var grad = new System.Windows.Media.RadialGradientBrush(); var n = rb.Stops.Count; if (n >= 2) { var locs = new float[n]; var comps = new int[n]; for (var i = 0; i < n; i++) { var s = rb.Stops[i]; grad.GradientStops.Add(new System.Windows.Media.GradientStop { Color = new System.Windows.Media.Color { A = s.Color.A, R = s.Color.R, B = s.Color.B, G = s.Color.G, }, Offset = s.Offset, }); } } return(grad); } return(null); }
public Text(string text, Rect frame, Font font, TextAlignment alignment = TextAlignment.Left, Pen pen = null, Brush brush = null) : base(pen, brush) { Frame = frame; Font = font; Alignment = alignment; Spans = new List <TextSpan> { new TextSpan(text) { Brush = brush, Pen = pen } }; }