//------------------------------------------------------------------- public static ITransform transform(string str) { str = str.Trim(); if (str == null || str == "") { return(null); } if (_re_scale == null) { _re_scale = new Regex(@"(matrix|translate|scale)\s*\([^a-zA-Z]+\)"); } //_re_scale = new Regex ("((translate|scale)\\s*\\(.*\\))"); MultiTransform tr = new MultiTransform(); str = str.Replace(",", " "); Regex re = _re_scale; for (Match match = re.Match(str); match.Success; match = match.NextMatch()) { if (match.Value.Length == 0) { continue; } string item = match.Value; double[] vec = numbers(item); if (item.StartsWith("scale") && !Scale.nop(vec)) { tr._items.Push(new Scale(vec)); } if (item.StartsWith("translate") && !Translate.nop(vec)) { tr._items.Push(new Translate(vec)); } if (item.StartsWith("matrix")) { tr._items.Push(new Transform(vec)); } } if (tr._items.Count < 1) { return(null); } if (tr._items.Count == 1) { return(tr._items.Pop()); } return(tr); }
//------------------------------------------------------------------- void draw(XmlNode xml_root, string id) { string name = xml_root.Name.ToLower(); ITransform tr = null; if (name == "svg") { string w = attribute(xml_root, "width", ""); string h = attribute(xml_root, "height", ""); string box = attribute(xml_root, "viewBox", ""); double w_mm = Parser.unit_mm(w); double h_mm = Parser.unit_mm(h); double[] vbox = Parser.numbers(box); if (vbox.Length == 4 && !ViewBox.nop(vbox, w_mm, h_mm)) { tr = new Scale(+w_mm / vbox [2], -h_mm / vbox [3]); } else { tr = new Scale(+1, -1); } //tr = new ViewBox (vbox [0], vbox [1], vbox [2] / w_mm, vbox [3] / h_mm); _transforms.Push(tr); } else { string str = attribute(xml_root, "transform", ""); tr = Parser.transform(str); _transforms.Push(tr); } //TODO: ensure balanced Push(tr)/Pop using (var aTR = new TransformLevel(_transforms, null)) { trace("transform stack (level): {0}", _transforms.Count); } ; trace("transform stack: {0}", _transforms.Count); // draw element Entity elem; if (name == "#comment") { trace("comment"); } else if (name == "svg") { trace("svg (root element)"); } else if (name == "g") { trace("g = group"); //_output.layer (id); } else if (name == "line") { double x1 = attribute(xml_root, "x1", 0); double y1 = attribute(xml_root, "y1", 0); double x2 = attribute(xml_root, "x2", 0); double y2 = attribute(xml_root, "y2", 0); double[] p1 = calc(x1, y1); double[] p2 = calc(x2, y2); trace("line a=[{0} {1}] b=[{2},{3}]", x1, y1, x2, y2); //trace(">> line: a=[{0} {1}] b=[{2},{3}].".Format(x1, y1, x2, y2)); elem = new Line(new Point2F(p1 [0], p1 [1]), new Point2F(p2 [0], p2 [1])); _output.draw(elem, id); } else if (name == "circle") { double x = attribute(xml_root, "cx", 0); double y = attribute(xml_root, "cy", 0); double r = attribute(xml_root, "r", 0); trace("circle x={0} y={1} r={2}", x, y, r); double[] cp = calc(x, y); double[] rr = scale(r, r); if (rr [0] != rr [1]) { trace("Not (yet) supportet: scale x<>y"); } elem = new Circle(new Point2F(cp [0], cp [1]), rr [0]); _output.draw(elem, id); } else if (name == "rect") { double x = attribute(xml_root, "x", 0); double y = attribute(xml_root, "y", 0); double w = attribute(xml_root, "width", 0); double h = attribute(xml_root, "height", 0); trace("rect w={0} h={1}", w, h); double[] p = calc(x, y); double[] d = scale(w, h); Point3F point = new Point3F(p [0], p [1], 0); elem = new PolyRectangle(point, d [0], d [1]); _output.draw(elem, id); } else if (name == "path") { string d = attribute(xml_root, "d", ""); string n = attribute(xml_root, "bezier_nodenumber", "10"); _pathparser.setNodeNumber(Int32.Parse(n)); //TODO: trace ("path d={0}", d); _pathparser.draw(d, id); } else if (name == "use") { double x = attribute(xml_root, "x", 0); double y = attribute(xml_root, "y", 0); string link = attribute(xml_root, "xlink:href", ""); trace("use x={0} y={1} xlink:href=[{2}]", x, y, link); if (!String.IsNullOrEmpty(link) && link [0] == '#') { link = link.Substring(1); } string xpath = String.Format("//*[@id='{0}']", link); XmlElement xml = (XmlElement)_xml.SelectSingleNode(xpath); if (xml == null) { trace("use [{0}]?!", link); } else { tr = null; double[] v = new double[2] { x, y }; if (!Translate.nop(v)) { tr = new Translate(x, y); } _transforms.Push(tr); draw(xml, id); _transforms.Pop(); } } else { trace("unknown: [{0}]", name); } // draw childs foreach (XmlNode xml_node in xml_root.ChildNodes) { draw(xml_node); } // transformation - switch back _transforms.Pop(); }