public static void DrawGradient(SKCanvas canvas, int width, int height) { var ltColor = SKColors.White; var dkColor = SKColors.Black; using (var paint = new SKPaint()) { paint.IsAntialias = true; using (var shader = SKShader.CreateLinearGradient( new SKPoint(0, 0), new SKPoint(0, height), new [] { ltColor, dkColor }, null, SKShaderTileMode.Clamp)) { paint.Shader = shader; canvas.DrawPaint(paint); } } // Center and Scale the Surface var scale = (width < height ? width : height) / (240f); canvas.Translate(width / 2f, height / 2f); canvas.Scale(scale, scale); canvas.Translate(-128, -128); using (var paint = new SKPaint()) { paint.IsAntialias = true; using (var shader = SKShader.CreateTwoPointConicalGradient( new SKPoint(115.2f, 102.4f), 25.6f, new SKPoint(102.4f, 102.4f), 128.0f, new [] { ltColor, dkColor }, null, SKShaderTileMode.Clamp )) { paint.Shader = shader; canvas.DrawOval(new SKRect(51.2f, 51.2f, 204.8f, 204.8f), 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(); }
protected override void InternalDraw(SkiaSharp.SKCanvas canvas) { base.InternalDraw(canvas); using (var paint = new SKPaint()) { var renderBounds = RenderBounds; var smallestSize = Math.Min(renderBounds.Size.Width, renderBounds.Size.Height); var square = new Rectangle(renderBounds.Center.X - smallestSize / 2.0, renderBounds.Center.Y - smallestSize / 2.0, smallestSize, smallestSize); Rectangle imageBounds = renderBounds; paint.IsAntialias = true; if (IsCheckable) { paint.Color = SelectionBackgroundColor.ToSKColor(); canvas.DrawOval(square.ToSKRect(), paint); var darkerOutline = SelectionBackgroundColor.AddLuminosity(-0.1); paint.IsStroke = true; paint.Color = darkerOutline.ToSKColor(); paint.StrokeWidth = 1.0f; canvas.DrawOval(square.ToSKRect(), paint); imageBounds = renderBounds.Inflate(-BorderSize, -BorderSize); } if (Drawable != null && !IsSelected) { var drawableSize = Drawable.GetSize(imageBounds.Width, imageBounds.Height); double ratioX = (double)imageBounds.Width / (double)drawableSize.Width; double ratioY = (double)imageBounds.Height / (double)drawableSize.Height; double ratio = ratioX < ratioY ? ratioX : ratioY; double newWidth = drawableSize.Width * ratio; double newHeight = drawableSize.Height * ratio; float cx = (float)(imageBounds.X + (imageBounds.Width / 2.0) - (newWidth / 2.0)); float cy = (float)(imageBounds.Y + (imageBounds.Height / 2.0) - (newHeight / 2.0)); paint.XferMode = SKXferMode.SrcOver; paint.FilterQuality = SKFilterQuality.Low; Drawable.Draw(canvas, new Rectangle(cx, cy, newWidth, newHeight), paint); } if (IsCheckable && IsSelected) { paint.Color = SelectionColor.ToSKColor(); paint.IsStroke = false; canvas.DrawOval(square.ToSKRect(), paint); double ratioX = (double)imageBounds.Width / (double)TickBitmap.Width; double ratioY = (double)imageBounds.Height / (double)TickBitmap.Height; double ratio = ratioX < ratioY ? ratioX : ratioY; double newWidth = TickBitmap.Width * ratio; double newHeight = TickBitmap.Height * ratio; float cx = (float)(imageBounds.X + (imageBounds.Width / 2.0) - (newWidth / 2.0)); float cy = (float)(imageBounds.Y + (imageBounds.Height / 2.0) - (newHeight / 2.0)); paint.XferMode = SKXferMode.SrcOver; paint.FilterQuality = SKFilterQuality.Low; canvas.DrawBitmap(TickBitmap, new SKRect(cx, cy, (float)(cx + newWidth), (float)(cy + newHeight)), paint); } } }
private void DrawEllipseInternal(SKCanvas canvas, SKPaint brush, SKPaint pen, bool isStroked, bool isFilled, ref SKRect rect) { if (isFilled) { canvas.DrawOval(rect, brush); } if (isStroked) { canvas.DrawOval(rect, pen); } }
public static void DrawGradient (SKCanvas canvas, int width, int height) { var ltColor = SKColors.White; var dkColor = SKColors.Black; using (var paint = new SKPaint ()) { paint.IsAntialias = true; using (var shader = SKShader.CreateLinearGradient ( new SKPoint (0, 0), new SKPoint (0, height), new [] {ltColor, dkColor}, null, SKShaderTileMode.Clamp)) { paint.Shader = shader; canvas.DrawPaint (paint); } } // Center and Scale the Surface var scale = (width < height ? width : height) / (240f); canvas.Translate (width/2f, height/2f); canvas.Scale (scale, scale); canvas.Translate (-128, -128); using (var paint = new SKPaint ()) { paint.IsAntialias = true; using (var shader = SKShader.CreateTwoPointConicalGradient ( new SKPoint (115.2f, 102.4f), 25.6f, new SKPoint (102.4f, 102.4f), 128.0f, new [] {ltColor, dkColor}, null, SKShaderTileMode.Clamp )) { paint.Shader = shader; canvas.DrawOval (new SKRect (51.2f, 51.2f, 204.8f, 204.8f), paint); } } }