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 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326
  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 org.apache.commons.logging.Log;
  22. import org.apache.commons.logging.LogFactory;
  23. import org.apache.fop.area.Area;
  24. import org.apache.fop.area.Block;
  25. import org.apache.fop.fo.flow.ListBlock;
  26. import org.apache.fop.fo.properties.KeepProperty;
  27. import org.apache.fop.layoutmgr.BlockStackingLayoutManager;
  28. import org.apache.fop.layoutmgr.ConditionalElementListener;
  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.RelSide;
  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 BlockStackingLayoutManager
  45. implements ConditionalElementListener {
  46. /** logging instance */
  47. private static Log log = LogFactory.getLog(ListBlockLayoutManager.class);
  48. private Block curBlockArea;
  49. private boolean discardBorderBefore;
  50. private boolean discardBorderAfter;
  51. private boolean discardPaddingBefore;
  52. private boolean discardPaddingAfter;
  53. private MinOptMax effSpaceBefore;
  54. private MinOptMax effSpaceAfter;
  55. /**
  56. * Create a new list block layout manager.
  57. * @param node list-block to create the layout manager for
  58. */
  59. public ListBlockLayoutManager(ListBlock node) {
  60. super(node);
  61. }
  62. /**
  63. * Convenience method.
  64. * @return the ListBlock node
  65. */
  66. protected ListBlock getListBlockFO() {
  67. return (ListBlock)fobj;
  68. }
  69. /** {@inheritDoc} */
  70. @Override
  71. public void initialize() {
  72. foSpaceBefore = new SpaceVal(
  73. getListBlockFO().getCommonMarginBlock().spaceBefore, this).getSpace();
  74. foSpaceAfter = new SpaceVal(
  75. getListBlockFO().getCommonMarginBlock().spaceAfter, this).getSpace();
  76. startIndent = getListBlockFO().getCommonMarginBlock().startIndent.getValue(this);
  77. endIndent = getListBlockFO().getCommonMarginBlock().endIndent.getValue(this);
  78. }
  79. private void resetSpaces() {
  80. this.discardBorderBefore = false;
  81. this.discardBorderAfter = false;
  82. this.discardPaddingBefore = false;
  83. this.discardPaddingAfter = false;
  84. this.effSpaceBefore = null;
  85. this.effSpaceAfter = null;
  86. }
  87. /** {@inheritDoc} */
  88. @Override
  89. public List getNextKnuthElements(LayoutContext context, int alignment) {
  90. resetSpaces();
  91. List returnList = super.getNextKnuthElements(context, alignment);
  92. //fox:widow-content-limit
  93. int widowRowLimit = getListBlockFO().getWidowContentLimit().getValue();
  94. if (widowRowLimit != 0) {
  95. ElementListUtils.removeLegalBreaks(returnList, widowRowLimit);
  96. }
  97. //fox:orphan-content-limit
  98. int orphanRowLimit = getListBlockFO().getOrphanContentLimit().getValue();
  99. if (orphanRowLimit != 0) {
  100. ElementListUtils.removeLegalBreaksFromEnd(returnList, orphanRowLimit);
  101. }
  102. return returnList;
  103. }
  104. /**
  105. * A list block generates one or more normal block areas whose child areas are
  106. * normal block areas returned by the children of fo:list-block. See XSL-FO 1.1 6.8.2.
  107. *
  108. * @param parentIter the position iterator
  109. * @param layoutContext the layout context for adding areas
  110. */
  111. @Override
  112. public void addAreas(PositionIterator parentIter, LayoutContext layoutContext) {
  113. getParentArea(null);
  114. // if this will create the first block area in a page
  115. // and display-align is after or center, add space before
  116. if (layoutContext.getSpaceBefore() > 0) {
  117. addBlockSpacing(0.0, MinOptMax.getInstance(layoutContext.getSpaceBefore()));
  118. }
  119. addId();
  120. // the list block contains areas stacked from each list item
  121. LayoutManager childLM;
  122. LayoutContext lc = LayoutContext.offspringOf(layoutContext);
  123. LayoutManager firstLM = null;
  124. LayoutManager lastLM = null;
  125. Position firstPos = null;
  126. Position lastPos = null;
  127. // "unwrap" the NonLeafPositions stored in parentIter
  128. // and put them in a new list;
  129. LinkedList<Position> positionList = new LinkedList<Position>();
  130. Position pos;
  131. while (parentIter.hasNext()) {
  132. pos = parentIter.next();
  133. if (pos.getIndex() >= 0) {
  134. if (firstPos == null) {
  135. firstPos = pos;
  136. }
  137. lastPos = pos;
  138. }
  139. if (pos instanceof NonLeafPosition
  140. && (pos.getPosition() != null)
  141. && pos.getPosition().getLM() != this) {
  142. // pos was created by a child of this ListBlockLM
  143. positionList.add(pos.getPosition());
  144. lastLM = pos.getPosition().getLM();
  145. if (firstLM == null) {
  146. firstLM = lastLM;
  147. }
  148. }
  149. }
  150. registerMarkers(true, isFirst(firstPos), isLast(lastPos));
  151. PositionIterator childPosIter = new PositionIterator(positionList.listIterator());
  152. while ((childLM = childPosIter.getNextChildLM()) != null) {
  153. // Add the block areas to Area
  154. // set the space adjustment ratio
  155. lc.setSpaceAdjust(layoutContext.getSpaceAdjust());
  156. lc.setFlags(LayoutContext.FIRST_AREA, childLM == firstLM);
  157. lc.setFlags(LayoutContext.LAST_AREA, childLM == lastLM);
  158. lc.setStackLimitBP(layoutContext.getStackLimitBP());
  159. childLM.addAreas(childPosIter, lc);
  160. }
  161. registerMarkers(false, isFirst(firstPos), isLast(lastPos));
  162. // We are done with this area add the background
  163. TraitSetter.addBackground(curBlockArea,
  164. getListBlockFO().getCommonBorderPaddingBackground(),
  165. this);
  166. TraitSetter.addSpaceBeforeAfter(curBlockArea, layoutContext.getSpaceAdjust(),
  167. effSpaceBefore, effSpaceAfter);
  168. flush();
  169. curBlockArea = null;
  170. resetSpaces();
  171. checkEndOfLayout(lastPos);
  172. }
  173. /**
  174. * Return an Area which can contain the passed childArea. The childArea
  175. * may not yet have any content, but it has essential traits set.
  176. * In general, if the LayoutManager already has an Area it simply returns
  177. * it. Otherwise, it makes a new Area of the appropriate class.
  178. * It gets a parent area for its area by calling its parent LM.
  179. * Finally, based on the dimensions of the parent area, it initializes
  180. * its own area. This includes setting the content IPD and the maximum
  181. * BPD.
  182. *
  183. * @param childArea the child area
  184. * @return the parent area of the child
  185. */
  186. @Override
  187. public Area getParentArea(Area childArea) {
  188. if (curBlockArea == null) {
  189. curBlockArea = new Block();
  190. // Set up dimensions
  191. // Must get dimensions from parent area
  192. /*Area parentArea =*/ parentLayoutManager.getParentArea(curBlockArea);
  193. // set traits
  194. TraitSetter.setProducerID(curBlockArea, getListBlockFO().getId());
  195. TraitSetter.addBorders(curBlockArea,
  196. getListBlockFO().getCommonBorderPaddingBackground(),
  197. discardBorderBefore, discardBorderAfter, false, false, this);
  198. TraitSetter.addPadding(curBlockArea,
  199. getListBlockFO().getCommonBorderPaddingBackground(),
  200. discardPaddingBefore, discardPaddingAfter, false, false, this);
  201. TraitSetter.addMargins(curBlockArea,
  202. getListBlockFO().getCommonBorderPaddingBackground(),
  203. getListBlockFO().getCommonMarginBlock(),
  204. this);
  205. TraitSetter.addBreaks(curBlockArea,
  206. getListBlockFO().getBreakBefore(),
  207. getListBlockFO().getBreakAfter());
  208. int contentIPD = referenceIPD - getIPIndents();
  209. curBlockArea.setIPD(contentIPD);
  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 void notifySpace(RelSide side, MinOptMax effectiveLength) {
  242. if (RelSide.BEFORE == side) {
  243. if (log.isDebugEnabled()) {
  244. log.debug(this + ": Space " + side + ", "
  245. + this.effSpaceBefore + "-> " + effectiveLength);
  246. }
  247. this.effSpaceBefore = effectiveLength;
  248. } else {
  249. if (log.isDebugEnabled()) {
  250. log.debug(this + ": Space " + side + ", "
  251. + this.effSpaceAfter + "-> " + effectiveLength);
  252. }
  253. this.effSpaceAfter = effectiveLength;
  254. }
  255. }
  256. /** {@inheritDoc} */
  257. public void notifyBorder(RelSide side, MinOptMax effectiveLength) {
  258. if (effectiveLength == null) {
  259. if (RelSide.BEFORE == side) {
  260. this.discardBorderBefore = true;
  261. } else {
  262. this.discardBorderAfter = true;
  263. }
  264. }
  265. if (log.isDebugEnabled()) {
  266. log.debug(this + ": Border " + side + " -> " + effectiveLength);
  267. }
  268. }
  269. /** {@inheritDoc} */
  270. public void notifyPadding(RelSide side, MinOptMax effectiveLength) {
  271. if (effectiveLength == null) {
  272. if (RelSide.BEFORE == side) {
  273. this.discardPaddingBefore = true;
  274. } else {
  275. this.discardPaddingAfter = true;
  276. }
  277. }
  278. if (log.isDebugEnabled()) {
  279. log.debug(this + ": Padding " + side + " -> " + effectiveLength);
  280. }
  281. }
  282. }