Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[SVG] Add support for fill-rule="evenodd" #69

Merged
merged 1 commit into from
Feb 11, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -274,7 +274,7 @@ private void ReadElement(XElement e, SKCanvas canvas, SKPaint stroke, SKPaint fi
case "line":
if (stroke != null || fill != null)
{
var elementPath = ReadElement(e);
var elementPath = ReadElement(e, style);
if (elementPath == null)
break;

Expand Down Expand Up @@ -408,7 +408,7 @@ private SKSvgImage ReadImage(XElement e)
return new SKSvgImage(rect, uri, bytes);
}

private SKPath ReadElement(XElement e)
private SKPath ReadElement(XElement e, Dictionary<string, string> style = null)
{
var path = new SKPath();

Expand All @@ -431,25 +431,25 @@ private SKPath ReadElement(XElement e)
path.AddCircle(circle.Center.X, circle.Center.Y, circle.Radius);
break;
case "path":
var d = e.Attribute("d")?.Value;
if (!string.IsNullOrWhiteSpace(d))
{
path.Dispose();
path = SKPath.ParseSvgPathData(d);
}
break;
case "polygon":
case "polyline":
var close = elementName == "polygon";
var p = e.Attribute("points")?.Value;
if (!string.IsNullOrWhiteSpace(p))
string data = null;
if (elementName == "path")
{
data = e.Attribute("d")?.Value;
}
else
{
data = "M" + e.Attribute("points")?.Value;
if (elementName == "polygon")
data += " Z";
}
if (!string.IsNullOrWhiteSpace(data))
{
p = "M" + p;
if (close)
p += " Z";
path.Dispose();
path = SKPath.ParseSvgPathData(p);
path = SKPath.ParseSvgPathData(data);
}
path.FillType = ReadFillRule(style);
break;
case "line":
var line = ReadLine(e);
Expand Down Expand Up @@ -583,23 +583,46 @@ private void ReadFontAttributes(XElement e, SKPaint paint)
{
var fontStyle = ReadStyle(e);

if (!fontStyle.TryGetValue("font-family", out string ffamily) || string.IsNullOrWhiteSpace(ffamily))
if (fontStyle == null || !fontStyle.TryGetValue("font-family", out string ffamily) || string.IsNullOrWhiteSpace(ffamily))
ffamily = paint.Typeface?.FamilyName;
var fweight = ReadFontWeight(fontStyle, paint.Typeface?.FontWeight ?? (int)SKFontStyleWeight.Normal);
var fwidth = ReadFontWidth(fontStyle, paint.Typeface?.FontWidth ?? (int)SKFontStyleWidth.Normal);
var fstyle = ReadFontStyle(fontStyle, paint.Typeface?.FontSlant ?? SKFontStyleSlant.Upright);

paint.Typeface = SKTypeface.FromFamilyName(ffamily, fweight, fwidth, fstyle);

if (fontStyle.TryGetValue("font-size", out string fsize) && !string.IsNullOrWhiteSpace(fsize))
if (fontStyle != null && fontStyle.TryGetValue("font-size", out string fsize) && !string.IsNullOrWhiteSpace(fsize))
paint.TextSize = ReadNumber(fsize);
}

private static SKPathFillType ReadFillRule(Dictionary<string, string> style, SKPathFillType defaultFillRule = SKPathFillType.Winding)
{
var fillRule = defaultFillRule;

if (style != null && style.TryGetValue("fill-rule", out string rule) && !string.IsNullOrWhiteSpace(rule))
{
switch (rule)
{
case "evenodd":
fillRule = SKPathFillType.EvenOdd;
break;
case "nonzero":
fillRule = SKPathFillType.Winding;
break;
default:
fillRule = defaultFillRule;
break;
}
}

return fillRule;
}

private static SKFontStyleSlant ReadFontStyle(Dictionary<string, string> fontStyle, SKFontStyleSlant defaultStyle = SKFontStyleSlant.Upright)
{
var style = defaultStyle;

if (fontStyle.TryGetValue("font-style", out string fstyle) && !string.IsNullOrWhiteSpace(fstyle))
if (fontStyle != null && fontStyle.TryGetValue("font-style", out string fstyle) && !string.IsNullOrWhiteSpace(fstyle))
{
switch (fstyle)
{
Expand All @@ -624,7 +647,7 @@ private static SKFontStyleSlant ReadFontStyle(Dictionary<string, string> fontSty
private int ReadFontWidth(Dictionary<string, string> fontStyle, int defaultWidth = (int)SKFontStyleWidth.Normal)
{
var width = defaultWidth;
if (fontStyle.TryGetValue("font-stretch", out string fwidth) && !string.IsNullOrWhiteSpace(fwidth) && !int.TryParse(fwidth, out width))
if (fontStyle != null && fontStyle.TryGetValue("font-stretch", out string fwidth) && !string.IsNullOrWhiteSpace(fwidth) && !int.TryParse(fwidth, out width))
{
switch (fwidth)
{
Expand Down Expand Up @@ -674,7 +697,7 @@ private int ReadFontWeight(Dictionary<string, string> fontStyle, int defaultWeig
{
var weight = defaultWeight;

if (fontStyle.TryGetValue("font-weight", out string fweight) && !string.IsNullOrWhiteSpace(fweight) && !int.TryParse(fweight, out weight))
if (fontStyle != null && fontStyle.TryGetValue("font-weight", out string fweight) && !string.IsNullOrWhiteSpace(fweight) && !int.TryParse(fweight, out weight))
{
switch (fweight)
{
Expand Down Expand Up @@ -709,7 +732,7 @@ private void LogOrThrow(string message)

private string GetString(Dictionary<string, string> style, string name, string defaultValue = "")
{
if (style.TryGetValue(name, out string v))
if (style != null && style.TryGetValue(name, out string v))
return v;
return defaultValue;
}
Expand Down Expand Up @@ -1342,7 +1365,7 @@ private float ReadOpacity(Dictionary<string, string> style)
private float ReadNumber(Dictionary<string, string> style, string key, float defaultValue)
{
float value = defaultValue;
if (style.TryGetValue(key, out string strValue))
if (style != null && style.TryGetValue(key, out string strValue))
{
value = ReadNumber(strValue);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -413,6 +413,16 @@ public void SvgFillsAreCorrect()
Assert.Equal(SKColors.White, bmp.GetPixel(11, 20));
}

[Fact]
public void FillRulesAreRespected()
{
var path = Path.Combine(PathToImages, "fill-rule.svg");
var bmp = LoadSvgBitmap(path, SKColors.Green);

Assert.Equal(SKColors.Black, bmp.GetPixel(60, 60));
Assert.Equal(SKColors.Green, bmp.GetPixel(160, 60));
}

private static SKBitmap LoadSvgBitmap(string svgPath, SKColor? background = null)
{
// open the SVG
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.