public static void RenderTexture(SKCanvas canvas, SKBitmap bitmap, float x, float y, float orientation = 0, float offsetX = 0, float offsetY = 0, LabelStyle.HorizontalAlignmentEnum horizontalAlignment = LabelStyle.HorizontalAlignmentEnum.Center, LabelStyle.VerticalAlignmentEnum verticalAlignment = LabelStyle.VerticalAlignmentEnum.Center, float opacity = 1f, float scale = 1f) { canvas.Save(); canvas.Translate(x, y); canvas.RotateDegrees(orientation, 0, 0); // todo: or degrees? canvas.Scale(scale, scale); x = offsetX + DetermineHorizontalAlignmentCorrection(horizontalAlignment, bitmap.Width); y = -offsetY + DetermineVerticalAlignmentCorrection(verticalAlignment, bitmap.Height); var halfWidth = bitmap.Width/2; var halfHeight = bitmap.Height/2; var rect = new SKRect(x - halfWidth, y - halfHeight, x + halfWidth, y + halfHeight); RenderTexture(canvas, bitmap, rect, opacity); canvas.Restore(); }
public SKAutoCanvasRestore(SKCanvas canvas, bool doSave) { this.canvas = canvas; this.saveCount = 0; if (canvas != null) { saveCount = canvas.SaveCount; if (doSave) { canvas.Save (); } } }
public SKAutoCanvasRestore(SKCanvas canvas, bool doSave) { this.canvas = canvas; this.saveCount = 0; if (canvas != null) { saveCount = canvas.SaveCount; if (doSave) { canvas.Save(); } } }
public static void ManageDrawMatrix(SKCanvas canvas, int width, int height) { var size = ((float)height > width ? width : height) * 0.5f; var center = new SKPoint((width - size) / 2f, (height - size) / 2f); // draw these at specific locations var leftRect = SKRect.Create(center.X - size / 2f, center.Y, size, size); var rightRect = SKRect.Create(center.X + size / 2f, center.Y, size, size); // draw this at the current location / transformation var rotatedRect = SKRect.Create(0f, 0f, size, size); using (var paint = new SKPaint()) { paint.IsAntialias = true; canvas.Clear(XamPurple); // draw paint.Color = XamDkBlue; canvas.DrawRect(leftRect, paint); // save canvas.Save(); // transform canvas.Translate(width / 2f, center.Y); canvas.RotateDegrees(45); // draw paint.Color = XamGreen; canvas.DrawRoundRect(rotatedRect, 10, 10, paint); // undo transform / restore canvas.Restore(); // draw paint.Color = XamLtBlue; canvas.DrawRect(rightRect, paint); } }
private void ReadElement(XElement e, SKCanvas canvas, SKPaint stroke, SKPaint fill) { ReadPaints(e, ref stroke, ref fill); // transform matrix var transform = ReadTransform(e.Attribute("transform")?.Value ?? string.Empty); canvas.Save(); canvas.Concat(ref transform); // SVG elements var elementName = e.Name.LocalName; switch (elementName) { case "text": if (stroke != null || fill != null) { ReadText(e, canvas, stroke?.Clone(), fill?.Clone()); } break; case "rect": if (stroke != null || fill != null) { 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")); var rect = SKRect.Create(x, y, width, height); if (rx > 0 || ry > 0) { if (fill != null) { canvas.DrawRoundRect(rect, rx, ry, fill); } if (stroke != null) { canvas.DrawRoundRect(rect, rx, ry, stroke); } } else { if (fill != null) { canvas.DrawRect(rect, fill); } if (stroke != null) { canvas.DrawRect(rect, stroke); } } } break; case "ellipse": if (stroke != null || fill != null) { var cx = ReadNumber(e.Attribute("cx")); var cy = ReadNumber(e.Attribute("cy")); var rx = ReadNumber(e.Attribute("rx")); var ry = ReadNumber(e.Attribute("ry")); if (fill != null) { canvas.DrawOval(cx, cy, rx, ry, fill); } if (stroke != null) { canvas.DrawOval(cx, cy, rx, ry, stroke); } } break; case "circle": if (stroke != null || fill != null) { var cx = ReadNumber(e.Attribute("cx")); var cy = ReadNumber(e.Attribute("cy")); var rr = ReadNumber(e.Attribute("r")); if (fill != null) { canvas.DrawCircle(cx, cy, rr, fill); } if (stroke != null) { canvas.DrawCircle(cx, cy, rr, stroke); } } break; case "path": if (stroke != null || fill != null) { var d = e.Attribute("d")?.Value; if (!string.IsNullOrWhiteSpace(d)) { var path = SKPath.ParseSvgPathData(d); if (fill != null) { canvas.DrawPath(path, fill); } if (stroke != null) { canvas.DrawPath(path, stroke); } } } break; case "polygon": case "polyline": if (stroke != null || fill != null) { var close = elementName == "polygon"; var p = e.Attribute("points")?.Value; if (!string.IsNullOrWhiteSpace(p)) { var path = ReadPolyPath(p, close); if (fill != null) { canvas.DrawPath(path, fill); } if (stroke != null) { canvas.DrawPath(path, stroke); } } } break; case "g": if (e.HasElements) { foreach (var gElement in e.Elements()) { ReadElement(gElement, canvas, stroke?.Clone(), fill?.Clone()); } } break; case "use": if (e.HasAttributes) { var href = ReadHref(e); if (href != null) { // TODO: copy/process other attributes var x = ReadNumber(e.Attribute("x")); var y = ReadNumber(e.Attribute("y")); var useTransform = SKMatrix.MakeTranslation(x, y); canvas.Save(); canvas.Concat(ref useTransform); ReadElement(href, canvas, stroke?.Clone(), fill?.Clone()); canvas.Restore(); } } break; case "line": if (stroke != null) { var x1 = ReadNumber(e.Attribute("x1")); var x2 = ReadNumber(e.Attribute("x2")); var y1 = ReadNumber(e.Attribute("y1")); var y2 = ReadNumber(e.Attribute("y2")); canvas.DrawLine(x1, y1, x2, y2, stroke); } break; case "switch": if (e.HasElements) { foreach (var ee in e.Elements()) { var requiredFeatures = ee.Attribute("requiredFeatures"); var requiredExtensions = ee.Attribute("requiredExtensions"); var systemLanguage = ee.Attribute("systemLanguage"); // TODO: evaluate requiredFeatures, requiredExtensions and systemLanguage var isVisible = requiredFeatures == null && requiredExtensions == null && systemLanguage == null; if (isVisible) { ReadElement(ee, canvas, stroke?.Clone(), fill?.Clone()); } } } break; case "defs": case "title": case "desc": case "description": // already read earlier break; default: LogOrThrow($"SVG element '{elementName}' is not supported"); break; } // restore matrix canvas.Restore(); }
public void Save() => _canvas.Save();
private SKPoint DrawLineArrowInternal(SKCanvas canvas, SKPaint pen, SKPaint brush, float x, float y, double angle, ArrowStyle style) { SKPoint pt = default(SKPoint); var rt = MatrixHelper.Rotation(angle, new SKPoint(x, y)); double rx = style.RadiusX; double ry = style.RadiusY; double sx = 2.0 * rx; double sy = 2.0 * ry; switch (style.ArrowType) { default: case ArrowType.None: { pt = new SKPoint(x, y); } break; case ArrowType.Rectangle: { pt = MatrixHelper.TransformPoint(rt, new SKPoint(x - (float)sx, y)); var rect = ToSKRect(x - sx, y - ry, sx, sy); int count = canvas.Save(); canvas.SetMatrix(rt); DrawRectangleInternal(canvas, brush, pen, style.IsStroked, style.IsFilled, ref rect); canvas.RestoreToCount(count); } break; case ArrowType.Ellipse: { pt = MatrixHelper.TransformPoint(rt, new SKPoint(x - (float)sx, y)); int count = canvas.Save(); canvas.SetMatrix(rt); var rect = ToSKRect(x - sx, y - ry, sx, sy); DrawEllipseInternal(canvas, brush, pen, style.IsStroked, style.IsFilled, ref rect); canvas.RestoreToCount(count); } break; case ArrowType.Arrow: { var pts = new SKPoint[] { new SKPoint(x, y), new SKPoint(x - (float)sx, y + (float)sy), new SKPoint(x, y), new SKPoint(x - (float)sx, y - (float)sy), new SKPoint(x, y) }; pt = MatrixHelper.TransformPoint(rt, pts[0]); var p11 = MatrixHelper.TransformPoint(rt, pts[1]); var p21 = MatrixHelper.TransformPoint(rt, pts[2]); var p12 = MatrixHelper.TransformPoint(rt, pts[3]); var p22 = MatrixHelper.TransformPoint(rt, pts[4]); DrawLineInternal(canvas, pen, style.IsStroked, ref p11, ref p21); DrawLineInternal(canvas, pen, style.IsStroked, ref p12, ref p22); } break; } return pt; }
public static void ManageDrawMatrix (SKCanvas canvas, int width, int height) { var size = ((float)height > width ? width : height) * 0.5f; var center = new SKPoint ((width - size) / 2f, (height - size) / 2f); // draw these at specific locations var leftRect = SKRect.Create (center.X - size / 2f, center.Y, size, size); var rightRect = SKRect.Create (center.X + size / 2f, center.Y, size, size); // draw this at the current location / transformation var rotatedRect = SKRect.Create (0f, 0f, size, size); using (var paint = new SKPaint ()) { paint.IsAntialias = true; canvas.Clear (XamPurple); // draw paint.Color = XamDkBlue; canvas.DrawRect (leftRect, paint); // save canvas.Save(); // transform canvas.Translate (width / 2f, center.Y); canvas.RotateDegrees (45); // draw paint.Color = XamGreen; canvas.DrawRect (rotatedRect, paint); // undo transform / restore canvas.Restore(); // draw paint.Color = XamLtBlue; canvas.DrawRect (rightRect, paint); } }
private static void DrawPointWithVectorStyle(SKCanvas canvas, VectorStyle vectorStyle, Point destination, SymbolType symbolType = SymbolType.Ellipse) { canvas.Save(); canvas.Translate((float)destination.X, (float)destination.Y); DrawPointWithVectorStyle(canvas, vectorStyle, symbolType); canvas.Restore(); }