Skip to content

Commit fb20a17

Browse files
committed
fix pdf colors outside the range causing NPE (resolves #740)
1 parent 81faaa5 commit fb20a17

File tree

3 files changed

+42
-26
lines changed

3 files changed

+42
-26
lines changed

core/src/processing/awt/PGraphicsJava2D.java

Lines changed: 32 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -2607,34 +2607,41 @@ public void backgroundImpl() {
26072607
clearPixels(backgroundColor);
26082608

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

2622-
pushMatrix();
2623-
resetMatrix();
2624-
g2.setColor(bgColor); //, backgroundAlpha));
2625-
// g2.fillRect(0, 0, width, height);
2626-
// On a hi-res display, image may be larger than width/height
2627-
if (image != null) {
2628-
// image will be null in subclasses (i.e. PDF)
2629-
g2.fillRect(0, 0, image.getWidth(null), image.getHeight(null));
2630-
} else {
2631-
// hope for the best if image is null
2632-
g2.fillRect(0, 0, width, height);
2633-
}
2634-
popMatrix();
26352614

2636-
g2.setComposite(oldComposite);
2615+
protected void backgroundRect() {
2616+
Color bgColor = new Color(backgroundColor);
2617+
2618+
// While more complete, this seems to fire an additional event that
2619+
// causes flickering, like an extra background erase on OS X.
2620+
//if (canvas != null) {
2621+
// canvas.setBackground(bgColor);
2622+
//}
2623+
2624+
// If there are transformations or blending changes at the top of
2625+
// draw() (before background() is called) or still in place from
2626+
// the last trip through draw(), need to store and re-apply after.
2627+
Composite oldComposite = g2.getComposite();
2628+
g2.setComposite(defaultComposite);
2629+
pushMatrix();
2630+
resetMatrix();
2631+
2632+
g2.setColor(bgColor);
2633+
// On a hi-res display, image may be larger than width/height
2634+
if (image != null) {
2635+
// image will be null in subclasses (i.e. PDF)
2636+
g2.fillRect(0, 0, image.getWidth(null), image.getHeight(null));
2637+
} else {
2638+
// hope for the best if image is null
2639+
g2.fillRect(0, 0, width, height);
26372640
}
2641+
2642+
// Reset the drawing state (see above)
2643+
popMatrix();
2644+
g2.setComposite(oldComposite);
26382645
}
26392646

26402647

java/libraries/pdf/src/processing/pdf/PGraphicsPDF.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -496,6 +496,14 @@ protected void textLineImpl(char buffer[], int start, int stop,
496496
//////////////////////////////////////////////////////////////
497497

498498

499+
public void backgroundImpl() {
500+
// Override so that even with alpha, we draw a rectangle.
501+
// https://github.com/processing/processing4/issues/740
502+
backgroundRect();
503+
}
504+
505+
//
506+
499507
public void loadPixels() {
500508
nope("loadPixels");
501509
}

todo.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
1293 (4.2.1)
2-
2+
X `NullPointerException` when background exceeds color range when writing PDF
3+
X https://github.com/processing/processing4/issues/740
34

45
sampottinger
56
X Syntax error highlighting placement / width incorrect

0 commit comments

Comments
 (0)