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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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.Iterator;
  20. import java.util.LinkedList;
  21. import org.apache.fop.layoutmgr.SpaceResolver.SpaceHandlingBreakPosition;
  22. /**
  23. * Utility class which provides common code for the addAreas stage.
  24. */
  25. public class AreaAdditionUtil {
  26. private static class StackingIter extends PositionIterator {
  27. StackingIter(Iterator parentIter) {
  28. super(parentIter);
  29. }
  30. protected LayoutManager getLM(Object nextObj) {
  31. return ((Position) nextObj).getLM();
  32. }
  33. protected Position getPos(Object nextObj) {
  34. return ((Position) nextObj);
  35. }
  36. }
  37. /**
  38. * Creates the child areas for the given layout manager.
  39. * @param bslm the BlockStackingLayoutManager instance for which "addAreas" is performed.
  40. * @param parentIter the position iterator
  41. * @param layoutContext the layout context
  42. */
  43. public static void addAreas(BlockStackingLayoutManager bslm,
  44. PositionIterator parentIter, LayoutContext layoutContext) {
  45. LayoutManager childLM = null;
  46. LayoutContext lc = new LayoutContext(0);
  47. LayoutManager firstLM = null;
  48. LayoutManager lastLM = null;
  49. Position firstPos = null;
  50. Position lastPos = null;
  51. // "unwrap" the NonLeafPositions stored in parentIter
  52. // and put them in a new list;
  53. LinkedList positionList = new LinkedList();
  54. Position pos;
  55. while (parentIter.hasNext()) {
  56. pos = (Position)parentIter.next();
  57. if (pos == null) {
  58. continue;
  59. }
  60. if (pos.getIndex() >= 0) {
  61. if (firstPos == null) {
  62. firstPos = pos;
  63. }
  64. lastPos = pos;
  65. }
  66. if (pos instanceof NonLeafPosition) {
  67. // pos was created by a child of this FlowLM
  68. positionList.add(((NonLeafPosition) pos).getPosition());
  69. lastLM = ((NonLeafPosition) pos).getPosition().getLM();
  70. if (firstLM == null) {
  71. firstLM = lastLM;
  72. }
  73. } else if (pos instanceof SpaceHandlingBreakPosition) {
  74. positionList.add(pos);
  75. } else {
  76. // pos was created by this LM, so it must be ignored
  77. }
  78. }
  79. if (firstPos == null) {
  80. return; //Nothing to do, return early
  81. //TODO This is a hack to avoid an NPE in the code block below.
  82. //If there's no firstPos/lastPos there's currently no way to
  83. //correctly determine first and last conditions. The Iterator
  84. //doesn't give us that info.
  85. }
  86. if (bslm != null) {
  87. bslm.addMarkersToPage(
  88. true,
  89. bslm.isFirst(firstPos),
  90. bslm.isLast(lastPos));
  91. }
  92. StackingIter childPosIter = new StackingIter(positionList.listIterator());
  93. while ((childLM = childPosIter.getNextChildLM()) != null) {
  94. // TODO vh: the test above might be problematic in some cases. See comment in
  95. // the TableCellLM.getNextKnuthElements method
  96. // Add the block areas to Area
  97. lc.setFlags(LayoutContext.FIRST_AREA, childLM == firstLM);
  98. lc.setFlags(LayoutContext.LAST_AREA, childLM == lastLM);
  99. // set the space adjustment ratio
  100. lc.setSpaceAdjust(layoutContext.getSpaceAdjust());
  101. // set space before for the first LM, in order to implement
  102. // display-align = center or after
  103. lc.setSpaceBefore((childLM == firstLM ? layoutContext.getSpaceBefore() : 0));
  104. // set space after for each LM, in order to implement
  105. // display-align = distribute
  106. lc.setSpaceAfter(layoutContext.getSpaceAfter());
  107. lc.setStackLimitsFrom(layoutContext);
  108. childLM.addAreas(childPosIter, lc);
  109. }
  110. if (bslm != null) {
  111. bslm.addMarkersToPage(
  112. false,
  113. bslm.isFirst(firstPos),
  114. bslm.isLast(lastPos));
  115. }
  116. }
  117. }