001    package net.abriraqui.dm4.importexport;
002    
003    import javax.xml.stream.XMLStreamException;
004    import javax.xml.stream.XMLStreamWriter;
005    import javax.xml.stream.XMLOutputFactory;
006    
007    import java.io.FileWriter;
008    import java.io.IOException;
009    
010    class SVGRenderer {
011    
012        private XMLStreamWriter svgWriter;
013    
014        public SVGRenderer(String filename) {
015            try {
016                XMLOutputFactory xof = XMLOutputFactory.newInstance();
017                svgWriter = xof.createXMLStreamWriter(new FileWriter(filename));
018                svgWriter.writeStartDocument();
019                svgWriter.writeDTD("<!DOCTYPE svg PUBLIC \"-//W3C//DTD SVG 20000802//EN\" " 
020                                   + "\"http://www.w3.org/TR/2000/CR-SVG-20000802/DTD/svg-20000802.dtd\">");
021                svgWriter.writeStartElement("svg");
022                svgWriter.writeAttribute("width", "1200");
023                svgWriter.writeAttribute("height", "1200");
024                svgWriter.writeAttribute("xmlns","http://www.w3.org/2000/svg");
025                svgWriter.writeAttribute("xmlns:xlink","http://www.w3.org/1999/xlink");
026            } catch (Exception e) {
027                throw new RuntimeException("Construction SVGRenderer failed", e);
028            }
029        }
030    
031        public void closeDocument() throws XMLStreamException {
032            svgWriter.writeEndDocument(); // closes svg element
033            svgWriter.flush();
034            svgWriter.close();
035        }
036    
037    
038        public void line(int x1, int x2, int y1, int y2) throws XMLStreamException {
039            
040            svgWriter.writeEmptyElement("line");
041            svgWriter.writeAttribute("x1", Integer.toString(x1));
042            svgWriter.writeAttribute("x2", Integer.toString(x2));
043            svgWriter.writeAttribute("y1",  Integer.toString(y1));
044            svgWriter.writeAttribute("y2",  Integer.toString(y2));
045            svgWriter.writeAttribute("stroke", "lightgray");
046            svgWriter.writeAttribute("stroke-width", "3");
047    
048        }
049    
050        public void rectangle(int x, int y, int width, int height, String color) throws XMLStreamException {
051    
052            svgWriter.writeEmptyElement("rect");
053            svgWriter.writeAttribute("x", Integer.toString(x));
054            svgWriter.writeAttribute("y", Integer.toString(y));
055            svgWriter.writeAttribute("width", Integer.toString(width));
056            svgWriter.writeAttribute("height", Integer.toString(height));
057            svgWriter.writeAttribute("fill", color);   
058    
059        }
060    
061        public void text(int x, int y, String value, String color) throws XMLStreamException {
062            text(x, y, 0, 0, value, color, 0);
063         }
064    
065    
066        public void text(int x, int y, int x1, int y1, String value, String color,  double alpha) throws XMLStreamException {
067    
068             svgWriter.writeStartElement("text");
069             svgWriter.writeAttribute("x", Integer.toString(x));
070             svgWriter.writeAttribute("y", Integer.toString(y));
071             svgWriter.writeAttribute("font-size", "0.8em");
072             svgWriter.writeAttribute("fill", color);
073             svgWriter.writeAttribute("transform","translate(" + x1 + "," + y1 +")" + " " + "rotate(" + Double.toString(alpha) + "," + x + "," + y  + ")");
074             svgWriter.writeCharacters(value);
075             svgWriter.writeEndElement();
076         }
077    
078    
079    
080        public void image(int x, int y, int imgWidth, int imgHeight, String imgUri) throws XMLStreamException {
081    
082            svgWriter.writeEmptyElement("image");
083            svgWriter.writeAttribute("x", Integer.toString(x));
084            svgWriter.writeAttribute("y", Integer.toString(y));
085            svgWriter.writeAttribute("width", Integer.toString(imgWidth));
086            svgWriter.writeAttribute("height", Integer.toString(imgHeight));
087            svgWriter.writeAttribute("xlink:href", imgUri);
088    
089        }
090    }