Skip to content

Commit

Permalink
fix pdf colors outside the range causing NPE (resolves #740)
Browse files Browse the repository at this point in the history
  • Loading branch information
benfry committed Jul 16, 2023
1 parent 81faaa5 commit fb20a17
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 26 deletions.
57 changes: 32 additions & 25 deletions core/src/processing/awt/PGraphicsJava2D.java
Original file line number Diff line number Diff line change
Expand Up @@ -2607,34 +2607,41 @@ public void backgroundImpl() {
clearPixels(backgroundColor);

} else {
Color bgColor = new Color(backgroundColor);
// seems to fire an additional event that causes flickering,
// like an extra background erase on OS X
// if (canvas != null) {
// canvas.setBackground(bgColor);
// }
//new Exception().printStackTrace(System.out);
// in case people do transformations before background(),
// need to handle this with a push/reset/pop
Composite oldComposite = g2.getComposite();
g2.setComposite(defaultComposite);
backgroundRect();
}
}

pushMatrix();
resetMatrix();
g2.setColor(bgColor); //, backgroundAlpha));
// g2.fillRect(0, 0, width, height);
// On a hi-res display, image may be larger than width/height
if (image != null) {
// image will be null in subclasses (i.e. PDF)
g2.fillRect(0, 0, image.getWidth(null), image.getHeight(null));
} else {
// hope for the best if image is null
g2.fillRect(0, 0, width, height);
}
popMatrix();

g2.setComposite(oldComposite);
protected void backgroundRect() {
Color bgColor = new Color(backgroundColor);

// While more complete, this seems to fire an additional event that
// causes flickering, like an extra background erase on OS X.
//if (canvas != null) {
// canvas.setBackground(bgColor);
//}

// If there are transformations or blending changes at the top of
// draw() (before background() is called) or still in place from
// the last trip through draw(), need to store and re-apply after.
Composite oldComposite = g2.getComposite();
g2.setComposite(defaultComposite);
pushMatrix();
resetMatrix();

g2.setColor(bgColor);
// On a hi-res display, image may be larger than width/height
if (image != null) {
// image will be null in subclasses (i.e. PDF)
g2.fillRect(0, 0, image.getWidth(null), image.getHeight(null));
} else {
// hope for the best if image is null
g2.fillRect(0, 0, width, height);
}

// Reset the drawing state (see above)
popMatrix();
g2.setComposite(oldComposite);
}


Expand Down
8 changes: 8 additions & 0 deletions java/libraries/pdf/src/processing/pdf/PGraphicsPDF.java
Original file line number Diff line number Diff line change
Expand Up @@ -496,6 +496,14 @@ protected void textLineImpl(char buffer[], int start, int stop,
//////////////////////////////////////////////////////////////


public void backgroundImpl() {
// Override so that even with alpha, we draw a rectangle.
// https://github.com/processing/processing4/issues/740
backgroundRect();
}

//

public void loadPixels() {
nope("loadPixels");
}
Expand Down
3 changes: 2 additions & 1 deletion todo.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
1293 (4.2.1)

X `NullPointerException` when background exceeds color range when writing PDF
X https://github.com/processing/processing4/issues/740

sampottinger
X Syntax error highlighting placement / width incorrect
Expand Down

0 comments on commit fb20a17

Please sign in to comment.