From 016ddc8aa591fde0adcaef76158f3f5ebcf6590a Mon Sep 17 00:00:00 2001 From: Adrian Cumiskey Date: Mon, 17 Nov 2008 15:26:06 +0000 Subject: [PATCH] Memory usage optimizations. git-svn-id: https://svn.apache.org/repos/asf/xmlgraphics/fop/branches/Temp_AFPGOCAResources@718262 13f79535-47bb-0310-9956-ffa450edef68 --- .../org/apache/fop/afp/AFPGraphics2D.java | 71 ++-- .../org/apache/fop/afp/fonts/RasterFont.java | 2 +- .../fop/afp/goca/AbstractGraphicsCoord.java | 54 +-- .../AbstractGraphicsObjectContainer.java} | 31 +- .../fop/afp/goca/AbstractGraphicsString.java | 74 ++++ .../org/apache/fop/afp/goca/GraphicsArea.java | 8 +- .../org/apache/fop/afp/goca/GraphicsBox.java | 24 +- .../fop/afp/goca/GraphicsChainedSegment.java | 14 +- .../org/apache/fop/afp/goca/GraphicsData.java | 29 +- .../apache/fop/afp/goca/GraphicsFillet.java | 3 +- .../fop/afp/goca/GraphicsFilletRelative.java | 4 +- .../apache/fop/afp/goca/GraphicsFullArc.java | 31 +- .../apache/fop/afp/goca/GraphicsImage.java | 4 +- .../org/apache/fop/afp/goca/GraphicsLine.java | 4 +- .../fop/afp/goca/GraphicsLineRelative.java | 2 +- .../fop/afp/goca/GraphicsSetCharacterSet.java | 21 +- .../fop/afp/goca/GraphicsSetLineType.java | 24 +- .../fop/afp/goca/GraphicsSetLineWidth.java | 20 +- .../apache/fop/afp/goca/GraphicsSetMix.java | 13 +- .../afp/goca/GraphicsSetPatternSymbol.java | 21 +- .../fop/afp/goca/GraphicsSetProcessColor.java | 45 ++- .../apache/fop/afp/goca/GraphicsString.java | 93 ++--- .../fop/afp/goca/GraphicsStringRelative.java | 57 +++ .../org/apache/fop/afp/ioca/ImageContent.java | 4 +- .../fop/afp/modca/AbstractAFPObject.java | 77 +++++ .../fop/afp/modca/AbstractDescriptor.java | 2 +- .../fop/afp/modca/AbstractNamedAFPObject.java | 2 +- .../afp/modca/AbstractPreparedAFPObject.java | 96 ------ .../modca/AbstractStructuredAFPObject.java | 324 ------------------ .../afp/modca/AbstractStructuredObject.java | 69 ++++ .../AbstractTripletStructuredObject.java | 178 ++++++++++ .../apache/fop/afp/modca/GraphicsObject.java | 32 +- .../apache/fop/afp/modca/IncludeObject.java | 4 +- .../apache/fop/afp/modca/MapCodedFont.java | 2 +- .../fop/afp/modca/MapContainerData.java | 6 +- .../apache/fop/afp/modca/MapDataResource.java | 4 +- .../fop/afp/modca/ObjectAreaDescriptor.java | 44 +-- .../modca/PreprocessPresentationObject.java | 42 +-- .../modca/PresentationEnvironmentControl.java | 79 ++--- .../org/apache/fop/afp/modca/Registry.java | 38 +- .../apache/fop/afp/modca/ResourceObject.java | 106 +++--- ...PObject.java => StructuredDataObject.java} | 9 +- .../{Triplet.java => AbstractTriplet.java} | 66 +--- .../afp/modca/triplets/CommentTriplet.java | 52 +++ .../triplets/DescriptorPositionTriplet.java | 50 +++ ...xtendedResourceLocalIdentifierTriplet.java | 34 +- .../triplets/FullyQualifiedNameTriplet.java | 137 ++++---- .../modca/triplets/MappingOptionTriplet.java | 39 ++- .../triplets/MeasurementUnitsTriplet.java | 50 ++- .../modca/triplets/ObjectAreaSizeTriplet.java | 58 +++- .../triplets/ObjectByteExtentTriplet.java | 23 +- .../triplets/ObjectClassificationTriplet.java | 136 +++++--- .../PresentationSpaceMixingRulesTriplet.java | 44 ++- .../PresentationSpaceResetMixingTriplet.java | 32 +- .../triplets/ResourceObjectTypeTriplet.java | 37 +- .../{ => svg}/AFPGraphicsConfiguration.java | 23 +- .../fop/afp/{ => svg}/AFPGraphicsDevice.java | 7 +- .../org/apache/fop/svg/PDFGraphics2D.java | 1 - 58 files changed, 1443 insertions(+), 1113 deletions(-) rename src/java/org/apache/fop/afp/{modca/AbstractPreparedObjectContainer.java => goca/AbstractGraphicsObjectContainer.java} (69%) create mode 100644 src/java/org/apache/fop/afp/goca/AbstractGraphicsString.java create mode 100644 src/java/org/apache/fop/afp/goca/GraphicsStringRelative.java delete mode 100644 src/java/org/apache/fop/afp/modca/AbstractPreparedAFPObject.java delete mode 100644 src/java/org/apache/fop/afp/modca/AbstractStructuredAFPObject.java create mode 100644 src/java/org/apache/fop/afp/modca/AbstractStructuredObject.java create mode 100644 src/java/org/apache/fop/afp/modca/AbstractTripletStructuredObject.java rename src/java/org/apache/fop/afp/modca/{PreparedAFPObject.java => StructuredDataObject.java} (82%) rename src/java/org/apache/fop/afp/modca/triplets/{Triplet.java => AbstractTriplet.java} (74%) create mode 100644 src/java/org/apache/fop/afp/modca/triplets/CommentTriplet.java create mode 100644 src/java/org/apache/fop/afp/modca/triplets/DescriptorPositionTriplet.java rename src/java/org/apache/fop/afp/{ => svg}/AFPGraphicsConfiguration.java (92%) rename src/java/org/apache/fop/afp/{ => svg}/AFPGraphicsDevice.java (98%) diff --git a/src/java/org/apache/fop/afp/AFPGraphics2D.java b/src/java/org/apache/fop/afp/AFPGraphics2D.java index df233dafa..2494eba69 100644 --- a/src/java/org/apache/fop/afp/AFPGraphics2D.java +++ b/src/java/org/apache/fop/afp/AFPGraphics2D.java @@ -47,6 +47,7 @@ import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.apache.fop.afp.goca.GraphicsSetLineType; import org.apache.fop.afp.modca.GraphicsObject; +import org.apache.fop.afp.svg.AFPGraphicsConfiguration; import org.apache.fop.fonts.FontInfo; import org.apache.fop.svg.NativeImageHandler; import org.apache.xmlgraphics.image.loader.ImageInfo; @@ -101,6 +102,9 @@ public class AFPGraphics2D extends AbstractGraphics2D implements NativeImageHand /** Current AFP state */ private AFPPaintingState paintingState = null; + /** AFP graphics configuration */ + private final AFPGraphicsConfiguration graphicsConfig = new AFPGraphicsConfiguration(); + /** The AFP FontInfo */ private FontInfo fontInfo; @@ -246,8 +250,8 @@ public class AFPGraphics2D extends AbstractGraphics2D implements NativeImageHand if (fill) { graphicsObj.beginArea(); } - AffineTransform trans = super.getTransform(); + AffineTransform trans = gc.getTransform(); PathIterator iter = shape.getPathIterator(trans); double[] dstPts = new double[6]; int[] coords = null; @@ -294,44 +298,39 @@ public class AFPGraphics2D extends AbstractGraphics2D implements NativeImageHand mhr ); } else { - for (int[] openingCoords = new int[2], currCoords = new int[2]; - !iter.isDone(); iter.next()) { + for (int[] openingCoords = new int[2]; !iter.isDone(); iter.next()) { int type = iter.currentSegment(dstPts); - if (type == PathIterator.SEG_MOVETO) { - openingCoords[X] = currCoords[X] = (int)Math.round(dstPts[X]); - openingCoords[Y] = currCoords[Y] = (int)Math.round(dstPts[Y]); - graphicsObj.setCurrentPosition(openingCoords); + int numCoords; + if (type == PathIterator.SEG_MOVETO || type == PathIterator.SEG_LINETO) { + numCoords = 2; + } else if (type == PathIterator.SEG_QUADTO) { + numCoords = 4; + } else if (type == PathIterator.SEG_CUBICTO) { + numCoords = 6; } else { - int numCoords; - if (type == PathIterator.SEG_LINETO) { - numCoords = 2; - } else if (type == PathIterator.SEG_QUADTO) { - numCoords = 4; - } else if (type == PathIterator.SEG_CUBICTO) { - numCoords = 6; + // close of the graphics segment + if (type == PathIterator.SEG_CLOSE) { + // close segment by drawing to opening position + graphicsObj.addLine(openingCoords, true); } else { - // close of the graphics segment - if (type == PathIterator.SEG_CLOSE) { - graphicsObj.addLine(openingCoords, true); - } else { - log.debug("Unrecognised path iterator type: " - + type); - } - continue; - } - coords = new int[numCoords]; - for (int i = 0; i < numCoords; i++) { - coords[i] = (int) Math.round(dstPts[i]); + log.debug("Unrecognised path iterator type: " + + type); } - if (type == PathIterator.SEG_LINETO) { - graphicsObj.addLine(coords, true); - } else if (type == PathIterator.SEG_QUADTO - || type == PathIterator.SEG_CUBICTO) { - graphicsObj.addFillet(coords, true); - } - // update current position coordinates - currCoords[X] = coords[coords.length - 2]; - currCoords[Y] = coords[coords.length - 1]; + continue; + } + coords = new int[numCoords]; + for (int i = 0; i < numCoords; i++) { + coords[i] = (int) Math.round(dstPts[i]); + } + if (type == PathIterator.SEG_MOVETO) { + graphicsObj.setCurrentPosition(coords); + openingCoords[X] = coords[X]; + openingCoords[Y] = coords[Y]; + } else if (type == PathIterator.SEG_LINETO) { + graphicsObj.addLine(coords, true); + } else if (type == PathIterator.SEG_QUADTO + || type == PathIterator.SEG_CUBICTO) { + graphicsObj.addFillet(coords, true); } } } @@ -379,7 +378,7 @@ public class AFPGraphics2D extends AbstractGraphics2D implements NativeImageHand /** {@inheritDoc} */ public GraphicsConfiguration getDeviceConfiguration() { - return new AFPGraphicsConfiguration(); + return graphicsConfig; } /** {@inheritDoc} */ diff --git a/src/java/org/apache/fop/afp/fonts/RasterFont.java b/src/java/org/apache/fop/afp/fonts/RasterFont.java index 7582159c5..c36027913 100644 --- a/src/java/org/apache/fop/afp/fonts/RasterFont.java +++ b/src/java/org/apache/fop/afp/fonts/RasterFont.java @@ -35,7 +35,7 @@ import org.apache.commons.logging.LogFactory; public class RasterFont extends AFPFont { /** Static logging instance */ - protected static final Log log = LogFactory.getLog("org.apache.xmlgraphics.afp.fonts"); + protected static final Log log = LogFactory.getLog("org.apache.fop.afp.fonts"); private final Map/**/ charSets = new java.util.HashMap/**/(); diff --git a/src/java/org/apache/fop/afp/goca/AbstractGraphicsCoord.java b/src/java/org/apache/fop/afp/goca/AbstractGraphicsCoord.java index 3adcd9466..066940874 100644 --- a/src/java/org/apache/fop/afp/goca/AbstractGraphicsCoord.java +++ b/src/java/org/apache/fop/afp/goca/AbstractGraphicsCoord.java @@ -19,13 +19,18 @@ package org.apache.fop.afp.goca; -import org.apache.fop.afp.modca.AbstractPreparedAFPObject; +import java.io.IOException; +import java.io.OutputStream; + +import org.apache.fop.afp.modca.AbstractNamedAFPObject; +import org.apache.fop.afp.modca.StructuredDataObject; import org.apache.fop.afp.util.BinaryUtils; /** * A base class encapsulating the structure of coordinate based GOCA objects */ -public abstract class AbstractGraphicsCoord extends AbstractPreparedAFPObject { +public abstract class AbstractGraphicsCoord extends AbstractNamedAFPObject + implements StructuredDataObject { /** array of x/y coordinates */ protected int[] coords = null; @@ -37,7 +42,6 @@ public abstract class AbstractGraphicsCoord extends AbstractPreparedAFPObject { */ public AbstractGraphicsCoord(int[] coords) { this.coords = coords; - prepareData(); } /** @@ -62,40 +66,48 @@ public abstract class AbstractGraphicsCoord extends AbstractPreparedAFPObject { this(new int[] {x1, y1, x2, y2}); } + /** {@inheritDoc} */ + public int getDataLength() { + return 2 + (coords.length * 2); + } + /** - * Returns the order code to use + * Returns the order code of this structured field * - * @return the order code to use + * @return the order code of this structured field */ - protected abstract byte getOrderCode(); + abstract byte getOrderCode(); /** - * Returns the length of this order code (typically this is the same as the coordinate length) + * Returns the coordinate data start index * - * @return the length of this order code + * @return the coordinate data start index */ - protected int getLength() { - return this.coords.length * 2; + int getCoordinateDataStartIndex() { + return 2; } /** - * Creates a newly created and initialized byte data + * Returns the coordinate data * - * @return a newly created and initialized byte data + * @return the coordinate data */ - protected byte[] createData() { - int len = getLength(); - byte[] data = new byte[len + 2]; - data[0] = getOrderCode(); // ORDER CODE - data[1] = (byte)len; // LENGTH + byte[] getData() { + int len = getDataLength(); + byte[] data = new byte[len]; + data[0] = getOrderCode(); + data[1] = (byte)(len - 2); + + if (coords != null) { + addCoords(data, getCoordinateDataStartIndex()); + } + return data; } /** {@inheritDoc} */ - protected void prepareData() { - super.data = createData(); - int fromIndex = data.length - getLength(); - addCoords(data, fromIndex); + public void writeToStream(OutputStream os) throws IOException { + os.write(getData()); } /** diff --git a/src/java/org/apache/fop/afp/modca/AbstractPreparedObjectContainer.java b/src/java/org/apache/fop/afp/goca/AbstractGraphicsObjectContainer.java similarity index 69% rename from src/java/org/apache/fop/afp/modca/AbstractPreparedObjectContainer.java rename to src/java/org/apache/fop/afp/goca/AbstractGraphicsObjectContainer.java index 9019a6acc..672193042 100644 --- a/src/java/org/apache/fop/afp/modca/AbstractPreparedObjectContainer.java +++ b/src/java/org/apache/fop/afp/goca/AbstractGraphicsObjectContainer.java @@ -17,27 +17,30 @@ /* $Id: $ */ -package org.apache.fop.afp.modca; +package org.apache.fop.afp.goca; import java.io.IOException; import java.io.OutputStream; import java.util.Iterator; import java.util.List; +import org.apache.fop.afp.modca.AbstractNamedAFPObject; +import org.apache.fop.afp.modca.StructuredDataObject; + /** * A base container of prepared structured AFP objects */ -public abstract class AbstractPreparedObjectContainer extends AbstractNamedAFPObject -implements PreparedAFPObject { +public abstract class AbstractGraphicsObjectContainer extends AbstractNamedAFPObject +implements StructuredDataObject { /** list of objects contained within this container */ - protected List/**/ objects - = new java.util.ArrayList/**/(); + protected List/**/ objects + = new java.util.ArrayList/**/(); /** * Default constructor */ - protected AbstractPreparedObjectContainer() { + protected AbstractGraphicsObjectContainer() { } /** @@ -45,7 +48,7 @@ implements PreparedAFPObject { * * @param name the name of the container */ - protected AbstractPreparedObjectContainer(String name) { + protected AbstractGraphicsObjectContainer(String name) { super(name); } @@ -55,12 +58,12 @@ implements PreparedAFPObject { } /** - * Adds a given prepared object to this container + * Adds a given graphics object to this container * - * @param preparedObject the prepared object + * @param drawingOrder the graphics object */ - public void addObject(PreparedAFPObject preparedObject) { - objects.add(preparedObject); + public void addObject(StructuredDataObject drawingOrder) { + objects.add(drawingOrder); } /** @@ -73,11 +76,7 @@ implements PreparedAFPObject { int dataLen = 0; Iterator it = objects.iterator(); while (it.hasNext()) { - Object obj = it.next(); - if (obj instanceof PreparedAFPObject) { - PreparedAFPObject prepObj = (PreparedAFPObject)obj; - dataLen += prepObj.getDataLength(); - } + dataLen += ((StructuredDataObject)it.next()).getDataLength(); } return dataLen; } diff --git a/src/java/org/apache/fop/afp/goca/AbstractGraphicsString.java b/src/java/org/apache/fop/afp/goca/AbstractGraphicsString.java new file mode 100644 index 000000000..80882db8a --- /dev/null +++ b/src/java/org/apache/fop/afp/goca/AbstractGraphicsString.java @@ -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.fop.afp.goca; + +import java.io.UnsupportedEncodingException; + +import org.apache.fop.afp.AFPConstants; + +public abstract class AbstractGraphicsString extends AbstractGraphicsCoord { + + /** Up to 255 bytes of character data */ + protected static final int MAX_STR_LEN = 255; + + /** the string to draw */ + protected final String str; + + /** + * Constructor (relative) + * + * @param str the text string + */ + public AbstractGraphicsString(String str) { + super(null); + if (str.length() > MAX_STR_LEN) { + str = str.substring(0, MAX_STR_LEN); + log.warn("truncated character string, longer than " + MAX_STR_LEN + " chars"); + } + this.str = str; + } + + /** + * Constructor (absolute) + * + * @param str the text string + * @param x the x coordinate + * @param y the y coordinate + */ + public AbstractGraphicsString(String str, int x, int y) { + super(x, y); + this.str = str; + } + + /** {@inheritDoc} */ + public int getDataLength() { + return 2 + str.length(); + } + + /** + * Returns the text string as an encoded byte array + * + * @return the text string as an encoded byte array + */ + protected byte[] getStringAsBytes() throws UnsupportedEncodingException { + return str.getBytes(AFPConstants.EBCIDIC_ENCODING); + } + +} diff --git a/src/java/org/apache/fop/afp/goca/GraphicsArea.java b/src/java/org/apache/fop/afp/goca/GraphicsArea.java index 2b6d8a804..3d3bafb45 100644 --- a/src/java/org/apache/fop/afp/goca/GraphicsArea.java +++ b/src/java/org/apache/fop/afp/goca/GraphicsArea.java @@ -22,12 +22,11 @@ package org.apache.fop.afp.goca; import java.io.IOException; import java.io.OutputStream; -import org.apache.fop.afp.modca.AbstractPreparedObjectContainer; /** * A GOCA graphics area (container for filled shapes/objects) */ -public final class GraphicsArea extends AbstractPreparedObjectContainer { +public final class GraphicsArea extends AbstractGraphicsObjectContainer { private static final int RES1 = 1; private static final int BOUNDARY = 2; @@ -47,7 +46,6 @@ public final class GraphicsArea extends AbstractPreparedObjectContainer { /** {@inheritDoc} */ public int getDataLength() { - // start len + end len + data len return 4 + super.getDataLength(); } @@ -62,11 +60,11 @@ public final class GraphicsArea extends AbstractPreparedObjectContainer { /** {@inheritDoc} */ protected void writeEnd(OutputStream os) throws IOException { - byte[] endData = new byte[] { + byte[] data = new byte[] { (byte)0x60, // GEAR order code 0x00, // LENGTH }; - os.write(endData); + os.write(data); } /** {@inheritDoc} */ diff --git a/src/java/org/apache/fop/afp/goca/GraphicsBox.java b/src/java/org/apache/fop/afp/goca/GraphicsBox.java index 4f4947000..945697ec2 100644 --- a/src/java/org/apache/fop/afp/goca/GraphicsBox.java +++ b/src/java/org/apache/fop/afp/goca/GraphicsBox.java @@ -19,6 +19,9 @@ package org.apache.fop.afp.goca; +import java.io.IOException; +import java.io.OutputStream; + /** * A GOCA graphics rectangular box */ @@ -34,28 +37,27 @@ public final class GraphicsBox extends AbstractGraphicsCoord { } /** {@inheritDoc} */ - protected byte getOrderCode() { - return (byte)0xC0; + public int getDataLength() { + return 12; } /** {@inheritDoc} */ - protected int getLength() { - return 10; + int getCoordinateDataStartIndex() { + return 4; } /** {@inheritDoc} */ - protected void prepareData() { - super.data = createData(); - final int fromIndex = 4; - addCoords(data, fromIndex); + byte getOrderCode() { + return (byte)0xC0; } /** {@inheritDoc} */ - protected byte[] createData() { - byte[] data = super.createData(); + public void writeToStream(OutputStream os) throws IOException { + byte[] data = getData(); data[2] = (byte)0x20; // CONTROL draw control flags data[3] = 0x00; // reserved - return data; + + os.write(data); } } \ No newline at end of file diff --git a/src/java/org/apache/fop/afp/goca/GraphicsChainedSegment.java b/src/java/org/apache/fop/afp/goca/GraphicsChainedSegment.java index 8dab3d922..697d4f841 100644 --- a/src/java/org/apache/fop/afp/goca/GraphicsChainedSegment.java +++ b/src/java/org/apache/fop/afp/goca/GraphicsChainedSegment.java @@ -22,14 +22,13 @@ package org.apache.fop.afp.goca; import java.io.IOException; import java.io.OutputStream; -import org.apache.fop.afp.modca.AbstractPreparedObjectContainer; -import org.apache.fop.afp.modca.PreparedAFPObject; +import org.apache.fop.afp.modca.StructuredDataObject; import org.apache.fop.afp.util.BinaryUtils; /** * A GOCA graphics segment */ -public final class GraphicsChainedSegment extends AbstractPreparedObjectContainer { +public final class GraphicsChainedSegment extends AbstractGraphicsObjectContainer { /** The maximum segment data length */ protected static final int MAX_DATA_LEN = 8192; @@ -83,12 +82,17 @@ public final class GraphicsChainedSegment extends AbstractPreparedObjectContaine return NAME_LENGTH; } + /** {@inheritDoc} */ + byte getOrderCode() { + return 0x70; + } + /** {@inheritDoc} */ protected void writeStart(OutputStream os) throws IOException { super.writeStart(os); byte[] data = new byte[14]; - data[0] = 0x70; // BEGIN_SEGMENT + data[0] = getOrderCode(); // BEGIN_SEGMENT data[1] = 0x0C; // Length of following parameters // segment name @@ -133,7 +137,7 @@ public final class GraphicsChainedSegment extends AbstractPreparedObjectContaine } /** {@inheritDoc} */ - public void addObject(PreparedAFPObject drawingOrder) { + public void addObject(StructuredDataObject drawingOrder) { if (currentArea != null) { currentArea.addObject(drawingOrder); } else { diff --git a/src/java/org/apache/fop/afp/goca/GraphicsData.java b/src/java/org/apache/fop/afp/goca/GraphicsData.java index ca5fef3f4..8b59436fc 100644 --- a/src/java/org/apache/fop/afp/goca/GraphicsData.java +++ b/src/java/org/apache/fop/afp/goca/GraphicsData.java @@ -22,21 +22,20 @@ package org.apache.fop.afp.goca; import java.io.IOException; import java.io.OutputStream; -import org.apache.fop.afp.modca.AbstractPreparedObjectContainer; -import org.apache.fop.afp.modca.PreparedAFPObject; +import org.apache.fop.afp.modca.StructuredDataObject; import org.apache.fop.afp.util.BinaryUtils; import org.apache.fop.afp.util.StringUtils; /** * A GOCA graphics data */ -public final class GraphicsData extends AbstractPreparedObjectContainer { +public final class GraphicsData extends AbstractGraphicsObjectContainer { /** The maximum graphics data length */ public static final int MAX_DATA_LEN = 32767; /** The graphics segment */ - private GraphicsChainedSegment currentSegment = null; + private GraphicsChainedSegment segment = null; /** {@inheritDoc} */ public int getDataLength() { @@ -74,10 +73,10 @@ public final class GraphicsData extends AbstractPreparedObjectContainer { * @return the current graphics chained segment */ private GraphicsChainedSegment getSegment() { - if (currentSegment == null) { + if (segment == null) { newSegment(); } - return this.currentSegment; + return this.segment; } /** @@ -87,23 +86,23 @@ public final class GraphicsData extends AbstractPreparedObjectContainer { */ public GraphicsChainedSegment newSegment() { String name = createSegmentName(); - if (currentSegment == null) { - this.currentSegment = new GraphicsChainedSegment(name); + if (segment == null) { + this.segment = new GraphicsChainedSegment(name); } else { - this.currentSegment = new GraphicsChainedSegment(name, currentSegment); + this.segment = new GraphicsChainedSegment(name, segment); } - super.addObject(currentSegment); - return currentSegment; + super.addObject(segment); + return segment; } /** {@inheritDoc} */ - public void addObject(PreparedAFPObject drawingOrder) { - if (currentSegment == null - || (currentSegment.getDataLength() + drawingOrder.getDataLength()) + public void addObject(StructuredDataObject drawingOrder) { + if (segment == null + || (segment.getDataLength() + drawingOrder.getDataLength()) >= GraphicsChainedSegment.MAX_DATA_LEN) { newSegment(); } - currentSegment.addObject(drawingOrder); + segment.addObject(drawingOrder); } /** {@inheritDoc} */ diff --git a/src/java/org/apache/fop/afp/goca/GraphicsFillet.java b/src/java/org/apache/fop/afp/goca/GraphicsFillet.java index 40b98b0d3..b4fa17d65 100644 --- a/src/java/org/apache/fop/afp/goca/GraphicsFillet.java +++ b/src/java/org/apache/fop/afp/goca/GraphicsFillet.java @@ -34,8 +34,7 @@ public final class GraphicsFillet extends AbstractGraphicsCoord { super(coords); } - /** {@inheritDoc} */ - protected byte getOrderCode() { + byte getOrderCode() { return (byte)0xC5; } diff --git a/src/java/org/apache/fop/afp/goca/GraphicsFilletRelative.java b/src/java/org/apache/fop/afp/goca/GraphicsFilletRelative.java index b0e408405..21a1c17d6 100644 --- a/src/java/org/apache/fop/afp/goca/GraphicsFilletRelative.java +++ b/src/java/org/apache/fop/afp/goca/GraphicsFilletRelative.java @@ -19,6 +19,7 @@ package org.apache.fop.afp.goca; + /** * A GOCA graphics curved tangential line to a specified set of * straight lines drawn from the given position or current position @@ -35,8 +36,7 @@ public final class GraphicsFilletRelative extends AbstractGraphicsCoord { } /** {@inheritDoc} */ - protected byte getOrderCode() { + byte getOrderCode() { return (byte)0x85; } - } \ No newline at end of file diff --git a/src/java/org/apache/fop/afp/goca/GraphicsFullArc.java b/src/java/org/apache/fop/afp/goca/GraphicsFullArc.java index 9d511e142..a4b6916ae 100644 --- a/src/java/org/apache/fop/afp/goca/GraphicsFullArc.java +++ b/src/java/org/apache/fop/afp/goca/GraphicsFullArc.java @@ -19,12 +19,16 @@ package org.apache.fop.afp.goca; +import java.io.IOException; +import java.io.OutputStream; + import org.apache.fop.afp.util.BinaryUtils; /** * A GOCA graphics arc (circle/ellipse) */ public class GraphicsFullArc extends AbstractGraphicsCoord { + /** the integer portion of the multiplier */ private final int mh; @@ -43,27 +47,29 @@ public class GraphicsFullArc extends AbstractGraphicsCoord { super(x, y); this.mh = mh; this.mhr = mhr; - // integer portion of multiplier - data[data.length - 2] = BinaryUtils.convert(mh, 1)[0]; - // fractional portion of multiplier - data[data.length - 1] = BinaryUtils.convert(mhr, 1)[0]; } /** {@inheritDoc} */ - protected byte getOrderCode() { - return (byte)0xC7; + public int getDataLength() { + return 8; } /** {@inheritDoc} */ - protected int getLength() { - return super.getLength() + 2; + byte getOrderCode() { + return (byte)0xC7; } /** {@inheritDoc} */ - protected void prepareData() { - super.data = super.createData(); - final int fromIndex = 2; - super.addCoords(data, fromIndex); + public void writeToStream(OutputStream os) throws IOException { + byte[] data = getData(); + + // integer portion of multiplier + data[6] = BinaryUtils.convert(mh, 1)[0]; + + // fractional portion of multiplier + data[7] = BinaryUtils.convert(mhr, 1)[0]; + + os.write(data); } /** {@inheritDoc} */ @@ -75,4 +81,5 @@ public class GraphicsFullArc extends AbstractGraphicsCoord { + ", mhr=" + mhr + "}"; } + } \ No newline at end of file diff --git a/src/java/org/apache/fop/afp/goca/GraphicsImage.java b/src/java/org/apache/fop/afp/goca/GraphicsImage.java index 24d4c78f8..94e9e9ab2 100644 --- a/src/java/org/apache/fop/afp/goca/GraphicsImage.java +++ b/src/java/org/apache/fop/afp/goca/GraphicsImage.java @@ -22,13 +22,13 @@ package org.apache.fop.afp.goca; import java.io.IOException; import java.io.OutputStream; -import org.apache.fop.afp.modca.AbstractStructuredAFPObject; +import org.apache.fop.afp.modca.AbstractStructuredObject; import org.apache.fop.afp.util.BinaryUtils; /** * A GOCA Image */ -public class GraphicsImage extends AbstractStructuredAFPObject { +public class GraphicsImage extends AbstractStructuredObject { /** x coordinate */ private final int x; diff --git a/src/java/org/apache/fop/afp/goca/GraphicsLine.java b/src/java/org/apache/fop/afp/goca/GraphicsLine.java index 99f54b2d1..d8ff1afaa 100644 --- a/src/java/org/apache/fop/afp/goca/GraphicsLine.java +++ b/src/java/org/apache/fop/afp/goca/GraphicsLine.java @@ -21,7 +21,7 @@ package org.apache.fop.afp.goca; /** * A GOCA graphics straight line drawn from the - * given position + * given absolute position */ public class GraphicsLine extends AbstractGraphicsCoord { @@ -35,7 +35,7 @@ public class GraphicsLine extends AbstractGraphicsCoord { } /** {@inheritDoc} */ - protected byte getOrderCode() { + byte getOrderCode() { return (byte)0xC1; } diff --git a/src/java/org/apache/fop/afp/goca/GraphicsLineRelative.java b/src/java/org/apache/fop/afp/goca/GraphicsLineRelative.java index 1eddc5129..43ffebf08 100644 --- a/src/java/org/apache/fop/afp/goca/GraphicsLineRelative.java +++ b/src/java/org/apache/fop/afp/goca/GraphicsLineRelative.java @@ -35,7 +35,7 @@ public class GraphicsLineRelative extends AbstractGraphicsCoord { } /** {@inheritDoc} */ - protected byte getOrderCode() { + byte getOrderCode() { return (byte)0x81; } diff --git a/src/java/org/apache/fop/afp/goca/GraphicsSetCharacterSet.java b/src/java/org/apache/fop/afp/goca/GraphicsSetCharacterSet.java index f4d04910a..1561ecf83 100644 --- a/src/java/org/apache/fop/afp/goca/GraphicsSetCharacterSet.java +++ b/src/java/org/apache/fop/afp/goca/GraphicsSetCharacterSet.java @@ -19,13 +19,18 @@ package org.apache.fop.afp.goca; -import org.apache.fop.afp.modca.AbstractPreparedAFPObject; +import java.io.IOException; +import java.io.OutputStream; + +import org.apache.fop.afp.modca.AbstractNamedAFPObject; +import org.apache.fop.afp.modca.StructuredDataObject; import org.apache.fop.afp.util.BinaryUtils; /** * Sets the current character set (font) to be used for following graphics strings */ -public class GraphicsSetCharacterSet extends AbstractPreparedAFPObject { +public class GraphicsSetCharacterSet extends AbstractNamedAFPObject + implements StructuredDataObject { /** font character set reference */ private final int fontReference; @@ -35,19 +40,25 @@ public class GraphicsSetCharacterSet extends AbstractPreparedAFPObject { */ public GraphicsSetCharacterSet(int fontReference) { this.fontReference = fontReference; - prepareData(); } /** {@inheritDoc} */ - protected void prepareData() { - super.data = new byte[] { + public void writeToStream(OutputStream os) throws IOException { + byte[] data = new byte[] { 0x38, // GSCS order code BinaryUtils.convert(fontReference)[0] }; + os.write(data); + } + + /** {@inheritDoc} */ + public int getDataLength() { + return 2; } /** {@inheritDoc} */ public String toString() { return "GraphicsSetCharacterSet(" + fontReference + ")"; } + } \ No newline at end of file diff --git a/src/java/org/apache/fop/afp/goca/GraphicsSetLineType.java b/src/java/org/apache/fop/afp/goca/GraphicsSetLineType.java index 0093885a6..3479bf4e5 100644 --- a/src/java/org/apache/fop/afp/goca/GraphicsSetLineType.java +++ b/src/java/org/apache/fop/afp/goca/GraphicsSetLineType.java @@ -19,12 +19,17 @@ package org.apache.fop.afp.goca; -import org.apache.fop.afp.modca.AbstractPreparedAFPObject; +import java.io.IOException; +import java.io.OutputStream; + +import org.apache.fop.afp.modca.AbstractNamedAFPObject; +import org.apache.fop.afp.modca.StructuredDataObject; /** * Sets the value of the current line type attribute when stroking GOCA shapes (structured fields) */ -public class GraphicsSetLineType extends AbstractPreparedAFPObject { +public class GraphicsSetLineType extends AbstractNamedAFPObject +implements StructuredDataObject { /** the default line type */ public static final byte DEFAULT = 0x00; // normally SOLID @@ -63,15 +68,20 @@ public class GraphicsSetLineType extends AbstractPreparedAFPObject { */ public GraphicsSetLineType(byte type) { this.type = type; - prepareData(); } /** {@inheritDoc} */ - protected void prepareData() { - super.data = new byte[] { - 0x18, // GSLW order code - type // line type + public int getDataLength() { + return 2; + } + + /** {@inheritDoc} */ + public void writeToStream(OutputStream os) throws IOException { + byte[] data = new byte[] { + 0x18, // GSLW order code + type // line type }; + os.write(data); } private static final String[] TYPES = { diff --git a/src/java/org/apache/fop/afp/goca/GraphicsSetLineWidth.java b/src/java/org/apache/fop/afp/goca/GraphicsSetLineWidth.java index 863cbf50d..09ed0d7dc 100644 --- a/src/java/org/apache/fop/afp/goca/GraphicsSetLineWidth.java +++ b/src/java/org/apache/fop/afp/goca/GraphicsSetLineWidth.java @@ -19,12 +19,17 @@ package org.apache.fop.afp.goca; -import org.apache.fop.afp.modca.AbstractPreparedAFPObject; +import java.io.IOException; +import java.io.OutputStream; + +import org.apache.fop.afp.modca.AbstractNamedAFPObject; +import org.apache.fop.afp.modca.StructuredDataObject; /** * Sets the line width to use when stroking GOCA shapes (structured fields) */ -public class GraphicsSetLineWidth extends AbstractPreparedAFPObject { +public class GraphicsSetLineWidth extends AbstractNamedAFPObject implements StructuredDataObject { + /** line width multiplier */ private int multiplier = 1; @@ -35,15 +40,20 @@ public class GraphicsSetLineWidth extends AbstractPreparedAFPObject { */ public GraphicsSetLineWidth(int multiplier) { this.multiplier = multiplier; - prepareData(); } /** {@inheritDoc} */ - protected void prepareData() { - super.data = new byte[] { + public int getDataLength() { + return 2; + } + + /** {@inheritDoc} */ + public void writeToStream(OutputStream os) throws IOException { + byte[] data = new byte[] { 0x19, // GSLW order code (byte)multiplier // MH (line-width) }; + os.write(data); } /** {@inheritDoc} */ diff --git a/src/java/org/apache/fop/afp/goca/GraphicsSetMix.java b/src/java/org/apache/fop/afp/goca/GraphicsSetMix.java index 339620271..0058a5013 100644 --- a/src/java/org/apache/fop/afp/goca/GraphicsSetMix.java +++ b/src/java/org/apache/fop/afp/goca/GraphicsSetMix.java @@ -19,9 +19,12 @@ package org.apache.fop.afp.goca; -import org.apache.fop.afp.modca.AbstractPreparedAFPObject; +import java.io.IOException; +import java.io.OutputStream; -public class GraphicsSetMix extends AbstractPreparedAFPObject { +import org.apache.fop.afp.modca.AbstractNamedAFPObject; + +public class GraphicsSetMix extends AbstractNamedAFPObject { public static final byte MODE_DEFAULT = 0x00; public static final byte MODE_OVERPAINT = 0x02; @@ -36,15 +39,15 @@ public class GraphicsSetMix extends AbstractPreparedAFPObject { */ public GraphicsSetMix(byte mode) { this.mode = mode; - prepareData(); } /** {@inheritDoc} */ - protected void prepareData() { - super.data = new byte[] { + public void writeToStream(OutputStream os) throws IOException { + byte[] data = new byte[] { 0x0C, // GSMX order code mode // MODE (mix mode value) }; + os.write(data); } /** {@inheritDoc} */ diff --git a/src/java/org/apache/fop/afp/goca/GraphicsSetPatternSymbol.java b/src/java/org/apache/fop/afp/goca/GraphicsSetPatternSymbol.java index f0e6845be..0d74aa9d3 100644 --- a/src/java/org/apache/fop/afp/goca/GraphicsSetPatternSymbol.java +++ b/src/java/org/apache/fop/afp/goca/GraphicsSetPatternSymbol.java @@ -19,12 +19,18 @@ package org.apache.fop.afp.goca; -import org.apache.fop.afp.modca.AbstractPreparedAFPObject; +import java.io.IOException; +import java.io.OutputStream; + +import org.apache.fop.afp.modca.AbstractNamedAFPObject; +import org.apache.fop.afp.modca.StructuredDataObject; /** * Sets the pattern symbol to use when filling following GOCA structured fields */ -public class GraphicsSetPatternSymbol extends AbstractPreparedAFPObject { +public class GraphicsSetPatternSymbol extends AbstractNamedAFPObject +implements StructuredDataObject { + /** dotted density 1 */ public static final byte DOTTED_DENSITY_1 = 0x01; @@ -86,15 +92,20 @@ public class GraphicsSetPatternSymbol extends AbstractPreparedAFPObject { */ public GraphicsSetPatternSymbol(byte symb) { this.symbol = symb; - prepareData(); } /** {@inheritDoc} */ - protected void prepareData() { - super.data = new byte[] { + public int getDataLength() { + return 2; + } + + /** {@inheritDoc} */ + public void writeToStream(OutputStream os) throws IOException { + byte[] data = new byte[] { 0x28, // GSPT order code symbol }; + os.write(data); } /** {@inheritDoc} */ diff --git a/src/java/org/apache/fop/afp/goca/GraphicsSetProcessColor.java b/src/java/org/apache/fop/afp/goca/GraphicsSetProcessColor.java index 6ec1e50ee..41ddeaa96 100644 --- a/src/java/org/apache/fop/afp/goca/GraphicsSetProcessColor.java +++ b/src/java/org/apache/fop/afp/goca/GraphicsSetProcessColor.java @@ -21,16 +21,22 @@ package org.apache.fop.afp.goca; import java.awt.Color; import java.awt.color.ColorSpace; +import java.io.IOException; +import java.io.OutputStream; -import org.apache.fop.afp.modca.AbstractPreparedAFPObject; +import org.apache.fop.afp.modca.AbstractNamedAFPObject; +import org.apache.fop.afp.modca.StructuredDataObject; /** * Sets the current processing color for the following GOCA structured fields */ -public class GraphicsSetProcessColor extends AbstractPreparedAFPObject { - /** the color to set */ +public class GraphicsSetProcessColor extends AbstractNamedAFPObject +implements StructuredDataObject { + private final Color color; + private final float[] colorComponents; + /** * Main constructor * @@ -38,11 +44,22 @@ public class GraphicsSetProcessColor extends AbstractPreparedAFPObject { */ public GraphicsSetProcessColor(Color color) { this.color = color; - prepareData(); + this.colorComponents = color.getColorComponents(null); + } + + /** {@inheritDoc} */ + public int getDataLength() { + return 12 + colorComponents.length; } /** {@inheritDoc} */ - protected void prepareData() { + byte getOrderCode() { + return (byte)0xB2; + } + + /** {@inheritDoc} */ + public void writeToStream(OutputStream os) throws IOException { + // COLSPCE byte colspace; int colSpaceType = color.getColorSpace().getType(); @@ -56,16 +73,15 @@ public class GraphicsSetProcessColor extends AbstractPreparedAFPObject { } // COLSIZE(S) - float[] colcomp = color.getColorComponents(null); byte[] colsizes = new byte[] {0x00, 0x00, 0x00, 0x00}; - for (int i = 0; i < colcomp.length; i++) { + for (int i = 0; i < colorComponents.length; i++) { colsizes[i] = (byte)8; } - int len = 10 + colcomp.length; - super.data = new byte[len + 2]; - data[0] = (byte)0xB2; // GSPCOL order code - data[1] = (byte)len; // LEN + int len = getDataLength(); + byte[] data = new byte[len]; + data[0] = getOrderCode(); // GSPCOL order code + data[1] = (byte)(len - 2); // LEN data[2] = 0x00; // reserved; must be zero data[3] = colspace; // COLSPCE data[4] = 0x00; // reserved; must be zero @@ -76,10 +92,13 @@ public class GraphicsSetProcessColor extends AbstractPreparedAFPObject { data[9] = colsizes[1]; data[10] = colsizes[2]; data[11] = colsizes[3]; + // COLVALUE(S) - for (int i = 0; i < colcomp.length; i++) { - data[i + 12] = (byte)(colcomp[i] * 255); + for (int i = 0; i < colorComponents.length; i++) { + data[i + 12] = (byte)(colorComponents[i] * 255); } + + os.write(data); } /** {@inheritDoc} */ diff --git a/src/java/org/apache/fop/afp/goca/GraphicsString.java b/src/java/org/apache/fop/afp/goca/GraphicsString.java index 0fa14bb6d..c08da64ec 100644 --- a/src/java/org/apache/fop/afp/goca/GraphicsString.java +++ b/src/java/org/apache/fop/afp/goca/GraphicsString.java @@ -19,41 +19,13 @@ package org.apache.fop.afp.goca; -import java.io.UnsupportedEncodingException; - -import org.apache.fop.afp.AFPConstants; -import org.apache.fop.afp.modca.AbstractPreparedAFPObject; -import org.apache.fop.afp.util.BinaryUtils; +import java.io.IOException; +import java.io.OutputStream; /** * A GOCA graphics string */ -public class GraphicsString extends AbstractPreparedAFPObject { - /** Up to 255 bytes of character data */ - private static final int MAX_STR_LEN = 255; - - /** drawn from the current position */ - private boolean fromCurrentPosition = false; - - /** the string to draw */ - private String str = null; - - /** x coordinate */ - private int x; - - /** y coordinate */ - private int y; - - /** - * Constructor - * - * @param str the character string - */ - public GraphicsString(String str) { - this.str = str; - fromCurrentPosition = true; - prepareData(); - } +public class GraphicsString extends AbstractGraphicsString { /** * Constructor @@ -63,53 +35,30 @@ public class GraphicsString extends AbstractPreparedAFPObject { * @param y the y coordinate */ public GraphicsString(String str, int x, int y) { - this.str = str; - this.x = x; - this.y = y; - prepareData(); + super(str, x, y); + } + + /** {@inheritDoc} */ + byte getOrderCode() { + return (byte)0xC3; } /** {@inheritDoc} */ - protected void prepareData() { - int maxStrLen = MAX_STR_LEN - (fromCurrentPosition ? 0 : 4); - if (str.length() > maxStrLen) { - str = str.substring(0, maxStrLen); - log.warn("truncated character string, longer than " + maxStrLen + " chars"); - } - byte[] strData = null; - try { - strData = str.getBytes(AFPConstants.EBCIDIC_ENCODING); - } catch (UnsupportedEncodingException ex) { - log.error("unsupported encoding: " + ex.getMessage()); - } - int len = strData.length; - if (fromCurrentPosition) { - data = new byte[len + 2]; - data[0] = (byte)0x83; - data[1] = (byte)len; - System.arraycopy(strData, 0, data, 2, strData.length); - } else { - len += 4; // x/y coordinates - byte[] osx = BinaryUtils.convert(x, 2); - byte[] osy = BinaryUtils.convert(y, 2); - data = new byte[len + 2]; - data[0] = (byte)0xC3; - data[1] = (byte)len; - data[2] = osx[0]; - data[3] = osx[1]; - data[4] = osy[0]; - data[5] = osy[1]; - System.arraycopy(strData, 0, data, 6, strData.length); - } + public int getDataLength() { + return super.getDataLength() + (coords.length * 2); + } + + /** {@inheritDoc} */ + public void writeToStream(OutputStream os) throws IOException { + byte[] data = getData(); + byte[] strData = getStringAsBytes(); + System.arraycopy(strData, 0, data, 6, strData.length); + + os.write(data); } /** {@inheritDoc} */ public String toString() { - String string = "GraphicsString{str='" + str + "'"; - if (!fromCurrentPosition) { - string += ",x=" + x + ",y=" + y; - } - string += "}"; - return string; + return "GraphicsString{x=" + coords[0] + ", y=" + coords[1] + "str='" + str + "'" + "}"; } } \ No newline at end of file diff --git a/src/java/org/apache/fop/afp/goca/GraphicsStringRelative.java b/src/java/org/apache/fop/afp/goca/GraphicsStringRelative.java new file mode 100644 index 000000000..af0c05b5d --- /dev/null +++ b/src/java/org/apache/fop/afp/goca/GraphicsStringRelative.java @@ -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.fop.afp.goca; + +import java.io.IOException; +import java.io.OutputStream; + +/** + * A GOCA graphics string + */ +public class GraphicsStringRelative extends AbstractGraphicsString { + + /** + * Constructor + * + * @param str the character string + */ + public GraphicsStringRelative(String str) { + super(str); + } + + /** {@inheritDoc} */ + byte getOrderCode() { + return (byte)0x83; + } + + /** {@inheritDoc} */ + public void writeToStream(OutputStream os) throws IOException { + byte[] data = getData(); + byte[] strData = getStringAsBytes(); + System.arraycopy(strData, 0, data, 2, strData.length); + os.write(data); + } + + /** {@inheritDoc} */ + public String toString() { + return "GraphicsStringRelative{str='" + str + "'" + "}"; + } + +} \ No newline at end of file diff --git a/src/java/org/apache/fop/afp/ioca/ImageContent.java b/src/java/org/apache/fop/afp/ioca/ImageContent.java index fc8ce0944..6cfddab94 100644 --- a/src/java/org/apache/fop/afp/ioca/ImageContent.java +++ b/src/java/org/apache/fop/afp/ioca/ImageContent.java @@ -22,12 +22,12 @@ package org.apache.fop.afp.ioca; import java.io.IOException; import java.io.OutputStream; -import org.apache.fop.afp.modca.AbstractStructuredAFPObject; +import org.apache.fop.afp.modca.AbstractStructuredObject; /** * An IOCA Image Content */ -public class ImageContent extends AbstractStructuredAFPObject { +public class ImageContent extends AbstractStructuredObject { /** * The CCITT T.4 Group 3 Coding Standard (G3 MH-Modified Huffman) is a diff --git a/src/java/org/apache/fop/afp/modca/AbstractAFPObject.java b/src/java/org/apache/fop/afp/modca/AbstractAFPObject.java index 9e1e107f3..f34ac7d00 100644 --- a/src/java/org/apache/fop/afp/modca/AbstractAFPObject.java +++ b/src/java/org/apache/fop/afp/modca/AbstractAFPObject.java @@ -20,6 +20,7 @@ package org.apache.fop.afp.modca; import java.io.IOException; +import java.io.InputStream; import java.io.OutputStream; import java.util.Collection; import java.util.Iterator; @@ -27,6 +28,7 @@ import java.util.Iterator; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.apache.fop.afp.Streamable; +import org.apache.fop.afp.util.BinaryUtils; /** * This is the base class for all data stream objects. Page objects are @@ -100,6 +102,81 @@ public abstract class AbstractAFPObject implements Streamable { } } + /** + * Reads data chunks from an inputstream + * and then formats them with a structured header to a given outputstream + * + * @param dataHeader the header data + * @param lengthOffset offset of length field in data chunk + * @param maxChunkLength the maximum chunk length + * @param inputStream the inputstream to read from + * @param outputStream the outputstream to write to + * @throws IOException thrown if an I/O exception of some sort has occurred. + */ + protected static void copyChunks(byte[] dataHeader, int lengthOffset, + int maxChunkLength, InputStream inputStream, OutputStream outputStream) + throws IOException { + int headerLen = dataHeader.length - lengthOffset; + // length field is just before data so do not include in data length + if (headerLen == 2) { + headerLen = 0; + } + byte[] data = new byte[maxChunkLength]; + int numBytesRead = 0; + while ((numBytesRead = inputStream.read(data, 0, maxChunkLength)) > 0) { + byte[] len = BinaryUtils.convert(headerLen + numBytesRead, 2); + dataHeader[lengthOffset] = len[0]; // Length byte 1 + dataHeader[lengthOffset + 1] = len[1]; // Length byte 2 + outputStream.write(dataHeader); + outputStream.write(data, 0, numBytesRead); + } + } + + /** + * Writes data chunks to a given outputstream + * + * @param data the data byte array + * @param dataHeader the header data + * @param lengthOffset offset of length field in data chunk + * @param maxChunkLength the maximum chunk length + * @param os the outputstream to write to + * @throws IOException thrown if an I/O exception of some sort has occurred. + */ + protected static void writeChunksToStream(byte[] data, byte[] dataHeader, + int lengthOffset, int maxChunkLength, OutputStream os) throws IOException { + int dataLength = data.length; + int numFullChunks = dataLength / maxChunkLength; + int lastChunkLength = dataLength % maxChunkLength; + + int headerLen = dataHeader.length - lengthOffset; + // length field is just before data so do not include in data length + if (headerLen == 2) { + headerLen = 0; + } + + byte[] len; + int off = 0; + if (numFullChunks > 0) { + // write out full data chunks + len = BinaryUtils.convert(headerLen + maxChunkLength, 2); + dataHeader[lengthOffset] = len[0]; // Length byte 1 + dataHeader[lengthOffset + 1] = len[1]; // Length byte 2 + for (int i = 0; i < numFullChunks; i++, off += maxChunkLength) { + os.write(dataHeader); + os.write(data, off, maxChunkLength); + } + } + + if (lastChunkLength > 0) { + // write last data chunk + len = BinaryUtils.convert(headerLen + lastChunkLength, 2); + dataHeader[lengthOffset] = len[0]; // Length byte 1 + dataHeader[lengthOffset + 1] = len[1]; // Length byte 2 + os.write(dataHeader); + os.write(data, off, lastChunkLength); + } + } + /** structured field type codes */ public interface Type { diff --git a/src/java/org/apache/fop/afp/modca/AbstractDescriptor.java b/src/java/org/apache/fop/afp/modca/AbstractDescriptor.java index f3734cfb7..c471794e9 100644 --- a/src/java/org/apache/fop/afp/modca/AbstractDescriptor.java +++ b/src/java/org/apache/fop/afp/modca/AbstractDescriptor.java @@ -22,7 +22,7 @@ package org.apache.fop.afp.modca; /** * Base class for AFP descriptor objects */ -public abstract class AbstractDescriptor extends AbstractStructuredAFPObject { +public abstract class AbstractDescriptor extends AbstractTripletStructuredObject { /** width of this descriptor */ protected int width = 0; /** height of this descriptor */ diff --git a/src/java/org/apache/fop/afp/modca/AbstractNamedAFPObject.java b/src/java/org/apache/fop/afp/modca/AbstractNamedAFPObject.java index de02f13d3..c3c158825 100644 --- a/src/java/org/apache/fop/afp/modca/AbstractNamedAFPObject.java +++ b/src/java/org/apache/fop/afp/modca/AbstractNamedAFPObject.java @@ -27,7 +27,7 @@ import org.apache.fop.afp.AFPConstants; * This is the base class for all named data stream objects. * A named data stream object has an 8 byte EBCIDIC name. */ -public abstract class AbstractNamedAFPObject extends AbstractStructuredAFPObject { +public abstract class AbstractNamedAFPObject extends AbstractTripletStructuredObject { private static final int DEFAULT_NAME_LENGTH = 8; diff --git a/src/java/org/apache/fop/afp/modca/AbstractPreparedAFPObject.java b/src/java/org/apache/fop/afp/modca/AbstractPreparedAFPObject.java deleted file mode 100644 index 10a516318..000000000 --- a/src/java/org/apache/fop/afp/modca/AbstractPreparedAFPObject.java +++ /dev/null @@ -1,96 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file to You under the Apache License, Version 2.0 - * (the "License"); you may not use this file except in compliance with - * the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -/* $Id: $ */ - -package org.apache.fop.afp.modca; - -import java.io.IOException; -import java.io.OutputStream; - -/** - * A base class that carries out early preparation of structured field data - * for the AFP object (so the data length can be pre-calculated) - */ -public abstract class AbstractPreparedAFPObject extends AbstractNamedAFPObject -implements PreparedAFPObject { - - /** structured field data to be written */ - protected byte[] data = null; - - /** - * Default constructor - */ - public AbstractPreparedAFPObject() { - } - - /** - * Named constructor - * - * @param name the name of this AFP object - */ - public AbstractPreparedAFPObject(String name) { - super(name); - } - - /** {@inheritDoc} */ - protected void writeContent(OutputStream os) throws IOException { - super.writeContent(os); // write triplets - if (this.data != null) { - os.write(this.data); - } - } - - /** - * Return the start data length of this structured field - * - * @return the start data length of this structured field - */ - protected int getStartDataLength() { - return 0; - } - - /** - * Return the data length of the structured field data of this AFP object - * - * @return the data length of the structured field data of this AFP object - */ - public int getDataLength() { - if (this.data != null) { - return this.data.length; - } - return 0; - } - - /** - * Return the structured field length - * - * @return the structured field length - */ - protected int getLength() { - return getStartDataLength() + getTripletDataLength() + getDataLength(); - } - - /** - * Sets the data - * - * @param data the data - */ - protected void setData(byte[] data) { - this.data = data; - } -} \ No newline at end of file diff --git a/src/java/org/apache/fop/afp/modca/AbstractStructuredAFPObject.java b/src/java/org/apache/fop/afp/modca/AbstractStructuredAFPObject.java deleted file mode 100644 index 9bfc11095..000000000 --- a/src/java/org/apache/fop/afp/modca/AbstractStructuredAFPObject.java +++ /dev/null @@ -1,324 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file to You under the Apache License, Version 2.0 - * (the "License"); you may not use this file except in compliance with - * the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -/* $Id$ */ - -package org.apache.fop.afp.modca; - -import java.io.IOException; -import java.io.InputStream; -import java.io.OutputStream; -import java.io.UnsupportedEncodingException; -import java.util.Collection; -import java.util.Iterator; -import java.util.List; - -import org.apache.commons.io.output.ByteArrayOutputStream; -import org.apache.fop.afp.modca.Registry.ObjectType; -import org.apache.fop.afp.modca.triplets.FullyQualifiedNameTriplet; -import org.apache.fop.afp.modca.triplets.ObjectClassificationTriplet; -import org.apache.fop.afp.modca.triplets.Triplet; -import org.apache.fop.afp.util.BinaryUtils; - -/** - * An abstract class encapsulating an MODCA structured object - */ -public abstract class AbstractStructuredAFPObject extends AbstractAFPObject { - /** - * list of object triplets - */ - protected List/**/ triplets = null; - - /** - * triplet data created from triplet list - */ - protected byte[] tripletData = null; - - /** - * Default constructor - */ - protected AbstractStructuredAFPObject() { - } - - /** - * Returns the triplet data length - * - * @return the triplet data length - */ - protected int getTripletDataLength() { - if (tripletData == null) { - try { - getTripletData(); - } catch (IOException e) { - log.error("failed to get triplet data"); - } - } - if (tripletData != null) { - return tripletData.length; - } - return 0; - } - - /** - * Returns the triplet data - * - * @return the triplet data - * @throws IOException throws an I/O exception if one occurred - */ - protected byte[] getTripletData() throws IOException { - if (tripletData == null && triplets != null) { - ByteArrayOutputStream baos = new ByteArrayOutputStream(); - writeObjects(triplets, baos); - this.tripletData = baos.toByteArray(); - triplets = null; // gc - } - return this.tripletData; - } - - /** - * Writes any triplet data - * - * @param os The stream to write to - * @throws IOException The stream to write to - */ - protected void writeTriplets(OutputStream os) throws IOException { - if (tripletData != null) { - os.write(tripletData); - } else if (triplets != null) { - writeObjects(triplets, os); - triplets = null; // gc - } - } - - /** - * Helper method to write the start of the Object. - * - * @param os The stream to write to - * @throws IOException throws an I/O exception if one occurred - */ - protected void writeStart(OutputStream os) throws IOException { - getTripletData(); - } - - /** - * Helper method to write the end of the Object. - * - * @param os The stream to write to - * @throws IOException an I/O exception if one occurred - */ - protected void writeEnd(OutputStream os) throws IOException { - } - - /** - * Helper method to write the contents of the Object. - * - * @param os The stream to write to - * @throws IOException throws an I/O exception if one occurred - */ - protected void writeContent(OutputStream os) throws IOException { - writeTriplets(os); - } - - /** {@inheritDoc} */ - public void writeToStream(OutputStream os) throws IOException { - writeStart(os); - writeContent(os); - writeEnd(os); - } - - /** - * Returns the first matching triplet found in the structured field triplet list - * - * @param tripletId the triplet identifier - */ - private Triplet getTriplet(byte tripletId) { - Iterator it = getTriplets().iterator(); - while (it.hasNext()) { - Triplet triplet = (Triplet)it.next(); - if (triplet.getId() == tripletId) { - return triplet; - } - } - return null; - } - - /** - * Returns true of this structured field has the given triplet - * - * @param tripletId the triplet identifier - * @return true if the structured field has the given triplet - */ - public boolean hasTriplet(byte tripletId) { - return getTriplet(tripletId) != null; - } - - /** - * Adds a triplet to this structured object - * - * @param triplet the triplet to add - */ - protected void addTriplet(Triplet triplet) { - getTriplets().add(triplet); - } - - /** - * Adds a list of triplets to the triplets contained within this structured field - * - * @param tripletCollection a collection of triplets - */ - public void addTriplets(Collection/**/ tripletCollection) { - if (tripletCollection != null) { - getTriplets().addAll(tripletCollection); - } - } - - /** @return the triplet list pertaining to this resource */ - protected List/**/ getTriplets() { - if (triplets == null) { - triplets = new java.util.ArrayList(); - } - return triplets; - } - - /** - * Sets the fully qualified name of this resource - * - * @param fqnType the fully qualified name type of this resource - * @param fqnFormat the fully qualified name format of this resource - * @param fqName the fully qualified name of this resource - */ - public void setFullyQualifiedName(byte fqnType, byte fqnFormat, String fqName) { - addTriplet(new FullyQualifiedNameTriplet(fqnType, fqnFormat, fqName)); - } - - /** @return the fully qualified name of this triplet or null if it does not exist */ - public String getFullyQualifiedName() { - FullyQualifiedNameTriplet fqNameTriplet - = (FullyQualifiedNameTriplet)getTriplet(Triplet.FULLY_QUALIFIED_NAME); - if (fqNameTriplet != null) { - return fqNameTriplet.getFullyQualifiedName(); - } - log.warn(this + " has no fully qualified name"); - return null; - } - - /** - * Sets the objects classification - * - * @param objectClass the classification of the object - * @param objectType the MOD:CA registry object type entry for the given - * object/component type of the object - * @param dataInContainer whether the data resides in the container - * @param containerHasOEG whether the container has an object environment group - * @param dataInOCD whether the data resides in a object container data structured field - */ - public void setObjectClassification( - byte objectClass, ObjectType objectType, - boolean dataInContainer, boolean containerHasOEG, boolean dataInOCD) { - addTriplet( - new ObjectClassificationTriplet( - objectClass, objectType, dataInContainer, containerHasOEG, dataInOCD)); - } - - /** - * Sets a comment on this resource - * - * @param comment a comment string - */ - public void setComment(String comment) { - try { - addTriplet(new Triplet(Triplet.COMMENT, comment)); - } catch (UnsupportedEncodingException e) { - log.error(e.getMessage()); - } - } - - /** - * Reads data chunks from an inputstream - * and then formats them with a structured header to a given outputstream - * - * @param dataHeader the header data - * @param lengthOffset offset of length field in data chunk - * @param maxChunkLength the maximum chunk length - * @param inputStream the inputstream to read from - * @param outputStream the outputstream to write to - * @throws IOException thrown if an I/O exception of some sort has occurred. - */ - protected static void copyChunks(byte[] dataHeader, int lengthOffset, - int maxChunkLength, InputStream inputStream, OutputStream outputStream) - throws IOException { - int headerLen = dataHeader.length - lengthOffset; - // length field is just before data so do not include in data length - if (headerLen == 2) { - headerLen = 0; - } - byte[] data = new byte[maxChunkLength]; - int numBytesRead = 0; - while ((numBytesRead = inputStream.read(data, 0, maxChunkLength)) > 0) { - byte[] len = BinaryUtils.convert(headerLen + numBytesRead, 2); - dataHeader[lengthOffset] = len[0]; // Length byte 1 - dataHeader[lengthOffset + 1] = len[1]; // Length byte 2 - outputStream.write(dataHeader); - outputStream.write(data, 0, numBytesRead); - } - } - - /** - * Writes data chunks to a given outputstream - * - * @param data the data byte array - * @param dataHeader the header data - * @param lengthOffset offset of length field in data chunk - * @param maxChunkLength the maximum chunk length - * @param os the outputstream to write to - * @throws IOException thrown if an I/O exception of some sort has occurred. - */ - protected static void writeChunksToStream(byte[] data, byte[] dataHeader, - int lengthOffset, int maxChunkLength, OutputStream os) throws IOException { - int dataLength = data.length; - int numFullChunks = dataLength / maxChunkLength; - int lastChunkLength = dataLength % maxChunkLength; - - int headerLen = dataHeader.length - lengthOffset; - // length field is just before data so do not include in data length - if (headerLen == 2) { - headerLen = 0; - } - - byte[] len; - int off = 0; - if (numFullChunks > 0) { - // write out full data chunks - len = BinaryUtils.convert(headerLen + maxChunkLength, 2); - dataHeader[lengthOffset] = len[0]; // Length byte 1 - dataHeader[lengthOffset + 1] = len[1]; // Length byte 2 - for (int i = 0; i < numFullChunks; i++, off += maxChunkLength) { - os.write(dataHeader); - os.write(data, off, maxChunkLength); - } - } - - if (lastChunkLength > 0) { - // write last data chunk - len = BinaryUtils.convert(headerLen + lastChunkLength, 2); - dataHeader[lengthOffset] = len[0]; // Length byte 1 - dataHeader[lengthOffset + 1] = len[1]; // Length byte 2 - os.write(dataHeader); - os.write(data, off, lastChunkLength); - } - } -} diff --git a/src/java/org/apache/fop/afp/modca/AbstractStructuredObject.java b/src/java/org/apache/fop/afp/modca/AbstractStructuredObject.java new file mode 100644 index 000000000..e848583b5 --- /dev/null +++ b/src/java/org/apache/fop/afp/modca/AbstractStructuredObject.java @@ -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.fop.afp.modca; + +import java.io.IOException; +import java.io.OutputStream; + +/** + * An abstract class encapsulating an MODCA structured object + */ +public abstract class AbstractStructuredObject extends AbstractAFPObject { + + /** + * Default constructor + */ + protected AbstractStructuredObject() { + } + + /** + * Helper method to write the start of the Object. + * + * @param os The stream to write to + * @throws IOException throws an I/O exception if one occurred + */ + protected void writeStart(OutputStream os) throws IOException { + } + + /** + * Helper method to write the end of the Object. + * + * @param os The stream to write to + * @throws IOException an I/O exception if one occurred + */ + protected void writeEnd(OutputStream os) throws IOException { + } + + /** + * Helper method to write the contents of the Object. + * + * @param os The stream to write to + * @throws IOException throws an I/O exception if one occurred + */ + protected void writeContent(OutputStream os) throws IOException { + } + + /** {@inheritDoc} */ + public void writeToStream(OutputStream os) throws IOException { + writeStart(os); + writeContent(os); + writeEnd(os); + } +} diff --git a/src/java/org/apache/fop/afp/modca/AbstractTripletStructuredObject.java b/src/java/org/apache/fop/afp/modca/AbstractTripletStructuredObject.java new file mode 100644 index 000000000..c1686d07c --- /dev/null +++ b/src/java/org/apache/fop/afp/modca/AbstractTripletStructuredObject.java @@ -0,0 +1,178 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +/* $Id$ */ + +package org.apache.fop.afp.modca; + +import java.io.IOException; +import java.io.OutputStream; +import java.util.Collection; +import java.util.Iterator; +import java.util.List; + +import org.apache.fop.afp.modca.Registry.ObjectType; +import org.apache.fop.afp.modca.triplets.AbstractTriplet; +import org.apache.fop.afp.modca.triplets.CommentTriplet; +import org.apache.fop.afp.modca.triplets.FullyQualifiedNameTriplet; +import org.apache.fop.afp.modca.triplets.ObjectClassificationTriplet; + +public class AbstractTripletStructuredObject extends AbstractStructuredObject { + + /** list of object triplets */ + protected List/**/ triplets = new java.util.ArrayList/**/(); + + /** + * Returns the triplet data length + * + * @return the triplet data length + */ + protected int getTripletDataLength() { + int dataLength = 0; + if (hasTriplets()) { + Iterator it = triplets.iterator(); + while (it.hasNext()) { + AbstractTriplet triplet = (AbstractTriplet)it.next(); + dataLength += triplet.getDataLength(); + } + } + return dataLength; + } + + /** + * Returns true when this structured field contains triplets + * + * @return true when this structured field contains triplets + */ + public boolean hasTriplets() { + return triplets.size() > 0; + } + + /** + * Writes any triplet data + * + * @param os The stream to write to + * @throws IOException The stream to write to + */ + protected void writeTriplets(OutputStream os) throws IOException { + if (hasTriplets()) { + writeObjects(triplets, os); + triplets = null; // gc + } + } + + /** + * Returns the first matching triplet found in the structured field triplet list + * + * @param tripletId the triplet identifier + */ + private AbstractTriplet getTriplet(byte tripletId) { + Iterator it = getTriplets().iterator(); + while (it.hasNext()) { + AbstractTriplet triplet = (AbstractTriplet)it.next(); + if (triplet.getId() == tripletId) { + return triplet; + } + } + return null; + } + + /** + * Returns true of this structured field has the given triplet + * + * @param tripletId the triplet identifier + * @return true if the structured field has the given triplet + */ + public boolean hasTriplet(byte tripletId) { + return getTriplet(tripletId) != null; + } + + /** + * Adds a triplet to this structured object + * + * @param triplet the triplet to add + */ + protected void addTriplet(AbstractTriplet triplet) { + triplets.add(triplet); + } + + /** + * Adds a list of triplets to the triplets contained within this structured field + * + * @param tripletCollection a collection of triplets + */ + public void addTriplets(Collection/**/ tripletCollection) { + if (tripletCollection != null) { + triplets.addAll(tripletCollection); + } + } + + /** @return the triplet list pertaining to this resource */ + protected List/**/ getTriplets() { + return triplets; + } + + /** + * Sets the fully qualified name of this resource + * + * @param fqnType the fully qualified name type of this resource + * @param fqnFormat the fully qualified name format of this resource + * @param fqName the fully qualified name of this resource + */ + public void setFullyQualifiedName(byte fqnType, byte fqnFormat, String fqName) { + addTriplet(new FullyQualifiedNameTriplet(fqnType, fqnFormat, fqName)); + } + + /** @return the fully qualified name of this triplet or null if it does not exist */ + public String getFullyQualifiedName() { + FullyQualifiedNameTriplet fqNameTriplet + = (FullyQualifiedNameTriplet)getTriplet(AbstractTriplet.FULLY_QUALIFIED_NAME); + if (fqNameTriplet != null) { + return fqNameTriplet.getFullyQualifiedName(); + } + log.warn(this + " has no fully qualified name"); + return null; + } + + /** + * Sets the objects classification + * + * @param objectClass the classification of the object + * @param objectType the MOD:CA registry object type entry for the given + * object/component type of the object + * @param dataInContainer whether the data resides in the container + * @param containerHasOEG whether the container has an object environment group + * @param dataInOCD whether the data resides in a object container data structured field + */ + public void setObjectClassification( + byte objectClass, ObjectType objectType, + boolean dataInContainer, boolean containerHasOEG, boolean dataInOCD) { + addTriplet( + new ObjectClassificationTriplet( + objectClass, objectType, dataInContainer, containerHasOEG, dataInOCD)); + } + + /** + * Sets a comment on this resource + * + * @param commentString a comment string + */ + public void setComment(String commentString) { + addTriplet(new CommentTriplet(AbstractTriplet.COMMENT, commentString)); + } + +} diff --git a/src/java/org/apache/fop/afp/modca/GraphicsObject.java b/src/java/org/apache/fop/afp/modca/GraphicsObject.java index 3848ca4c3..da6079d33 100644 --- a/src/java/org/apache/fop/afp/modca/GraphicsObject.java +++ b/src/java/org/apache/fop/afp/modca/GraphicsObject.java @@ -51,11 +51,11 @@ import org.apache.fop.afp.goca.GraphicsString; public class GraphicsObject extends AbstractDataObject { /** The graphics data */ - private GraphicsData currentGraphicsData = null; + private GraphicsData data = null; /** list of objects contained within this container */ - protected List/**/ objects - = new java.util.ArrayList/**/(); + protected List/**/ objects + = new java.util.ArrayList/**/(); /** * Default constructor @@ -85,13 +85,13 @@ public class GraphicsObject extends AbstractDataObject { } /** {@inheritDoc} */ - public void addObject(PreparedAFPObject drawingOrder) { - if (currentGraphicsData == null - || (currentGraphicsData.getDataLength() + drawingOrder.getDataLength()) + public void addObject(StructuredDataObject drawingOrder) { + if (data == null + || (data.getDataLength() + drawingOrder.getDataLength()) >= GraphicsData.MAX_DATA_LEN) { newData(); } - currentGraphicsData.addObject(drawingOrder); + data.addObject(drawingOrder); } /** @@ -100,10 +100,10 @@ public class GraphicsObject extends AbstractDataObject { * @return the current graphics data */ private GraphicsData getData() { - if (this.currentGraphicsData == null) { + if (this.data == null) { return newData(); } - return this.currentGraphicsData; + return this.data; } /** @@ -112,9 +112,9 @@ public class GraphicsObject extends AbstractDataObject { * @return a newly created graphics data */ private GraphicsData newData() { - this.currentGraphicsData = factory.createGraphicsData(); - objects.add(currentGraphicsData); - return currentGraphicsData; + this.data = factory.createGraphicsData(); + objects.add(data); + return data; } /** @@ -284,18 +284,18 @@ public class GraphicsObject extends AbstractDataObject { * Begins a graphics area (start of fill) */ public void beginArea() { - if (currentGraphicsData == null) { + if (data == null) { newData(); } - currentGraphicsData.beginArea(); + data.beginArea(); } /** * Ends a graphics area (end of fill) */ public void endArea() { - if (currentGraphicsData != null) { - currentGraphicsData.endArea(); + if (data != null) { + data.endArea(); } } diff --git a/src/java/org/apache/fop/afp/modca/IncludeObject.java b/src/java/org/apache/fop/afp/modca/IncludeObject.java index c0ab5c640..2dff6bd87 100644 --- a/src/java/org/apache/fop/afp/modca/IncludeObject.java +++ b/src/java/org/apache/fop/afp/modca/IncludeObject.java @@ -238,9 +238,7 @@ public class IncludeObject extends AbstractNamedAFPObject { os.write(data); // Write triplet for FQN internal/external object reference - if (tripletData != null) { - os.write(tripletData); - } + writeTriplets(os); } private String getObjectTypeName() { diff --git a/src/java/org/apache/fop/afp/modca/MapCodedFont.java b/src/java/org/apache/fop/afp/modca/MapCodedFont.java index 159aa200a..01e9abc6f 100644 --- a/src/java/org/apache/fop/afp/modca/MapCodedFont.java +++ b/src/java/org/apache/fop/afp/modca/MapCodedFont.java @@ -41,7 +41,7 @@ import org.apache.fop.afp.util.BinaryUtils; * the Map Coded Font structured field specifies a set of resource attributes * for the coded font. */ -public class MapCodedFont extends AbstractStructuredAFPObject { +public class MapCodedFont extends AbstractStructuredObject { /** * The collection of map coded fonts (maximum of 254) diff --git a/src/java/org/apache/fop/afp/modca/MapContainerData.java b/src/java/org/apache/fop/afp/modca/MapContainerData.java index dba7616a8..8411592aa 100644 --- a/src/java/org/apache/fop/afp/modca/MapContainerData.java +++ b/src/java/org/apache/fop/afp/modca/MapContainerData.java @@ -29,7 +29,7 @@ import org.apache.fop.afp.util.BinaryUtils; * The Map Container Data structured field specifies how a presentation data object * that is carried in an Object Container is mapped into its object area. */ -public class MapContainerData extends AbstractStructuredAFPObject { +public class MapContainerData extends AbstractTripletStructuredObject { /** * Main constructor @@ -45,13 +45,15 @@ public class MapContainerData extends AbstractStructuredAFPObject { byte[] data = new byte[11]; copySF(data, Type.MAP, Category.OBJECT_CONTAINER); int tripletLen = getTripletDataLength(); + byte[] len = BinaryUtils.convert(10 + tripletLen, 2); data[1] = len[0]; data[2] = len[1]; + len = BinaryUtils.convert(2 + tripletLen, 2); data[9] = len[0]; data[10] = len[1]; os.write(data); - os.write(tripletData); + writeTriplets(os); } } diff --git a/src/java/org/apache/fop/afp/modca/MapDataResource.java b/src/java/org/apache/fop/afp/modca/MapDataResource.java index 7bebc4c07..566f60ce5 100644 --- a/src/java/org/apache/fop/afp/modca/MapDataResource.java +++ b/src/java/org/apache/fop/afp/modca/MapDataResource.java @@ -28,7 +28,7 @@ import org.apache.fop.afp.util.BinaryUtils; * The Map Data Resource structured field specifies resources that are * required for presentation. */ -public class MapDataResource extends AbstractStructuredAFPObject { +public class MapDataResource extends AbstractTripletStructuredObject { /** * Main constructor @@ -53,6 +53,6 @@ public class MapDataResource extends AbstractStructuredAFPObject { data[10] = len[1]; os.write(data); - os.write(tripletData); + writeTriplets(os); } } diff --git a/src/java/org/apache/fop/afp/modca/ObjectAreaDescriptor.java b/src/java/org/apache/fop/afp/modca/ObjectAreaDescriptor.java index 21c21e39f..80d7f746f 100644 --- a/src/java/org/apache/fop/afp/modca/ObjectAreaDescriptor.java +++ b/src/java/org/apache/fop/afp/modca/ObjectAreaDescriptor.java @@ -22,11 +22,10 @@ package org.apache.fop.afp.modca; import java.io.IOException; import java.io.OutputStream; -import org.apache.commons.io.output.ByteArrayOutputStream; +import org.apache.fop.afp.modca.triplets.DescriptorPositionTriplet; import org.apache.fop.afp.modca.triplets.MeasurementUnitsTriplet; import org.apache.fop.afp.modca.triplets.ObjectAreaSizeTriplet; import org.apache.fop.afp.modca.triplets.PresentationSpaceResetMixingTriplet; -import org.apache.fop.afp.modca.triplets.Triplet; import org.apache.fop.afp.util.BinaryUtils; /** @@ -48,40 +47,27 @@ public class ObjectAreaDescriptor extends AbstractDescriptor { super(width, height, widthRes, heightRes); } - /** {@inheritDoc} */ - protected byte[] getTripletData() throws IOException { - if (tripletData == null) { - ByteArrayOutputStream baos = new ByteArrayOutputStream(); - - // Specifies the associated ObjectAreaPosition structured field - final byte oapId = 0x01; - Triplet triplet = new Triplet(Triplet.DESCRIPTOR_POSITION, oapId); - triplet.writeToStream(baos); - - triplet = new MeasurementUnitsTriplet(widthRes, heightRes); - triplet.writeToStream(baos); - - triplet = new ObjectAreaSizeTriplet(width, height); - triplet.writeToStream(baos); - - triplet = new PresentationSpaceResetMixingTriplet( - PresentationSpaceResetMixingTriplet.NOT_RESET); - triplet.writeToStream(baos); - - this.tripletData = baos.toByteArray(); - } - return this.tripletData; - } + private static final byte oapId = 0x01; /** {@inheritDoc} */ - public void writeStart(OutputStream os) throws IOException { - super.writeStart(os); + public void writeToStream(OutputStream os) throws IOException { + // add triplets + addTriplet(new DescriptorPositionTriplet(oapId)); + addTriplet(new MeasurementUnitsTriplet(widthRes, heightRes)); + addTriplet(new ObjectAreaSizeTriplet(width, height)); + addTriplet(new PresentationSpaceResetMixingTriplet( + PresentationSpaceResetMixingTriplet.NOT_RESET)); + byte[] data = new byte[9]; copySF(data, Type.DESCRIPTOR, Category.OBJECT_AREA); - byte[] len = BinaryUtils.convert(data.length + tripletData.length - 1, 2); + + int tripletDataLength = getTripletDataLength(); + byte[] len = BinaryUtils.convert(data.length + tripletDataLength, 2); data[1] = len[0]; // Length data[2] = len[1]; os.write(data); + + writeTriplets(os); } } diff --git a/src/java/org/apache/fop/afp/modca/PreprocessPresentationObject.java b/src/java/org/apache/fop/afp/modca/PreprocessPresentationObject.java index b1f95e236..279b31201 100644 --- a/src/java/org/apache/fop/afp/modca/PreprocessPresentationObject.java +++ b/src/java/org/apache/fop/afp/modca/PreprocessPresentationObject.java @@ -5,9 +5,9 @@ * 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. @@ -27,24 +27,24 @@ import org.apache.fop.afp.util.BinaryUtils; /** * The Preprocess Presentation Object structured field specifies presentation - * parameters for a data object that has been mapped as a resource. + * parameters for a data object that has been mapped as a resource. */ -public class PreprocessPresentationObject extends AbstractStructuredAFPObject { +public class PreprocessPresentationObject extends AbstractTripletStructuredObject { private static final byte TYPE_OTHER = (byte)0x92; private static final byte TYPE_OVERLAY = (byte)0xDF; private static final byte TYPE_IMAGE = (byte)0xFB; - + private byte objType = TYPE_OTHER; private byte objOrent = 0; // object always processed at 0 degree orientation private int objXOffset = -1; private int objYOffset = -1; - + /** * Main constructor - * - * @param prePresObj the presentation object to be preprocessed + * + * @param prePresObj the presentation object to be preprocessed */ - public PreprocessPresentationObject(AbstractStructuredAFPObject prePresObj) { + public PreprocessPresentationObject(AbstractTripletStructuredObject prePresObj) { if (prePresObj instanceof ImageObject || prePresObj instanceof Overlay) { if (prePresObj instanceof ImageObject) { this.objType = TYPE_IMAGE; @@ -52,40 +52,40 @@ public class PreprocessPresentationObject extends AbstractStructuredAFPObject { this.objType = TYPE_OVERLAY; } setFullyQualifiedName( - FullyQualifiedNameTriplet.TYPE_BEGIN_RESOURCE_OBJECT_REF, + FullyQualifiedNameTriplet.TYPE_BEGIN_RESOURCE_OBJECT_REF, FullyQualifiedNameTriplet.FORMAT_CHARSTR, prePresObj.getFullyQualifiedName()); } else { this.objType = TYPE_OTHER; } } - + public static final byte ORIENTATION_ZERO_DEGREES = 1; public static final byte ORIENTATION_90_DEGREES = 2; public static final byte ORIENTATION_180_DEGREES = 4; public static final byte ORIENTATION_270_DEGREES = 8; - + /** * Sets the object orientations relative to media leading edge - * + * * @param orientation the object orientations relative to media leading edge */ public void setOrientation(byte orientation) { - objOrent = (byte)orientation; + objOrent = orientation; } - + /** * Sets the X axis origin for object content - * + * * @param xOffset the X axis origin for object content */ public void setXOffset(int xOffset) { - this.objXOffset = xOffset; + this.objXOffset = xOffset; } - + /** * Sets the Y axis origin for object content - * + * * @param yOffset the Y axis origin for object content */ public void setYOffset(int yOffset) { @@ -105,7 +105,7 @@ public class PreprocessPresentationObject extends AbstractStructuredAFPObject { os.write(data); } - + /** {@inheritDoc} */ public void writeContent(OutputStream os) throws IOException { byte[] data = new byte[12]; @@ -137,7 +137,7 @@ public class PreprocessPresentationObject extends AbstractStructuredAFPObject { data[11] = (byte)0xFF; // YocaOset } os.write(data); - + // Triplets super.writeContent(os); } diff --git a/src/java/org/apache/fop/afp/modca/PresentationEnvironmentControl.java b/src/java/org/apache/fop/afp/modca/PresentationEnvironmentControl.java index 49b33c0ef..a2ebe22f6 100644 --- a/src/java/org/apache/fop/afp/modca/PresentationEnvironmentControl.java +++ b/src/java/org/apache/fop/afp/modca/PresentationEnvironmentControl.java @@ -22,7 +22,6 @@ package org.apache.fop.afp.modca; import java.io.IOException; import java.io.OutputStream; -import org.apache.fop.afp.modca.triplets.Triplet; import org.apache.fop.afp.util.BinaryUtils; /** @@ -30,7 +29,7 @@ import org.apache.fop.afp.util.BinaryUtils; * affect the rendering of presentation data and the appearance that is to be assumed * by the presentation device. */ -public class PresentationEnvironmentControl extends AbstractStructuredAFPObject { +public class PresentationEnvironmentControl extends AbstractTripletStructuredObject { /** * Main constructor @@ -38,27 +37,6 @@ public class PresentationEnvironmentControl extends AbstractStructuredAFPObject public PresentationEnvironmentControl() { } - /** - * Sets the object offset - */ - public void setObjectOffset() { - addTriplet(new ObjectOffsetTriplet()); - } - - /** - * Sets the rendering intent - */ - public void setRenderingIntent() { - addTriplet(new RenderingIntentTriplet()); - } - - /** - * Sets the device appearance - */ - public void setDeviceAppearance() { - addTriplet(new DeviceAppearanceTriplet()); - } - /** {@inheritDoc} */ public void writeToStream(OutputStream os) throws IOException { byte[] data = new byte[11]; @@ -71,27 +49,50 @@ public class PresentationEnvironmentControl extends AbstractStructuredAFPObject data[10] = 0x00; // Reserved; must be zero os.write(data); - os.write(tripletData); + + writeTriplets(os); } +// /** +// * Sets the object offset +// */ +// public void setObjectOffset() { +// addTriplet(new ObjectOffsetTriplet()); +// } +// +// /** +// * Sets the rendering intent +// */ +// public void setRenderingIntent() { +// addTriplet(new RenderingIntentTriplet()); +// } +// +// /** +// * Sets the device appearance +// */ +// public void setDeviceAppearance() { +// addTriplet(new DeviceAppearanceTriplet()); +// } + + // TODO - private class DeviceAppearanceTriplet extends Triplet { - public DeviceAppearanceTriplet() { - super(Triplet.DEVICE_APPEARANCE); - } - } +// private class DeviceAppearanceTriplet extends AbstractTriplet { +// public DeviceAppearanceTriplet() { +// super(AbstractTriplet.DEVICE_APPEARANCE); +// } +// } // TODO - private class RenderingIntentTriplet extends Triplet { - public RenderingIntentTriplet() { - super(Triplet.RENDERING_INTENT); - } - } +// private class RenderingIntentTriplet extends AbstractTriplet { +// public RenderingIntentTriplet() { +// super(AbstractTriplet.RENDERING_INTENT); +// } +// } // TODO - private class ObjectOffsetTriplet extends Triplet { - public ObjectOffsetTriplet() { - super(Triplet.OBJECT_OFFSET); - } - } +// private class ObjectOffsetTriplet extends AbstractTriplet { +// public ObjectOffsetTriplet() { +// super(AbstractTriplet.OBJECT_OFFSET); +// } +// } } diff --git a/src/java/org/apache/fop/afp/modca/Registry.java b/src/java/org/apache/fop/afp/modca/Registry.java index 23c72cd39..481a72afd 100644 --- a/src/java/org/apache/fop/afp/modca/Registry.java +++ b/src/java/org/apache/fop/afp/modca/Registry.java @@ -30,6 +30,7 @@ public final class Registry { /** IOB supported object types */ private static final byte COMPID_IOCA_FS10 = 5; private static final byte COMPID_IOCA_FS11 = 11; + private static final byte COMPID_IOCA_FS40 = 55; private static final byte COMPID_IOCA_FS45 = 12; private static final byte COMPID_EPS = 13; private static final byte COMPID_TIFF = 14; @@ -38,6 +39,9 @@ public final class Registry { private static final byte COMPID_PDF_SINGLE_PAGE = 25; private static final byte COMPID_PCL_PAGE_OBJECT = 34; + private static final byte COMPID_TRUETYPE_OPENTYPE_FONT_RESOURCE_OBJECT = 51; + private static final byte COMPID_TRUETYPE_OPENTYPE_FONT_COLLECTION_RESOURCE_OBJECT = 53; + /** mime type entry mapping */ private final java.util.Map/**/ mimeObjectTypeMap = Collections.synchronizedMap( @@ -91,6 +95,16 @@ public final class Registry { MimeConstants.MIME_AFP_IOCA_FS11 ) ); +// mimeObjectTypeMap.put( +// MimeConstants.MIME_AFP_IOCA_FS40, +// new ObjectType( +// COMPID_IOCA_FS40, +// new byte[] {0x06, 0x07, 0x2B, 0x12, 0x00, 0x04, 0x01, 0x01, 0x37}, +// "IOCA FS40", +// true, +// MimeConstants.MIME_AFP_IOCA_FS40 +// ) +//); mimeObjectTypeMap.put( MimeConstants.MIME_AFP_IOCA_FS45, new ObjectType( @@ -160,6 +174,26 @@ public final class Registry { MimeConstants.MIME_PCL ) ); +// mimeObjectTypeMap.put( +// null, +// new ObjectType( +// COMPID_TRUETYPE_OPENTYPE_FONT_RESOURCE_OBJECT, +// new byte[] {0x06, 0x07, 0x2B, 0x12, 0x00, 0x04, 0x01, 0x01, 0x33}, +// "TrueType/OpenType Font Resource Object", +// true, +// null +// ) +// ); +// mimeObjectTypeMap.put( +// null, +// new ObjectType( +// COMPID_TRUETYPE_OPENTYPE_FONT_COLLECTION_RESOURCE_OBJECT, +// new byte[] {0x06, 0x07, 0x2B, 0x12, 0x00, 0x04, 0x01, 0x01, 0x35}, +// "TrueType/OpenType Font Collection Resource Object", +// true, +// null +// ) +// ); } /** @@ -193,11 +227,11 @@ public final class Registry { */ public ObjectType(byte componentId, byte[] oid, String name, boolean includable, String mimeType) { + this.componentId = componentId; + this.oid = oid; this.name = name; this.includable = includable; this.mimeType = mimeType; - this.componentId = componentId; - this.oid = oid; } /** diff --git a/src/java/org/apache/fop/afp/modca/ResourceObject.java b/src/java/org/apache/fop/afp/modca/ResourceObject.java index a5b551e07..0f555a42e 100644 --- a/src/java/org/apache/fop/afp/modca/ResourceObject.java +++ b/src/java/org/apache/fop/afp/modca/ResourceObject.java @@ -22,14 +22,50 @@ package org.apache.fop.afp.modca; import java.io.IOException; import java.io.OutputStream; -import org.apache.fop.afp.modca.triplets.Triplet; +import org.apache.fop.afp.modca.triplets.ResourceObjectTypeTriplet; import org.apache.fop.afp.util.BinaryUtils; /** * This resource structured field begins an envelope that is used to carry * resource objects in print-file-level (external) resource groups. */ -public class ResourceObject extends AbstractPreparedAFPObject { +public class ResourceObject extends AbstractNamedAFPObject { + + /** graphics object type */ + public static final byte TYPE_GRAPHIC = 0x03; + + /** barcode object type */ + public static final byte TYPE_BARCODE = 0x05; + + /** image object type */ + public static final byte TYPE_IMAGE = 0x06; + + /** font character set type */ + public static final byte TYPE_FONT_CHARACTER_SET = 0x40; + + /** code page type */ + public static final byte TYPE_CODE_PAGE = 0x41; + + /** coded font type */ + public static final byte TYPE_CODED_FONT = 0x42; + + /** object container type */ + public static final byte TYPE_OBJECT_CONTAINER = (byte) 0x92; + + /** document object type */ + public static final byte TYPE_DOCUMENT = (byte) 0xA8; + + /** page segment object type */ + public static final byte TYPE_PAGE_SEGMENT = (byte) 0xFB; + + /** overlay object type */ + public static final byte TYPE_OVERLAY_OBJECT = (byte) 0xFC; + + /** page def type */ + public static final byte TYPE_PAGEDEF = (byte) 0xFD; + + /** form def type */ + public static final byte TYPE_FORMDEF = (byte) 0xFE; private AbstractNamedAFPObject namedObject; @@ -45,10 +81,10 @@ public class ResourceObject extends AbstractPreparedAFPObject { /** * Sets the data object referenced by this resource object * - * @param obj the named data object + * @param namedObject the named data object */ - public void setDataObject(AbstractNamedAFPObject obj) { - this.namedObject = obj; + public void setDataObject(AbstractNamedAFPObject namedObject) { + this.namedObject = namedObject; } /** @@ -78,11 +114,13 @@ public class ResourceObject extends AbstractPreparedAFPObject { data[18] = 0x00; // Reserved os.write(data); + + // Write triplets + writeTriplets(os); } /** {@inheritDoc} */ protected void writeContent(OutputStream os) throws IOException { - super.writeContent(os); // write triplets if (namedObject != null) { namedObject.writeToStream(os); } @@ -109,60 +147,4 @@ public class ResourceObject extends AbstractPreparedAFPObject { getTriplets().add(new ResourceObjectTypeTriplet(type)); } - /** graphics object type */ - public static final byte TYPE_GRAPHIC = 0x03; - - /** barcode object type */ - public static final byte TYPE_BARCODE = 0x05; - - /** image object type */ - public static final byte TYPE_IMAGE = 0x06; - - /** font character set type */ - public static final byte TYPE_FONT_CHARACTER_SET = 0x40; - - /** code page type */ - public static final byte TYPE_CODE_PAGE = 0x41; - - /** coded font type */ - public static final byte TYPE_CODED_FONT = 0x42; - - /** object container type */ - public static final byte TYPE_OBJECT_CONTAINER = (byte) 0x92; - - /** document object type */ - public static final byte TYPE_DOCUMENT = (byte) 0xA8; - - /** page segment object type */ - public static final byte TYPE_PAGE_SEGMENT = (byte) 0xFB; - - /** overlay object type */ - public static final byte TYPE_OVERLAY_OBJECT = (byte) 0xFC; - - /** page def type */ - public static final byte TYPE_PAGEDEF = (byte) 0xFD; - - /** form def type */ - public static final byte TYPE_FORMDEF = (byte) 0xFE; - - - /** resource object type triplet */ - private class ResourceObjectTypeTriplet extends Triplet { - - private static final byte RESOURCE_OBJECT = 0x21; - - /** - * Main constructor - * - * @param objectType the resource object type - */ - public ResourceObjectTypeTriplet(byte objectType) { - super(RESOURCE_OBJECT, - new byte[] { - objectType, - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 // Constant Data - } - ); - } - } } diff --git a/src/java/org/apache/fop/afp/modca/PreparedAFPObject.java b/src/java/org/apache/fop/afp/modca/StructuredDataObject.java similarity index 82% rename from src/java/org/apache/fop/afp/modca/PreparedAFPObject.java rename to src/java/org/apache/fop/afp/modca/StructuredDataObject.java index 2771515bf..f95810ff2 100644 --- a/src/java/org/apache/fop/afp/modca/PreparedAFPObject.java +++ b/src/java/org/apache/fop/afp/modca/StructuredDataObject.java @@ -20,15 +20,14 @@ package org.apache.fop.afp.modca; /** - * An AFP object which is able to know its own data length before write() + * An AFP object which is able to know its own data length before writeToStream() */ -public interface PreparedAFPObject { +public interface StructuredDataObject { /** - * Returns the current data length of this container + * Returns the data length of this structured field * - * @return the current data length of this container including - * all enclosed GOCA drawing objects + * @return the data length of this structured field */ int getDataLength(); } \ No newline at end of file diff --git a/src/java/org/apache/fop/afp/modca/triplets/Triplet.java b/src/java/org/apache/fop/afp/modca/triplets/AbstractTriplet.java similarity index 74% rename from src/java/org/apache/fop/afp/modca/triplets/Triplet.java rename to src/java/org/apache/fop/afp/modca/triplets/AbstractTriplet.java index b7f51466b..63914eb73 100644 --- a/src/java/org/apache/fop/afp/modca/triplets/Triplet.java +++ b/src/java/org/apache/fop/afp/modca/triplets/AbstractTriplet.java @@ -19,17 +19,13 @@ package org.apache.fop.afp.modca.triplets; -import java.io.IOException; -import java.io.OutputStream; -import java.io.UnsupportedEncodingException; - -import org.apache.fop.afp.AFPConstants; import org.apache.fop.afp.Streamable; +import org.apache.fop.afp.modca.StructuredDataObject; /** * A simple implementation of a MOD:CA triplet */ -public class Triplet implements Streamable { +public abstract class AbstractTriplet implements Streamable, StructuredDataObject { public static final byte CODED_GRAPHIC_CHARACTER_SET_GLOBAL_IDENTIFIER = 0x01; /** Triplet identifiers */ @@ -89,59 +85,17 @@ public class Triplet implements Streamable { public static final byte DEVICE_APPEARANCE = (byte)0x97; /** the triplet identifier */ - private final byte id; - - /** the triplet's data contents */ - private byte[] data; - - /** - * Main constructor - * - * @param id the triplet identifier (see static definitions above) - * @param data the data item contained in this triplet - */ - public Triplet(byte id, byte[] data) { - this(id); - setData(data); - } + protected final byte id; /** * Constructor * * @param id the triplet identifier (see static definitions above) */ - public Triplet(byte id) { + public AbstractTriplet(byte id) { this.id = id; } - /** - * Constructor - * - * @param id the triplet identifier (see static definitions above) - * @param content the content byte data - */ - public Triplet(byte id, byte content) { - this(id, new byte[] {content}); - } - - /** - * Constructor - * - * @param id the triplet identifier (see static definitions above) - * @param data the data item (in String form) contained in this triplet - * @throws UnsupportedEncodingException EBCIDIC encoding is not supported - */ - public Triplet(byte id, String data) throws UnsupportedEncodingException { - this(id, data.getBytes(AFPConstants.EBCIDIC_ENCODING)); - } - - /** {@inheritDoc} */ - public void writeToStream(OutputStream os) throws IOException { - os.write((byte)data.length + 2); - os.write(id); - os.write(data); - } - /** * Returns the triplet identifier * @@ -152,11 +106,15 @@ public class Triplet implements Streamable { } /** - * Sets the data contents of this triplet + * Returns the structured triplet data array * - * @param data the data contents + * @return the structured triplet data array */ - protected void setData(byte[] data) { - this.data = data; + public byte[] getData() { + int dataLen = getDataLength(); + byte[] data = new byte[dataLen]; + data[0] = (byte)dataLen; + data[1] = id; + return data; } } diff --git a/src/java/org/apache/fop/afp/modca/triplets/CommentTriplet.java b/src/java/org/apache/fop/afp/modca/triplets/CommentTriplet.java new file mode 100644 index 000000000..9b15de8c1 --- /dev/null +++ b/src/java/org/apache/fop/afp/modca/triplets/CommentTriplet.java @@ -0,0 +1,52 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +/* $Id$ */ + +package org.apache.fop.afp.modca.triplets; + +import java.io.IOException; +import java.io.OutputStream; + +import org.apache.fop.afp.AFPConstants; + +/** + * An inline comment triplet. + */ +public class CommentTriplet extends AbstractTriplet { + + private final String commentString; + + public CommentTriplet(byte id, String commentString) { + super(id); + this.commentString = commentString; + } + + /** {@inheritDoc} */ + public int getDataLength() { + return 2 + commentString.length(); + } + + /** {@inheritDoc} */ + public void writeToStream(OutputStream os) throws IOException { + byte[] data = getData(); + byte[] strData = commentString.getBytes(AFPConstants.EBCIDIC_ENCODING); + System.arraycopy(strData, 0, data, 2, strData.length); + os.write(data); + } + +} diff --git a/src/java/org/apache/fop/afp/modca/triplets/DescriptorPositionTriplet.java b/src/java/org/apache/fop/afp/modca/triplets/DescriptorPositionTriplet.java new file mode 100644 index 000000000..61b279b5c --- /dev/null +++ b/src/java/org/apache/fop/afp/modca/triplets/DescriptorPositionTriplet.java @@ -0,0 +1,50 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +/* $Id$ */ + +package org.apache.fop.afp.modca.triplets; + +import java.io.IOException; +import java.io.OutputStream; + +public class DescriptorPositionTriplet extends AbstractTriplet { + + private final byte oapId; + + /** + * Main constructor + * + * @param oapId the object area position id + */ + public DescriptorPositionTriplet(byte oapId) { + super(DESCRIPTOR_POSITION); + this.oapId = oapId; + } + + /** {@inheritDoc} */ + public int getDataLength() { + return 3; + } + + /** {@inheritDoc} */ + public void writeToStream(OutputStream os) throws IOException { + byte[] data = getData(); + data[2] = oapId; + os.write(data); + } +} diff --git a/src/java/org/apache/fop/afp/modca/triplets/ExtendedResourceLocalIdentifierTriplet.java b/src/java/org/apache/fop/afp/modca/triplets/ExtendedResourceLocalIdentifierTriplet.java index a6059d3ec..af9452fcb 100644 --- a/src/java/org/apache/fop/afp/modca/triplets/ExtendedResourceLocalIdentifierTriplet.java +++ b/src/java/org/apache/fop/afp/modca/triplets/ExtendedResourceLocalIdentifierTriplet.java @@ -19,6 +19,9 @@ package org.apache.fop.afp.modca.triplets; +import java.io.IOException; +import java.io.OutputStream; + import org.apache.fop.afp.util.BinaryUtils; /** @@ -27,7 +30,7 @@ import org.apache.fop.afp.util.BinaryUtils; * resource name by a map structured field, such as a Map Data Resource structured * field, or a Map Media Type structured field. */ -public class ExtendedResourceLocalIdentifierTriplet extends Triplet { +public class ExtendedResourceLocalIdentifierTriplet extends AbstractTriplet { /** the image resource type */ public static final byte TYPE_IMAGE_RESOURCE = 0x10; @@ -38,6 +41,12 @@ public class ExtendedResourceLocalIdentifierTriplet extends Triplet { /** the retired value type */ public static final byte TYPE_MEDIA_RESOURCE = 0x40; + /** the resource type */ + private final byte type; + + /** the resource local id */ + private final int localId; + /** * Main constructor * @@ -45,11 +54,22 @@ public class ExtendedResourceLocalIdentifierTriplet extends Triplet { * @param localId the resource local id */ public ExtendedResourceLocalIdentifierTriplet(byte type, int localId) { - super(Triplet.EXTENDED_RESOURCE_LOCAL_IDENTIFIER); - byte[] data = new byte[5]; - data[0] = type; - byte[] resLID = BinaryUtils.convert(localId, 4); - System.arraycopy(resLID, 0, data, 1, resLID.length); - super.setData(data); + super(AbstractTriplet.EXTENDED_RESOURCE_LOCAL_IDENTIFIER); + this.type = type; + this.localId = localId; + } + + /** {@inheritDoc} */ + public void writeToStream(OutputStream os) throws IOException { + byte[] data = getData(); + data[2] = type; + byte[] resLID = BinaryUtils.convert(localId, 4); // 4 bytes + System.arraycopy(resLID, 0, data, 3, resLID.length); + os.write(data); + } + + /** {@inheritDoc} */ + public int getDataLength() { + return 7; } } diff --git a/src/java/org/apache/fop/afp/modca/triplets/FullyQualifiedNameTriplet.java b/src/java/org/apache/fop/afp/modca/triplets/FullyQualifiedNameTriplet.java index a949124c8..55653457c 100644 --- a/src/java/org/apache/fop/afp/modca/triplets/FullyQualifiedNameTriplet.java +++ b/src/java/org/apache/fop/afp/modca/triplets/FullyQualifiedNameTriplet.java @@ -5,9 +5,9 @@ * 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. @@ -19,105 +19,107 @@ package org.apache.fop.afp.modca.triplets; +import java.io.IOException; +import java.io.OutputStream; import java.io.UnsupportedEncodingException; import org.apache.fop.afp.AFPConstants; /** * A Fully Qualified Name triplet enable the identification and referencing of - * objects using Gloabl Identifiers (GIDs). + * objects using Gloabl Identifiers (GIDs). */ -public class FullyQualifiedNameTriplet extends Triplet { +public class FullyQualifiedNameTriplet extends AbstractTriplet { // Specifies how the GID will be used /** This GID replaces the first parameter in the structured field that contains a GID name. */ public static final byte TYPE_REPLACE_FIRST_GID_NAME = 0x01; - + /** This triplet contains the name of a font family. */ public static final byte TYPE_FONT_FAMILY_NAME = 0x07; - + /** This triplet contains the name of a font typeface. */ public static final byte TYPE_FONT_TYPEFACE_NAME = 0x08; - + /** This triplet specifies a reference to the MO:DCA resource hierarchy. */ public static final byte TYPE_MODCA_RESOURCE_HIERARCHY_REF = 0x09; - + /** The triplet contains a GID reference to a begin resource group structured field. */ public static final byte TYPE_BEGIN_RESOURCE_GROUP_REF = 0x0A; - + /** The triplet contains a GID reference to a document attribute. */ public static final byte TYPE_ATTRIBUTE_GID = 0x0B; - + /** The triplet contains the GID of a process element. */ public static final byte TYPE_PROCESS_ELEMENT_GID = 0x0C; - + /** The triplet contains a reference to a begin page group structured field. */ public static final byte TYPE_BEGIN_PAGE_GROUP_REF = 0x0D; - + /** The triplet contains a reference to a media type. */ public static final byte TYPE_MEDIA_TYPE_REF = 0x11; - + /** The triplet contains a reference to a color management resource. */ public static final byte TYPE_COLOR_MANAGEMENT_RESOURCE_REF = 0x41; /** The triplet contains a reference to a data-object font file that defines a base font. */ public static final byte TYPE_DATA_OBJECT_FONT_BASE_FONT_ID = 0x6E; - + /** The triplet contains a reference to a data-object font file that defines a linked font. */ public static final byte TYPE_DATA_OBJECT_FONT_LINKED_FONT_ID = 0x7E; - - /** The triplet contains a reference to a begin document structured field. */ + + /** The triplet contains a reference to a begin document structured field. */ public static final byte TYPE_BEGIN_DOCUMENT_REF = (byte)0x83; - + /** * The triplet contains a reference to a begin structured field associated with a resource; * or contains a GID reference to a coded font. */ public static final byte TYPE_BEGIN_RESOURCE_OBJECT_REF = (byte)0x84; - + /** * The triplet contains a GID reference to a code page that specifies the code points and * graphic character names for a coded font. */ public static final byte TYPE_CODE_PAGE_NAME_REF = (byte)0x85; - - /** + + /** * The triplet contains a GID name reference to a font character set that specifies * a set of graphics characters. */ public static final byte TYPE_FONT_CHARSET_NAME_REF = (byte)0x86; - + /** The triplet contains a GID reference to a begin page structured field. */ public static final byte TYPE_BEGIN_PAGE_REF = (byte)0x87; - + /** The triplet contains a GID reference to a begin medium map structured field. */ public static final byte TYPE_BEGIN_MEDIUM_MAP_REF = (byte)0x8D; - + /** * The triplet contains a GID reference to a coded font, which identifies a specific * code page and a specific font character set. */ public static final byte TYPE_CODED_FONT_NAME_REF = (byte)0x8E; - + /** The triplet contains a GID reference to a begin document index structured field. */ - public static final byte TYPE_BEGIN_DOCUMENT_INDEX_REF = (byte)0x98; - + public static final byte TYPE_BEGIN_DOCUMENT_INDEX_REF = (byte)0x98; + /** The triplet contains a GID reference to a begin overlay structured field. */ public static final byte TYPE_BEGIN_OVERLAY_REF = (byte)0xB0; - + /** The triplet contains a GID reference to a resource used by a data object. */ public static final byte TYPE_DATA_OBJECT_INTERNAL_RESOURCE_REF = (byte)0xBE; - + /** The triplet contains a GID reference to an index element structured field. */ public static final byte TYPE_INDEX_ELEMENT_GID = (byte)0xCA; - + /** * The triplet contains a reference to other object data which may or may * not be defined by an IBM presentation architecture. */ public static final byte TYPE_OTHER_OBJECT_DATA_REF = (byte)0xCE; - + /** * The triplet contains a reference to a resource used by a data object. * The GID may be a filename or any other identifier associated with the @@ -127,44 +129,71 @@ public class FullyQualifiedNameTriplet extends Triplet { */ public static final byte TYPE_DATA_OBJECT_EXTERNAL_RESOURCE_REF = (byte)0xDE; - + // GID Format - + /** The GID is a character encoded name. */ public static final byte FORMAT_CHARSTR = (byte)0x00; - + /** the GID is a ASN.1 object identifier (OID). */ public static final byte FORMAT_OID = (byte)0x10; /** the GID is a uniform resource locator (URL). */ public static final byte FORMAT_URL = (byte)0x20; - - private String fqName; - - /** - * @return the actual fully qualified name of this triplet - */ - public String getFullyQualifiedName() { - return fqName; - } + /** the fully qualified name type */ + private final byte type; + + /** the fully qualified name format */ + private final byte format; + + /** the actual fully qualified name */ + private final String fqName; /** * Main constructor - * + * * @param type the fully qualified name type * @param format the fully qualified name format * @param fqName the fully qualified name */ public FullyQualifiedNameTriplet(byte type, byte format, String fqName) { super(FULLY_QUALIFIED_NAME); - + this.type = type; + this.format = format; this.fqName = fqName; + } + /** + * Returns the actual fully qualified name + * + * @return the actual fully qualified name + */ + public String getFullyQualifiedName() { + return fqName; + } + + /** {@inheritDoc} */ + public String toString() { + return this.fqName; + } + + /** {@inheritDoc} */ + public int getDataLength() { + return 4 + fqName.length(); + } + + /** {@inheritDoc} */ + public void writeToStream(OutputStream os) throws IOException { + byte[] data = getData(); + data[2] = type; + data[3] = format; + + // FQName byte[] fqNameBytes; - String encoding = AFPConstants.EBCIDIC_ENCODING; + String encoding = AFPConstants.EBCIDIC_ENCODING; if (format == FORMAT_URL) { - encoding = AFPConstants.US_ASCII_ENCODING; + encoding = AFPConstants.US_ASCII_ENCODING; } try { fqNameBytes = fqName.getBytes(encoding); @@ -172,20 +201,8 @@ public class FullyQualifiedNameTriplet extends Triplet { throw new IllegalArgumentException( encoding + " encoding failed"); } + System.arraycopy(fqNameBytes, 0, data, 4, fqNameBytes.length); - byte[] data = new byte[2 + fqNameBytes.length]; - data[0] = type; - data[1] = format; - // FQName - System.arraycopy(fqNameBytes, 0, data, 2, fqNameBytes.length); - - super.setData(data); - } - - /** - * {@inheritDoc} - */ - public String toString() { - return this.fqName; + os.write(data); } } \ No newline at end of file diff --git a/src/java/org/apache/fop/afp/modca/triplets/MappingOptionTriplet.java b/src/java/org/apache/fop/afp/modca/triplets/MappingOptionTriplet.java index 9fe6dad79..0d20d0227 100644 --- a/src/java/org/apache/fop/afp/modca/triplets/MappingOptionTriplet.java +++ b/src/java/org/apache/fop/afp/modca/triplets/MappingOptionTriplet.java @@ -5,9 +5,9 @@ * 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. @@ -19,10 +19,13 @@ package org.apache.fop.afp.modca.triplets; +import java.io.IOException; +import java.io.OutputStream; + /** * Specifies the mapping of data object presentation space to object area */ -public class MappingOptionTriplet extends Triplet { +public class MappingOptionTriplet extends AbstractTriplet { /** * the data object is placed in the upper left corner, all data must be presented * within the object area extents @@ -34,35 +37,51 @@ public class MappingOptionTriplet extends Triplet { * the object area extents will be presented but data that falls outside will not be presented */ public static final byte POSITION_AND_TRIM = 0x10; - + /** * the data object is centred and symmetrically scaled up or down * while preserving aspect ratio */ public static final byte SCALE_TO_FIT = 0x20; - + /** * the data object is centred, all data that falls within the object area extents * will be presented but data that falls outside will not be presented */ public static final byte CENTER_AND_TRIM = 0x30; - + // public static final byte MIGRATION_MAPPING_1 = 0x41; // public static final byte MIGRATION_MAPPING_2 = 0x42; // public static final byte MIGRATION_MAPPING_3 = 0x50; - + /** the data object is centred, aspect ratio is not always preserved */ public static final byte SCALE_TO_FILL = 0x60; - + /** used to map ip3i print data objects */ public static final byte UP3I_PRINT_DATA = 0x70; + private final byte mapValue; + /** * Main constructor - * + * * @param mapValue the mapping option to use */ public MappingOptionTriplet(byte mapValue) { - super(Triplet.MAPPING_OPTION, mapValue); + super(AbstractTriplet.MAPPING_OPTION); + this.mapValue = mapValue; + } + + /** {@inheritDoc} */ + public int getDataLength() { + return 3; + } + + /** {@inheritDoc} */ + public void writeToStream(OutputStream os) throws IOException { + byte[] data = getData(); + data[2] = mapValue; + + os.write(data); } } diff --git a/src/java/org/apache/fop/afp/modca/triplets/MeasurementUnitsTriplet.java b/src/java/org/apache/fop/afp/modca/triplets/MeasurementUnitsTriplet.java index d39dc5f96..68d3fc40c 100644 --- a/src/java/org/apache/fop/afp/modca/triplets/MeasurementUnitsTriplet.java +++ b/src/java/org/apache/fop/afp/modca/triplets/MeasurementUnitsTriplet.java @@ -5,9 +5,9 @@ * 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. @@ -19,36 +19,54 @@ package org.apache.fop.afp.modca.triplets; +import java.io.IOException; +import java.io.OutputStream; + import org.apache.fop.afp.util.BinaryUtils; /** * The Measurement Units triplet is used to specify the units of measure * for a presentation space */ -public class MeasurementUnitsTriplet extends Triplet { +public class MeasurementUnitsTriplet extends AbstractTriplet { - private static final byte TEN_INCHES = 0x00; + private static final byte TEN_INCHES = 0x00; private static final byte TEN_CM = 0x01; - + private final int xRes; + private final int yRes; + /** * Main constructor - * + * * @param xRes units per base on the x-axis * @param yRes units per base on the y-axis */ public MeasurementUnitsTriplet(int xRes, int yRes) { super(MEASUREMENT_UNITS); - //TODO: units correct? + this.xRes = xRes; + this.yRes = yRes; + } + + /** {@inheritDoc} */ + public int getDataLength() { + return 8; + } + + /** {@inheritDoc} */ + public void writeToStream(OutputStream os) throws IOException { + byte[] data = getData(); + + data[2] = TEN_INCHES; // XoaBase + data[3] = TEN_INCHES; // YoaBase + byte[] xUnits = BinaryUtils.convert(xRes * 10, 2); + data[4] = xUnits[0]; // XoaUnits (x units per unit base) + data[5] = xUnits[1]; + byte[] yUnits = BinaryUtils.convert(yRes * 10, 2); - byte[] data = new byte[] { - TEN_INCHES, // XoaBase - TEN_INCHES, // YoaBase - xUnits[0], // XoaUnits (x units per unit base) - xUnits[1], - yUnits[0], // YoaUnits (y units per unit base) - yUnits[1] - }; - super.setData(data); + data[6] = yUnits[0]; // YoaUnits (y units per unit base) + data[7] = yUnits[1]; + + os.write(data); } } diff --git a/src/java/org/apache/fop/afp/modca/triplets/ObjectAreaSizeTriplet.java b/src/java/org/apache/fop/afp/modca/triplets/ObjectAreaSizeTriplet.java index 64fba23c9..3d408639e 100644 --- a/src/java/org/apache/fop/afp/modca/triplets/ObjectAreaSizeTriplet.java +++ b/src/java/org/apache/fop/afp/modca/triplets/ObjectAreaSizeTriplet.java @@ -5,9 +5,9 @@ * 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. @@ -19,44 +19,66 @@ package org.apache.fop.afp.modca.triplets; +import java.io.IOException; +import java.io.OutputStream; + import org.apache.fop.afp.util.BinaryUtils; /** * The Object Area Size triplet is used to specify the extent of an object area * in the X and Y directions */ -public class ObjectAreaSizeTriplet extends Triplet { +public class ObjectAreaSizeTriplet extends AbstractTriplet { + + private final int x; + private final int y; + private final byte type; /** * Main constructor - * + * * @param x the object area extent for the X axis * @param y the object area extent for the Y axis * @param type the object area size type */ public ObjectAreaSizeTriplet(int x, int y, byte type) { - super(Triplet.OBJECT_AREA_SIZE); - byte[] xOASize = BinaryUtils.convert(x, 3); - byte[] yOASize = BinaryUtils.convert(y, 3); - byte[] data = new byte[] { - type, // SizeType - xOASize[0], // XoaSize - Object area extent for X axis - xOASize[1], - xOASize[2], - yOASize[0], // YoaSize - Object area extent for Y axis - yOASize[1], - yOASize[2] - }; - super.setData(data); + super(AbstractTriplet.OBJECT_AREA_SIZE); + this.x = x; + this.y = y; + this.type = type; } /** * Main constructor - * + * * @param x the object area extent for the X axis * @param y the object area extent for the Y axis */ public ObjectAreaSizeTriplet(int x, int y) { this(x, y, (byte)0x02); } + + /** {@inheritDoc} */ + public int getDataLength() { + return 9; + } + + /** {@inheritDoc} */ + public void writeToStream(OutputStream os) throws IOException { + byte[] data = getData(); + + data[2] = type; // SizeType + + byte[] xOASize = BinaryUtils.convert(x, 3); + data[3] = xOASize[0]; // XoaSize - Object area extent for X axis + data[4] = xOASize[1]; + data[5] = xOASize[2]; + + byte[] yOASize = BinaryUtils.convert(y, 3); + data[6] = yOASize[0]; // YoaSize - Object area extent for Y axis + data[7] = yOASize[1]; + data[8] = yOASize[2]; + + os.write(data); + } } diff --git a/src/java/org/apache/fop/afp/modca/triplets/ObjectByteExtentTriplet.java b/src/java/org/apache/fop/afp/modca/triplets/ObjectByteExtentTriplet.java index 355fe8288..3aaa6969a 100644 --- a/src/java/org/apache/fop/afp/modca/triplets/ObjectByteExtentTriplet.java +++ b/src/java/org/apache/fop/afp/modca/triplets/ObjectByteExtentTriplet.java @@ -19,12 +19,17 @@ package org.apache.fop.afp.modca.triplets; +import java.io.IOException; +import java.io.OutputStream; + import org.apache.fop.afp.util.BinaryUtils; /** * The Object Byte Extent triplet is used to specify the number of bytes contained in an object */ -public class ObjectByteExtentTriplet extends Triplet { +public class ObjectByteExtentTriplet extends AbstractTriplet { + + private final int byteExt; /** * Main constructor @@ -33,7 +38,19 @@ public class ObjectByteExtentTriplet extends Triplet { */ public ObjectByteExtentTriplet(int byteExt) { super(OBJECT_BYTE_EXTENT); - byte[] data = BinaryUtils.convert(byteExt, 4); - super.setData(data); + this.byteExt = byteExt; + } + + /** {@inheritDoc} */ + public int getDataLength() { + return 6; + } + + /** {@inheritDoc} */ + public void writeToStream(OutputStream os) throws IOException { + byte[] data = getData(); + byte[] extData = BinaryUtils.convert(byteExt, 4); + System.arraycopy(extData, 0, data, 2, extData.length); + os.write(data); } } diff --git a/src/java/org/apache/fop/afp/modca/triplets/ObjectClassificationTriplet.java b/src/java/org/apache/fop/afp/modca/triplets/ObjectClassificationTriplet.java index 1692339ac..346dddaab 100644 --- a/src/java/org/apache/fop/afp/modca/triplets/ObjectClassificationTriplet.java +++ b/src/java/org/apache/fop/afp/modca/triplets/ObjectClassificationTriplet.java @@ -19,7 +19,8 @@ package org.apache.fop.afp.modca.triplets; -import java.io.UnsupportedEncodingException; +import java.io.IOException; +import java.io.OutputStream; import org.apache.fop.afp.AFPConstants; import org.apache.fop.afp.modca.Registry.ObjectType; @@ -29,7 +30,7 @@ import org.apache.fop.afp.util.StringUtils; * The Object Classification is used to classify and identify object data. * The object data may or may not be defined by an IBM presentation architecture */ -public class ObjectClassificationTriplet extends Triplet { +public class ObjectClassificationTriplet extends AbstractTriplet { /** * The scope of this object is the including page or overlay @@ -64,6 +65,28 @@ public class ObjectClassificationTriplet extends Triplet { */ public static final byte CLASS_DATA_OBJECT_FONT = 0x41; + /** the object class */ + private final byte objectClass; + + /** the object type */ + private final ObjectType objectType; + + /** whether the container has an object environment group */ + private final boolean containerHasOEG; + + /** whether the data resides within the container */ + private final boolean dataInContainer; + + /** whether the data resides within the object container data */ + private final boolean dataInOCD; + + /** the object level (version) */ + private final String objectLevel; + + /** the company/organization name */ + private final String companyName; + + /** * Main constructor * @@ -79,11 +102,6 @@ public class ObjectClassificationTriplet extends Triplet { this(objectClass, objectType, dataInContainer, containerHasOEG, dataInOCD, null, null); } - - private static final int OBJECT_LEVEL_LEN = 8; - private static final int OBJECT_TYPE_NAME_LEN = 32; - private static final int COMPANY_NAME_LEN = 32; - /** * Fully parameterized constructor * @@ -100,56 +118,16 @@ public class ObjectClassificationTriplet extends Triplet { String objLev, String compName) { super(OBJECT_CLASSIFICATION); + this.objectClass = objectClass; if (objectType == null) { throw new IllegalArgumentException("MO:DCA Registry object type is null"); } - - byte[] data = new byte[94]; - data[0] = 0x00; // reserved (must be zero) - data[1] = objectClass; // ObjClass - data[2] = 0x00; // reserved (must be zero) - data[3] = 0x00; // reserved (must be zero) - - // StrucFlgs - Information on the structure of the object container - byte[] strucFlgs = getStrucFlgs(dataInContainer, containerHasOEG, dataInOCD); - data[4] = strucFlgs[0]; - data[5] = strucFlgs[1]; - - byte[] oid = objectType.getOID(); - // RegObjId - MOD:CA-registered ASN.1 OID for object type (8-23) - System.arraycopy(oid, 0, data, 6, oid.length); - - // ObjTpName - name of object type (24-55) - byte[] objTpName; - try { - objTpName = StringUtils.rpad(objectType.getName(), ' ', OBJECT_TYPE_NAME_LEN).getBytes( - AFPConstants.EBCIDIC_ENCODING); - System.arraycopy(objTpName, 0, data, 22, objTpName.length); - } catch (UnsupportedEncodingException e) { - throw new IllegalArgumentException("an encoding exception occurred"); - } - - // ObjLev - release level or version number of object type (56-63) - byte[] objectLevel; - try { - objectLevel = StringUtils.rpad(objLev, ' ', OBJECT_LEVEL_LEN).getBytes( - AFPConstants.EBCIDIC_ENCODING); - } catch (UnsupportedEncodingException e) { - throw new IllegalArgumentException("an encoding exception occurred"); - } - System.arraycopy(objectLevel, 0, data, 54, objectLevel.length); - - // CompName - name of company or organization that owns object definition (64-95) - byte[] companyName; - try { - companyName = StringUtils.rpad(compName, ' ', COMPANY_NAME_LEN).getBytes( - AFPConstants.EBCIDIC_ENCODING); - } catch (UnsupportedEncodingException e) { - throw new IllegalArgumentException("an encoding exception occurred"); - } - System.arraycopy(companyName, 0, data, 62, companyName.length); - - super.setData(data); + this.objectType = objectType; + this.dataInContainer = dataInContainer; + this.containerHasOEG = containerHasOEG; + this.dataInOCD = dataInOCD; + this.objectLevel = objLev; + this.companyName = compName; } /** @@ -161,7 +139,7 @@ public class ObjectClassificationTriplet extends Triplet { * * @return the byte value of this structure */ - public byte[] getStrucFlgs(boolean dataInContainer, boolean containerHasOEG, + public byte[] getStructureFlagsAsBytes(boolean dataInContainer, boolean containerHasOEG, boolean dataInOCD) { byte[] strucFlgs = new byte[2]; // Object Container (BOC/EOC) @@ -185,4 +163,52 @@ public class ObjectClassificationTriplet extends Triplet { strucFlgs[1] = 0x00; return strucFlgs; } + + /** {@inheritDoc} */ + public int getDataLength() { + return 95; + } + + private static final int OBJECT_LEVEL_LEN = 8; + private static final int OBJECT_TYPE_NAME_LEN = 32; + private static final int COMPANY_NAME_LEN = 32; + + /** {@inheritDoc} */ + public void writeToStream(OutputStream os) throws IOException { + byte[] data = getData(); + data[2] = 0x00; // reserved (must be zero) + data[3] = objectClass; // ObjClass + data[4] = 0x00; // reserved (must be zero) + data[5] = 0x00; // reserved (must be zero) + + // StrucFlgs - Information on the structure of the object container + byte[] structureFlagsBytes = getStructureFlagsAsBytes(dataInContainer, containerHasOEG, dataInOCD); + data[6] = structureFlagsBytes[0]; + data[7] = structureFlagsBytes[1]; + + byte[] objectIdBytes = objectType.getOID(); + // RegObjId - MOD:CA-registered ASN.1 OID for object type (8-23) + System.arraycopy(objectIdBytes, 0, data, 8, objectIdBytes.length); + + // ObjTpName - name of object type (24-55) + byte[] objectTypeNameBytes; + objectTypeNameBytes + = StringUtils.rpad(objectType.getName(), ' ', OBJECT_TYPE_NAME_LEN).getBytes( + AFPConstants.EBCIDIC_ENCODING); + System.arraycopy(objectTypeNameBytes, 0, data, 24, objectTypeNameBytes.length); + + // ObjLev - release level or version number of object type (56-63) + byte[] objectLevelBytes; + objectLevelBytes = StringUtils.rpad(objectLevel, ' ', OBJECT_LEVEL_LEN).getBytes( + AFPConstants.EBCIDIC_ENCODING); + System.arraycopy(objectLevelBytes, 0, data, 56, objectLevelBytes.length); + + // CompName - name of company or organization that owns object definition (64-95) + byte[] companyNameBytes; + companyNameBytes = StringUtils.rpad(companyName, ' ', COMPANY_NAME_LEN).getBytes( + AFPConstants.EBCIDIC_ENCODING); + System.arraycopy(companyNameBytes, 0, data, 64, companyNameBytes.length); + + os.write(data); + } } \ No newline at end of file diff --git a/src/java/org/apache/fop/afp/modca/triplets/PresentationSpaceMixingRulesTriplet.java b/src/java/org/apache/fop/afp/modca/triplets/PresentationSpaceMixingRulesTriplet.java index 0f087e4fd..b93ec5477 100644 --- a/src/java/org/apache/fop/afp/modca/triplets/PresentationSpaceMixingRulesTriplet.java +++ b/src/java/org/apache/fop/afp/modca/triplets/PresentationSpaceMixingRulesTriplet.java @@ -5,9 +5,9 @@ * 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. @@ -19,28 +19,31 @@ package org.apache.fop.afp.modca.triplets; +import java.io.IOException; +import java.io.OutputStream; + /** * This triplet is used to specify the resulting appearance when data in a new * presentation space is merged with data in an existing presentation space. */ -public class PresentationSpaceMixingRulesTriplet extends Triplet { +public class PresentationSpaceMixingRulesTriplet extends AbstractTriplet { - /** background on background mixing rule */ + /** background on background mixing rule */ public static final byte RULE_BACK_ON_BACK = 0x70; - /** background on foreground mixing rule */ + /** background on foreground mixing rule */ public static final byte RULE_BACK_ON_FORE = 0x71; - /** foreground on background mixing rule */ + /** foreground on background mixing rule */ public static final byte RULE_FORE_ON_BACK = 0x72; - /** foreground on foreground mixing rule */ + /** foreground on foreground mixing rule */ public static final byte RULE_FORE_ON_FORE = 0x73; - + /** overpaint */ public static final byte OVERPAINT = (byte)0x01; - + /** underpaint */ public static final byte UNDERPAINT = (byte)0x02; @@ -49,14 +52,29 @@ public class PresentationSpaceMixingRulesTriplet extends Triplet { /** MO:DCA default mixing */ public static final byte DEFAULT = (byte)0xFF; - - + + /** the mixing rules */ + private final byte[] rules; + /** * Main constructor - * + * * @param rules the mixing rules */ public PresentationSpaceMixingRulesTriplet(byte[] rules) { - super(PRESENTATION_SPACE_MIXING_RULE, rules); + super(PRESENTATION_SPACE_MIXING_RULE); + this.rules = rules; + } + + /** {@inheritDoc} */ + public int getDataLength() { + return 2 + rules.length; + } + + /** {@inheritDoc} */ + public void writeToStream(OutputStream os) throws IOException { + byte[] data = getData(); + System.arraycopy(rules, 0, data, 2, rules.length); + os.write(data); } } diff --git a/src/java/org/apache/fop/afp/modca/triplets/PresentationSpaceResetMixingTriplet.java b/src/java/org/apache/fop/afp/modca/triplets/PresentationSpaceResetMixingTriplet.java index 38041f140..cecd40910 100644 --- a/src/java/org/apache/fop/afp/modca/triplets/PresentationSpaceResetMixingTriplet.java +++ b/src/java/org/apache/fop/afp/modca/triplets/PresentationSpaceResetMixingTriplet.java @@ -5,9 +5,9 @@ * 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. @@ -19,30 +19,48 @@ package org.apache.fop.afp.modca.triplets; +import java.io.IOException; +import java.io.OutputStream; + /** * This triplet is used to specify the resulting appearance when data in a new * presentation space is merged with data in an existing presentation space. */ -public class PresentationSpaceResetMixingTriplet extends Triplet { +public class PresentationSpaceResetMixingTriplet extends AbstractTriplet { /** * Do not reset to the color of the medium prior to * placing data into this MO:DCA presentation space. */ public static final byte NOT_RESET = 0x00; - + /** * Reset to the color of the medium prior to placing * data into this MO:DCA presentation space. */ public static final byte RESET = 0x01; - + + private final byte backgroundMixFlag; + /** * Main constructor - * + * * @param backgroundMixFlag the background mixing flag */ public PresentationSpaceResetMixingTriplet(byte backgroundMixFlag) { - super(PRESENTATION_SPACE_RESET_MIXING, backgroundMixFlag); + super(PRESENTATION_SPACE_RESET_MIXING); + this.backgroundMixFlag = backgroundMixFlag; + } + + /** {@inheritDoc} */ + public int getDataLength() { + return 3; + } + + /** {@inheritDoc} */ + public void writeToStream(OutputStream os) throws IOException { + byte[] data = getData(); + data[2] = backgroundMixFlag; + os.write(data); } } diff --git a/src/java/org/apache/fop/afp/modca/triplets/ResourceObjectTypeTriplet.java b/src/java/org/apache/fop/afp/modca/triplets/ResourceObjectTypeTriplet.java index 784975c43..ecc12122d 100644 --- a/src/java/org/apache/fop/afp/modca/triplets/ResourceObjectTypeTriplet.java +++ b/src/java/org/apache/fop/afp/modca/triplets/ResourceObjectTypeTriplet.java @@ -5,9 +5,9 @@ * 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. @@ -19,3 +19,36 @@ package org.apache.fop.afp.modca.triplets; +import java.io.IOException; +import java.io.OutputStream; + +/** resource object type triplet */ +public class ResourceObjectTypeTriplet extends AbstractTriplet { + + private static final byte RESOURCE_OBJECT = 0x21; + + /** the resource object type */ + private final byte objectType; + + /** + * Main constructor + * + * @param objectType the resource object type + */ + public ResourceObjectTypeTriplet(byte objectType) { + super(RESOURCE_OBJECT); + this.objectType = objectType; + } + + /** {@inheritDoc} */ + public int getDataLength() { + return 10; + } + + /** {@inheritDoc} */ + public void writeToStream(OutputStream os) throws IOException { + byte[] data = getData(); + data[2] = objectType; + os.write(data); + } +} diff --git a/src/java/org/apache/fop/afp/AFPGraphicsConfiguration.java b/src/java/org/apache/fop/afp/svg/AFPGraphicsConfiguration.java similarity index 92% rename from src/java/org/apache/fop/afp/AFPGraphicsConfiguration.java rename to src/java/org/apache/fop/afp/svg/AFPGraphicsConfiguration.java index 6c6d92098..76d6396e3 100644 --- a/src/java/org/apache/fop/afp/AFPGraphicsConfiguration.java +++ b/src/java/org/apache/fop/afp/svg/AFPGraphicsConfiguration.java @@ -5,9 +5,9 @@ * 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. @@ -17,7 +17,7 @@ /* $Id$ */ -package org.apache.fop.afp; +package org.apache.fop.afp.svg; import java.awt.GraphicsDevice; import java.awt.Rectangle; @@ -77,10 +77,10 @@ public class AFPGraphicsConfiguration extends GraphicsConfiguration { /** * TODO: This should return the page bounds in Pts, * I couldn't figure out how to get this for the current - * page from the PDFDocument (this still works for now, + * page (this still works for now, * but it should be fixed...). * - * @return the bounds of the PDF document page + * @return the bounds of the page */ public Rectangle getBounds() { return null; @@ -109,10 +109,10 @@ public class AFPGraphicsConfiguration extends GraphicsConfiguration { } private static final Log log = LogFactory.getLog(AFPGraphicsConfiguration.class); - + private AffineTransform defaultTransform = null; private AffineTransform normalizingTransform = null; - private GraphicsDevice graphicsDevice = null; + private final GraphicsDevice graphicsDevice = new AFPGraphicsDevice(this);; /** * The default transform (1:1). @@ -122,7 +122,7 @@ public class AFPGraphicsConfiguration extends GraphicsConfiguration { public AffineTransform getDefaultTransform() { log.debug("getDefaultTransform()"); if (defaultTransform == null) { - defaultTransform = new AffineTransform(); + defaultTransform = new AffineTransform(); } return defaultTransform; } @@ -142,14 +142,9 @@ public class AFPGraphicsConfiguration extends GraphicsConfiguration { return normalizingTransform; } - /** - * {@inheritDoc} - */ + /** {@inheritDoc} */ public GraphicsDevice getDevice() { log.debug("getDevice()"); - if (graphicsDevice == null) { - graphicsDevice = new AFPGraphicsDevice(this); - } return graphicsDevice; } } diff --git a/src/java/org/apache/fop/afp/AFPGraphicsDevice.java b/src/java/org/apache/fop/afp/svg/AFPGraphicsDevice.java similarity index 98% rename from src/java/org/apache/fop/afp/AFPGraphicsDevice.java rename to src/java/org/apache/fop/afp/svg/AFPGraphicsDevice.java index 20270e426..daa25aaa0 100644 --- a/src/java/org/apache/fop/afp/AFPGraphicsDevice.java +++ b/src/java/org/apache/fop/afp/svg/AFPGraphicsDevice.java @@ -5,9 +5,9 @@ * 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. @@ -17,12 +17,11 @@ /* $Id$ */ -package org.apache.fop.afp; +package org.apache.fop.afp.svg; import java.awt.GraphicsConfiguration; import java.awt.GraphicsDevice; - /** * This implements the GraphicsDevice interface as appropriate for * an AFPGraphics2D. diff --git a/src/java/org/apache/fop/svg/PDFGraphics2D.java b/src/java/org/apache/fop/svg/PDFGraphics2D.java index f8a0bd415..ab0ece2a7 100644 --- a/src/java/org/apache/fop/svg/PDFGraphics2D.java +++ b/src/java/org/apache/fop/svg/PDFGraphics2D.java @@ -101,7 +101,6 @@ import org.apache.xmlgraphics.java2d.GraphicContext; * @see org.apache.batik.ext.awt.g2d.AbstractGraphics2D */ public class PDFGraphics2D extends AbstractGraphics2D implements NativeImageHandler { - private static final AffineTransform IDENTITY_TRANSFORM = new AffineTransform(); /** The number of decimal places. */ -- 2.39.5