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.

FlowLayoutManager.java 8.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. /*
  2. * $Id: FlowLayoutManager.java,v 1.21 2003/03/07 07:58:51 jeremias Exp $
  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.layoutmgr;
  52. import org.apache.fop.fo.flow.Marker;
  53. import org.apache.fop.area.Area;
  54. import org.apache.fop.area.BlockParent;
  55. import java.util.ArrayList;
  56. import java.util.List;
  57. import org.apache.fop.traits.MinOptMax;
  58. /**
  59. * LayoutManager for an fo:flow object.
  60. * Its parent LM is the PageLayoutManager.
  61. * This LM is responsible for getting columns of the appropriate size
  62. * and filling them with block-level areas generated by its children.
  63. */
  64. public class FlowLayoutManager extends BlockStackingLayoutManager {
  65. /** List of break possibilities */
  66. protected List blockBreaks = new ArrayList();
  67. /** Array of areas currently being filled stored by area class */
  68. private BlockParent[] currentAreas = new BlockParent[Area.CLASS_MAX];
  69. private int iStartPos = 0;
  70. /**
  71. * This is the top level layout manager.
  72. * It is created by the PageSequence FO.
  73. */
  74. public FlowLayoutManager() {
  75. }
  76. /**
  77. * @see org.apache.fop.layoutmgr.LayoutProcessor#getNextBreakPoss(LayoutContext)
  78. */
  79. public BreakPoss getNextBreakPoss(LayoutContext context) {
  80. // currently active LM
  81. LayoutProcessor curLM;
  82. MinOptMax stackSize = new MinOptMax();
  83. while ((curLM = getChildLM()) != null) {
  84. if (curLM.generatesInlineAreas()) {
  85. getLogger().error("inline area not allowed under flow - ignoring");
  86. curLM.setFinished(true);
  87. continue;
  88. }
  89. // Make break positions and return page break
  90. // Set up a LayoutContext
  91. MinOptMax bpd = context.getStackLimit();
  92. BreakPoss bp;
  93. LayoutContext childLC = new LayoutContext(0);
  94. boolean breakPage = false;
  95. childLC.setStackLimit(MinOptMax.subtract(bpd, stackSize));
  96. childLC.setRefIPD(context.getRefIPD());
  97. if (!curLM.isFinished()) {
  98. if ((bp = curLM.getNextBreakPoss(childLC)) != null) {
  99. stackSize.add(bp.getStackingSize());
  100. blockBreaks.add(bp);
  101. // set stackLimit for remaining space
  102. childLC.setStackLimit(MinOptMax.subtract(bpd, stackSize));
  103. if (bp.isForcedBreak() || bp.nextBreakOverflows()) {
  104. breakPage = true;
  105. }
  106. }
  107. }
  108. // check the stack bpd and if greater than available
  109. // height then go to the last best break and return
  110. // break position
  111. if (stackSize.opt > context.getStackLimit().opt) {
  112. breakPage = true;
  113. }
  114. if (breakPage) {
  115. return new BreakPoss(
  116. new LeafPosition(this, blockBreaks.size() - 1));
  117. }
  118. }
  119. setFinished(true);
  120. if (blockBreaks.size() > 0) {
  121. return new BreakPoss(
  122. new LeafPosition(this, blockBreaks.size() - 1));
  123. }
  124. return null;
  125. }
  126. /**
  127. * @see org.apache.fop.layoutmgr.LayoutProcessor#addAreas(PositionIterator, LayoutContext)
  128. */
  129. public void addAreas(PositionIterator parentIter, LayoutContext layoutContext) {
  130. LayoutProcessor childLM;
  131. LayoutContext lc = new LayoutContext(0);
  132. while (parentIter.hasNext()) {
  133. LeafPosition lfp = (LeafPosition) parentIter.next();
  134. // Add the block areas to Area
  135. PositionIterator breakPosIter =
  136. new BreakPossPosIter(blockBreaks, iStartPos,
  137. lfp.getLeafPos() + 1);
  138. iStartPos = lfp.getLeafPos() + 1;
  139. while ((childLM = breakPosIter.getNextChildLM()) != null) {
  140. childLM.addAreas(breakPosIter, lc);
  141. }
  142. }
  143. flush();
  144. }
  145. /**
  146. * Add child area to a the correct container, depending on its
  147. * area class. A Flow can fill at most one area container of any class
  148. * at any one time. The actual work is done by BlockStackingLM.
  149. * @param childArea child area to add
  150. */
  151. public void addChild(Area childArea) {
  152. addChildToArea(childArea,
  153. this.currentAreas[childArea.getAreaClass()]);
  154. }
  155. /**
  156. * @see org.apache.fop.layoutmgr.LayoutProcessor#getParentArea(Area)
  157. */
  158. public Area getParentArea(Area childArea) {
  159. // Get an area from the Page
  160. BlockParent parentArea =
  161. (BlockParent) parentLM.getParentArea(childArea);
  162. this.currentAreas[parentArea.getAreaClass()] = parentArea;
  163. setCurrentArea(parentArea);
  164. return parentArea;
  165. }
  166. /**
  167. * @see org.apache.fop.layoutmgr.LayoutProcessor#resetPosition(Position)
  168. */
  169. public void resetPosition(Position resetPos) {
  170. if (resetPos == null) {
  171. reset(null);
  172. }
  173. }
  174. /**
  175. * Retrieve marker is not allowed in the flow so this reports an
  176. * error and returns null.
  177. *
  178. * @see org.apache.fop.layoutmgr.LayoutManager
  179. */
  180. public Marker retrieveMarker(String name, int pos, int boundary) {
  181. // error cannot retrieve markers in flow
  182. getLogger().error("Cannot retrieve a marker from the flow");
  183. return null;
  184. }
  185. }