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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. /*
  2. * $Id$
  3. * Copyright (C) 2002 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.layoutmgr;
  8. import org.apache.fop.apps.FOPException;
  9. import org.apache.fop.fo.FObj;
  10. import org.apache.fop.fo.properties.Constants;
  11. import org.apache.fop.area.*;
  12. import java.util.ArrayList;
  13. import java.util.List;
  14. /**
  15. * LayoutManager for an fo:flow object.
  16. * Its parent LM is the PageLayoutManager.
  17. * This LM is responsible for getting columns of the appropriate size
  18. * and filling them with block-level areas generated by its children.
  19. */
  20. public class FlowLayoutManager extends BlockStackingLayoutManager {
  21. ArrayList blockBreaks = new ArrayList();
  22. /** Array of areas currently being filled stored by area class */
  23. private BlockParent[] currentAreas = new BlockParent[Area.CLASS_MAX];
  24. /**
  25. * This is the top level layout manager.
  26. * It is created by the PageSequence FO.
  27. */
  28. public FlowLayoutManager(FObj fobj) {
  29. super(fobj);
  30. }
  31. public BreakPoss getNextBreakPoss(LayoutContext context) {
  32. LayoutManager curLM ; // currently active LM
  33. MinOptMax stackSize = new MinOptMax();
  34. while ((curLM = getChildLM()) != null) {
  35. if (curLM.generatesInlineAreas()) {
  36. // problem
  37. curLM.setFinished(true);
  38. continue;
  39. }
  40. // Make break positions and return page break
  41. // Set up a LayoutContext
  42. MinOptMax bpd = context.getStackLimit();
  43. BreakPoss bp;
  44. LayoutContext childLC = new LayoutContext(0);
  45. boolean breakPage = false;
  46. childLC.setStackLimit(MinOptMax.subtract(bpd, stackSize));
  47. childLC.setRefIPD(context.getRefIPD());
  48. if (!curLM.isFinished()) {
  49. if ((bp = curLM.getNextBreakPoss(childLC)) != null) {
  50. stackSize.add(bp.getStackingSize());
  51. blockBreaks.add(bp);
  52. // set stackLimit for remaining space
  53. childLC.setStackLimit(MinOptMax.subtract(bpd, stackSize));
  54. if (bp.isForcedBreak()) {
  55. breakPage = true;
  56. break;
  57. }
  58. }
  59. }
  60. // check the stack bpd and if greater than available
  61. // height then go to the last best break and return
  62. // break position
  63. if (stackSize.opt > context.getStackLimit().opt) {
  64. breakPage = true;
  65. }
  66. if (breakPage) {
  67. return new BreakPoss(
  68. new LeafPosition(this, blockBreaks.size() - 1));
  69. }
  70. }
  71. setFinished(true);
  72. if (blockBreaks.size() > 0) {
  73. return new BreakPoss(
  74. new LeafPosition(this, blockBreaks.size() - 1));
  75. }
  76. return null;
  77. }
  78. public void addAreas(PositionIterator parentIter, LayoutContext layoutContext) {
  79. LayoutManager childLM ;
  80. int iStartPos = 0;
  81. LayoutContext lc = new LayoutContext(0);
  82. while (parentIter.hasNext()) {
  83. LeafPosition lfp = (LeafPosition) parentIter.next();
  84. // Add the block areas to Area
  85. PositionIterator breakPosIter =
  86. new BreakPossPosIter(blockBreaks, iStartPos,
  87. lfp.getLeafPos() + 1);
  88. iStartPos = lfp.getLeafPos() + 1;
  89. while ((childLM = breakPosIter.getNextChildLM()) != null) {
  90. childLM.addAreas(breakPosIter, lc);
  91. }
  92. }
  93. flush();
  94. // clear the breaks for the page to start for the next page
  95. blockBreaks.clear();
  96. }
  97. /**
  98. * Add child area to a the correct container, depending on its
  99. * area class. A Flow can fill at most one area container of any class
  100. * at any one time. The actual work is done by BlockStackingLM.
  101. */
  102. public boolean addChild(Area childArea) {
  103. return addChildToArea(childArea,
  104. this.currentAreas[childArea.getAreaClass()]);
  105. }
  106. public Area getParentArea(Area childArea) {
  107. // Get an area from the Page
  108. BlockParent parentArea =
  109. (BlockParent) parentLM.getParentArea(childArea);
  110. this.currentAreas[parentArea.getAreaClass()] = parentArea;
  111. setCurrentArea(parentArea);
  112. return parentArea;
  113. }
  114. public void resetPosition(Position resetPos) {
  115. if (resetPos == null) {
  116. reset(null);
  117. }
  118. }
  119. /**
  120. * Retrieve marker is not allowed in the flow so this reports an
  121. * error and returns null.
  122. *
  123. * @see org.apache.fop.layoutmgr.LayoutManager
  124. */
  125. public LayoutManager retrieveMarker(String name, int pos, int boundary) {
  126. // error cannot retrieve markers in flow
  127. return null;
  128. }
  129. }