From deb2c2cbc18790ed8ad17303f841a178441e1a6a Mon Sep 17 00:00:00 2001 From: Simon Steiner Date: Mon, 16 Sep 2024 14:20:03 +0100 Subject: [PATCH] =?utf8?q?FOP-3191:=20Add=20mandatory=20MODCA=20triplet=20?= =?utf8?q?to=20AFP=20by=20Jo=C3=A3o=20Andr=C3=A9=20Gon=C3=A7alves?= MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit --- .../org/apache/fop/afp/modca/Document.java | 30 +++++++++++++++-- .../apache/fop/afp/DataStreamTestCase.java | 32 +++++++++++++++++++ .../fop/render/afp/AFPPainterTestCase.java | 12 +++---- .../fop/render/afp/AFPTrueTypeTestCase.java | 2 +- .../fop/render/afp/PageOverlayTestCase.java | 2 +- 5 files changed, 68 insertions(+), 10 deletions(-) diff --git a/fop-core/src/main/java/org/apache/fop/afp/modca/Document.java b/fop-core/src/main/java/org/apache/fop/afp/modca/Document.java index 69aed97ef..d4d7c75a6 100644 --- a/fop-core/src/main/java/org/apache/fop/afp/modca/Document.java +++ b/fop-core/src/main/java/org/apache/fop/afp/modca/Document.java @@ -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 @@ -50,6 +53,8 @@ import org.apache.fop.afp.Factory; */ public final class Document extends AbstractResourceEnvironmentGroupContainer { + private static final int CODE_PAGE = 500; + /** * Constructor for the document object. * @@ -76,8 +81,29 @@ public final class Document extends AbstractResourceEnvironmentGroupContainer { /** {@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); } diff --git a/fop-core/src/test/java/org/apache/fop/afp/DataStreamTestCase.java b/fop-core/src/test/java/org/apache/fop/afp/DataStreamTestCase.java index 702126989..f178a815e 100644 --- a/fop-core/src/test/java/org/apache/fop/afp/DataStreamTestCase.java +++ b/fop-core/src/test/java/org/apache/fop/afp/DataStreamTestCase.java @@ -31,11 +31,14 @@ import org.junit.Test; 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; @@ -105,6 +108,7 @@ public class DataStreamTestCase { 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); } @@ -121,7 +125,35 @@ public class DataStreamTestCase { 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()); + } } diff --git a/fop-core/src/test/java/org/apache/fop/render/afp/AFPPainterTestCase.java b/fop-core/src/test/java/org/apache/fop/render/afp/AFPPainterTestCase.java index 92bf91edb..29c478cc2 100644 --- a/fop-core/src/test/java/org/apache/fop/render/afp/AFPPainterTestCase.java +++ b/fop-core/src/test/java/org/apache/fop/render/afp/AFPPainterTestCase.java @@ -161,7 +161,7 @@ public class AFPPainterTestCase { public void testPresentationText() throws Exception { List strings = new ArrayList(); 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" @@ -176,7 +176,7 @@ public class AFPPainterTestCase { 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" @@ -198,7 +198,7 @@ public class AFPPainterTestCase { 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" @@ -287,7 +287,7 @@ public class AFPPainterTestCase { 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" @@ -325,7 +325,7 @@ public class AFPPainterTestCase { 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" @@ -392,7 +392,7 @@ public class AFPPainterTestCase { 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: " diff --git a/fop-core/src/test/java/org/apache/fop/render/afp/AFPTrueTypeTestCase.java b/fop-core/src/test/java/org/apache/fop/render/afp/AFPTrueTypeTestCase.java index f875d80d8..4407c2af2 100644 --- a/fop-core/src/test/java/org/apache/fop/render/afp/AFPTrueTypeTestCase.java +++ b/fop-core/src/test/java/org/apache/fop/render/afp/AFPTrueTypeTestCase.java @@ -111,7 +111,7 @@ public class AFPTrueTypeTestCase { 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" diff --git a/fop-core/src/test/java/org/apache/fop/render/afp/PageOverlayTestCase.java b/fop-core/src/test/java/org/apache/fop/render/afp/PageOverlayTestCase.java index dc908eb59..9f60fd476 100644 --- a/fop-core/src/test/java/org/apache/fop/render/afp/PageOverlayTestCase.java +++ b/fop-core/src/test/java/org/apache/fop/render/afp/PageOverlayTestCase.java @@ -40,7 +40,7 @@ import org.apache.fop.render.intermediate.IFContext; 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" -- 2.39.5