diff options
author | Adrian Cumiskey <acumiskey@apache.org> | 2008-11-17 15:26:06 +0000 |
---|---|---|
committer | Adrian Cumiskey <acumiskey@apache.org> | 2008-11-17 15:26:06 +0000 |
commit | 016ddc8aa591fde0adcaef76158f3f5ebcf6590a (patch) | |
tree | bbba62706cdaf1c15113b0c1bd309a84e476e36f /src/java/org/apache/fop/afp/goca | |
parent | 3bc890586dede254dad93f506d12e68b1533047c (diff) | |
download | xmlgraphics-fop-016ddc8aa591fde0adcaef76158f3f5ebcf6590a.tar.gz xmlgraphics-fop-016ddc8aa591fde0adcaef76158f3f5ebcf6590a.zip |
Memory usage optimizations.
git-svn-id: https://svn.apache.org/repos/asf/xmlgraphics/fop/branches/Temp_AFPGOCAResources@718262 13f79535-47bb-0310-9956-ffa450edef68
Diffstat (limited to 'src/java/org/apache/fop/afp/goca')
21 files changed, 438 insertions, 190 deletions
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/goca/AbstractGraphicsObjectContainer.java b/src/java/org/apache/fop/afp/goca/AbstractGraphicsObjectContainer.java new file mode 100644 index 000000000..672193042 --- /dev/null +++ b/src/java/org/apache/fop/afp/goca/AbstractGraphicsObjectContainer.java @@ -0,0 +1,83 @@ +/* + * 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; +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 AbstractGraphicsObjectContainer extends AbstractNamedAFPObject +implements StructuredDataObject { + + /** list of objects contained within this container */ + protected List/*<StructuredDataObject>*/ objects + = new java.util.ArrayList/*<StructuredDataObject>*/(); + + /** + * Default constructor + */ + protected AbstractGraphicsObjectContainer() { + } + + /** + * Named constructor + * + * @param name the name of the container + */ + protected AbstractGraphicsObjectContainer(String name) { + super(name); + } + + /** {@inheritDoc} */ + protected void writeContent(OutputStream os) throws IOException { + writeObjects(objects, os); + } + + /** + * Adds a given graphics object to this container + * + * @param drawingOrder the graphics object + */ + public void addObject(StructuredDataObject drawingOrder) { + objects.add(drawingOrder); + } + + /** + * Returns the current data length + * + * @return the current data length of this container including + * all enclosed objects (and their containers) + */ + public int getDataLength() { + int dataLen = 0; + Iterator it = objects.iterator(); + while (it.hasNext()) { + 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; @@ -84,11 +83,16 @@ public final class GraphicsChainedSegment extends AbstractPreparedObjectContaine } /** {@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 |