From 4d2cb06d14f480d24bd2a10198940811748d730e Mon Sep 17 00:00:00 2001 From: Jeremias Maerki Date: Wed, 19 Oct 2005 16:30:58 +0000 Subject: [PATCH] Fix to make the Graphics.create() method (calls a copy constructor) work. Extracted stuff into a PDFContext class which gets passed to each copy of PDFDocumentGraphics2D, so the state is uniform over all copies. git-svn-id: https://svn.apache.org/repos/asf/xmlgraphics/fop/trunk@326600 13f79535-47bb-0310-9956-ffa450edef68 --- src/java/org/apache/fop/svg/PDFContext.java | 80 +++++++++++++++++ .../apache/fop/svg/PDFDocumentGraphics2D.java | 90 ++++++++----------- .../org/apache/fop/svg/PDFGraphics2D.java | 11 +++ .../org/apache/fop/svg/PDFTranscoder.java | 5 +- 4 files changed, 132 insertions(+), 54 deletions(-) create mode 100644 src/java/org/apache/fop/svg/PDFContext.java diff --git a/src/java/org/apache/fop/svg/PDFContext.java b/src/java/org/apache/fop/svg/PDFContext.java new file mode 100644 index 000000000..0c1bd8f08 --- /dev/null +++ b/src/java/org/apache/fop/svg/PDFContext.java @@ -0,0 +1,80 @@ +/* + * Copyright 2005 The Apache Software Foundation. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +/* $Id$ */ + +package org.apache.fop.svg; + +import java.util.List; + +import org.apache.fop.pdf.PDFPage; + +/** + * Context class which holds state information which should remain in sync over multiple instances + * of PDFDocumentGraphics2D. + */ +public class PDFContext { + + private PDFPage currentPage; + private List fontList; + + /** number of pages generated */ + private int pagecount; + + /** + * Sets the font list as creates by the FontSetup class. + * @param list the font list + */ + public void setFontList(List list) { + this.fontList = list; + } + + /** @return the font list */ + public List getFontList() { + return this.fontList; + } + + /** @return true if a page is set up for painting. */ + public boolean isPagePending() { + return this.currentPage != null; + } + + /** + * After this call, there's no current page. + */ + public void clearCurrentPage() { + currentPage = null; + } + + /** @return the current page or null if there is none */ + public PDFPage getCurrentPage() { + return this.currentPage; + } + + /** + * Sets the current page + * @param page the page + */ + public void setCurrentPage(PDFPage page) { + this.currentPage = page; + } + + /** Notifies the context to increase the page count. */ + public void increasePageCount() { + this.pagecount++; + } + +} diff --git a/src/java/org/apache/fop/svg/PDFDocumentGraphics2D.java b/src/java/org/apache/fop/svg/PDFDocumentGraphics2D.java index 00d1ec5d7..d90857743 100644 --- a/src/java/org/apache/fop/svg/PDFDocumentGraphics2D.java +++ b/src/java/org/apache/fop/svg/PDFDocumentGraphics2D.java @@ -1,5 +1,5 @@ /* - * Copyright 1999-2004 The Apache Software Foundation. + * Copyright 1999-2005 The Apache Software Foundation. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -34,8 +34,6 @@ import org.apache.avalon.framework.activity.Initializable; import org.apache.avalon.framework.configuration.Configurable; import org.apache.avalon.framework.configuration.Configuration; import org.apache.avalon.framework.configuration.ConfigurationException; -import org.apache.commons.logging.impl.SimpleLog; -import org.apache.commons.logging.Log; import java.awt.Graphics; import java.awt.Font; @@ -47,7 +45,6 @@ import java.awt.geom.AffineTransform; import java.io.OutputStream; import java.io.IOException; import java.io.StringWriter; -import java.util.List; /** * This class is a wrapper for the PDFGraphics2D that @@ -55,13 +52,13 @@ import java.util.List; * PDFGraphics2D. * * @author Keiron Liddle - * @version $Id: PDFDocumentGraphics2D.java,v 1.27 2003/03/07 09:51:26 jeremias Exp $ + * @version $Id$ * @see org.apache.fop.svg.PDFGraphics2D */ public class PDFDocumentGraphics2D extends PDFGraphics2D implements Configurable, Initializable { - private PDFPage currentPage; + private PDFContext pdfContext; private int width; private int height; @@ -70,13 +67,6 @@ public class PDFDocumentGraphics2D extends PDFGraphics2D private float svgWidth; private float svgHeight; - private List fontList; - - /** number of pages generated */ - protected int pagecount; - /** indicates whether a page is currently being generated */ - protected boolean pagePending; - /** Initial clipping area, used to restore to original setting when a new page is started. */ protected Shape initialClip; /** @@ -85,8 +75,6 @@ public class PDFDocumentGraphics2D extends PDFGraphics2D */ protected AffineTransform initialTransform; - private Log logger; - //Avalon component private Configuration cfg; @@ -104,6 +92,7 @@ public class PDFDocumentGraphics2D extends PDFGraphics2D public PDFDocumentGraphics2D(boolean textAsShapes) { super(textAsShapes); + this.pdfContext = new PDFContext(); if (!textAsShapes) { fontInfo = new FontInfo(); FontSetup.setup(fontInfo, null); @@ -147,23 +136,7 @@ public class PDFDocumentGraphics2D extends PDFGraphics2D * This constructor is Avalon-style. */ public PDFDocumentGraphics2D() { - super(false); - } - - public void setLogger(Log logger) { - this.logger = logger; - } - - /** - * Returns the logger. - * @return Logger the logger - */ - protected final Log getLogger() { - if (this.logger == null) { - this.logger = new SimpleLog("FOP/PDF"); - ((SimpleLog) logger).setLevel(SimpleLog.LOG_LEVEL_INFO); - } - return this.logger; + this(false); } /** @@ -171,7 +144,7 @@ public class PDFDocumentGraphics2D extends PDFGraphics2D */ public void configure(Configuration cfg) throws ConfigurationException { this.cfg = cfg; - this.fontList = FontSetup.buildFontListFromConfiguration(cfg); + this.pdfContext.setFontList(FontSetup.buildFontListFromConfiguration(cfg)); } /** @@ -180,7 +153,7 @@ public class PDFDocumentGraphics2D extends PDFGraphics2D public void initialize() throws Exception { if (this.fontInfo == null) { fontInfo = new FontInfo(); - FontSetup.setup(fontInfo, this.fontList); + FontSetup.setup(fontInfo, this.pdfContext.getFontList()); //FontState fontState = new FontState("Helvetica", "normal", // FontInfo.NORMAL, 12, 0); } @@ -257,34 +230,39 @@ public class PDFDocumentGraphics2D extends PDFGraphics2D currentStream.write("Q\n"); } + /** + * Is called to prepare the PDFDocumentGraphics2D for the next page to be painted. Basically, + * this closes the current page. A new page is prepared as soon as painting starts. + */ public void nextPage() { closePage(); } - + /** + * Closes the current page and adds it to the PDF file. + */ protected void closePage() { - if (!this.pagePending) { + if (!pdfContext.isPagePending()) { return; //ignore } //Finish page - PDFStream pdfStream = this.pdfDoc.getFactory().makeStream(PDFFilterList.CONTENT_FILTER, false); + PDFStream pdfStream = this.pdfDoc.getFactory().makeStream( + PDFFilterList.CONTENT_FILTER, false); pdfStream.add(getString()); currentStream = null; this.pdfDoc.registerObject(pdfStream); - currentPage.setContents(pdfStream); - PDFAnnotList annots = currentPage.getAnnotations(); + pdfContext.getCurrentPage().setContents(pdfStream); + PDFAnnotList annots = pdfContext.getCurrentPage().getAnnotations(); if (annots != null) { this.pdfDoc.addObject(annots); } - this.pdfDoc.addObject(currentPage); - this.currentPage = null; - - this.pagePending = false; + this.pdfDoc.addObject(pdfContext.getCurrentPage()); + pdfContext.clearCurrentPage(); } /** {@inheritDoc} */ protected void preparePainting() { - if (this.pagePending) { + if (pdfContext.isPagePending()) { return; } try { @@ -294,8 +272,12 @@ public class PDFDocumentGraphics2D extends PDFGraphics2D } } + /** + * Called to prepare a new page + * @throws IOException if starting the new page fails due to I/O errors. + */ protected void startPage() throws IOException { - if (this.pagePending) { + if (pdfContext.isPagePending()) { throw new IllegalStateException("Close page first before starting another"); } //Start page @@ -318,10 +300,11 @@ public class PDFDocumentGraphics2D extends PDFGraphics2D } PDFResources pdfResources = this.pdfDoc.getResources(); - currentPage = this.pdfDoc.getFactory().makePage(pdfResources, - width, height); - resourceContext = currentPage; - pageRef = currentPage.referencePDF(); + PDFPage page = this.pdfDoc.getFactory().makePage(pdfResources, + width, height); + resourceContext = page; + pdfContext.setCurrentPage(page); + pageRef = page.referencePDF(); graphicsState.setTransform(new AffineTransform(1.0, 0.0, 0.0, -1.0, 0.0, (double)height)); currentStream.write("1 0 0 -1 0 " + height + " cm\n"); if (svgWidth != 0) { @@ -329,8 +312,7 @@ public class PDFDocumentGraphics2D extends PDFGraphics2D + PDFNumber.doubleOut(height / svgHeight) + " 0 0 cm\n"); } - this.pagecount++; - this.pagePending = true; + pdfContext.increasePageCount(); } @@ -361,6 +343,12 @@ public class PDFDocumentGraphics2D extends PDFGraphics2D */ public PDFDocumentGraphics2D(PDFDocumentGraphics2D g) { super(g); + this.pdfContext = g.pdfContext; + this.cfg = g.cfg; + this.width = g.width; + this.height = g.height; + this.svgWidth = g.svgWidth; + this.svgHeight = g.svgHeight; } /** diff --git a/src/java/org/apache/fop/svg/PDFGraphics2D.java b/src/java/org/apache/fop/svg/PDFGraphics2D.java index b28d7cba6..4d91422d3 100644 --- a/src/java/org/apache/fop/svg/PDFGraphics2D.java +++ b/src/java/org/apache/fop/svg/PDFGraphics2D.java @@ -207,6 +207,17 @@ public class PDFGraphics2D extends AbstractGraphics2D { */ public PDFGraphics2D(PDFGraphics2D g) { super(g); + this.pdfDoc = g.pdfDoc; + this.resourceContext = g.resourceContext; + this.currentFontName = g.currentFontName; + this.currentFontSize = g.currentFontSize; + this.fontInfo = g.fontInfo; + this.pageRef = g.pageRef; + this.graphicsState = g.graphicsState; + this.currentStream = g.currentStream; + this.jpegCount = g.jpegCount; + this.outputStream = g.outputStream; + this.ovFontState = g.ovFontState; } /** diff --git a/src/java/org/apache/fop/svg/PDFTranscoder.java b/src/java/org/apache/fop/svg/PDFTranscoder.java index 839cd46a5..dba8f84c5 100644 --- a/src/java/org/apache/fop/svg/PDFTranscoder.java +++ b/src/java/org/apache/fop/svg/PDFTranscoder.java @@ -1,5 +1,5 @@ /* - * Copyright 1999-2004 The Apache Software Foundation. + * Copyright 1999-2005 The Apache Software Foundation. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -63,7 +63,7 @@ import org.w3c.dom.svg.SVGLength; * millimeter conversion factor. * * @author Keiron Liddle - * @version $Id: PDFTranscoder.java,v 1.24 2003/03/07 09:51:26 jeremias Exp $ + * @version $Id$ */ public class PDFTranscoder extends AbstractFOPTranscoder implements Configurable { @@ -112,7 +112,6 @@ public class PDFTranscoder extends AbstractFOPTranscoder throws TranscoderException { graphics = new PDFDocumentGraphics2D(); - graphics.setLogger(getLogger()); try { if (this.cfg != null) { -- 2.39.5