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 9.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  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 javax.xml.transform.stream.StreamResult;
  25. import org.junit.Test;
  26. import org.mockito.invocation.InvocationOnMock;
  27. import org.mockito.stubbing.Answer;
  28. import static org.junit.Assert.assertEquals;
  29. import static org.mockito.Matchers.endsWith;
  30. import static org.mockito.Mockito.anyInt;
  31. import static org.mockito.Mockito.mock;
  32. import static org.mockito.Mockito.spy;
  33. import static org.mockito.Mockito.times;
  34. import static org.mockito.Mockito.verify;
  35. import static org.mockito.Mockito.when;
  36. import org.apache.fop.apps.FOUserAgent;
  37. import org.apache.fop.apps.FopFactory;
  38. import org.apache.fop.fo.Constants;
  39. import org.apache.fop.fonts.FontInfo;
  40. import org.apache.fop.fonts.FontTriplet;
  41. import org.apache.fop.fonts.MultiByteFont;
  42. import org.apache.fop.pdf.PDFDocument;
  43. import org.apache.fop.pdf.PDFProfile;
  44. import org.apache.fop.pdf.PDFStructElem;
  45. import org.apache.fop.pdf.PDFTextUtil;
  46. import org.apache.fop.render.RenderingContext;
  47. import org.apache.fop.render.intermediate.IFContext;
  48. import org.apache.fop.render.intermediate.IFException;
  49. import org.apache.fop.traits.BorderProps;
  50. public class PDFPainterTestCase {
  51. private FOUserAgent foUserAgent;
  52. private PDFContentGenerator pdfContentGenerator;
  53. private PDFDocumentHandler pdfDocumentHandler;
  54. private PDFPainter pdfPainter;
  55. private PDFStructElem elem = new PDFStructElem();
  56. @Test
  57. public void testDrawBorderRect() throws Exception {
  58. // the goal of this test is to check that the drawing of rounded corners in PDF calls
  59. // PDFGraphicsPaiter.cubicBezierTo(); the check is done by verifying that a " c " command is written
  60. // to the PDFContentGenerator
  61. createPDFPainter(false);
  62. // build rectangle 200 x 50 (points, which are converted to milipoints)
  63. Rectangle rectangle = new Rectangle(0, 0, 200000, 50000);
  64. // build border properties: width 4pt, radius 30pt
  65. BorderProps border = new BorderProps(Constants.EN_SOLID, 4000, 30000, 30000, Color.BLACK,
  66. BorderProps.Mode.SEPARATE);
  67. pdfPainter.drawBorderRect(rectangle, border, border, border, border, Color.WHITE);
  68. // since we cannot mock the PDFContentGenerator.format() static method we have to restrict the
  69. // verification to commands that end with " c ".
  70. verify(pdfContentGenerator, times(16)).add(endsWith(" c "));
  71. }
  72. private void createPDFPainter(boolean accessibility) {
  73. mockFOUserAgent(accessibility);
  74. mockPDFContentGenerator();
  75. mockPDFDocumentHandler();
  76. PDFLogicalStructureHandler handler = mock(PDFLogicalStructureHandler.class);
  77. pdfPainter = new PDFPainter(pdfDocumentHandler, handler);
  78. }
  79. private void mockFOUserAgent(boolean accessibility) {
  80. foUserAgent = mock(FOUserAgent.class);
  81. when(foUserAgent.isAccessibilityEnabled()).thenReturn(accessibility);
  82. }
  83. private void mockPDFContentGenerator() {
  84. pdfContentGenerator = mock(PDFContentGenerator.class);
  85. }
  86. private void mockPDFDocumentHandler() {
  87. pdfDocumentHandler = mock(PDFDocumentHandler.class);
  88. when(pdfDocumentHandler.getGenerator()).thenReturn(pdfContentGenerator);
  89. IFContext ifContext = mock(IFContext.class);
  90. when(ifContext.getUserAgent()).thenReturn(foUserAgent);
  91. when(pdfDocumentHandler.getContext()).thenReturn(ifContext);
  92. when(ifContext.getStructureTreeElement()).thenReturn(elem);
  93. }
  94. private PDFDocument createMockPDFDocument() {
  95. PDFDocument pdfDoc = mock(PDFDocument.class);
  96. when(pdfContentGenerator.getDocument()).thenReturn(pdfDoc);
  97. when(pdfDocumentHandler.getPDFDocument()).thenReturn(pdfDoc);
  98. when(pdfDoc.getProfile()).thenReturn(new PDFProfile(pdfDoc));
  99. return pdfDoc;
  100. }
  101. @Test
  102. public void testPageNumber() throws IFException {
  103. FopFactory fopFactory = FopFactory.newInstance(new File(".").toURI());
  104. foUserAgent = fopFactory.newFOUserAgent();
  105. pdfDocumentHandler = new PDFDocumentHandler(new IFContext(foUserAgent));
  106. pdfDocumentHandler.setResult(new StreamResult(new ByteArrayOutputStream()));
  107. pdfDocumentHandler.startDocument();
  108. pdfDocumentHandler.startPage(0, "", "", new Dimension());
  109. pdfDocumentHandler.getContext().setPageNumber(3);
  110. MyPDFPainter pdfPainter = new MyPDFPainter(pdfDocumentHandler, null);
  111. pdfPainter.drawImage("test/resources/images/cmyk.jpg", new Rectangle());
  112. assertEquals(pdfPainter.renderingContext.getHints().get("page-number"), 3);
  113. }
  114. class MyPDFPainter extends PDFPainter {
  115. protected RenderingContext renderingContext;
  116. public MyPDFPainter(PDFDocumentHandler documentHandler, PDFLogicalStructureHandler logicalStructureHandler) {
  117. super(documentHandler, logicalStructureHandler);
  118. }
  119. protected RenderingContext createRenderingContext() {
  120. renderingContext = super.createRenderingContext();
  121. return renderingContext;
  122. }
  123. }
  124. @Test
  125. public void testSimulateStyle() throws IFException {
  126. final StringBuilder sb = new StringBuilder();
  127. pdfDocumentHandler = makePDFDocumentHandler(sb);
  128. FontInfo fi = new FontInfo();
  129. fi.addFontProperties("f1", new FontTriplet("a", "italic", 700));
  130. MultiByteFont font = new MultiByteFont(null, null);
  131. font.setSimulateStyle(true);
  132. fi.addMetrics("f1", font);
  133. pdfDocumentHandler.setFontInfo(fi);
  134. MyPDFPainter pdfPainter = new MyPDFPainter(pdfDocumentHandler, null);
  135. pdfPainter.setFont("a", "italic", 700, null, 12, null);
  136. pdfPainter.drawText(0, 0, 0, 0, null, "test");
  137. assertEquals(sb.toString(), "BT\n/f1 0.012 Tf\n1 0 0.3333 -1 0 0 Tm [<0000000000000000>] TJ\n");
  138. verify(pdfContentGenerator).add("q\n");
  139. verify(pdfContentGenerator).add("2 Tr 0.31543 w\n");
  140. verify(pdfContentGenerator).add("Q\n");
  141. }
  142. @Test
  143. public void testDrawTextWithMultiByteFont() throws IFException {
  144. StringBuilder output = new StringBuilder();
  145. PDFDocumentHandler pdfDocumentHandler = makePDFDocumentHandler(output);
  146. //0x48 0x65 0x6C 0x6C 0x6F 0x20 0x4D 0x6F 0x63 0x6B 0x21 0x1F4A9
  147. String text = "Hello Mock!\uD83D\uDCA9";
  148. String expectedHex = "00480065006C006C006F0020004D006F0063006B002101F4A9";
  149. MultiByteFont font = spy(new MultiByteFont(null, null));
  150. when(font.mapCodePoint(anyInt())).thenAnswer(new FontMapCodepointAnswer());
  151. FontInfo fi = new FontInfo();
  152. fi.addFontProperties("f1", new FontTriplet("a", "normal", 400));
  153. fi.addMetrics("f1", font);
  154. pdfDocumentHandler.setFontInfo(fi);
  155. MyPDFPainter pdfPainter = new MyPDFPainter(pdfDocumentHandler, null);
  156. pdfPainter.setFont("a", "normal", 400, null, 12, null);
  157. pdfPainter.drawText(0, 0, 0, 0, null, text);
  158. assertEquals("BT\n/f1 0.012 Tf\n1 0 0 -1 0 0 Tm [<" + expectedHex + ">] TJ\n", output.toString());
  159. }
  160. private PDFDocumentHandler makePDFDocumentHandler(final StringBuilder sb) throws IFException {
  161. FopFactory fopFactory = FopFactory.newInstance(new File(".").toURI());
  162. foUserAgent = fopFactory.newFOUserAgent();
  163. mockPDFContentGenerator();
  164. PDFTextUtil pdfTextUtil = new PDFTextUtil() {
  165. protected void write(String code) {
  166. sb.append(code);
  167. }
  168. protected void write(StringBuffer code) {
  169. sb.append(code);
  170. }
  171. };
  172. pdfTextUtil.beginTextObject();
  173. when(pdfContentGenerator.getTextUtil()).thenReturn(pdfTextUtil);
  174. PDFDocumentHandler pdfDocumentHandler = new PDFDocumentHandler(new IFContext(foUserAgent)) {
  175. PDFContentGenerator getGenerator() {
  176. return pdfContentGenerator;
  177. }
  178. };
  179. pdfDocumentHandler.setResult(new StreamResult(new ByteArrayOutputStream()));
  180. pdfDocumentHandler.startDocument();
  181. pdfDocumentHandler.startPage(0, "", "", new Dimension());
  182. return pdfDocumentHandler;
  183. }
  184. private static class FontMapCodepointAnswer implements Answer<Integer> {
  185. @Override
  186. public Integer answer(InvocationOnMock invocation) throws Throwable {
  187. return (Integer) invocation.getArguments()[0];
  188. }
  189. }
  190. }