Exemplo n.º 1
0
        public static void Main(string[] args)
        {
                        #if false
            double[] m;
            double[] x0 = new double[2] {
                0, 0
            };
            double[] x1 = new double[2] {
                -1, 0
            };
            double[] x2 = new double[2] {
                +1, 0
            };
            double[] x3 = new double[2] {
                0, -1
            };
            double[] x4 = new double[2] {
                0, +1
            };
            double[] x5 = new double[2] {
                10, 5
            };
            m = SVGLoader.Geometry.circle_center(x1, x2, 1);
            m = SVGLoader.Geometry.circle_center(x3, x4, 1);
            m = SVGLoader.Geometry.circle_center(x1, x4, 1);
            m = SVGLoader.Geometry.circle_center(x0, x5, 10, false);

            string str = "scale ( -.1) ";
            //string s_re = "scale";
            ITransform tr = SVGLoader.Parser.transform(str);

            SVGLoader.Transform t1 = new SVGLoader.Transform(Math.PI);
            double[]            p0 = new double[3] {
                1, 2, 3
            };
            double[]             p1 = t1.calc(p0);
            double[]             p2 = t1.calc(1, 2, 3);
            SVGLoader.ITransform t2 = new SVGLoader.Translate(2, 2);
            t1.rotate(0);
            t1.scale();
            t1.scale(-1, 1, 3);
            p1 = t2.calc(p0);
            Stack <ITransform> t3 = new Stack <ITransform> ();
            t3.Push(t2);
            t3.Push(t1);
            p1 = (double[])p0.Clone();
            foreach (ITransform t in t3)
            {
                p1 = t.calc(p1);
            }
            MultiTransform t4 = new MultiTransform(t3);
            p1 = t4.calc(p0);
            Queue <ITransform> queue = new Queue <ITransform> ();
            queue.Enqueue(t1);
            MultiTransform t5 = new MultiTransform(queue);
            p1 = t5.calc(p0);
                        #endif
            Stdout             stdout   = new Stdout();
            SVGLoader.Graphics graphics = new SVGLoader.Graphics(stdout);
            //string path = "/home/jvater/c#-prj/data/test.svg";
            //string path = "/home/jvater/prj/svg-loader/data/Steg.svg";
            //string path = "/home/jvater/prj/svg-loader/data/test-00.svg";
            string path = "/home/jvater/prj/svg-loader/data/forum-01/Acanthus.svg";
            Console.WriteLine("Hello World!");
            //CamBam.ThisApplication.MsgBox("SVG: " + path);
            XmlDocument xml = new XmlDocument();
            xml.Load(path);
            XmlNodeList list = xml.GetElementsByTagName("defs");
            //XmlElement y = xml.GetElementById ("vol");
            XmlNode y = xml.SelectSingleNode("//*[@id='vol']");
            foreach (XmlElement x in list)
            {
                stdout.trace(1, x.ToString());
            }
            stdout.trace(0, "SVG loaded:" + path);
            graphics.draw(xml);
        }
Exemplo n.º 2
0
        //-------------------------------------------------------------------
        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();
        }