From fb20a176ee18341cf01ece1496af1450d6c831fb Mon Sep 17 00:00:00 2001 From: Ben Fry Date: Sun, 16 Jul 2023 12:40:21 -0400 Subject: [PATCH] fix pdf colors outside the range causing NPE (resolves #740) --- core/src/processing/awt/PGraphicsJava2D.java | 61 +++++++++++-------- .../pdf/src/processing/pdf/PGraphicsPDF.java | 8 +++ todo.txt | 3 +- 3 files changed, 44 insertions(+), 28 deletions(-) diff --git a/core/src/processing/awt/PGraphicsJava2D.java b/core/src/processing/awt/PGraphicsJava2D.java index ee41fe052..edb376710 100644 --- a/core/src/processing/awt/PGraphicsJava2D.java +++ b/core/src/processing/awt/PGraphicsJava2D.java @@ -2607,37 +2607,44 @@ public class PGraphicsJava2D extends PGraphics { 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); - - 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); + backgroundRect(); } } + 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); + } + + ////////////////////////////////////////////////////////////// diff --git a/java/libraries/pdf/src/processing/pdf/PGraphicsPDF.java b/java/libraries/pdf/src/processing/pdf/PGraphicsPDF.java index c0bb22f17..ea1574908 100644 --- a/java/libraries/pdf/src/processing/pdf/PGraphicsPDF.java +++ b/java/libraries/pdf/src/processing/pdf/PGraphicsPDF.java @@ -496,6 +496,14 @@ public class PGraphicsPDF extends PGraphicsJava2D { ////////////////////////////////////////////////////////////// + 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"); } diff --git a/todo.txt b/todo.txt index b31ad19cf..6f4e33cb0 100755 --- a/todo.txt +++ b/todo.txt @@ -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