Skip to content

Commit

Permalink
Fixes LibrePDF#1077: Override create() in PdfPrinterGraphics2D
Browse files Browse the repository at this point in the history
  • Loading branch information
mperktold committed Mar 5, 2024
1 parent d64fa34 commit 361e808
Show file tree
Hide file tree
Showing 3 changed files with 89 additions and 38 deletions.
89 changes: 51 additions & 38 deletions openpdf/src/main/java/com/lowagie/text/pdf/PdfGraphics2D.java
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,47 @@ private PdfGraphics2D() {
setRenderingHint(HyperLinkKey.KEY_INSTANCE, HyperLinkKey.VALUE_HYPERLINKKEY_OFF);
}

/**
* Copy constructor for child PdfGraphics2D objects.
* @param parent the parent PdfGraphics2D
* @see #create()
*/
protected PdfGraphics2D(PdfGraphics2D parent) {
this();
rhints.putAll( parent.rhints );
onlyShapes = parent.onlyShapes;
transform = new AffineTransform(parent.transform);
baseFonts = parent.baseFonts;
fontMapper = parent.fontMapper;
paint = parent.paint;
fillGState = parent.fillGState;
currentFillGState = parent.currentFillGState;
currentStrokeGState = parent.currentStrokeGState;
strokeGState = parent.strokeGState;
background = parent.background;
mediaTracker = parent.mediaTracker;
convertImagesToJPEG = parent.convertImagesToJPEG;
jpegQuality = parent.jpegQuality;
setFont(parent.font);
cb = parent.cb.getDuplicate();
cb.saveState();
width = parent.width;
height = parent.height;
followPath(new Area(new Rectangle2D.Float(0, 0, width, height)), CLIP);
if (parent.clip != null)
clip = new Area(parent.clip);
composite = parent.composite;
stroke = parent.stroke;
originalStroke = parent.originalStroke;
strokeOne = (BasicStroke)transformStroke(parent.strokeOne);
oldStroke = parent.strokeOne;
setStrokeDiff(oldStroke, null);
cb.saveState();
if (clip != null)
followPath(clip, CLIP);
kid = true;
}

/**
* Shortcut constructor for PDFGraphics2D.
* @param cb the PdfContentByte
Expand Down Expand Up @@ -953,46 +994,18 @@ public FontRenderContext getFontRenderContext() {
* @see Graphics#create()
*/
public Graphics create() {
PdfGraphics2D g2 = new PdfGraphics2D();
g2.rhints.putAll( this.rhints );
g2.onlyShapes = this.onlyShapes;
g2.transform = new AffineTransform(this.transform);
g2.baseFonts = this.baseFonts;
g2.fontMapper = this.fontMapper;
g2.paint = this.paint;
g2.fillGState = this.fillGState;
g2.currentFillGState = this.currentFillGState;
g2.currentStrokeGState = this.currentStrokeGState;
g2.strokeGState = this.strokeGState;
g2.background = this.background;
g2.mediaTracker = this.mediaTracker;
g2.convertImagesToJPEG = this.convertImagesToJPEG;
g2.jpegQuality = this.jpegQuality;
g2.setFont(this.font);
g2.cb = this.cb.getDuplicate();
g2.cb.saveState();
g2.width = this.width;
g2.height = this.height;
g2.followPath(new Area(new Rectangle2D.Float(0, 0, width, height)), CLIP);
if (this.clip != null)
g2.clip = new Area(this.clip);
g2.composite = composite;
g2.stroke = stroke;
g2.originalStroke = originalStroke;
g2.strokeOne = (BasicStroke)g2.transformStroke(g2.strokeOne);
g2.oldStroke = g2.strokeOne;
g2.setStrokeDiff(g2.oldStroke, null);
g2.cb.saveState();
if (g2.clip != null)
g2.followPath(g2.clip, CLIP);
g2.kid = true;
if (this.kids == null)
this.kids = new ArrayList<>();
this.kids.add(cb.getInternalBuffer().size());
this.kids.add(g2);
PdfGraphics2D g2 = createChild();
if (kids == null)
kids = new ArrayList<>();
kids.add(cb.getInternalBuffer().size());
kids.add(g2);
return g2;
}


protected PdfGraphics2D createChild() {
return new PdfGraphics2D(this);
}

public PdfContentByte getContent() {
return this.cb;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,16 @@ public PdfPrinterGraphics2D(PdfContentByte cb, float width, float height, FontMa
this.printerJob = printerJob;
}

protected PdfPrinterGraphics2D(PdfPrinterGraphics2D parent) {
super(parent);
printerJob = parent.printerJob;
}

public PrinterJob getPrinterJob() {
return printerJob;
}

protected PdfGraphics2D createChild() {
return new PdfPrinterGraphics2D(this);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package com.lowagie.text.pdf;

import static org.junit.jupiter.api.Assertions.*;

import com.lowagie.text.Document;

import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.print.PrinterJob;
import java.io.ByteArrayOutputStream;

import org.junit.jupiter.api.Test;

class PdfPrinterGraphics2DTest
{
@Test
void testCreate() {
try (Document document = new Document()) {
PdfWriter writer = PdfWriter.getInstance(document, new ByteArrayOutputStream());
document.open();
Graphics2D graphics1 = writer.getDirectContent().createPrinterGraphics(10, 10, PrinterJob.getPrinterJob());
Graphics graphics2 = graphics1.create();
assertEquals(PdfPrinterGraphics2D.class, graphics1.getClass());
assertEquals(PdfPrinterGraphics2D.class, graphics2.getClass());
graphics2.dispose();
graphics1.dispose();
}
}
}

0 comments on commit 361e808

Please sign in to comment.