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.

AreaAdditionUtil.java 4.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. /*
  2. * Licensed to the Apache Software Foundation (ASF) under one or more
  3. * contributor license agreements. See the NOTICE file distributed with
  4. * this work for additional information regarding copyright ownership.
  5. * The ASF licenses this file to You under the Apache License, Version 2.0
  6. * (the "License"); you may not use this file except in compliance with
  7. * the License. You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. */
  17. /* $Id$ */
  18. package org.apache.fop.layoutmgr;
  19. import java.util.LinkedList;
  20. import org.apache.fop.layoutmgr.SpaceResolver.SpaceHandlingBreakPosition;
  21. /**
  22. * Utility class which provides common code for the addAreas stage.
  23. */
  24. public final class AreaAdditionUtil {
  25. private AreaAdditionUtil() {
  26. }
  27. /**
  28. * Creates the child areas for the given layout manager.
  29. * @param bslm the BlockStackingLayoutManager instance for which "addAreas" is performed.
  30. * @param parentIter the position iterator
  31. * @param layoutContext the layout context
  32. */
  33. public static void addAreas(BlockStackingLayoutManager bslm,
  34. PositionIterator parentIter, LayoutContext layoutContext) {
  35. LayoutManager childLM;
  36. LayoutContext lc = LayoutContext.offspringOf(layoutContext);
  37. LayoutManager firstLM = null;
  38. LayoutManager lastLM = null;
  39. Position firstPos = null;
  40. Position lastPos = null;
  41. if (bslm != null) {
  42. bslm.addId();
  43. }
  44. // "unwrap" the NonLeafPositions stored in parentIter
  45. // and put them in a new list;
  46. LinkedList<Position> positionList = new LinkedList<Position>();
  47. Position pos;
  48. while (parentIter.hasNext()) {
  49. pos = parentIter.next();
  50. if (pos == null) {
  51. continue;
  52. }
  53. if (pos.getIndex() >= 0) {
  54. if (firstPos == null) {
  55. firstPos = pos;
  56. }
  57. lastPos = pos;
  58. }
  59. if (pos instanceof NonLeafPosition) {
  60. // pos was created by a child of this FlowLM
  61. positionList.add(pos.getPosition());
  62. lastLM = (pos.getPosition().getLM());
  63. if (firstLM == null) {
  64. firstLM = lastLM;
  65. }
  66. } else if (pos instanceof SpaceHandlingBreakPosition) {
  67. positionList.add(pos);
  68. } else {
  69. // pos was created by this LM, so it must be ignored
  70. }
  71. }
  72. if (firstPos == null) {
  73. return; //Nothing to do, return early
  74. //TODO This is a hack to avoid an NPE in the code block below.
  75. //If there's no firstPos/lastPos there's currently no way to
  76. //correctly determine first and last conditions. The Iterator
  77. //doesn't give us that info.
  78. }
  79. if (bslm != null) {
  80. bslm.registerMarkers(
  81. true,
  82. bslm.isFirst(firstPos),
  83. bslm.isLast(lastPos));
  84. }
  85. PositionIterator childPosIter = new PositionIterator(positionList.listIterator());
  86. while ((childLM = childPosIter.getNextChildLM()) != null) {
  87. // TODO vh: the test above might be problematic in some cases. See comment in
  88. // the TableCellLM.getNextKnuthElements method
  89. // Add the block areas to Area
  90. lc.setFlags(LayoutContext.FIRST_AREA, childLM == firstLM);
  91. lc.setFlags(LayoutContext.LAST_AREA, childLM == lastLM);
  92. // set the space adjustment ratio
  93. lc.setSpaceAdjust(layoutContext.getSpaceAdjust());
  94. // set space before for the first LM, in order to implement
  95. // display-align = center or after
  96. lc.setSpaceBefore((childLM == firstLM ? layoutContext.getSpaceBefore() : 0));
  97. // set space after for each LM, in order to implement
  98. // display-align = distribute
  99. lc.setSpaceAfter(layoutContext.getSpaceAfter());
  100. lc.setStackLimitBP(layoutContext.getStackLimitBP());
  101. childLM.addAreas(childPosIter, lc);
  102. }
  103. if (bslm != null) {
  104. bslm.registerMarkers(
  105. false,
  106. bslm.isFirst(firstPos),
  107. bslm.isLast(lastPos));
  108. }
  109. }
  110. }