/// <summary> /// Parse all graphic elements /// </summary> private GraphicGroup ParseGroup(XNamespace ns, XElement groupElement, Matrix matrix) { var group = new GraphicGroup(); cssStyleCascade.PushStyles(groupElement); Matrix currentTransformationMatrix = matrix; var transform = cssStyleCascade.GetPropertyFromTop("transform"); if (!string.IsNullOrEmpty(transform)) { var transformMatrix = TransformMatrixParser.GetTransformMatrix(transform); currentTransformationMatrix = transformMatrix * currentTransformationMatrix; } Clipping.SetClipPath(group, currentTransformationMatrix, cssStyleCascade, globalDefinitions); group.Opacity = cssStyleCascade.GetDoubleFromTop("opacity", 1); var shapeParser = new ShapeParser(); foreach (var element in groupElement.Elements()) { switch (element.Name.LocalName) { case "defs": case "style": // already read, ignore break; case "g": case "svg": { var childGroup = ParseGroup(ns, element, currentTransformationMatrix); group.Childreen.Add(childGroup); break; } default: { var shape = shapeParser.Parse(element, ns, currentTransformationMatrix, cssStyleCascade, globalDefinitions); if (shape != null) { group.Childreen.Add(shape); } break; } } } cssStyleCascade.Pop(); return(group); }
/// <summary> /// Parse an SVG document fragment /// </summary> private GraphicGroup ParseSVG(XElement element, Matrix matrix, bool isTopLevelSvg) { cssStyleCascade.PushStyles(element); var(newViewBoxPushOnStack, viewBoxMatrix) = GetViewBoxMatrix(element, isTopLevelSvg); matrix = viewBoxMatrix * matrix; var elementTransformationMatrix = cssStyleCascade.GetTransformMatrixFromTop(); elementTransformationMatrix = elementTransformationMatrix * matrix; var group = ParseChildren(element, elementTransformationMatrix); group.Opacity = cssStyleCascade.GetNumberPercentFromTop("opacity", 1); clipping.SetClipPath(group, elementTransformationMatrix); if (newViewBoxPushOnStack) { cssStyleCascade.PopViewBox(); } cssStyleCascade.Pop(); return(group); }
/// <summary> /// Parse a single SVG shape /// </summary> public GraphicVisual Parse(XElement shape, XNamespace svgNamespace, Matrix currentTransformationMatrix, CssStyleCascade cssStyleCascade, Dictionary <string, XElement> globalDefinitions) { GraphicVisual graphicVisual = null; cssStyleCascade.PushStyles(shape); var transform = cssStyleCascade.GetPropertyFromTop("transform"); if (!string.IsNullOrEmpty(transform)) { var transformMatrix = TransformMatrixParser.GetTransformMatrix(transform); currentTransformationMatrix = transformMatrix * currentTransformationMatrix; } var geometry = GeometryParser.Parse(shape, currentTransformationMatrix); if (geometry != null) { var graphicPath = new GraphicPath(); graphicPath.Geometry = geometry; graphicVisual = graphicPath; this.svgNamespace = svgNamespace; this.globalDefinitions = globalDefinitions; this.currentTransformationMatrix = currentTransformationMatrix; this.cssStyleCascade = cssStyleCascade; SetFillAndStroke(shape, graphicPath); if (Clipping.IsClipPathSet(cssStyleCascade)) { // shapes don't support clipping, create a group around it var group = new GraphicGroup(); graphicVisual = group; group.Childreen.Add(graphicPath); Clipping.SetClipPath(group, currentTransformationMatrix, cssStyleCascade, globalDefinitions); } cssStyleCascade.Pop(); } return(graphicVisual); }
/// <summary> /// Parse a single SVG shape /// </summary> public GraphicVisual Parse(XElement shape, Matrix currentTransformationMatrix) { GraphicVisual graphicVisual = null; cssStyleCascade.PushStyles(shape); var transformMatrix = cssStyleCascade.GetTransformMatrixFromTop(); currentTransformationMatrix = transformMatrix * currentTransformationMatrix; var geometry = geometryParser.Parse(shape, currentTransformationMatrix); if (geometry != null) { var graphicPath = new GraphicPath(); graphicPath.Geometry = geometry; graphicVisual = graphicPath; brushParser.SetFillAndStroke(shape, graphicPath, currentTransformationMatrix); if (clipping.IsClipPathSet()) { // shapes don't support clipping, create a group around it var group = new GraphicGroup(); graphicVisual = group; group.Children.Add(graphicPath); clipping.SetClipPath(group, currentTransformationMatrix); } } cssStyleCascade.Pop(); return(graphicVisual); }