Example #1
0
		void AddElement (IList<Element> list, XElement e, Pen inheritPen, Brush inheritBrush)
		{
			//
			// Style
			//
			Element r = null;
            Style eStyle = new Style();
			ApplyStyle (e.Attributes ().ToDictionary (k => k.Name.LocalName, v => v.Value), eStyle);
			var style = ReadString (e.Attribute ("style"));
			if (!string.IsNullOrWhiteSpace (style)) {
				ApplyStyle (style, eStyle);
			}
            if (!eStyle.hasPen) eStyle.pen = inheritPen;
            if (!eStyle.hasBrush) eStyle.brush = inheritBrush;
            //var id = ReadString (e.Attribute ("id"));

            //
            // Elements
            //
            switch (e.Name.LocalName) {
			case "text":
				{
					var x = ReadNumber (e.Attribute ("x"));
					var y = ReadNumber (e.Attribute ("y"));
					var font = new Font ();
					var fontFamily = ReadTextFontFamily(e);
					if (!string.IsNullOrEmpty(fontFamily))
						font.Family = fontFamily;
					var fontSize = ReadTextFontSize(e);
					if (fontSize >= 0)
						font.Size = fontSize;
					TextAlignment textAlignment = ReadTextAlignment(e);
					var txt = new Text (new Rect (new Point (x, y), new Size (double.MaxValue, double.MaxValue)), font, textAlignment, eStyle.pen, eStyle.brush);
					ReadTextSpans (txt, e);
					r = txt;
				}
				break;
			case "rect":
				{
					var x = ReadNumber (e.Attribute ("x"));
					var y = ReadNumber (e.Attribute ("y"));
					var width = ReadNumber (e.Attribute ("width"));
					var height = ReadNumber (e.Attribute ("height"));
					var rx = ReadNumber (e.Attribute ("rx"));
					var ry = ReadNumber (e.Attribute ("ry"));
					if (ry == 0) {
						ry = rx;
					}
					r = new Rectangle (new Rect (new Point (x, y), new Size (width, height)), new Size (rx, ry), eStyle.pen, eStyle.brush);
				}
				break;
			case "ellipse":
				{
					var cx = ReadNumber (e.Attribute ("cx"));
					var cy = ReadNumber (e.Attribute ("cy"));
					var rx = ReadNumber (e.Attribute ("rx"));
					var ry = ReadNumber (e.Attribute ("ry"));
					r = new Ellipse (new Point (cx - rx, cy - ry), new Size (2 * rx, 2 * ry), eStyle.pen, eStyle.brush);
				}
				break;
			case "circle":
				{
					var cx = ReadNumber (e.Attribute ("cx"));
					var cy = ReadNumber (e.Attribute ("cy"));
					var rr = ReadNumber (e.Attribute ("r"));
					r = new Ellipse (new Point (cx - rr, cy - rr), new Size (2 * rr, 2 * rr), eStyle.pen, eStyle.brush);
				}
				break;

			case "path":
				{
					var dA = e.Attribute ("d");
					if (dA != null && !string.IsNullOrWhiteSpace (dA.Value)) {
						var p = new Path (eStyle.pen, eStyle.brush);
						ReadPath (p, dA.Value);
						r = p;
					}
				}
				break;
			case "polygon":
				{
					var pA = e.Attribute ("points");
					if (pA != null && !string.IsNullOrWhiteSpace (pA.Value)) {
						var path = new Path (eStyle.pen, eStyle.brush);
						ReadPoints (path, pA.Value, true);
						r = path;
					}
				}
				break;
			case "polyline":
				{
					var pA = e.Attribute ("points");
					if (pA != null && !string.IsNullOrWhiteSpace (pA.Value)) {
						var path = new Path (eStyle.pen, eStyle.brush);
						ReadPoints (path, pA.Value, false);
						r = path;
					}
				}
			break;
			case "g":
				{
					var g = new Group ();
					var groupId = e.Attribute("id");
					if (groupId != null && !string.IsNullOrEmpty(groupId.Value))
						g.Id = groupId.Value;
					AddElements (g.Children, e.Elements (), eStyle.pen, eStyle.brush);
					r = 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<Element> ();
							AddElement (useList, useE, eStyle.pen, eStyle.brush);
							r = useList.FirstOrDefault ();
						}
					}
				}
				break;
			case "title":
				Graphic.Title = ReadString (e);
				break;
			case "desc":
			case "description":
				Graphic.Description = ReadString (e);
				break;
			case "defs":
				// Already read in earlier pass
				break;
			case "namedview":
			case "metadata":
			case "image":
				// Ignore
				break;

				case "line":
				{
					var x1 = ReadNumber ( e.Attribute("x1") );
					var x2 = ReadNumber ( e.Attribute("x2") );
					var y1 = ReadNumber ( e.Attribute("y1") );
					var y2 = ReadNumber ( e.Attribute("y2") );
					var p = new Path (eStyle.pen, null);
					p.MoveTo (x1, y1);
					p.LineTo (x2, y2);
					r = p;
				}
				break;

				case "foreignObject":
				{
					var x = ReadNumber ( e.Attribute("x") );
					var y = ReadNumber ( e.Attribute("y") );
					var width = ReadNumber ( e.Attribute("width") );
					var height = ReadNumber ( e.Attribute("height") );
					r = new ForeignObject(new Point(x, y), new Size(width, height));
				}
				break;

				case "pgf":
				{
					var id = e.Attribute("id");
					System.Diagnostics.Debug.WriteLine("Ignoring pgf element" + (id != null ? ": '" + id.Value + "'" : ""));
				}
				break;

				case "switch":
				{
					// Evaluate requiredFeatures, requiredExtensions and systemLanguage
					foreach (var ee in e.Elements())
					{
						var requiredFeatures = ee.Attribute("requiredFeatures");
						var requiredExtensions = ee.Attribute("requiredExtensions");
						var systemLanguage = ee.Attribute("systemLanguage");
						// currently no support for any of these restrictions
						if (requiredFeatures == null && requiredExtensions == null && systemLanguage == null)
							AddElement (list, ee, eStyle.pen, eStyle.brush);
					}
				}
				break;


				// color definition that can be referred to by other elements
				case "linearGradient":
				break;


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

			if (r != null) {
                eStyle.Apply(r);
				r.Transform = ReadTransform (ReadString (e.Attribute ("transform")));
				var ida = e.Attribute("id");
				if (ida != null && !string.IsNullOrEmpty (ida.Value)) {
					r.Id = ida.Value.Trim ();
				}
				list.Add (r);
			}
		}
        void AddElement(IList<IDrawable> list, XElement e, Pen inheritPen, Brush inheritBrush)
        {
            //
            // Style
            //
            Element r = null;
            Pen pen = null;
            Brush brush = null;
            bool hasPen, hasBrush;
            ApplyStyle (e.Attributes ().ToDictionary (k => k.Name.LocalName, v => v.Value), ref pen, out hasPen, ref brush, out hasBrush);
            var style = ReadString (e.Attribute ("style"));
            if (!string.IsNullOrWhiteSpace (style)) {
                ApplyStyle (style, ref pen, out hasPen, ref brush, out hasBrush);
            }
            pen = hasPen ? pen : inheritPen;
            brush = hasBrush ? brush : inheritBrush;
            //var id = ReadString (e.Attribute ("id"));

            //
            // Elements
            //
            switch (e.Name.LocalName) {
            case "text":
                {
                    var x = ReadNumber (e.Attribute ("x"));
                    var y = ReadNumber (e.Attribute ("y"));
                    var text = e.Value.Trim ();
                    var fontFamilyAttribute = e.Attribute("font-family");
                    var font = new Font ();
                    //if (fontFamilyAttribute != null)
                    //	font.Family = fontFamilyAttribute.Value.Trim('\'');
                    var fontSizeAttribute = e.Attribute("font-size");
                    if (fontSizeAttribute != null)
                        font.Size = ReadNumber(fontSizeAttribute.Value);
                    r = new Text (text, new Rect (new Point (x, y), new Size (double.MaxValue, double.MaxValue)), font, TextAlignment.Left, pen, brush);
                }
                break;
            case "rect":
                {
                    var x = ReadNumber (e.Attribute ("x"));
                    var y = ReadNumber (e.Attribute ("y"));
                    var width = ReadNumber (e.Attribute ("width"));
                    var height = ReadNumber (e.Attribute ("height"));
                    r = new Rectangle (new Point (x, y), new Size (width, height), pen, brush);
                }
                break;
            case "ellipse":
                {
                    var cx = ReadNumber (e.Attribute ("cx"));
                    var cy = ReadNumber (e.Attribute ("cy"));
                    var rx = ReadNumber (e.Attribute ("rx"));
                    var ry = ReadNumber (e.Attribute ("ry"));
                    r = new Ellipse (new Point (cx - rx, cy - ry), new Size (2 * rx, 2 * ry), pen, brush);
                }
                break;
            case "circle":
                {
                    var cx = ReadNumber (e.Attribute ("cx"));
                    var cy = ReadNumber (e.Attribute ("cy"));
                    var rr = ReadNumber (e.Attribute ("r"));
                    r = new Ellipse (new Point (cx - rr, cy - rr), new Size (2 * rr, 2 * rr), pen, brush);
                }
                break;
            case "path":
                {
                    var dA = e.Attribute ("d");
                    if (dA != null && !string.IsNullOrWhiteSpace (dA.Value)) {
                        var p = new Path (pen, brush);
                        ReadPath (p, dA.Value);
                        r = p;
                    }
                }
                break;
            case "polygon":
                {
                    var pA = e.Attribute ("points");
                    if (pA != null && !string.IsNullOrWhiteSpace (pA.Value)) {
                        var path = new Path (pen, brush);
                        ReadPolygon (path, pA.Value);
                        r = path;
                    }
                }
                break;
            case "g":
                {
                    var g = new Group ();
                    AddElements (g.Children, e.Elements (), pen, brush);
                    r = 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, brush);
                            r = useList.OfType<Element> ().FirstOrDefault ();
                        }
                    }
                }
                break;
            case "title":
                Graphic.Title = ReadString (e);
                break;
            case "desc":
            case "description":
                Graphic.Description = ReadString (e);
                break;
            case "defs":
                // Already read in earlier pass
                break;
            case "namedview":
            case "metadata":
            case "image":
                // Ignore
                break;

                case "line":
                {
                    var x1 = ReadNumber ( e.Attribute("x1") );
                    var x2 = ReadNumber ( e.Attribute("x2") );
                    var y1 = ReadNumber ( e.Attribute("y1") );
                    var y2 = ReadNumber ( e.Attribute("y2") );
                    r = new Line(new Point(x1, y1), new Point(x2, y2), pen);
                }
                break;

                // color definition that can be referred to by other elements
                case "linearGradient":
                break;

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

            if (r != null) {
                r.Transform = ReadTransform (ReadString (e.Attribute ("transform")));
                list.Add (r);
            }
        }
Example #3
0
		public void Visit (Rectangle rectangle)
		{
			WriteStartElement ("rect", rectangle);
			w.Write (" x=\"{0}\" y=\"{1}\" width=\"{2}\" height=\"{3}\"",
				rectangle.Frame.X, rectangle.Frame.Y,
				rectangle.Frame.Width, rectangle.Frame.Height);
		}
Example #4
0
		public void EndVisit (Rectangle rectangle)
		{
			w.WriteLine (" />");
		}
Example #5
0
		public virtual void EndVisit (Rectangle rectangle)
		{
			EndVisitElement (rectangle);
		}