コード例 #1
0
ファイル: SvgReader.cs プロジェクト: pdbeckwith/NGraphics
        private void AddElement(IList <IDrawable> list, XElement e, Pen inheritPen, BaseBrush inheritBaseBrush)
        {
            Element element = null;

            var styleAttributedDictionary = e.Attributes().ToDictionary(k => k.Name.LocalName, v => v.Value);

            var pen       = _stylesParser.GetPen(styleAttributedDictionary);
            var baseBrush = _stylesParser.GetBrush(styleAttributedDictionary, defs, pen);

            var style = ReadString(e.Attribute("style"));

            if (!string.IsNullOrWhiteSpace(style))
            {
                ApplyStyle(style, ref pen, ref baseBrush);
            }

            pen       = pen ?? inheritPen;
            baseBrush = baseBrush ?? inheritBaseBrush;

            //var id = ReadString (e.Attribute ("id"));

            //
            // Elements
            //
            switch (e.Name.LocalName)
            {
            case "text":
            {
                var x    = _valuesParser.ReadNumber(e.Attribute("x"));
                var y    = _valuesParser.ReadNumber(e.Attribute("y"));
                var text = e.Value.Trim();
                var font = new Font();
                element = new Text(text, new Rect(new Point(x, y), new Size(double.MaxValue, double.MaxValue)), font,
                                   TextAlignment.Left, pen, baseBrush);
            }
            break;

            case "rect":
            {
                var x      = _valuesParser.ReadNumber(e.Attribute("x"));
                var y      = _valuesParser.ReadNumber(e.Attribute("y"));
                var width  = _valuesParser.ReadNumber(e.Attribute("width"));
                var height = _valuesParser.ReadNumber(e.Attribute("height"));
                element = new Rectangle(new Point(x, y), new Size(width, height), pen, baseBrush);
            }
            break;

            case "ellipse":
            {
                var cx = _valuesParser.ReadNumber(e.Attribute("cx"));
                var cy = _valuesParser.ReadNumber(e.Attribute("cy"));
                var rx = _valuesParser.ReadNumber(e.Attribute("rx"));
                var ry = _valuesParser.ReadNumber(e.Attribute("ry"));
                element = new Ellipse(new Point(cx - rx, cy - ry), new Size(2 * rx, 2 * ry), pen, baseBrush);
            }
            break;

            case "circle":
            {
                var cx = _valuesParser.ReadNumber(e.Attribute("cx"));
                var cy = _valuesParser.ReadNumber(e.Attribute("cy"));
                var rr = _valuesParser.ReadNumber(e.Attribute("r"));
                element = new Ellipse(new Point(cx - rr, cy - rr), new Size(2 * rr, 2 * rr), pen, baseBrush);
            }
            break;

            case "path":
            {
                var dA = e.Attribute("d");
                if (dA != null && !string.IsNullOrWhiteSpace(dA.Value))
                {
                    var p = new Path(pen, baseBrush);
                    SvgPathParser.Parse(p, dA.Value);
                    element = p;
                }
            }
            break;

            case "g":
            {
                var g = new Group();
                AddElements(g.Children, e.Elements(), pen, baseBrush);
                element = g;
            }
            break;

            case "use":
            {
                var href = ReadString(e.Attributes().FirstOrDefault(x => x.Name.LocalName == "href"));
                if (!string.IsNullOrWhiteSpace(href))
                {
                    XElement useE;
                    if (defs.TryGetValue(href.Trim().Replace("#", ""), out useE))
                    {
                        var useList = new List <IDrawable>();
                        AddElement(useList, useE, pen, baseBrush);
                        element = useList.OfType <Element>().FirstOrDefault();
                    }
                }
            }
            break;

            case "title":
                Graphic.Title = ReadString(e);
                break;

            case "description":
                Graphic.Description = ReadString(e);
                break;

            case "defs":
                // Already read in earlier pass
                break;

            case "namedview":
            case "metadata":
            case "SVGTestCase":
            case "switch":
                break;

            default:
                throw new NotSupportedException("SVG element \"" + e.Name.LocalName + "\" is not supported");
            }

            if (element != null)
            {
                element.Transform = ReadTransform(ReadString(e.Attribute("transform")));
                list.Add(element);
            }
        }
コード例 #2
0
ファイル: SvgReader.cs プロジェクト: yard/NGraphics
        private void AddElement(IList<IDrawable> list, XElement e, Pen inheritPen, BaseBrush inheritBaseBrush)
        {
            Element element = null;

              var styleAttributedDictionary = e.Attributes().ToDictionary(k => k.Name.LocalName, v => v.Value);

              var pen = _stylesParser.GetPen(styleAttributedDictionary);
              var baseBrush = _stylesParser.GetBrush(styleAttributedDictionary,defs, pen);

              var style = ReadString(e.Attribute("style"));

              if (!string.IsNullOrWhiteSpace(style))
              {
            ApplyStyle(style, ref pen, ref baseBrush);
              }

              pen = pen ?? inheritPen;
              baseBrush = baseBrush ?? inheritBaseBrush;

              //var id = ReadString (e.Attribute ("id"));

              //
              // Elements
              //
              switch (e.Name.LocalName)
              {
            case "text":
            {
              var x = _valuesParser.ReadNumber(e.Attribute("x"));
              var y = _valuesParser.ReadNumber(e.Attribute("y"));
              var text = e.Value.Trim();
              var font = new Font();
              element = new Text(text, new Rect(new Point(x, y), new Size(double.MaxValue, double.MaxValue)), font,
            TextAlignment.Left, pen, baseBrush);
            }
              break;
            case "rect":
            {
              var x = _valuesParser.ReadNumber(e.Attribute("x"));
              var y = _valuesParser.ReadNumber(e.Attribute("y"));
              var width = _valuesParser.ReadNumber(e.Attribute("width"));
              var height = _valuesParser.ReadNumber(e.Attribute("height"));
              element = new Rectangle(new Point(x, y), new Size(width, height), pen, baseBrush);
            }
              break;
            case "ellipse":
            {
              var cx = _valuesParser.ReadNumber(e.Attribute("cx"));
              var cy = _valuesParser.ReadNumber(e.Attribute("cy"));
              var rx = _valuesParser.ReadNumber(e.Attribute("rx"));
              var ry = _valuesParser.ReadNumber(e.Attribute("ry"));
              element = new Ellipse(new Point(cx - rx, cy - ry), new Size(2*rx, 2*ry), pen, baseBrush);
            }
              break;
            case "circle":
            {
              var cx = _valuesParser.ReadNumber(e.Attribute("cx"));
              var cy = _valuesParser.ReadNumber(e.Attribute("cy"));
              var rr = _valuesParser.ReadNumber(e.Attribute("r"));
              element = new Ellipse(new Point(cx - rr, cy - rr), new Size(2*rr, 2*rr), pen, baseBrush);
            }
              break;
            case "path":
            {
              var dA = e.Attribute("d");
              if (dA != null && !string.IsNullOrWhiteSpace(dA.Value))
              {
            var p = new Path(pen, baseBrush);
            SvgPathParser.Parse(p, dA.Value);
            element = p;
              }
            }
              break;
            case "g":
            {
              var g = new Group();
              AddElements(g.Children, e.Elements(), pen, baseBrush);
              element = g;
            }
              break;
            case "use":
            {
              var href = ReadString(e.Attributes().FirstOrDefault(x => x.Name.LocalName == "href"));
              if (!string.IsNullOrWhiteSpace(href))
              {
            XElement useE;
            if (defs.TryGetValue(href.Trim().Replace("#", ""), out useE))
            {
              var useList = new List<IDrawable>();
              AddElement(useList, useE, pen, baseBrush);
              element = useList.OfType<Element>().FirstOrDefault();
            }
              }
            }
              break;
            case "title":
              Graphic.Title = ReadString(e);
              break;
            case "description":
              Graphic.Description = ReadString(e);
              break;
            case "defs":
              // Already read in earlier pass
              break;
            case "namedview":
            case "metadata":
            case "SVGTestCase":
            case "switch":
            break;
            default:
              throw new NotSupportedException("SVG element \"" + e.Name.LocalName + "\" is not supported");
              }

              if (element != null) {
            element.Id = ReadString(e.Attribute("id"));
              }

              if (element != null)
              {
            element.Transform = ReadTransform(ReadString(e.Attribute("transform")));
            list.Add(element);
              }
        }