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.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. /*
  2. * Copyright 1999-2004 The Apache Software Foundation.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. /* $Id$ */
  17. package org.apache.fop.layoutmgr;
  18. import org.apache.fop.area.Area;
  19. import org.apache.fop.area.BlockParent;
  20. import org.apache.fop.area.Block;
  21. import org.apache.fop.traits.MinOptMax;
  22. /**
  23. * Base LayoutManager class for all areas which stack their child
  24. * areas in the block-progression direction, such as Flow, Block, ListBlock.
  25. */
  26. public abstract class BlockStackingLayoutManager extends AbstractLayoutManager {
  27. /**
  28. * Reference to FO whose areas it's managing or to the traits
  29. * of the FO.
  30. */
  31. protected LayoutProcessor curChildLM = null;
  32. protected BlockParent parentArea = null;
  33. public BlockStackingLayoutManager() {
  34. }
  35. private BreakCost evaluateBreakCost(Area parent, Area child) {
  36. return new BreakCost(child, 0);
  37. }
  38. /** return current area being filled
  39. */
  40. protected BlockParent getCurrentArea() {
  41. return this.parentArea;
  42. }
  43. /**
  44. * Set the current area being filled.
  45. */
  46. protected void setCurrentArea(BlockParent parentArea) {
  47. this.parentArea = parentArea;
  48. }
  49. protected MinOptMax resolveSpaceSpecifier(Area nextArea) {
  50. SpaceSpecifier spaceSpec = new SpaceSpecifier(false);
  51. // Area prevArea = getCurrentArea().getLast();
  52. // if (prevArea != null) {
  53. // spaceSpec.addSpace(prevArea.getSpaceAfter());
  54. // }
  55. // spaceSpec.addSpace(nextArea.getSpaceBefore());
  56. return spaceSpec.resolve(false);
  57. }
  58. /**
  59. * Add a block spacer for space before and space after a block.
  60. * This adds an empty Block area that acts as a block space.
  61. *
  62. * @param adjust the adjustment value
  63. * @param minoptmax the min/opt/max value of the spacing
  64. */
  65. public void addBlockSpacing(double adjust, MinOptMax minoptmax) {
  66. if (minoptmax == null) {
  67. return;
  68. }
  69. int sp = minoptmax.opt;
  70. if (adjust > 0) {
  71. sp = sp + (int)(adjust * (minoptmax.max - minoptmax.opt));
  72. } else {
  73. sp = sp + (int)(adjust * (minoptmax.opt - minoptmax.min));
  74. }
  75. if (sp != 0) {
  76. Block spacer = new Block();
  77. spacer.setHeight(sp);
  78. parentLM.addChild(spacer);
  79. }
  80. }
  81. /**
  82. * Add the childArea to the passed area.
  83. * Called by child LayoutManager when it has filled one of its areas.
  84. * The LM should already have an Area in which to put the child.
  85. * See if the area will fit in the current area.
  86. * If so, add it. Otherwise initiate breaking.
  87. * @param childArea the area to add: will be some block-stacked Area.
  88. * @param parentArea the area in which to add the childArea
  89. */
  90. protected void addChildToArea(Area childArea,
  91. BlockParent parentArea) {
  92. // This should be a block-level Area (Block in the generic sense)
  93. if (!(childArea instanceof Block)) {
  94. //log.error("Child not a Block in BlockStackingLM!");
  95. }
  96. MinOptMax spaceBefore = resolveSpaceSpecifier(childArea);
  97. parentArea.addBlock((Block) childArea);
  98. flush(); // hand off current area to parent
  99. }
  100. /**
  101. * Add the childArea to the current area.
  102. * Called by child LayoutManager when it has filled one of its areas.
  103. * The LM should already have an Area in which to put the child.
  104. * See if the area will fit in the current area.
  105. * If so, add it. Otherwise initiate breaking.
  106. * @param childArea the area to add: will be some block-stacked Area.
  107. */
  108. public void addChild(Area childArea) {
  109. addChildToArea(childArea, getCurrentArea());
  110. }
  111. /**
  112. * Force current area to be added to parent area.
  113. */
  114. protected void flush() {
  115. if (getCurrentArea() != null) {
  116. parentLM.addChild(getCurrentArea());
  117. }
  118. }
  119. }