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.

AbstractRenderer.java 7.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  1. /*
  2. * $Id$
  3. * Copyright (C) 2001 The Apache Software Foundation. All rights reserved.
  4. * For details on use and redistribution please refer to the
  5. * LICENSE file included with these sources.
  6. */
  7. package org.apache.fop.render;
  8. // FOP
  9. import org.apache.fop.image.ImageArea;
  10. import org.apache.fop.apps.FOPException;
  11. import org.apache.fop.area.*;
  12. import org.apache.fop.area.Span;
  13. import org.apache.fop.area.inline.*;
  14. import org.apache.fop.area.inline.Space;
  15. import org.apache.fop.fo.FOUserAgent;
  16. import org.apache.log.Logger;
  17. // Java
  18. import java.io.IOException;
  19. import java.io.OutputStream;
  20. import java.util.HashMap;
  21. import java.util.List;
  22. /**
  23. * Abstract base class for all renderers.
  24. * The Abstract renderer does all the top level processing
  25. * of the area tree and adds some abstract methods to handle
  26. * viewports. This keeps track of the current block and inline
  27. * position.
  28. */
  29. public abstract class AbstractRenderer implements Renderer {
  30. protected Logger log;
  31. protected FOUserAgent userAgent;
  32. protected HashMap options;
  33. // block progression position
  34. protected int currentBPPosition = 0;
  35. // inline progression position
  36. protected int currentIPPosition = 0;
  37. protected int currentBlockIPPosition = 0;
  38. public void setLogger(Logger logger) {
  39. log = logger;
  40. }
  41. public void setUserAgent(FOUserAgent agent) {
  42. userAgent = agent;
  43. }
  44. public void setOptions(HashMap opt) {
  45. options = opt;
  46. }
  47. public void startPageSequence(Title seqTitle) {
  48. }
  49. // normally this would be overriden to create a page in the
  50. // output
  51. public void renderPage(PageViewport page) throws IOException,
  52. FOPException {
  53. Page p = page.getPage();
  54. renderPageAreas(p);
  55. }
  56. protected void renderPageAreas(Page page) {
  57. RegionViewport viewport;
  58. viewport = page.getRegion(Region.BEFORE);
  59. renderRegionViewport(viewport);
  60. viewport = page.getRegion(Region.START);
  61. renderRegionViewport(viewport);
  62. viewport = page.getRegion(Region.BODY);
  63. renderRegionViewport(viewport);
  64. viewport = page.getRegion(Region.END);
  65. renderRegionViewport(viewport);
  66. viewport = page.getRegion(Region.AFTER);
  67. renderRegionViewport(viewport);
  68. }
  69. // the region may clip the area and it establishes
  70. // a position from where the region is placed
  71. protected void renderRegionViewport(RegionViewport port) {
  72. if (port != null) {
  73. Region region = port.getRegion();
  74. if (region.getRegionClass() == Region.BODY) {
  75. renderBodyRegion((BodyRegion) region);
  76. } else {
  77. renderRegion(region);
  78. }
  79. }
  80. }
  81. protected void renderRegion(Region region) {
  82. List blocks = region.getBlocks();
  83. renderBlocks(blocks);
  84. }
  85. protected void renderBodyRegion(BodyRegion region) {
  86. BeforeFloat bf = region.getBeforeFloat();
  87. if (bf != null) {
  88. renderBeforeFloat(bf);
  89. }
  90. MainReference mr = region.getMainReference();
  91. if (mr != null) {
  92. renderMainReference(mr);
  93. }
  94. Footnote foot = region.getFootnote();
  95. if (foot != null) {
  96. renderFootnote(foot);
  97. }
  98. }
  99. protected void renderBeforeFloat(BeforeFloat bf) {
  100. List blocks = bf.getBlocks();
  101. if (blocks != null) {
  102. renderBlocks(blocks);
  103. Block sep = bf.getSeparator();
  104. if (sep != null) {
  105. renderBlock(sep);
  106. }
  107. }
  108. }
  109. protected void renderFootnote(Footnote footnote) {
  110. List blocks = footnote.getBlocks();
  111. if (blocks != null) {
  112. Block sep = footnote.getSeparator();
  113. if (sep != null) {
  114. renderBlock(sep);
  115. }
  116. renderBlocks(blocks);
  117. }
  118. }
  119. // the main reference area contains a list of spans that are
  120. // stacked on the page
  121. // the spans contain a list of normal flow reference areas
  122. // that are positioned into columns.
  123. protected void renderMainReference(MainReference mr) {
  124. int saveIPPos = currentIPPosition;
  125. Span span = null;
  126. List spans = mr.getSpans();
  127. for (int count = 0; count < spans.size(); count++) {
  128. span = (Span) spans.get(count);
  129. int offset = (mr.getWidth() -
  130. (span.getColumnCount() - 1) * mr.getColumnGap()) /
  131. span.getColumnCount() + mr.getColumnGap();
  132. for (int c = 0; c < span.getColumnCount(); c++) {
  133. Flow flow = (Flow) span.getFlow(c);
  134. renderFlow(flow);
  135. currentIPPosition += offset;
  136. }
  137. currentIPPosition = saveIPPos;
  138. currentBPPosition += span.getHeight();
  139. }
  140. }
  141. // the normal flow reference area contains stacked blocks
  142. protected void renderFlow(Flow flow) {
  143. List blocks = flow.getBlocks();
  144. renderBlocks(blocks);
  145. }
  146. protected void renderBlock(Block block) {
  147. boolean childrenblocks = block.isChildrenBlocks();
  148. List children = block.getChildAreas();
  149. if (childrenblocks) {
  150. renderBlocks(children);
  151. } else {
  152. if (children == null) {
  153. // simply move position
  154. } else {
  155. // a line area is rendered from the top left position
  156. // of the line, each inline object is offset from there
  157. for (int count = 0; count < children.size(); count++) {
  158. LineArea line = (LineArea) children.get(count);
  159. renderLineArea(line);
  160. }
  161. }
  162. }
  163. }
  164. // a line area may have grouped styling for its children
  165. // such as underline, background
  166. protected void renderLineArea(LineArea line) {
  167. List children = line.getInlineAreas();
  168. for (int count = 0; count < children.size(); count++) {
  169. InlineArea inline = (InlineArea) children.get(count);
  170. inline.render(this);
  171. }
  172. }
  173. public void renderViewport(Viewport viewport) {
  174. Area content = viewport.getContent();
  175. if (content instanceof Image) {
  176. renderImage((Image) content);
  177. } else if (content instanceof Container) {
  178. renderContainer((Container) content);
  179. } else if (content instanceof ForeignObject) {
  180. renderForeignObject((ForeignObject) content);
  181. }
  182. }
  183. public void renderImage(Image image) {
  184. }
  185. public void renderContainer(Container cont) {
  186. List blocks = cont.getBlocks();
  187. renderBlocks(blocks);
  188. }
  189. public void renderForeignObject(ForeignObject fo) {
  190. }
  191. public void renderCharacter(org.apache.fop.area.inline.Character ch) {
  192. currentBlockIPPosition += ch.getWidth();
  193. }
  194. // an inline space moves the inline progression position
  195. // for the current block by the width or height of the space
  196. // it may also have styling (only on this object) that needs
  197. // handling
  198. public void renderInlineSpace(Space space) {
  199. currentBlockIPPosition += space.getWidth();
  200. }
  201. public void renderLeader(Leader area) {
  202. currentBlockIPPosition += area.getWidth();
  203. }
  204. public void renderWord(Word word) {
  205. currentBlockIPPosition += word.getWidth();
  206. }
  207. protected void renderBlocks(List blocks) {
  208. for (int count = 0; count < blocks.size(); count++) {
  209. Block block = (Block) blocks.get(count);
  210. renderBlock(block);
  211. }
  212. }
  213. }