Index: C:/Dev/apache.org/rw/xmlgraphics-commons-trunk/test/java/org/apache/xmlgraphics/ps/dsc/tools/DSCToolsTestCase.java =================================================================== --- C:/Dev/apache.org/rw/xmlgraphics-commons-trunk/test/java/org/apache/xmlgraphics/ps/dsc/tools/DSCToolsTestCase.java (revision 0) +++ C:/Dev/apache.org/rw/xmlgraphics-commons-trunk/test/java/org/apache/xmlgraphics/ps/dsc/tools/DSCToolsTestCase.java (revision 0) @@ -0,0 +1,57 @@ +/* + * 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.xmlgraphics.ps.dsc.tools; + +import org.apache.xmlgraphics.ps.dsc.events.DSCCommentEndComments; +import org.apache.xmlgraphics.ps.dsc.events.DSCCommentPages; +import org.apache.xmlgraphics.ps.dsc.events.DSCEvent; +import org.apache.xmlgraphics.ps.dsc.events.PostScriptComment; +import org.apache.xmlgraphics.ps.dsc.events.PostScriptLine; + +import junit.framework.TestCase; + +public class DSCToolsTestCase extends TestCase { + + public void testEndComment() throws Exception { + DSCEvent event; + + event = new DSCCommentEndComments(); + assertTrue(DSCTools.headerCommentsEndHere(event)); + + event = new PostScriptComment("FOPTest"); + assertFalse(DSCTools.headerCommentsEndHere(event)); + + event = new DSCCommentPages(7); + assertFalse(DSCTools.headerCommentsEndHere(event)); + + event = new PostScriptComment(null); + assertTrue(DSCTools.headerCommentsEndHere(event)); + + event = new PostScriptComment("\t"); + assertTrue(DSCTools.headerCommentsEndHere(event)); + + event = new PostScriptComment(" ***"); + assertTrue(DSCTools.headerCommentsEndHere(event)); + + event = new PostScriptLine("/pgsave save def"); + assertTrue(DSCTools.headerCommentsEndHere(event)); + } + +} Property changes on: C:\Dev\apache.org\rw\xmlgraphics-commons-trunk\test\java\org\apache\xmlgraphics\ps\dsc\tools\DSCToolsTestCase.java ___________________________________________________________________ Name: svn:eol-style + native Index: C:/Dev/apache.org/rw/xmlgraphics-commons-trunk/test/java/org/apache/xmlgraphics/ps/dsc/events/DSCValueParserTestCase.java =================================================================== --- C:/Dev/apache.org/rw/xmlgraphics-commons-trunk/test/java/org/apache/xmlgraphics/ps/dsc/events/DSCValueParserTestCase.java (revision 0) +++ C:/Dev/apache.org/rw/xmlgraphics-commons-trunk/test/java/org/apache/xmlgraphics/ps/dsc/events/DSCValueParserTestCase.java (revision 0) @@ -0,0 +1,72 @@ +/* + * 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.xmlgraphics.ps.dsc.events; + +import java.util.List; + +import org.apache.xmlgraphics.ps.dsc.events.DSCCommentBeginResource; + +import junit.framework.TestCase; + +public class DSCValueParserTestCase extends TestCase { + + private String[] toArray(List params) { + return (String[])params.toArray(new String[params.size()]); + } + + public void testText() throws Exception { + DSCCommentBeginResource obj = new DSCCommentBeginResource(); + String[] res = toArray(obj.splitParams("procset Test")); + assertEquals(2, res.length); + assertEquals("procset", res[0]); + assertEquals("Test", res[1]); + + res = toArray(obj.splitParams("procset\tTest")); + assertEquals(2, res.length); + assertEquals("procset", res[0]); + assertEquals("Test", res[1]); + } + + public void testParentheseText() throws Exception { + DSCCommentBeginResource obj = new DSCCommentBeginResource(); + String[] res = toArray(obj.splitParams("procset (Hello World!)")); + assertEquals(2, res.length); + assertEquals("procset", res[0]); + assertEquals("Hello World!", res[1]); + + res = toArray(obj.splitParams("procset\t(Hello\t\\\\wonderful/ World!)")); + assertEquals(2, res.length); + assertEquals("procset", res[0]); + assertEquals("Hello\t\\wonderful/ World!", res[1]); + + res = toArray(obj.splitParams("procset (Hello \\042wonderful\\042 World!) blahblah")); + assertEquals(3, res.length); + assertEquals("procset", res[0]); + assertEquals("Hello \"wonderful\" World!", res[1]); + assertEquals("blahblah", res[2]); + + //Parentheses not balanced + res = toArray(obj.splitParams("procset (Hello (wonderful) World! blahblah")); + assertEquals(2, res.length); + assertEquals("procset", res[0]); + assertEquals("Hello (wonderful) World! blahblah", res[1]); + } + +} Property changes on: C:\Dev\apache.org\rw\xmlgraphics-commons-trunk\test\java\org\apache\xmlgraphics\ps\dsc\events\DSCValueParserTestCase.java ___________________________________________________________________ Name: svn:eol-style + native Index: C:/Dev/apache.org/rw/xmlgraphics-commons-trunk/test/java/org/apache/xmlgraphics/StandardTestSuite.java =================================================================== --- C:/Dev/apache.org/rw/xmlgraphics-commons-trunk/test/java/org/apache/xmlgraphics/StandardTestSuite.java (revision 500235) +++ C:/Dev/apache.org/rw/xmlgraphics-commons-trunk/test/java/org/apache/xmlgraphics/StandardTestSuite.java (working copy) @@ -20,6 +20,8 @@ package org.apache.xmlgraphics; import org.apache.xmlgraphics.image.codec.png.PNGEncoderTest; +import org.apache.xmlgraphics.ps.dsc.events.DSCValueParserTestCase; +import org.apache.xmlgraphics.ps.dsc.tools.DSCToolsTestCase; import org.apache.xmlgraphics.util.ServiceTest; import org.apache.xmlgraphics.util.io.ASCII85InputStreamTestCase; import org.apache.xmlgraphics.util.io.ASCII85OutputStreamTestCase; @@ -46,6 +48,8 @@ suite.addTest(new TestSuite(ASCII85OutputStreamTestCase.class)); suite.addTest(new TestSuite(PNGEncoderTest.class)); suite.addTest(new TestSuite(ServiceTest.class)); + suite.addTest(new TestSuite(DSCValueParserTestCase.class)); + suite.addTest(new TestSuite(DSCToolsTestCase.class)); //$JUnit-END$ return suite; } Index: C:/Dev/apache.org/rw/xmlgraphics-commons-trunk/src/java/org/apache/xmlgraphics/ps/PSProcSet.java =================================================================== --- C:/Dev/apache.org/rw/xmlgraphics-commons-trunk/src/java/org/apache/xmlgraphics/ps/PSProcSet.java (revision 0) +++ C:/Dev/apache.org/rw/xmlgraphics-commons-trunk/src/java/org/apache/xmlgraphics/ps/PSProcSet.java (revision 0) @@ -0,0 +1,69 @@ +/* + * 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.xmlgraphics.ps; + +/** + * PSResource subclass that represents a ProcSet resource. + */ +public class PSProcSet extends PSResource { + + private float version; + private int revision; + + /** + * Creates a new instance. + * @param name name of the resource + */ + public PSProcSet(String name) { + this(name, 1.0f, 0); + } + + /** + * Creates a new instance. + * @param name name of the resource + * @param version version of the resource + * @param revision revision of the resource + */ + public PSProcSet(String name, float version, int revision) { + super(TYPE_PROCSET, name); + this.version = version; + this.revision = revision; + } + + /** @return the version */ + public float getVersion() { + return version; + } + + /** @return the revision */ + public int getRevision() { + return revision; + } + + /** @return the specification as defined in DSC v3.0 spec. */ + public String getResourceSpecification() { + StringBuffer sb = new StringBuffer(); + sb.append(getType()).append(" ").append(PSGenerator.convertStringToDSC(getName())); + sb.append(" ").append(PSGenerator.convertRealToDSC(getVersion())); + sb.append(" ").append(Integer.toString(getRevision())); + return sb.toString(); + } + +} Property changes on: C:\Dev\apache.org\rw\xmlgraphics-commons-trunk\src\java\org\apache\xmlgraphics\ps\PSProcSet.java ___________________________________________________________________ Name: svn:eol-style + native Index: C:/Dev/apache.org/rw/xmlgraphics-commons-trunk/src/java/org/apache/xmlgraphics/ps/PSProcSets.java =================================================================== --- C:/Dev/apache.org/rw/xmlgraphics-commons-trunk/src/java/org/apache/xmlgraphics/ps/PSProcSets.java (revision 500235) +++ C:/Dev/apache.org/rw/xmlgraphics-commons-trunk/src/java/org/apache/xmlgraphics/ps/PSProcSets.java (working copy) @@ -22,33 +22,34 @@ import java.io.IOException; /** - * This class defines the basic resources (procsets) used by FOP's PostScript - * renderer and SVG transcoder. + * This class defines the basic resources (procsets) used by the Apache XML Graphics project. * - * @author Apache FOP Development Team * @version $Id$ */ public final class PSProcSets { - /** the standard FOP procset */ + /** the standard procset for the XML Graphics project */ public static final PSResource STD_PROCSET = new StdProcSet(); - /** the EPS FOP procset */ + /** the EPS procset for the XML Graphics project */ public static final PSResource EPS_PROCSET = new EPSProcSet(); - private static class StdProcSet extends PSResource { + private static class StdProcSet extends PSProcSet { public StdProcSet() { - super("procset", "Apache FOP Std ProcSet"); + super("Apache XML Graphics Std ProcSet", 1.0f, 0); } public void writeTo(PSGenerator gen) throws IOException { gen.writeDSCComment(DSCConstants.BEGIN_RESOURCE, - new Object[] {"procset", getName(), "1.0", "0"}); + new Object[] {TYPE_PROCSET, getName(), + Float.toString(getVersion()), Integer.toString(getRevision())}); gen.writeDSCComment(DSCConstants.VERSION, - new Object[] {"1.0", "0"}); + new Object[] {Float.toString(getVersion()), Integer.toString(getRevision())}); gen.writeDSCComment(DSCConstants.COPYRIGHT, "Copyright 2001-2003 " - + "The Apache Software Foundation. All rights reserved."); - gen.writeDSCComment(DSCConstants.TITLE, "Basic set of procedures used by FOP"); + + "The Apache Software Foundation. " + + "License terms: http://www.apache.org/licenses/LICENSE-2.0"); + gen.writeDSCComment(DSCConstants.TITLE, + "Basic set of procedures used by the XML Graphics project (Batik and FOP)"); gen.writeln("/bd{bind def}bind def"); gen.writeln("/ld{load def}bd"); @@ -140,24 +141,28 @@ gen.writeln("} bd"); gen.writeDSCComment(DSCConstants.END_RESOURCE); + gen.getResourceTracker().registerSuppliedResource(this); } } - private static class EPSProcSet extends PSResource { + private static class EPSProcSet extends PSProcSet { public EPSProcSet() { - super("procset", "Apache FOP EPS ProcSet"); + super("Apache XML Graphics EPS ProcSet", 1.0f, 0); } public void writeTo(PSGenerator gen) throws IOException { gen.writeDSCComment(DSCConstants.BEGIN_RESOURCE, - new Object[] {"procset", getName(), "1.0", "0"}); + new Object[] {TYPE_PROCSET, getName(), + Float.toString(getVersion()), Integer.toString(getRevision())}); gen.writeDSCComment(DSCConstants.VERSION, - new Object[] {"1.0", "0"}); + new Object[] {Float.toString(getVersion()), Integer.toString(getRevision())}); gen.writeDSCComment(DSCConstants.COPYRIGHT, "Copyright 2002-2003 " - + "The Apache Software Foundation. All rights reserved."); - gen.writeDSCComment(DSCConstants.TITLE, "EPS procedures used by FOP"); + + "The Apache Software Foundation. " + + "License terms: http://www.apache.org/licenses/LICENSE-2.0"); + gen.writeDSCComment(DSCConstants.TITLE, + "EPS procedures used by the Apache XML Graphics project (Batik and FOP)"); gen.writeln("/BeginEPSF { %def"); gen.writeln("/b4_Inc_state save def % Save state for cleanup"); @@ -183,6 +188,7 @@ gen.writeln("} bd"); gen.writeDSCComment(DSCConstants.END_RESOURCE); + gen.getResourceTracker().registerSuppliedResource(this); } } @@ -192,7 +198,7 @@ * @param gen PSGenerator to use for output * @throws IOException In case of an I/O problem */ - public static void writeFOPStdProcSet(PSGenerator gen) throws IOException { + public static void writeStdProcSet(PSGenerator gen) throws IOException { ((StdProcSet)STD_PROCSET).writeTo(gen); } @@ -202,7 +208,7 @@ * @param gen PSGenerator to use for output * @throws IOException In case of an I/O problem */ - public static void writeFOPEPSProcSet(PSGenerator gen) throws IOException { + public static void writeEPSProcSet(PSGenerator gen) throws IOException { ((EPSProcSet)EPS_PROCSET).writeTo(gen); } Index: C:/Dev/apache.org/rw/xmlgraphics-commons-trunk/src/java/org/apache/xmlgraphics/ps/DSCConstants.java =================================================================== --- C:/Dev/apache.org/rw/xmlgraphics-commons-trunk/src/java/org/apache/xmlgraphics/ps/DSCConstants.java (revision 500235) +++ C:/Dev/apache.org/rw/xmlgraphics-commons-trunk/src/java/org/apache/xmlgraphics/ps/DSCConstants.java (working copy) @@ -46,7 +46,7 @@ public static final String CREATION_DATE = "CreationDate"; /** Type of data */ public static final String DOCUMENT_DATA = "BoundingBox"; - /** Use for inidicating an emulator being invoked in the document */ + /** Use for indicating an emulator being invoked in the document */ public static final String EMULATION = "Emulation"; /** Explicit end of comments */ public static final String END_COMMENTS = "EndComments"; @@ -135,7 +135,10 @@ public static final String PAGE_TRAILER = "PageTrailer"; /** Indicates the start of the document trailer */ public static final String TRAILER = "Trailer"; - /** Indicates the end of a page (NON-STANDARD!) */ + /** + * Indicates the end of a page (NON-STANDARD!) + * @deprecated Shouldn't really use that. Bad idea. "Page" and "Trailer" end a page. + */ public static final String END_PAGE = "EndPage"; /** Indicates the end of the document */ public static final String EOF = "EOF"; @@ -216,4 +219,25 @@ */ public static final String PAGE_RESOURCES = "PageResources"; + // ----==== (atend) indicator ====---- + + /** + * Indicator for the PostScript interpreter that the value is provided + * later in the document (mostly in the %%Trailer section). + */ + public static final Object ATEND = new AtendIndicator(); + + /** Used for the ATEND constant. See there. */ + private static final class AtendIndicator extends Object { + + private AtendIndicator() { + super(); + } + + public String toString() { + return "(atend)"; + } + } + + } Index: C:/Dev/apache.org/rw/xmlgraphics-commons-trunk/src/java/org/apache/xmlgraphics/ps/PSImageUtils.java =================================================================== --- C:/Dev/apache.org/rw/xmlgraphics-commons-trunk/src/java/org/apache/xmlgraphics/ps/PSImageUtils.java (revision 500235) +++ C:/Dev/apache.org/rw/xmlgraphics-commons-trunk/src/java/org/apache/xmlgraphics/ps/PSImageUtils.java (working copy) @@ -43,7 +43,7 @@ * Writes a bitmap image to the PostScript stream. * @param img the bitmap image as a byte array * @param imgDim the dimensions of the image - * @param imgName the name of the image + * @param imgDescription the name of the image * @param targetRect the target rectangle to place the image in * @param isJPEG true if "img" contains a DCT-encoded images, false if "img" contains the * decoded bitmap @@ -52,26 +52,17 @@ * @throws IOException In case of an I/O exception */ public static void writeImage(byte[] img, - Dimension imgDim, String imgName, + Dimension imgDim, String imgDescription, Rectangle2D targetRect, boolean isJPEG, ColorSpace colorSpace, PSGenerator gen) throws IOException { - boolean iscolor = colorSpace.getType() != ColorSpace.CS_GRAY; - gen.saveGraphicsState(); gen.writeln(gen.formatDouble(targetRect.getX()) + " " + gen.formatDouble(targetRect.getY()) + " translate"); gen.writeln(gen.formatDouble(targetRect.getWidth()) + " " + gen.formatDouble(targetRect.getHeight()) + " scale"); - gen.commentln("%FOPBeginBitmap: " + imgName); - if (colorSpace.getType() == ColorSpace.TYPE_CMYK) { - gen.writeln("/DeviceCMYK setcolorspace"); - } else if (colorSpace.getType() == ColorSpace.CS_GRAY) { - gen.writeln("/DeviceGray setcolorspace"); - } else { - gen.writeln("/DeviceRGB setcolorspace"); - } + gen.commentln("%AXGBeginBitmap: " + imgDescription); gen.writeln("{{"); // Template: (RawData is used for the EOF signal only) @@ -89,8 +80,27 @@ gen.writeln("/Data RawData /RunLengthDecode filter def"); } } - gen.writeln("<<"); - gen.writeln(" /ImageType 1"); + writeImageCommand(imgDim, colorSpace, gen, "Data"); + /* the following two lines could be enabled if something still goes wrong + * gen.write("Data closefile"); + * gen.write("RawData flushfile"); + */ + gen.writeln("} stopped {handleerror} if"); + gen.writeln(" RawData flushfile"); + gen.writeln("} exec"); + + encodeBitmap(img, isJPEG, gen); + + gen.writeln(""); + gen.commentln("%AXGEndBitmap"); + gen.restoreGraphicsState(); + } + + private static void writeImageCommand(Dimension imgDim, ColorSpace colorSpace, + PSGenerator gen, String dataSource) throws IOException { + boolean iscolor = colorSpace.getType() != ColorSpace.CS_GRAY; + prepareColorspace(colorSpace, gen); + gen.writeln("<< /ImageType 1"); gen.writeln(" /Width " + imgDim.width); gen.writeln(" /Height " + imgDim.height); gen.writeln(" /BitsPerComponent 8"); @@ -109,17 +119,138 @@ gen.writeln(" /ImageMatrix [" + imgDim.width + " 0 0 " + imgDim.height + " 0 0]"); - gen.writeln(" /DataSource Data"); - gen.writeln(">>"); - gen.writeln("image"); - /* the following two lines could be enabled if something still goes wrong - * gen.write("Data closefile"); - * gen.write("RawData flushfile"); - */ - gen.writeln("} stopped {handleerror} if"); - gen.writeln(" RawData flushfile"); - gen.writeln("} exec"); + gen.writeln(" /DataSource " + dataSource); + gen.writeln(">> image"); + } + /** + * Writes a bitmap image as a PostScript form enclosed by DSC resource wrappers to the + * PostScript file. + * @param img the raw bitmap data + * @param imgDim the dimensions of the image + * @param formName the name of the PostScript form to use + * @param imageDescription a description of the image added as a DSC Title comment + * @param isJPEG true if "img" contains a DCT-encoded images, false if "img" contains the + * decoded bitmap + * @param colorSpace the color space of the image + * @param gen the PostScript generator + * @return a PSResource representing the form for resource tracking + * @throws IOException In case of an I/O exception + */ + public static PSResource writeReusableImage(byte[] img, + Dimension imgDim, String formName, String imageDescription, + boolean isJPEG, ColorSpace colorSpace, + PSGenerator gen) throws IOException { + if (gen.getPSLevel() < 2) { + throw new UnsupportedOperationException( + "Reusable images requires at least Level 2 PostScript"); + } + String dataName = formName + ":Data"; + gen.writeDSCComment(DSCConstants.BEGIN_RESOURCE, formName); + if (imageDescription != null) { + gen.writeDSCComment(DSCConstants.TITLE, imageDescription); + } + + String additionalFilters; + if (isJPEG) { + additionalFilters = "/ASCII85Decode filter /DCTDecode filter"; + } else { + if (gen.getPSLevel() >= 3) { + additionalFilters = "/ASCII85Decode filter /FlateDecode filter"; + } else { + additionalFilters = "/ASCII85Decode filter /RunLengthDecode filter"; + } + } + + gen.writeln("/" + formName); + gen.writeln("<< /FormType 1"); + gen.writeln(" /BBox [0 0 " + imgDim.width + " " + imgDim.height + "]"); + gen.writeln(" /Matrix [1 0 0 1 0 0]"); + gen.writeln(" /PaintProc {"); + gen.writeln(" pop"); + gen.writeln(" gsave"); + if (gen.getPSLevel() == 2) { + gen.writeln(" userdict /i 0 put"); //rewind image data + } else { + gen.writeln(" " + dataName + " 0 setfileposition"); //rewind image data + } + String dataSource; + if (gen.getPSLevel() == 2) { + dataSource = "{ " + dataName + " i get /i i 1 add store } bind"; + } else { + dataSource = dataName; + } + writeImageCommand(imgDim, colorSpace, gen, dataSource); + gen.writeln(" grestore"); + gen.writeln(" } bind"); + gen.writeln(">> def"); + gen.writeln("/" + dataName + " currentfile"); + gen.writeln(additionalFilters); + if (gen.getPSLevel() == 2) { + //Creates a data array from the inline file + gen.writeln("{ /temp exch def [" + + " { temp 16384 string readstring not {exit } if } loop ] } exec"); + } else { + gen.writeln("/ReusableStreamDecode filter"); + } + encodeBitmap(img, isJPEG, gen); + gen.writeln("def"); + gen.writeDSCComment(DSCConstants.END_RESOURCE); + PSResource res = new PSResource(PSResource.TYPE_FORM, formName); + gen.getResourceTracker().registerSuppliedResource(res); + return res; + } + + /** + * Paints a reusable image (previously added as a PostScript form). + * @param formName the name of the PostScript form implementing the image + * @param targetRect the target rectangle to place the image in + * @param gen the PostScript generator + * @throws IOException In case of an I/O exception + */ + public static void paintReusableImage( + String formName, + Rectangle2D targetRect, + PSGenerator gen) throws IOException { + PSResource form = new PSResource(PSResource.TYPE_FORM, formName); + paintForm(form, targetRect, gen); + } + + /** + * Paints a reusable image (previously added as a PostScript form). + * @param form the PostScript form resource implementing the image + * @param targetRect the target rectangle to place the image in + * @param gen the PostScript generator + * @throws IOException In case of an I/O exception + */ + public static void paintForm( + PSResource form, + Rectangle2D targetRect, + PSGenerator gen) throws IOException { + gen.saveGraphicsState(); + gen.writeln(gen.formatDouble(targetRect.getX()) + " " + + gen.formatDouble(targetRect.getY()) + " translate"); + gen.writeln(gen.formatDouble(targetRect.getWidth()) + " " + + gen.formatDouble(targetRect.getHeight()) + " scale"); + gen.writeln(form.getName() + " execform"); + + gen.getResourceTracker().notifyResourceUsageOnPage(form); + gen.restoreGraphicsState(); + } + + private static void prepareColorspace(ColorSpace colorSpace, PSGenerator gen) + throws IOException { + if (colorSpace.getType() == ColorSpace.TYPE_CMYK) { + gen.writeln("/DeviceCMYK setcolorspace"); + } else if (colorSpace.getType() == ColorSpace.CS_GRAY) { + gen.writeln("/DeviceGray setcolorspace"); + } else { + gen.writeln("/DeviceRGB setcolorspace"); + } + } + + private static void encodeBitmap(byte[] img, boolean isJPEG, PSGenerator gen) + throws IOException { OutputStream out = gen.getOutputStream(); out = new ASCII85OutputStream(out); if (isJPEG) { @@ -137,10 +268,7 @@ } else { out.flush(); } - - gen.writeln(""); - gen.commentln("%FOPEndBitmap"); - gen.restoreGraphicsState(); + gen.newLine(); } /** @@ -184,11 +312,24 @@ return bitmaps; } + /** + * Extracts a packed RGB integer array of a RenderedImage. + * @param img the image + * @param startX the starting X coordinate + * @param startY the starting Y coordinate + * @param w the width of the cropped image + * @param h the height of the cropped image + * @param rgbArray the prepared integer array to write to + * @param offset offset in the target array + * @param scansize width of a row in the target array + * @return the populated integer array previously passed in as rgbArray parameter + */ public static int[] getRGB(RenderedImage img, - int startX, int startY, int w, int h, + int startX, int startY, + int w, int h, int[] rgbArray, int offset, int scansize) { Raster raster = img.getData(); - int yoff = offset; + int yoff = offset; int off; Object data; int nbands = raster.getNumBands(); @@ -215,21 +356,18 @@ } if (rgbArray == null) { - rgbArray = new int[offset+h*scansize]; + rgbArray = new int[offset + h * scansize]; } ColorModel colorModel = img.getColorModel(); - for (int y = startY; y < startY+h; y++, yoff+=scansize) { + for (int y = startY; y < startY + h; y++, yoff += scansize) { off = yoff; - for (int x = startX; x < startX+w; x++) { - rgbArray[off++] = colorModel.getRGB(raster.getDataElements(x, - y, - data)); + for (int x = startX; x < startX + w; x++) { + rgbArray[off++] = colorModel.getRGB(raster.getDataElements(x, y, data)); } } return rgbArray; - } /** @@ -251,8 +389,8 @@ float x, float y, float w, float h, float bboxx, float bboxy, float bboxw, float bboxh, PSGenerator gen) throws IOException { - gen.notifyResourceUsage(PSProcSets.EPS_PROCSET, false); - gen.writeln("%FOPBeginEPS: " + name); + gen.getResourceTracker().notifyResourceUsageOnPage(PSProcSets.EPS_PROCSET); + gen.writeln("%AXGBeginEPS: " + name); gen.writeln("BeginEPSF"); gen.writeln(gen.formatDouble(x) + " " + gen.formatDouble(y) + " translate"); @@ -271,12 +409,12 @@ gen.writeln("newpath"); PSResource res = new PSResource(PSResource.TYPE_FILE, name); - gen.notifyResourceUsage(res, false); + gen.getResourceTracker().notifyResourceUsageOnPage(res); gen.writeDSCComment(DSCConstants.BEGIN_DOCUMENT, res.getName()); gen.writeByteArr(rawEPS); gen.writeDSCComment(DSCConstants.END_DOCUMENT); gen.writeln("EndEPSF"); - gen.writeln("%FOPEndEPS"); + gen.writeln("%AXGEndEPS"); } } Index: C:/Dev/apache.org/rw/xmlgraphics-commons-trunk/src/java/org/apache/xmlgraphics/ps/PSGenerator.java =================================================================== --- C:/Dev/apache.org/rw/xmlgraphics-commons-trunk/src/java/org/apache/xmlgraphics/ps/PSGenerator.java (revision 500235) +++ C:/Dev/apache.org/rw/xmlgraphics-commons-trunk/src/java/org/apache/xmlgraphics/ps/PSGenerator.java (working copy) @@ -27,13 +27,13 @@ import java.text.DecimalFormat; import java.text.DecimalFormatSymbols; import java.util.Date; -import java.util.Iterator; import java.util.Locale; -import java.util.Set; import java.util.Stack; import javax.xml.transform.Source; +import org.apache.xmlgraphics.ps.dsc.ResourceTracker; + /** * This class is used to output PostScript code to an OutputStream. * @@ -41,17 +41,20 @@ */ public class PSGenerator { + public static final int DEFAULT_LANGUAGE_LEVEL = 3; + /** * Indicator for the PostScript interpreter that the value is provided * later in the document (mostly in the %%Trailer section). + * @deprecated Please use DSCConstants.ATEND. This constant was in the wrong place. */ - public static final AtendIndicator ATEND = new AtendIndicator() { - }; + public static final Object ATEND = DSCConstants.ATEND; /** Line feed used by PostScript */ public static final char LF = '\n'; private OutputStream out; + private int psLevel = DEFAULT_LANGUAGE_LEVEL; private boolean commentsEnabled = true; private Stack graphicsStateStack = new Stack(); @@ -62,7 +65,10 @@ private StringBuffer tempBuffer = new StringBuffer(256); - /** @see java.io.FilterOutputStream **/ + /** + * Creates a new instance. + * @param out the OutputStream to write the generated PostScript code to + */ public PSGenerator(OutputStream out) { this.out = out; this.currentState = new PSState(); @@ -79,14 +85,21 @@ /** * Returns the selected PostScript level. - * (Hardcoded to level 2 for the moment.) * @return the PostScript level */ public int getPSLevel() { - return 2; + return this.psLevel; } /** + * Sets the PostScript level that is used to generate the current document. + * @param level the PostScript level (currently 1, 2 and 3 are known) + */ + public void setPSLevel(int level) { + this.psLevel = level; + } + + /** * Attempts to resolve the given URI. PSGenerator should be subclasses to provide more * sophisticated URI resolution. * @param uri URI to access @@ -238,6 +251,14 @@ return convertStringToDSC(text, false); } + /** + * Converts a value for use in DSC comments. + * @param value the value to convert + * @return String The resulting String + */ + public static final String convertRealToDSC(float value) { + return Float.toString(value); + } /** * Converts text by applying escaping rules established in the DSC specs. @@ -319,8 +340,8 @@ if (params[i] instanceof String) { tempBuffer.append(convertStringToDSC((String)params[i])); - } else if (params[i] instanceof AtendIndicator) { - tempBuffer.append("(atend)"); + } else if (params[i] == DSCConstants.ATEND) { + tempBuffer.append(DSCConstants.ATEND); } else if (params[i] instanceof Double) { tempBuffer.append(df3.format(params[i])); } else if (params[i] instanceof Number) { @@ -505,96 +526,22 @@ } } - private Set documentSuppliedResources; - private Set documentNeededResources; - private Set pageResources; + private ResourceTracker resTracker = new ResourceTracker(); /** - * Notifies the generator that a new page has been started and that the page resource - * set can be cleared. + * Resturns the ResourceTracker instance associated with this PSGenerator. + * @return the ResourceTracker instance or null if none is assigned */ - public void notifyStartNewPage() { - if (pageResources != null) { - pageResources.clear(); - } + public ResourceTracker getResourceTracker() { + return this.resTracker; } /** - * Notifies the generator about the usage of a resource on the current page. - * @param res the resource being used - * @param needed true if this is a needed resource, false for a supplied resource + * Sets the ResourceTracker instance to be associated with this PSGenerator. + * @param resTracker the ResourceTracker instance */ - public void notifyResourceUsage(PSResource res, boolean needed) { - if (pageResources == null) { - pageResources = new java.util.HashSet(); - } - pageResources.add(res); - if (needed) { - if (documentNeededResources == null) { - documentNeededResources = new java.util.HashSet(); - } - documentNeededResources.add(res); - } else { - if (documentSuppliedResources == null) { - documentSuppliedResources = new java.util.HashSet(); - } - documentSuppliedResources.add(res); - } + public void setResourceTracker(ResourceTracker resTracker) { + this.resTracker = resTracker; } - - /** - * Indicates whether a particular resource is supplied, rather than needed. - * @param res the resource - * @return true if the resource is registered as being supplied. - */ - public boolean isResourceSupplied(PSResource res) { - return documentSuppliedResources.contains(res); - } - - /** - * Writes a DSC comment for the accumulated used resources, either at page level or - * at document level. - * @param pageLevel true if the DSC comment for the page level should be generated, - * false for the document level (in the trailer) - * @exception IOException In case of an I/O problem - */ - public void writeResources(boolean pageLevel) throws IOException { - if (pageLevel) { - writeResourceComment(DSCConstants.PAGE_RESOURCES, pageResources); - } else { - writeResourceComment(DSCConstants.DOCUMENT_NEEDED_RESOURCES, - documentNeededResources); - writeResourceComment(DSCConstants.DOCUMENT_SUPPLIED_RESOURCES, - documentSuppliedResources); - } - } - private void writeResourceComment(String name, Set resources) throws IOException { - if (resources == null || resources.size() == 0) { - return; - } - tempBuffer.setLength(0); - tempBuffer.append("%%"); - tempBuffer.append(name); - tempBuffer.append(" "); - boolean first = true; - Iterator i = resources.iterator(); - while (i.hasNext()) { - if (!first) { - writeln(tempBuffer.toString()); - tempBuffer.setLength(0); - tempBuffer.append("%%+ "); - } - PSResource res = (PSResource)i.next(); - tempBuffer.append(res.getResourceSpecification()); - first = false; - } - writeln(tempBuffer.toString()); - } - - /** Used for the ATEND constant. See there. */ - private static interface AtendIndicator { - } - - } Index: C:/Dev/apache.org/rw/xmlgraphics-commons-trunk/src/java/org/apache/xmlgraphics/ps/dsc/DSCFilter.java =================================================================== --- C:/Dev/apache.org/rw/xmlgraphics-commons-trunk/src/java/org/apache/xmlgraphics/ps/dsc/DSCFilter.java (revision 0) +++ C:/Dev/apache.org/rw/xmlgraphics-commons-trunk/src/java/org/apache/xmlgraphics/ps/dsc/DSCFilter.java (revision 0) @@ -0,0 +1,36 @@ +/* + * 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.xmlgraphics.ps.dsc; + +import org.apache.xmlgraphics.ps.dsc.events.DSCEvent; + +/** + * Filter interface for DSC events used by the DSC parser. + */ +public interface DSCFilter { + + /** + * Indicates whether a particular event is acceptable or if it should be skipped/ignored. + * @param event the DSC event + * @return true if the event should be accepted + */ + boolean accept(DSCEvent event); + +} Property changes on: C:\Dev\apache.org\rw\xmlgraphics-commons-trunk\src\java\org\apache\xmlgraphics\ps\dsc\DSCFilter.java ___________________________________________________________________ Name: svn:eol-style + native Index: C:/Dev/apache.org/rw/xmlgraphics-commons-trunk/src/java/org/apache/xmlgraphics/ps/dsc/NestedDocumentHandler.java =================================================================== --- C:/Dev/apache.org/rw/xmlgraphics-commons-trunk/src/java/org/apache/xmlgraphics/ps/dsc/NestedDocumentHandler.java (revision 0) +++ C:/Dev/apache.org/rw/xmlgraphics-commons-trunk/src/java/org/apache/xmlgraphics/ps/dsc/NestedDocumentHandler.java (revision 0) @@ -0,0 +1,45 @@ +/* + * 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.xmlgraphics.ps.dsc; + +import java.io.IOException; + +import org.apache.xmlgraphics.ps.dsc.events.DSCEvent; + +/** + * Interface that is used to delegate the handling of nested documents (EPS files, data sections) + * in a PostScript document. The implementation receives a parser instance so it can step forward + * until the end of the nested document is reached at which point control is given back to the + * original consumer. + */ +public interface NestedDocumentHandler { + + /** + * Handle a DSC event. Implementations may issue additional calls to the DSC parser and may + * modify its state. When returning from the call, state information such as filters should + * be restored. + * @param event the DSC event to handle + * @param parser the DSC parser to work with + * @throws IOException In case of an I/O error + * @throws DSCException In case of a violation of the DSC spec + */ + void handle(DSCEvent event, DSCParser parser) throws IOException, DSCException; + +} \ No newline at end of file Property changes on: C:\Dev\apache.org\rw\xmlgraphics-commons-trunk\src\java\org\apache\xmlgraphics\ps\dsc\NestedDocumentHandler.java ___________________________________________________________________ Name: svn:eol-style + native Index: C:/Dev/apache.org/rw/xmlgraphics-commons-trunk/src/java/org/apache/xmlgraphics/ps/dsc/tools/PageExtractor.java =================================================================== --- C:/Dev/apache.org/rw/xmlgraphics-commons-trunk/src/java/org/apache/xmlgraphics/ps/dsc/tools/PageExtractor.java (revision 0) +++ C:/Dev/apache.org/rw/xmlgraphics-commons-trunk/src/java/org/apache/xmlgraphics/ps/dsc/tools/PageExtractor.java (revision 0) @@ -0,0 +1,119 @@ +/* + * 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.xmlgraphics.ps.dsc.tools; + +import java.io.IOException; +import java.io.InputStream; +import java.io.OutputStream; + +import org.apache.xmlgraphics.ps.DSCConstants; +import org.apache.xmlgraphics.ps.PSGenerator; +import org.apache.xmlgraphics.ps.dsc.DSCException; +import org.apache.xmlgraphics.ps.dsc.DSCFilter; +import org.apache.xmlgraphics.ps.dsc.DSCParser; +import org.apache.xmlgraphics.ps.dsc.DSCParserConstants; +import org.apache.xmlgraphics.ps.dsc.DefaultNestedDocumentHandler; +import org.apache.xmlgraphics.ps.dsc.events.DSCComment; +import org.apache.xmlgraphics.ps.dsc.events.DSCCommentPage; +import org.apache.xmlgraphics.ps.dsc.events.DSCCommentPages; +import org.apache.xmlgraphics.ps.dsc.events.DSCEvent; +import org.apache.xmlgraphics.ps.dsc.events.DSCHeaderComment; + +/** + * This class can extract a certain range of pages from a DSC-compliant PostScript file. + */ +public class PageExtractor implements DSCParserConstants { + + /** + * Parses a DSC-compliant file and pipes the content through to the OutputStream omitting + * all pages not within the range. + * @param in the InputStream to parse from + * @param out the OutputStream to write the modified file to + * @param from the starting page (1-based) + * @param to the last page (inclusive, 1-based) + * @throws IOException In case of an I/O error + * @throws DSCException In case of a violation of the DSC spec + */ + public static void extractPages(InputStream in, OutputStream out, int from, int to) + throws IOException, DSCException { + if (from <= 0) { + throw new IllegalArgumentException("'from' page number must be 1 or higher"); + } + if (to < from) { + throw new IllegalArgumentException( + "'to' page number must be equal or larger than the 'from' page number"); + } + + DSCParser parser = new DSCParser(in); + PSGenerator gen = new PSGenerator(out); + parser.setNestedDocumentHandler(new DefaultNestedDocumentHandler(gen)); + int pageCount = 0; + + //Skip DSC header + DSCHeaderComment header = DSCTools.checkAndSkipDSC30Header(parser); + header.generate(gen); + //Set number of pages + DSCCommentPages pages = new DSCCommentPages(to - from + 1); + pages.generate(gen); + + parser.setFilter(new DSCFilter() { + public boolean accept(DSCEvent event) { + if (event.isDSCComment()) { + //Filter %%Pages which we add manually above + return !event.asDSCComment().getName().equals(DSCConstants.PAGES); + } else { + return true; + } + } + }); + + //Skip the prolog and to the first page + DSCComment pageOrTrailer = parser.nextDSCComment(DSCConstants.PAGE, gen); + if (pageOrTrailer == null) { + throw new DSCException("Page expected, but none found"); + } + parser.setFilter(null); //Remove filter + + //Process individual pages (and skip as necessary) + while (true) { + DSCCommentPage page = (DSCCommentPage)pageOrTrailer; + boolean validPage = (page.getPagePosition() >= from && page.getPagePosition() <= to); + if (validPage) { + page.setPagePosition(page.getPagePosition() - from + 1); + page.generate(gen); + pageCount++; + } + pageOrTrailer = DSCTools.nextPageOrTrailer(parser, (validPage ? gen : null)); + if (pageOrTrailer == null) { + throw new DSCException("File is not DSC-compliant: Unexpected end of file"); + } else if (!DSCConstants.PAGE.equals(pageOrTrailer.getName())) { + pageOrTrailer.generate(gen); + break; + } + } + + //Write the rest + while (parser.hasNext()) { + DSCEvent event = parser.nextEvent(); + event.generate(gen); + } + } + +} \ No newline at end of file Property changes on: C:\Dev\apache.org\rw\xmlgraphics-commons-trunk\src\java\org\apache\xmlgraphics\ps\dsc\tools\PageExtractor.java ___________________________________________________________________ Name: svn:eol-style + native Index: C:/Dev/apache.org/rw/xmlgraphics-commons-trunk/src/java/org/apache/xmlgraphics/ps/dsc/tools/DSCTools.java =================================================================== --- C:/Dev/apache.org/rw/xmlgraphics-commons-trunk/src/java/org/apache/xmlgraphics/ps/dsc/tools/DSCTools.java (revision 0) +++ C:/Dev/apache.org/rw/xmlgraphics-commons-trunk/src/java/org/apache/xmlgraphics/ps/dsc/tools/DSCTools.java (revision 0) @@ -0,0 +1,120 @@ +/* + * 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.xmlgraphics.ps.dsc.tools; + +import java.io.IOException; + +import org.apache.xmlgraphics.ps.DSCConstants; +import org.apache.xmlgraphics.ps.PSGenerator; +import org.apache.xmlgraphics.ps.dsc.DSCException; +import org.apache.xmlgraphics.ps.dsc.DSCParser; +import org.apache.xmlgraphics.ps.dsc.DSCParserConstants; +import org.apache.xmlgraphics.ps.dsc.events.DSCComment; +import org.apache.xmlgraphics.ps.dsc.events.DSCEvent; +import org.apache.xmlgraphics.ps.dsc.events.DSCHeaderComment; +import org.apache.xmlgraphics.ps.dsc.events.PostScriptComment; + +/** + * Helper methods commonly used when dealing with DSC-compliant PostScript files. + */ +public class DSCTools implements DSCParserConstants { + + /** + * Indicates whether the given event ends a header comment section according to the rules in + * DSC 3.0, chapter 4.4. + * @param event the event to check + * @return true if a header comment section would be ended either explicitely or implicitely + * by the given event + */ + public static boolean headerCommentsEndHere(DSCEvent event) { + switch (event.getEventType()) { + case DSC_COMMENT: + DSCComment comment = event.asDSCComment(); + return (comment.getName().equals(DSCConstants.END_COMMENTS)); + case COMMENT: + String s = ((PostScriptComment)event).getComment(); + if (s == null || s.length() == 0) { + return true; + } else { + char c = s.charAt(0); + return ("\n\t ".indexOf(c) >= 0); + } + default: + return true; + } + } + + /** + * Verifies that the file being parsed is a DSC 3.0 file. + * @param parser the DSC parser + * @return the header comment event + * @throws DSCException In case of a violation of the DSC spec + * @throws IOException In case of an I/O problem + */ + public static DSCHeaderComment checkAndSkipDSC30Header(DSCParser parser) + throws DSCException, IOException { + if (!parser.hasNext()) { + throw new DSCException("File has no content"); + } + DSCEvent event = parser.nextEvent(); + if (event.getEventType() == HEADER_COMMENT) { + DSCHeaderComment header = (DSCHeaderComment)event; + if (!header.isPSAdobe30()) { + throw new DSCException("PostScript file does not start with '" + + DSCConstants.PS_ADOBE_30 + "'"); + } + return header; + } else { + throw new DSCException("PostScript file does not start with '" + + DSCConstants.PS_ADOBE_30 + "'"); + } + } + + /** + * Advances the parser to the next page or to the trailer or the end of file comment. + * @param parser the DSC parser + * @param gen the PSGenerator instance to pass the skipped events through to + * @return the DSC comment found (Page, Trailer or EOF) + * @throws IOException In case of an I/O error + * @throws DSCException In case of a violation of the DSC spec + */ + public static DSCComment nextPageOrTrailer(DSCParser parser, PSGenerator gen) + throws IOException, DSCException { + while (parser.hasNext()) { + DSCEvent event = parser.nextEvent(); + if (event.getEventType() == DSC_COMMENT) { + DSCComment comment = event.asDSCComment(); + if (DSCConstants.PAGE.equals(comment.getName())) { + return comment; + } else if (DSCConstants.TRAILER.equals(comment.getName())) { + return comment; + } + } else if (event.getEventType() == EOF) { + //The Trailer may be missing + return event.asDSCComment(); + } + if (gen != null) { + event.generate(gen); //Pipe through to PSGenerator + } + } + return null; + } + +} Property changes on: C:\Dev\apache.org\rw\xmlgraphics-commons-trunk\src\java\org\apache\xmlgraphics\ps\dsc\tools\DSCTools.java ___________________________________________________________________ Name: svn:eol-style + native Index: C:/Dev/apache.org/rw/xmlgraphics-commons-trunk/src/java/org/apache/xmlgraphics/ps/dsc/tools/package.html =================================================================== --- C:/Dev/apache.org/rw/xmlgraphics-commons-trunk/src/java/org/apache/xmlgraphics/ps/dsc/tools/package.html (revision 0) +++ C:/Dev/apache.org/rw/xmlgraphics-commons-trunk/src/java/org/apache/xmlgraphics/ps/dsc/tools/package.html (revision 0) @@ -0,0 +1,23 @@ + + + +org.apache.xmlgraphics.ps.dsc.tools Package + +

Tools for working with DSC-compliant PostScript files.

+ + \ No newline at end of file Property changes on: C:\Dev\apache.org\rw\xmlgraphics-commons-trunk\src\java\org\apache\xmlgraphics\ps\dsc\tools\package.html ___________________________________________________________________ Name: svn:eol-style + native Index: C:/Dev/apache.org/rw/xmlgraphics-commons-trunk/src/java/org/apache/xmlgraphics/ps/dsc/DSCParserConstants.java =================================================================== --- C:/Dev/apache.org/rw/xmlgraphics-commons-trunk/src/java/org/apache/xmlgraphics/ps/dsc/DSCParserConstants.java (revision 0) +++ C:/Dev/apache.org/rw/xmlgraphics-commons-trunk/src/java/org/apache/xmlgraphics/ps/dsc/DSCParserConstants.java (revision 0) @@ -0,0 +1,38 @@ +/* + * 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.xmlgraphics.ps.dsc; + +/** + * Constants the DSC parser uses. + */ +public interface DSCParserConstants { + + /** Indicates a header comment (starting with "%!") */ + int HEADER_COMMENT = 0; + /** Indicates a DSC comment (starting with "%%") */ + int DSC_COMMENT = 1; + /** Indicates a normal PostScript comment (starting with "%") */ + int COMMENT = 2; + /** Indicates a normal PostScript line */ + int LINE = 3; + /** Indicates the end of the file (equivalent to the "%%EOF" DSC comment) */ + int EOF = 4; + +} Property changes on: C:\Dev\apache.org\rw\xmlgraphics-commons-trunk\src\java\org\apache\xmlgraphics\ps\dsc\DSCParserConstants.java ___________________________________________________________________ Name: svn:eol-style + native Index: C:/Dev/apache.org/rw/xmlgraphics-commons-trunk/src/java/org/apache/xmlgraphics/ps/dsc/ResourceTracker.java =================================================================== --- C:/Dev/apache.org/rw/xmlgraphics-commons-trunk/src/java/org/apache/xmlgraphics/ps/dsc/ResourceTracker.java (revision 0) +++ C:/Dev/apache.org/rw/xmlgraphics-commons-trunk/src/java/org/apache/xmlgraphics/ps/dsc/ResourceTracker.java (revision 0) @@ -0,0 +1,173 @@ +/* + * 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.xmlgraphics.ps.dsc; + +import java.io.IOException; +import java.util.Collection; +import java.util.Collections; +import java.util.Iterator; +import java.util.Set; + +import org.apache.xmlgraphics.ps.PSGenerator; +import org.apache.xmlgraphics.ps.PSResource; +import org.apache.xmlgraphics.ps.dsc.events.DSCCommentDocumentNeededResources; +import org.apache.xmlgraphics.ps.dsc.events.DSCCommentDocumentSuppliedResources; +import org.apache.xmlgraphics.ps.dsc.events.DSCCommentPageResources; + +/** + * This class is used to track resources in a DSC-compliant PostScript file. The distinction is + * made between supplied and needed resources. For the details of this distinction, please see + * the DSC specification. + */ +public class ResourceTracker { + + private Set documentSuppliedResources; + private Set documentNeededResources; + private Set usedResources; + private Set pageResources; + + /** + * Returns the set of supplied resources. + * @return the set of supplied resources + */ + public Set getDocumentSuppliedResources() { + if (documentSuppliedResources != null) { + return Collections.unmodifiableSet(documentSuppliedResources); + } else { + return Collections.EMPTY_SET; + } + } + + /** + * Returns the set of needed resources. + * @return the set of needed resources + */ + public Set getDocumentNeededResources() { + if (documentNeededResources != null) { + return Collections.unmodifiableSet(documentNeededResources); + } else { + return Collections.EMPTY_SET; + } + } + + /** + * Notifies the resource tracker that a new page has been started and that the page resource + * set can be cleared. + */ + public void notifyStartNewPage() { + if (pageResources != null) { + pageResources.clear(); + } + } + + /** + * Registers a supplied resource. If the same resources is already in the set of needed + * resources, it is removed there. + * @param res the resource + */ + public void registerSuppliedResource(PSResource res) { + if (documentSuppliedResources == null) { + documentSuppliedResources = new java.util.HashSet(); + } + documentSuppliedResources.add(res); + if (documentNeededResources != null) { + documentNeededResources.remove(res); + } + } + + /** + * Registers a needed resource. If the same resources is already in the set of supplied + * resources, it is ignored, i.e. it is assumed to be supplied. + * @param res the resource + */ + public void registerNeededResource(PSResource res) { + if (documentNeededResources == null) { + documentNeededResources = new java.util.HashSet(); + } + if (!documentSuppliedResources.contains(res)) { + documentNeededResources.add(res); + } + } + + /** + * Notifies the resource tracker about the usage of a resource on the current page. + * @param res the resource being used + */ + public void notifyResourceUsageOnPage(PSResource res) { + if (pageResources == null) { + pageResources = new java.util.HashSet(); + } + pageResources.add(res); + } + + /** + * Notifies the resource tracker about the usage of resources on the current page. + * @param resources the resources being used + */ + public void notifyResourceUsageOnPage(Collection resources) { + if (pageResources == null) { + pageResources = new java.util.HashSet(); + } + pageResources.addAll(resources); + } + + /** + * Indicates whether a particular resource is supplied, rather than needed. + * @param res the resource + * @return true if the resource is registered as being supplied. + */ + public boolean isResourceSupplied(PSResource res) { + return (documentSuppliedResources != null) && documentSuppliedResources.contains(res); + } + + /** + * Writes a DSC comment for the accumulated used resources, either at page level or + * at document level. + * @param pageLevel true if the DSC comment for the page level should be generated, + * false for the document level (in the trailer) + * @param gen the PSGenerator to write the DSC comments with + * @exception IOException In case of an I/O problem + */ + public void writeResources(boolean pageLevel, PSGenerator gen) throws IOException { + if (pageLevel) { + new DSCCommentPageResources(pageResources).generate(gen); + if (usedResources == null) { + usedResources = new java.util.HashSet(); + } + usedResources.addAll(pageResources); + } else { + if (usedResources != null) { + Iterator iter = usedResources.iterator(); + while (iter.hasNext()) { + PSResource res = (PSResource)iter.next(); + if (documentSuppliedResources == null + || !documentSuppliedResources.contains(res)) { + registerNeededResource(res); + } + } + } + new DSCCommentDocumentNeededResources(documentNeededResources).generate(gen); + new DSCCommentDocumentSuppliedResources(documentSuppliedResources).generate(gen); + } + } + + + +} Property changes on: C:\Dev\apache.org\rw\xmlgraphics-commons-trunk\src\java\org\apache\xmlgraphics\ps\dsc\ResourceTracker.java ___________________________________________________________________ Name: svn:eol-style + native Index: C:/Dev/apache.org/rw/xmlgraphics-commons-trunk/src/java/org/apache/xmlgraphics/ps/dsc/events/AbstractEvent.java =================================================================== --- C:/Dev/apache.org/rw/xmlgraphics-commons-trunk/src/java/org/apache/xmlgraphics/ps/dsc/events/AbstractEvent.java (revision 0) +++ C:/Dev/apache.org/rw/xmlgraphics-commons-trunk/src/java/org/apache/xmlgraphics/ps/dsc/events/AbstractEvent.java (revision 0) @@ -0,0 +1,69 @@ +/* + * 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.xmlgraphics.ps.dsc.events; + +/** + * Abstract base class for DSC events. + */ +public abstract class AbstractEvent implements DSCEvent { + + /** + * @see org.apache.xmlgraphics.ps.dsc.events.DSCEvent#isComment() + */ + public boolean isComment() { + return false; + } + + /** + * @see org.apache.xmlgraphics.ps.dsc.events.DSCEvent#isDSCComment() + */ + public boolean isDSCComment() { + return false; + } + + /** + * @see org.apache.xmlgraphics.ps.dsc.events.DSCEvent#isHeaderComment() + */ + public boolean isHeaderComment() { + return false; + } + + /** + * @see org.apache.xmlgraphics.ps.dsc.events.DSCEvent#isLine() + */ + public boolean isLine() { + return false; + } + + /** + * @see org.apache.xmlgraphics.ps.dsc.events.DSCEvent#asDSCComment() + */ + public DSCComment asDSCComment() { + throw new ClassCastException(this.getClass().getName()); + } + + /** + * @see org.apache.xmlgraphics.ps.dsc.events.DSCEvent#asLine() + */ + public PostScriptLine asLine() { + throw new ClassCastException(this.getClass().getName()); + } + +} Property changes on: C:\Dev\apache.org\rw\xmlgraphics-commons-trunk\src\java\org\apache\xmlgraphics\ps\dsc\events\AbstractEvent.java ___________________________________________________________________ Name: svn:eol-style + native Index: C:/Dev/apache.org/rw/xmlgraphics-commons-trunk/src/java/org/apache/xmlgraphics/ps/dsc/events/DSCHeaderComment.java =================================================================== --- C:/Dev/apache.org/rw/xmlgraphics-commons-trunk/src/java/org/apache/xmlgraphics/ps/dsc/events/DSCHeaderComment.java (revision 0) +++ C:/Dev/apache.org/rw/xmlgraphics-commons-trunk/src/java/org/apache/xmlgraphics/ps/dsc/events/DSCHeaderComment.java (revision 0) @@ -0,0 +1,80 @@ +/* + * 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.xmlgraphics.ps.dsc.events; + +import java.io.IOException; + +import org.apache.xmlgraphics.ps.DSCConstants; +import org.apache.xmlgraphics.ps.PSGenerator; + +/** + * Represents a DSC header comment (beginning with "%!). + */ +public class DSCHeaderComment extends AbstractEvent { + + private String comment; + + /** + * Creates a new instance. + * @param comment the comment + */ + public DSCHeaderComment(String comment) { + this.comment = comment; + } + + /** + * Returns the comment. + * @return the comment + */ + public String getComment() { + return this.comment; + } + + /** + * Indicates whether the file started by this comments is DSC 3.0 compliant. + * @return true if the file is DSC 3.0 compliant. + */ + public boolean isPSAdobe30() { + return getComment().startsWith(DSCConstants.PS_ADOBE_30.substring(2)); + } + + /** + * @see org.apache.xmlgraphics.ps.dsc.events.DSCEvent#generate( + * org.apache.xmlgraphics.ps.PSGenerator) + */ + public void generate(PSGenerator gen) throws IOException { + gen.writeln("%!" + getComment()); + } + + /** + * @see org.apache.xmlgraphics.ps.dsc.events.DSCEvent#getEventType() + */ + public int getEventType() { + return HEADER_COMMENT; + } + + /** + * @see org.apache.xmlgraphics.ps.dsc.events.AbstractEvent#isHeaderComment() + */ + public boolean isHeaderComment() { + return true; + } + +} Property changes on: C:\Dev\apache.org\rw\xmlgraphics-commons-trunk\src\java\org\apache\xmlgraphics\ps\dsc\events\DSCHeaderComment.java ___________________________________________________________________ Name: svn:eol-style + native Index: C:/Dev/apache.org/rw/xmlgraphics-commons-trunk/src/java/org/apache/xmlgraphics/ps/dsc/events/DSCCommentEndOfFile.java =================================================================== --- C:/Dev/apache.org/rw/xmlgraphics-commons-trunk/src/java/org/apache/xmlgraphics/ps/dsc/events/DSCCommentEndOfFile.java (revision 0) +++ C:/Dev/apache.org/rw/xmlgraphics-commons-trunk/src/java/org/apache/xmlgraphics/ps/dsc/events/DSCCommentEndOfFile.java (revision 0) @@ -0,0 +1,67 @@ +/* + * 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.xmlgraphics.ps.dsc.events; + +import java.io.IOException; + +import org.apache.xmlgraphics.ps.DSCConstants; +import org.apache.xmlgraphics.ps.PSGenerator; + +/** + * Represents a %%EOF DSC comment. + */ +public class DSCCommentEndOfFile extends AbstractDSCComment { + + /** + * @see org.apache.xmlgraphics.ps.dsc.events.DSCComment#getName() + */ + public String getName() { + return DSCConstants.EOF; + } + + /** + * @see org.apache.xmlgraphics.ps.dsc.events.DSCComment#hasValues() + */ + public boolean hasValues() { + return false; + } + + /** + * @see org.apache.xmlgraphics.ps.dsc.events.DSCComment#parseValue(java.lang.String) + */ + public void parseValue(String value) { + //nop + } + + /** + * @see org.apache.xmlgraphics.ps.dsc.events.DSCEvent#generate(org.apache.xmlgraphics.ps.PSGenerator) + */ + public void generate(PSGenerator gen) throws IOException { + gen.writeDSCComment(getName()); + } + + /** + * @see org.apache.xmlgraphics.ps.dsc.events.AbstractDSCComment#getEventType() + */ + public int getEventType() { + return EOF; + } + +} Property changes on: C:\Dev\apache.org\rw\xmlgraphics-commons-trunk\src\java\org\apache\xmlgraphics\ps\dsc\events\DSCCommentEndOfFile.java ___________________________________________________________________ Name: svn:eol-style + native Index: C:/Dev/apache.org/rw/xmlgraphics-commons-trunk/src/java/org/apache/xmlgraphics/ps/dsc/events/DSCCommentBeginResource.java =================================================================== --- C:/Dev/apache.org/rw/xmlgraphics-commons-trunk/src/java/org/apache/xmlgraphics/ps/dsc/events/DSCCommentBeginResource.java (revision 0) +++ C:/Dev/apache.org/rw/xmlgraphics-commons-trunk/src/java/org/apache/xmlgraphics/ps/dsc/events/DSCCommentBeginResource.java (revision 0) @@ -0,0 +1,141 @@ +/* + * 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.xmlgraphics.ps.dsc.events; + +import java.io.IOException; +import java.util.Iterator; +import java.util.List; + +import org.apache.xmlgraphics.ps.DSCConstants; +import org.apache.xmlgraphics.ps.PSGenerator; +import org.apache.xmlgraphics.ps.PSProcSet; +import org.apache.xmlgraphics.ps.PSResource; + +/** + * Represents a %BeginResource DSC comment. + */ +public class DSCCommentBeginResource extends AbstractDSCComment { + + private PSResource resource; + private Integer min; + private Integer max; + + /** + * Creates a new instance + */ + public DSCCommentBeginResource() { + } + + /** + * Creates a new instance for a given PSResource instance + * @param resource the resource + */ + public DSCCommentBeginResource(PSResource resource) { + this.resource = resource; + } + + /** + * Creates a new instance for a given PSResource instance + * @param resource the resource + * @param min Minimum VM used by the resource + * @param max Maximum VM used by the resource + */ + public DSCCommentBeginResource(PSResource resource, int min, int max) { + this.resource = resource; + this.min = new Integer(min); + this.max = new Integer(max); + } + + /** + * Returns the associated PSResource. + * @return the resource + */ + public PSResource getResource() { + return this.resource; + } + + /** + * Returns the minimum VM used by the resource. + * @return the minimum VM used by the resource + */ + public Integer getMin() { + return this.min; + } + + /** + * Returns the maximum VM used by the resource. + * @return the maximum VM used by the resource + */ + public Integer getMax() { + return this.max; + } + + /** + * @see org.apache.xmlgraphics.ps.dsc.events.DSCComment#getName() + */ + public String getName() { + return DSCConstants.BEGIN_RESOURCE; + } + + /** + * @see org.apache.xmlgraphics.ps.dsc.events.DSCComment#hasValues() + */ + public boolean hasValues() { + return true; + } + + /** + * @see org.apache.xmlgraphics.ps.dsc.events.DSCComment#parseValue(java.lang.String) + */ + public void parseValue(String value) { + List params = splitParams(value); + Iterator iter = params.iterator(); + String name = (String)iter.next(); + if (PSResource.TYPE_FONT.equals(name)) { + String fontname = (String)iter.next(); + this.resource = new PSResource(name, fontname); + } else if (PSResource.TYPE_PROCSET.equals(name)) { + String procname = (String)iter.next(); + String version = (String)iter.next(); + String revision = (String)iter.next(); + this.resource = new PSProcSet(procname, + Float.parseFloat(version), Integer.parseInt(revision)); + } else if (PSResource.TYPE_FILE.equals(name)) { + String filename = (String)iter.next(); + this.resource = new PSResource(name, filename); + } else { + throw new IllegalArgumentException("Invalid resource type: " + name); + } + } + + /** + * @see org.apache.xmlgraphics.ps.dsc.events.DSCEvent#generate( + * org.apache.xmlgraphics.ps.PSGenerator) + */ + public void generate(PSGenerator gen) throws IOException { + if (getMin() != null) { + Object[] params = new Object[] {getResource(), getMin(), getMax()}; + gen.writeDSCComment(getName(), params); + } else { + gen.writeDSCComment(getName(), getResource()); + } + } + +} Property changes on: C:\Dev\apache.org\rw\xmlgraphics-commons-trunk\src\java\org\apache\xmlgraphics\ps\dsc\events\DSCCommentBeginResource.java ___________________________________________________________________ Name: svn:eol-style + native Index: C:/Dev/apache.org/rw/xmlgraphics-commons-trunk/src/java/org/apache/xmlgraphics/ps/dsc/events/DSCEvent.java =================================================================== --- C:/Dev/apache.org/rw/xmlgraphics-commons-trunk/src/java/org/apache/xmlgraphics/ps/dsc/events/DSCEvent.java (revision 0) +++ C:/Dev/apache.org/rw/xmlgraphics-commons-trunk/src/java/org/apache/xmlgraphics/ps/dsc/events/DSCEvent.java (revision 0) @@ -0,0 +1,84 @@ +/* + * 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.xmlgraphics.ps.dsc.events; + +import java.io.IOException; + +import org.apache.xmlgraphics.ps.PSGenerator; +import org.apache.xmlgraphics.ps.dsc.DSCParserConstants; + +/** + * Interface representing a DSC event. A DSC event can be a DSC comment, a PostScript comment + * or a line of PostScript code. + */ +public interface DSCEvent extends DSCParserConstants { + + /** + * Returns the event type. + * @return the event type (see {@link DSCParserConstants}) + */ + int getEventType(); + + /** + * Casts this instance to a DSCComment if possible. + * @return this event as a DSCComment + * @throws ClassCastException if the event is no DSCComment + */ + DSCComment asDSCComment(); + + /** + * Casts this instance to a PostScriptLine if possible. + * @return this event as a PostScriptLine + * @throws ClassCastException if the event is no PostScriptLine + */ + PostScriptLine asLine(); + + /** + * Indicates whether the instance is a DSC comment. + * @return true if the instance is a DSC comment + */ + boolean isDSCComment(); + + /** + * Indicates whether the instance is a PostScript comment. + * @return true if the instance is a PostScript comment + */ + boolean isComment(); + + /** + * Indicates whether the instance is a header comment. + * @return true if the instance is a header comment + */ + boolean isHeaderComment(); + + /** + * Indicates whether the instance is a PostScript line. + * @return true if the instance is a PostScript line + */ + boolean isLine(); + + /** + * Writes the event to the given PSGenerator. + * @param gen the PSGenerator to write to + * @throws IOException In case of an I/O error + */ + void generate(PSGenerator gen) throws IOException; + +} Property changes on: C:\Dev\apache.org\rw\xmlgraphics-commons-trunk\src\java\org\apache\xmlgraphics\ps\dsc\events\DSCEvent.java ___________________________________________________________________ Name: svn:eol-style + native Index: C:/Dev/apache.org/rw/xmlgraphics-commons-trunk/src/java/org/apache/xmlgraphics/ps/dsc/events/AbstractResourcesDSCComment.java =================================================================== --- C:/Dev/apache.org/rw/xmlgraphics-commons-trunk/src/java/org/apache/xmlgraphics/ps/dsc/events/AbstractResourcesDSCComment.java (revision 0) +++ C:/Dev/apache.org/rw/xmlgraphics-commons-trunk/src/java/org/apache/xmlgraphics/ps/dsc/events/AbstractResourcesDSCComment.java (revision 0) @@ -0,0 +1,171 @@ +/* + * 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.xmlgraphics.ps.dsc.events; + +import java.io.IOException; +import java.util.Collection; +import java.util.Collections; +import java.util.Iterator; +import java.util.List; +import java.util.Set; + +import org.apache.xmlgraphics.ps.PSGenerator; +import org.apache.xmlgraphics.ps.PSProcSet; +import org.apache.xmlgraphics.ps.PSResource; + +/** + * Abstract base class for Resource DSC comments (DocumentNeededResources, + * DocumentSuppliedResources and PageResources). + */ +public abstract class AbstractResourcesDSCComment extends AbstractDSCComment { + + private Set resources; + + /** + * Creates a new instance. + */ + public AbstractResourcesDSCComment() { + super(); + } + + /** + * Creates a new instance. + * @param resources a Collection of PSResource instances + */ + public AbstractResourcesDSCComment(Collection resources) { + addResources(resources); + } + + /** + * @see org.apache.xmlgraphics.ps.dsc.events.DSCComment#hasValues() + */ + public boolean hasValues() { + return true; + } + + /** + * Adds a new resource. + * @param res the resource + */ + public void addResource(PSResource res) { + if (this.resources == null) { + this.resources = new java.util.HashSet(); + } + this.resources.add(res); + } + + /** + * Adds a collection of resources. + * @param resources a Collection of PSResource instances. + */ + public void addResources(Collection resources) { + if (resources != null) { + if (this.resources == null) { + this.resources = new java.util.HashSet(); + } + this.resources.addAll(resources); + } + } + + /** + * Returns the set of resources associated with this DSC comment. + * @return the set of resources + */ + public Set getResources() { + return Collections.unmodifiableSet(this.resources); + } + + /** + * Defines the known resource types (font, procset, file, pattern etc.). + */ + protected static final Set RESOURCE_TYPES = new java.util.HashSet(); + + static { + RESOURCE_TYPES.add(PSResource.TYPE_FONT); + RESOURCE_TYPES.add(PSResource.TYPE_PROCSET); + RESOURCE_TYPES.add(PSResource.TYPE_FILE); + RESOURCE_TYPES.add(PSResource.TYPE_PATTERN); + RESOURCE_TYPES.add(PSResource.TYPE_FORM); + RESOURCE_TYPES.add(PSResource.TYPE_ENCODING); + } + + /** + * @see org.apache.xmlgraphics.ps.dsc.events.DSCComment#parseValue(java.lang.String) + */ + public void parseValue(String value) { + List params = splitParams(value); + String currentResourceType = null; + Iterator iter = params.iterator(); + while (iter.hasNext()) { + String name = (String)iter.next(); + if (RESOURCE_TYPES.contains(name)) { + currentResourceType = name; + } + if (currentResourceType == null) { + throw new IllegalArgumentException( + " must begin with a resource type. Found: " + name); + } + if (PSResource.TYPE_FONT.equals(currentResourceType)) { + String fontname = (String)iter.next(); + addResource(new PSResource(name, fontname)); + } else if (PSResource.TYPE_FORM.equals(currentResourceType)) { + String formname = (String)iter.next(); + addResource(new PSResource(name, formname)); + } else if (PSResource.TYPE_PROCSET.equals(currentResourceType)) { + String procname = (String)iter.next(); + String version = (String)iter.next(); + String revision = (String)iter.next(); + addResource(new PSProcSet(procname, + Float.parseFloat(version), Integer.parseInt(revision))); + } else if (PSResource.TYPE_FILE.equals(currentResourceType)) { + String filename = (String)iter.next(); + addResource(new PSResource(name, filename)); + } else { + throw new IllegalArgumentException("Invalid resource type: " + currentResourceType); + } + } + } + + /** + * @see org.apache.xmlgraphics.ps.dsc.events.DSCEvent#generate( + * org.apache.xmlgraphics.ps.PSGenerator) + */ + public void generate(PSGenerator gen) throws IOException { + if (resources == null || resources.size() == 0) { + return; + } + StringBuffer sb = new StringBuffer(); + sb.append("%%").append(getName()).append(": "); + boolean first = true; + Iterator i = resources.iterator(); + while (i.hasNext()) { + if (!first) { + gen.writeln(sb.toString()); + sb.setLength(0); + sb.append("%%+ "); + } + PSResource res = (PSResource)i.next(); + sb.append(res.getResourceSpecification()); + first = false; + } + gen.writeln(sb.toString()); + } + +} Property changes on: C:\Dev\apache.org\rw\xmlgraphics-commons-trunk\src\java\org\apache\xmlgraphics\ps\dsc\events\AbstractResourcesDSCComment.java ___________________________________________________________________ Name: svn:eol-style + native Index: C:/Dev/apache.org/rw/xmlgraphics-commons-trunk/src/java/org/apache/xmlgraphics/ps/dsc/events/DSCCommentPages.java =================================================================== --- C:/Dev/apache.org/rw/xmlgraphics-commons-trunk/src/java/org/apache/xmlgraphics/ps/dsc/events/DSCCommentPages.java (revision 0) +++ C:/Dev/apache.org/rw/xmlgraphics-commons-trunk/src/java/org/apache/xmlgraphics/ps/dsc/events/DSCCommentPages.java (revision 0) @@ -0,0 +1,96 @@ +/* + * 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.xmlgraphics.ps.dsc.events; + +import java.io.IOException; + +import org.apache.xmlgraphics.ps.DSCConstants; +import org.apache.xmlgraphics.ps.PSGenerator; + +/** + * Represents the %%Pages DSC comment. + */ +public class DSCCommentPages extends AbstractDSCComment { + + private int pageCount = -1; + + /** + * Creates a new instance. + */ + public DSCCommentPages() { + } + + /** + * Creates a new instance. + * @param pageCount the number of pages + */ + public DSCCommentPages(int pageCount) { + this.pageCount = pageCount; + } + + /** + * Returns the page count. + * @return the page count + */ + public int getPageCount() { + return this.pageCount; + } + + /** + * Sets the page count. + * @param count the new page count + */ + public void setPageCount(int count) { + this.pageCount = count; + } + + /** + * @see org.apache.xmlgraphics.ps.dsc.events.DSCComment#getName() + */ + public String getName() { + return DSCConstants.PAGES; + } + + /** + * @see org.apache.xmlgraphics.ps.dsc.events.DSCComment#hasValues() + */ + public boolean hasValues() { + return true; + } + + /** + * @see org.apache.xmlgraphics.ps.dsc.events.DSCComment#parseValue(java.lang.String) + */ + public void parseValue(String value) { + this.pageCount = Integer.parseInt(value); + } + + /** + * @see org.apache.xmlgraphics.ps.dsc.events.DSCEvent#generate(org.apache.xmlgraphics.ps.PSGenerator) + */ + public void generate(PSGenerator gen) throws IOException { + if (getPageCount() > 0) { + gen.writeDSCComment(getName(), new Integer(getPageCount())); + } else { + gen.writeDSCComment(getName(), DSCConstants.ATEND); + } + } + +} Property changes on: C:\Dev\apache.org\rw\xmlgraphics-commons-trunk\src\java\org\apache\xmlgraphics\ps\dsc\events\DSCCommentPages.java ___________________________________________________________________ Name: svn:eol-style + native Index: C:/Dev/apache.org/rw/xmlgraphics-commons-trunk/src/java/org/apache/xmlgraphics/ps/dsc/events/package.html =================================================================== --- C:/Dev/apache.org/rw/xmlgraphics-commons-trunk/src/java/org/apache/xmlgraphics/ps/dsc/events/package.html (revision 0) +++ C:/Dev/apache.org/rw/xmlgraphics-commons-trunk/src/java/org/apache/xmlgraphics/ps/dsc/events/package.html (revision 0) @@ -0,0 +1,23 @@ + + + +org.apache.xmlgraphics.ps.dsc.events Package + +

Event classes used by the DSC parser.

+ + \ No newline at end of file Property changes on: C:\Dev\apache.org\rw\xmlgraphics-commons-trunk\src\java\org\apache\xmlgraphics\ps\dsc\events\package.html ___________________________________________________________________ Name: svn:eol-style + native Index: C:/Dev/apache.org/rw/xmlgraphics-commons-trunk/src/java/org/apache/xmlgraphics/ps/dsc/events/AbstractDSCComment.java =================================================================== --- C:/Dev/apache.org/rw/xmlgraphics-commons-trunk/src/java/org/apache/xmlgraphics/ps/dsc/events/AbstractDSCComment.java (revision 0) +++ C:/Dev/apache.org/rw/xmlgraphics-commons-trunk/src/java/org/apache/xmlgraphics/ps/dsc/events/AbstractDSCComment.java (revision 0) @@ -0,0 +1,157 @@ +/* + * 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.xmlgraphics.ps.dsc.events; + +import java.util.List; + +/** + * Abstract base class for DSC comments. + */ +public abstract class AbstractDSCComment extends AbstractEvent implements DSCComment { + + private final boolean isWhitespace(char c) { + return c == ' ' || c == '\t'; + } + + private int parseNextParam(String value, int pos, List lst) { + int startPos = pos; + pos++; + while (pos < value.length() && !isWhitespace(value.charAt(pos))) { + pos++; + } + String param = value.substring(startPos, pos); + lst.add(param); + return pos; + } + + private int parseNextParentheseString(String value, int pos, List lst) { + int nestLevel = 1; + pos++; + StringBuffer sb = new StringBuffer(); + while (pos < value.length() && nestLevel > 0) { + final char c = value.charAt(pos); + switch (c) { + case '(': + nestLevel++; + if (nestLevel > 1) { + sb.append(c); + } + break; + case ')': + if (nestLevel > 1) { + sb.append(c); + } + nestLevel--; + break; + case '\\': + pos++; + char cnext = value.charAt(pos); + switch (cnext) { + case '\\': + sb.append(cnext); + break; + case 'n': + sb.append('\n'); + break; + case 'r': + sb.append('\r'); + break; + case 't': + sb.append('\t'); + break; + case 'b': + sb.append('\b'); + break; + case 'f': + sb.append('\f'); + break; + case '(': + sb.append('('); + break; + case ')': + sb.append(')'); + break; + default: + int code = Integer.parseInt(value.substring(pos, pos + 3), 8); + sb.append((char)code); + pos += 2; + } + break; + default: + sb.append(c); + } + pos++; + } + lst.add(sb.toString()); + pos++; + return pos; + } + + /** + * Splits the params of the DSC comment value in to a List. + * @param value the DSC comment value + * @return the List of values + */ + protected List splitParams(String value) { + List lst = new java.util.ArrayList(); + int pos = 0; + value = value.trim(); + while (pos < value.length()) { + if (isWhitespace(value.charAt(pos))) { + pos++; + continue; + } + if (value.charAt(pos) == '(') { + pos = parseNextParentheseString(value, pos, lst); + } else { + pos = parseNextParam(value, pos, lst); + } + } + return lst; + } + + /** + * @see org.apache.xmlgraphics.ps.dsc.events.DSCComment#isAtend() + */ + public boolean isAtend() { + return false; + } + + /** + * @see org.apache.xmlgraphics.ps.dsc.events.AbstractEvent#asDSCComment() + */ + public DSCComment asDSCComment() { + return this; + } + + /** + * @see org.apache.xmlgraphics.ps.dsc.events.AbstractEvent#isDSCComment() + */ + public boolean isDSCComment() { + return true; + } + + /** + * @see org.apache.xmlgraphics.ps.dsc.events.DSCEvent#getEventType() + */ + public int getEventType() { + return DSC_COMMENT; + } +} Property changes on: C:\Dev\apache.org\rw\xmlgraphics-commons-trunk\src\java\org\apache\xmlgraphics\ps\dsc\events\AbstractDSCComment.java ___________________________________________________________________ Name: svn:eol-style + native Index: C:/Dev/apache.org/rw/xmlgraphics-commons-trunk/src/java/org/apache/xmlgraphics/ps/dsc/events/PostScriptLine.java =================================================================== --- C:/Dev/apache.org/rw/xmlgraphics-commons-trunk/src/java/org/apache/xmlgraphics/ps/dsc/events/PostScriptLine.java (revision 0) +++ C:/Dev/apache.org/rw/xmlgraphics-commons-trunk/src/java/org/apache/xmlgraphics/ps/dsc/events/PostScriptLine.java (revision 0) @@ -0,0 +1,77 @@ +/* + * 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.xmlgraphics.ps.dsc.events; + +import java.io.IOException; + +import org.apache.xmlgraphics.ps.PSGenerator; + +/** + * Represents a line of PostScript code. + */ +public class PostScriptLine extends AbstractEvent { + + private String line; + + /** + * Creates a new instance. + * @param line the code line + */ + public PostScriptLine(String line) { + this.line = line; + } + + /** + * Returns the code line. + * @return the code line + */ + public String getLine() { + return this.line; + } + + /** + * @see org.apache.xmlgraphics.ps.dsc.events.DSCEvent#generate(org.apache.xmlgraphics.ps.PSGenerator) + */ + public void generate(PSGenerator gen) throws IOException { + gen.writeln(getLine()); + } + + /** + * @see org.apache.xmlgraphics.ps.dsc.events.DSCEvent#getEventType() + */ + public int getEventType() { + return LINE; + } + + /** + * @see org.apache.xmlgraphics.ps.dsc.events.AbstractEvent#asLine() + */ + public PostScriptLine asLine() { + return this; + } + + /** + * @see org.apache.xmlgraphics.ps.dsc.events.AbstractEvent#isLine() + */ + public boolean isLine() { + return true; + } + +} Property changes on: C:\Dev\apache.org\rw\xmlgraphics-commons-trunk\src\java\org\apache\xmlgraphics\ps\dsc\events\PostScriptLine.java ___________________________________________________________________ Name: svn:eol-style + native Index: C:/Dev/apache.org/rw/xmlgraphics-commons-trunk/src/java/org/apache/xmlgraphics/ps/dsc/events/DSCCommentPageResources.java =================================================================== --- C:/Dev/apache.org/rw/xmlgraphics-commons-trunk/src/java/org/apache/xmlgraphics/ps/dsc/events/DSCCommentPageResources.java (revision 0) +++ C:/Dev/apache.org/rw/xmlgraphics-commons-trunk/src/java/org/apache/xmlgraphics/ps/dsc/events/DSCCommentPageResources.java (revision 0) @@ -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.xmlgraphics.ps.dsc.events; + +import java.util.Collection; + +import org.apache.xmlgraphics.ps.DSCConstants; + +/** + * Represents a %%PageResources DSC comment. + */ +public class DSCCommentPageResources extends AbstractResourcesDSCComment { + + /** + * Creates a new instance. + */ + public DSCCommentPageResources() { + super(); + } + + /** + * Creates a new instance. + * @param resources a Collection of PSResource instances + */ + public DSCCommentPageResources(Collection resources) { + super(resources); + } + + /** + * @see org.apache.xmlgraphics.ps.dsc.events.DSCComment#getName() + */ + public String getName() { + return DSCConstants.PAGE_RESOURCES; + } + +} Property changes on: C:\Dev\apache.org\rw\xmlgraphics-commons-trunk\src\java\org\apache\xmlgraphics\ps\dsc\events\DSCCommentPageResources.java ___________________________________________________________________ Name: svn:eol-style + native Index: C:/Dev/apache.org/rw/xmlgraphics-commons-trunk/src/java/org/apache/xmlgraphics/ps/dsc/events/DSCAtend.java =================================================================== --- C:/Dev/apache.org/rw/xmlgraphics-commons-trunk/src/java/org/apache/xmlgraphics/ps/dsc/events/DSCAtend.java (revision 0) +++ C:/Dev/apache.org/rw/xmlgraphics-commons-trunk/src/java/org/apache/xmlgraphics/ps/dsc/events/DSCAtend.java (revision 0) @@ -0,0 +1,88 @@ +/* + * 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.xmlgraphics.ps.dsc.events; + +import java.io.IOException; + +import org.apache.xmlgraphics.ps.DSCConstants; +import org.apache.xmlgraphics.ps.PSGenerator; +import org.apache.xmlgraphics.ps.dsc.DSCCommentFactory; + +/** + * This class represents a DSC comment with an "(ATEND)" value. + */ +public class DSCAtend extends AbstractDSCComment { + + private String name; + + /** + * Creates a new instance + * @param name the name of the DSC comment (without the "%%" prefix) + */ + public DSCAtend(String name) { + this.name = name; + } + + /** + * @see org.apache.xmlgraphics.ps.dsc.events.DSCComment#getName() + */ + public String getName() { + return this.name; + } + + /** + * @see org.apache.xmlgraphics.ps.dsc.events.DSCComment#hasValues() + */ + public boolean hasValues() { + return false; + } + + /** + * @see org.apache.xmlgraphics.ps.dsc.events.AbstractDSCComment#isAtend() + */ + public boolean isAtend() { + return true; + } + + /** + * @see org.apache.xmlgraphics.ps.dsc.events.DSCComment#parseValue(java.lang.String) + */ + public void parseValue(String value) { + //nop + } + + /** + * @see org.apache.xmlgraphics.ps.dsc.events.DSCEvent#generate( + * org.apache.xmlgraphics.ps.PSGenerator) + */ + public void generate(PSGenerator gen) throws IOException { + gen.writeDSCComment(getName(), DSCConstants.ATEND); + } + + /** + * Creates a new instance of a DSC comment with the same name as this instance but does not + * have an "Atend" value. + * @return the new DSC comment + */ + public DSCComment createDSCCommentFromAtend() { + return DSCCommentFactory.createDSCCommentFor(getName()); + } + +} Property changes on: C:\Dev\apache.org\rw\xmlgraphics-commons-trunk\src\java\org\apache\xmlgraphics\ps\dsc\events\DSCAtend.java ___________________________________________________________________ Name: svn:eol-style + native Index: C:/Dev/apache.org/rw/xmlgraphics-commons-trunk/src/java/org/apache/xmlgraphics/ps/dsc/events/DSCCommentLanguageLevel.java =================================================================== --- C:/Dev/apache.org/rw/xmlgraphics-commons-trunk/src/java/org/apache/xmlgraphics/ps/dsc/events/DSCCommentLanguageLevel.java (revision 0) +++ C:/Dev/apache.org/rw/xmlgraphics-commons-trunk/src/java/org/apache/xmlgraphics/ps/dsc/events/DSCCommentLanguageLevel.java (revision 0) @@ -0,0 +1,87 @@ +/* + * 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.xmlgraphics.ps.dsc.events; + +import java.io.IOException; + +import org.apache.xmlgraphics.ps.DSCConstants; +import org.apache.xmlgraphics.ps.PSGenerator; + +/** + * Represents a %%LanguageLevel DSC comment + */ +public class DSCCommentLanguageLevel extends AbstractDSCComment { + + private int level; + + /** + * Creates a new instance. + */ + public DSCCommentLanguageLevel() { + } + + /** + * Creates a new instance + * @param level the PostScript language level (usually 2 or 3) + */ + public DSCCommentLanguageLevel(int level) { + this.level = level; + } + + /** + * Returns the PostScript language level (usually 2 or 3). + * @return the language level + */ + public int getLanguageLevel() { + return this.level; + } + + /** + * @see org.apache.xmlgraphics.ps.dsc.events.DSCComment#getName() + */ + public String getName() { + return DSCConstants.LANGUAGE_LEVEL; + } + + /** + * @see org.apache.xmlgraphics.ps.dsc.events.DSCComment#hasValues() + */ + public boolean hasValues() { + return true; + } + + /** + * @see org.apache.xmlgraphics.ps.dsc.events.DSCComment#parseValue(java.lang.String) + */ + public void parseValue(String value) { + this.level = Integer.parseInt(value); + } + + /** + * @see org.apache.xmlgraphics.ps.dsc.events.DSCEvent#generate(org.apache.xmlgraphics.ps.PSGenerator) + */ + public void generate(PSGenerator gen) throws IOException { + if (level <= 0) { + throw new IllegalStateException("Language Level was not properly set"); + } + gen.writeDSCComment(getName(), new Integer(getLanguageLevel())); + } + +} Property changes on: C:\Dev\apache.org\rw\xmlgraphics-commons-trunk\src\java\org\apache\xmlgraphics\ps\dsc\events\DSCCommentLanguageLevel.java ___________________________________________________________________ Name: svn:eol-style + native Index: C:/Dev/apache.org/rw/xmlgraphics-commons-trunk/src/java/org/apache/xmlgraphics/ps/dsc/events/DSCCommentEndComments.java =================================================================== --- C:/Dev/apache.org/rw/xmlgraphics-commons-trunk/src/java/org/apache/xmlgraphics/ps/dsc/events/DSCCommentEndComments.java (revision 0) +++ C:/Dev/apache.org/rw/xmlgraphics-commons-trunk/src/java/org/apache/xmlgraphics/ps/dsc/events/DSCCommentEndComments.java (revision 0) @@ -0,0 +1,61 @@ +/* + * 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.xmlgraphics.ps.dsc.events; + +import java.io.IOException; + +import org.apache.xmlgraphics.ps.DSCConstants; +import org.apache.xmlgraphics.ps.PSGenerator; + +/** + * Respresents a %%EndComments DSC comment. + */ +public class DSCCommentEndComments extends AbstractDSCComment { + + /** + * @see org.apache.xmlgraphics.ps.dsc.events.DSCComment#getName() + */ + public String getName() { + return DSCConstants.END_COMMENTS; + } + + /** + * @see org.apache.xmlgraphics.ps.dsc.events.DSCComment#hasValues() + */ + public boolean hasValues() { + return false; + } + + /** + * @see org.apache.xmlgraphics.ps.dsc.events.DSCComment#parseValue(java.lang.String) + */ + public void parseValue(String value) { + //nop + } + + /** + * @see org.apache.xmlgraphics.ps.dsc.events.DSCEvent#generate( + * org.apache.xmlgraphics.ps.PSGenerator) + */ + public void generate(PSGenerator gen) throws IOException { + gen.writeDSCComment(getName()); + } + +} Property changes on: C:\Dev\apache.org\rw\xmlgraphics-commons-trunk\src\java\org\apache\xmlgraphics\ps\dsc\events\DSCCommentEndComments.java ___________________________________________________________________ Name: svn:eol-style + native Index: C:/Dev/apache.org/rw/xmlgraphics-commons-trunk/src/java/org/apache/xmlgraphics/ps/dsc/events/DSCCommentPage.java =================================================================== --- C:/Dev/apache.org/rw/xmlgraphics-commons-trunk/src/java/org/apache/xmlgraphics/ps/dsc/events/DSCCommentPage.java (revision 0) +++ C:/Dev/apache.org/rw/xmlgraphics-commons-trunk/src/java/org/apache/xmlgraphics/ps/dsc/events/DSCCommentPage.java (revision 0) @@ -0,0 +1,129 @@ +/* + * 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.xmlgraphics.ps.dsc.events; + +import java.io.IOException; +import java.util.Iterator; +import java.util.List; + +import org.apache.xmlgraphics.ps.DSCConstants; +import org.apache.xmlgraphics.ps.PSGenerator; + +/** + * Represents a %%Page DSC comment. + */ +public class DSCCommentPage extends AbstractDSCComment { + + private String pageName; + private int pagePosition = -1; + + /** + * Creates a new instance. + */ + public DSCCommentPage() { + } + + /** + * Creates a new instance. + * @param pageName the name of the page + * @param pagePosition the position of the page within the file (1-based) + */ + public DSCCommentPage(String pageName, int pagePosition) { + setPageName(pageName); + setPagePosition(pagePosition); + } + + /** + * Creates a new instance. The page name will be set to the same value as the page position. + * @param pagePosition the position of the page within the file (1-based) + */ + public DSCCommentPage(int pagePosition) { + this(Integer.toString(pagePosition), pagePosition); + } + + /** + * Resturns the name of the page. + * @return the page name + */ + public String getPageName() { + return this.pageName; + } + + /** + * Sets the page name. + * @param name the page name + */ + public void setPageName(String name) { + this.pageName = name; + } + + /** + * Returns the page position. + * @return the page position (1-based) + */ + public int getPagePosition() { + return this.pagePosition; + } + + /** + * Sets the page position. + * @param position the page position (1-based) + */ + public void setPagePosition(int position) { + if (position <= 0) { + throw new IllegalArgumentException("position must be 1 or above"); + } + this.pagePosition = position; + } + + /** + * @see org.apache.xmlgraphics.ps.dsc.events.DSCComment#getName() + */ + public String getName() { + return DSCConstants.PAGE; + } + + /** + * @see org.apache.xmlgraphics.ps.dsc.events.DSCComment#hasValues() + */ + public boolean hasValues() { + return true; + } + + /** + * @see org.apache.xmlgraphics.ps.dsc.events.DSCComment#parseValue(java.lang.String) + */ + public void parseValue(String value) { + List params = splitParams(value); + Iterator iter = params.iterator(); + this.pageName = (String)iter.next(); + this.pagePosition = Integer.parseInt((String)iter.next()); + } + + /** + * @see org.apache.xmlgraphics.ps.dsc.events.DSCEvent#generate( + * org.apache.xmlgraphics.ps.PSGenerator) + */ + public void generate(PSGenerator gen) throws IOException { + gen.writeDSCComment(getName(), + new Object[] {getPageName(), new Integer(getPagePosition())}); + } + +} Property changes on: C:\Dev\apache.org\rw\xmlgraphics-commons-trunk\src\java\org\apache\xmlgraphics\ps\dsc\events\DSCCommentPage.java ___________________________________________________________________ Name: svn:eol-style + native Index: C:/Dev/apache.org/rw/xmlgraphics-commons-trunk/src/java/org/apache/xmlgraphics/ps/dsc/events/DSCCommentDocumentNeededResources.java =================================================================== --- C:/Dev/apache.org/rw/xmlgraphics-commons-trunk/src/java/org/apache/xmlgraphics/ps/dsc/events/DSCCommentDocumentNeededResources.java (revision 0) +++ C:/Dev/apache.org/rw/xmlgraphics-commons-trunk/src/java/org/apache/xmlgraphics/ps/dsc/events/DSCCommentDocumentNeededResources.java (revision 0) @@ -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.xmlgraphics.ps.dsc.events; + +import java.util.Collection; + +import org.apache.xmlgraphics.ps.DSCConstants; + +/** + * Represents a %%DocumentNeededResources DSC comment. + */ +public class DSCCommentDocumentNeededResources extends AbstractResourcesDSCComment { + + /** + * Creates a new instance. + */ + public DSCCommentDocumentNeededResources() { + super(); + } + + /** + * Creates a new instance. + * @param resources a Collection of PSResource instances + */ + public DSCCommentDocumentNeededResources(Collection resources) { + super(resources); + } + + /** + * @see org.apache.xmlgraphics.ps.dsc.events.DSCComment#getName() + */ + public String getName() { + return DSCConstants.DOCUMENT_NEEDED_RESOURCES; + } + +} Property changes on: C:\Dev\apache.org\rw\xmlgraphics-commons-trunk\src\java\org\apache\xmlgraphics\ps\dsc\events\DSCCommentDocumentNeededResources.java ___________________________________________________________________ Name: svn:eol-style + native Index: C:/Dev/apache.org/rw/xmlgraphics-commons-trunk/src/java/org/apache/xmlgraphics/ps/dsc/events/DSCComment.java =================================================================== --- C:/Dev/apache.org/rw/xmlgraphics-commons-trunk/src/java/org/apache/xmlgraphics/ps/dsc/events/DSCComment.java (revision 0) +++ C:/Dev/apache.org/rw/xmlgraphics-commons-trunk/src/java/org/apache/xmlgraphics/ps/dsc/events/DSCComment.java (revision 0) @@ -0,0 +1,61 @@ +/* + * 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.xmlgraphics.ps.dsc.events; + +import java.io.IOException; + +import org.apache.xmlgraphics.ps.PSGenerator; + +/** + * Base interface for all DSC comments. + */ +public interface DSCComment extends DSCEvent { + + /** + * Returns the name of the DSC comment. + * @return the name of the DSC comment (without the "%%" prefix) + */ + String getName(); + + /** + * Parses the value of the DSC comment. + * @param value the value + */ + void parseValue(String value); + + /** + * Indicates whether this DSC comment has values. + * @return true if the DSC comment has values + */ + boolean hasValues(); + + /** + * Indicates whether the DSC comment's value is "Atend". + * @return true if the value is "Atend" + */ + boolean isAtend(); + + /** + * @see org.apache.xmlgraphics.ps.dsc.events.DSCEvent#generate( + * org.apache.xmlgraphics.ps.PSGenerator) + */ + void generate(PSGenerator gen) throws IOException; + +} Property changes on: C:\Dev\apache.org\rw\xmlgraphics-commons-trunk\src\java\org\apache\xmlgraphics\ps\dsc\events\DSCComment.java ___________________________________________________________________ Name: svn:eol-style + native Index: C:/Dev/apache.org/rw/xmlgraphics-commons-trunk/src/java/org/apache/xmlgraphics/ps/dsc/events/DSCCommentDocumentSuppliedResources.java =================================================================== --- C:/Dev/apache.org/rw/xmlgraphics-commons-trunk/src/java/org/apache/xmlgraphics/ps/dsc/events/DSCCommentDocumentSuppliedResources.java (revision 0) +++ C:/Dev/apache.org/rw/xmlgraphics-commons-trunk/src/java/org/apache/xmlgraphics/ps/dsc/events/DSCCommentDocumentSuppliedResources.java (revision 0) @@ -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.xmlgraphics.ps.dsc.events; + +import java.util.Collection; + +import org.apache.xmlgraphics.ps.DSCConstants; + +/** + * Represents a %%DocumentSuppliedResources DSC comment. + */ +public class DSCCommentDocumentSuppliedResources extends AbstractResourcesDSCComment { + + /** + * Creates a new instance. + */ + public DSCCommentDocumentSuppliedResources() { + super(); + } + + /** + * Creates a new instance. + * @param resources a Collection of PSResource instances + */ + public DSCCommentDocumentSuppliedResources(Collection resources) { + super(resources); + } + + /** + * @see org.apache.xmlgraphics.ps.dsc.events.DSCComment#getName() + */ + public String getName() { + return DSCConstants.DOCUMENT_SUPPLIED_RESOURCES; + } + +} Property changes on: C:\Dev\apache.org\rw\xmlgraphics-commons-trunk\src\java\org\apache\xmlgraphics\ps\dsc\events\DSCCommentDocumentSuppliedResources.java ___________________________________________________________________ Name: svn:eol-style + native Index: C:/Dev/apache.org/rw/xmlgraphics-commons-trunk/src/java/org/apache/xmlgraphics/ps/dsc/events/PostScriptComment.java =================================================================== --- C:/Dev/apache.org/rw/xmlgraphics-commons-trunk/src/java/org/apache/xmlgraphics/ps/dsc/events/PostScriptComment.java (revision 0) +++ C:/Dev/apache.org/rw/xmlgraphics-commons-trunk/src/java/org/apache/xmlgraphics/ps/dsc/events/PostScriptComment.java (revision 0) @@ -0,0 +1,74 @@ +/* + * 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.xmlgraphics.ps.dsc.events; + +import java.io.IOException; + +import org.apache.xmlgraphics.ps.PSGenerator; + +/** + * Represents a PostScript comment + */ +public class PostScriptComment extends AbstractEvent { + + private String comment; + + /** + * Creates a new instance. + * @param comment the comment + */ + public PostScriptComment(String comment) { + if (comment != null && comment.startsWith("%")) { + this.comment = this.comment.substring(1); + } else { + this.comment = comment; + } + } + + /** + * Returns the comment text. + * @return the comment (without the "%" prefix) + */ + public String getComment() { + return this.comment; + } + + /** + * @see org.apache.xmlgraphics.ps.dsc.events.DSCEvent#generate(org.apache.xmlgraphics.ps.PSGenerator) + */ + public void generate(PSGenerator gen) throws IOException { + gen.commentln("%" + getComment()); + } + + /** + * @see org.apache.xmlgraphics.ps.dsc.events.DSCEvent#getEventType() + */ + public int getEventType() { + return COMMENT; + } + + /** + * @see org.apache.xmlgraphics.ps.dsc.events.AbstractEvent#isComment() + */ + public boolean isComment() { + return true; + } + +} Property changes on: C:\Dev\apache.org\rw\xmlgraphics-commons-trunk\src\java\org\apache\xmlgraphics\ps\dsc\events\PostScriptComment.java ___________________________________________________________________ Name: svn:eol-style + native Index: C:/Dev/apache.org/rw/xmlgraphics-commons-trunk/src/java/org/apache/xmlgraphics/ps/dsc/events/UnparsedDSCComment.java =================================================================== --- C:/Dev/apache.org/rw/xmlgraphics-commons-trunk/src/java/org/apache/xmlgraphics/ps/dsc/events/UnparsedDSCComment.java (revision 0) +++ C:/Dev/apache.org/rw/xmlgraphics-commons-trunk/src/java/org/apache/xmlgraphics/ps/dsc/events/UnparsedDSCComment.java (revision 0) @@ -0,0 +1,100 @@ +/* + * 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.xmlgraphics.ps.dsc.events; + +import java.io.IOException; + +import org.apache.xmlgraphics.ps.PSGenerator; + +/** + * Represents a DSC comment that is not parsed into one of the concrete DSCComment subclasses. + * It is used whenever a DSC comment is encountered that is unknown to the parser. + * @see org.apache.xmlgraphics.ps.dsc.DSCCommentFactory + */ +public class UnparsedDSCComment extends AbstractEvent implements DSCComment { + + private String name; + private String value; + + /** + * Creates a new instance. + * @param name the name of the DSC comment + */ + public UnparsedDSCComment(String name) { + this.name = name; + } + + /** + * @see org.apache.xmlgraphics.ps.dsc.events.DSCComment#getName() + */ + public String getName() { + return this.name; + } + + /** + * @see org.apache.xmlgraphics.ps.dsc.events.DSCComment#hasValues() + */ + public boolean hasValues() { + return value != null; + } + + /** + * @see org.apache.xmlgraphics.ps.dsc.events.DSCComment#isAtend() + */ + public boolean isAtend() { + return false; + } + + /** + * @see org.apache.xmlgraphics.ps.dsc.events.DSCComment#parseValue(java.lang.String) + */ + public void parseValue(String value) { + this.value = value; + } + + /** + * @see org.apache.xmlgraphics.ps.dsc.events.DSCEvent#generate(org.apache.xmlgraphics.ps.PSGenerator) + */ + public void generate(PSGenerator gen) throws IOException { + gen.writeln("%%" + name + (hasValues() ? ": " + value : "")); + } + + /** + * @see org.apache.xmlgraphics.ps.dsc.events.AbstractEvent#isDSCComment() + */ + public boolean isDSCComment() { + return true; + } + + /** + * @see org.apache.xmlgraphics.ps.dsc.events.DSCEvent#getEventType() + */ + public int getEventType() { + return DSC_COMMENT; + } + + /** + * @see org.apache.xmlgraphics.ps.dsc.events.AbstractEvent#asDSCComment() + */ + public DSCComment asDSCComment() { + return this; + } + +} Property changes on: C:\Dev\apache.org\rw\xmlgraphics-commons-trunk\src\java\org\apache\xmlgraphics\ps\dsc\events\UnparsedDSCComment.java ___________________________________________________________________ Name: svn:eol-style + native Index: C:/Dev/apache.org/rw/xmlgraphics-commons-trunk/src/java/org/apache/xmlgraphics/ps/dsc/DSCCommentFactory.java =================================================================== --- C:/Dev/apache.org/rw/xmlgraphics-commons-trunk/src/java/org/apache/xmlgraphics/ps/dsc/DSCCommentFactory.java (revision 0) +++ C:/Dev/apache.org/rw/xmlgraphics-commons-trunk/src/java/org/apache/xmlgraphics/ps/dsc/DSCCommentFactory.java (revision 0) @@ -0,0 +1,87 @@ +/* + * 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.xmlgraphics.ps.dsc; + +import java.util.Map; + +import org.apache.xmlgraphics.ps.DSCConstants; +import org.apache.xmlgraphics.ps.dsc.events.DSCComment; +import org.apache.xmlgraphics.ps.dsc.events.DSCCommentBeginResource; +import org.apache.xmlgraphics.ps.dsc.events.DSCCommentDocumentNeededResources; +import org.apache.xmlgraphics.ps.dsc.events.DSCCommentDocumentSuppliedResources; +import org.apache.xmlgraphics.ps.dsc.events.DSCCommentEndComments; +import org.apache.xmlgraphics.ps.dsc.events.DSCCommentLanguageLevel; +import org.apache.xmlgraphics.ps.dsc.events.DSCCommentPage; +import org.apache.xmlgraphics.ps.dsc.events.DSCCommentPageResources; +import org.apache.xmlgraphics.ps.dsc.events.DSCCommentPages; +import org.apache.xmlgraphics.ps.dsc.events.DSCCommentEndOfFile; + +/** + * Factory for DSCComment subclasses. + */ +public class DSCCommentFactory { + + private static final Map DSC_FACTORIES = new java.util.HashMap(); + + static { + DSC_FACTORIES.put(DSCConstants.END_COMMENTS, + DSCCommentEndComments.class); + DSC_FACTORIES.put(DSCConstants.BEGIN_RESOURCE, + DSCCommentBeginResource.class); + DSC_FACTORIES.put(DSCConstants.PAGE_RESOURCES, + DSCCommentPageResources.class); + DSC_FACTORIES.put(DSCConstants.PAGE, + DSCCommentPage.class); + DSC_FACTORIES.put(DSCConstants.PAGES, + DSCCommentPages.class); + DSC_FACTORIES.put(DSCConstants.LANGUAGE_LEVEL, + DSCCommentLanguageLevel.class); + DSC_FACTORIES.put(DSCConstants.DOCUMENT_NEEDED_RESOURCES, + DSCCommentDocumentNeededResources.class); + DSC_FACTORIES.put(DSCConstants.DOCUMENT_SUPPLIED_RESOURCES, + DSCCommentDocumentSuppliedResources.class); + DSC_FACTORIES.put(DSCConstants.EOF, + DSCCommentEndOfFile.class); + //TODO Add additional implementations as needed + } + + /** + * Creates and returns new instances for DSC comments with a given name. + * @param name the name of the DSCComment (without the "%%" prefix) + * @return the new instance or null if no particular subclass registered for the given + * DSC comment. + */ + public static DSCComment createDSCCommentFor(String name) { + Class clazz = (Class)DSC_FACTORIES.get(name); + if (clazz == null) { + return null; + } + try { + return (DSCComment)clazz.newInstance(); + } catch (InstantiationException e) { + throw new RuntimeException("Error instantiating instance for '" + name + "': " + + e.getMessage()); + } catch (IllegalAccessException e) { + throw new RuntimeException("Illegal Access error while instantiating instance for '" + + name + "': " + e.getMessage()); + } + } + +} Property changes on: C:\Dev\apache.org\rw\xmlgraphics-commons-trunk\src\java\org\apache\xmlgraphics\ps\dsc\DSCCommentFactory.java ___________________________________________________________________ Name: svn:eol-style + native Index: C:/Dev/apache.org/rw/xmlgraphics-commons-trunk/src/java/org/apache/xmlgraphics/ps/dsc/DefaultNestedDocumentHandler.java =================================================================== --- C:/Dev/apache.org/rw/xmlgraphics-commons-trunk/src/java/org/apache/xmlgraphics/ps/dsc/DefaultNestedDocumentHandler.java (revision 0) +++ C:/Dev/apache.org/rw/xmlgraphics-commons-trunk/src/java/org/apache/xmlgraphics/ps/dsc/DefaultNestedDocumentHandler.java (revision 0) @@ -0,0 +1,73 @@ +/* + * 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.xmlgraphics.ps.dsc; + +import java.io.IOException; + +import org.apache.xmlgraphics.ps.DSCConstants; +import org.apache.xmlgraphics.ps.PSGenerator; +import org.apache.xmlgraphics.ps.dsc.events.DSCComment; +import org.apache.xmlgraphics.ps.dsc.events.DSCEvent; + +/** + * Default implementation of the NestedDocumentHandler interface which automatically skips data + * between Begin/EndDocument and Begin/EndData. + */ +public class DefaultNestedDocumentHandler implements DSCParserConstants, NestedDocumentHandler { + + private PSGenerator gen; + + /** + * Creates a new instance. + * @param gen PSGenerator to pass through the skipped content + */ + public DefaultNestedDocumentHandler(PSGenerator gen) { + this.gen = gen; + } + + /** + * @see org.apache.xmlgraphics.ps.dsc.NestedDocumentHandler#handle(org.apache.xmlgraphics.ps.dsc.events.DSCEvent, org.apache.xmlgraphics.ps.dsc.DSCParser) + */ + public void handle(DSCEvent event, DSCParser parser) throws IOException, DSCException { + if (event.isDSCComment()) { + DSCComment comment = event.asDSCComment(); + if (DSCConstants.BEGIN_DOCUMENT.equals(comment.getName())) { + comment.generate(gen); + comment = parser.nextDSCComment(DSCConstants.END_DOCUMENT, gen); + if (comment == null) { + throw new DSCException("File is not DSC-compliant: Didn't find an " + + DSCConstants.END_DOCUMENT); + } + comment.generate(gen); + parser.next(); + } else if (DSCConstants.BEGIN_DATA.equals(comment.getName())) { + comment.generate(gen); + comment = parser.nextDSCComment(DSCConstants.END_DATA, gen); + if (comment == null) { + throw new DSCException("File is not DSC-compliant: Didn't find an " + + DSCConstants.END_DATA); + } + comment.generate(gen); + parser.next(); + } + } + } + +} Property changes on: C:\Dev\apache.org\rw\xmlgraphics-commons-trunk\src\java\org\apache\xmlgraphics\ps\dsc\DefaultNestedDocumentHandler.java ___________________________________________________________________ Name: svn:eol-style + native Index: C:/Dev/apache.org/rw/xmlgraphics-commons-trunk/src/java/org/apache/xmlgraphics/ps/dsc/DSCParser.java =================================================================== --- C:/Dev/apache.org/rw/xmlgraphics-commons-trunk/src/java/org/apache/xmlgraphics/ps/dsc/DSCParser.java (revision 0) +++ C:/Dev/apache.org/rw/xmlgraphics-commons-trunk/src/java/org/apache/xmlgraphics/ps/dsc/DSCParser.java (revision 0) @@ -0,0 +1,385 @@ +/* + * 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.xmlgraphics.ps.dsc; + +import java.io.BufferedReader; +import java.io.IOException; +import java.io.InputStream; +import java.io.UnsupportedEncodingException; +import java.util.NoSuchElementException; + +import org.apache.xmlgraphics.ps.DSCConstants; +import org.apache.xmlgraphics.ps.PSGenerator; +import org.apache.xmlgraphics.ps.dsc.events.DSCAtend; +import org.apache.xmlgraphics.ps.dsc.events.DSCComment; +import org.apache.xmlgraphics.ps.dsc.events.DSCEvent; +import org.apache.xmlgraphics.ps.dsc.events.DSCHeaderComment; +import org.apache.xmlgraphics.ps.dsc.events.PostScriptComment; +import org.apache.xmlgraphics.ps.dsc.events.PostScriptLine; +import org.apache.xmlgraphics.ps.dsc.events.UnparsedDSCComment; +import org.apache.xmlgraphics.ps.dsc.tools.DSCTools; + +/** + * Parser for DSC-compliant PostScript files (DSC = Document Structuring Conventions). The parser + * is implemented as a pull parser but has the ability to act as a push parser through the + * DSCHandler interface. + */ +public class DSCParser implements DSCParserConstants { + + private InputStream in; + private BufferedReader reader; + private boolean eofFound = false; + private DSCEvent currentEvent; + private DSCEvent nextEvent; + private DSCFilter filter; + private NestedDocumentHandler nestedDocumentHandler; + + /** + * Creates a new DSC parser. + * @param in InputStream to read the PostScript file from + * (the stream is not closed by this class, the caller is responsible for that) + * @throws IOException In case of an I/O error + * @throws DSCException In case of a violation of the DSC spec + */ + public DSCParser(InputStream in) throws IOException, DSCException { + if (in.markSupported()) { + this.in = in; + } else { + //Decorate for better performance + this.in = new java.io.BufferedInputStream(this.in); + } + String encoding = "US-ASCII"; + try { + this.reader = new java.io.BufferedReader( + new java.io.InputStreamReader(this.in, encoding)); + } catch (UnsupportedEncodingException e) { + throw new RuntimeException("Incompatible VM! " + e.getMessage()); + } + parseNext(); + } + + /** + * Returns the InputStream the PostScript code is read from. + * @return the InputStream the PostScript code is read from + */ + public InputStream getInputStream() { + return this.in; + } + + /** + * This method is used to write out warning messages for the parsing process. Subclass to + * override this method. The default implementation writes to System.err. + * @param msg the warning message + */ + protected void warn(String msg) { + System.err.println(msg); + } + + /** + * Reads one line from the input file + * @return the line or null if there are no more lines + * @throws IOException In case of an I/O error + * @throws DSCException In case of a violation of the DSC spec + */ + protected String readLine() throws IOException, DSCException { + String line; + line = this.reader.readLine(); + checkLine(line); + + return line; + } + + private void checkLine(String line) throws DSCException { + if (line == null) { + if (!eofFound) { + throw new DSCException("%%EOF not found. File is not well-formed."); + } + } else if (line.length() > 255) { + warn("Line longer than 255 characters. This file is not fully PostScript conforming."); + } + } + + private final boolean isWhitespace(char c) { + return c == ' ' || c == '\t'; + } + + private DSCComment parseDSCLine(String line) throws IOException, DSCException { + int colon = line.indexOf(':'); + String name, value; + if (colon > 0) { + name = line.substring(2, colon); + int startOfValue = colon + 1; + if (isWhitespace(line.charAt(startOfValue))) { + startOfValue++; + } + value = line.substring(startOfValue).trim(); + if (value.equals(DSCConstants.ATEND.toString())) { + return new DSCAtend(name); + } + String nextLine; + while (true) { + this.reader.mark(512); + nextLine = readLine(); + if (nextLine == null) { + break; + } else if (!nextLine.startsWith("%%+")) { + break; + } + value = value + nextLine.substring(3); + } + this.reader.reset(); + } else { + name = line.substring(2); + value = null; + } + return parseDSCComment(name, value); + } + + private DSCComment parseDSCComment(String name, String value) { + DSCComment parsed = DSCCommentFactory.createDSCCommentFor(name); + if (parsed != null) { + parsed.parseValue(value); + return parsed; + } else { + UnparsedDSCComment unparsed = new UnparsedDSCComment(name); + unparsed.parseValue(value); + return unparsed; + } + } + + /** + * Starts the parser in push parsing mode sending events to the DSCHandler instance. + * @param handler the DSCHandler instance to send the events to + * @throws IOException In case of an I/O error + * @throws DSCException In case of a violation of the DSC spec + */ + public void parse(DSCHandler handler) throws IOException, DSCException { + DSCHeaderComment header = DSCTools.checkAndSkipDSC30Header(this); + handler.startDocument("%!" + header.getComment()); + DSCEvent event; + while (hasNext()) { + event = nextEvent(); + switch (event.getEventType()) { + case HEADER_COMMENT: + handler.startDocument("%!" + ((DSCHeaderComment)event).getComment()); + break; + case DSC_COMMENT: + handler.handleDSCComment(event.asDSCComment()); + break; + case COMMENT: + handler.comment(((PostScriptComment)event).getComment()); + break; + case LINE: + handler.line(getLine()); + break; + case EOF: + this.eofFound = true; + handler.endDocument(); + break; + default: + throw new IllegalStateException("Illegal event type: " + event.getEventType()); + } + } + } + + /** + * Indicates whether there are additional items. + * @return true if there are additonal items, false if the end of the file has been reached + */ + public boolean hasNext() { + return (this.nextEvent != null); + } + + /** + * Steps to the next item indicating the type of event. + * @return the type of event (See {@link DSCParserConstants}) + * @throws IOException In case of an I/O error + * @throws DSCException In case of a violation of the DSC spec + * @throws NoSuchElementException If an attempt was made to advance beyond the end of the file + */ + public int next() throws IOException, DSCException { + if (hasNext()) { + this.currentEvent = nextEvent; + parseNext(); + if (this.nestedDocumentHandler != null) { + this.nestedDocumentHandler.handle(this.currentEvent, this); + } + return this.currentEvent.getEventType(); + } else { + throw new NoSuchElementException("There are no more events"); + } + } + + /** + * Steps to the next item returning the new event. + * @return the new event + * @throws IOException In case of an I/O error + * @throws DSCException In case of a violation of the DSC spec + */ + public DSCEvent nextEvent() throws IOException, DSCException { + next(); + return getCurrentEvent(); + } + + /** + * Returns the current event. + * @return the current event + */ + public DSCEvent getCurrentEvent() { + return this.currentEvent; + } + + /** + * Returns the next event without moving the cursor to the next event. + * @return the next event + */ + public DSCEvent peek() { + return this.nextEvent; + } + + /** + * Parses the next event. + * @throws IOException In case of an I/O error + * @throws DSCException In case of a violation of the DSC spec + */ + protected void parseNext() throws IOException, DSCException { + String line = readLine(); + if (line != null) { + if (eofFound && (line.length() > 0)) { + throw new DSCException("Content found after EOF"); + } + if (line.startsWith("%%")) { + DSCComment comment = parseDSCLine(line); + if (comment.getEventType() == EOF) { + this.eofFound = true; + } + this.nextEvent = comment; + } else if (line.startsWith("%!")) { + this.nextEvent = new DSCHeaderComment(line.substring(2)); + } else if (line.startsWith("%")) { + this.nextEvent = new PostScriptComment(line.substring(1)); + } else { + this.nextEvent = new PostScriptLine(line); + } + if (this.filter != null && !filter.accept(this.nextEvent)) { + parseNext(); //skip + } + } else { + this.nextEvent = null; + } + } + + /** + * Returns the current PostScript line. + * @return the current PostScript line + * @throws IllegalStateException if the current event is not a normal PostScript line + */ + public String getLine() { + if (this.currentEvent.getEventType() == LINE) { + return ((PostScriptLine)this.currentEvent).getLine(); + } else { + throw new IllegalStateException("Current event is not a PostScript line"); + } + } + + /** + * Advances to the next DSC comment with the given name. + * @param name the name of the DSC comment + * @return the requested DSC comment or null if the end of the file is reached + * @throws IOException In case of an I/O error + * @throws DSCException In case of a violation of the DSC spec + */ + public DSCComment nextDSCComment(String name) + throws IOException, DSCException { + return nextDSCComment(name, null); + } + + /** + * Advances to the next DSC comment with the given name. + * @param name the name of the DSC comment + * @param gen PSGenerator to pass the skipped events though to + * @return the requested DSC comment or null if the end of the file is reached + * @throws IOException In case of an I/O error + * @throws DSCException In case of a violation of the DSC spec + */ + public DSCComment nextDSCComment(String name, PSGenerator gen) + throws IOException, DSCException { + while (hasNext()) { + DSCEvent event = nextEvent(); + if (event.isDSCComment()) { + DSCComment comment = event.asDSCComment(); + if (name.equals(comment.getName())) { + return comment; + } + } + if (gen != null) { + event.generate(gen); //Pipe through to PSGenerator + } + } + return null; + } + + /** + * Advances to the next PostScript comment with the given prefix. This is used to find + * comments following the DSC extension mechanism. + *

+ * Example: To find FOP's custom comments, pass in "FOP" as a prefix. This will find comments + * like "%FOPFontSetup". + * @param prefix the prefix of the extension comment + * @param gen PSGenerator to pass the skipped events though to + * @return the requested PostScript comment or null if the end of the file is reached + * @throws IOException In case of an I/O error + * @throws DSCException In case of a violation of the DSC spec + */ + public PostScriptComment nextPSComment(String prefix, PSGenerator gen) + throws IOException, DSCException { + while (hasNext()) { + DSCEvent event = nextEvent(); + if (event.isComment()) { + PostScriptComment comment = (PostScriptComment)event; + if (comment.getComment().startsWith(prefix)) { + return comment; + } + } + if (gen != null) { + event.generate(gen); //Pipe through to PSGenerator + } + } + return null; + } + + /** + * Sets a filter for DSC events. + * @param filter the filter to use or null to disable filtering + */ + public void setFilter(DSCFilter filter) { + this.filter = filter; + } + + /** + * Sets a NestedDocumentHandler which is used to skip nested documents like embedded EPS files. + * You can also process those parts in a special way. + * @param handler the NestedDocumentHandler instance or null to disable the feature + */ + public void setNestedDocumentHandler(NestedDocumentHandler handler) { + this.nestedDocumentHandler = handler; + } + +} Property changes on: C:\Dev\apache.org\rw\xmlgraphics-commons-trunk\src\java\org\apache\xmlgraphics\ps\dsc\DSCParser.java ___________________________________________________________________ Name: svn:eol-style + native Index: C:/Dev/apache.org/rw/xmlgraphics-commons-trunk/src/java/org/apache/xmlgraphics/ps/dsc/EventRecorder.java =================================================================== --- C:/Dev/apache.org/rw/xmlgraphics-commons-trunk/src/java/org/apache/xmlgraphics/ps/dsc/EventRecorder.java (revision 0) +++ C:/Dev/apache.org/rw/xmlgraphics-commons-trunk/src/java/org/apache/xmlgraphics/ps/dsc/EventRecorder.java (revision 0) @@ -0,0 +1,120 @@ +/* + * 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.xmlgraphics.ps.dsc; + +import java.io.IOException; +import java.util.Iterator; +import java.util.List; + +import org.apache.xmlgraphics.ps.dsc.events.DSCComment; + +/** + * DSCHandler implementation that records DSC events. + */ +public class EventRecorder implements DSCHandler { + + private List events = new java.util.ArrayList(); + + /** + * Replays the recorded events to a specified DSCHandler instance. + * @param handler the DSCHandler to send the recorded events to + * @throws IOException In case of an I/O error + */ + public void replay(DSCHandler handler) throws IOException { + Iterator iter = events.iterator(); + while (iter.hasNext()) { + Object obj = iter.next(); + if (obj instanceof PSLine) { + handler.line(((PSLine)obj).getLine()); + } else if (obj instanceof PSComment) { + handler.comment(((PSComment)obj).getComment()); + } else if (obj instanceof DSCComment) { + handler.handleDSCComment((DSCComment)obj); + } else { + throw new IllegalStateException("Unsupported class type"); + } + } + } + + /** + * @see org.apache.xmlgraphics.ps.dsc.DSCHandler#comment(java.lang.String) + */ + public void comment(String comment) throws IOException { + events.add(new PSComment(comment)); + } + + /** + * @see org.apache.xmlgraphics.ps.dsc.DSCHandler#handleDSCComment( + * org.apache.xmlgraphics.ps.dsc.events.DSCComment) + */ + public void handleDSCComment(DSCComment comment) throws IOException { + events.add(comment); + } + + /** + * @see org.apache.xmlgraphics.ps.dsc.DSCHandler#line(java.lang.String) + */ + public void line(String line) throws IOException { + events.add(new PSLine(line)); + } + + /** + * @see org.apache.xmlgraphics.ps.dsc.DSCHandler#startDocument(java.lang.String) + */ + public void startDocument(String header) throws IOException { + throw new UnsupportedOperationException( + getClass().getName() + " is only used to handle parts of a document"); + } + + /** + * @see org.apache.xmlgraphics.ps.dsc.DSCHandler#endDocument() + */ + public void endDocument() throws IOException { + throw new UnsupportedOperationException( + getClass().getName() + " is only used to handle parts of a document"); + } + + private static class PSComment { + + private String comment; + + public PSComment(String comment) { + this.comment = comment; + } + + public String getComment() { + return this.comment; + } + } + + private static class PSLine { + + private String line; + + public PSLine(String line) { + this.line = line; + } + + public String getLine() { + return this.line; + } + } + +} Property changes on: C:\Dev\apache.org\rw\xmlgraphics-commons-trunk\src\java\org\apache\xmlgraphics\ps\dsc\EventRecorder.java ___________________________________________________________________ Name: svn:eol-style + native Index: C:/Dev/apache.org/rw/xmlgraphics-commons-trunk/src/java/org/apache/xmlgraphics/ps/dsc/DSCHandler.java =================================================================== --- C:/Dev/apache.org/rw/xmlgraphics-commons-trunk/src/java/org/apache/xmlgraphics/ps/dsc/DSCHandler.java (revision 0) +++ C:/Dev/apache.org/rw/xmlgraphics-commons-trunk/src/java/org/apache/xmlgraphics/ps/dsc/DSCHandler.java (revision 0) @@ -0,0 +1,68 @@ +/* + * 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.xmlgraphics.ps.dsc; + +import java.io.IOException; + +import org.apache.xmlgraphics.ps.dsc.events.DSCComment; + +/** + * Interface containing events generated by the DSCParser. Applications can implement this + * interface to react to certain events. + */ +public interface DSCHandler { + + /** + * Called as a new PostScript file starts. + * @param header the first line of the DSC-compliant file + * @throws IOException In case of an I/O error + */ + public void startDocument(String header) throws IOException; + + /** + * Called when the PostScript file is fully processed, i.e. after the %%EOF comment. + * @throws IOException In case of an I/O error + */ + public void endDocument() throws IOException; + + /** + * Called for each standard DSC comment. The classes passed to this method may be simple + * DSCComment classes or special subclasses for some of the DSC comments. + * @param comment the DSC comment + * @throws IOException In case of an I/O error + */ + public void handleDSCComment(DSCComment comment) throws IOException; + + /** + * Called for a normal line of PostScript code. + * @param line the line of code + * @throws IOException In case of an I/O error + */ + public void line(String line) throws IOException; + + /** + * Called for any line containing a full-line PostScript comment. This is also called for + * custom comments following the extension mechanism of the DSC specification. + * @param comment the comment line + * @throws IOException In case of an I/O error + */ + public void comment(String comment) throws IOException; + +} Property changes on: C:\Dev\apache.org\rw\xmlgraphics-commons-trunk\src\java\org\apache\xmlgraphics\ps\dsc\DSCHandler.java ___________________________________________________________________ Name: svn:eol-style + native Index: C:/Dev/apache.org/rw/xmlgraphics-commons-trunk/src/java/org/apache/xmlgraphics/ps/dsc/DSCException.java =================================================================== --- C:/Dev/apache.org/rw/xmlgraphics-commons-trunk/src/java/org/apache/xmlgraphics/ps/dsc/DSCException.java (revision 0) +++ C:/Dev/apache.org/rw/xmlgraphics-commons-trunk/src/java/org/apache/xmlgraphics/ps/dsc/DSCException.java (revision 0) @@ -0,0 +1,37 @@ +/* + * 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.xmlgraphics.ps.dsc; + +/** + * Exception to signal problems while parsing a supposedly DSC-conformant file. + */ +public class DSCException extends Exception { + + private static final long serialVersionUID = -1549406547978409615L; + + /** + * Creates a new DSCException. + * @param msg the exception message + */ + public DSCException(String msg) { + super(msg); + } + +} Property changes on: C:\Dev\apache.org\rw\xmlgraphics-commons-trunk\src\java\org\apache\xmlgraphics\ps\dsc\DSCException.java ___________________________________________________________________ Name: svn:eol-style + native Index: C:/Dev/apache.org/rw/xmlgraphics-commons-trunk/src/java/org/apache/xmlgraphics/ps/dsc/DefaultDSCHandler.java =================================================================== --- C:/Dev/apache.org/rw/xmlgraphics-commons-trunk/src/java/org/apache/xmlgraphics/ps/dsc/DefaultDSCHandler.java (revision 0) +++ C:/Dev/apache.org/rw/xmlgraphics-commons-trunk/src/java/org/apache/xmlgraphics/ps/dsc/DefaultDSCHandler.java (revision 0) @@ -0,0 +1,77 @@ +/* + * 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.xmlgraphics.ps.dsc; + +import java.io.IOException; +import java.io.OutputStream; + +import org.apache.xmlgraphics.ps.DSCConstants; +import org.apache.xmlgraphics.ps.PSGenerator; +import org.apache.xmlgraphics.ps.dsc.events.DSCComment; + +/** + * Default implementation of a DSCHandler which simply passes through the PostScript content + * unchanged. Subclasses can implement different behaviour, for example to filter certain + * DSC comments or to insert PostScript code at specific places. + */ +public class DefaultDSCHandler implements DSCHandler { + + protected OutputStream out; + protected PSGenerator gen; + + /** + * Creates a new instance. + * @param out OutputStream to pipe all received events to + */ + public DefaultDSCHandler(OutputStream out) { + this.out = out; + this.gen = new PSGenerator(this.out); + } + + /** @see org.apache.xmlgraphics.ps.dsc.DSCHandler#startDocument(java.lang.String) */ + public void startDocument(String header) throws IOException { + gen.writeln(header); + } + + /** @see org.apache.xmlgraphics.ps.dsc.DSCHandler#endDocument() */ + public void endDocument() throws IOException { + gen.writeDSCComment(DSCConstants.EOF); + } + + /** + * @see org.apache.xmlgraphics.ps.dsc.DSCHandler#handleDSCComment( + * org.apache.xmlgraphics.ps.dsc.events.DSCComment) + */ + public void handleDSCComment(DSCComment comment) throws IOException { + comment.generate(gen); + + } + + /** @see org.apache.xmlgraphics.ps.dsc.DSCHandler#line(java.lang.String) */ + public void line(String line) throws IOException { + gen.writeln(line); + } + + /** @see org.apache.xmlgraphics.ps.dsc.DSCHandler#comment(java.lang.String) */ + public void comment(String comment) throws IOException { + gen.commentln("%" + comment); + } + +} Property changes on: C:\Dev\apache.org\rw\xmlgraphics-commons-trunk\src\java\org\apache\xmlgraphics\ps\dsc\DefaultDSCHandler.java ___________________________________________________________________ Name: svn:eol-style + native Index: C:/Dev/apache.org/rw/xmlgraphics-commons-trunk/src/java/org/apache/xmlgraphics/ps/dsc/package.html =================================================================== --- C:/Dev/apache.org/rw/xmlgraphics-commons-trunk/src/java/org/apache/xmlgraphics/ps/dsc/package.html (revision 0) +++ C:/Dev/apache.org/rw/xmlgraphics-commons-trunk/src/java/org/apache/xmlgraphics/ps/dsc/package.html (revision 0) @@ -0,0 +1,23 @@ + + + +org.apache.xmlgraphics.ps.dsc Package + +

Tools for DSC-compliant PostScript files (DSC = Document Structuring Conventions).

+ + \ No newline at end of file Property changes on: C:\Dev\apache.org\rw\xmlgraphics-commons-trunk\src\java\org\apache\xmlgraphics\ps\dsc\package.html ___________________________________________________________________ Name: svn:eol-style + native Index: C:/Dev/apache.org/rw/xmlgraphics-commons-trunk/src/java/org/apache/xmlgraphics/ps/PSResource.java =================================================================== --- C:/Dev/apache.org/rw/xmlgraphics-commons-trunk/src/java/org/apache/xmlgraphics/ps/PSResource.java (revision 500235) +++ C:/Dev/apache.org/rw/xmlgraphics-commons-trunk/src/java/org/apache/xmlgraphics/ps/PSResource.java (working copy) @@ -30,6 +30,12 @@ public static final String TYPE_FONT = "font"; /** a procset resource */ public static final String TYPE_PROCSET = "procset"; + /** a procset resource */ + public static final String TYPE_PATTERN = "pattern"; + /** a procset resource */ + public static final String TYPE_FORM = "form"; + /** a procset resource */ + public static final String TYPE_ENCODING = "encoding"; private String type; private String name; @@ -61,4 +67,26 @@ return sb.toString(); } + + /** @see java.lang.Object#equals(java.lang.Object) */ + public boolean equals(Object obj) { + if (obj == this) { + return true; + } else if (obj instanceof PSResource) { + PSResource other = (PSResource)obj; + return other.toString().equals(toString()); + } else { + return false; + } + } + + /** @see java.lang.Object#hashCode() */ + public int hashCode() { + return toString().hashCode(); + } + + /** @see java.lang.Object#toString() */ + public String toString() { + return getResourceSpecification(); + } } Index: C:/Dev/apache.org/rw/xmlgraphics-commons-trunk/src/java/org/apache/xmlgraphics/ps/PSFontUtils.java =================================================================== --- C:/Dev/apache.org/rw/xmlgraphics-commons-trunk/src/java/org/apache/xmlgraphics/ps/PSFontUtils.java (revision 500235) +++ C:/Dev/apache.org/rw/xmlgraphics-commons-trunk/src/java/org/apache/xmlgraphics/ps/PSFontUtils.java (working copy) @@ -112,5 +112,23 @@ gen.writeln("] def"); } + /** + * Redefines the encoding of a font. + * @param gen the PostScript generator + * @param fontName the font name + * @param encoding the new encoding (must be predefined in the PS file) + * @throws IOException In case of an I/O problem + */ + public static void redefineFontEncoding(PSGenerator gen, String fontName, String encoding) + throws IOException { + gen.writeln("/" + fontName + " findfont"); + gen.writeln("dup length dict begin"); + gen.writeln(" {1 index /FID ne {def} {pop pop} ifelse} forall"); + gen.writeln(" /Encoding " + encoding + " def"); + gen.writeln(" currentdict"); + gen.writeln("end"); + gen.writeln("/" + fontName + " exch definefont pop"); + } + } Index: C:/Dev/apache.org/rw/xmlgraphics-commons-trunk/src/java/org/apache/xmlgraphics/java2d/ps/PSDocumentGraphics2D.java =================================================================== --- C:/Dev/apache.org/rw/xmlgraphics-commons-trunk/src/java/org/apache/xmlgraphics/java2d/ps/PSDocumentGraphics2D.java (revision 500235) +++ C:/Dev/apache.org/rw/xmlgraphics-commons-trunk/src/java/org/apache/xmlgraphics/java2d/ps/PSDocumentGraphics2D.java (working copy) @@ -24,7 +24,6 @@ import java.io.IOException; import org.apache.xmlgraphics.ps.DSCConstants; -import org.apache.xmlgraphics.ps.PSGenerator; import org.apache.xmlgraphics.ps.PSProcSets; /** @@ -88,7 +87,7 @@ + ": PostScript Generator for Java2D"}); gen.writeDSCComment(DSCConstants.CREATION_DATE, new Object[] {new java.util.Date()}); - gen.writeDSCComment(DSCConstants.PAGES, PSGenerator.ATEND); + gen.writeDSCComment(DSCConstants.PAGES, DSCConstants.ATEND); gen.writeDSCComment(DSCConstants.BBOX, new Object[] {ZERO, ZERO, pagewidth, pageheight}); gen.writeDSCComment(DSCConstants.END_COMMENTS); @@ -103,8 +102,8 @@ //Setup gen.writeDSCComment(DSCConstants.BEGIN_SETUP); - PSProcSets.writeFOPStdProcSet(gen); - PSProcSets.writeFOPEPSProcSet(gen); + PSProcSets.writeStdProcSet(gen); + PSProcSets.writeEPSProcSet(gen); if (customTextHandler != null) { customTextHandler.writeSetup(); } @@ -129,8 +128,6 @@ protected void writePageTrailer() throws IOException { gen.writeln("showpage"); - gen.writeDSCComment(DSCConstants.PAGE_TRAILER); - gen.writeDSCComment(DSCConstants.END_PAGE); } /** Index: C:/Dev/apache.org/rw/xmlgraphics-commons-trunk/src/java/org/apache/xmlgraphics/java2d/ps/EPSDocumentGraphics2D.java =================================================================== --- C:/Dev/apache.org/rw/xmlgraphics-commons-trunk/src/java/org/apache/xmlgraphics/java2d/ps/EPSDocumentGraphics2D.java (revision 500235) +++ C:/Dev/apache.org/rw/xmlgraphics-commons-trunk/src/java/org/apache/xmlgraphics/java2d/ps/EPSDocumentGraphics2D.java (working copy) @@ -60,7 +60,7 @@ + ": EPS Generator for Java2D"}); gen.writeDSCComment(DSCConstants.CREATION_DATE, new Object[] {new java.util.Date()}); - gen.writeDSCComment(DSCConstants.PAGES, new Integer(0)); + gen.writeDSCComment(DSCConstants.PAGES, DSCConstants.ATEND); gen.writeDSCComment(DSCConstants.BBOX, new Object[] {ZERO, ZERO, pagewidth, pageheight}); gen.writeDSCComment(DSCConstants.LANGUAGE_LEVEL, new Integer(gen.getPSLevel())); @@ -68,8 +68,8 @@ //Prolog gen.writeDSCComment(DSCConstants.BEGIN_PROLOG); - PSProcSets.writeFOPStdProcSet(gen); - PSProcSets.writeFOPEPSProcSet(gen); + PSProcSets.writeStdProcSet(gen); + PSProcSets.writeEPSProcSet(gen); if (customTextHandler != null) { customTextHandler.writeSetup(); } @@ -89,8 +89,7 @@ } protected void writePageTrailer() throws IOException { - gen.writeDSCComment(DSCConstants.PAGE_TRAILER); - gen.writeDSCComment(DSCConstants.END_PAGE); + //nop, no DSC PageTrailer needed } } Index: C:/Dev/apache.org/rw/xmlgraphics-commons-trunk/src/documentation/content/xdocs/site.xml =================================================================== --- C:/Dev/apache.org/rw/xmlgraphics-commons-trunk/src/documentation/content/xdocs/site.xml (revision 500235) +++ C:/Dev/apache.org/rw/xmlgraphics-commons-trunk/src/documentation/content/xdocs/site.xml (working copy) @@ -35,6 +35,9 @@ + + + Index: C:/Dev/apache.org/rw/xmlgraphics-commons-trunk/src/documentation/content/xdocs/index.xml =================================================================== --- C:/Dev/apache.org/rw/xmlgraphics-commons-trunk/src/documentation/content/xdocs/index.xml (revision 500235) +++ C:/Dev/apache.org/rw/xmlgraphics-commons-trunk/src/documentation/content/xdocs/index.xml (working copy) @@ -81,6 +81,13 @@ FOP + Parser/Processor for DSC-compliant PostScript files (DSC = + Document Structuring Conventions) + + org.apache.xmlgraphics.ps.dsc + new + + XMP metadata framework org.apache.xmlgraphics.xmp new Index: C:/Dev/apache.org/rw/xmlgraphics-commons-trunk/src/documentation/content/xdocs/postscript.xml =================================================================== --- C:/Dev/apache.org/rw/xmlgraphics-commons-trunk/src/documentation/content/xdocs/postscript.xml (revision 0) +++ C:/Dev/apache.org/rw/xmlgraphics-commons-trunk/src/documentation/content/xdocs/postscript.xml (revision 0) @@ -0,0 +1,142 @@ + + + + +
+ Tools for Adobe PostScript +
+ +
+ Overview +

+ Apache XML Graphics Commons contains various tools for writing and processing Adobe + PostScript files. This includes: +

+
    +
  • A PostScript generator class which helps writing PostScript files from scratch.
  • +
  • Two Graphics2D implementations, one for plain PostScript and one for writing + Encapsulated PostScript (EPS).
  • +
  • A DSC-parser/processor: Parse, post-process and change DSC-compliant PostScript files.
  • +
+ + We don't currently include a PostScript interpreter though we would love to have one. A + Java-based PostScript interpreter to keep an eye on is the one from the + FOray project. + +
+
+ The PostScript generator +

+ The "PSGenerator" class can help writing PostScript files. It deals with things like + escaping, saving/tracking/restoring graphics state, writing DSC comments and tracking of + DSC resources. +

+

+ You will rarely interact with the PS generator itself, as it is probably more interesting + to generate a PostScript file using Java2D which is described in the following section. +

+
+
+ Java2D: Graphics2D implementation for generating PostScript and EPS +

+ We provide two classes (PSDocumentGraphics2D and EPSDocumentGraphics2D) which you can use + to generated complete PostScript files using normal Java2D means. The difference between + the two classes is that the EPS variant creates a fully compliant Encapsulated + PostScript file while the PS variant simply creates a normal DSC-compliant level 2 + PostScript file. It depends on your requirement which variant you choose. The PS variant + is mostly for printing purposes while the EPS variant is better suited for inclusion in + other documents. +

+
+ Creating an EPS file +

+ Creating an EPS file using the Graphics2D implementation is easy. Instantiate + EPSDocumentGraphics2D, set a GraphicContext and set up the output document. Here's an + example: +

+ +

+ A complete example for generating an EPS files can be found in the + "examples" directory + in the distribution. +

+
+
+
+ DSC parser/processor +

+ Many PostScript files use special comments to structure a document. This allows manipulation + of PostScript files without interpreting them. These special comments are defined in the + Document Structuring Conventions. + The code in Commons is designed to work with DSC 3.0. For details on how DSC is used, + please take a look at the DSC specification. +

+

+ The DSC support in Commons was primarily developed to implement resource optimization + features in Apache FOP's PostScript output support. Resources like + images which are used repeatedly in a document should not be written to the PostScript + file each time it is used. Instead it is written once at the beginning of the file as a + PostScript form. The form is then called whenever the image needs painting. +

+

+ But the DSC parser could potentially be used for other purposes. The most obvious is + extracting a subset of pages from a DSC-compliant file. Assume you want to print only + page 45 to 57 of a particular document. There's an example that demonstrates exactly this. + Check out the "examples" directory in the distribution. Other potential use cases for the + DSC parser are: +

+
    +
  • Patching PostScript files, for example, adding OMR marks for automatic packaging
  • +
  • Imposition (2-up, n-up, rotation, etc.)
  • +
  • EPS graphic extraction
  • +
  • Inspecting the page count
  • +
  • etc. etc.
  • +
+

+ The DSC parser (DSCParser) was designed as a pull parser, i.e. you fetch new events from + the parser inspecting them and acting on them as they are found. If you prefer to work + with a push parser, you can pass the DSCParser a DSCHandler implementation and the parser + will send you all the events. +

+

+ The best example to understand how to use the DSC parser is the PageExtractor class + that implements the page extraction functionality mentioned above. +

+ + The DSC parser is not considered feature-complete. The basic infrastructure is there but, + for example, not all DSC comments are available as concrete Java classes. If you need + to extend the DSC parser for your own use cases, please send us your patches. + +
+ +
Property changes on: C:\Dev\apache.org\rw\xmlgraphics-commons-trunk\src\documentation\content\xdocs\postscript.xml ___________________________________________________________________ Name: svn:keywords + Id Name: svn:eol-style + native Index: C:/Dev/apache.org/rw/xmlgraphics-commons-trunk/examples/java/ps/DSCProcessingExample1.java =================================================================== --- C:/Dev/apache.org/rw/xmlgraphics-commons-trunk/examples/java/ps/DSCProcessingExample1.java (revision 0) +++ C:/Dev/apache.org/rw/xmlgraphics-commons-trunk/examples/java/ps/DSCProcessingExample1.java (revision 0) @@ -0,0 +1,89 @@ +/* + * 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 ps; + +import java.io.File; +import java.io.IOException; +import java.io.OutputStream; +import java.io.InputStream; + +import org.apache.commons.io.IOUtils; +import org.apache.xmlgraphics.ps.dsc.DSCException; +import org.apache.xmlgraphics.ps.dsc.tools.PageExtractor; + +public class DSCProcessingExample1 { + + public void extractPages(File srcFile, File tgtFile, int from, int to) throws IOException { + InputStream in = new java.io.FileInputStream(srcFile); + in = new java.io.BufferedInputStream(in); + try { + OutputStream out = new java.io.FileOutputStream(tgtFile); + out = new java.io.BufferedOutputStream(out); + try { + PageExtractor.extractPages(in, out, from, to); + } catch (DSCException e) { + throw new RuntimeException(e.getMessage()); + } finally { + IOUtils.closeQuietly(out); + } + } finally { + IOUtils.closeQuietly(in); + } + + } + + private static void showInfo() { + System.out.println( + "Call: DSCProcessingExample1 "); + } + + /** + * Command-line interface + * @param args command-line arguments + */ + public static void main(String[] args) { + try { + File srcFile , tgtFile; + int from, to; + if (args.length >= 4) { + srcFile = new File(args[0]); + tgtFile = new File(args[1]); + from = Integer.parseInt(args[2]); + to = Integer.parseInt(args[3]); + } else { + throw new IllegalArgumentException("Invalid number of parameters!"); + } + if (!srcFile.exists()) { + throw new IllegalArgumentException("Source file not found: " + srcFile); + } + DSCProcessingExample1 app = new DSCProcessingExample1(); + app.extractPages(srcFile, tgtFile, from, to); + System.out.println("File written: " + tgtFile.getCanonicalPath()); + } catch (IllegalArgumentException iae) { + System.err.println(iae.getMessage()); + showInfo(); + System.exit(1); + } catch (Exception e) { + e.printStackTrace(); + System.exit(-1); + } + } + +} Property changes on: C:\Dev\apache.org\rw\xmlgraphics-commons-trunk\examples\java\ps\DSCProcessingExample1.java ___________________________________________________________________ Name: svn:eol-style + native Index: C:/Dev/apache.org/rw/xmlgraphics-commons-trunk/examples/java/java2d/ps/EPSExample1.java =================================================================== --- C:/Dev/apache.org/rw/xmlgraphics-commons-trunk/examples/java/java2d/ps/EPSExample1.java (revision 500235) +++ C:/Dev/apache.org/rw/xmlgraphics-commons-trunk/examples/java/java2d/ps/EPSExample1.java (working copy) @@ -22,9 +22,11 @@ import java.awt.Color; import java.awt.Font; import java.awt.Graphics2D; +import java.awt.font.TextAttribute; import java.io.File; import java.io.IOException; import java.io.OutputStream; +import java.text.AttributedString; import org.apache.commons.io.IOUtils; import org.apache.xmlgraphics.java2d.ps.EPSDocumentGraphics2D; @@ -36,12 +38,54 @@ public class EPSExample1 { /** + * Paints a few things on a Graphics2D instance. + * @param g2d the Graphics2D instance + * @param pageNum a page number + */ + public void paintSome(Graphics2D g2d, int pageNum) { + //Paint a bounding box + g2d.drawRect(0, 0, 400, 200); + + //A few rectangles rotated and with different color + Graphics2D copy = (Graphics2D)g2d.create(); + int c = 12; + for (int i = 0; i < c; i++) { + float f = ((i + 1) / (float)c); + Color col = new Color(0.0f, 1 - f, 0.0f); + copy.setColor(col); + copy.fillRect(70, 90, 50, 50); + copy.rotate(-2 * Math.PI / (double)c, 70, 90); + } + copy.dispose(); + + //Some text + copy = (Graphics2D)g2d.create(); + copy.rotate(-0.25); + copy.setColor(Color.RED); + copy.setFont(new Font("sans-serif", Font.PLAIN, 36)); + copy.drawString("Hello world!", 140, 140); + copy.setColor(Color.RED.darker()); + copy.setFont(new Font("serif", Font.PLAIN, 36)); + copy.drawString("Hello world!", 140, 180); + copy.dispose(); + + //Try attributed text + AttributedString aString = new AttributedString("This is attributed text."); + aString.addAttribute(TextAttribute.FAMILY, "SansSerif"); + aString.addAttribute(TextAttribute.FAMILY, "Serif", 8, 18); + aString.addAttribute(TextAttribute.FOREGROUND, Color.orange, 8, 18); + g2d.drawString(aString.getIterator(), 250, 170); + + g2d.drawString("Page: " + pageNum, 250, 190); + } + + /** * Creates an EPS file. The contents are painted using a Graphics2D implementation that * generates an EPS file. * @param outputFile the target file * @throws IOException In case of an I/O error */ - public static void generateEPSusingJava2D(File outputFile) throws IOException { + public void generateEPSusingJava2D(File outputFile) throws IOException { OutputStream out = new java.io.FileOutputStream(outputFile); out = new java.io.BufferedOutputStream(out); try { @@ -52,37 +96,16 @@ //Set up the document size g2d.setupDocument(out, 400, 200); //400pt x 200pt - //Paint a bounding box - g2d.drawRect(0, 0, 400, 200); + //Paint something + paintSome(g2d, 1); - //A few rectangles rotated and with different color - Graphics2D copy = (Graphics2D)g2d.create(); - int c = 12; - for (int i = 0; i < c; i++) { - float f = ((i + 1) / (float)c); - Color col = new Color(0.0f, 1 - f, 0.0f); - copy.setColor(col); - copy.fillRect(70, 90, 50, 50); - copy.rotate(-2 * Math.PI / (double)c, 70, 90); - } - copy.dispose(); - - //Some text - g2d.rotate(-0.25); - g2d.setColor(Color.RED); - g2d.setFont(new Font("sans-serif", Font.PLAIN, 36)); - g2d.drawString("Hello world!", 140, 140); - g2d.setColor(Color.RED.darker()); - g2d.setFont(new Font("serif", Font.PLAIN, 36)); - g2d.drawString("Hello world!", 140, 180); - //Cleanup g2d.finish(); } finally { IOUtils.closeQuietly(out); } } - + /** * Command-line interface * @param args command-line arguments @@ -98,7 +121,10 @@ if (!targetDir.exists()) { System.err.println("Target Directory does not exist: " + targetDir); } - generateEPSusingJava2D(new File(targetDir, "eps-example1.eps")); + File outputFile = new File(targetDir, "eps-example1.eps"); + EPSExample1 app = new EPSExample1(); + app.generateEPSusingJava2D(outputFile); + System.out.println("File written: " + outputFile.getCanonicalPath()); } catch (Exception e) { e.printStackTrace(); }