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.

ListBlockLayoutManager.java 10KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  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.list;
  19. import java.util.LinkedList;
  20. import java.util.List;
  21. import java.util.Stack;
  22. import org.apache.commons.logging.Log;
  23. import org.apache.commons.logging.LogFactory;
  24. import org.apache.fop.area.Area;
  25. import org.apache.fop.area.Block;
  26. import org.apache.fop.fo.flow.ListBlock;
  27. import org.apache.fop.fo.properties.CommonBorderPaddingBackground;
  28. import org.apache.fop.fo.properties.KeepProperty;
  29. import org.apache.fop.layoutmgr.ElementListUtils;
  30. import org.apache.fop.layoutmgr.LayoutContext;
  31. import org.apache.fop.layoutmgr.LayoutManager;
  32. import org.apache.fop.layoutmgr.NonLeafPosition;
  33. import org.apache.fop.layoutmgr.Position;
  34. import org.apache.fop.layoutmgr.PositionIterator;
  35. import org.apache.fop.layoutmgr.SpacedBorderedPaddedBlockLayoutManager;
  36. import org.apache.fop.layoutmgr.TraitSetter;
  37. import org.apache.fop.traits.MinOptMax;
  38. import org.apache.fop.traits.SpaceVal;
  39. /**
  40. * LayoutManager for a list-block FO.
  41. * A list block contains list items which are stacked within
  42. * the list block area..
  43. */
  44. public class ListBlockLayoutManager extends SpacedBorderedPaddedBlockLayoutManager {
  45. /** logging instance */
  46. private static Log log = LogFactory.getLog(ListBlockLayoutManager.class);
  47. private Block curBlockArea;
  48. /**
  49. * Create a new list block layout manager.
  50. * @param node list-block to create the layout manager for
  51. */
  52. public ListBlockLayoutManager(ListBlock node) {
  53. super(node);
  54. }
  55. @Override
  56. protected CommonBorderPaddingBackground getCommonBorderPaddingBackground() {
  57. return getListBlockFO().getCommonBorderPaddingBackground();
  58. }
  59. /**
  60. * Convenience method.
  61. * @return the ListBlock node
  62. */
  63. protected ListBlock getListBlockFO() {
  64. return (ListBlock)fobj;
  65. }
  66. /** {@inheritDoc} */
  67. @Override
  68. public void initialize() {
  69. foSpaceBefore = new SpaceVal(
  70. getListBlockFO().getCommonMarginBlock().spaceBefore, this).getSpace();
  71. foSpaceAfter = new SpaceVal(
  72. getListBlockFO().getCommonMarginBlock().spaceAfter, this).getSpace();
  73. startIndent = getListBlockFO().getCommonMarginBlock().startIndent.getValue(this);
  74. endIndent = getListBlockFO().getCommonMarginBlock().endIndent.getValue(this);
  75. }
  76. private void resetSpaces() {
  77. this.discardBorderBefore = false;
  78. this.discardBorderAfter = false;
  79. this.discardPaddingBefore = false;
  80. this.discardPaddingAfter = false;
  81. this.effSpaceBefore = null;
  82. this.effSpaceAfter = null;
  83. }
  84. /** {@inheritDoc} */
  85. public List getNextKnuthElements(LayoutContext context, int alignment, Stack lmStack,
  86. Position restartPosition, LayoutManager restartAtLM) {
  87. resetSpaces();
  88. List returnList;
  89. returnList = super.getNextKnuthElements(context, alignment, lmStack, restartPosition, restartAtLM);
  90. //fox:widow-content-limit
  91. int widowRowLimit = getListBlockFO().getWidowContentLimit().getValue();
  92. if (widowRowLimit != 0) {
  93. ElementListUtils.removeLegalBreaks(returnList, widowRowLimit);
  94. }
  95. //fox:orphan-content-limit
  96. int orphanRowLimit = getListBlockFO().getOrphanContentLimit().getValue();
  97. if (orphanRowLimit != 0) {
  98. ElementListUtils.removeLegalBreaksFromEnd(returnList, orphanRowLimit);
  99. }
  100. return returnList;
  101. }
  102. /**
  103. * A list block generates one or more normal block areas whose child areas are
  104. * normal block areas returned by the children of fo:list-block. See XSL-FO 1.1 6.8.2.
  105. *
  106. * @param parentIter the position iterator
  107. * @param layoutContext the layout context for adding areas
  108. */
  109. @Override
  110. public void addAreas(PositionIterator parentIter, LayoutContext layoutContext) {
  111. getParentArea(null);
  112. // if this will create the first block area in a page
  113. // and display-align is after or center, add space before
  114. if (layoutContext.getSpaceBefore() > 0) {
  115. addBlockSpacing(0.0, MinOptMax.getInstance(layoutContext.getSpaceBefore()));
  116. }
  117. addId();
  118. // the list block contains areas stacked from each list item
  119. LayoutManager childLM;
  120. LayoutContext lc = LayoutContext.offspringOf(layoutContext);
  121. LayoutManager firstLM = null;
  122. LayoutManager lastLM = null;
  123. Position firstPos = null;
  124. Position lastPos = null;
  125. // "unwrap" the NonLeafPositions stored in parentIter
  126. // and put them in a new list;
  127. LinkedList<Position> positionList = new LinkedList<Position>();
  128. Position pos;
  129. while (parentIter.hasNext()) {
  130. pos = parentIter.next();
  131. if (pos.getIndex() >= 0) {
  132. if (firstPos == null) {
  133. firstPos = pos;
  134. }
  135. lastPos = pos;
  136. }
  137. if (pos instanceof NonLeafPosition
  138. && (pos.getPosition() != null)
  139. && pos.getPosition().getLM() != this) {
  140. // pos was created by a child of this ListBlockLM
  141. positionList.add(pos.getPosition());
  142. lastLM = pos.getPosition().getLM();
  143. if (firstLM == null) {
  144. firstLM = lastLM;
  145. }
  146. }
  147. }
  148. registerMarkers(true, isFirst(firstPos), isLast(lastPos));
  149. PositionIterator childPosIter = new PositionIterator(positionList.listIterator());
  150. while ((childLM = childPosIter.getNextChildLM()) != null) {
  151. // Add the block areas to Area
  152. // set the space adjustment ratio
  153. lc.setSpaceAdjust(layoutContext.getSpaceAdjust());
  154. lc.setFlags(LayoutContext.FIRST_AREA, childLM == firstLM);
  155. lc.setFlags(LayoutContext.LAST_AREA, childLM == lastLM);
  156. lc.setStackLimitBP(layoutContext.getStackLimitBP());
  157. childLM.addAreas(childPosIter, lc);
  158. }
  159. registerMarkers(false, isFirst(firstPos), isLast(lastPos));
  160. // We are done with this area add the background
  161. TraitSetter.addBackground(curBlockArea,
  162. getListBlockFO().getCommonBorderPaddingBackground(),
  163. this);
  164. TraitSetter.addSpaceBeforeAfter(curBlockArea, layoutContext.getSpaceAdjust(),
  165. effSpaceBefore, effSpaceAfter);
  166. flush();
  167. curBlockArea = null;
  168. resetSpaces();
  169. checkEndOfLayout(lastPos);
  170. }
  171. /**
  172. * Return an Area which can contain the passed childArea. The childArea
  173. * may not yet have any content, but it has essential traits set.
  174. * In general, if the LayoutManager already has an Area it simply returns
  175. * it. Otherwise, it makes a new Area of the appropriate class.
  176. * It gets a parent area for its area by calling its parent LM.
  177. * Finally, based on the dimensions of the parent area, it initializes
  178. * its own area. This includes setting the content IPD and the maximum
  179. * BPD.
  180. *
  181. * @param childArea the child area
  182. * @return the parent area of the child
  183. */
  184. @Override
  185. public Area getParentArea(Area childArea) {
  186. if (curBlockArea == null) {
  187. curBlockArea = new Block();
  188. curBlockArea.setChangeBarList(getChangeBarList());
  189. // Set up dimensions
  190. // Must get dimensions from parent area
  191. /*Area parentArea =*/ parentLayoutManager.getParentArea(curBlockArea);
  192. // set traits
  193. TraitSetter.setProducerID(curBlockArea, getListBlockFO().getId());
  194. TraitSetter.addBorders(curBlockArea,
  195. getListBlockFO().getCommonBorderPaddingBackground(),
  196. discardBorderBefore, discardBorderAfter, false, false, this);
  197. TraitSetter.addPadding(curBlockArea,
  198. getListBlockFO().getCommonBorderPaddingBackground(),
  199. discardPaddingBefore, discardPaddingAfter, false, false, this);
  200. TraitSetter.addMargins(curBlockArea,
  201. getListBlockFO().getCommonBorderPaddingBackground(),
  202. getListBlockFO().getCommonMarginBlock(),
  203. this);
  204. TraitSetter.addBreaks(curBlockArea,
  205. getListBlockFO().getBreakBefore(),
  206. getListBlockFO().getBreakAfter());
  207. int contentIPD = referenceIPD - getIPIndents();
  208. curBlockArea.setIPD(contentIPD);
  209. curBlockArea.setBidiLevel(getListBlockFO().getBidiLevel());
  210. setCurrentArea(curBlockArea);
  211. }
  212. return curBlockArea;
  213. }
  214. /**
  215. * Add the child area to this layout manager.
  216. *
  217. * @param childArea the child area to add
  218. */
  219. @Override
  220. public void addChildArea(Area childArea) {
  221. if (curBlockArea != null) {
  222. curBlockArea.addBlock((Block) childArea);
  223. }
  224. }
  225. /** {@inheritDoc} */
  226. @Override
  227. public KeepProperty getKeepTogetherProperty() {
  228. return getListBlockFO().getKeepTogether();
  229. }
  230. /** {@inheritDoc} */
  231. @Override
  232. public KeepProperty getKeepWithPreviousProperty() {
  233. return getListBlockFO().getKeepWithPrevious();
  234. }
  235. /** {@inheritDoc} */
  236. @Override
  237. public KeepProperty getKeepWithNextProperty() {
  238. return getListBlockFO().getKeepWithNext();
  239. }
  240. /** {@inheritDoc} */
  241. public boolean isRestartable() {
  242. return true;
  243. }
  244. }