Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

ResourceOptimizationTestCase.java 10KB

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