Пример #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);
			}
		}
Пример #2
0
		void ReadTextSpans (Text txt, XElement e)
		{
			foreach (XNode c in e.Nodes ()) {
				if (c.NodeType == XmlNodeType.Text) {
					txt.Spans.Add (new TextSpan (((XText)c).Value));
				} else if (c.NodeType == XmlNodeType.Element) {
					var ce = (XElement)c;
					if (ce.Name.LocalName == "tspan") {
						var tspan = new TextSpan (ce.Value);
						var x = ReadOptionalNumber (ce.Attribute ("x"));
						var y = ReadOptionalNumber (ce.Attribute ("y"));
						if (x.HasValue && y.HasValue) {
							tspan.Position = new Point (x.Value, y.Value);
						}

						var font = txt.Font;

						var ffamily = ReadTextFontFamily (ce);
						if (!string.IsNullOrWhiteSpace (ffamily)) {
							font = font.WithFamily (ffamily);
						}
						var fweight = ReadTextFontWeight (ce);
						if (!string.IsNullOrWhiteSpace (fweight)) {
							font = font.WithWeight (fweight);
						}
						var fstyle = ReadTextFontStyle (ce);
						if (!string.IsNullOrWhiteSpace (fstyle)) {
							font = font.WithStyle (fstyle);
						}
						var fsize = ReadTextFontSize (ce);
						if (fsize > 0) {
							font = font.WithSize (fsize);
						}

						if (font != txt.Font) {
							tspan.Font = font;
						}

						txt.Spans.Add (tspan);
					}
				}
			}
			txt.Trim ();
		}
        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);
            }
        }
Пример #4
0
		public void EndVisit (Text e)
		{
			w.WriteLine ("</text>");
		}
Пример #5
0
		public void Visit (Text text)
		{
			WriteStartElement ("text", text);
			w.Write (" x=\"{0}\" y=\"{1}\"",
				text.Frame.X, text.Frame.Y);
			if (text.Frame.Width != double.MaxValue || text.Frame.Height != double.MaxValue) {
				w.Write (" width=\"{0}\" height=\"{1}\"",
					text.Frame.Width, text.Frame.Height);
			}
			if (text.Font != null) {
				w.Write (" font-family=\"{0}\" font-size=\"{1}\"",
					text.Font.Name, text.Font.Size);
			}
			w.Write (">");
			w.Write (Escape (text.String));
		}
Пример #6
0
		public virtual void EndVisit (Text text)
		{
			EndVisitElement (text);
		}