private void InitRecursively(Group group, G g, StringDictionary parentStyle)
        {
            var style = StyleHelper.MergeStyles(parentStyle, g.Style);

            Init(group, g.Transform, style);
            AppendAll(group.Children, g.Children, style);
        }
        public DocumentWrapper <Vector> Convert([NotNull] DocumentWrapper <Svg> svgDocument)
        {
            Reset();

            _map = new Dictionary <string, Element>();
            FillMap(_map, svgDocument.Root);

            var vectorDocument = VectorDocumentWrapper.CreateFromFile(_blankVectorDrawablePath);

            var viewBox = svgDocument.Root.ViewBox;

            if (viewBox.X != 0 || viewBox.Y != 0)
            {
                throw new UnsupportedFormatException("X and y coordinates of viewBox must be zeros.");
            }
            vectorDocument.Root.ViewportWidth  = viewBox.Width;
            vectorDocument.Root.ViewportHeight = viewBox.Height;

            vectorDocument.Root.Width  = ConvertToDp(svgDocument.Root.Width, viewBox.Width);
            vectorDocument.Root.Height = ConvertToDp(svgDocument.Root.Height, viewBox.Height);

            AppendAll(vectorDocument.Root.Children, svgDocument.Root.Children, StyleHelper.MergeStyles(StyleHelper.InitialStyles, svgDocument.Root.Style));

            VectorOptimizer.Optimize(vectorDocument.Root);
            return(vectorDocument);
        }
        public DocumentWrapper <Vector> Convert([NotNull] DocumentWrapper <Svg> svgDocument)
        {
            Reset();

            _map = new Dictionary <string, Element>();
            FillMap(_map, svgDocument.Root);

            var vectorDocument = VectorDocumentWrapper.CreateFromFile(_blankVectorDrawablePath);

            var viewBox = svgDocument.Root.ViewBox;

            vectorDocument.Root.ViewportWidth  = viewBox.Width;
            vectorDocument.Root.ViewportHeight = viewBox.Height;

            vectorDocument.Root.Width  = ConvertToDp(svgDocument.Root.Width, viewBox.Width);
            vectorDocument.Root.Height = ConvertToDp(svgDocument.Root.Height, viewBox.Height);

            var style = StyleHelper.MergeStyles(StyleHelper.InitialStyles, svgDocument.Root.Style);

            vectorDocument.Root.Alpha = float.Parse(style["opacity"] ?? "1", CultureInfo.InvariantCulture);

            var group = vectorDocument.Root.Children.Append <Group>();

            group.TranslateX = -viewBox.X;
            group.TranslateY = -viewBox.Y;
            AppendAll(group.Children, svgDocument.Root.Children, style);

            VectorOptimizer.Optimize(vectorDocument.Root);
            return(vectorDocument);
        }
        private static IEnumerable <PathWithStyle> ExtractPaths(ElementWithChildren element, StringDictionary parentStyle)
        {
            var style = parentStyle;

            if (element is IStyleableElement)
            {
                style = StyleHelper.MergeStyles(parentStyle, ((IStyleableElement)element).Style);
            }

            foreach (var child in element.Children)
            {
                if (child is Path)
                {
                    var path = (Path)child;
                    yield return(new PathWithStyle(path, StyleHelper.MergeStyles(style, path.Style)));
                }
                if (child is ElementWithChildren)
                {
                    foreach (var x in ExtractPaths((ElementWithChildren)child, style))
                    {
                        yield return(x);
                    }
                }
            }
        }
        private void InitRecursively(Group group, G g, StringDictionary parentStyle)
        {
            var style = StyleHelper.MergeStyles(parentStyle, g.Style);

            Init(group, g.Transform, style);
            AppendAll(group.Children, g.Children, style);

            _isGroupOpacityUsed |= style["opacity"] != null;
        }
        private void Init(Group group, SvgPath svgPath, StringDictionary parentStyle)
        {
            var style = StyleHelper.MergeStyles(parentStyle, svgPath.Style);

            Init(group, svgPath.Transform, style);
            var vdPath = group.Children.Append <VdPath>();

            vdPath.PathData = PathDataFixer.Fix(svgPath.D);

            foreach (string key in style.Keys)
            {
                var value = style[key];
                switch (key)
                {
                case "fill":
                    if (value.StartsWith("#"))
                    {
                        vdPath.FillColor = value;
                    }
                    break;

                case "stroke":
                    if (value.StartsWith("#"))
                    {
                        vdPath.StrokeColor = value;
                    }
                    break;

                case "stroke-width":
                    vdPath.StrokeWidth = float.Parse(value, CultureInfo.InvariantCulture);
                    break;

                case "stroke-opacity":
                    vdPath.StrokeAlpha = float.Parse(value, CultureInfo.InvariantCulture);
                    break;

                case "fill-opacity":
                    vdPath.FillAlpha = float.Parse(value, CultureInfo.InvariantCulture);
                    break;

                case "stroke-linecap":
                    vdPath.StrokeLineCap = value;
                    break;

                case "stroke-linejoin":
                    vdPath.StrokeLineJoin = value;
                    break;

                case "stroke-miterlimit":
                    vdPath.StrokeMiterLimit = value;
                    break;

                case "fill-rule":
                    SetFillType(vdPath, value);
                    break;

                case "stroke-dasharray":
                    _isStrokeDasharrayUsed |= value != "none";
                    break;
                }
            }
        }
Ejemplo n.º 7
0
        private void Init(Group outerGroup, Group innerGroup, SvgPath svgPath, StringDictionary parentStyle)
        {
            var style = StyleHelper.MergeStyles(parentStyle, svgPath.Style);

            Init(outerGroup, innerGroup, svgPath.Transform, style);
            var fillPath   = innerGroup.Children.Append <VdPath>();
            var strokePath = fillPath;

            fillPath.PathData = svgPath.D;
            if (style.ContainsKey("fill") && SetFillType(fillPath, style["fill-rule"]))
            {
                strokePath          = innerGroup.Children.Append <VdPath>();
                strokePath.PathData = PathDataFixer.Fix(svgPath.D);
            }
            fillPath.PathData = PathDataFixer.Fix(fillPath.PathData);

            foreach (string key in style.Keys)
            {
                var value = style[key];
                switch (key)
                {
                case "fill":
                    if (value.StartsWith("#"))
                    {
                        fillPath.FillColor = value;
                    }
                    break;

                case "stroke":
                    if (value.StartsWith("#"))
                    {
                        strokePath.StrokeColor = value;
                    }
                    break;

                case "stroke-width":
                    strokePath.StrokeWidth = (float)UnitConverter.ConvertToPx(value, 0);
                    break;

                case "stroke-opacity":
                    strokePath.StrokeAlpha *= float.Parse(value, CultureInfo.InvariantCulture);
                    break;

                case "fill-opacity":
                    fillPath.FillAlpha *= float.Parse(value, CultureInfo.InvariantCulture);
                    break;

                case "opacity":
                    strokePath.StrokeAlpha *= float.Parse(value, CultureInfo.InvariantCulture);
                    fillPath.FillAlpha     *= float.Parse(value, CultureInfo.InvariantCulture);
                    break;

                case "stroke-linecap":
                    strokePath.StrokeLineCap = value;
                    break;

                case "stroke-linejoin":
                    strokePath.StrokeLineJoin = value;
                    break;

                case "stroke-miterlimit":
                    strokePath.StrokeMiterLimit = value;
                    break;

                case "stroke-dasharray":
                    _isStrokeDasharrayUsed |= value != "none";
                    break;
                }
            }
        }