Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PdfPrinterGraphics2D does not override create #1077

Closed
mperktold opened this issue Feb 23, 2024 · 1 comment · Fixed by #1093
Closed

PdfPrinterGraphics2D does not override create #1077

mperktold opened this issue Feb 23, 2024 · 1 comment · Fixed by #1093
Labels

Comments

@mperktold
Copy link
Contributor

Describe the bug
The PdfPrinterGraphics2D class inherits the create method from PdfGraphics2D, which in turn overrides. The method is declared as abstract in Graphics and is convenient to create equivalent copies that can be modified without modifying the original object, just like the classic clone method.

Because PdfPrinterGraphics2D does not override create, it returns a PdfGraphics2D instead of a PdfPrinterGraphics2D, which is unexpected.

To Reproduce

try (var document = new Document()) {
    var writer = PdfWriter.getInstance(document, new ByteArrayOutputStream());
    document.open();
    var graphics1 = writer.getDirectContent().createPrinterGraphics(10, 10, PrinterJob.getPrinterJob());
    var graphics2 = graphics1.create();
    System.out.println(graphics1.getClass());
    System.out.println(graphics2.getClass());
}

Expected behavior
The above code should print:

class com.lowagie.text.pdf.PdfPrinterGraphics2D
class com.lowagie.text.pdf.PdfPrinterGraphics2D

But it prints:

class com.lowagie.text.pdf.PdfPrinterGraphics2D
class com.lowagie.text.pdf.PdfGraphics2D

It also throws an exception, but that is a problem with the example code and therefore expected and not the point of this issue.

System (please complete the following information):

  • OS: Windows 11
  • Used Font: not relevant

Additional context
You might want to consider to make it easier to override PdfGraphics2D.create. At the moment, it creates a new empty PdfGraphics2D and then copies all own fields to the copy. When overriding the method, you would need to create an empty PdfPrinterGraphics2D object instead. Then, you would still need to copy the fields of the base class to the new copy in addition to any field of the derived class, but you cannot use super.create() for that.
It would be much easier if create were implemented by two separate hooks (possibly protected): one for creating an empty copy, and another for copying all fields to a copy that is given as a parameter.
Alternatively, PdfGraphics2D could also implement the Cloneable interface, move the implementation of create() to clone() and implement create() on top of that.

@mperktold mperktold added the bug label Feb 23, 2024
@asturio
Copy link
Member

asturio commented Feb 23, 2024

Pull requests are welcome.

mperktold added a commit to mperktold/OpenPDF that referenced this issue Mar 5, 2024
mperktold added a commit to mperktold/OpenPDF that referenced this issue Mar 5, 2024
@asturio asturio linked a pull request Mar 5, 2024 that will close this issue
2 tasks
asturio pushed a commit that referenced this issue Mar 5, 2024
* Fixes #1077: Override create() in PdfPrinterGraphics2D
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants