package inkscape2physics; import java.io.*; import org.xml.sax.*; import org.xml.sax.helpers.*; import javax.xml.parsers.*; //import org.apache.xerces.parsers.SAXParser; /** converts SVG to Java code generating a (physics) level. * polygon = genBackground * circle = genCircle * rectangle = genRect * text = genSpecial */ public class InkscapetoPhysics extends DefaultHandler { StringBuffer code=new StringBuffer(); String layer=null; final static String ignorelayer="sky"; float [] layerofs=null; int g_level=0; private float[] multvm(float [] vec,float [] m) { return new float[] { m[0]*vec[0] + m[2]*vec[1] + m[4], m[1]*vec[0] + m[3]*vec[1] + m[5] }; } public void startElement(String namespaceURI, String localName, String qName, Attributes atts) { // some universally needed attributes float [] ofs = getTransform(atts.getValue("","transform")); int fillcol = getFillColor(atts.getValue("","style")); String colstr = "-1"; if (fillcol != -1) colstr = "0x"+Integer.toString(fillcol,16); //colstr= "new short[]{"+fillcol[0]+","+fillcol[1]+","+fillcol[2]+"}"; if (qName.equals("g")) { g_level++; //System.out.println("XXX"+g_level); if (g_level==1) { // get layer id (id) layer=atts.getValue("inkscape:label"); layerofs = ofs; //System.out.println("tr"+trans[0]+" "+trans[1]); return; } } if (layer==null) return; // paths outside layers are assumed to be // clip paths if (layer.equals(ignorelayer)) return; if (qName.equals("path") && atts.getValue("","sodipodi:type")!=null && atts.getValue("","sodipodi:type").equals("arc") ) { float x=Float.parseFloat(atts.getValue("","sodipodi:cx")); float y=Float.parseFloat(atts.getValue("","sodipodi:cy")); float width=Float.parseFloat(atts.getValue("","sodipodi:rx")); float height=Float.parseFloat(atts.getValue("","sodipodi:ry")); float [] vt = multvm(multvm(new float[]{x,y},layerofs),ofs); code.append("\t\tw.genCircle(\""+layer+"\","+colstr+","); code.append((vt[0])+"f,"+(vt[1])+ "f,"+((width+height)/2f)+"f);\n"); } else if (qName.equals("path")) { float x=0,y=0,newx,newy; String d=atts.getValue("","d"); //System.out.println("ofs "+ofs[0]+" "+ofs[1]); //code.append("\nd="+d); code.append("\t\tw.genPolygon(\""+layer+"\","+colstr+","); code.append("new float[] {"); // format of d: // 'm' ' ' ( ('l')? ',' )* ' ' 'z' String [] par = d.split("[ ,zZ]+"); int i=0; boolean relative=false; while (i