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