Skip to content

Commit

Permalink
Resolve PDF renderer parse issue in preproc.
Browse files Browse the repository at this point in the history
Resolve moving the PDF renderer information provided in size into settings as part of preproc. Resolves #66.
  • Loading branch information
sampottinger committed Jan 22, 2020
1 parent 3b0ff8f commit 6fe274f
Show file tree
Hide file tree
Showing 4 changed files with 79 additions and 6 deletions.
21 changes: 15 additions & 6 deletions java/src/processing/mode/java/preproc/PdeParseTreeListener.java
Original file line number Diff line number Diff line change
Expand Up @@ -73,8 +73,9 @@ public class PdeParseTreeListener extends ProcessingBaseListener {

private String sketchWidth;
private String sketchHeight;
private String sketchRenderer;
private String pixelDensity;
private String sketchRenderer = null;
private String sketchOutputFilename = null;

private boolean sizeRequiresRewrite = false;
private boolean pixelDensityRequiresRewrite = false;
Expand Down Expand Up @@ -627,10 +628,15 @@ protected void handleSizeCall(ParserRuleContext ctx) {
sketchRenderer.equals("P3D") ||
sketchRenderer.equals("OPENGL") ||
sketchRenderer.equals("JAVA2D") ||
sketchRenderer.equals("PDF") ||
sketchRenderer.equals("FX2D"))) {
thisRequiresRewrite = false;
}
}

if (argsContext.getChildCount() > 5) {
sketchOutputFilename = argsContext.getChild(6).getText();
}
}

if (isFullscreen) {
Expand Down Expand Up @@ -1021,12 +1027,12 @@ protected void writeExtraFieldsAndMethods(PrintWriterWithEditGen classBodyWriter

String settingsOuterTemplate = indent1 + "public void settings() { %s }";

StringBuilder settingsInner = new StringBuilder();
StringJoiner settingsInner = new StringJoiner("\n");

if (sizeRequiresRewrite) {
if (sizeIsFullscreen) {
String fullscreenInner = sketchRenderer == null ? "" : sketchRenderer;
settingsInner.append(String.format("fullScreen(%s);", fullscreenInner));
settingsInner.add(String.format("fullScreen(%s);", fullscreenInner));
} else {

if (sketchWidth.isEmpty() || sketchHeight.isEmpty()) {
Expand All @@ -1041,13 +1047,16 @@ protected void writeExtraFieldsAndMethods(PrintWriterWithEditGen classBodyWriter
argJoiner.add(sketchRenderer);
}

settingsInner.append(String.format("size(%s);", argJoiner.toString()));
if (sketchOutputFilename != null) {
argJoiner.add(sketchOutputFilename);
}

settingsInner.add(String.format("size(%s);", argJoiner.toString()));
}
}

if (pixelDensityRequiresRewrite) {
settingsInner.append("\n");
settingsInner.append(String.format("pixelDensity(%s);", pixelDensity));
settingsInner.add(String.format("pixelDensity(%s);", pixelDensity));
}


Expand Down
5 changes: 5 additions & 0 deletions java/test/processing/mode/java/ParserTests.java
Original file line number Diff line number Diff line change
Expand Up @@ -388,6 +388,11 @@ public void testParamPixelDensity() {
expectGood("parampixeldensity");
}

@Test
public void testPdfWrite() {
expectGood("pdfwrite");
}

private static boolean compile(String id, String program) {
// Create compilable AST to get syntax problems
CompilationUnit compilableCU = JdtCompilerUtil.makeAST(
Expand Down
45 changes: 45 additions & 0 deletions java/test/resources/pdfwrite.expected
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import processing.core.*;
import processing.data.*;
import processing.event.*;
import processing.opengl.*;

import processing.pdf.*;

import java.util.HashMap;
import java.util.ArrayList;
import java.io.File;
import java.io.BufferedReader;
import java.io.PrintWriter;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.IOException;

public class pdfwrite extends PApplet {



public void setup() {
/* size commented out by preprocessor */;
}

public void draw() {
// Draw something good here
line(0, 0, width/2, height);

// Exit the program
println("Finished.");
exit();
}


public void settings() { size(400,400,PDF,"filename.pdf"); }

static public void main(String[] passedArgs) {
String[] appletArgs = new String[] { "pdfwrite" };
if (passedArgs != null) {
PApplet.main(concat(appletArgs, passedArgs));
} else {
PApplet.main(appletArgs);
}
}
}
14 changes: 14 additions & 0 deletions java/test/resources/pdfwrite.pde
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import processing.pdf.*;

void setup() {
size(400, 400, PDF, "filename.pdf");
}

void draw() {
// Draw something good here
line(0, 0, width/2, height);

// Exit the program
println("Finished.");
exit();
}

0 comments on commit 6fe274f

Please sign in to comment.