]> source.dussan.org Git - xmlgraphics-fop.git/commitdiff
FOP-3191: Add mandatory MODCA triplet to AFP by João André Gonçalves
authorSimon Steiner <ssteiner@apache.org>
Mon, 16 Sep 2024 13:20:03 +0000 (14:20 +0100)
committerSimon Steiner <ssteiner@apache.org>
Mon, 16 Sep 2024 13:20:03 +0000 (14:20 +0100)
fop-core/src/main/java/org/apache/fop/afp/modca/Document.java
fop-core/src/test/java/org/apache/fop/afp/DataStreamTestCase.java
fop-core/src/test/java/org/apache/fop/render/afp/AFPPainterTestCase.java
fop-core/src/test/java/org/apache/fop/render/afp/AFPTrueTypeTestCase.java
fop-core/src/test/java/org/apache/fop/render/afp/PageOverlayTestCase.java

index 69aed97efd6cc5ec2b6b602af010ffe1f9453680..d4d7c75a6b855c5d8cb359440e1d85f210af15bf 100644 (file)
 
 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);
     }
 
index 70212698945219ea5753b15caeaaf8232850c41d..f178a815e7555d174dad0522a30fc1b62b7a4947 100644 (file)
@@ -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());
+    }
 }
index 92bf91edb6cd0be2a3ea8a634a89a080239e54fe..29c478cc2e09828fdeb66c4d3ba2ae809e009920 100644 (file)
@@ -161,7 +161,7 @@ public class AFPPainterTestCase {
     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"
@@ -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: "
index f875d80d8e387bd2a13c30d76d994f48838ffc15..4407c2af23bbd160717f6f1cf7b46c436538de7a 100644 (file)
@@ -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"
index dc908eb59cf67913287ab62b4a3e37aaa722baae..9f60fd4760e4c0ee41ab25e0222cb30cc46608cc 100644 (file)
@@ -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"