Skip to content

reset graphics state in PDFPrintable.print method#443

Open
valerybokov wants to merge 2 commits intoapache:trunkfrom
valerybokov:reset-graphics-state-in-PDFPrintable.print
Open

reset graphics state in PDFPrintable.print method#443
valerybokov wants to merge 2 commits intoapache:trunkfrom
valerybokov:reset-graphics-state-in-PDFPrintable.print

Conversation

@valerybokov
Copy link
Copy Markdown

No description provided.

@THausherr
Copy link
Copy Markdown
Contributor

You're focusing on the border painting, but the transform is also changed previously elsewhere.

@THausherr
Copy link
Copy Markdown
Contributor

Why check both? I assume with "cast" you mean "printerGraphics = (Graphics2D) graphics". Shouldn't it be enough to check only "printerGraphics", and if not, why not?

@valerybokov
Copy link
Copy Markdown
Author

valerybokov commented May 5, 2026

Why check both? I assume with "cast" you mean "printerGraphics = (Graphics2D) graphics". Shouldn't it be enough to check only "printerGraphics", and if not, why not?

I hope I understood you correctly. Line 298 (new version): the graphics2D reference doesn't always refer to the same object as the printerGraphics reference. Therefore, we only dispose the object if a new graphics object has been created.

@THausherr
Copy link
Copy Markdown
Contributor

I'm not asking about the dispose, I'm asking about the line "if (printerGraphics != null && callerTransform != null)". printerGraphics is no longer null as soon as the assignment on line 221 is successful. So why check whether callerTransform isn't null? It will always be non null if the casted assignment was successful.

@valerybokov
Copy link
Copy Markdown
Author

valerybokov commented May 6, 2026

I'm not asking about the dispose, I'm asking about the line "if (printerGraphics != null && callerTransform != null)". printerGraphics is no longer null as soon as the assignment on line 221 is successful. So why check whether callerTransform isn't null? It will always be non null if the casted assignment was successful.

Let's say this is a standard check, but it's really unlikely. Here are three cases:

  1. If the graphics variable isn't a Graphics2D, a ClassCastException will occur.
  2. There's an OOM error on line 223 (printerGraphics.getTransform()).
  3. The graphics parameter is null. Then there will be a null pointer exception.

For all these cases, the result is the same: the finality block will be triggered, and the printerGraphics and callerTransform variables will be null.

I can simplify the check if you want.

@THausherr
Copy link
Copy Markdown
Contributor

Re (2) you're really going to extremes. If there is an OOM we're doomed anyway, it won't matter that we restore printerGraphics settings. Re (1) I've never heard of the cast to Graphics2D failing. A google search finds nothing, ChatGPT says it's historical.

@xzel23
Copy link
Copy Markdown

xzel23 commented May 7, 2026

Isn't the standard in Swing to simply work on a temporary context obtained by calling context.create() if you don't want changes to transformation etc to persist? When done, simply forget about it. No need to try to reset the original context to its former state. Should also be more performant.

@THausherr
Copy link
Copy Markdown
Contributor

Yes, there is an SO answer about this
https://stackoverflow.com/questions/901046/
and ChatGPT agrees, and suggests this

Graphics2D g2 = (Graphics2D) graphics.create();
try {
    // modify freely
} finally {
    g2.dispose();
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants