You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

PDFVTTestCase.java 8.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  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. /* $Id$ */
  18. package org.apache.fop.pdf;
  19. import java.awt.geom.Rectangle2D;
  20. import java.io.ByteArrayInputStream;
  21. import java.io.ByteArrayOutputStream;
  22. import java.io.File;
  23. import java.io.IOException;
  24. import java.io.InputStream;
  25. import java.util.Collection;
  26. import java.util.Iterator;
  27. import java.util.Map;
  28. import javax.xml.transform.Result;
  29. import javax.xml.transform.Source;
  30. import javax.xml.transform.Transformer;
  31. import javax.xml.transform.TransformerException;
  32. import javax.xml.transform.TransformerFactory;
  33. import javax.xml.transform.sax.SAXResult;
  34. import javax.xml.transform.stream.StreamResult;
  35. import javax.xml.transform.stream.StreamSource;
  36. import org.junit.Assert;
  37. import org.junit.Test;
  38. import org.xml.sax.SAXException;
  39. import org.apache.xmlgraphics.util.QName;
  40. import org.apache.xmlgraphics.xmp.Metadata;
  41. import org.apache.fop.apps.FOUserAgent;
  42. import org.apache.fop.apps.Fop;
  43. import org.apache.fop.apps.FopFactory;
  44. import org.apache.fop.apps.MimeConstants;
  45. import org.apache.fop.render.intermediate.IFContext;
  46. import org.apache.fop.render.intermediate.IFDocumentHandler;
  47. import org.apache.fop.render.intermediate.IFException;
  48. import org.apache.fop.render.intermediate.IFParser;
  49. import org.apache.fop.render.intermediate.IFSerializer;
  50. import org.apache.fop.render.intermediate.IFUtil;
  51. import org.apache.fop.render.pdf.PDFContentGenerator;
  52. public class PDFVTTestCase {
  53. @Test
  54. public void testXMP() throws IOException {
  55. PDFDocument doc = new PDFDocument("");
  56. doc.getProfile().setPDFXMode(PDFXMode.PDFX_4);
  57. doc.getProfile().setPDFVTMode(PDFVTMode.PDFVT_1);
  58. Metadata metadata = PDFMetadata.createXMPFromPDFDocument(doc);
  59. StringBuilder sb = new StringBuilder();
  60. Iterator i = metadata.iterator();
  61. while (i.hasNext()) {
  62. QName k = (QName) i.next();
  63. sb.append(k + ": " + metadata.getProperty(k).getValue() + "\n");
  64. }
  65. String s = sb.toString();
  66. Assert.assertTrue(s.contains("pdfxid:GTS_PDFXVersion: PDF/X-4"));
  67. Assert.assertTrue(s.contains("xmpMM:VersionID: 1"));
  68. Assert.assertTrue(s.contains("pdf:Trapped: False"));
  69. Assert.assertTrue(s.contains("xmpMM:RenditionClass: default"));
  70. Assert.assertTrue(s.contains("pdf:PDFVersion: 1.4"));
  71. Assert.assertTrue(s.contains("pdfvtid:GTS_PDFVTVersion: PDF/VT-1"));
  72. }
  73. @Test
  74. public void testPDF() throws IOException {
  75. PDFDocument doc = new PDFDocument("");
  76. doc.getInfo().setTitle("title");
  77. doc.getProfile().setPDFXMode(PDFXMode.PDFX_4);
  78. doc.getProfile().setPDFVTMode(PDFVTMode.PDFVT_1);
  79. PDFResources resources = new PDFResources(doc);
  80. doc.addObject(resources);
  81. PDFResourceContext context = new PDFResourceContext(resources);
  82. ByteArrayOutputStream out = new ByteArrayOutputStream();
  83. PDFContentGenerator gen = new PDFContentGenerator(doc, out, context);
  84. Rectangle2D.Float f = new Rectangle2D.Float();
  85. PDFPage page = new PDFPage(resources, 0, f, f, f, f);
  86. doc.addImage(context, new BitmapImage("", 1, 1, new byte[0], null));
  87. doc.registerObject(page);
  88. doc.getFactory().makeDPart(page, "master");
  89. gen.flushPDFDoc();
  90. doc.outputTrailer(out);
  91. Collection<StringBuilder> objs = PDFLinearizationTestCase.readObjs(
  92. new ByteArrayInputStream(out.toByteArray())).values();
  93. Assert.assertTrue(getObj(objs, "/Type /Catalog").contains("/DPartRoot "));
  94. Assert.assertTrue(getObj(objs, "/Type /DPartRoot").contains("/NodeNameList [/root /record]"));
  95. Assert.assertTrue(
  96. getObj(objs, "/Subtype /Image").contains("/GTS_XID (uuid:d41d8cd9-8f00-3204-a980-0998ecf8427e)"));
  97. }
  98. @Test
  99. public void textFO() throws IOException, SAXException, TransformerException, IFException {
  100. ByteArrayOutputStream out = new ByteArrayOutputStream();
  101. foToOutput(out, MimeConstants.MIME_PDF);
  102. checkPDF(out);
  103. }
  104. @Test
  105. public void textIF() throws IOException, SAXException, TransformerException, IFException {
  106. ByteArrayOutputStream out = new ByteArrayOutputStream();
  107. foToOutput(out, MimeConstants.MIME_FOP_IF);
  108. iFToPDF(new ByteArrayInputStream(out.toByteArray()));
  109. }
  110. private void foToOutput(ByteArrayOutputStream out, String mimeFopIf)
  111. throws IOException, SAXException, TransformerException {
  112. FopFactory fopFactory = getFopFactory();
  113. FOUserAgent userAgent = fopFactory.newFOUserAgent();
  114. if (mimeFopIf.equals(MimeConstants.MIME_FOP_IF)) {
  115. IFSerializer serializer = new IFSerializer(new IFContext(userAgent));
  116. IFDocumentHandler targetHandler
  117. = userAgent.getRendererFactory().createDocumentHandler(userAgent, MimeConstants.MIME_PDF);
  118. serializer.mimicDocumentHandler(targetHandler);
  119. userAgent.setDocumentHandlerOverride(serializer);
  120. }
  121. Fop fop = fopFactory.newFop(mimeFopIf, userAgent, out);
  122. Transformer transformer = TransformerFactory.newInstance().newTransformer();
  123. Source src = new StreamSource(PDFVTTestCase.class.getResource("PDFVT.fo").openStream());
  124. Result res = new SAXResult(fop.getDefaultHandler());
  125. transformer.transform(src, res);
  126. }
  127. private FopFactory getFopFactory() throws IOException, SAXException {
  128. return FopFactory.newInstance(new File(".").toURI(),
  129. PDFVTTestCase.class.getResource("PDFVT.xconf").openStream());
  130. }
  131. private void iFToPDF(InputStream is) throws IOException, SAXException, TransformerException, IFException {
  132. ByteArrayOutputStream out = new ByteArrayOutputStream();
  133. FOUserAgent userAgent = getFopFactory().newFOUserAgent();
  134. Transformer transformer = TransformerFactory.newInstance().newTransformer();
  135. Source src = new StreamSource(is);
  136. IFDocumentHandler documentHandler
  137. = userAgent.getRendererFactory().createDocumentHandler(userAgent, MimeConstants.MIME_PDF);
  138. documentHandler.setResult(new StreamResult(out));
  139. IFUtil.setupFonts(documentHandler);
  140. IFParser parser = new IFParser();
  141. Result res = new SAXResult(parser.getContentHandler(documentHandler, userAgent));
  142. transformer.transform(src, res);
  143. checkPDF(out);
  144. }
  145. private void checkPDF(ByteArrayOutputStream out) throws IOException {
  146. Map<String, StringBuilder> objs =
  147. PDFLinearizationTestCase.readObjs(new ByteArrayInputStream(out.toByteArray()));
  148. String dpart = getObj(objs.values(), "/DParts");
  149. int v = getValue("/DParts", dpart);
  150. String dpm = objs.get(v + " 0 obj").toString();
  151. Assert.assertTrue(dpm.contains(
  152. "/DPM << /CIP4_Root << /CIP4_Production << /CIP4_Part << /CIP4_ProductType (frontpages) >>"));
  153. }
  154. private int getValue(String name, String firstObj) throws IOException {
  155. String[] split = firstObj.split(" ");
  156. for (int i = 0; i < split.length; i++) {
  157. if (split[i].equals(name)) {
  158. return Integer.valueOf(split[i + 1].replace("[[", ""));
  159. }
  160. }
  161. throw new IOException(name + " not found " + firstObj);
  162. }
  163. public static String getObj(Collection<StringBuilder> objs, String x) {
  164. for (StringBuilder s : objs) {
  165. if (s.toString().contains(x)) {
  166. return s.toString();
  167. }
  168. }
  169. return null;
  170. }
  171. }