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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. package org.apache.fop.apps;
  2. import java.io.OutputStream;
  3. import java.io.IOException;
  4. import java.util.HashSet;
  5. import org.xml.sax.SAXException;
  6. import org.apache.fop.layout.FontInfo;
  7. import org.apache.fop.area.PageViewport;
  8. import org.apache.fop.area.AreaTree;
  9. import org.apache.fop.area.Title;
  10. import org.apache.fop.render.Renderer;
  11. import org.apache.fop.fo.pagination.PageSequence;
  12. import org.apache.fop.fo.pagination.LayoutMasterSet;
  13. import org.apache.avalon.framework.logger.Logger;
  14. /**
  15. * Layout handler that receives the structure events.
  16. * This initiates layout processes and corresponding
  17. * rendering processes such as start/end.
  18. */
  19. public class LayoutHandler extends StructureHandler {
  20. private static final boolean MEM_PROFILE_WITH_GC = false;
  21. /**
  22. Somewhere to get our stats from.
  23. */
  24. private Runtime runtime = Runtime.getRuntime();
  25. /**
  26. Keep track of the number of pages rendered.
  27. */
  28. int pageCount = 0;
  29. /**
  30. Keep track of heap memory allocated,
  31. for statistical purposes.
  32. */
  33. private long initialMemory;
  34. /**
  35. Keep track of time used by renderer.
  36. */
  37. private long startTime;
  38. /**
  39. The stream to which this rendering is to be
  40. written to. <B>Note</B> that some renderers
  41. do not render to a stream, and that this
  42. member can therefore be null.
  43. */
  44. private OutputStream outputStream;
  45. /**
  46. The renderer being used.
  47. */
  48. private Renderer renderer;
  49. /**
  50. The FontInfo for this renderer.
  51. */
  52. private FontInfo fontInfo = new FontInfo();
  53. /**
  54. * The current AreaTree for the PageSequence being rendered.
  55. */
  56. private AreaTree areaTree;
  57. private AreaTree.StorePagesModel atModel;
  58. /**
  59. * @param outputStream the stream that the result is rendered to
  60. * @param renderer the renderer to call
  61. * @param store if true then use the store pages model and keep the
  62. * area tree in memory
  63. */
  64. public LayoutHandler(OutputStream outputStream, Renderer renderer,
  65. boolean store) {
  66. this.outputStream = outputStream;
  67. this.renderer = renderer;
  68. this.areaTree = new AreaTree();
  69. this.atModel = AreaTree.createStorePagesModel();
  70. areaTree.setTreeModel(atModel);
  71. }
  72. public AreaTree getAreaTree() {
  73. return areaTree;
  74. }
  75. public void startDocument() throws SAXException {
  76. pageCount = 0;
  77. if (MEM_PROFILE_WITH_GC)
  78. System.gc(); // This takes time but gives better results
  79. initialMemory = runtime.totalMemory() - runtime.freeMemory();
  80. startTime = System.currentTimeMillis();
  81. try {
  82. renderer.setupFontInfo(fontInfo);
  83. // check that the "any,normal,400" font exists
  84. if(!fontInfo.isSetupValid()) {
  85. throw new SAXException(new FOPException("no default font defined by OutputConverter"));
  86. }
  87. renderer.startRenderer(outputStream);
  88. } catch (IOException e) {
  89. throw new SAXException(e);
  90. }
  91. }
  92. public void endDocument() throws SAXException {
  93. /*
  94. Force the processing of any more queue elements,
  95. even if they are not resolved.
  96. */
  97. try {
  98. //processQueue(true);
  99. processAreaTree();
  100. renderer.stopRenderer();
  101. } catch (FOPException e) {
  102. throw new SAXException(e);
  103. }
  104. catch (IOException e) {
  105. throw new SAXException(e);
  106. }
  107. if (MEM_PROFILE_WITH_GC)
  108. System.gc(); // This takes time but gives better results
  109. long memoryNow = runtime.totalMemory() - runtime.freeMemory();
  110. long memoryUsed = (memoryNow - initialMemory) / 1024L;
  111. if (getLogger().isDebugEnabled()) {
  112. getLogger().debug("Initial heap size: " + (initialMemory / 1024L) + "Kb");
  113. getLogger().debug("Current heap size: " + (memoryNow / 1024L) + "Kb");
  114. getLogger().debug("Total memory used: " + memoryUsed + "Kb");
  115. if (!MEM_PROFILE_WITH_GC) {
  116. getLogger().debug(" Memory use is indicative; no GC was performed");
  117. getLogger().debug(" These figures should not be used comparatively");
  118. }
  119. }
  120. long timeUsed = System.currentTimeMillis() - startTime;
  121. if (getLogger().isDebugEnabled()) {
  122. getLogger().debug("Total time used: " + timeUsed + "ms");
  123. getLogger().debug("Pages rendered: " + pageCount);
  124. if (pageCount > 0) {
  125. getLogger().debug("Avg render time: " + (timeUsed / pageCount) + "ms/page");
  126. }
  127. }
  128. }
  129. public void startPageSequence(PageSequence pageSeq, org.apache.fop.fo.Title seqTitle, LayoutMasterSet lms) {
  130. Title title = null;
  131. if(seqTitle != null) {
  132. title = seqTitle.getTitleArea();
  133. }
  134. areaTree.startPageSequence(title);
  135. }
  136. /**
  137. Format the PageSequence. The PageSequence
  138. formats Pages and adds them to the AreaTree,
  139. which subsequently calls the StreamRenderer
  140. instance (this) again to render the page.
  141. At this time the page might be printed
  142. or it might be queued. A page might not
  143. be renderable immediately if the IDReferences
  144. are not all valid. In this case we defer
  145. the rendering until they are all valid.
  146. */
  147. public void endPageSequence(PageSequence pageSequence)
  148. throws FOPException {
  149. //areaTree.setFontInfo(fontInfo);
  150. pageSequence.format(areaTree);
  151. }
  152. private void processAreaTree() throws FOPException {
  153. int count = 0;
  154. int seqc = atModel.getPageSequenceCount();
  155. while (count < seqc) {
  156. Title title = atModel.getTitle(count);
  157. renderer.startPageSequence(title);
  158. int pagec = atModel.getPageCount(count);
  159. for (int c = 0; c < pagec; c++) {
  160. try {
  161. renderer.renderPage(atModel.getPage(count, c));
  162. } catch (java.io.IOException ioex) {
  163. throw new FOPException("I/O Error rendering page",
  164. ioex);
  165. }
  166. }
  167. count++;
  168. }
  169. }
  170. public FontInfo getFontInfo() {
  171. return this.fontInfo;
  172. }
  173. }