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.

PDFPainterTestCase.java 13KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  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.render.pdf;
  19. import java.awt.Color;
  20. import java.awt.Dimension;
  21. import java.awt.Rectangle;
  22. import java.io.ByteArrayOutputStream;
  23. import java.io.File;
  24. import java.io.IOException;
  25. import java.util.Locale;
  26. import javax.xml.transform.stream.StreamResult;
  27. import org.junit.Assert;
  28. import org.junit.Test;
  29. import org.mockito.invocation.InvocationOnMock;
  30. import org.mockito.stubbing.Answer;
  31. import static org.junit.Assert.assertEquals;
  32. import static org.mockito.Matchers.endsWith;
  33. import static org.mockito.Mockito.anyInt;
  34. import static org.mockito.Mockito.mock;
  35. import static org.mockito.Mockito.spy;
  36. import static org.mockito.Mockito.times;
  37. import static org.mockito.Mockito.verify;
  38. import static org.mockito.Mockito.when;
  39. import org.apache.fop.apps.FOUserAgent;
  40. import org.apache.fop.apps.FopFactory;
  41. import org.apache.fop.fo.Constants;
  42. import org.apache.fop.fonts.FontInfo;
  43. import org.apache.fop.fonts.FontTriplet;
  44. import org.apache.fop.fonts.MultiByteFont;
  45. import org.apache.fop.pdf.PDFDocument;
  46. import org.apache.fop.pdf.PDFFilterList;
  47. import org.apache.fop.pdf.PDFPage;
  48. import org.apache.fop.pdf.PDFProfile;
  49. import org.apache.fop.pdf.PDFResources;
  50. import org.apache.fop.pdf.PDFStructElem;
  51. import org.apache.fop.pdf.PDFTextUtil;
  52. import org.apache.fop.pdf.PDFUAMode;
  53. import org.apache.fop.pdf.StandardStructureTypes;
  54. import org.apache.fop.render.RenderingContext;
  55. import org.apache.fop.render.intermediate.IFContext;
  56. import org.apache.fop.render.intermediate.IFException;
  57. import org.apache.fop.traits.BorderProps;
  58. public class PDFPainterTestCase {
  59. private FOUserAgent foUserAgent;
  60. private PDFContentGenerator pdfContentGenerator;
  61. private PDFDocumentHandler pdfDocumentHandler;
  62. private PDFPainter pdfPainter;
  63. private PDFStructElem elem = new PDFStructElem();
  64. @Test
  65. public void testDrawBorderRect() throws Exception {
  66. // the goal of this test is to check that the drawing of rounded corners in PDF calls
  67. // PDFGraphicsPaiter.cubicBezierTo(); the check is done by verifying that a " c " command is written
  68. // to the PDFContentGenerator
  69. createPDFPainter(false);
  70. // build rectangle 200 x 50 (points, which are converted to milipoints)
  71. Rectangle rectangle = new Rectangle(0, 0, 200000, 50000);
  72. // build border properties: width 4pt, radius 30pt
  73. BorderProps border = new BorderProps(Constants.EN_SOLID, 4000, 30000, 30000, Color.BLACK,
  74. BorderProps.Mode.SEPARATE);
  75. pdfPainter.drawBorderRect(rectangle, border, border, border, border, Color.WHITE);
  76. // since we cannot mock the PDFContentGenerator.format() static method we have to restrict the
  77. // verification to commands that end with " c ".
  78. verify(pdfContentGenerator, times(16)).add(endsWith(" c "));
  79. }
  80. private void createPDFPainter(boolean accessibility) {
  81. mockFOUserAgent(accessibility);
  82. mockPDFContentGenerator();
  83. mockPDFDocumentHandler();
  84. PDFLogicalStructureHandler handler = mock(PDFLogicalStructureHandler.class);
  85. pdfPainter = new PDFPainter(pdfDocumentHandler, handler);
  86. }
  87. private void mockFOUserAgent(boolean accessibility) {
  88. foUserAgent = mock(FOUserAgent.class);
  89. when(foUserAgent.isAccessibilityEnabled()).thenReturn(accessibility);
  90. }
  91. private void mockPDFContentGenerator() {
  92. pdfContentGenerator = mock(PDFContentGenerator.class);
  93. }
  94. private void mockPDFDocumentHandler() {
  95. pdfDocumentHandler = mock(PDFDocumentHandler.class);
  96. when(pdfDocumentHandler.getGenerator()).thenReturn(pdfContentGenerator);
  97. IFContext ifContext = mock(IFContext.class);
  98. when(ifContext.getUserAgent()).thenReturn(foUserAgent);
  99. when(pdfDocumentHandler.getContext()).thenReturn(ifContext);
  100. when(ifContext.getStructureTreeElement()).thenReturn(elem);
  101. }
  102. private PDFDocument createMockPDFDocument() {
  103. PDFDocument pdfDoc = mock(PDFDocument.class);
  104. when(pdfContentGenerator.getDocument()).thenReturn(pdfDoc);
  105. when(pdfDocumentHandler.getPDFDocument()).thenReturn(pdfDoc);
  106. when(pdfDoc.getProfile()).thenReturn(new PDFProfile(pdfDoc));
  107. return pdfDoc;
  108. }
  109. @Test
  110. public void testPageNumber() throws IFException {
  111. FopFactory fopFactory = FopFactory.newInstance(new File(".").toURI());
  112. foUserAgent = fopFactory.newFOUserAgent();
  113. pdfDocumentHandler = new PDFDocumentHandler(new IFContext(foUserAgent));
  114. pdfDocumentHandler.setResult(new StreamResult(new ByteArrayOutputStream()));
  115. pdfDocumentHandler.startDocument();
  116. pdfDocumentHandler.startPage(0, "", "", new Dimension());
  117. pdfDocumentHandler.getContext().setPageNumber(3);
  118. MyPDFPainter pdfPainter = new MyPDFPainter(pdfDocumentHandler, null);
  119. pdfPainter.drawImage("test/resources/images/cmyk.jpg", new Rectangle());
  120. assertEquals(pdfPainter.renderingContext.getHints().get("page-number"), 3);
  121. }
  122. class MyPDFPainter extends PDFPainter {
  123. protected RenderingContext renderingContext;
  124. public MyPDFPainter(PDFDocumentHandler documentHandler, PDFLogicalStructureHandler logicalStructureHandler) {
  125. super(documentHandler, logicalStructureHandler);
  126. }
  127. protected RenderingContext createRenderingContext() {
  128. renderingContext = super.createRenderingContext();
  129. return renderingContext;
  130. }
  131. }
  132. @Test
  133. public void testSimulateStyle() throws IFException {
  134. final StringBuilder sb = new StringBuilder();
  135. pdfDocumentHandler = makePDFDocumentHandler(sb);
  136. FontInfo fi = new FontInfo();
  137. fi.addFontProperties("f1", new FontTriplet("a", "italic", 700));
  138. MultiByteFont font = new MultiByteFont(null, null);
  139. font.setSimulateStyle(true);
  140. fi.addMetrics("f1", font);
  141. pdfDocumentHandler.setFontInfo(fi);
  142. MyPDFPainter pdfPainter = new MyPDFPainter(pdfDocumentHandler, null);
  143. pdfPainter.setFont("a", "italic", 700, null, 12, null);
  144. pdfPainter.drawText(0, 0, 0, 0, null, "test");
  145. assertEquals(sb.toString(), "BT\n/f1 0.012 Tf\n1 0 0.3333 -1 0 0 Tm [<0000000000000000>] TJ\n");
  146. verify(pdfContentGenerator).add("q\n");
  147. verify(pdfContentGenerator).add("2 Tr 0.31543 w\n");
  148. verify(pdfContentGenerator).add("Q\n");
  149. }
  150. @Test
  151. public void testDrawTextWithMultiByteFont() throws IFException {
  152. StringBuilder output = new StringBuilder();
  153. PDFDocumentHandler pdfDocumentHandler = makePDFDocumentHandler(output);
  154. //0x48 0x65 0x6C 0x6C 0x6F 0x20 0x4D 0x6F 0x63 0x6B 0x21 0x1F4A9
  155. String text = "Hello Mock!\uD83D\uDCA9";
  156. String expectedHex = "00480065006C006C006F0020004D006F0063006B002101F4A9";
  157. MultiByteFont font = spy(new MultiByteFont(null, null));
  158. when(font.mapCodePoint(anyInt())).thenAnswer(new FontMapCodepointAnswer());
  159. FontInfo fi = new FontInfo();
  160. fi.addFontProperties("f1", new FontTriplet("a", "normal", 400));
  161. fi.addMetrics("f1", font);
  162. pdfDocumentHandler.setFontInfo(fi);
  163. MyPDFPainter pdfPainter = new MyPDFPainter(pdfDocumentHandler, null);
  164. pdfPainter.setFont("a", "normal", 400, null, 12, null);
  165. pdfPainter.drawText(0, 0, 0, 0, null, text);
  166. assertEquals("BT\n/f1 0.012 Tf\n1 0 0 -1 0 0 Tm [<" + expectedHex + ">] TJ\n", output.toString());
  167. }
  168. private PDFDocumentHandler makePDFDocumentHandler(final StringBuilder sb) throws IFException {
  169. FopFactory fopFactory = FopFactory.newInstance(new File(".").toURI());
  170. foUserAgent = fopFactory.newFOUserAgent();
  171. mockPDFContentGenerator();
  172. PDFTextUtil pdfTextUtil = new PDFTextUtil() {
  173. protected void write(String code) {
  174. sb.append(code);
  175. }
  176. protected void write(StringBuffer code) {
  177. sb.append(code);
  178. }
  179. };
  180. pdfTextUtil.beginTextObject();
  181. when(pdfContentGenerator.getTextUtil()).thenReturn(pdfTextUtil);
  182. PDFDocumentHandler pdfDocumentHandler = new PDFDocumentHandler(new IFContext(foUserAgent)) {
  183. PDFContentGenerator getGenerator() {
  184. return pdfContentGenerator;
  185. }
  186. };
  187. pdfDocumentHandler.setResult(new StreamResult(new ByteArrayOutputStream()));
  188. pdfDocumentHandler.startDocument();
  189. pdfDocumentHandler.startPage(0, "", "", new Dimension());
  190. return pdfDocumentHandler;
  191. }
  192. private static class FontMapCodepointAnswer implements Answer<Integer> {
  193. @Override
  194. public Integer answer(InvocationOnMock invocation) throws Throwable {
  195. return (Integer) invocation.getArguments()[0];
  196. }
  197. }
  198. @Test
  199. public void testPDFUAImage() throws IFException, IOException {
  200. FopFactory fopFactory = FopFactory.newInstance(new File(".").toURI());
  201. foUserAgent = fopFactory.newFOUserAgent();
  202. foUserAgent.setAccessibility(true);
  203. IFContext ifContext = new IFContext(foUserAgent);
  204. pdfDocumentHandler = new PDFDocumentHandler(ifContext);
  205. pdfDocumentHandler.getStructureTreeEventHandler();
  206. pdfDocumentHandler.setResult(new StreamResult(new ByteArrayOutputStream()));
  207. pdfDocumentHandler.startDocument();
  208. pdfDocumentHandler.startPage(0, "", "", new Dimension());
  209. PDFDocument doc = pdfDocumentHandler.getPDFDocument();
  210. doc.getProfile().setPDFUAMode(PDFUAMode.PDFUA_1);
  211. doc.getInfo().setTitle("a");
  212. PDFLogicalStructureHandler structureHandler = new PDFLogicalStructureHandler(doc);
  213. structureHandler.startPage(new PDFPage(new PDFResources(doc), 0,
  214. new Rectangle(), new Rectangle(), new Rectangle(), new Rectangle()));
  215. PDFPainter pdfPainter = new PDFPainter(pdfDocumentHandler, structureHandler);
  216. ifContext.setLanguage(Locale.US);
  217. drawImage(doc, pdfPainter, ifContext);
  218. String output = drawImage(doc, pdfPainter, ifContext);
  219. Assert.assertTrue(output, output.contains("/BBox [0 0 0 0]"));
  220. }
  221. private String drawImage(PDFDocument doc, PDFPainter pdfPainter, IFContext ifContext)
  222. throws IOException, IFException {
  223. PDFStructElem structElem = new PDFStructElem(doc.getRoot(), StandardStructureTypes.InlineLevelStructure.NOTE);
  224. structElem.setDocument(doc);
  225. ifContext.setStructureTreeElement(structElem);
  226. pdfPainter.drawImage("test/resources/images/cmyk.jpg", new Rectangle());
  227. ByteArrayOutputStream bos = new ByteArrayOutputStream();
  228. structElem.output(bos);
  229. return bos.toString();
  230. }
  231. @Test
  232. public void testFooterText() throws IFException, IOException {
  233. FopFactory fopFactory = FopFactory.newInstance(new File(".").toURI());
  234. foUserAgent = fopFactory.newFOUserAgent();
  235. foUserAgent.setAccessibility(true);
  236. PDFDocumentHandler pdfDocumentHandler = new PDFDocumentHandler(new IFContext(foUserAgent));
  237. pdfDocumentHandler.getStructureTreeEventHandler();
  238. pdfDocumentHandler.setResult(new StreamResult(new ByteArrayOutputStream()));
  239. pdfDocumentHandler.startDocument();
  240. pdfDocumentHandler.startPage(0, "", "", new Dimension());
  241. FontInfo fi = new FontInfo();
  242. fi.addFontProperties("f1", new FontTriplet("a", "italic", 700));
  243. MultiByteFont font = new MultiByteFont(null, null);
  244. font.setWidthArray(new int[1]);
  245. fi.addMetrics("f1", font);
  246. pdfDocumentHandler.setFontInfo(fi);
  247. PDFDocument doc = pdfDocumentHandler.getPDFDocument();
  248. PDFLogicalStructureHandler structureHandler = new PDFLogicalStructureHandler(doc);
  249. MyPDFPainter pdfPainter = new MyPDFPainter(pdfDocumentHandler, structureHandler);
  250. pdfPainter.getContext().setRegionType(Constants.FO_REGION_AFTER);
  251. pdfPainter.setFont("a", "italic", 700, null, 12, null);
  252. pdfPainter.drawText(0, 0, 0, 0, null, "test");
  253. ByteArrayOutputStream bos = new ByteArrayOutputStream();
  254. PDFFilterList filters = pdfPainter.generator.getStream().getFilterList();
  255. filters.setDisableAllFilters(true);
  256. pdfPainter.generator.getStream().output(bos);
  257. Assert.assertEquals(bos.toString(), "<< /Length 1 0 R >>\n"
  258. + "stream\n"
  259. + "q\n"
  260. + "1 0 0 -1 0 0 cm\n"
  261. + "/Artifact\n"
  262. + "<</Type /Pagination\n"
  263. + "/Subtype /Footer>>\n"
  264. + "BDC\n"
  265. + "BT\n"
  266. + "/f1 0.012 Tf\n"
  267. + "1 0 0 -1 0 0 Tm [<0000000000000000>] TJ\n"
  268. + "\n"
  269. + "endstream");
  270. }
  271. }