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.

LayoutHandler.java 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312
  1. /*
  2. * $Id$
  3. * ============================================================================
  4. * The Apache Software License, Version 1.1
  5. * ============================================================================
  6. *
  7. * Copyright (C) 1999-2003 The Apache Software Foundation. All rights reserved.
  8. *
  9. * Redistribution and use in source and binary forms, with or without modifica-
  10. * tion, are permitted provided that the following conditions are met:
  11. *
  12. * 1. Redistributions of source code must retain the above copyright notice,
  13. * this list of conditions and the following disclaimer.
  14. *
  15. * 2. Redistributions in binary form must reproduce the above copyright notice,
  16. * this list of conditions and the following disclaimer in the documentation
  17. * and/or other materials provided with the distribution.
  18. *
  19. * 3. The end-user documentation included with the redistribution, if any, must
  20. * include the following acknowledgment: "This product includes software
  21. * developed by the Apache Software Foundation (http://www.apache.org/)."
  22. * Alternately, this acknowledgment may appear in the software itself, if
  23. * and wherever such third-party acknowledgments normally appear.
  24. *
  25. * 4. The names "FOP" and "Apache Software Foundation" must not be used to
  26. * endorse or promote products derived from this software without prior
  27. * written permission. For written permission, please contact
  28. * apache@apache.org.
  29. *
  30. * 5. Products derived from this software may not be called "Apache", nor may
  31. * "Apache" appear in their name, without prior written permission of the
  32. * Apache Software Foundation.
  33. *
  34. * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES,
  35. * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
  36. * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  37. * APACHE SOFTWARE FOUNDATION OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
  38. * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLU-
  39. * DING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
  40. * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
  41. * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  42. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  43. * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  44. * ============================================================================
  45. *
  46. * This software consists of voluntary contributions made by many individuals
  47. * on behalf of the Apache Software Foundation and was originally created by
  48. * James Tauber <jtauber@jtauber.com>. For more information on the Apache
  49. * Software Foundation, please see <http://www.apache.org/>.
  50. */
  51. package org.apache.fop.apps;
  52. // Java
  53. import java.io.IOException;
  54. import java.io.OutputStream;
  55. import java.util.List;
  56. // SAX
  57. import org.xml.sax.SAXException;
  58. // FOP
  59. import org.apache.fop.area.AreaTree;
  60. import org.apache.fop.area.AreaTreeModel;
  61. import org.apache.fop.area.StorePagesModel;
  62. import org.apache.fop.area.Title;
  63. import org.apache.fop.area.TreeExt;
  64. import org.apache.fop.fo.pagination.LayoutMasterSet;
  65. import org.apache.fop.fo.pagination.PageSequence;
  66. import org.apache.fop.layout.FontInfo;
  67. import org.apache.fop.render.Renderer;
  68. /**
  69. * Layout handler that receives the structure events.
  70. * This initiates layout processes and corresponding
  71. * rendering processes such as start/end.
  72. */
  73. public class LayoutHandler extends StructureHandler {
  74. // TODO: Collecting of statistics should be configurable
  75. private final boolean collectStatistics = true;
  76. private static final boolean MEM_PROFILE_WITH_GC = false;
  77. /**
  78. * Somewhere to get our stats from.
  79. */
  80. private Runtime runtime;
  81. /**
  82. * Keep track of the number of pages rendered.
  83. */
  84. private int pageCount;
  85. /**
  86. * Keep track of heap memory allocated,
  87. * for statistical purposes.
  88. */
  89. private long initialMemory;
  90. /**
  91. * Keep track of time used by renderer.
  92. */
  93. private long startTime;
  94. /**
  95. * The stream to which this rendering is to be
  96. * written to. <B>Note</B> that some renderers
  97. * do not render to a stream, and that this
  98. * member can therefore be null.
  99. */
  100. private OutputStream outputStream;
  101. /**
  102. * The renderer being used.
  103. */
  104. private Renderer renderer;
  105. /**
  106. * The FontInfo for this renderer.
  107. */
  108. private FontInfo fontInfo = new FontInfo();
  109. /**
  110. * The current AreaTree for the PageSequence being rendered.
  111. */
  112. private AreaTree areaTree;
  113. private AreaTreeModel atModel;
  114. /**
  115. * Main constructor
  116. * @param outputStream the stream that the result is rendered to
  117. * @param renderer the renderer to call
  118. * @param store if true then use the store pages model and keep the
  119. * area tree in memory
  120. */
  121. public LayoutHandler(OutputStream outputStream, Renderer renderer,
  122. boolean store) {
  123. if (collectStatistics) {
  124. runtime = Runtime.getRuntime();
  125. }
  126. this.outputStream = outputStream;
  127. this.renderer = renderer;
  128. this.areaTree = new AreaTree();
  129. this.atModel = AreaTree.createRenderPagesModel(renderer);
  130. //this.atModel = new CachedRenderPagesModel(renderer);
  131. areaTree.setTreeModel(atModel);
  132. }
  133. /**
  134. * Get the area tree for this layout handler.
  135. *
  136. * @return the area tree for this document
  137. */
  138. public AreaTree getAreaTree() {
  139. return areaTree;
  140. }
  141. /**
  142. * Start the document.
  143. * This starts the document in the renderer.
  144. *
  145. * @throws SAXException if there is an error
  146. */
  147. public void startDocument() throws SAXException {
  148. //Initialize statistics
  149. if (collectStatistics) {
  150. pageCount = 0;
  151. if (MEM_PROFILE_WITH_GC) {
  152. System.gc(); // This takes time but gives better results
  153. }
  154. initialMemory = runtime.totalMemory() - runtime.freeMemory();
  155. startTime = System.currentTimeMillis();
  156. }
  157. try {
  158. renderer.setupFontInfo(fontInfo);
  159. // check that the "any,normal,400" font exists
  160. if (!fontInfo.isSetupValid()) {
  161. throw new SAXException(new FOPException(
  162. "No default font defined by OutputConverter"));
  163. }
  164. renderer.startRenderer(outputStream);
  165. } catch (IOException e) {
  166. throw new SAXException(e);
  167. }
  168. }
  169. /**
  170. * End the document.
  171. *
  172. * @throws SAXException if there is some error
  173. */
  174. public void endDocument() throws SAXException {
  175. try {
  176. //processAreaTree(atModel);
  177. areaTree.endDocument();
  178. renderer.stopRenderer();
  179. } catch (Exception e) {
  180. throw new SAXException(e);
  181. }
  182. if (collectStatistics) {
  183. if (MEM_PROFILE_WITH_GC) {
  184. // This takes time but gives better results
  185. System.gc();
  186. }
  187. long memoryNow = runtime.totalMemory() - runtime.freeMemory();
  188. long memoryUsed = (memoryNow - initialMemory) / 1024L;
  189. long timeUsed = System.currentTimeMillis() - startTime;
  190. if (getLogger().isDebugEnabled()) {
  191. getLogger().debug("Initial heap size: " + (initialMemory / 1024L) + "Kb");
  192. getLogger().debug("Current heap size: " + (memoryNow / 1024L) + "Kb");
  193. getLogger().debug("Total memory used: " + memoryUsed + "Kb");
  194. if (!MEM_PROFILE_WITH_GC) {
  195. getLogger().debug(" Memory use is indicative; no GC was performed");
  196. getLogger().debug(" These figures should not be used comparatively");
  197. }
  198. getLogger().debug("Total time used: " + timeUsed + "ms");
  199. getLogger().debug("Pages rendered: " + pageCount);
  200. if (pageCount > 0) {
  201. getLogger().debug("Avg render time: " + (timeUsed / pageCount) + "ms/page");
  202. }
  203. }
  204. }
  205. }
  206. /**
  207. * Start a page sequence.
  208. * At the start of a page sequence it can start the page sequence
  209. * on the area tree with the page sequence title.
  210. *
  211. * @param pageSeq the page sequence starting
  212. * @param seqTitle the title of the page sequence
  213. * @param lms the layout master set
  214. */
  215. public void startPageSequence(PageSequence pageSeq,
  216. org.apache.fop.fo.Title seqTitle,
  217. LayoutMasterSet lms) {
  218. Title title = null;
  219. if (seqTitle != null) {
  220. title = seqTitle.getTitleArea();
  221. }
  222. areaTree.startPageSequence(title);
  223. }
  224. /**
  225. * End the PageSequence.
  226. * The PageSequence formats Pages and adds them to the AreaTree.
  227. * The area tree then handles what happens with the pages.
  228. *
  229. * @param pageSequence the page sequence ending
  230. * @throws FOPException if there is an error formatting the pages
  231. */
  232. public void endPageSequence(PageSequence pageSequence)
  233. throws FOPException {
  234. //areaTree.setFontInfo(fontInfo);
  235. if (collectStatistics) {
  236. if (MEM_PROFILE_WITH_GC) {
  237. // This takes time but gives better results
  238. System.gc();
  239. }
  240. long memoryNow = runtime.totalMemory() - runtime.freeMemory();
  241. if (getLogger().isDebugEnabled()) {
  242. getLogger().debug("Current heap size: " + (memoryNow / 1024L) + "Kb");
  243. }
  244. }
  245. pageSequence.format(areaTree);
  246. }
  247. /**
  248. * Process an area tree.
  249. * If a store pages model is used this can read and send all the
  250. * pages to the renderer.
  251. *
  252. * @param model the store pages model
  253. * @throws FOPException if there is an error
  254. */
  255. private void processAreaTree(StorePagesModel model) throws FOPException {
  256. int count = 0;
  257. int seqc = model.getPageSequenceCount();
  258. while (count < seqc) {
  259. Title title = model.getTitle(count);
  260. renderer.startPageSequence(title);
  261. int pagec = model.getPageCount(count);
  262. for (int c = 0; c < pagec; c++) {
  263. try {
  264. renderer.renderPage(model.getPage(count, c));
  265. } catch (IOException ioex) {
  266. throw new FOPException("I/O Error rendering page",
  267. ioex);
  268. }
  269. }
  270. count++;
  271. }
  272. List list = model.getEndExtensions();
  273. for (count = 0; count < list.size(); count++) {
  274. TreeExt ext = (TreeExt)list.get(count);
  275. renderer.renderExtension(ext);
  276. }
  277. }
  278. /**
  279. * Get the font information for the layout handler.
  280. *
  281. * @return the font information
  282. */
  283. public FontInfo getFontInfo() {
  284. return this.fontInfo;
  285. }
  286. }