Пример #1
0
        //==========================================================================
        public SvgRectElement(SvgDocument document, SvgBaseElement parent, XElement rectElement)
            : base(document, parent, rectElement)
        {
            var x_attribute = rectElement.Attribute("x");

            if (x_attribute != null)
            {
                X = SvgCoordinate.Parse(x_attribute.Value);
            }

            var y_attribute = rectElement.Attribute("y");

            if (y_attribute != null)
            {
                Y = SvgCoordinate.Parse(y_attribute.Value);
            }

            var width_attribute = rectElement.Attribute("width");

            if (width_attribute != null)
            {
                Width = SvgLength.Parse(width_attribute.Value);
            }

            var height_attribute = rectElement.Attribute("height");

            if (height_attribute != null)
            {
                Height = SvgLength.Parse(height_attribute.Value);
            }

            var rx_attribute = rectElement.Attribute("rx");

            if (rx_attribute != null)
            {
                CornerRadiusX = SvgCoordinate.Parse(rx_attribute.Value);
            }

            var ry_attribute = rectElement.Attribute("ry");

            if (ry_attribute != null)
            {
                CornerRadiusY = SvgCoordinate.Parse(ry_attribute.Value);
            }
        }
Пример #2
0
        //==========================================================================
        public SvgPatternElement(SvgDocument document, SvgBaseElement parent, XElement patternElement)
            : base(document, parent, patternElement)
        {
            var pattern_transform_attribute = patternElement.Attribute("patternTransform");

            if (pattern_transform_attribute != null)
            {
                PatternTransform = SvgTransform.Parse(pattern_transform_attribute.Value);
            }

            var pattern_units_attribute = patternElement.Attribute("patternUnits");

            if (pattern_units_attribute != null)
            {
                switch (pattern_units_attribute.Value)
                {
                case "objectBoundingBox":
                    PatternUnits = SvgPatternUnits.ObjectBoundingBox;
                    break;

                case "userSpaceOnUse":
                    PatternUnits = SvgPatternUnits.UserSpaceOnUse;
                    break;

                default:
                    throw new NotImplementedException($"patternUnits value '{pattern_units_attribute.Value}' is no supported");
                }
            }

            var width_attribute = patternElement.Attribute("width");

            if (width_attribute != null)
            {
                Width = SvgLength.Parse(width_attribute.Value);
            }

            var height_attribute = patternElement.Attribute("height");

            if (height_attribute != null)
            {
                Height = SvgLength.Parse(height_attribute.Value);
            }
        }
Пример #3
0
        //==========================================================================
        public SvgRadialGradientElement(SvgDocument document, SvgBaseElement parent, XElement radialGradientElement)
            : base(document, parent, radialGradientElement)
        {
            var cx_attribute = radialGradientElement.Attribute("cx");

            if (cx_attribute != null)
            {
                CX = SvgCoordinate.Parse(cx_attribute.Value);
            }

            var cy_attribute = radialGradientElement.Attribute("cy");

            if (cy_attribute != null)
            {
                CY = SvgCoordinate.Parse(cy_attribute.Value);
            }

            var r_attribute = radialGradientElement.Attribute("r");

            if (r_attribute != null)
            {
                R = SvgCoordinate.Parse(r_attribute.Value);
            }

            var fx_attribute = radialGradientElement.Attribute("fx");

            if (fx_attribute != null)
            {
                FX = SvgCoordinate.Parse(fx_attribute.Value);
            }

            var fy_attribute = radialGradientElement.Attribute("fy");

            if (fy_attribute != null)
            {
                FY = SvgCoordinate.Parse(fy_attribute.Value);
            }
        }
Пример #4
0
        //==========================================================================
        public SvgStopElement(SvgDocument document, SvgBaseElement parent, XElement stopElement)
            : base(document, parent, stopElement)
        {
            var offset_attribute = stopElement.Attribute("offset");

            if (offset_attribute != null)
            {
                Offset = SvgLength.Parse(offset_attribute.Value);
            }

            var stop_color_attribute = stopElement.Attribute("stop-color");

            if (stop_color_attribute != null)
            {
                Color = SvgColor.Parse(stop_color_attribute.Value);
            }

            var stop_opacity_attribute = stopElement.Attribute("stop-opacity");

            if (stop_opacity_attribute != null)
            {
                Opacity = SvgLength.Parse(stop_opacity_attribute.Value);
            }
        }
Пример #5
0
        //==========================================================================
        public SvgCircleElement(SvgDocument document, SvgBaseElement parent, XElement circleElement)
            : base(document, parent, circleElement)
        {
            var cx_attribute = circleElement.Attribute("cx");

            if (cx_attribute != null)
            {
                CenterX = SvgCoordinate.Parse(cx_attribute.Value);
            }

            var cy_attribute = circleElement.Attribute("cy");

            if (cy_attribute != null)
            {
                CenterY = SvgCoordinate.Parse(cy_attribute.Value);
            }

            var r_attribute = circleElement.Attribute("r");

            if (r_attribute != null)
            {
                Radius = SvgLength.Parse(r_attribute.Value);
            }
        }
Пример #6
0
        //==========================================================================
        public SvgImageElement(SvgDocument document, SvgBaseElement parent, XElement imageElement)
            : base(document, parent, imageElement)
        {
            var x_attribute = imageElement.Attribute("x");

            if (x_attribute != null)
            {
                X = SvgCoordinate.Parse(x_attribute.Value);
            }

            var y_attribute = imageElement.Attribute("y");

            if (y_attribute != null)
            {
                Y = SvgCoordinate.Parse(y_attribute.Value);
            }

            var width_attribute = imageElement.Attribute("width");

            if (width_attribute != null)
            {
                Width = SvgLength.Parse(width_attribute.Value);
            }

            var height_attribute = imageElement.Attribute("height");

            if (height_attribute != null)
            {
                Height = SvgLength.Parse(height_attribute.Value);
            }

            var href_attribute = imageElement.Attribute(XName.Get("href", "http://www.w3.org/1999/xlink"));

            if (href_attribute != null)
            {
                var reference = href_attribute.Value.TrimStart();
                if (reference.StartsWith("data:"))
                {
                    reference = reference.Substring(5).TrimStart();
                    var index = reference.IndexOf(";");
                    if (index > -1)
                    {
                        var type = reference.Substring(0, index).Trim();
                        reference = reference.Substring(index + 1);

                        index = reference.IndexOf(",");
                        var encoding = reference.Substring(0, index).Trim();
                        reference = reference.Substring(index + 1).TrimStart();

                        switch (encoding)
                        {
                        case "base64":
                            Data = Convert.FromBase64String(reference);
                            break;

                        default:
                            throw new NotSupportedException($"Unsupported encoding: {encoding}");
                        }

                        var type_tokens = type.Split('/');
                        if (type_tokens.Length != 2)
                        {
                            throw new NotSupportedException($"Unsupported type: {type}");
                        }

                        type_tokens[0] = type_tokens[0].Trim();
                        if (type_tokens[0] != "image")
                        {
                            throw new NotSupportedException($"Unsupported type: {type}");
                        }

                        switch (type_tokens[1].Trim())
                        {
                        case "jpeg":
                            DataType = "jpeg";
                            break;

                        case "png":
                            DataType = "png";
                            break;

                        default:
                            throw new NotSupportedException($"Unsupported type: {type}");
                        }
                    }
                }
            }
        }
        //==========================================================================
        public SvgDrawableContainerBaseElement(SvgDocument document, SvgBaseElement parent, XElement drawableContainerElement)
            : base(document, parent, drawableContainerElement)
        {
            var opacity_attribute = drawableContainerElement.Attribute("opacity");

            if (opacity_attribute != null)
            {
                Opacity = SvgLength.Parse(opacity_attribute.Value);
            }

            var transform_attribute = drawableContainerElement.Attribute("transform");

            if (transform_attribute != null)
            {
                Transform = SvgTransform.Parse(transform_attribute.Value);
            }

            var clip_attribute = drawableContainerElement.Attribute("clip-path");

            if (clip_attribute != null)
            {
                ClipPath = SvgURL.Parse(clip_attribute.Value);
            }

            var filter_attribute = drawableContainerElement.Attribute("filter");

            if (filter_attribute != null)
            {
                Filter = SvgURL.Parse(filter_attribute.Value);
            }

            var mask_attribute = drawableContainerElement.Attribute("mask");

            if (mask_attribute != null)
            {
                Mask = SvgURL.Parse(mask_attribute.Value);
            }

            var display_attribute = drawableContainerElement.Attribute("display");

            if (display_attribute != null)
            {
                switch (display_attribute.Value)
                {
                case "inline":
                    Display = SvgDisplay.Inline;
                    break;

                case "block":
                    Display = SvgDisplay.Block;
                    break;

                case "list-item":
                    Display = SvgDisplay.ListItem;
                    break;

                case "run-in":
                    Display = SvgDisplay.RunIn;
                    break;

                case "compact":
                    Display = SvgDisplay.Compact;
                    break;

                case "marker":
                    Display = SvgDisplay.Marker;
                    break;

                case "table":
                    Display = SvgDisplay.Table;
                    break;

                case "inline-table":
                    Display = SvgDisplay.InlineTable;
                    break;

                case "table-row-group":
                    Display = SvgDisplay.TableRowGroup;
                    break;

                case "table-header-group":
                    Display = SvgDisplay.TableHeaderGroup;
                    break;

                case "table-footer-group":
                    Display = SvgDisplay.TableFooterGroup;
                    break;

                case "table-row":
                    Display = SvgDisplay.TableRow;
                    break;

                case "table-column-group":
                    Display = SvgDisplay.TableColumnGroup;
                    break;

                case "table-column":
                    Display = SvgDisplay.TableColumn;
                    break;

                case "table-cell":
                    Display = SvgDisplay.TableCell;
                    break;

                case "table-caption":
                    Display = SvgDisplay.TableCaption;
                    break;

                case "none":
                    Display = SvgDisplay.None;
                    break;

                default:
                    throw new NotImplementedException();
                }
            }
        }
        //==========================================================================
        public SvgDrawableBaseElement(SvgDocument document, SvgBaseElement parent, XElement drawableBaseElement)
            : base(document, parent, drawableBaseElement)
        {
            var opacity_attribute = drawableBaseElement.Attribute("opacity");

            if (opacity_attribute != null)
            {
                Opacity = SvgLength.Parse(opacity_attribute.Value);
            }

            var fill_opacity_attribute = drawableBaseElement.Attribute("fill-opacity");

            if (fill_opacity_attribute != null)
            {
                FillOpacity = SvgLength.Parse(fill_opacity_attribute.Value);
            }

            var stroke_opacity_attribute = drawableBaseElement.Attribute("stroke-opacity");

            if (stroke_opacity_attribute != null)
            {
                StrokeOpacity = SvgLength.Parse(stroke_opacity_attribute.Value);
            }

            var transform_attribute = drawableBaseElement.Attribute("transform");

            if (transform_attribute != null)
            {
                Transform = SvgTransform.Parse(transform_attribute.Value);
            }

            var fill_attribute = drawableBaseElement.Attribute("fill");

            if (fill_attribute != null)
            {
                Fill = SvgPaint.Parse(fill_attribute.Value);
            }

            var stroke_attribute = drawableBaseElement.Attribute("stroke");

            if (stroke_attribute != null)
            {
                Stroke = SvgPaint.Parse(stroke_attribute.Value);
            }

            var stroke_width_attribute = drawableBaseElement.Attribute("stroke-width");

            if (stroke_width_attribute != null)
            {
                StrokeWidth = SvgLength.Parse(stroke_width_attribute.Value);
            }

            var stroke_linecap_attribute = drawableBaseElement.Attribute("stroke-linecap");

            if (stroke_linecap_attribute != null)
            {
                StrokeLinecap = stroke_linecap_attribute.Value switch
                {
                    "butt" => SvgStrokeLinecap.Butt,
                    "round" => SvgStrokeLinecap.Round,
                    "square" => SvgStrokeLinecap.Square,
                    "inherit" => SvgStrokeLinecap.Inherit,
                    _ => throw new NotImplementedException()
                }
            }
            ;

            var stroke_linejoin_attribute = drawableBaseElement.Attribute("stroke-linejoin");

            if (stroke_linejoin_attribute != null)
            {
                StrokeLinejoin = stroke_linejoin_attribute.Value switch
                {
                    "miter" => SvgStrokeLinejoin.Miter,
                    "round" => SvgStrokeLinejoin.Round,
                    "bevel" => SvgStrokeLinejoin.Bevel,
                    "inherit" => SvgStrokeLinejoin.Inherit,
                    _ => throw new NotSupportedException()
                }
            }
            ;

            var stroke_miterlimit_attribute = drawableBaseElement.Attribute("stroke-miterlimit");

            if (stroke_miterlimit_attribute != null)
            {
                if (stroke_miterlimit_attribute.Value == "inherit")
                {
                    StrokeMiterlimit = double.NaN;
                }
                else
                {
                    var miterlimit = double.Parse(stroke_miterlimit_attribute.Value, CultureInfo.InvariantCulture.NumberFormat);
                    //if(miterlimit < 1)
                    //throw new NotSupportedException("A miterlimit less than 1 is not supported.");
                    StrokeMiterlimit = miterlimit;
                }
            }

            var stroke_dasharray_attribute = drawableBaseElement.Attribute("stroke-dasharray");

            if (stroke_dasharray_attribute != null)
            {
                if (stroke_dasharray_attribute.Value == "none")
                {
                    StrokeDasharray = null;
                }
                else if (stroke_dasharray_attribute.Value == "inherit")
                {
                    StrokeDasharray = Array.Empty <SvgLength>();
                }
                else
                {
                    var lengths = stroke_dasharray_attribute.Value.Split(',').Select(SvgLength.Parse).ToList();

                    if (lengths.Count % 2 == 1)
                    {
                        StrokeDasharray = new SvgLength[lengths.Count * 2];
                        for (var i = 0; i < lengths.Count - 1; ++i)
                        {
                            StrokeDasharray[i] = lengths[i];
                            StrokeDasharray[i + lengths.Count] = lengths[i];
                        }
                    }
                    else
                    {
                        StrokeDasharray = lengths.ToArray();
                    }
                }
            }

            var stroke_dashoffset_attribute = drawableBaseElement.Attribute("stroke-dashoffset");

            if (stroke_dashoffset_attribute != null)
            {
                StrokeDashoffset = SvgLength.Parse(stroke_dashoffset_attribute.Value);
            }

            var clip_attribute = drawableBaseElement.Attribute("clip-path");
            var clip_path      = clip_attribute?.Value.Trim();

            if (clip_path?.StartsWith("url") == true)
            {
                clip_path = clip_path.Substring(3).Trim();
                if (clip_path.StartsWith("(") && clip_path.EndsWith(")"))
                {
                    clip_path = clip_path.Substring(1, clip_path.Length - 2).Trim();
                    if (clip_path.StartsWith("#"))
                    {
                        ClipPath = clip_path.Substring(1);
                    }
                }
            }

            var filter_attribute = drawableBaseElement.Attribute("filter");
            var filter           = filter_attribute?.Value.Trim();

            if (filter?.StartsWith("url") == true)
            {
                filter = filter.Substring(3).Trim();
                if (filter.StartsWith("(") && filter.EndsWith(")"))
                {
                    filter = filter.Substring(1, filter.Length - 2).Trim();
                    if (filter.StartsWith("#"))
                    {
                        Filter = filter.Substring(1);
                    }
                }
            }

            var mask_attribute = drawableBaseElement.Attribute("mask");
            var mask           = mask_attribute?.Value.Trim();

            if (mask?.StartsWith("url") == true)
            {
                mask = mask.Substring(3).Trim();
                if (mask.StartsWith("(") && mask.EndsWith(")"))
                {
                    mask = mask.Substring(1, mask.Length - 2).Trim();
                    if (mask.StartsWith("#"))
                    {
                        Mask = mask.Substring(1);
                    }
                }
            }

            var display_attribute = drawableBaseElement.Attribute("display");

            if (display_attribute != null)
            {
                switch (display_attribute.Value)
                {
                case "inline":
                    Display = SvgDisplay.Inline;
                    break;

                case "block":
                    Display = SvgDisplay.Block;
                    break;

                case "list-item":
                    Display = SvgDisplay.ListItem;
                    break;

                case "run-in":
                    Display = SvgDisplay.RunIn;
                    break;

                case "compact":
                    Display = SvgDisplay.Compact;
                    break;

                case "marker":
                    Display = SvgDisplay.Marker;
                    break;

                case "table":
                    Display = SvgDisplay.Table;
                    break;

                case "inline-table":
                    Display = SvgDisplay.InlineTable;
                    break;

                case "table-row-group":
                    Display = SvgDisplay.TableRowGroup;
                    break;

                case "table-header-group":
                    Display = SvgDisplay.TableHeaderGroup;
                    break;

                case "table-footer-group":
                    Display = SvgDisplay.TableFooterGroup;
                    break;

                case "table-row":
                    Display = SvgDisplay.TableRow;
                    break;

                case "table-column-group":
                    Display = SvgDisplay.TableColumnGroup;
                    break;

                case "table-column":
                    Display = SvgDisplay.TableColumn;
                    break;

                case "table-cell":
                    Display = SvgDisplay.TableCell;
                    break;

                case "table-caption":
                    Display = SvgDisplay.TableCaption;
                    break;

                case "none":
                    Display = SvgDisplay.None;
                    break;

                default:
                    throw new NotImplementedException();
                }
            }

            var fill_rule_attribute = drawableBaseElement.Attribute("fill-rule");

            if (fill_rule_attribute != null)
            {
                FillRule = fill_rule_attribute.Value switch
                {
                    "nonzero" => SvgFillRule.Nonzero,
                    "evenodd" => SvgFillRule.Evenodd,
                    "inherit" => SvgFillRule.Inherit,
                    _ => throw new NotImplementedException()
                }
            }
            ;

            // color, color-interpolation, color-rendering

            // viewBox attribute
            // preserveAspectRatio attribute

            // overflow


            foreach (var element in from element in drawableBaseElement.Elements()
                     where element.Name.NamespaceName == "http://www.w3.org/2000/svg"
                     select element)
            {
                switch (element.Name.LocalName)
                {
                default:
                    throw new NotImplementedException($"Unhandled element: {element}");
                }
            }
        }