diff options
author | Jeremias Maerki <jeremias@apache.org> | 2009-01-11 11:31:24 +0000 |
---|---|---|
committer | Jeremias Maerki <jeremias@apache.org> | 2009-01-11 11:31:24 +0000 |
commit | 25b82eeedf54b42083102ce9d4c3dad3a472d586 (patch) | |
tree | 31e89c33e88d6473d092a706c54a4851e148b112 /src/java/org/apache/fop/afp/ptoca | |
parent | 04bb06d6912f18601d1623935ac0d202da372e67 (diff) | |
download | xmlgraphics-fop-25b82eeedf54b42083102ce9d4c3dad3a472d586.tar.gz xmlgraphics-fop-25b82eeedf54b42083102ce9d4c3dad3a472d586.zip |
Started new IF implementation for AFP (incomplete, supporting filled rects, text and images). Work in progress!
Moved PTOCA (presentation text) command sequence production into the new "ptoca" package.
The PtocaBuilder class allows more flexible command sequence production than if you have to pass in an "info object".
git-svn-id: https://svn.apache.org/repos/asf/xmlgraphics/fop/branches/Temp_AreaTreeNewDesign@733456 13f79535-47bb-0310-9956-ffa450edef68
Diffstat (limited to 'src/java/org/apache/fop/afp/ptoca')
6 files changed, 658 insertions, 0 deletions
diff --git a/src/java/org/apache/fop/afp/ptoca/LineDataInfoProducer.java b/src/java/org/apache/fop/afp/ptoca/LineDataInfoProducer.java new file mode 100644 index 000000000..c702d72a8 --- /dev/null +++ b/src/java/org/apache/fop/afp/ptoca/LineDataInfoProducer.java @@ -0,0 +1,76 @@ +/* + * 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.ptoca; + +import java.io.IOException; + +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; + +import org.apache.fop.afp.AFPLineDataInfo; + +/** + * {@link PtocaProducer} implementation that interprets {@link AFPLineDataInfo} objects. + */ +public class LineDataInfoProducer implements PtocaProducer, PtocaConstants { + + /** Static logging instance */ + private static final Log log = LogFactory.getLog(LineDataInfoProducer.class); + + private AFPLineDataInfo lineDataInfo; + + /** + * Main constructor. + * @param lineDataInfo the info object + */ + public LineDataInfoProducer(AFPLineDataInfo lineDataInfo) { + this.lineDataInfo = lineDataInfo; + } + + /** {@inheritDoc} */ + public void produce(PtocaBuilder builder) throws IOException { + builder.setTextOrientation(lineDataInfo.getRotation()); + int x1 = ensurePositive(lineDataInfo.getX1()); + int y1 = ensurePositive(lineDataInfo.getY1()); + builder.absoluteMoveBaseline(y1); + builder.absoluteMoveInline(x1); + builder.setExtendedTextColor(lineDataInfo.getColor()); + + int x2 = ensurePositive(lineDataInfo.getX2()); + int y2 = ensurePositive(lineDataInfo.getY2()); + int thickness = lineDataInfo.getThickness(); + if (y1 == y2) { + builder.drawIaxisRule(x2 - x1, thickness); + } else if (x1 == x2) { + builder.drawBaxisRule(y2 - y1, thickness); + } else { + log.error("Invalid axis rule: unable to draw line"); + return; + } + } + + private static int ensurePositive(int value) { + if (value < 0) { + return 0; + } + return value; + } + +} diff --git a/src/java/org/apache/fop/afp/ptoca/PtocaBuilder.java b/src/java/org/apache/fop/afp/ptoca/PtocaBuilder.java new file mode 100644 index 000000000..b5e866380 --- /dev/null +++ b/src/java/org/apache/fop/afp/ptoca/PtocaBuilder.java @@ -0,0 +1,389 @@ +/* + * 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.ptoca; + +import java.awt.Color; +import java.io.IOException; +import java.io.OutputStream; + +import org.apache.commons.io.output.ByteArrayOutputStream; + +/** + * Generator class for PTOCA data structures. + */ +public abstract class PtocaBuilder implements PtocaConstants { + + private ByteArrayOutputStream baout = new ByteArrayOutputStream(256); + + /** the current x coordinate. */ + private int currentX = -1; + + /** the current y coordinate */ + private int currentY = -1; + + /** the current font */ + private int currentFont = Integer.MIN_VALUE; + + /** the current orientation */ + private int currentOrientation = 0; + + /** the current color */ + private Color currentColor = Color.BLACK; + + /** the current variable space increment */ + private int currentVariableSpaceCharacterIncrement = 0; + + /** the current inter character adjustment */ + private int currentInterCharacterAdjustment = 0; + + + /** + * Returns an {@link OutputStream} for the next control sequence. This gives a subclass a + * chance to do chunking of control sequences into multiple presentation text data objects. + * @param length the length of the following control sequence + * @return the output stream where the control sequence will be written to + */ + protected abstract OutputStream getOutputStreamForControlSequence(int length); + + private static byte chained(byte functionType) { + return (byte)(functionType | CHAIN_BIT); + } + + private void newControlSequence() { + baout.reset(); + } + + private void commit(byte functionType) throws IOException { + int length = baout.size() + 2; + assert length < 256; + + OutputStream out = getOutputStreamForControlSequence(length); + out.write(length); + out.write(functionType); + baout.writeTo(out); + } + + private void write(byte[] data, int offset, int length) { + baout.write(data, offset, length); + } + + private void writeByte(int data) { + baout.write(data); + } + + private void writeShort(int data) { + baout.write((data >>> 8) & 0xFF); + baout.write(data & 0xFF); + } + + /** + * Writes the introducer for a chained control sequence. + * @throws IOException if an I/O error occurs + */ + public void writeIntroducer() throws IOException { + OutputStream out = getOutputStreamForControlSequence(ESCAPE.length); + out.write(ESCAPE); + } + + /** + * The Set Coded Font Local control sequence activates a coded font and + * specifies the character attributes to be used. + * <p> + * This is a modal control sequence. + * + * @param font The font local identifier. + * @throws IOException if an I/O error occurs + */ + public void setCodedFont(byte font) throws IOException { + // Avoid unnecessary specification of the font + if (currentFont == font) { + return; + } else { + currentFont = font; + } + + newControlSequence(); + writeByte(font); + commit(chained(SCFL)); + } + + /** + * Establishes the current presentation position on the baseline at a new + * I-axis coordinate, which is a specified number of measurement units from + * the B-axis. There is no change to the current B-axis coordinate. + * + * @param coordinate The coordinate for the inline move. + * @throws IOException if an I/O error occurs + */ + public void absoluteMoveInline(int coordinate) throws IOException { + if (coordinate == this.currentX) { + return; + } + newControlSequence(); + writeShort(coordinate); + commit(chained(AMI)); + + currentX = coordinate; + } + + /** + * Moves the inline coordinate of the presentation position relative to the current + * inline position. + * @param increment the increment in 1/1440 inch units + * @throws IOException if an I/O error occurs + */ + public void relativeMoveInline(int increment) throws IOException { + newControlSequence(); + writeShort(increment); + commit(chained(RMI)); + } + + /** + * Establishes the baseline and the current presentation position at a new + * B-axis coordinate, which is a specified number of measurement units from + * the I-axis. There is no change to the current I-axis coordinate. + * + * @param coordinate The coordinate for the baseline move. + * @throws IOException if an I/O error occurs + */ + public void absoluteMoveBaseline(int coordinate) throws IOException { + if (coordinate == this.currentY) { + return; + } + newControlSequence(); + writeShort(coordinate); + commit(chained(AMB)); + + currentY = coordinate; + currentX = -1; + } + + private static final int TRANSPARENT_MAX_SIZE = 253; + + /** + * The Transparent Data control sequence contains a sequence of code points + * that are presented without a scan for embedded control sequences. If the data is larger + * than fits in one chunk, additional chunks are automatically generated. + * + * @param data The text data to add. + * @throws IOException if an I/O error occurs + */ + public void addTransparentData(byte[] data) throws IOException { + if (data.length <= TRANSPARENT_DATA_MAX_SIZE) { + addTransparentDataChunk(data); + } else { + // data size greater than TRANSPARENT_MAX_SIZE, so slice + int numTransData = data.length / TRANSPARENT_DATA_MAX_SIZE; + int currIndex = 0; + for (int transDataCnt = 0; transDataCnt < numTransData; transDataCnt++) { + addTransparentDataChunk(data, currIndex, TRANSPARENT_DATA_MAX_SIZE); + currIndex += TRANSPARENT_DATA_MAX_SIZE; + } + int left = data.length - currIndex; + addTransparentDataChunk(data, currIndex, left); + } + } + + private void addTransparentDataChunk(byte[] data) throws IOException { + addTransparentDataChunk(data, 0, data.length); + } + + private void addTransparentDataChunk(byte[] data, int offset, int length) throws IOException { + if (length > TRANSPARENT_MAX_SIZE) { + // Check that we are not exceeding the maximum length + throw new IllegalArgumentException( + "Transparent data is longer than " + TRANSPARENT_MAX_SIZE + " bytes"); + } + newControlSequence(); + write(data, offset, length); + commit(chained(TRN)); + } + + /** + * Draws a line of specified length and specified width in the B-direction + * from the current presentation position. The location of the current + * presentation position is unchanged. + * + * @param length The length of the rule. + * @param width The width of the rule. + * @throws IOException if an I/O error occurs + */ + public void drawBaxisRule(int length, int width) throws IOException { + newControlSequence(); + writeShort(length); // Rule length + writeShort(width); // Rule width + writeByte(0); // Rule width fraction is always null. enough? + commit(chained(DBR)); + } + + /** + * Draws a line of specified length and specified width in the I-direction + * from the current presentation position. The location of the current + * presentation position is unchanged. + * + * @param length The length of the rule. + * @param width The width of the rule. + * @throws IOException if an I/O error occurs + */ + public void drawIaxisRule(int length, int width) throws IOException { + newControlSequence(); + writeShort(length); // Rule length + writeShort(width); // Rule width + writeByte(0); // Rule width fraction is always null. enough? + commit(chained(DIR)); + } + + /** + * The Set Text Orientation control sequence establishes the I-direction and + * B-direction for the subsequent text. This is a modal control sequence. + * + * Semantics: This control sequence specifies the I-axis and B-axis + * orientations with respect to the Xp-axis for the current Presentation + * Text object. The orientations are rotational values expressed in degrees + * and minutes. + * + * @param orientation The text orientation (0, 90, 180, 270). + * @throws IOException if an I/O error occurs + */ + public void setTextOrientation(int orientation) throws IOException { + if (orientation == this.currentOrientation) { + return; + } + newControlSequence(); + switch (orientation) { + case 90: + writeByte(0x2D); + writeByte(0x00); + writeByte(0x5A); + writeByte(0x00); + break; + case 180: + writeByte(0x5A); + writeByte(0x00); + writeByte(0x87); + writeByte(0x00); + break; + case 270: + writeByte(0x87); + writeByte(0x00); + writeByte(0x00); + writeByte(0x00); + break; + default: + writeByte(0x00); + writeByte(0x00); + writeByte(0x2D); + writeByte(0x00); + break; + } + commit(chained(STO)); + this.currentOrientation = orientation; + currentX = -1; + currentY = -1; + } + + /** + * The Set Extended Text Color control sequence specifies a color value and + * defines the color space and encoding for that value. The specified color + * value is applied to foreground areas of the text presentation space. + * <p> + * This is a modal control sequence. + * + * @param col The color to be set. + * @throws IOException if an I/O error occurs + */ + public void setExtendedTextColor(Color col) throws IOException { + if (col.equals(currentColor)) { + return; + } + newControlSequence(); + writeByte(0x00); // Reserved; must be zero + writeByte(0x01); // Color space - 0x01 = RGB + writeByte(0x00); // Reserved; must be zero + writeByte(0x00); // Reserved; must be zero + writeByte(0x00); // Reserved; must be zero + writeByte(0x00); // Reserved; must be zero + writeByte(8); // Number of bits in component 1 + writeByte(8); // Number of bits in component 2 + writeByte(8); // Number of bits in component 3 + writeByte(0); // Number of bits in component 4 + writeByte(col.getRed()); // Red intensity + writeByte(col.getGreen()); // Green intensity + writeByte(col.getBlue()); // Blue intensity + commit(chained(SEC)); + this.currentColor = col; + } + + /** + * Sets the variable space character increment. + * <p> + * This is a modal control sequence. + * + * @param incr The increment to be set (positive integer, 1/1440 inch) + * @throws IOException if an I/O error occurs + */ + public void setVariableSpaceCharacterIncrement(int incr) throws IOException { + if (incr == this.currentVariableSpaceCharacterIncrement) { + return; + } + assert incr > 0 && incr < (1 << 16); + newControlSequence(); + writeShort(Math.abs(incr)); //Increment + commit(chained(SVI)); + + this.currentVariableSpaceCharacterIncrement = incr; + } + + /** + * Sets the intercharacter adjustment (additional increment or decrement between graphic + * characters). + * <p> + * This is a modal control sequence. + * + * @param incr The increment to be set (1/1440 inch) + * @throws IOException if an I/O error occurs + */ + public void setInterCharacterAdjustment(int incr) throws IOException { + if (incr == this.currentInterCharacterAdjustment) { + return; + } + assert incr >= Short.MIN_VALUE && incr <= Short.MAX_VALUE; + newControlSequence(); + writeShort(Math.abs(incr)); //Increment + writeByte(incr >= 0 ? 0 : 1); // Direction + commit(chained(SIA)); + + this.currentInterCharacterAdjustment = incr; + } + + /** + * A control sequence is a sequence of bytes that specifies a control + * function. A control sequence consists of a control sequence introducer + * and zero or more parameters. The control sequence can extend multiple + * presentation text data objects, but must eventually be terminated. This + * method terminates the control sequence (by using a NOP command). + * + * @throws IOException if an I/O error occurs + */ + public void endChainedControlSequence() throws IOException { + newControlSequence(); + commit(NOP); + } +} diff --git a/src/java/org/apache/fop/afp/ptoca/PtocaConstants.java b/src/java/org/apache/fop/afp/ptoca/PtocaConstants.java new file mode 100644 index 000000000..2e692af2c --- /dev/null +++ b/src/java/org/apache/fop/afp/ptoca/PtocaConstants.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.ptoca; + +/** + * A collection of PTOCA constants. + */ +public interface PtocaConstants { + + /** + * "Escape" sequence for normal PTOCA command sequences. + */ + byte[] ESCAPE = new byte[] {0x2B, (byte)0xD3}; + + /** Bit to set for chained control sequences */ + byte CHAIN_BIT = 1; + + /** Set Intercharacter Adjustment */ + byte SIA = (byte)0xC2; + /** Set Variable Space Character Increment */ + byte SVI = (byte)0xC4; + /** Absolute Move Inline */ + byte AMI = (byte)0xC6; + /** Relative Move Inline */ + byte RMI = (byte)0xC8; + + /** Absolute Move Baseline */ + byte AMB = (byte)0xD2; + + /** Transparent Data */ + byte TRN = (byte)0xDA; + + /** Draw I-axis Rule */ + byte DIR = (byte)0xE4; + /** Draw B-axis Rule */ + byte DBR = (byte)0xE6; + + /** Set Extended Text Color */ + byte SEC = (byte)0x80; + + /** Set Coded Font Local */ + byte SCFL = (byte)0xF0; + /** Set Text Orientation */ + byte STO = (byte)0xF6; + + /** No Operation */ + byte NOP = (byte)0xF8; + + /** Maximum size of transparent data chunks */ + int TRANSPARENT_DATA_MAX_SIZE = 253; + +} diff --git a/src/java/org/apache/fop/afp/ptoca/PtocaProducer.java b/src/java/org/apache/fop/afp/ptoca/PtocaProducer.java new file mode 100644 index 000000000..504c49658 --- /dev/null +++ b/src/java/org/apache/fop/afp/ptoca/PtocaProducer.java @@ -0,0 +1,39 @@ +/* + * 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.ptoca; + +import java.io.IOException; + +import org.apache.fop.afp.modca.PresentationTextObject; + +/** + * Producer interface that is passed to a {@link PresentationTextObject} to produce PTOCA control + * sequences using a {@link PtocaBuilder}. + */ +public interface PtocaProducer { + + /** + * Produces the PTOCA control sequences by calling methods on {@link PtocaBuilder}. + * @param builder the builder object + * @throws IOException if an I/O error occurs + */ + void produce(PtocaBuilder builder) throws IOException; + +} diff --git a/src/java/org/apache/fop/afp/ptoca/TextDataInfoProducer.java b/src/java/org/apache/fop/afp/ptoca/TextDataInfoProducer.java new file mode 100644 index 000000000..7ae3028e8 --- /dev/null +++ b/src/java/org/apache/fop/afp/ptoca/TextDataInfoProducer.java @@ -0,0 +1,62 @@ +/* + * 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.ptoca; + +import java.io.IOException; + +import org.apache.fop.afp.AFPTextDataInfo; + +/** + * {@link PtocaProducer} implementation that interprets {@link AFPTextDataInfo} objects. + */ +public class TextDataInfoProducer implements PtocaProducer, PtocaConstants { + + private AFPTextDataInfo textDataInfo; + + /** + * Main constructor. + * @param textDataInfo the info object + */ + public TextDataInfoProducer(AFPTextDataInfo textDataInfo) { + this.textDataInfo = textDataInfo; + } + + /** {@inheritDoc} */ + public void produce(PtocaBuilder builder) throws IOException { + builder.setTextOrientation(textDataInfo.getRotation()); + builder.absoluteMoveBaseline(textDataInfo.getY()); + builder.absoluteMoveInline(textDataInfo.getX()); + + builder.setVariableSpaceCharacterIncrement( + textDataInfo.getVariableSpaceCharacterIncrement()); + builder.setInterCharacterAdjustment( + textDataInfo.getInterCharacterAdjustment()); + builder.setExtendedTextColor(textDataInfo.getColor()); + builder.setCodedFont((byte)textDataInfo.getFontReference()); + + + // Add transparent data + String textString = textDataInfo.getString(); + String encoding = textDataInfo.getEncoding(); + byte[] data = textString.getBytes(encoding); + builder.addTransparentData(data); + } + +} diff --git a/src/java/org/apache/fop/afp/ptoca/package.html b/src/java/org/apache/fop/afp/ptoca/package.html new file mode 100644 index 000000000..7520e1cb2 --- /dev/null +++ b/src/java/org/apache/fop/afp/ptoca/package.html @@ -0,0 +1,23 @@ +<!-- + 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$ --> +<HTML> +<TITLE>org.apache.fop.afp.ptoca Package</TITLE> +<BODY> +<P>Contains a collection of classes for working with Presentation Text Objects (PTOCA).</P> +</BODY> +</HTML>
\ No newline at end of file |