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.

ImageHandlingTestCase.java 8.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  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.ps;
  19. import java.awt.Rectangle;
  20. import java.io.ByteArrayOutputStream;
  21. import java.io.File;
  22. import java.io.IOException;
  23. import java.io.InputStream;
  24. import org.junit.Test;
  25. import org.w3c.dom.Document;
  26. import static org.junit.Assert.assertEquals;
  27. import static org.junit.Assert.assertTrue;
  28. import org.apache.commons.io.IOUtils;
  29. import org.apache.batik.anim.dom.SAXSVGDocumentFactory;
  30. import org.apache.xmlgraphics.image.loader.impl.ImageXMLDOM;
  31. import org.apache.xmlgraphics.ps.DSCConstants;
  32. import org.apache.xmlgraphics.ps.PSGenerator;
  33. import org.apache.xmlgraphics.ps.PSResource;
  34. import org.apache.xmlgraphics.ps.dsc.DSCException;
  35. import org.apache.xmlgraphics.ps.dsc.DSCParser;
  36. import org.apache.xmlgraphics.ps.dsc.events.DSCCommentPage;
  37. import org.apache.xmlgraphics.ps.dsc.events.DSCCommentPages;
  38. import org.apache.xmlgraphics.ps.dsc.events.DSCCommentTitle;
  39. import org.apache.xmlgraphics.ps.dsc.events.DSCEvent;
  40. import org.apache.fop.apps.FOUserAgent;
  41. import org.apache.fop.apps.FopFactory;
  42. import org.apache.fop.fonts.FontInfo;
  43. import org.apache.fop.render.intermediate.IFContext;
  44. /**
  45. * Tests the image handling in PostScript output.
  46. */
  47. public class ImageHandlingTestCase extends AbstractPostScriptTest {
  48. /**
  49. * Tests JPEG handling.
  50. * @throws Exception if an error occurs
  51. */
  52. @Test
  53. public void testJPEGImageLevel3() throws Exception {
  54. innerTestJPEGImage(3);
  55. }
  56. /**
  57. * Tests JPEG handling.
  58. * @throws Exception if an error occurs
  59. */
  60. @Test
  61. public void testJPEGImageLevel2() throws Exception {
  62. innerTestJPEGImage(2);
  63. }
  64. private void innerTestJPEGImage(int level) throws Exception {
  65. FOUserAgent ua = fopFactory.newFOUserAgent();
  66. PSDocumentHandler handler = new PSDocumentHandler(new IFContext(ua));
  67. PSRenderingUtil psUtil = handler.getPSUtil();
  68. psUtil.setLanguageLevel(level);
  69. psUtil.setOptimizeResources(true);
  70. ua.setDocumentHandlerOverride(handler);
  71. // Prepare output file
  72. File outputFile = renderFile(ua, "ps-jpeg-image.fo",
  73. "-if-l" + psUtil.getLanguageLevel());
  74. verifyPostScriptFile(outputFile, psUtil.getLanguageLevel());
  75. }
  76. private void verifyPostScriptFile(File psFile, int level)
  77. throws IOException, DSCException {
  78. InputStream in = new java.io.FileInputStream(psFile);
  79. in = new java.io.BufferedInputStream(in);
  80. try {
  81. DSCParser parser = new DSCParser(in);
  82. DSCCommentPages pages = (DSCCommentPages)gotoDSCComment(parser, DSCConstants.PAGES);
  83. assertEquals(1, pages.getPageCount());
  84. //Skip procsets and encoding
  85. gotoDSCComment(parser, DSCConstants.BEGIN_RESOURCE);
  86. gotoDSCComment(parser, DSCConstants.BEGIN_RESOURCE);
  87. gotoDSCComment(parser, DSCConstants.BEGIN_RESOURCE);
  88. gotoDSCComment(parser, DSCConstants.BEGIN_RESOURCE);
  89. PSResource form2 = new PSResource(PSResource.TYPE_FORM, "FOPForm:2");
  90. checkResourceComment(parser, DSCConstants.BEGIN_RESOURCE, form2);
  91. DSCCommentTitle title = (DSCCommentTitle)parser.nextEvent().asDSCComment();
  92. assertEquals("image/jpeg test/resources/images/bgimg300dpi.jpg", title.getTitle());
  93. String resourceContent = getResourceContent(parser);
  94. if (level == 3) {
  95. assertContains(resourceContent, "/FOPForm:2");
  96. assertContains(resourceContent, "/DCTDecode filter");
  97. assertContains(resourceContent, "/ReusableStreamDecode filter");
  98. } else {
  99. assertContains(resourceContent, "/FOPForm:2");
  100. assertContains(resourceContent, "/DCTDecode filter");
  101. assertAbsent(resourceContent, "/ReusableStreamDecode filter");
  102. }
  103. //---=== Page 1 ===---
  104. DSCCommentPage page = (DSCCommentPage)gotoDSCComment(parser, DSCConstants.PAGE);
  105. assertEquals(1, page.getPagePosition());
  106. PSResource form1 = new PSResource(PSResource.TYPE_FORM, "FOPForm:1");
  107. checkResourceComment(parser, DSCConstants.BEGIN_RESOURCE, form1);
  108. title = (DSCCommentTitle)parser.nextEvent().asDSCComment();
  109. assertEquals("image/jpeg test/resources/images/bgimg72dpi.jpg", title.getTitle());
  110. resourceContent = getResourceContent(parser);
  111. if (level == 3) {
  112. assertContains(resourceContent, "/FOPForm:1");
  113. assertContains(resourceContent, "/DCTDecode filter");
  114. assertContains(resourceContent, "/ReusableStreamDecode filter");
  115. } else {
  116. assertContains(resourceContent, "/FOPForm:1");
  117. assertContains(resourceContent, "/DCTDecode filter");
  118. assertAbsent(resourceContent, "/ReusableStreamDecode filter");
  119. }
  120. } finally {
  121. IOUtils.closeQuietly(in);
  122. }
  123. }
  124. private void assertContains(String text, String searchString) {
  125. assertTrue("Text doesn't contain '" + searchString + "'", text.indexOf(searchString) >= 0);
  126. }
  127. private void assertAbsent(String text, String searchString) {
  128. assertTrue("Text contains '" + searchString + "'", text.indexOf(searchString) < 0);
  129. }
  130. private String getResourceContent(DSCParser parser) throws IOException, DSCException {
  131. StringBuffer sb = new StringBuffer();
  132. while (parser.hasNext()) {
  133. DSCEvent event = parser.nextEvent();
  134. if (event.isLine()) {
  135. sb.append(event.asLine().getLine()).append('\n');
  136. } else if (event.isDSCComment()) {
  137. if (DSCConstants.END_RESOURCE.equals(event.asDSCComment().getName())) {
  138. break;
  139. }
  140. }
  141. }
  142. return sb.toString();
  143. }
  144. @Test
  145. public void testPSImageHandlerSVG() throws IOException {
  146. FOUserAgent ua = FopFactory.newInstance(new File(".").toURI()).newFOUserAgent();
  147. String svg = "<svg xmlns:xlink=\"http://www.w3.org/1999/xlink\" xmlns=\"http://www.w3.org/2000/svg\" "
  148. + "width=\"210mm\" height=\"297mm\" font-size=\"20\" stroke-width=\"0.1mm\" font-family=\"Arial\">\n"
  149. + " <filter id=\"drop-shadow-font\" width=\"150%\" height=\"150%\">\n"
  150. + " <feGaussianBlur in=\"SourceAlpha\" result=\"blur\" stdDeviation=\"1\"/>\n"
  151. + " <feOffset in=\"blur\" result=\"offsetBlur\" dy=\"1\" dx=\"1\"/>\n"
  152. + " <feBlend in=\"SourceGraphic\" in2=\"offsetBlur\" mode=\"normal\"/>\n"
  153. + " </filter>\n"
  154. + " <text x=\"4.9mm\" filter=\"url(#drop-shadow-font)\" y=\"10.5mm\" fill=\"black\" "
  155. + "rotate=\"30 30 0 15\">Hello SVG with FOP</text>\n"
  156. + "</svg>";
  157. SAXSVGDocumentFactory factory = new SAXSVGDocumentFactory(null);
  158. Document doc = factory.createDocument(null, IOUtils.toInputStream(svg));
  159. ByteArrayOutputStream bos = new ByteArrayOutputStream();
  160. new PSImageHandlerSVG().handleImage(
  161. new PSRenderingContext(ua, new PSGenerator(bos), new FontInfo()),
  162. new ImageXMLDOM(null, doc, ""),
  163. new Rectangle());
  164. assertTrue(bos.toString().contains("/DataSource Data"));
  165. }
  166. }