From f4ffdaceadd8502b9322954098eb54c62f76a5b3 Mon Sep 17 00:00:00 2001 From: Daniel Luberda Date: Sat, 9 Jun 2018 18:16:23 +0200 Subject: [PATCH] SVG fixes --- .../Shared/Pages/SvgSamplePage.xaml | 6 +-- source/FFImageLoading.Svg.Shared/SkSvg.cs | 54 ++++++++++--------- 2 files changed, 31 insertions(+), 29 deletions(-) diff --git a/samples/ImageLoading.Forms.Sample/Shared/Pages/SvgSamplePage.xaml b/samples/ImageLoading.Forms.Sample/Shared/Pages/SvgSamplePage.xaml index b4a090d5b..17da42307 100644 --- a/samples/ImageLoading.Forms.Sample/Shared/Pages/SvgSamplePage.xaml +++ b/samples/ImageLoading.Forms.Sample/Shared/Pages/SvgSamplePage.xaml @@ -16,15 +16,15 @@ - + - + diff --git a/source/FFImageLoading.Svg.Shared/SkSvg.cs b/source/FFImageLoading.Svg.Shared/SkSvg.cs index 52e27b968..7af23e68f 100644 --- a/source/FFImageLoading.Svg.Shared/SkSvg.cs +++ b/source/FFImageLoading.Svg.Shared/SkSvg.cs @@ -349,8 +349,7 @@ private void ReadElement(XElement e, SKCanvas canvas, SKPaint stroke, SKPaint fi } string fillId = e.Attribute("fill")?.Value; - object addFill = null; - if (!string.IsNullOrWhiteSpace(fillId) && fills.TryGetValue(fillId, out addFill)) + if (!string.IsNullOrWhiteSpace(fillId) && fills.TryGetValue(fillId, out object addFill)) { var x = ReadNumber(e.Attribute("x")); var y = ReadNumber(e.Attribute("y")); @@ -388,17 +387,17 @@ private void ReadElement(XElement e, SKCanvas canvas, SKPaint stroke, SKPaint fi var startPoint = gradient.GetStartPoint(x, y, width, height); var endPoint = gradient.GetEndPoint(x, y, width, height); - var gradientShader = SKShader.CreateLinearGradient( - startPoint, - endPoint, - gradient.Colors, - gradient.Positions, - gradient.TileMode); - - var gradientPaint = new SKPaint() { Color = SKColors.Black, Shader = gradientShader, IsAntialias = true, IsDither = true }; - canvas.DrawPath(elementPath, gradientPaint); - gradientShader.TryDispose(); - gradientPaint.TryDispose(); + using (var gradientShader = SKShader.CreateLinearGradient( + startPoint, endPoint, gradient.Colors, gradient.Positions, gradient.TileMode)) + { + var oldColor = fill.Color; + var oldShader = fill.Shader; + fill.Color = SKColors.Black; + fill.Shader = gradientShader; + canvas.DrawPath(elementPath, fill); + fill.Color = oldColor; + fill.Shader = oldShader; + } } else if (addFillType == typeof(SKRadialGradient)) { @@ -406,21 +405,24 @@ private void ReadElement(XElement e, SKCanvas canvas, SKPaint stroke, SKPaint fi var centerPoint = gradient.GetCenterPoint(x, y, width, height); var radius = gradient.GetRadius(width, height); - var gradientShader = SKShader.CreateRadialGradient( - centerPoint, - radius, - gradient.Colors, - gradient.Positions, - gradient.TileMode); - - var gradientPaint = new SKPaint() { Color = SKColors.Black, Shader = gradientShader, IsAntialias = true, IsDither = true }; - canvas.DrawPath(elementPath, gradientPaint); - gradientShader.TryDispose(); - gradientPaint.TryDispose(); + using (var gradientShader = SKShader.CreateRadialGradient( + centerPoint, radius, gradient.Colors, gradient.Positions, gradient.TileMode)) + { + var oldColor = fill.Color; + var oldShader = fill.Shader; + fill.Color = SKColors.Black; + fill.Shader = gradientShader; + canvas.DrawPath(elementPath, fill); + fill.Color = oldColor; + fill.Shader = oldShader; + } } } else if (fill != null) + { canvas.DrawPath(elementPath, fill); + } + if (stroke != null) canvas.DrawPath(elementPath, stroke); @@ -989,7 +991,7 @@ private void ReadPaints(Dictionary style, ref SKPaint strokePain if (ColorHelper.TryParse(stroke, out SKColor color)) { // preserve alpha - if (color.Alpha == 255) + if (color.Alpha == 255 && fillPaint.Color.Alpha > 0) strokePaint.Color = color.WithAlpha(strokePaint.Color.Alpha); else strokePaint.Color = color; @@ -1130,7 +1132,7 @@ private void ReadPaints(Dictionary style, ref SKPaint strokePain if (ColorHelper.TryParse(fill, out SKColor color)) { // preserve alpha - if (color.Alpha == 255) + if (color.Alpha == 255 && fillPaint.Color.Alpha > 0) fillPaint.Color = color.WithAlpha(fillPaint.Color.Alpha); else fillPaint.Color = color;