public Geometry CreateGeometryEx(SvgPathElement element) { PathGeometry geometry = new PathGeometry(); string pathScript = element.PathScript; if (string.IsNullOrWhiteSpace(pathScript)) { return(geometry); } var comparer = StringComparison.OrdinalIgnoreCase; string fillRule = element.GetPropertyValue("fill-rule"); string clipRule = element.GetAttribute("clip-rule"); if (!string.IsNullOrWhiteSpace(clipRule) && string.Equals(clipRule, "evenodd", comparer) || string.Equals(clipRule, CssConstants.ValNonzero, comparer)) { fillRule = clipRule; } if (string.Equals(fillRule, "evenodd", comparer)) { geometry.FillRule = FillRule.EvenOdd; } else if (string.Equals(fillRule, CssConstants.ValNonzero, comparer)) { geometry.FillRule = FillRule.Nonzero; } try { geometry.Figures = PathFigureCollection.Parse(pathScript); if (_flattenClosedPath && element.IsClosed && geometry.MayHaveCurves() == element.MayHaveCurves) { int closedCount = 0; foreach (var figure in geometry.Figures) { if (figure.IsClosed) { closedCount++; } } if (geometry.Figures.Count == closedCount) { return(geometry.GetFlattenedPathGeometry(_flattenTolerance, _flattenToleranceType)); } } return(geometry); } catch (FormatException ex) { Trace.TraceError(ex.GetType().Name + ": " + ex.Message); //return null; return(CreateGeometry(element)); } }
public Geometry CreateGeometryEx(SvgPathElement element) { PathGeometry geometry = new PathGeometry(); string pathScript = element.PathScript; if (string.IsNullOrWhiteSpace(pathScript)) { return(geometry); } string fillRule = element.GetPropertyValue("fill-rule"); string clipRule = element.GetAttribute("clip-rule"); if (!string.IsNullOrWhiteSpace(clipRule) && string.Equals(clipRule, "evenodd") || string.Equals(clipRule, "nonzero")) { fillRule = clipRule; } if (fillRule == "evenodd") { geometry.FillRule = FillRule.EvenOdd; } else if (fillRule == "nonzero") { geometry.FillRule = FillRule.Nonzero; } try { geometry.Figures = PathFigureCollection.Parse(pathScript); if (_flattenClosedPath && element.IsClosed && geometry.MayHaveCurves() == element.MayHaveCurves) { int closedCount = 0; foreach (var figure in geometry.Figures) { if (figure.IsClosed) { closedCount++; } } if (geometry.Figures.Count == closedCount) { return(geometry.GetFlattenedPathGeometry(_flattenTolerance, _flattenToleranceType)); } } return(geometry); } catch { return(CreateGeometry(element)); } }
public Geometry CreateGeometry(SvgPathElement element) { PathGeometry geometry = new PathGeometry(); var comparer = StringComparison.OrdinalIgnoreCase; string fillRule = element.GetPropertyValue("fill-rule"); string clipRule = element.GetAttribute("clip-rule"); if (!string.IsNullOrWhiteSpace(clipRule) && string.Equals(clipRule, "evenodd", comparer) || string.Equals(clipRule, CssConstants.ValNonzero, comparer)) { fillRule = clipRule; } if (string.Equals(fillRule, "evenodd", comparer)) { geometry.FillRule = FillRule.EvenOdd; } else if (string.Equals(fillRule, CssConstants.ValNonzero, comparer)) { geometry.FillRule = FillRule.Nonzero; } SvgPointF initPoint = new SvgPointF(0, 0); SvgPointF lastPoint = new SvgPointF(0, 0); SvgPointF ptXY = new SvgPointF(0, 0); SvgPathSeg segment = null; SvgPathSegMoveto pathMoveTo = null; SvgPathSegLineto pathLineTo = null; SvgPathSegCurveto pathCurveTo = null; SvgPathSegArc pathArc = null; SvgPathSegList segments = element.PathSegList; int numSegs = segments.NumberOfItems; if (numSegs == 0) { return(geometry); } PathFigure pathFigure = null; for (int i = 0; i < numSegs; i++) { segment = segments.GetItem(i); switch (segment.PathType) { case SvgPathType.MoveTo: //if (DynamicCast.Cast(segment, out pathMoveTo)) pathMoveTo = (SvgPathSegMoveto)segment; if (pathFigure != null) { pathFigure.IsClosed = false; pathFigure.IsFilled = true; geometry.Figures.Add(pathFigure); pathFigure = null; } lastPoint = initPoint = pathMoveTo.AbsXY; pathFigure = new PathFigure(); pathFigure.StartPoint = new Point(initPoint.ValueX, initPoint.ValueY); break; case SvgPathType.LineTo: //else if (DynamicCast.Cast(segment, out pathLineTo)) pathLineTo = (SvgPathSegLineto)segment; ptXY = pathLineTo.AbsXY; pathFigure.Segments.Add(new LineSegment(new Point(ptXY.ValueX, ptXY.ValueY), true)); lastPoint = ptXY; break; case SvgPathType.CurveTo: //else if (DynamicCast.Cast(segment, out pathCurveTo)) pathCurveTo = (SvgPathSegCurveto)segment; SvgPointF xy = pathCurveTo.AbsXY; SvgPointF x1y1 = pathCurveTo.CubicX1Y1; SvgPointF x2y2 = pathCurveTo.CubicX2Y2; pathFigure.Segments.Add(new BezierSegment(new Point(x1y1.ValueX, x1y1.ValueY), new Point(x2y2.ValueX, x2y2.ValueY), new Point(xy.ValueX, xy.ValueY), true)); lastPoint = xy; break; case SvgPathType.ArcTo: //else if (DynamicCast.Cast(segment, out pathArc)) pathArc = (SvgPathSegArc)segment; ptXY = pathArc.AbsXY; if (lastPoint.Equals(ptXY)) { // If the endpoints (x, y) and (x0, y0) are identical, then this // is equivalent to omitting the elliptical arc segment entirely. } else if (pathArc.R1.Equals(0) || pathArc.R2.Equals(0)) { // Ensure radii are valid pathFigure.Segments.Add(new LineSegment(new Point(ptXY.ValueX, ptXY.ValueY), true)); } else { CalculatedArcValues calcValues = pathArc.GetCalculatedArcValues(); pathFigure.Segments.Add(new ArcSegment(new Point(ptXY.ValueX, ptXY.ValueY), new Size(pathArc.R1, pathArc.R2), pathArc.Angle, pathArc.LargeArcFlag, pathArc.SweepFlag ? SweepDirection.Clockwise : SweepDirection.Counterclockwise, true)); } lastPoint = ptXY; break; case SvgPathType.Close: //else if (segment is SvgPathSegClosePath) if (pathFigure != null) { pathFigure.IsClosed = true; pathFigure.IsFilled = true; geometry.Figures.Add(pathFigure); pathFigure = null; } lastPoint = initPoint; break; } } if (pathFigure != null) { pathFigure.IsClosed = false; pathFigure.IsFilled = true; geometry.Figures.Add(pathFigure); } return(geometry); }