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.

ResourceOptimizationTestCase.java 9.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  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 static org.junit.Assert.assertEquals;
  20. import static org.junit.Assert.assertFalse;
  21. import static org.junit.Assert.assertNotNull;
  22. import static org.junit.Assert.assertTrue;
  23. import java.io.File;
  24. import java.io.IOException;
  25. import java.io.InputStream;
  26. import java.util.Arrays;
  27. import java.util.Collection;
  28. import java.util.Set;
  29. import org.apache.commons.io.IOUtils;
  30. import org.apache.fop.apps.FOUserAgent;
  31. import org.apache.fop.render.intermediate.IFContext;
  32. import org.apache.xmlgraphics.ps.DSCConstants;
  33. import org.apache.xmlgraphics.ps.PSResource;
  34. import org.apache.xmlgraphics.ps.dsc.DSCException;
  35. import org.apache.xmlgraphics.ps.dsc.DSCListener;
  36. import org.apache.xmlgraphics.ps.dsc.DSCParser;
  37. import org.apache.xmlgraphics.ps.dsc.DefaultNestedDocumentHandler;
  38. import org.apache.xmlgraphics.ps.dsc.events.AbstractResourcesDSCComment;
  39. import org.apache.xmlgraphics.ps.dsc.events.DSCAtend;
  40. import org.apache.xmlgraphics.ps.dsc.events.DSCCommentBeginDocument;
  41. import org.apache.xmlgraphics.ps.dsc.events.DSCCommentDocumentNeededResources;
  42. import org.apache.xmlgraphics.ps.dsc.events.DSCCommentDocumentSuppliedResources;
  43. import org.apache.xmlgraphics.ps.dsc.events.DSCCommentEndOfFile;
  44. import org.apache.xmlgraphics.ps.dsc.events.DSCCommentIncludeResource;
  45. import org.apache.xmlgraphics.ps.dsc.events.DSCCommentPage;
  46. import org.apache.xmlgraphics.ps.dsc.events.DSCCommentPages;
  47. import org.junit.Test;
  48. /**
  49. * Tests the PostScript resource optimization (selective de-duplication of
  50. * images that are used multiple times).
  51. */
  52. public class ResourceOptimizationTestCase extends AbstractPostScriptTestCase {
  53. /**
  54. * Tests resource optimization.
  55. * @throws Exception if an error occurs
  56. */
  57. @Test
  58. public void testResourceOptimization() throws Exception {
  59. FOUserAgent ua = fopFactory.newFOUserAgent();
  60. PSDocumentHandler handler = new PSDocumentHandler();
  61. handler.setContext(new IFContext(ua));
  62. // This is the important part: we're enabling resource optimization
  63. handler.getPSUtil().setOptimizeResources(true);
  64. ua.setDocumentHandlerOverride(handler);
  65. // Prepare output file
  66. File outputFile = renderFile(ua, "ps-resources.fo",
  67. "-if-l" + handler.getPSUtil().getLanguageLevel());
  68. verifyPostScriptFile(outputFile);
  69. }
  70. private void verifyPostScriptFile(File psFile) throws IOException, DSCException {
  71. InputStream in = new java.io.FileInputStream(psFile);
  72. in = new java.io.BufferedInputStream(in);
  73. try {
  74. DSCParser parser = new DSCParser(in);
  75. //The first form is for arrow_down_small.png (to be reused)
  76. PSResource form1 = new PSResource(PSResource.TYPE_FORM, "FOPForm:1");
  77. PSResource helvetica = new PSResource(PSResource.TYPE_FONT, "Helvetica");
  78. PSResource helveticaBold = new PSResource(PSResource.TYPE_FONT, "Helvetica-Bold");
  79. PSResource res;
  80. DSCCommentPages pages = (DSCCommentPages)gotoDSCComment(parser, DSCConstants.PAGES);
  81. assertEquals(2, pages.getPageCount());
  82. DSCCommentDocumentSuppliedResources supplied
  83. = (DSCCommentDocumentSuppliedResources)gotoDSCComment(parser,
  84. DSCConstants.DOCUMENT_SUPPLIED_RESOURCES);
  85. Set resources = supplied.getResources();
  86. assertEquals(5, resources.size());
  87. assertTrue(resources.contains(form1));
  88. assertTrue("Expected barcode.eps as supplied resource",
  89. resources.contains(new PSResource(PSResource.TYPE_FILE,
  90. "test/resources/images/barcode.eps")));
  91. DSCCommentDocumentNeededResources needed
  92. = (DSCCommentDocumentNeededResources)gotoDSCComment(parser,
  93. DSCConstants.DOCUMENT_NEEDED_RESOURCES);
  94. resources = needed.getResources();
  95. assertEquals(2, resources.size());
  96. assertTrue("Expected Helvetica as needed resource",
  97. resources.contains(new PSResource(PSResource.TYPE_FONT, "Helvetica")));
  98. assertTrue("Expected Helvetica-Bold as needed resource",
  99. resources.contains(new PSResource(PSResource.TYPE_FONT, "Helvetica-Bold")));
  100. //Some document structure checking
  101. assertNotNull(gotoDSCComment(parser, DSCConstants.BEGIN_DEFAULTS));
  102. assertNotNull(gotoDSCComment(parser, DSCConstants.END_DEFAULTS));
  103. assertNotNull(gotoDSCComment(parser, DSCConstants.BEGIN_PROLOG));
  104. assertNotNull(gotoDSCComment(parser, DSCConstants.END_PROLOG));
  105. assertNotNull(gotoDSCComment(parser, DSCConstants.BEGIN_SETUP));
  106. //Check includes for the two referenced base 14 fonts
  107. DSCCommentIncludeResource include;
  108. Collection strings = new java.util.HashSet(
  109. Arrays.asList(new String[] {"Helvetica", "Helvetica-Bold"}));
  110. for (int i = 0; i < 2; i++) {
  111. include = (DSCCommentIncludeResource)gotoDSCComment(
  112. parser, DSCConstants.INCLUDE_RESOURCE);
  113. res = include.getResource();
  114. assertEquals(PSResource.TYPE_FONT, res.getType());
  115. strings.remove(res.getName());
  116. }
  117. assertEquals(0, strings.size());
  118. checkResourceComment(parser, DSCConstants.BEGIN_RESOURCE,
  119. new PSResource(PSResource.TYPE_ENCODING, "WinAnsiEncoding"));
  120. //Here, we encounter form 1 again
  121. checkResourceComment(parser, DSCConstants.BEGIN_RESOURCE, form1);
  122. assertNotNull(gotoDSCComment(parser, DSCConstants.END_SETUP));
  123. //Now the actual pages begin
  124. //---=== Page 1 ===---
  125. DSCCommentPage page = (DSCCommentPage)gotoDSCComment(parser, DSCConstants.PAGE);
  126. assertEquals(1, page.getPagePosition());
  127. assertEquals(DSCAtend.class,
  128. gotoDSCComment(parser, DSCConstants.PAGE_RESOURCES).getClass());
  129. assertNotNull(gotoDSCComment(parser, DSCConstants.BEGIN_PAGE_SETUP));
  130. assertNotNull(gotoDSCComment(parser, DSCConstants.END_PAGE_SETUP));
  131. PSResource form2 = new PSResource(PSResource.TYPE_FORM, "FOPForm:2");
  132. checkResourceComment(parser, DSCConstants.BEGIN_RESOURCE, form2);
  133. assertNotNull(gotoDSCComment(parser, DSCConstants.PAGE_TRAILER));
  134. AbstractResourcesDSCComment pageResources;
  135. pageResources = (AbstractResourcesDSCComment)gotoDSCComment(
  136. parser, DSCConstants.PAGE_RESOURCES);
  137. resources = pageResources.getResources();
  138. assertEquals(5, resources.size());
  139. assertTrue(resources.contains(form1));
  140. assertTrue(resources.contains(form2));
  141. assertTrue(resources.contains(helvetica));
  142. assertTrue(resources.contains(helveticaBold));
  143. //---=== Page 2 ===---
  144. page = (DSCCommentPage)gotoDSCComment(parser, DSCConstants.PAGE);
  145. assertEquals(2, page.getPagePosition());
  146. assertEquals(DSCAtend.class,
  147. gotoDSCComment(parser, DSCConstants.PAGE_RESOURCES).getClass());
  148. assertNotNull(gotoDSCComment(parser, DSCConstants.BEGIN_PAGE_SETUP));
  149. assertNotNull(gotoDSCComment(parser, DSCConstants.END_PAGE_SETUP));
  150. DSCCommentBeginDocument beginDocument;
  151. beginDocument = (DSCCommentBeginDocument)gotoDSCComment(
  152. parser, DSCConstants.BEGIN_DOCUMENT);
  153. assertEquals("test/resources/images/barcode.eps",
  154. beginDocument.getResource().getName());
  155. DSCListener listener = new DefaultNestedDocumentHandler(null);
  156. listener.processEvent(beginDocument, parser);
  157. //And again (the barcode is generated twice)
  158. beginDocument = (DSCCommentBeginDocument)gotoDSCComment(
  159. parser, DSCConstants.BEGIN_DOCUMENT);
  160. assertEquals("test/resources/images/barcode.eps",
  161. beginDocument.getResource().getName());
  162. listener.processEvent(beginDocument, parser);
  163. assertNotNull(gotoDSCComment(parser, DSCConstants.PAGE_TRAILER));
  164. pageResources = (AbstractResourcesDSCComment)gotoDSCComment(
  165. parser, DSCConstants.PAGE_RESOURCES);
  166. resources = pageResources.getResources();
  167. assertEquals(6, resources.size());
  168. assertTrue(resources.contains(form1));
  169. assertFalse(resources.contains(form2));
  170. assertTrue(resources.contains(helvetica));
  171. assertTrue(resources.contains(helveticaBold));
  172. assertTrue(resources.contains(beginDocument.getResource()));
  173. assertNotNull(gotoDSCComment(parser, DSCConstants.TRAILER));
  174. //No headers in between, as they should have been put at the beginning of the file
  175. assertEquals(DSCCommentEndOfFile.class, parser.nextEvent().asDSCComment().getClass());
  176. } finally {
  177. IOUtils.closeQuietly(in);
  178. }
  179. }
  180. }