diff --git a/core/PApplet.java b/core/PApplet.java index bd564edde..1ce18b123 100644 --- a/core/PApplet.java +++ b/core/PApplet.java @@ -2104,14 +2104,13 @@ public class PApplet extends Applet /** - * This version of save() is an override of PImage.save(), - * rather than calling g.save(). This version properly saves - * the image to the applet folder (whereas PImage.save() and - * the inherited PGraphics.save() don't know where to put things). + * Intercepts any relative paths to make them absolute (relative + * to the sketch folder) before passing to save() in PImage. + * (Changed in 0100) */ - //public void save(String filename) { - //g.save(savePath(filename)); - //} + public void save(String filename) { + g.save(savePath(filename)); + } /** @@ -3665,6 +3664,14 @@ public class PApplet extends Applet * libraries to save to the sketch folder. */ public String sketchPath(String where) { + if (sketchPath == null) { + throw new RuntimeException("Applet not inited properly, " + + "the sketch path could not be determined."); + } + // isAbsolute() could throw an access exception, but so will writing + // to the local disk using the sketch path, so this is safe here. + if (new File(where).isAbsolute()) return where; + return sketchPath + File.separator + where; } @@ -3683,11 +3690,19 @@ public class PApplet extends Applet /** * Return a full path to an item in the data folder. + *

+ * In this method, the data path is defined not as the applet's actual + * data path, but a folder titled "data" in the sketch's working + * directory. This is because in an application, the "data" folder is + * exported as part of the jar file, and it's not as though you're gonna + * write into the jar file itself. If you need to get things out of + * the jar file, you should use openStream(). */ public String dataPath(String where) { - //if (sketchPath == null) { // can't be, set by init() - //return "data" + File.separator + where; - //} + // isAbsolute() could throw an access exception, but so will writing + // to the local disk using the sketch path, so this is safe here. + if (new File(where).isAbsolute()) return where; + return sketchPath + File.separator + "data" + File.separator + where; } @@ -5983,12 +5998,6 @@ public class PApplet extends Applet } - public void save(String filename) { - if (recorder != null) recorder.save(filename); - g.save(filename); - } - - public void hint(int which) { if (recorder != null) recorder.hint(which); g.hint(which); diff --git a/core/PImage.java b/core/PImage.java index 0ce06527d..5eb968e18 100644 --- a/core/PImage.java +++ b/core/PImage.java @@ -678,11 +678,11 @@ public class PImage implements PConstants, Cloneable { // [toxi20050728] added new filters case ERODE: - throw new RuntimeException("Use filter(ERODE) instead of " + - "filter(ERODE, param)"); + throw new RuntimeException("Use filter(ERODE) instead of " + + "filter(ERODE, param)"); case DILATE: - throw new RuntimeException("Use filter(DILATE) instead of " + - "filter(DILATE, param)"); + throw new RuntimeException("Use filter(DILATE) instead of " + + "filter(DILATE, param)"); } updatePixels(); // mark as modified } @@ -1987,20 +1987,27 @@ public class PImage implements PConstants, Cloneable { /** - * Save this image to disk. This method will save to the "current" - * folder, which when running inside the PDE will be the location - * of the Processing application, not the sketch folder. To save - * inside the sketch folder, use the function savePath from PApplet, - * or use saveFrame() instead. + * Save this image to disk. + *

+ * As of revision 0100, this function requires an absolute path, + * in order to avoid confusion. To save inside the sketch folder, + * use the function savePath() from PApplet, or use saveFrame() instead. + *

+ * TODO write reflection code here to use java 1.4 imageio + * methods for writing out images that might be much better. + * won't want to use them in all cases.. how to determine?
+ * boolean ImageIO.write(RenderedImage im, String formatName, File output) */ - public void save(String filename) { - // TODO write reflection code here to use java 1.4 imageio methods - // for writing out images that might be much better - // won't want to use them in all cases.. how to determine? - // maybe for the 1.4 kids, would be nice to have: - //boolean ImageIO.write(RenderedImage im, String formatName, File output) + public void save(String filename) { // ignore boolean success = false; + File file = new File(filename); + if (!file.isAbsolute()) { + System.err.println("PImage.save() requires an absolute path, " + + "you might need to use savePath()."); + return; + } + try { OutputStream os = null; diff --git a/core/todo.txt b/core/todo.txt index 1cb921b7a..909c02303 100644 --- a/core/todo.txt +++ b/core/todo.txt @@ -1,6 +1,11 @@ 0100 core X user.dir wasn't getting set properly X when graphics can be resized, resize rather than creating new context +X change savePath() et al a great deal, include better docs +_ + +_ registering font directories in pdf.. is it necessary? +_ (commented out for 0100) _ make dxf writer that'll work with recordRaw() X enable PGraphicsPDF for inclusion diff --git a/pdf/PGraphicsPDF.java b/pdf/PGraphicsPDF.java index cdb40ad96..250b794c9 100644 --- a/pdf/PGraphicsPDF.java +++ b/pdf/PGraphicsPDF.java @@ -36,11 +36,23 @@ public class PGraphicsPDF extends PGraphics2 { // BaseFont baseFont = mapper.awtToPdf(java.awt.Font awtFont) + + public PGraphicsPDF(int width, int height, PApplet applet) { + this(width, height, applet, null); // will throw an error + } + + public PGraphicsPDF(int width, int height, PApplet applet, String path) { super(width, height, null); - if (path == null) path = "output.pdf"; - this.file = new File(path); + file = new File(path); + if (!file.isAbsolute() || (path == null)) { + throw new RuntimeException("PGraphicsPDF requires an absolute path " + + "for the location of the output file."); + } + + //if (path == null) path = "output.pdf"; + //this.file = new File(path); // don't want to require PApplet as the way to do this.. but how? //if (applet != null) { @@ -59,9 +71,9 @@ public class PGraphicsPDF extends PGraphics2 { */ // seems to only pick up ttf and otf fonts - FontFactory.registerDirectory("/System/Library/Fonts"); - FontFactory.registerDirectory("/Library/Fonts"); - FontFactory.registerDirectory("/Users/fry/Library/Fonts"); + //FontFactory.registerDirectory("/System/Library/Fonts"); + //FontFactory.registerDirectory("/Library/Fonts"); + //FontFactory.registerDirectory("/Users/fry/Library/Fonts"); /* Set registered = FontFactory.getRegisteredFonts(); @@ -134,7 +146,7 @@ public class PGraphicsPDF extends PGraphics2 { public void beginFrame() { // temporary - file = new File(filename); //"test.pdf"); + //file = new File(filename); //"test.pdf"); //document = new Document(); document = new Document(new Rectangle(width, height));