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.

BlockLayoutManager.java 13KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350
  1. /*
  2. * $Id: BlockLayoutManager.java,v 1.32 2003/03/05 20:38:26 jeremias Exp $
  3. * ============================================================================
  4. * The Apache Software License, Version 1.1
  5. * ============================================================================
  6. *
  7. * Copyright (C) 1999-2003 The Apache Software Foundation. All rights reserved.
  8. *
  9. * Redistribution and use in source and binary forms, with or without modifica-
  10. * tion, are permitted provided that the following conditions are met:
  11. *
  12. * 1. Redistributions of source code must retain the above copyright notice,
  13. * this list of conditions and the following disclaimer.
  14. *
  15. * 2. Redistributions in binary form must reproduce the above copyright notice,
  16. * this list of conditions and the following disclaimer in the documentation
  17. * and/or other materials provided with the distribution.
  18. *
  19. * 3. The end-user documentation included with the redistribution, if any, must
  20. * include the following acknowledgment: "This product includes software
  21. * developed by the Apache Software Foundation (http://www.apache.org/)."
  22. * Alternately, this acknowledgment may appear in the software itself, if
  23. * and wherever such third-party acknowledgments normally appear.
  24. *
  25. * 4. The names "FOP" and "Apache Software Foundation" must not be used to
  26. * endorse or promote products derived from this software without prior
  27. * written permission. For written permission, please contact
  28. * apache@apache.org.
  29. *
  30. * 5. Products derived from this software may not be called "Apache", nor may
  31. * "Apache" appear in their name, without prior written permission of the
  32. * Apache Software Foundation.
  33. *
  34. * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES,
  35. * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
  36. * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  37. * APACHE SOFTWARE FOUNDATION OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
  38. * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLU-
  39. * DING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
  40. * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
  41. * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  42. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  43. * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  44. * ============================================================================
  45. *
  46. * This software consists of voluntary contributions made by many individuals
  47. * on behalf of the Apache Software Foundation and was originally created by
  48. * James Tauber <jtauber@jtauber.com>. For more information on the Apache
  49. * Software Foundation, please see <http://www.apache.org/>.
  50. */
  51. package org.apache.fop.layoutmgr;
  52. import java.util.ListIterator;
  53. import java.util.ArrayList;
  54. import java.util.List;
  55. import org.apache.fop.fo.FObj;
  56. import org.apache.fop.fo.TextInfo;
  57. import org.apache.fop.fo.PropertyManager;
  58. import org.apache.fop.area.Area;
  59. import org.apache.fop.area.Block;
  60. import org.apache.fop.area.LineArea;
  61. import org.apache.fop.traits.LayoutProps;
  62. import org.apache.fop.fo.properties.CommonBorderAndPadding;
  63. import org.apache.fop.fo.properties.CommonBackground;
  64. import org.apache.fop.traits.MinOptMax;
  65. /**
  66. * LayoutManager for a block FO.
  67. */
  68. public class BlockLayoutManager extends BlockStackingLayoutManager {
  69. private Block curBlockArea;
  70. private LayoutProps layoutProps;
  71. private CommonBorderAndPadding borderProps;
  72. private CommonBackground backgroundProps;
  73. private int lead = 12000;
  74. private int lineHeight = 14000;
  75. private int follow = 2000;
  76. private int iStartPos = 0;
  77. protected List childBreaks = new java.util.ArrayList();
  78. /**
  79. * Iterator for Block layout.
  80. * This iterator combines consecutive inline areas and
  81. * creates a line layout manager.
  82. * The use of this iterator means that it can be reset properly.
  83. */
  84. protected class BlockLMiter extends LMiter {
  85. private ListIterator proxy;
  86. public BlockLMiter(ListIterator pr) {
  87. super(null);
  88. proxy = pr;
  89. }
  90. protected boolean preLoadNext() {
  91. while (proxy.hasNext()) {
  92. LayoutProcessor lm = (LayoutProcessor) proxy.next();
  93. lm.setParent(BlockLayoutManager.this);
  94. if (lm.generatesInlineAreas()) {
  95. LineLayoutManager lineLM = createLineManager(lm);
  96. listLMs.add(lineLM);
  97. } else {
  98. listLMs.add(lm);
  99. }
  100. if (curPos < listLMs.size()) {
  101. return true;
  102. }
  103. }
  104. return false;
  105. }
  106. protected LineLayoutManager createLineManager(
  107. LayoutManager firstlm) {
  108. LayoutProcessor lm;
  109. List inlines = new ArrayList();
  110. inlines.add(firstlm);
  111. while (proxy.hasNext()) {
  112. lm = (LayoutProcessor) proxy.next();
  113. lm.setParent(BlockLayoutManager.this);
  114. if (lm.generatesInlineAreas()) {
  115. inlines.add(lm);
  116. } else {
  117. proxy.previous();
  118. break;
  119. }
  120. }
  121. LineLayoutManager child;
  122. child = new LineLayoutManager(lineHeight,
  123. lead, follow);
  124. child.setUserAgent(getUserAgent());
  125. child.setFObj(fobj);
  126. child.setLMiter(inlines.listIterator());
  127. return child;
  128. }
  129. }
  130. public BlockLayoutManager() {
  131. }
  132. /**
  133. * Set the FO object for this layout manager
  134. *
  135. * @param fo the fo for this layout manager
  136. */
  137. public void setFObj(FObj fo) {
  138. super.setFObj(fo);
  139. childLMiter = new BlockLMiter(childLMiter);
  140. }
  141. public void setBlockTextInfo(TextInfo ti) {
  142. lead = ti.fs.getAscender();
  143. follow = ti.fs.getDescender();
  144. lineHeight = ti.lineHeight;
  145. }
  146. /**
  147. * This method provides a hook for a LayoutManager to intialize traits
  148. * for the areas it will create, based on Properties set on its FO.
  149. */
  150. protected void initProperties(PropertyManager pm) {
  151. layoutProps = pm.getLayoutProps();
  152. borderProps = pm.getBorderAndPadding();
  153. backgroundProps = pm.getBackgroundProps();
  154. }
  155. public BreakPoss getNextBreakPoss(LayoutContext context) {
  156. LayoutProcessor curLM; // currently active LM
  157. int ipd = context.getRefIPD();
  158. MinOptMax stackSize = new MinOptMax();
  159. // if starting add space before
  160. stackSize.add(layoutProps.spaceBefore.getSpace());
  161. BreakPoss lastPos = null;
  162. while ((curLM = getChildLM()) != null) {
  163. // Make break positions and return blocks!
  164. // Set up a LayoutContext
  165. BreakPoss bp;
  166. LayoutContext childLC = new LayoutContext(0);
  167. // if line layout manager then set stack limit to ipd
  168. // line LM actually generates a LineArea which is a block
  169. if (curLM.generatesInlineAreas()) {
  170. // set stackLimit for lines
  171. childLC.setStackLimit(new MinOptMax(ipd/* - iIndents - iTextIndent*/));
  172. childLC.setRefIPD(ipd);
  173. } else {
  174. childLC.setStackLimit(
  175. MinOptMax.subtract(context.getStackLimit(),
  176. stackSize));
  177. childLC.setRefIPD(ipd);
  178. }
  179. boolean over = false;
  180. while (!curLM.isFinished()) {
  181. if ((bp = curLM.getNextBreakPoss(childLC)) != null) {
  182. if (stackSize.opt + bp.getStackingSize().opt > context.getStackLimit().max) {
  183. // reset to last break
  184. if (lastPos != null) {
  185. LayoutProcessor lm = lastPos.getLayoutManager();
  186. lm.resetPosition(lastPos.getPosition());
  187. if (lm != curLM) {
  188. curLM.resetPosition(null);
  189. }
  190. } else {
  191. curLM.resetPosition(null);
  192. }
  193. over = true;
  194. break;
  195. }
  196. stackSize.add(bp.getStackingSize());
  197. lastPos = bp;
  198. childBreaks.add(bp);
  199. if (bp.nextBreakOverflows()) {
  200. over = true;
  201. break;
  202. }
  203. if (curLM.generatesInlineAreas()) {
  204. // Reset stackLimit for non-first lines
  205. childLC.setStackLimit(new MinOptMax(ipd/* - iIndents*/));
  206. } else {
  207. childLC.setStackLimit(MinOptMax.subtract(
  208. context.getStackLimit(), stackSize));
  209. }
  210. }
  211. }
  212. if (getChildLM() == null || over) {
  213. if (getChildLM() == null) {
  214. setFinished(true);
  215. stackSize.add(layoutProps.spaceAfter.getSpace());
  216. }
  217. BreakPoss breakPoss = new BreakPoss(
  218. new LeafPosition(this, childBreaks.size() - 1));
  219. if (over) {
  220. breakPoss.setFlag(BreakPoss.NEXT_OVERFLOWS, true);
  221. }
  222. breakPoss.setStackingSize(stackSize);
  223. return breakPoss;
  224. }
  225. }
  226. setFinished(true);
  227. BreakPoss breakPoss = new BreakPoss(new LeafPosition(this, -2));
  228. breakPoss.setStackingSize(stackSize);
  229. return breakPoss;
  230. }
  231. public void addAreas(PositionIterator parentIter,
  232. LayoutContext layoutContext) {
  233. getParentArea(null);
  234. // if adjusted space before
  235. double adjust = layoutContext.getSpaceAdjust();
  236. addBlockSpacing(adjust, layoutProps.spaceBefore.getSpace());
  237. addID();
  238. addMarkers(true, true);
  239. LayoutProcessor childLM;
  240. LayoutContext lc = new LayoutContext(0);
  241. while (parentIter.hasNext()) {
  242. LeafPosition lfp = (LeafPosition) parentIter.next();
  243. if (lfp.getLeafPos() == -2) {
  244. curBlockArea = null;
  245. flush();
  246. return;
  247. }
  248. // Add the block areas to Area
  249. PositionIterator breakPosIter =
  250. new BreakPossPosIter(childBreaks, iStartPos,
  251. lfp.getLeafPos() + 1);
  252. iStartPos = lfp.getLeafPos() + 1;
  253. while ((childLM = breakPosIter.getNextChildLM()) != null) {
  254. childLM.addAreas(breakPosIter, lc);
  255. }
  256. }
  257. addMarkers(false, true);
  258. flush();
  259. // if adjusted space after
  260. addBlockSpacing(adjust, layoutProps.spaceAfter.getSpace());
  261. curBlockArea = null;
  262. }
  263. /**
  264. * Return an Area which can contain the passed childArea. The childArea
  265. * may not yet have any content, but it has essential traits set.
  266. * In general, if the LayoutManager already has an Area it simply returns
  267. * it. Otherwise, it makes a new Area of the appropriate class.
  268. * It gets a parent area for its area by calling its parent LM.
  269. * Finally, based on the dimensions of the parent area, it initializes
  270. * its own area. This includes setting the content IPD and the maximum
  271. * BPD.
  272. */
  273. public Area getParentArea(Area childArea) {
  274. if (curBlockArea == null) {
  275. curBlockArea = new Block();
  276. // set traits
  277. TraitSetter.addBorders(curBlockArea, borderProps);
  278. TraitSetter.addBackground(curBlockArea, backgroundProps);
  279. // Set up dimensions
  280. // Must get dimensions from parent area
  281. Area parentArea = parentLM.getParentArea(curBlockArea);
  282. int referenceIPD = parentArea.getIPD();
  283. curBlockArea.setIPD(referenceIPD);
  284. curBlockArea.setWidth(referenceIPD);
  285. // Get reference IPD from parentArea
  286. setCurrentArea(curBlockArea); // ??? for generic operations
  287. }
  288. return curBlockArea;
  289. }
  290. public void addChild(Area childArea) {
  291. if (curBlockArea != null) {
  292. if (childArea instanceof LineArea) {
  293. curBlockArea.addLineArea((LineArea) childArea);
  294. } else {
  295. curBlockArea.addBlock((Block) childArea);
  296. }
  297. }
  298. }
  299. public void resetPosition(Position resetPos) {
  300. if (resetPos == null) {
  301. reset(null);
  302. childBreaks.clear();
  303. iStartPos = 0;
  304. } else {
  305. //reset(resetPos);
  306. LayoutManager lm = resetPos.getLM();
  307. }
  308. }
  309. }