Skip to content

Commit

Permalink
SVG fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
daniel-luberda committed Jun 9, 2018
1 parent 2d6483b commit f4ffdac
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 29 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,15 @@
<ScrollView><StackLayout>

<!-- FIRST APPROACH: -->
<!--<ffimageloadingsvg:SvgCachedImage WidthRequest="200" HeightRequest="200" Source="sample.svg"/>-->
<ffimageloadingsvg:SvgCachedImage WidthRequest="200" HeightRequest="200" Source="sample.svg"/>

<!-- SECOND APPROACH (PCL EmbeddedResource): -->
<ffimageloadingsvg:SvgCachedImage WidthRequest="200" HeightRequest="200" Source="resource://FFImageLoading.Forms.Sample.Resources.sample.svg"/>

<!-- THIRD APPROACH: -->
<!-- <ffimageloading:CachedImage WidthRequest="200" HeightRequest="200"
<ffimageloading:CachedImage WidthRequest="200" HeightRequest="200"
Source="{Binding Source, Converter={StaticResource SvgImageSourceConverter}}">
</ffimageloading:CachedImage>-->
</ffimageloading:CachedImage>

</StackLayout></ScrollView>
</ContentPage.Content>
Expand Down
54 changes: 28 additions & 26 deletions source/FFImageLoading.Svg.Shared/SkSvg.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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"));
Expand Down Expand Up @@ -388,39 +387,42 @@ 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))
{
var gradient = (SKRadialGradient)addFill;
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);

Expand Down Expand Up @@ -989,7 +991,7 @@ private void ReadPaints(Dictionary<string, string> 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;
Expand Down Expand Up @@ -1130,7 +1132,7 @@ private void ReadPaints(Dictionary<string, string> 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;
Expand Down

0 comments on commit f4ffdac

Please sign in to comment.