From f304a87dfbcccc8dbbab01cf3882dacc08980f6f Mon Sep 17 00:00:00 2001 From: benfry Date: Sat, 11 Oct 2008 15:31:49 +0000 Subject: [PATCH] the pshape train is comin' down the track --- build/macosx/dist.sh | 2 - build/macosx/make.sh | 34 +-------------- core/src/processing/core/PApplet.java | 23 +++++++++++ core/src/processing/core/PShape.java | 55 +++++++++++++++++-------- core/src/processing/core/PShapeSVG.java | 43 ++++++++++++------- 5 files changed, 91 insertions(+), 66 deletions(-) diff --git a/build/macosx/dist.sh b/build/macosx/dist.sh index 388229c5a..756fffe39 100755 --- a/build/macosx/dist.sh +++ b/build/macosx/dist.sh @@ -44,8 +44,6 @@ cp -r ../../serial processing/libraries/ cp -r ../../video processing/libraries/ cp -r ../../pdf processing/libraries/ cp -r ../../dxf processing/libraries/ -cp -r ../../xml processing/libraries/ -cp -r ../../candy processing/libraries/ # get ds_store file (!) cp dist/DS_Store processing/.DS_Store diff --git a/build/macosx/make.sh b/build/macosx/make.sh index 03a2b96af..40ea9fb8d 100755 --- a/build/macosx/make.sh +++ b/build/macosx/make.sh @@ -31,8 +31,6 @@ else cp -r ../../video work/libraries/ cp -r ../../pdf work/libraries/ cp -r ../../dxf work/libraries/ - cp -r ../../xml work/libraries/ - cp -r ../../candy work/libraries/ # to have a copy of this guy around for messing with echo Copying Processing.app... @@ -62,9 +60,9 @@ cd core perl preproc.pl mkdir -p bin -javac -source 1.5 -target 1.5 -d bin src/processing/core/*.java +javac -source 1.5 -target 1.5 -d bin src/processing/core/*.java src/processing/xml/*.java rm -f ../build/macosx/work/lib/core.jar -cd bin && zip -rq ../../build/macosx/work/lib/core.jar processing/core/*.class && cd .. +cd bin && zip -rq ../../build/macosx/work/lib/core.jar processing/core/*.class processing/xml/*.class && cd .. # head back to "processing/app" cd ../app @@ -233,33 +231,5 @@ mkdir -p $LIBRARIES/dxf/library/ cp library/dxf.jar $LIBRARIES/dxf/library/ -# XML LIBRARY -echo Building XML library... -cd ../xml -mkdir -p bin -$JAVAC \ - -classpath "$CLASSPATH" \ - -d bin src/processing/xml/*.java -rm -f library/xml.jar -find bin -name "*~" -exec rm -f {} ';' -cd bin && zip -rq ../library/xml.jar processing/xml/*.class && cd .. -mkdir -p $LIBRARIES/xml/library/ -cp library/xml.jar $LIBRARIES/xml/library/ - - -# CANDY SVG LIBRARY -echo Building Candy SVG library... -cd ../candy -mkdir -p bin -$JAVAC \ - -classpath "../xml/library/xml.jar:$CLASSPATH" \ - -d bin src/processing/candy/*.java -rm -f library/candy.jar -find bin -name "*~" -exec rm -f {} ';' -cd bin && zip -rq ../library/candy.jar processing/candy/*.class && cd .. -mkdir -p $LIBRARIES/candy/library/ -cp library/candy.jar $LIBRARIES/candy/library/ - - echo echo Done. \ No newline at end of file diff --git a/core/src/processing/core/PApplet.java b/core/src/processing/core/PApplet.java index 5391cf8f7..7acf760b9 100644 --- a/core/src/processing/core/PApplet.java +++ b/core/src/processing/core/PApplet.java @@ -3455,6 +3455,23 @@ public class PApplet extends Applet + ////////////////////////////////////////////////////////////// + + // SHAPE I/O + + + /** + * Load a geometry from a file as a PShape. Currently only supports SVG data. + */ + public PShape loadShape(String filename) { + if (filename.toLowerCase().endsWith(".svg")) { + return new PShapeSVG(this, filename); + } + return null; + } + + + ////////////////////////////////////////////////////////////// // FONT I/O @@ -6721,6 +6738,12 @@ public class PApplet extends Applet } + public void vertex(float[] v) { + if (recorder != null) recorder.vertex(v); + g.vertex(v); + } + + public void vertex(float x, float y, float u, float v) { if (recorder != null) recorder.vertex(x, y, u, v); g.vertex(x, y, u, v); diff --git a/core/src/processing/core/PShape.java b/core/src/processing/core/PShape.java index c2ff093d7..9540cf67b 100644 --- a/core/src/processing/core/PShape.java +++ b/core/src/processing/core/PShape.java @@ -25,14 +25,34 @@ package processing.core; import java.util.HashMap; -// take a look at the obj loader to see how this fits with things -// PShape.line() PShape.ellipse()? -// PShape s = beginShape() -// line() -// endShape(s) - -abstract public class PShape implements PConstants { +/** + * In-progress class to handle shape data, currently to be considered of + * alpha or beta quality. Major structural work may be performed on this class + * after the release of Processing 1.0. Such changes may include: + * + * + * + *

For the time being, this class and its shape() and loadShape() friends in + * PApplet exist as placeholders for more exciting things to come. If you'd + * like to work with this class, make a subclass (see how PShapeSVG works) + * and you can play with its internal methods all you like.

+ * + *

Library developers are encouraged to create PShape objects when loading + * shape data, so that they can eventually hook into the bounty that will be + * the PShape interface, and the ease of loadShape() and shape().

+ */ +public class PShape implements PConstants { protected String name; protected HashMap nameTable; @@ -252,6 +272,7 @@ abstract public class PShape implements PConstants { matrix.m30, matrix.m31, matrix.m32, matrix.m33); } */ + g.pushMatrix(); g.applyMatrix(matrix); } @@ -343,6 +364,7 @@ abstract public class PShape implements PConstants { * Draws the SVG document. */ public void drawImpl(PGraphics g) { + //System.out.println("drawing " + family); if (family == GROUP) { drawGroup(g); } else if (family == PRIMITIVE) { @@ -561,19 +583,16 @@ abstract public class PShape implements PConstants { } - public PShape getChild(String who) { - if (name != null && name.equals(who)) { + public PShape getChild(String target) { + if (name != null && name.equals(target)) { return this; } if (nameTable != null) { - for (String n : nameTable.keySet()) { - if (n.equals(name)) { - return nameTable.get(name); - } - } + PShape found = nameTable.get(target); + if (found != null) return found; } for (int i = 0; i < childCount; i++) { - PShape found = children[i].getChild(name); + PShape found = children[i].getChild(target); if (found != null) return found; } return null; @@ -584,12 +603,12 @@ abstract public class PShape implements PConstants { * Same as getChild(name), except that it first walks all the way up the * hierarchy to the farthest parent, so that children can be found anywhere. */ - public PShape findChild(String name) { + public PShape findChild(String target) { if (parent == null) { - return getChild(name); + return getChild(target); } else { - return parent.findChild(name); + return parent.findChild(target); } } diff --git a/core/src/processing/core/PShapeSVG.java b/core/src/processing/core/PShapeSVG.java index 141d83ce4..c6084c292 100644 --- a/core/src/processing/core/PShapeSVG.java +++ b/core/src/processing/core/PShapeSVG.java @@ -244,12 +244,7 @@ public class PShapeSVG extends PShape { } element = properties; - name = properties.getStringAttribute("id"); -// if (name != null) { -// table.put(name, this); -// //System.out.println("now parsing " + id); -// } String displayStr = properties.getStringAttribute("display", "inline"); visible = !displayStr.equals("none"); @@ -285,43 +280,52 @@ public class PShapeSVG extends PShape { */ protected PShape parseChild(XMLElement elem) { String name = elem.getName(); - PShapeSVG shape = new PShapeSVG(this, elem); + PShapeSVG shape = null; if (name.equals("g")) { //return new BaseObject(this, elem); - + shape = new PShapeSVG(this, elem); + } else if (name.equals("defs")) { // generally this will contain gradient info, so may // as well just throw it into a group element for parsing //return new BaseObject(this, elem); - + shape = new PShapeSVG(this, elem); + } else if (name.equals("line")) { //return new Line(this, elem); //return new BaseObject(this, elem, LINE); + shape = new PShapeSVG(this, elem); shape.parseLine(); } else if (name.equals("circle")) { //return new BaseObject(this, elem, ELLIPSE); + shape = new PShapeSVG(this, elem); shape.parseEllipse(true); } else if (name.equals("ellipse")) { //return new BaseObject(this, elem, ELLIPSE); + shape = new PShapeSVG(this, elem); shape.parseEllipse(false); } else if (name.equals("rect")) { //return new BaseObject(this, elem, RECT); + shape = new PShapeSVG(this, elem); shape.parseRect(); } else if (name.equals("polygon")) { //return new BaseObject(this, elem, POLYGON); + shape = new PShapeSVG(this, elem); shape.parsePoly(true); } else if (name.equals("polyline")) { //return new BaseObject(this, elem, POLYGON); + shape = new PShapeSVG(this, elem); shape.parsePoly(false); } else if (name.equals("path")) { //return new BaseObject(this, elem, PATH); + shape = new PShapeSVG(this, elem); shape.parsePath(); } else if (name.equals("radialGradient")) { @@ -343,7 +347,7 @@ public class PShapeSVG extends PShape { } else { PGraphics.showWarning("Ignoring <" + name + "> tag."); } - return null; + return shape; } @@ -472,6 +476,8 @@ public class PShapeSVG extends PShape { // use whitespace constant to get rid of extra spaces and CR or LF String[] pathDataKeys = PApplet.splitTokens(pathBuffer.toString(), "|" + WHITESPACE); + vertices = new float[pathDataKeys.length][2]; + vertexCodes = new int[pathDataKeys.length]; float cx = 0; float cy = 0; @@ -1035,7 +1041,6 @@ public class PShapeSVG extends PShape { // . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . - static class Gradient extends PShapeSVG { AffineTransform transform; @@ -1423,14 +1428,24 @@ public class PShapeSVG extends PShape { * beneath them can be used here. *
 	 * // This code grabs "Layer 3" and the shapes beneath it.
-	 * SVG layer3 = svg.get("Layer 3");
+	 * SVG layer3 = svg.getChild("Layer 3");
 	 * 
*/ public PShape getChild(String name) { PShape found = super.getChild(name); - if (found != null) return found; - // otherwise try with underscores instead of spaces - return super.getChild(name.replace(' ', '_')); + if (found == null) { + // Otherwise try with underscores instead of spaces + // (this is how Illustrator handles spaces in the layer names). + found = super.getChild(name.replace(' ', '_')); + } + // Set bounding box based on the parent bounding box + if (found != null) { + found.x = this.x; + found.y = this.y; + found.width = this.width; + found.height = this.height; + } + return found; }