From 86fb9989a551ff40a4c0512f550e4ece355939a1 Mon Sep 17 00:00:00 2001 From: Simon Steiner Date: Fri, 4 Dec 2015 12:18:50 +0000 Subject: [PATCH] FOP-2548: Support Barcode4J page number git-svn-id: https://svn.apache.org/repos/asf/xmlgraphics/fop/trunk@1717946 13f79535-47bb-0310-9956-ffa450edef68 --- .../bitmap/AbstractBitmapDocumentHandler.java | 2 +- .../intermediate/AbstractIFPainter.java | 2 +- .../fop/render/intermediate/IFContext.java | 9 ++ .../fop/render/intermediate/IFParser.java | 1 + .../fop/render/intermediate/IFRenderer.java | 1 + .../fop/render/java2d/Java2DPainter.java | 16 +++- .../fop/render/pcl/PCLDocumentHandler.java | 2 +- .../fop/render/pdf/PDFPainterTestCase.java | 89 ++++++++++++++++--- 8 files changed, 106 insertions(+), 16 deletions(-) diff --git a/src/java/org/apache/fop/render/bitmap/AbstractBitmapDocumentHandler.java b/src/java/org/apache/fop/render/bitmap/AbstractBitmapDocumentHandler.java index 374fa5833..0c8b4bc0e 100644 --- a/src/java/org/apache/fop/render/bitmap/AbstractBitmapDocumentHandler.java +++ b/src/java/org/apache/fop/render/bitmap/AbstractBitmapDocumentHandler.java @@ -273,7 +273,7 @@ public abstract class AbstractBitmapDocumentHandler extends AbstractBinaryWritin } graphics2D.scale(scale / 1000f, scale / 1000f); - return new Java2DPainter(graphics2D, getContext(), getFontInfo()); + return new Java2DPainter(graphics2D, getContext(), getFontInfo(), this); } /** diff --git a/src/java/org/apache/fop/render/intermediate/AbstractIFPainter.java b/src/java/org/apache/fop/render/intermediate/AbstractIFPainter.java index 2c04a0714..929d786ea 100644 --- a/src/java/org/apache/fop/render/intermediate/AbstractIFPainter.java +++ b/src/java/org/apache/fop/render/intermediate/AbstractIFPainter.java @@ -188,7 +188,7 @@ public abstract class AbstractIFPainter implements if (conversionMode != null) { hints.put(ImageHandlerUtil.CONVERSION_MODE, conversionMode); } - + hints.put("page-number", documentHandler.getContext().getPageNumber()); return hints; } diff --git a/src/java/org/apache/fop/render/intermediate/IFContext.java b/src/java/org/apache/fop/render/intermediate/IFContext.java index 7464e26e0..0edb94c4b 100644 --- a/src/java/org/apache/fop/render/intermediate/IFContext.java +++ b/src/java/org/apache/fop/render/intermediate/IFContext.java @@ -57,6 +57,8 @@ public class IFContext { private int pageIndex = -1; + private int pageNumber = -1; + /** * Main constructor. * @param ua the user agent @@ -234,4 +236,11 @@ public class IFContext { return this.pageIndex; } + public int getPageNumber() { + return pageNumber; + } + + public void setPageNumber(int pageNumber) { + this.pageNumber = pageNumber; + } } diff --git a/src/java/org/apache/fop/render/intermediate/IFParser.java b/src/java/org/apache/fop/render/intermediate/IFParser.java index 3d0bfd32f..235e051f9 100644 --- a/src/java/org/apache/fop/render/intermediate/IFParser.java +++ b/src/java/org/apache/fop/render/intermediate/IFParser.java @@ -523,6 +523,7 @@ public class IFParser implements IFConstants { establishForeignAttributes(foreignAttributes); documentHandler.startPage(index, name, pageMasterName, new Dimension(width, height)); + documentHandler.getContext().setPageNumber(Integer.parseInt(name)); resetForeignAttributes(); } diff --git a/src/java/org/apache/fop/render/intermediate/IFRenderer.java b/src/java/org/apache/fop/render/intermediate/IFRenderer.java index e0843ef74..f2eae9ae1 100644 --- a/src/java/org/apache/fop/render/intermediate/IFRenderer.java +++ b/src/java/org/apache/fop/render/intermediate/IFRenderer.java @@ -573,6 +573,7 @@ public class IFRenderer extends AbstractPathOrientedRenderer { establishForeignAttributes(page.getForeignAttributes()); documentHandler.getContext().setPageIndex(page.getPageIndex()); + documentHandler.getContext().setPageNumber(page.getPageNumber()); documentHandler.startPage(page.getPageIndex(), page.getPageNumberString(), page.getSimplePageMasterName(), dim); resetForeignAttributes(); diff --git a/src/java/org/apache/fop/render/java2d/Java2DPainter.java b/src/java/org/apache/fop/render/java2d/Java2DPainter.java index 328d1a4f8..06f67e75b 100644 --- a/src/java/org/apache/fop/render/java2d/Java2DPainter.java +++ b/src/java/org/apache/fop/render/java2d/Java2DPainter.java @@ -41,6 +41,7 @@ import org.apache.fop.render.intermediate.AbstractIFPainter; import org.apache.fop.render.intermediate.BorderPainter; import org.apache.fop.render.intermediate.GraphicsPainter; import org.apache.fop.render.intermediate.IFContext; +import org.apache.fop.render.intermediate.IFDocumentHandler; import org.apache.fop.render.intermediate.IFException; import org.apache.fop.render.intermediate.IFState; import org.apache.fop.traits.BorderProps; @@ -51,7 +52,7 @@ import org.apache.fop.util.CharUtilities; * {@link org.apache.fop.render.intermediate.IFPainter} implementation that paints on a Graphics2D * instance. */ -public class Java2DPainter extends AbstractIFPainter { +public class Java2DPainter extends AbstractIFPainter { /** the IF context */ protected IFContext ifContext; @@ -74,7 +75,11 @@ public class Java2DPainter extends AbstractIFPainter { * @param fontInfo the font information */ public Java2DPainter(Graphics2D g2d, IFContext context, FontInfo fontInfo) { - this(g2d, context, fontInfo, null); + this(g2d, context, fontInfo, new Java2DDocumentHandler()); + } + + public Java2DPainter(Graphics2D g2d, IFContext context, FontInfo fontInfo, IFDocumentHandler documentHandler) { + this(g2d, context, fontInfo, null, documentHandler); } /** @@ -86,7 +91,12 @@ public class Java2DPainter extends AbstractIFPainter { * @param state the IF state object */ public Java2DPainter(Graphics2D g2d, IFContext context, FontInfo fontInfo, IFState state) { - super(new Java2DDocumentHandler()); + this(g2d, context, fontInfo, state, new Java2DDocumentHandler()); + } + + public Java2DPainter(Graphics2D g2d, IFContext context, FontInfo fontInfo, IFState state, + IFDocumentHandler documentHandler) { + super(documentHandler); this.ifContext = context; if (state != null) { this.state = state.push(); diff --git a/src/java/org/apache/fop/render/pcl/PCLDocumentHandler.java b/src/java/org/apache/fop/render/pcl/PCLDocumentHandler.java index 4d0efd8ed..1991295bd 100644 --- a/src/java/org/apache/fop/render/pcl/PCLDocumentHandler.java +++ b/src/java/org/apache/fop/render/pcl/PCLDocumentHandler.java @@ -251,7 +251,7 @@ public class PCLDocumentHandler extends AbstractBinaryWritingIFDocumentHandler graphics2D.scale(scale / 1000f, scale / 1000f); graphics2D.translate(-printArea.x, -printArea.y); - return new Java2DPainter(graphics2D, getContext(), getFontInfo()); + return new Java2DPainter(graphics2D, getContext(), getFontInfo(), this); } private BufferedImage createBufferedImage(int bitmapWidth, int bitmapHeight) { diff --git a/test/java/org/apache/fop/render/pdf/PDFPainterTestCase.java b/test/java/org/apache/fop/render/pdf/PDFPainterTestCase.java index db1757847..d70c00f9e 100644 --- a/test/java/org/apache/fop/render/pdf/PDFPainterTestCase.java +++ b/test/java/org/apache/fop/render/pdf/PDFPainterTestCase.java @@ -20,7 +20,12 @@ package org.apache.fop.render.pdf; import java.awt.Color; +import java.awt.Dimension; import java.awt.Rectangle; +import java.io.ByteArrayOutputStream; +import java.io.File; + +import javax.xml.transform.stream.StreamResult; import org.junit.Test; @@ -31,27 +36,32 @@ import static org.mockito.Mockito.verify; import static org.mockito.Mockito.when; import org.apache.fop.apps.FOUserAgent; +import org.apache.fop.apps.FopFactory; import org.apache.fop.fo.Constants; +import org.apache.fop.pdf.PDFDocument; +import org.apache.fop.pdf.PDFProfile; +import org.apache.fop.pdf.PDFStructElem; +import org.apache.fop.render.RenderingContext; import org.apache.fop.render.intermediate.IFContext; +import org.apache.fop.render.intermediate.IFException; import org.apache.fop.traits.BorderProps; +import junit.framework.Assert; + public class PDFPainterTestCase { + private FOUserAgent foUserAgent; + private PDFContentGenerator pdfContentGenerator; + private PDFDocumentHandler pdfDocumentHandler; + private PDFPainter pdfPainter; + private PDFStructElem elem = new PDFStructElem(); + @Test public void testDrawBorderRect() throws Exception { // the goal of this test is to check that the drawing of rounded corners in PDF calls // PDFGraphicsPaiter.cubicBezierTo(); the check is done by verifying that a " c " command is written // to the PDFContentGenerator - PDFContentGenerator pdfContentGenerator = mock(PDFContentGenerator.class); - FOUserAgent foUserAgent = mock(FOUserAgent.class); - when(foUserAgent.isAccessibilityEnabled()).thenReturn(false); - IFContext ifContext = mock(IFContext.class); - when(ifContext.getUserAgent()).thenReturn(foUserAgent); - PDFDocumentHandler pdfDocumentHandler = mock(PDFDocumentHandler.class); - when(pdfDocumentHandler.getGenerator()).thenReturn(pdfContentGenerator); - when(pdfDocumentHandler.getContext()).thenReturn(ifContext); - PDFLogicalStructureHandler pdfLogicalStructureHandler = mock(PDFLogicalStructureHandler.class); - PDFPainter pdfPainter = new PDFPainter(pdfDocumentHandler, pdfLogicalStructureHandler); + createPDFPainter(false); // build rectangle 200 x 50 (points, which are converted to milipoints) Rectangle rectangle = new Rectangle(0, 0, 200000, 50000); // build border properties: width 4pt, radius 30pt @@ -63,4 +73,63 @@ public class PDFPainterTestCase { verify(pdfContentGenerator, times(16)).add(endsWith(" c ")); } + private void createPDFPainter(boolean value) { + mockFOUserAgent(value); + mockPDFContentGenerator(); + mockPDFDocumentHandler(); + PDFLogicalStructureHandler handler = mock(PDFLogicalStructureHandler.class); + pdfPainter = new PDFPainter(pdfDocumentHandler, handler); + } + + private void mockFOUserAgent(boolean value) { + foUserAgent = mock(FOUserAgent.class); + when(foUserAgent.isAccessibilityEnabled()).thenReturn(value); + } + + private void mockPDFContentGenerator() { + pdfContentGenerator = mock(PDFContentGenerator.class); + } + + private void mockPDFDocumentHandler() { + pdfDocumentHandler = mock(PDFDocumentHandler.class); + when(pdfDocumentHandler.getGenerator()).thenReturn(pdfContentGenerator); + IFContext ifContext = mock(IFContext.class); + when(ifContext.getUserAgent()).thenReturn(foUserAgent); + when(pdfDocumentHandler.getContext()).thenReturn(ifContext); + when(ifContext.getStructureTreeElement()).thenReturn(elem); + } + + private PDFDocument createMockPDFDocument() { + PDFDocument pdfDoc = mock(PDFDocument.class); + when(pdfContentGenerator.getDocument()).thenReturn(pdfDoc); + when(pdfDocumentHandler.getPDFDocument()).thenReturn(pdfDoc); + when(pdfDoc.getProfile()).thenReturn(new PDFProfile(pdfDoc)); + return pdfDoc; + } + + @Test + public void testPageNumber() throws IFException { + FopFactory fopFactory = FopFactory.newInstance(new File(".").toURI()); + foUserAgent = fopFactory.newFOUserAgent(); + pdfDocumentHandler = new PDFDocumentHandler(new IFContext(foUserAgent)); + pdfDocumentHandler.setResult(new StreamResult(new ByteArrayOutputStream())); + pdfDocumentHandler.startDocument(); + pdfDocumentHandler.startPage(0, "", "", new Dimension()); + pdfDocumentHandler.getContext().setPageNumber(3); + MyPDFPainter pdfPainter = new MyPDFPainter(pdfDocumentHandler, null); + pdfPainter.drawImage("test/resources/images/cmyk.jpg", new Rectangle()); + Assert.assertEquals(pdfPainter.renderingContext.getHints().get("page-number"), 3); + } + + class MyPDFPainter extends PDFPainter { + protected RenderingContext renderingContext; + public MyPDFPainter(PDFDocumentHandler documentHandler, PDFLogicalStructureHandler logicalStructureHandler) { + super(documentHandler, logicalStructureHandler); + } + + protected RenderingContext createRenderingContext() { + renderingContext = super.createRenderingContext(); + return renderingContext; + } + } } -- 2.39.5