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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  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.io.File;
  20. import java.io.IOException;
  21. import java.io.InputStream;
  22. import org.apache.commons.io.IOUtils;
  23. import org.apache.xmlgraphics.ps.DSCConstants;
  24. import org.apache.xmlgraphics.ps.PSResource;
  25. import org.apache.xmlgraphics.ps.dsc.DSCException;
  26. import org.apache.xmlgraphics.ps.dsc.DSCParser;
  27. import org.apache.xmlgraphics.ps.dsc.events.DSCCommentPage;
  28. import org.apache.xmlgraphics.ps.dsc.events.DSCCommentPages;
  29. import org.apache.xmlgraphics.ps.dsc.events.DSCCommentTitle;
  30. import org.apache.xmlgraphics.ps.dsc.events.DSCEvent;
  31. import org.apache.fop.apps.FOUserAgent;
  32. /**
  33. * Tests the image handling in PostScript output.
  34. */
  35. public class ImageHandlingTestCase extends AbstractPostScriptTestCase {
  36. /**
  37. * Tests JPEG handling with the {@link PSRenderer}.
  38. * @throws Exception if an error occurs
  39. */
  40. public void testJPEGImageWithRendererLevel3() throws Exception {
  41. innerTestJPEGImageWithRenderer(3);
  42. }
  43. /**
  44. * Tests JPEG handling with the {@link PSRenderer}.
  45. * @throws Exception if an error occurs
  46. */
  47. public void testJPEGImageWithRendererLevel2() throws Exception {
  48. innerTestJPEGImageWithRenderer(2);
  49. }
  50. /**
  51. * Tests JPEG handling with the {@link PSDocumentHandler}.
  52. * @throws Exception if an error occurs
  53. */
  54. public void testJPEGImageWithIFLevel3() throws Exception {
  55. innerTestJPEGImageWithIF(3);
  56. }
  57. /**
  58. * Tests JPEG handling with the {@link PSDocumentHandler}.
  59. * @throws Exception if an error occurs
  60. */
  61. public void testJPEGImageWithIFLevel2() throws Exception {
  62. innerTestJPEGImageWithIF(2);
  63. }
  64. private void innerTestJPEGImageWithRenderer(int level) throws Exception {
  65. FOUserAgent ua = fopFactory.newFOUserAgent();
  66. PSRenderer renderer = new PSRenderer();
  67. renderer.setUserAgent(ua);
  68. PSRenderingUtil psUtil = renderer.getPSUtil();
  69. psUtil.setLanguageLevel(level);
  70. psUtil.setOptimizeResources(true);
  71. ua.setRendererOverride(renderer);
  72. // Prepare output file
  73. File outputFile = renderFile(ua, "ps-jpeg-image.fo", "-rend-l" + psUtil.getLanguageLevel());
  74. verifyPostScriptFile(outputFile, psUtil.getLanguageLevel());
  75. }
  76. private void innerTestJPEGImageWithIF(int level) throws Exception {
  77. FOUserAgent ua = fopFactory.newFOUserAgent();
  78. PSDocumentHandler handler = new PSDocumentHandler();
  79. handler.setUserAgent(ua);
  80. PSRenderingUtil psUtil = handler.getPSUtil();
  81. psUtil.setLanguageLevel(level);
  82. psUtil.setOptimizeResources(true);
  83. ua.setDocumentHandlerOverride(handler);
  84. // Prepare output file
  85. File outputFile = renderFile(ua, "ps-jpeg-image.fo", "-if-l" + psUtil.getLanguageLevel());
  86. verifyPostScriptFile(outputFile, psUtil.getLanguageLevel());
  87. }
  88. private void verifyPostScriptFile(File psFile, int level)
  89. throws IOException, DSCException {
  90. InputStream in = new java.io.FileInputStream(psFile);
  91. in = new java.io.BufferedInputStream(in);
  92. try {
  93. DSCParser parser = new DSCParser(in);
  94. DSCCommentPages pages = (DSCCommentPages)gotoDSCComment(parser, DSCConstants.PAGES);
  95. assertEquals(1, pages.getPageCount());
  96. //Skip procsets and encoding
  97. gotoDSCComment(parser, DSCConstants.BEGIN_RESOURCE);
  98. gotoDSCComment(parser, DSCConstants.BEGIN_RESOURCE);
  99. gotoDSCComment(parser, DSCConstants.BEGIN_RESOURCE);
  100. PSResource form2 = new PSResource(PSResource.TYPE_FORM, "FOPForm:2");
  101. checkResourceComment(parser, DSCConstants.BEGIN_RESOURCE, form2);
  102. DSCCommentTitle title = (DSCCommentTitle)parser.nextEvent().asDSCComment();
  103. assertEquals("image/jpeg test/resources/images/bgimg300dpi.jpg", title.getTitle());
  104. String resourceContent = getResourceContent(parser);
  105. if (level == 3) {
  106. assertContains(resourceContent, "/FOPForm:2");
  107. assertContains(resourceContent, "/DCTDecode filter");
  108. assertContains(resourceContent, "/ReusableStreamDecode filter");
  109. } else {
  110. assertContains(resourceContent, "/FOPForm:2");
  111. assertContains(resourceContent, "/DCTDecode filter");
  112. assertAbsent(resourceContent, "/ReusableStreamDecode filter");
  113. }
  114. //---=== Page 1 ===---
  115. DSCCommentPage page = (DSCCommentPage)gotoDSCComment(parser, DSCConstants.PAGE);
  116. assertEquals(1, page.getPagePosition());
  117. PSResource form1 = new PSResource(PSResource.TYPE_FORM, "FOPForm:1");
  118. checkResourceComment(parser, DSCConstants.BEGIN_RESOURCE, form1);
  119. title = (DSCCommentTitle)parser.nextEvent().asDSCComment();
  120. assertEquals("image/jpeg test/resources/images/bgimg72dpi.jpg", title.getTitle());
  121. resourceContent = getResourceContent(parser);
  122. if (level == 3) {
  123. assertContains(resourceContent, "/FOPForm:1");
  124. assertContains(resourceContent, "/DCTDecode filter");
  125. assertContains(resourceContent, "/ReusableStreamDecode filter");
  126. } else {
  127. assertContains(resourceContent, "/FOPForm:1");
  128. assertContains(resourceContent, "/DCTDecode filter");
  129. assertAbsent(resourceContent, "/ReusableStreamDecode filter");
  130. }
  131. } finally {
  132. IOUtils.closeQuietly(in);
  133. }
  134. }
  135. private void assertMatches(String text, String regex) {
  136. assertTrue("Text didn't match '" + regex + "'", text.matches(regex));
  137. }
  138. private void assertContains(String text, String searchString) {
  139. assertTrue("Text doesn't contain '" + searchString + "'", text.indexOf(searchString) >= 0);
  140. }
  141. private void assertAbsent(String text, String searchString) {
  142. assertTrue("Text contains '" + searchString + "'", text.indexOf(searchString) < 0);
  143. }
  144. private String getResourceContent(DSCParser parser) throws IOException, DSCException {
  145. StringBuffer sb = new StringBuffer();
  146. while (parser.hasNext()) {
  147. DSCEvent event = parser.nextEvent();
  148. if (event.isLine()) {
  149. sb.append(event.asLine().getLine()).append('\n');
  150. } else if (event.isDSCComment()) {
  151. if (DSCConstants.END_RESOURCE.equals(event.asDSCComment().getName())) {
  152. break;
  153. }
  154. }
  155. }
  156. return sb.toString();
  157. }
  158. }