public static System.Windows.Media.Geometry CreateGeometry(DrawingGroup drawingGroup, SvgImportOptions opts) { var geometry = new GeometryGroup { FillRule = opts.FillRule }; var defaultPen = new Pen(new SolidColorBrush(System.Windows.Media.Color.FromRgb(0, 0, 0)), 1); foreach (var drawing in drawingGroup.Children) { switch (drawing) { case GeometryDrawing gd: HandleGeometry(gd.Geometry, geometry, gd, opts, defaultPen); break; case GlyphRunDrawing grd: geometry.Children.Add(grd.GlyphRun.BuildGeometry()); break; case DrawingGroup dg: var c = new SvgImportOptions { FillRule = FillRule.EvenOdd, Smoothness = opts.Smoothness, UseOutlinedGeometry = opts.UseOutlinedGeometry, NeverWidenClosedPaths = opts.NeverWidenClosedPaths }; geometry.Children.Add(CreateGeometry(dg, c)); break; } } geometry.Transform = drawingGroup.Transform; return(geometry); }
private static void HandleGeometry(System.Windows.Media.Geometry g, GeometryGroup geometry, GeometryDrawing gd, SvgImportOptions opts, Pen defaultPen) { switch (g) { case CombinedGeometry cg: geometry.Children.Add(cg); break; case EllipseGeometry eg: if (gd.Pen != null && !opts.NeverWidenClosedPaths) { geometry.Children.Add(eg.GetWidenedPathGeometry(gd.Pen, opts.Smoothness, ToleranceType.Absolute).GetOutlinedPathGeometry(opts.Smoothness, ToleranceType.Absolute)); } else { geometry.Children.Add(eg); } break; case GeometryGroup gg: foreach (var c in gg.Children) { HandleGeometry(c, geometry, gd, opts, defaultPen); } break; case LineGeometry lg: geometry.Children.Add(lg.GetWidenedPathGeometry(gd.Pen, opts.Smoothness, ToleranceType.Absolute)); break; case PathGeometry pg: for (int i = pg.Figures.Count - 1; i >= 0; i--) { if (!pg.Figures[i].IsClosed || (gd.Pen != null && !opts.NeverWidenClosedPaths)) { var penToUse = gd.Pen ?? defaultPen; pg.AddGeometry(new PathGeometry(new List <PathFigure> { pg.Figures[i] }) .GetWidenedPathGeometry(penToUse, opts.Smoothness, ToleranceType.Absolute) .GetOutlinedPathGeometry(opts.Smoothness, ToleranceType.Absolute)); pg.Figures.RemoveAt(i); } } geometry.Children.Add(pg); break; case RectangleGeometry rg: if (gd.Pen != null && !opts.NeverWidenClosedPaths) { geometry.Children.Add(rg.GetWidenedPathGeometry(gd.Pen, opts.Smoothness, ToleranceType.Absolute).GetOutlinedPathGeometry(opts.Smoothness, ToleranceType.Absolute)); } else { geometry.Children.Add(rg); } break; case StreamGeometry sg: geometry.Children.Add(sg); break; default: geometry.Children.Add(gd.Geometry); break; } }