//==========================================================================
        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);
            }
        }
        //==========================================================================
        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}");
                }
            }
        }
        //==========================================================================
        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();
                }
            }
        }
Example #4
0
        //==========================================================================
        public SvgGradientBaseElement(SvgDocument document, SvgBaseElement parent, XElement gradientElement)
            : base(document, parent, gradientElement)
        {
            var gradient_units_attribute = gradientElement.Attribute("gradientUnits");

            if (gradient_units_attribute != null)
            {
                switch (gradient_units_attribute.Value)
                {
                case "objectBoundingBox":
                    GradientUnits = SvgGradientUnits.ObjectBoundingBox;
                    break;

                case "userSpaceOnUse":
                    GradientUnits = SvgGradientUnits.UserSpaceOnUse;
                    break;

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

            var gradient_transform_attribute = gradientElement.Attribute("gradientTransform");

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

            var spread_method_attribute = gradientElement.Attribute("spreadMethod");

            if (spread_method_attribute != null)
            {
                switch (spread_method_attribute.Value)
                {
                case "pad":
                    SpreadMethod = SvgSpreadMethod.Pad;
                    break;

                case "reflect":
                    SpreadMethod = SvgSpreadMethod.Reflect;
                    break;

                case "repeat":
                    SpreadMethod = SvgSpreadMethod.Repeat;
                    break;
                }
            }



            foreach (var element in from element in gradientElement.Elements()
                     where element.Name.NamespaceName == "http://www.w3.org/2000/svg"
                     select element)
            {
                switch (element.Name.LocalName)
                {
                case "stop":
                    Stops.Add(new SvgStopElement(Document, this, element));
                    break;

                default:
                    throw new NotImplementedException($"Unhandled element: {element}");
                }
            }
        }