/** Copyright 2004-2008 Ricard Marxer This file is part of Geomerative. Geomerative is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. Geomerative is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with Geomerative. If not, see . */ package geomerative; import processing.core.PApplet; import processing.core.PConstants; import processing.core.PGraphics; import processing.data.XML; /** * @extended */ public class RSVG { public void draw(String filename, PGraphics g) { this.toGroup(filename).draw(g); } public void draw(String filename, PApplet p) { this.toGroup(filename).draw(p); } public void draw(String filename) { this.toGroup(filename).draw(); } public void saveShape(String filename, RShape shp) { String str = fromShape(shp); String[] strs = PApplet.split(str, "\n"); RG.parent().saveStrings(filename, strs); } public String fromShape(RShape shape) { String header = "\n\n\n"; return header + shapeToString(shape) + ""; } public void saveGroup(String filename, RGroup grp) { String str = fromGroup(grp); String[] strs = PApplet.split(str, "\n"); RG.parent().saveStrings(filename, strs); } public String fromGroup(RGroup group) { String header = "\n\n\n"; return header + groupToString(group) + ""; } public RGroup toGroup(String filename) { XML svg; svg = RG.parent().loadXML(filename); if (svg == null) return new RGroup(); if (!svg.getName().equals("svg")) { throw new RuntimeException("root is not , it's <" + svg.getName() + ">"); } return elemToGroup(svg); } public float unitsToPixels(String units, float originalPxSize) { // TODO: check if it is possible to know the dpi of a given PGraphics or device return unitsToPixels(units, originalPxSize, 72.0f/*Toolkit.getDefaultToolkit().getScreenResolution()*/); } public float unitsToPixels(String units, float originalPxSize, float dpi) { int chars; float multiplier; if (units.endsWith("em")) { chars = 2; multiplier = 1.0f; } else if (units.endsWith("ex")) { chars = 2; multiplier = 1.0f; } else if (units.endsWith("px")) { chars = 2; multiplier = 1.0f; } else if (units.endsWith("pt")) { chars = 2; multiplier = 1.25f; } else if (units.endsWith("pc")) { chars = 2; multiplier = 15f; } else if (units.endsWith("cm")) { chars = 2; multiplier = 35.43307f / 90.0f * dpi; } else if (units.endsWith("mm")) { chars = 2; multiplier = 3.543307f / 90.0f * dpi; } else if (units.endsWith("in")) { chars = 2; multiplier = dpi; } else if (units.endsWith("%")) { chars = 1; multiplier = originalPxSize / 100.0f; } else { chars = 0; multiplier = 1.0f; } return Float.parseFloat(units.substring(0, units.length()-chars)) * multiplier; } public RShape toShape(String filename) { XML svg; svg = RG.parent().loadXML(filename); if (svg == null) return new RShape(); if ( !svg.getName().equals("svg") ) { throw new RuntimeException("root is not , it's <" + svg.getName() + ">"); } RShape result = elemToCompositeShape(svg); result.origWidth = result.getWidth(); result.origHeight = result.getHeight(); if (svg.hasAttribute("width") && svg.hasAttribute("height")) { String widthStr = svg.getString("width").trim(); String heightStr = svg.getString("height").trim(); result.width = unitsToPixels(widthStr, result.origWidth); result.height = unitsToPixels(heightStr, result.origHeight); } else { result.width = result.origWidth; result.height = result.origHeight; } return result; } public RPolygon toPolygon(String filename) { return toGroup(filename).toPolygon(); } public RMesh toMesh(String filename) { return toGroup(filename).toMesh(); } public String groupToString(RGroup grp) { String result = ""; result += " 0) { result += "\n"; } for (int i=0; i0 && charline[i-1] != 'e' && charline[i-1] != 'E'){ charline=PApplet.splice(charline,' ',i); i++; } break; case ',': case '\n': case '\r': case '\t': charline[i] = ' '; break; } } String formatted = new String(charline); String[] tags = PApplet.splitTokens(formatted); //PApplet.println("formatted: " + formatted); //PApplet.println("tags: "); //PApplet.println(tags); //build points RPoint curp = new RPoint(); RPoint relp = new RPoint(); RPoint refp = new RPoint(); RPoint strp = new RPoint(); char command = 'a'; for (int i=0;i