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.

BlockStackingLayoutManager.java 4.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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.fo.FObj;
  9. import org.apache.fop.area.Area;
  10. import org.apache.fop.area.BlockParent;
  11. import org.apache.fop.area.Block;
  12. import org.apache.fop.area.MinOptMax;
  13. import java.util.Iterator;
  14. /**
  15. * Base LayoutManager class for all areas which stack their child
  16. * areas in the block-progression direction, such as Flow, Block, ListBlock.
  17. */
  18. public abstract class BlockStackingLayoutManager extends AbstractLayoutManager {
  19. /**
  20. * Reference to FO whose areas it's managing or to the traits
  21. * of the FO.
  22. */
  23. protected LayoutManager curChildLM = null;
  24. protected BlockParent parentArea = null;
  25. public BlockStackingLayoutManager(FObj fobj) {
  26. super(fobj);
  27. }
  28. private BreakCost evaluateBreakCost(Area parent, Area child) {
  29. return new BreakCost(child, 0);
  30. }
  31. /** return current area being filled
  32. */
  33. protected BlockParent getCurrentArea() {
  34. return this.parentArea;
  35. }
  36. /**
  37. * Set the current area being filled.
  38. */
  39. protected void setCurrentArea(BlockParent parentArea) {
  40. this.parentArea = parentArea;
  41. }
  42. protected MinOptMax resolveSpaceSpecifier(Area nextArea) {
  43. SpaceSpecifier spaceSpec = new SpaceSpecifier(false);
  44. // Area prevArea = getCurrentArea().getLast();
  45. // if (prevArea != null) {
  46. // spaceSpec.addSpace(prevArea.getSpaceAfter());
  47. // }
  48. // spaceSpec.addSpace(nextArea.getSpaceBefore());
  49. return spaceSpec.resolve(false);
  50. }
  51. /**
  52. * Add a block spacer for space before and space after a block.
  53. * This adds an empty Block area that acts as a block space.
  54. *
  55. * @param adjust the adjustment value
  56. * @param minoptmax the min/opt/max value of the spacing
  57. */
  58. public void addBlockSpacing(double adjust, MinOptMax minoptmax) {
  59. int sp = minoptmax.opt;
  60. if(adjust > 0) {
  61. sp = sp + (int)(adjust * (minoptmax.max - minoptmax.opt));
  62. } else {
  63. sp = sp + (int)(adjust * (minoptmax.opt - minoptmax.min));
  64. }
  65. if(sp != 0) {
  66. Block spacer = new Block();
  67. spacer.setHeight(sp);
  68. parentLM.addChild(spacer);
  69. }
  70. }
  71. /**
  72. * Add the childArea to the passed area.
  73. * Called by child LayoutManager when it has filled one of its areas.
  74. * The LM should already have an Area in which to put the child.
  75. * See if the area will fit in the current area.
  76. * If so, add it. Otherwise initiate breaking.
  77. * @param childArea the area to add: will be some block-stacked Area.
  78. * @param parentArea the area in which to add the childArea
  79. */
  80. protected boolean addChildToArea(Area childArea,
  81. BlockParent parentArea) {
  82. // This should be a block-level Area (Block in the generic sense)
  83. if (!(childArea instanceof Block)) {
  84. //log.error("Child not a Block in BlockStackingLM!");
  85. return false;
  86. }
  87. MinOptMax spaceBefore = resolveSpaceSpecifier(childArea);
  88. parentArea.addBlock((Block) childArea);
  89. flush(); // hand off current area to parent
  90. return true;
  91. }
  92. /**
  93. * Add the childArea to the current area.
  94. * Called by child LayoutManager when it has filled one of its areas.
  95. * The LM should already have an Area in which to put the child.
  96. * See if the area will fit in the current area.
  97. * If so, add it. Otherwise initiate breaking.
  98. * @param childArea the area to add: will be some block-stacked Area.
  99. */
  100. public boolean addChild(Area childArea) {
  101. return addChildToArea(childArea, getCurrentArea());
  102. }
  103. /**
  104. * Force current area to be added to parent area.
  105. */
  106. protected boolean flush() {
  107. if (getCurrentArea() != null) {
  108. return parentLM.addChild(getCurrentArea());
  109. }
  110. return false;
  111. }
  112. }