Пример #1
0
        /// <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);
        }
Пример #2
0
        /// <summary>
        /// Parse an SVG given as XElement root
        /// </summary>
        public GraphicVisual ParseRoot(XElement root)
        {
            var        nameSpaceAttributes       = root.Attributes().Where(a => a.IsNamespaceDeclaration);
            var        defaultNamespaceAttribute = root.Attributes().Where(a => a.IsNamespaceDeclaration && a.Name.Namespace == XNamespace.None).FirstOrDefault();
            XNamespace defaultNamespace          = defaultNamespaceAttribute.Value;

            Matrix currentTransformationMatrix = Matrix.Identity;

            cssStyleCascade = new CssStyleCascade(root);

            var svgViewBox = new SvgViewBox
            {
                ViewBox = new Rect(0, 0, 100, 100),
                Align   = "none",
                Slice   = "meet"
            };

            cssStyleCascade.PushViewBox(svgViewBox);

            doubleParser = new DoubleParser(cssStyleCascade);
            ReadGlobalDefinitions(root);

            var brushParser    = new BrushParser(defaultNamespace, cssStyleCascade, globalDefinitions, doubleParser);
            var geometryParser = new GeometryParser(doubleParser);

            var geometryTextParser = new GeometryTextParser(cssStyleCascade, doubleParser);

            clipping    = new Clipping(cssStyleCascade, globalDefinitions, geometryParser, geometryTextParser);
            shapeParser = new ShapeParser(cssStyleCascade, brushParser, geometryParser, clipping);
            textParser  = new TextParser(cssStyleCascade, doubleParser, brushParser, clipping);

            GraphicVisual visual = ParseSVG(root, currentTransformationMatrix, true);

            visual = OptimizeVisual.Optimize(visual);

            return(visual);
        }