From 208afa6c7df69e98c16e766aa815e603c53c293b Mon Sep 17 00:00:00 2001 From: Chris Bowditch Date: Fri, 5 Oct 2007 08:36:48 +0000 Subject: [PATCH] Bugzilla #43070 Postscript extension : comment before and after page Submitted by Adrian Cumiskey git-svn-id: https://svn.apache.org/repos/asf/xmlgraphics/fop/trunk@582124 13f79535-47bb-0310-9956-ffa450edef68 --- .../org/apache/fop/render/ps/PSRenderer.java | 104 +++++++++++++----- .../extensions/AbstractPSCommentElement.java | 54 +++++++++ .../render/ps/extensions/PSCommentAfter.java | 53 +++++++++ .../ps/extensions/PSCommentAfterElement.java | 54 +++++++++ .../render/ps/extensions/PSCommentBefore.java | 53 +++++++++ .../ps/extensions/PSCommentBeforeElement.java | 52 +++++++++ .../extensions/PSExtensionElementMapping.java | 14 +++ .../ps/extensions/PSExtensionHandler.java | 8 +- .../standard-testcases/ps-extension_2.xml | 25 ++++- 9 files changed, 383 insertions(+), 34 deletions(-) create mode 100644 src/java/org/apache/fop/render/ps/extensions/AbstractPSCommentElement.java create mode 100644 src/java/org/apache/fop/render/ps/extensions/PSCommentAfter.java create mode 100644 src/java/org/apache/fop/render/ps/extensions/PSCommentAfterElement.java create mode 100644 src/java/org/apache/fop/render/ps/extensions/PSCommentBefore.java create mode 100644 src/java/org/apache/fop/render/ps/extensions/PSCommentBeforeElement.java diff --git a/src/java/org/apache/fop/render/ps/PSRenderer.java b/src/java/org/apache/fop/render/ps/PSRenderer.java index 5d823e86e..91769f6de 100644 --- a/src/java/org/apache/fop/render/ps/PSRenderer.java +++ b/src/java/org/apache/fop/render/ps/PSRenderer.java @@ -73,6 +73,8 @@ import org.apache.fop.render.RendererContext; import org.apache.fop.render.ps.extensions.PSExtensionAttachment; import org.apache.fop.render.ps.extensions.PSSetPageDevice; import org.apache.fop.render.ps.extensions.PSSetupCode; +import org.apache.fop.render.ps.extensions.PSCommentAfter; +import org.apache.fop.render.ps.extensions.PSCommentBefore; import org.apache.fop.util.CharUtilities; import org.apache.xmlgraphics.ps.DSCConstants; @@ -135,7 +137,6 @@ public class PSRenderer extends AbstractPathOrientedRenderer implements ImageAda private boolean ioTrouble = false; private boolean inTextMode = false; - private boolean firstPageSequenceReceived = false; /** Used to temporarily store PSSetupCode instance until they can be written. */ private List setupCodeList; @@ -154,6 +155,11 @@ public class PSRenderer extends AbstractPathOrientedRenderer implements ImageAda /** Whether or not Dublin Core Standard (dsc) compliant output is enforced */ private boolean dscCompliant = true; + /** This is a collection holding all document header comments */ + private Collection headerComments; + + /** This is a collection holding all document footer comments */ + private Collection footerComments; /** * {@inheritDoc} */ @@ -729,7 +735,9 @@ public class PSRenderer extends AbstractPathOrientedRenderer implements ImageAda this.pageDeviceDictionary = new PSPageDeviceDictionary(); pageDeviceDictionary.setFlushOnRetrieval(!this.dscCompliant); pageDeviceDictionary.put("/ImagingBBox", "null"); + } + private void writeHeader() throws IOException { //PostScript Header writeln(DSCConstants.PS_ADOBE_30); gen.writeDSCComment(DSCConstants.CREATOR, new String[] {userAgent.getProducer()}); @@ -738,6 +746,12 @@ public class PSRenderer extends AbstractPathOrientedRenderer implements ImageAda gen.writeDSCComment(DSCConstants.PAGES, new Object[] {DSCConstants.ATEND}); gen.writeDSCComment(DSCConstants.DOCUMENT_SUPPLIED_RESOURCES, new Object[] {DSCConstants.ATEND}); + if (headerComments != null) { + for (Iterator iter = headerComments.iterator(); iter.hasNext();) { + PSExtensionAttachment comment = (PSExtensionAttachment)iter.next(); + gen.writeln("%" + comment.getContent()); + } + } gen.writeDSCComment(DSCConstants.END_COMMENTS); //Defaults @@ -745,6 +759,22 @@ public class PSRenderer extends AbstractPathOrientedRenderer implements ImageAda gen.writeDSCComment(DSCConstants.END_DEFAULTS); //Prolog and Setup written right before the first page-sequence, see startPageSequence() + //Do this only once, as soon as we have all the content for the Setup section! + //Prolog + gen.writeDSCComment(DSCConstants.BEGIN_PROLOG); + PSProcSets.writeStdProcSet(gen); + PSProcSets.writeEPSProcSet(gen); + gen.writeDSCComment(DSCConstants.END_PROLOG); + + //Setup + gen.writeDSCComment(DSCConstants.BEGIN_SETUP); + writeSetupCodeList(setupCodeList, "SetupCode"); + if (!twoPassGeneration) { + this.fontResources = PSFontUtils.writeFontDict(gen, fontInfo); + } else { + gen.commentln("%FOPFontSetup"); + } + gen.writeDSCComment(DSCConstants.END_SETUP); } /** @@ -763,6 +793,12 @@ public class PSRenderer extends AbstractPathOrientedRenderer implements ImageAda //Write trailer gen.writeDSCComment(DSCConstants.TRAILER); + if (footerComments != null) { + for (Iterator iter = footerComments.iterator(); iter.hasNext();) { + PSExtensionAttachment comment = (PSExtensionAttachment)iter.next(); + gen.commentln("%" + comment.getContent()); + } + } gen.writeDSCComment(DSCConstants.PAGES, new Integer(this.currentPageNumber)); gen.getResourceTracker().writeResources(false, gen); gen.writeDSCComment(DSCConstants.EOF); @@ -840,7 +876,17 @@ public class PSRenderer extends AbstractPathOrientedRenderer implements ImageAda + e.getMessage() + ", content = '" + content + "'"); } } - } + } else if (attachment instanceof PSCommentBefore) { + if (headerComments == null) { + headerComments = new java.util.TreeSet(); + } + headerComments.add(attachment); + } else if (attachment instanceof PSCommentAfter) { + if (footerComments == null) { + footerComments = new java.util.TreeSet(); + } + footerComments.add(attachment); + } } } } @@ -850,30 +896,6 @@ public class PSRenderer extends AbstractPathOrientedRenderer implements ImageAda /** {@inheritDoc} */ public void startPageSequence(LineArea seqTitle) { super.startPageSequence(seqTitle); - if (!firstPageSequenceReceived) { - //Do this only once, as soon as we have all the content for the Setup section! - try { - //Prolog - gen.writeDSCComment(DSCConstants.BEGIN_PROLOG); - PSProcSets.writeStdProcSet(gen); - PSProcSets.writeEPSProcSet(gen); - gen.writeDSCComment(DSCConstants.END_PROLOG); - - //Setup - gen.writeDSCComment(DSCConstants.BEGIN_SETUP); - writeSetupCodeList(setupCodeList, "SetupCode"); - if (!twoPassGeneration) { - this.fontResources = PSFontUtils.writeFontDict(gen, fontInfo); - } else { - gen.commentln("%FOPFontSetup"); - } - gen.writeDSCComment(DSCConstants.END_SETUP); - } catch (IOException ioe) { - handleIOTrouble(ioe); - } - - firstPageSequenceReceived = true; - } } /** @@ -911,6 +933,10 @@ public class PSRenderer extends AbstractPathOrientedRenderer implements ImageAda throws IOException, FOPException { log.debug("renderPage(): " + page); + if (this.currentPageNumber == 0) { + writeHeader(); + } + this.currentPageNumber++; gen.getResourceTracker().notifyStartNewPage(); @@ -994,6 +1020,17 @@ public class PSRenderer extends AbstractPathOrientedRenderer implements ImageAda gen.writeDSCComment(DSCConstants.BEGIN_PAGE_SETUP); + if (page.hasExtensionAttachments()) { + List extensionAttachments = page.getExtensionAttachments(); + for (int i = 0; i < extensionAttachments.size(); i++) { + PSExtensionAttachment attachment + = (PSExtensionAttachment)extensionAttachments.get(i); + if (attachment instanceof PSCommentBefore) { + gen.commentln("%" + attachment.getContent()); + } + } + } + // Write any unwritten changes to page device dictionary if (!pageDeviceDictionary.isEmpty()) { String content = pageDeviceDictionary.getContent(); @@ -1019,7 +1056,16 @@ public class PSRenderer extends AbstractPathOrientedRenderer implements ImageAda //Show page writeln("showpage"); gen.writeDSCComment(DSCConstants.PAGE_TRAILER); - + if (page.hasExtensionAttachments()) { + List extensionAttachments = page.getExtensionAttachments(); + for (int i = 0; i < extensionAttachments.size(); i++) { + PSExtensionAttachment attachment; + attachment = (PSExtensionAttachment)extensionAttachments.get(i); + if (attachment instanceof PSCommentAfter) { + gen.commentln("%" + attachment.getContent()); + } + } + } gen.getResourceTracker().writeResources(true, gen); } @@ -1385,7 +1431,7 @@ public class PSRenderer extends AbstractPathOrientedRenderer implements ImageAda iter.remove(); } } - + /** * Sets whether or not the safe set page device macro should be used * (as opposed to directly invoking setpagedevice) when setting the @@ -1416,5 +1462,5 @@ public class PSRenderer extends AbstractPathOrientedRenderer implements ImageAda */ public void setDSCCompliant(boolean dscCompliant) { this.dscCompliant = dscCompliant; - } + } } diff --git a/src/java/org/apache/fop/render/ps/extensions/AbstractPSCommentElement.java b/src/java/org/apache/fop/render/ps/extensions/AbstractPSCommentElement.java new file mode 100644 index 000000000..a6e77fb13 --- /dev/null +++ b/src/java/org/apache/fop/render/ps/extensions/AbstractPSCommentElement.java @@ -0,0 +1,54 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You 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.render.ps.extensions; + +import org.apache.fop.apps.FOPException; +import org.apache.fop.fo.Constants; +import org.apache.fop.fo.FONode; +import org.apache.fop.fo.ValidationException; + +/** + * Base postscript commment element class + */ +public abstract class AbstractPSCommentElement extends AbstractPSExtensionElement { + + /** + * Default constructor + * + * @param parent parent of this node + * @see org.apache.fop.fo.FONode#FONode(FONode) + */ + public AbstractPSCommentElement(FONode parent) { + super(parent); + } + + /** + * @throws FOPException if there's a problem during processing + * @see org.apache.fop.fo.FONode#startOfNode() + */ + protected void startOfNode() throws FOPException { + if (parent.getNameId() != Constants.FO_DECLARATIONS + && parent.getNameId() != Constants.FO_SIMPLE_PAGE_MASTER) { + throw new ValidationException(getName() + + " must be a child of fo:declarations or fo:simple-page-master."); + } + } + +} diff --git a/src/java/org/apache/fop/render/ps/extensions/PSCommentAfter.java b/src/java/org/apache/fop/render/ps/extensions/PSCommentAfter.java new file mode 100644 index 000000000..60c47b29c --- /dev/null +++ b/src/java/org/apache/fop/render/ps/extensions/PSCommentAfter.java @@ -0,0 +1,53 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You 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.render.ps.extensions; + +/** + * Custom postscript comment after class + */ +public class PSCommentAfter extends PSExtensionAttachment { + + /** + * Element name + */ + protected static final String ELEMENT = "ps-comment-after"; + + /** + * Default constructor + */ + public PSCommentAfter() { + super(); + } + + /** + * Constructor + * @param content comment + */ + public PSCommentAfter(String content) { + super(content); + } + + /** + * @return the element name + */ + protected String getElement() { + return ELEMENT; + } +} diff --git a/src/java/org/apache/fop/render/ps/extensions/PSCommentAfterElement.java b/src/java/org/apache/fop/render/ps/extensions/PSCommentAfterElement.java new file mode 100644 index 000000000..2dbbf1f36 --- /dev/null +++ b/src/java/org/apache/fop/render/ps/extensions/PSCommentAfterElement.java @@ -0,0 +1,54 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You 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.render.ps.extensions; + +import org.apache.fop.fo.FONode; +import org.apache.fop.fo.extensions.ExtensionAttachment; + +/** + * Comment after element + */ +public class PSCommentAfterElement extends AbstractPSCommentElement { + + /** + * Main constructor + * @param parent node + */ + public PSCommentAfterElement(FONode parent) { + super(parent); + } + + /** + * @return local name + * @see org.apache.fop.fo.FONode#getLocalName() + */ + public String getLocalName() { + return "ps-comment-after"; + } + + /** + * @return instance of its extension attachment object + * @see org.apache.fop.render.ps.extensions.AbstractPSExtensionElement + * #instantiateExtensionAttachment() + */ + protected ExtensionAttachment instantiateExtensionAttachment() { + return new PSCommentAfter(); + } +} diff --git a/src/java/org/apache/fop/render/ps/extensions/PSCommentBefore.java b/src/java/org/apache/fop/render/ps/extensions/PSCommentBefore.java new file mode 100644 index 000000000..60b6f2e4f --- /dev/null +++ b/src/java/org/apache/fop/render/ps/extensions/PSCommentBefore.java @@ -0,0 +1,53 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You 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.render.ps.extensions; + +/** + * Custom postscript comment before class + */ +public class PSCommentBefore extends PSExtensionAttachment { + + /** + * Element name + */ + protected static final String ELEMENT = "ps-comment-before"; + + /** + * Default constructor + * @param content the actual comment + */ + public PSCommentBefore(String content) { + super(content); + } + + /** + * Constructor + */ + public PSCommentBefore() { + super(); + } + + /** + * @return element name + */ + protected String getElement() { + return ELEMENT; + } +} diff --git a/src/java/org/apache/fop/render/ps/extensions/PSCommentBeforeElement.java b/src/java/org/apache/fop/render/ps/extensions/PSCommentBeforeElement.java new file mode 100644 index 000000000..7abb67007 --- /dev/null +++ b/src/java/org/apache/fop/render/ps/extensions/PSCommentBeforeElement.java @@ -0,0 +1,52 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You 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.render.ps.extensions; + +import org.apache.fop.fo.FONode; +import org.apache.fop.fo.extensions.ExtensionAttachment; + +/** + * Comment before element + */ +public class PSCommentBeforeElement extends AbstractPSCommentElement { + + /** + * Main constructor + * @param parent parent node + */ + public PSCommentBeforeElement(FONode parent) { + super(parent); + } + + /** + * @return local name + * @see org.apache.fop.fo.FONode#getLocalName() + */ + public String getLocalName() { + return "ps-comment-before"; + } + + /** + * @return instance of its extension attachment object + */ + protected ExtensionAttachment instantiateExtensionAttachment() { + return new PSCommentBefore(); + } +} diff --git a/src/java/org/apache/fop/render/ps/extensions/PSExtensionElementMapping.java b/src/java/org/apache/fop/render/ps/extensions/PSExtensionElementMapping.java index a90af2a9c..bfb402d29 100644 --- a/src/java/org/apache/fop/render/ps/extensions/PSExtensionElementMapping.java +++ b/src/java/org/apache/fop/render/ps/extensions/PSExtensionElementMapping.java @@ -42,6 +42,8 @@ public class PSExtensionElementMapping extends ElementMapping { foObjs.put("ps-setup-code", new PSSetupCodeMaker()); foObjs.put("ps-page-setup-code", new PSPageSetupCodeMaker()); foObjs.put("ps-setpagedevice", new PSSetPageDeviceMaker()); + foObjs.put("ps-comment-before", new PSCommentBeforeMaker()); + foObjs.put("ps-comment-after", new PSCommentAfterMaker()); } } @@ -62,4 +64,16 @@ public class PSExtensionElementMapping extends ElementMapping { return new PSSetPageDeviceElement(parent); } } + + static class PSCommentBeforeMaker extends ElementMapping.Maker { + public FONode make(FONode parent) { + return new PSCommentBeforeElement(parent); + } + } + + static class PSCommentAfterMaker extends ElementMapping.Maker { + public FONode make(FONode parent) { + return new PSCommentAfterElement(parent); + } + } } diff --git a/src/java/org/apache/fop/render/ps/extensions/PSExtensionHandler.java b/src/java/org/apache/fop/render/ps/extensions/PSExtensionHandler.java index 6cc41f8cc..72f665a0f 100644 --- a/src/java/org/apache/fop/render/ps/extensions/PSExtensionHandler.java +++ b/src/java/org/apache/fop/render/ps/extensions/PSExtensionHandler.java @@ -50,7 +50,9 @@ public class PSExtensionHandler extends DefaultHandler lastAttributes = attributes; handled = false; if (localName.equals(PSSetupCode.ELEMENT) - || localName.equals(PSSetPageDevice.ELEMENT)) { + || localName.equals(PSSetPageDevice.ELEMENT) + || localName.equals(PSCommentBefore.ELEMENT) + || localName.equals(PSCommentAfter.ELEMENT)) { //handled in endElement handled = true; } @@ -75,6 +77,10 @@ public class PSExtensionHandler extends DefaultHandler } else if (PSSetPageDevice.ELEMENT.equals(localName)) { String name = lastAttributes.getValue("name"); this.returnedObject = new PSSetPageDevice(name, content.toString()); + } else if (PSCommentBefore.ELEMENT.equals(localName)) { + this.returnedObject = new PSCommentBefore(content.toString()); + } else if (PSCommentAfter.ELEMENT.equals(localName)) { + this.returnedObject = new PSCommentAfter(content.toString()); } } content.setLength(0); //Reset text buffer (see characters()) diff --git a/test/layoutengine/standard-testcases/ps-extension_2.xml b/test/layoutengine/standard-testcases/ps-extension_2.xml index d0c18a977..6429b789e 100644 --- a/test/layoutengine/standard-testcases/ps-extension_2.xml +++ b/test/layoutengine/standard-testcases/ps-extension_2.xml @@ -28,10 +28,14 @@ > ]]> + A4 before page + A4 after page > ]]> + A4a before page + A4a after page @@ -42,6 +46,8 @@ > ]]> + header comment + footer comment @@ -55,16 +61,27 @@ - + + + + + + - + + + - + + + - + + + -- 2.39.5