Skip to content

Commit

Permalink
FOP-3191: Add mandatory MODCA triplet to AFP by João André Gonçalves
Browse files Browse the repository at this point in the history
  • Loading branch information
simonsteiner1984 committed Sep 16, 2024
1 parent 6ef241f commit deb2c2c
Show file tree
Hide file tree
Showing 5 changed files with 68 additions and 10 deletions.
30 changes: 28 additions & 2 deletions fop-core/src/main/java/org/apache/fop/afp/modca/Document.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,13 @@

package org.apache.fop.afp.modca;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.OutputStream;

import org.apache.fop.afp.Factory;
import org.apache.fop.afp.modca.triplets.FullyQualifiedNameTriplet;
import org.apache.fop.afp.util.BinaryUtils;

/**
* The document is the highest level of the MO:DCA data-stream document
Expand Down Expand Up @@ -50,6 +53,8 @@
*/
public final class Document extends AbstractResourceEnvironmentGroupContainer {

private static final int CODE_PAGE = 500;

/**
* Constructor for the document object.
*
Expand All @@ -76,8 +81,29 @@ public boolean isComplete() {

/** {@inheritDoc} */
protected void writeStart(OutputStream os) throws IOException {
byte[] data = new byte[17];
copySF(data, Type.BEGIN, Category.DOCUMENT);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
byte[] startData = new byte[17];
copySF(startData, Type.BEGIN, Category.DOCUMENT);
baos.write(startData);
baos.write(0x00);
baos.write(0x06); // Total length of triplets (6)
/*
The triplet below was created following the
Mixed Object Document Content Architecture (MO:DCA) Reference
*/
// X'01 triplet
baos.write(0x06); //triplet length
baos.write(FullyQualifiedNameTriplet.CODED_GRAPHIC_CHARACTER_SET_GLOBAL_IDENTIFIER);
baos.write(0xFF); //part 1 of GCSGID
baos.write(0xFF); //part 2 of GCSGID
baos.write(BinaryUtils.convert(CODE_PAGE, 2)); //CPGID

byte[] data = baos.toByteArray();
// Set the total record length
byte[] rl1 = BinaryUtils.convert(data.length - 1, 2);
data[1] = rl1[0];
data[2] = rl1[1];

os.write(data);
}

Expand Down
32 changes: 32 additions & 0 deletions fop-core/src/test/java/org/apache/fop/afp/DataStreamTestCase.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,14 @@
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;


import org.apache.fop.afp.fonts.CharacterSet;
import org.apache.fop.afp.fonts.CharacterSetBuilder;
import org.apache.fop.afp.modca.InterchangeSet;
import org.apache.fop.afp.modca.InvokeMediumMap;
import org.apache.fop.afp.modca.PageGroup;
import org.apache.fop.afp.modca.triplets.FullyQualifiedNameTriplet;
import org.apache.fop.afp.util.BinaryUtils;
import org.apache.fop.fonts.Font;
import org.apache.fop.fonts.Typeface;
import org.apache.fop.util.CharUtilities;
Expand Down Expand Up @@ -105,6 +108,7 @@ public void testMediumMapOnDocument() throws Exception {
ds.endDocument();
ByteArrayInputStream data = new ByteArrayInputStream(outStream.toByteArray());
data.skip(21);
data.skip(8);
Assert.assertEquals((byte)data.read(), InvokeMediumMap.Type.MAP);
Assert.assertEquals((byte)data.read(), InvokeMediumMap.Category.MEDIUM_MAP);
}
Expand All @@ -121,7 +125,35 @@ public void testMediumMapBeforePageGroupOnDocument() throws Exception {
ds.endDocument();
ByteArrayInputStream data = new ByteArrayInputStream(outStream.toByteArray());
data.skip(21);
data.skip(8);
Assert.assertEquals((byte)data.read(), InvokeMediumMap.Type.MAP);
Assert.assertEquals((byte)data.read(), InvokeMediumMap.Category.MEDIUM_MAP);
}

@Test
public void testMandatoryTripletIsAddedToAFP() throws Exception {
ds = new DataStream(new Factory(), paintState, outStream);
ds.startDocument();
ds.startPageGroup();
ds.startPage(1, 1, 0, 1, 1);
ds.endPage();
ds.endPageGroup();
ds.endDocument();
ByteArrayInputStream data = new ByteArrayInputStream(outStream.toByteArray());
data.skip(17); //skipping the begin document data
Assert.assertEquals("Separation byte", 0x00, (byte) data.read());
Assert.assertEquals("Sum of the length of triplets", 0x06, (byte) data.read());
Assert.assertEquals("Length of the current triplet", 0x06, (byte) data.read());
Assert.assertEquals("Byte code of the mandatory triplet",
FullyQualifiedNameTriplet.CODED_GRAPHIC_CHARACTER_SET_GLOBAL_IDENTIFIER,
(byte) data.read());
Assert.assertEquals("Part 1 of the 0xFFFF byte. Sets the character set to All",
-1, (byte) data.read());
Assert.assertEquals("Part 2 of the 0xFFFF byte. Sets the character set to All",
-1, (byte) data.read()); //the 0xFF byte is converted to -1
Assert.assertEquals("Part 1 of the default code page id",
BinaryUtils.convert(500, 2)[0], (byte) data.read());
Assert.assertEquals("Part 2 of the default code page id",
BinaryUtils.convert(500, 2)[1], (byte) data.read());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ public void testDrawBorderRect() throws Exception {
public void testPresentationText() throws Exception {
List<String> strings = new ArrayList<String>();
strings.add("test");
Assert.assertEquals(writeText(strings), "BEGIN DOCUMENT DOC00001\n"
Assert.assertEquals(writeText(strings), "BEGIN DOCUMENT DOC00001 Triplets: 0x01,\n"
+ "BEGIN PAGE PGN00001\n"
+ "BEGIN ACTIVE_ENVIRONMENT_GROUP AEG00001\n"
+ "DESCRIPTOR PAGE\n"
Expand All @@ -176,7 +176,7 @@ public void testPresentationText() throws Exception {
for (int i = 0; i < 5000; i++) {
strings.add("test");
}
Assert.assertEquals(writeText(strings), "BEGIN DOCUMENT DOC00001\n"
Assert.assertEquals(writeText(strings), "BEGIN DOCUMENT DOC00001 Triplets: 0x01,\n"
+ "BEGIN PAGE PGN00001\n"
+ "BEGIN ACTIVE_ENVIRONMENT_GROUP AEG00001\n"
+ "DESCRIPTOR PAGE\n"
Expand All @@ -198,7 +198,7 @@ public void testPresentationText2() throws Exception {
for (int i = 0; i < 5000; i++) {
strings.add("tes");
}
Assert.assertEquals(writeText(strings), "BEGIN DOCUMENT DOC00001\n"
Assert.assertEquals(writeText(strings), "BEGIN DOCUMENT DOC00001 Triplets: 0x01,\n"
+ "BEGIN PAGE PGN00001\n"
+ "BEGIN ACTIVE_ENVIRONMENT_GROUP AEG00001\n"
+ "DESCRIPTOR PAGE\n"
Expand Down Expand Up @@ -287,7 +287,7 @@ public void testDrawBorderRect3() throws IFException, PropertyException, IOExcep
InputStream bis = new ByteArrayInputStream(os.toByteArray());
StringBuilder sb = new StringBuilder();
new AFPParser(false).read(bis, sb);
Assert.assertEquals(sb.toString(), "BEGIN DOCUMENT DOC00001\n"
Assert.assertEquals(sb.toString(), "BEGIN DOCUMENT DOC00001 Triplets: 0x01,\n"
+ "BEGIN PAGE PGN00001\n"
+ "BEGIN ACTIVE_ENVIRONMENT_GROUP AEG00001\n"
+ "DESCRIPTOR PAGE\n"
Expand Down Expand Up @@ -325,7 +325,7 @@ public void testDrawBorderRectAndText() throws IFException, PropertyException, I
InputStream bis = new ByteArrayInputStream(os.toByteArray());
StringBuilder sb = new StringBuilder();
new AFPParser(false).read(bis, sb);
Assert.assertEquals(sb.toString(), "BEGIN DOCUMENT DOC00001\n"
Assert.assertEquals(sb.toString(), "BEGIN DOCUMENT DOC00001 Triplets: 0x01,\n"
+ "BEGIN PAGE PGN00001\n"
+ "BEGIN ACTIVE_ENVIRONMENT_GROUP AEG00001\n"
+ "DESCRIPTOR PAGE\n"
Expand Down Expand Up @@ -392,7 +392,7 @@ public void testPageGroup() throws IFException, IOException {
InputStream bis = new ByteArrayInputStream(os.toByteArray());
StringBuilder sb = new StringBuilder();
new AFPParser(false).read(bis, sb);
Assert.assertEquals(sb.toString(), "BEGIN DOCUMENT DOC00001\n"
Assert.assertEquals(sb.toString(), "BEGIN DOCUMENT DOC00001 Triplets: 0x01,\n"
+ "BEGIN PAGE PGN00001\n"
+ "BEGIN ACTIVE_ENVIRONMENT_GROUP AEG00001\n"
+ "MAP CODED_FONT Triplets: "
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ public void testAFPTrueType() throws Exception {
format += "END OBJECT_CONTAINER OC000001\n"
+ "END NAME_RESOURCE RES00001\n"
+ "END RESOURCE_GROUP RG000001\n"
+ "BEGIN DOCUMENT DOC00001\n"
+ "BEGIN DOCUMENT DOC00001 Triplets: 0x01,\n"
+ "BEGIN PAGE_GROUP PGP00001\n"
+ "BEGIN PAGE PGN00001\n"
+ "BEGIN ACTIVE_ENVIRONMENT_GROUP AEG00001\n"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@
public class PageOverlayTestCase {
@Test
public void testPageOverlay() throws Exception {
Assert.assertEquals(getPageOverlay(), "BEGIN DOCUMENT DOC00001\n"
Assert.assertEquals(getPageOverlay(), "BEGIN DOCUMENT DOC00001 Triplets: 0x01,\n"
+ "BEGIN PAGE_GROUP PGP00001\n"
+ "END PAGE_GROUP PGP00001\n"
+ "BEGIN PAGE PGN00001\n"
Expand Down

0 comments on commit deb2c2c

Please sign in to comment.