1 /* 2 * Licensed to the Apache Software Foundation (ASF) under one or more 3 * contributor license agreements. See the NOTICE file distributed with 4 * this work for additional information regarding copyright ownership. 5 * The ASF licenses this file to You under the Apache License, Version 2.0 6 * (the "License"); you may not use this file except in compliance with 7 * the License. You may obtain a copy of the License at 8 * 9 * http://www.apache.org/licenses/LICENSE-2.0 10 * 11 * Unless required by applicable law or agreed to in writing, software 12 * distributed under the License is distributed on an "AS IS" BASIS, 13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 * See the License for the specific language governing permissions and 15 * limitations under the License. 16 */ 17 18 /* $Id$ */ 19 package org.apache.fop.pdf; 20 21 import java.awt.geom.Rectangle2D; 22 import java.io.ByteArrayInputStream; 23 import java.io.ByteArrayOutputStream; 24 import java.io.File; 25 import java.io.IOException; 26 import java.io.InputStream; 27 import java.util.Collection; 28 import java.util.Iterator; 29 import java.util.Map; 30 31 import javax.xml.transform.Result; 32 import javax.xml.transform.Source; 33 import javax.xml.transform.Transformer; 34 import javax.xml.transform.TransformerException; 35 import javax.xml.transform.TransformerFactory; 36 import javax.xml.transform.sax.SAXResult; 37 import javax.xml.transform.stream.StreamResult; 38 import javax.xml.transform.stream.StreamSource; 39 40 import org.junit.Assert; 41 import org.junit.Test; 42 43 import org.xml.sax.SAXException; 44 45 import org.apache.xmlgraphics.util.QName; 46 import org.apache.xmlgraphics.xmp.Metadata; 47 48 import org.apache.fop.apps.FOUserAgent; 49 import org.apache.fop.apps.Fop; 50 import org.apache.fop.apps.FopFactory; 51 import org.apache.fop.apps.MimeConstants; 52 import org.apache.fop.render.intermediate.IFContext; 53 import org.apache.fop.render.intermediate.IFDocumentHandler; 54 import org.apache.fop.render.intermediate.IFException; 55 import org.apache.fop.render.intermediate.IFParser; 56 import org.apache.fop.render.intermediate.IFSerializer; 57 import org.apache.fop.render.intermediate.IFUtil; 58 import org.apache.fop.render.pdf.PDFContentGenerator; 59 60 public class PDFVTTestCase { 61 @Test testXMP()62 public void testXMP() throws IOException { 63 PDFDocument doc = new PDFDocument(""); 64 doc.getProfile().setPDFXMode(PDFXMode.PDFX_4); 65 doc.getProfile().setPDFVTMode(PDFVTMode.PDFVT_1); 66 Metadata metadata = PDFMetadata.createXMPFromPDFDocument(doc); 67 StringBuilder sb = new StringBuilder(); 68 Iterator i = metadata.iterator(); 69 while (i.hasNext()) { 70 QName k = (QName) i.next(); 71 sb.append(k + ": " + metadata.getProperty(k).getValue() + "\n"); 72 } 73 String s = sb.toString(); 74 Assert.assertTrue(s.contains("pdfxid:GTS_PDFXVersion: PDF/X-4")); 75 Assert.assertTrue(s.contains("xmpMM:VersionID: 1")); 76 Assert.assertTrue(s.contains("pdf:Trapped: False")); 77 Assert.assertTrue(s.contains("xmpMM:RenditionClass: default")); 78 Assert.assertTrue(s.contains("pdf:PDFVersion: 1.4")); 79 Assert.assertTrue(s.contains("pdfvtid:GTS_PDFVTVersion: PDF/VT-1")); 80 } 81 82 @Test testPDF()83 public void testPDF() throws IOException { 84 PDFDocument doc = new PDFDocument(""); 85 doc.getInfo().setTitle("title"); 86 doc.getProfile().setPDFXMode(PDFXMode.PDFX_4); 87 doc.getProfile().setPDFVTMode(PDFVTMode.PDFVT_1); 88 PDFResources resources = new PDFResources(doc); 89 doc.addObject(resources); 90 PDFResourceContext context = new PDFResourceContext(resources); 91 ByteArrayOutputStream out = new ByteArrayOutputStream(); 92 PDFContentGenerator gen = new PDFContentGenerator(doc, out, context); 93 Rectangle2D.Float f = new Rectangle2D.Float(); 94 PDFPage page = new PDFPage(resources, 0, f, f, f, f); 95 doc.addImage(context, new BitmapImage("", 1, 1, new byte[0], null)); 96 doc.registerObject(page); 97 doc.getFactory().makeDPart(page, "master"); 98 gen.flushPDFDoc(); 99 doc.outputTrailer(out); 100 101 Collection<StringBuilder> objs = PDFLinearizationTestCase.readObjs( 102 new ByteArrayInputStream(out.toByteArray())).values(); 103 Assert.assertTrue(getObj(objs, "/Type /Catalog").contains("/DPartRoot ")); 104 Assert.assertTrue(getObj(objs, "/Type /DPartRoot").contains("/NodeNameList [/root /record]")); 105 Assert.assertTrue( 106 getObj(objs, "/Subtype /Image").contains("/GTS_XID (uuid:d41d8cd9-8f00-3204-a980-0998ecf8427e)")); 107 } 108 109 @Test textFO()110 public void textFO() throws IOException, SAXException, TransformerException, IFException { 111 ByteArrayOutputStream out = new ByteArrayOutputStream(); 112 foToOutput(out, MimeConstants.MIME_PDF); 113 checkPDF(out); 114 } 115 116 @Test textIF()117 public void textIF() throws IOException, SAXException, TransformerException, IFException { 118 ByteArrayOutputStream out = new ByteArrayOutputStream(); 119 foToOutput(out, MimeConstants.MIME_FOP_IF); 120 iFToPDF(new ByteArrayInputStream(out.toByteArray())); 121 } 122 123 foToOutput(ByteArrayOutputStream out, String mimeFopIf)124 private void foToOutput(ByteArrayOutputStream out, String mimeFopIf) 125 throws IOException, SAXException, TransformerException { 126 FopFactory fopFactory = getFopFactory(); 127 FOUserAgent userAgent = fopFactory.newFOUserAgent(); 128 129 if (mimeFopIf.equals(MimeConstants.MIME_FOP_IF)) { 130 IFSerializer serializer = new IFSerializer(new IFContext(userAgent)); 131 IFDocumentHandler targetHandler 132 = userAgent.getRendererFactory().createDocumentHandler(userAgent, MimeConstants.MIME_PDF); 133 serializer.mimicDocumentHandler(targetHandler); 134 userAgent.setDocumentHandlerOverride(serializer); 135 } 136 137 Fop fop = fopFactory.newFop(mimeFopIf, userAgent, out); 138 Transformer transformer = TransformerFactory.newInstance().newTransformer(); 139 Source src = new StreamSource(PDFVTTestCase.class.getResource("PDFVT.fo").openStream()); 140 Result res = new SAXResult(fop.getDefaultHandler()); 141 transformer.transform(src, res); 142 } 143 getFopFactory()144 private FopFactory getFopFactory() throws IOException, SAXException { 145 return FopFactory.newInstance(new File(".").toURI(), 146 PDFVTTestCase.class.getResource("PDFVT.xconf").openStream()); 147 } 148 iFToPDF(InputStream is)149 private void iFToPDF(InputStream is) throws IOException, SAXException, TransformerException, IFException { 150 ByteArrayOutputStream out = new ByteArrayOutputStream(); 151 152 FOUserAgent userAgent = getFopFactory().newFOUserAgent(); 153 Transformer transformer = TransformerFactory.newInstance().newTransformer(); 154 Source src = new StreamSource(is); 155 IFDocumentHandler documentHandler 156 = userAgent.getRendererFactory().createDocumentHandler(userAgent, MimeConstants.MIME_PDF); 157 documentHandler.setResult(new StreamResult(out)); 158 IFUtil.setupFonts(documentHandler); 159 IFParser parser = new IFParser(); 160 Result res = new SAXResult(parser.getContentHandler(documentHandler, userAgent)); 161 transformer.transform(src, res); 162 163 checkPDF(out); 164 } 165 checkPDF(ByteArrayOutputStream out)166 private void checkPDF(ByteArrayOutputStream out) throws IOException { 167 Map<String, StringBuilder> objs = 168 PDFLinearizationTestCase.readObjs(new ByteArrayInputStream(out.toByteArray())); 169 String dpart = getObj(objs.values(), "/DParts"); 170 int v = getValue("/DParts", dpart); 171 String dpm = objs.get(v + " 0 obj").toString(); 172 Assert.assertTrue(dpm.contains( 173 "/DPM << /CIP4_Root << /CIP4_Production << /CIP4_Part << /CIP4_ProductType (frontpages) >>")); 174 } 175 getValue(String name, String firstObj)176 private int getValue(String name, String firstObj) throws IOException { 177 String[] split = firstObj.split(" "); 178 for (int i = 0; i < split.length; i++) { 179 if (split[i].equals(name)) { 180 return Integer.valueOf(split[i + 1].replace("[[", "")); 181 } 182 } 183 throw new IOException(name + " not found " + firstObj); 184 } 185 getObj(Collection<StringBuilder> objs, String x)186 public static String getObj(Collection<StringBuilder> objs, String x) { 187 for (StringBuilder s : objs) { 188 if (s.toString().contains(x)) { 189 return s.toString(); 190 } 191 } 192 return null; 193 } 194 } 195