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.

ListItemContentLayoutManager.java 8.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  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.fop.area.Area;
  23. import org.apache.fop.area.Block;
  24. import org.apache.fop.fo.flow.AbstractListItemPart;
  25. import org.apache.fop.fo.flow.ListItemBody;
  26. import org.apache.fop.fo.flow.ListItemLabel;
  27. import org.apache.fop.fo.properties.KeepProperty;
  28. import org.apache.fop.layoutmgr.BlockStackingLayoutManager;
  29. import org.apache.fop.layoutmgr.BreakOpportunity;
  30. import org.apache.fop.layoutmgr.Keep;
  31. import org.apache.fop.layoutmgr.LayoutContext;
  32. import org.apache.fop.layoutmgr.LayoutManager;
  33. import org.apache.fop.layoutmgr.ListElement;
  34. import org.apache.fop.layoutmgr.NonLeafPosition;
  35. import org.apache.fop.layoutmgr.Position;
  36. import org.apache.fop.layoutmgr.PositionIterator;
  37. import org.apache.fop.layoutmgr.SpaceResolver.SpaceHandlingBreakPosition;
  38. import org.apache.fop.layoutmgr.TraitSetter;
  39. /**
  40. * LayoutManager for a list-item-label or list-item-body FO.
  41. */
  42. public class ListItemContentLayoutManager extends BlockStackingLayoutManager implements BreakOpportunity {
  43. private Block curBlockArea;
  44. private int xOffset;
  45. // private int itemIPD; // FIXME: never written!
  46. /**
  47. * Create a new Cell layout manager.
  48. * @param node list-item-label node
  49. */
  50. public ListItemContentLayoutManager(ListItemLabel node) {
  51. super(node);
  52. }
  53. /**
  54. * Create a new Cell layout manager.
  55. * @param node list-item-body node
  56. */
  57. public ListItemContentLayoutManager(ListItemBody node) {
  58. super(node);
  59. }
  60. /**
  61. * Convenience method.
  62. * @return the ListBlock node
  63. */
  64. protected AbstractListItemPart getPartFO() {
  65. return (AbstractListItemPart)fobj;
  66. }
  67. /**
  68. * Set the x offset of this list item.
  69. * This offset is used to set the absolute position
  70. * of the list item within the parent block area.
  71. *
  72. * @param off the x offset
  73. */
  74. public void setXOffset(int off) {
  75. xOffset = off;
  76. }
  77. /**
  78. * Add the areas for the break points.
  79. * The list item contains block stacking layout managers
  80. * that add block areas.
  81. *
  82. * @param parentIter the iterator of the break positions
  83. * @param layoutContext the layout context for adding the areas
  84. */
  85. @Override
  86. public void addAreas(PositionIterator parentIter,
  87. LayoutContext layoutContext) {
  88. getParentArea(null);
  89. addId();
  90. LayoutManager childLM;
  91. LayoutContext lc = LayoutContext.offspringOf(layoutContext);
  92. LayoutManager firstLM = null;
  93. LayoutManager lastLM = null;
  94. Position firstPos = null;
  95. Position lastPos = null;
  96. // "unwrap" the NonLeafPositions stored in parentIter
  97. // and put them in a new list;
  98. LinkedList<Position> positionList = new LinkedList<Position>();
  99. Position pos;
  100. while (parentIter.hasNext()) {
  101. pos = parentIter.next();
  102. if (pos == null) {
  103. continue;
  104. }
  105. if (pos.getIndex() >= 0) {
  106. if (firstPos == null) {
  107. firstPos = pos;
  108. }
  109. lastPos = pos;
  110. }
  111. if (pos instanceof NonLeafPosition) {
  112. // pos was created by a child of this ListBlockLM
  113. positionList.add(pos.getPosition());
  114. lastLM = pos.getPosition().getLM();
  115. if (firstLM == null) {
  116. firstLM = lastLM;
  117. }
  118. } else if (pos instanceof SpaceHandlingBreakPosition) {
  119. positionList.add(pos);
  120. } else {
  121. // pos was created by this ListBlockLM, so it must be ignored
  122. }
  123. }
  124. registerMarkers(true, isFirst(firstPos), isLast(lastPos));
  125. PositionIterator childPosIter = new PositionIterator(positionList.listIterator());
  126. while ((childLM = childPosIter.getNextChildLM()) != null) {
  127. // Add the block areas to Area
  128. lc.setFlags(LayoutContext.FIRST_AREA, childLM == firstLM);
  129. lc.setFlags(LayoutContext.LAST_AREA, childLM == lastLM);
  130. // set the space adjustment ratio
  131. lc.setSpaceAdjust(layoutContext.getSpaceAdjust());
  132. lc.setStackLimitBP(layoutContext.getStackLimitBP());
  133. childLM.addAreas(childPosIter, lc);
  134. }
  135. registerMarkers(false, isFirst(firstPos), isLast(lastPos));
  136. flush();
  137. curBlockArea = null;
  138. checkEndOfLayout(lastPos);
  139. }
  140. /**
  141. * Return an Area which can contain the passed childArea. The childArea
  142. * may not yet have any content, but it has essential traits set.
  143. * In general, if the LayoutManager already has an Area it simply returns
  144. * it. Otherwise, it makes a new Area of the appropriate class.
  145. * It gets a parent area for its area by calling its parent LM.
  146. * Finally, based on the dimensions of the parent area, it initializes
  147. * its own area. This includes setting the content IPD and the maximum
  148. * BPD.
  149. *
  150. * @param childArea the child area to get the parent for
  151. * @return the parent area
  152. */
  153. @Override
  154. public Area getParentArea(Area childArea) {
  155. if (curBlockArea == null) {
  156. curBlockArea = new Block();
  157. curBlockArea.setChangeBarList(getChangeBarList());
  158. curBlockArea.setPositioning(Block.ABSOLUTE);
  159. // set position
  160. curBlockArea.setXOffset(xOffset);
  161. // FIXME - itemIPD is never written!
  162. curBlockArea.setIPD(/*itemIPD*/0);
  163. //curBlockArea.setHeight();
  164. curBlockArea.setBidiLevel(getPartFO().getBidiLevel());
  165. TraitSetter.setProducerID(curBlockArea, getPartFO().getId());
  166. // Set up dimensions
  167. Area parentArea = parentLayoutManager.getParentArea(curBlockArea);
  168. int referenceIPD = parentArea.getIPD();
  169. curBlockArea.setIPD(referenceIPD);
  170. // Get reference IPD from parentArea
  171. setCurrentArea(curBlockArea); // ??? for generic operations
  172. }
  173. return curBlockArea;
  174. }
  175. /**
  176. * Add the child to the list item area.
  177. *
  178. * @param childArea the child to add to the cell
  179. */
  180. @Override
  181. public void addChildArea(Area childArea) {
  182. if (curBlockArea != null) {
  183. curBlockArea.addBlock((Block) childArea);
  184. }
  185. }
  186. /** {@inheritDoc} */
  187. @Override
  188. public KeepProperty getKeepTogetherProperty() {
  189. return getPartFO().getKeepTogether();
  190. }
  191. /** {@inheritDoc} */
  192. @Override
  193. public Keep getKeepWithNext() {
  194. return Keep.KEEP_AUTO;
  195. }
  196. /** {@inheritDoc} */
  197. @Override
  198. public Keep getKeepWithPrevious() {
  199. return Keep.KEEP_AUTO;
  200. }
  201. /** {@inheritDoc} */
  202. public List getNextKnuthElements(LayoutContext context, int alignment, Stack lmStack,
  203. Position restartPosition, LayoutManager restartAtLM) {
  204. List<ListElement> elements = new LinkedList<ListElement>();
  205. do {
  206. elements.addAll(super.getNextKnuthElements(context, alignment, lmStack, restartPosition,
  207. restartAtLM));
  208. } while (!isFinished());
  209. return elements;
  210. }
  211. }