From 7c12eba22a44124f7b580c4e1a4e358500e13a0f Mon Sep 17 00:00:00 2001 From: benfry Date: Thu, 12 Jan 2006 05:10:27 +0000 Subject: [PATCH] pdf stuff really starting to work --- core/PApplet.java | 88 ++++++++++++++++++++++++++---------- core/PGraphics.java | 32 +++++++++++-- core/PGraphics2.java | 13 ++++++ core/PGraphics3.java | 11 ++++- pdf/PGraphicsPDF.java | 102 ++++++++++++++++++++++++++++-------------- todo.txt | 10 +++++ 6 files changed, 196 insertions(+), 60 deletions(-) diff --git a/core/PApplet.java b/core/PApplet.java index 4177b6fe6..c4a66a703 100644 --- a/core/PApplet.java +++ b/core/PApplet.java @@ -528,7 +528,7 @@ public class PApplet extends Applet // the thread on, instead of doing it inside paint() //finished = true; // why did i comment this out? - //System.out.println("stopping applet"); + //System.out.println("stopping applet " + thread); // don't run stop and disposers twice if (thread == null) return; @@ -918,6 +918,11 @@ public class PApplet extends Applet */ + public PGraphics createGraphics(String irenderer, String ipath) { + return createGraphics(width, height, irenderer, this, ipath); + } + + public PGraphics createGraphics(int iwidth, int iheight, String irenderer, String ipath) { return createGraphics(iwidth, iheight, irenderer, this, ipath); @@ -978,7 +983,7 @@ public class PApplet extends Applet } else { ite.getTargetException().printStackTrace(); Throwable target = ite.getTargetException(); - target.printStackTrace(System.out); // macosx bug + if (platform == MACOSX) target.printStackTrace(System.out); // bug // neither of these help, or work //target.printStackTrace(System.err); //System.err.flush(); @@ -995,7 +1000,7 @@ public class PApplet extends Applet } } catch (Exception e) { - System.out.println("ex3"); + //System.out.println("ex3"); if ((e instanceof IllegalArgumentException) || (e instanceof NoSuchMethodException) || (e instanceof IllegalAccessException)) { @@ -1008,7 +1013,7 @@ public class PApplet extends Applet throw new RuntimeException(msg); } else { - e.printStackTrace(System.out); + if (platform == MACOSX) e.printStackTrace(System.out); //System.err.flush(); //return null; throw new RuntimeException(e.getMessage()); @@ -1191,10 +1196,12 @@ public class PApplet extends Applet // and the run button quits out leechErr.println(LEECH_WAKEUP); e.printStackTrace(leechErr); + e.printStackTrace(System.out); } else { System.err.println(LEECH_WAKEUP); e.printStackTrace(); + e.printStackTrace(System.out); } } if (THREAD_DEBUG) println(Thread.currentThread().getName() + @@ -1242,8 +1249,8 @@ public class PApplet extends Applet if (THREAD_DEBUG) println(Thread.currentThread().getName() + " 1b draw"); - boolean recorderNull = true; - boolean recorderRawNull = true; + //boolean recorderNull = true; + //boolean recorderRawNull = true; if (frameCount == 0) { try { @@ -1333,8 +1340,8 @@ public class PApplet extends Applet // as of draw().. this will prevent the recorder from being // reset if recordShape() is called in an event method, such // as mousePressed() - recorderNull = (recorder == null); - recorderRawNull = (g.recorderRaw == null); + //recorderNull = (recorder == null); + //recorderRawNull = (g.recorderRaw == null); // dmouseX/Y is updated only once per frame dmouseX = mouseX; @@ -1360,6 +1367,7 @@ public class PApplet extends Applet } g.endFrame(); + /* if (!recorderNull) { if (recorder != null) { recorder.endFrame(); @@ -1372,6 +1380,7 @@ public class PApplet extends Applet g.recorderRaw = null; } } + */ //} // older end sync @@ -5834,27 +5843,54 @@ public class PApplet extends Applet ////////////////////////////////////////////////////////////// - public void record(PGraphics recorder) { + public void beginRecord(PGraphics recorder) { this.recorder = recorder; - recorder.beginFrame(); + recorder.beginRecord(); + //recorder.beginFrame(); } - public PGraphics record(String renderer, String filename) { - this.recorder = createGraphics(width, height, renderer, filename); - recorder.beginFrame(); - return recorder; - } - - - public PGraphics recordRaw(String renderer, String filename) { - filename = savePath(filename); // ensure an absolute path + public PGraphics beginRecord(String renderer, String filename) { + //this.recorder = createGraphics(width, height, renderer, filename); + //recorder.beginFrame(); PGraphics rec = createGraphics(width, height, renderer, filename); - g.recordRaw(rec); + beginRecord(rec); return rec; } + public void endRecord() { + //println("endRecord()"); + //if (!recorderNull) { + if (recorder != null) { + recorder.endRecord(); + //recorder.endFrame(); + recorder = null; + } + } + + + public PGraphics beginRaw(String renderer, String filename) { + //filename = savePath(filename); // ensure an absolute path + PGraphics rec = createGraphics(width, height, renderer, filename); + //g.recordRaw(rec); + g.beginRaw(rec); + return rec; + } + + + /* + public void endRaw() { + //if (!recorderRawNull) { + if (g.recorderRaw != null) { + //g.recorderRaw.endFrame(); + g.endRaw(); + g.recorderRaw = null; + } + } + */ + + ////////////////////////////////////////////////////////////// @@ -6989,8 +7025,14 @@ public class PApplet extends Applet } - public void recordRaw(PGraphics recorderRaw) { - if (recorder != null) recorder.recordRaw(recorderRaw); - g.recordRaw(recorderRaw); + public void beginRaw(PGraphics recorderRaw) { + if (recorder != null) recorder.beginRaw(recorderRaw); + g.beginRaw(recorderRaw); + } + + + public void endRaw() { + if (recorder != null) recorder.endRaw(); + g.endRaw(); } } diff --git a/core/PGraphics.java b/core/PGraphics.java index c7298a7fd..07d03bf4c 100644 --- a/core/PGraphics.java +++ b/core/PGraphics.java @@ -3533,8 +3533,34 @@ public class PGraphics extends PImage implements PConstants { ////////////////////////////////////////////////////////////// - public void recordRaw(PGraphics recorderRaw) { - throw new RuntimeException("recordRaw() not supported " + - "by this renderer."); + public void beginRecord() { // ignore + beginFrame(); // default is just to open the frame } + + + public void endRecord() { // ignore + endFrame(); + } + + + public void beginRaw(PGraphics recorderRaw) { + this.recorderRaw = recorderRaw; + recorderRaw.beginFrame(); + } + + + public void endRaw() { + if (recorderRaw != null) { + recorderRaw.endFrame(); + recorderRaw = null; + } + } + + //public void beginRaw() { // ignore + //beginFrame(); + //} + + //public void endRaw() { // ignore + //endFrame(); + //} } diff --git a/core/PGraphics2.java b/core/PGraphics2.java index c00fd31e6..1630346fc 100644 --- a/core/PGraphics2.java +++ b/core/PGraphics2.java @@ -935,6 +935,19 @@ public class PGraphics2 extends PGraphics { } + + ////////////////////////////////////////////////////////////// + + + public void beginRaw(PGraphics recorderRaw) { + throw new RuntimeException("beginRaw() not available with this renderer"); + } + + + public void endRaw() { + } + + ////////////////////////////////////////////////////////////// diff --git a/core/PGraphics3.java b/core/PGraphics3.java index 189bf749c..38a1f1763 100644 --- a/core/PGraphics3.java +++ b/core/PGraphics3.java @@ -3799,12 +3799,21 @@ public class PGraphics3 extends PGraphics { // RAW SHAPE RECORDING - public void recordRaw(PGraphics recorderRaw) { + /* + public void beginRaw(PGraphics recorderRaw) { this.recorderRaw = recorderRaw; recorderRaw.beginFrame(); } + public void endRaw() { + if (recorderRaw != null) { + recorderRaw.endFrame(); + recorderRaw = null; + } + } + */ + ////////////////////////////////////////////////////////////// diff --git a/pdf/PGraphicsPDF.java b/pdf/PGraphicsPDF.java index e8c0c4016..1ae020823 100644 --- a/pdf/PGraphicsPDF.java +++ b/pdf/PGraphicsPDF.java @@ -47,12 +47,19 @@ public class PGraphicsPDF extends PGraphics2 { //System.out.println("trying " + path); - file = new File(path); - if (!file.isAbsolute() || (path == null)) { + if (path != null) { + file = new File(path); + if (!file.isAbsolute()) file = null; + } + if (file == null) { throw new RuntimeException("PGraphicsPDF requires an absolute path " + "for the location of the output file."); } + if (applet != null) { + applet.registerDispose(this); + } + //System.out.println("making " + path); //if (path == null) path = "output.pdf"; @@ -88,6 +95,17 @@ public class PGraphicsPDF extends PGraphics2 { } + public void dispose() { + //System.out.println("calling dispose"); + if (document != null) { + g2.dispose(); + document.close(); // can't be done in finalize, not always called + document = null; + } + //new Exception().printStackTrace(System.out); + } + + // create a temporary file and put the graphics crap there // don't start a fresh page if frameCount is zero (setup isn't its own page) @@ -134,34 +152,36 @@ public class PGraphicsPDF extends PGraphics2 { //System.out.println("pdf beginFrame()"); //document = new Document(); - document = new Document(new Rectangle(width, height)); - try { - writer = PdfWriter.getInstance(document, new FileOutputStream(file)); - document.open(); - content = writer.getDirectContent(); + if (document == null) { + document = new Document(new Rectangle(width, height)); + try { + writer = PdfWriter.getInstance(document, new FileOutputStream(file)); + document.open(); + content = writer.getDirectContent(); - } catch (Exception e) { - e.printStackTrace(); - throw new RuntimeException("Problem saving the PDF file."); + } catch (Exception e) { + e.printStackTrace(); + throw new RuntimeException("Problem saving the PDF file."); + } + + // how to call newPage() in here? + /* + System.out.println("beginFrame() " + width + ", " + height); + tp = content.createTemplate(width, height); + //g2 = tp.createGraphics(width, height, mapper); + g2 = tp.createGraphicsShapes(width, height); + //System.out.println("g2 is " + g2); + tp.setWidth(width); + tp.setHeight(height); + */ + + // what's a good way to switch between these? + // the regular createGraphics doesn't seem to recognize fonts + // how should the insertDirectory stuff be used properly? + //g2 = content.createGraphics(width, height); + g2 = content.createGraphicsShapes(width, height); } - // how to call newPage() in here? - /* - System.out.println("beginFrame() " + width + ", " + height); - tp = content.createTemplate(width, height); - //g2 = tp.createGraphics(width, height, mapper); - g2 = tp.createGraphicsShapes(width, height); - //System.out.println("g2 is " + g2); - tp.setWidth(width); - tp.setHeight(height); - */ - - // what's a good way to switch between these? - // the regular createGraphics doesn't seem to recognize fonts - // how should the insertDirectory stuff be used properly? - //g2 = content.createGraphics(width, height); - g2 = content.createGraphicsShapes(width, height); - super.beginFrame(); } @@ -185,6 +205,8 @@ public class PGraphicsPDF extends PGraphics2 { e.printStackTrace(); } g2 = content.createGraphicsShapes(width, height); + + // should there be a beginFrame/endFrame in here? } @@ -198,26 +220,40 @@ public class PGraphicsPDF extends PGraphics2 { g2.drawString(text1, 100, 100); */ - g2.dispose(); /* content.addTemplate(tp, 0, 0); //50, 400); */ + /* try { document.newPage(); // is this bad if no addl pages are made? } catch (Exception e) { e.printStackTrace(); } + */ - //System.out.println("closing document"); - document.close(); // can't be done in finalize, not always called + /* + g2.dispose(); + document.close(); // can't be done in finalize, not always called + */ } - //protected void finalize() throws Throwable { - //System.out.println("calling finalize"); + /* + protected void finalize() throws Throwable { + System.out.println("calling finalize"); //document.close(); // do this in dispose instead? - //} + } + */ + + + ////////////////////////////////////////////////////////////// + + + public void endRecord() { + super.endRecord(); + dispose(); + } ////////////////////////////////////////////////////////////// diff --git a/todo.txt b/todo.txt index ad57e54c8..565ec3c60 100644 --- a/todo.txt +++ b/todo.txt @@ -6,6 +6,16 @@ X switch to using date as default for archive sketch _ add this as a preference _ need method for showing prefs for the tools +pdf stuff +_ don't open a window the size of the pdf if in pdf mode +_ need to have some sort of flag in the gfx context that it's visible or not +_ handled inside updateSize()? +_ how to flush when the sketch is done +_ inside dispose method? explicit close? +_ call exit() at end of pdf apps? exit calls dispose on gfx? +_ beginRecord() and endRecord() so that record doesn't stop after frame? + + _ when a conflicting library is found, need to report it _ altho prolly only when it's actually different (md5hash it?)