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

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