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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351
  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.fonts.*;
  65. import org.apache.fop.apps.*;
  66. /**
  67. * LayoutManager for a block FO.
  68. */
  69. public class BlockLayoutManager extends BlockStackingLayoutManager {
  70. private Block curBlockArea;
  71. private LayoutProps layoutProps;
  72. private CommonBorderAndPadding borderProps;
  73. private CommonBackground backgroundProps;
  74. private int lead = 12000;
  75. private int lineHeight = 14000;
  76. private int follow = 2000;
  77. private int iStartPos = 0;
  78. protected List childBreaks = new java.util.ArrayList();
  79. /**
  80. * Iterator for Block layout.
  81. * This iterator combines consecutive inline areas and
  82. * creates a line layout manager.
  83. * The use of this iterator means that it can be reset properly.
  84. */
  85. protected class BlockLMiter extends LMiter {
  86. private ListIterator proxy;
  87. public BlockLMiter(ListIterator pr) {
  88. super(null);
  89. proxy = pr;
  90. }
  91. protected boolean preLoadNext() {
  92. while (proxy.hasNext()) {
  93. LayoutProcessor lm = (LayoutProcessor) proxy.next();
  94. lm.setParent(BlockLayoutManager.this);
  95. if (lm.generatesInlineAreas()) {
  96. LineLayoutManager lineLM = createLineManager(lm);
  97. listLMs.add(lineLM);
  98. } else {
  99. listLMs.add(lm);
  100. }
  101. if (curPos < listLMs.size()) {
  102. return true;
  103. }
  104. }
  105. return false;
  106. }
  107. protected LineLayoutManager createLineManager(
  108. LayoutManager firstlm) {
  109. LayoutProcessor lm;
  110. List inlines = new ArrayList();
  111. inlines.add(firstlm);
  112. while (proxy.hasNext()) {
  113. lm = (LayoutProcessor) proxy.next();
  114. lm.setParent(BlockLayoutManager.this);
  115. if (lm.generatesInlineAreas()) {
  116. inlines.add(lm);
  117. } else {
  118. proxy.previous();
  119. break;
  120. }
  121. }
  122. LineLayoutManager child;
  123. child = new LineLayoutManager(lineHeight,
  124. lead, follow);
  125. child.setUserAgent(getUserAgent());
  126. child.setFObj(fobj);
  127. child.setLMiter(inlines.listIterator());
  128. return child;
  129. }
  130. }
  131. public BlockLayoutManager() {
  132. }
  133. /**
  134. * Set the FO object for this layout manager
  135. *
  136. * @param fo the fo for this layout manager
  137. */
  138. public void setFObj(FObj fo) {
  139. super.setFObj(fo);
  140. childLMiter = new BlockLMiter(childLMiter);
  141. }
  142. public void setBlockTextInfo(TextInfo ti) {
  143. lead = ti.fs.getAscender();
  144. follow = ti.fs.getDescender();
  145. lineHeight = ti.lineHeight;
  146. }
  147. /**
  148. * This method provides a hook for a LayoutManager to intialize traits
  149. * for the areas it will create, based on Properties set on its FO.
  150. */
  151. protected void initProperties(PropertyManager pm) {
  152. layoutProps = pm.getLayoutProps();
  153. borderProps = pm.getBorderAndPadding();
  154. backgroundProps = pm.getBackgroundProps();
  155. }
  156. public BreakPoss getNextBreakPoss(LayoutContext context) {
  157. LayoutProcessor curLM; // currently active LM
  158. int ipd = context.getRefIPD();
  159. MinOptMax stackSize = new MinOptMax();
  160. // if starting add space before
  161. stackSize.add(layoutProps.spaceBefore.getSpace());
  162. BreakPoss lastPos = null;
  163. while ((curLM = getChildLM()) != null) {
  164. // Make break positions and return blocks!
  165. // Set up a LayoutContext
  166. BreakPoss bp;
  167. LayoutContext childLC = new LayoutContext(0);
  168. // if line layout manager then set stack limit to ipd
  169. // line LM actually generates a LineArea which is a block
  170. if (curLM.generatesInlineAreas()) {
  171. // set stackLimit for lines
  172. childLC.setStackLimit(new MinOptMax(ipd/* - iIndents - iTextIndent*/));
  173. childLC.setRefIPD(ipd);
  174. } else {
  175. childLC.setStackLimit(
  176. MinOptMax.subtract(context.getStackLimit(),
  177. stackSize));
  178. childLC.setRefIPD(ipd);
  179. }
  180. boolean over = false;
  181. while (!curLM.isFinished()) {
  182. if ((bp = curLM.getNextBreakPoss(childLC)) != null) {
  183. if (stackSize.opt + bp.getStackingSize().opt > context.getStackLimit().max) {
  184. // reset to last break
  185. if (lastPos != null) {
  186. LayoutProcessor lm = lastPos.getLayoutManager();
  187. lm.resetPosition(lastPos.getPosition());
  188. if (lm != curLM) {
  189. curLM.resetPosition(null);
  190. }
  191. } else {
  192. curLM.resetPosition(null);
  193. }
  194. over = true;
  195. break;
  196. }
  197. stackSize.add(bp.getStackingSize());
  198. lastPos = bp;
  199. childBreaks.add(bp);
  200. if (bp.nextBreakOverflows()) {
  201. over = true;
  202. break;
  203. }
  204. if (curLM.generatesInlineAreas()) {
  205. // Reset stackLimit for non-first lines
  206. childLC.setStackLimit(new MinOptMax(ipd/* - iIndents*/));
  207. } else {
  208. childLC.setStackLimit(MinOptMax.subtract(
  209. context.getStackLimit(), stackSize));
  210. }
  211. }
  212. }
  213. if (getChildLM() == null || over) {
  214. if (getChildLM() == null) {
  215. setFinished(true);
  216. stackSize.add(layoutProps.spaceAfter.getSpace());
  217. }
  218. BreakPoss breakPoss = new BreakPoss(
  219. new LeafPosition(this, childBreaks.size() - 1));
  220. if (over) {
  221. breakPoss.setFlag(BreakPoss.NEXT_OVERFLOWS, true);
  222. }
  223. breakPoss.setStackingSize(stackSize);
  224. return breakPoss;
  225. }
  226. }
  227. setFinished(true);
  228. BreakPoss breakPoss = new BreakPoss(new LeafPosition(this, -2));
  229. breakPoss.setStackingSize(stackSize);
  230. return breakPoss;
  231. }
  232. public void addAreas(PositionIterator parentIter,
  233. LayoutContext layoutContext) {
  234. getParentArea(null);
  235. // if adjusted space before
  236. double adjust = layoutContext.getSpaceAdjust();
  237. addBlockSpacing(adjust, layoutProps.spaceBefore.getSpace());
  238. addID();
  239. addMarkers(true, true);
  240. LayoutProcessor childLM;
  241. LayoutContext lc = new LayoutContext(0);
  242. while (parentIter.hasNext()) {
  243. LeafPosition lfp = (LeafPosition) parentIter.next();
  244. if (lfp.getLeafPos() == -2) {
  245. curBlockArea = null;
  246. flush();
  247. return;
  248. }
  249. // Add the block areas to Area
  250. PositionIterator breakPosIter =
  251. new BreakPossPosIter(childBreaks, iStartPos,
  252. lfp.getLeafPos() + 1);
  253. iStartPos = lfp.getLeafPos() + 1;
  254. while ((childLM = breakPosIter.getNextChildLM()) != null) {
  255. childLM.addAreas(breakPosIter, lc);
  256. }
  257. }
  258. addMarkers(false, true);
  259. flush();
  260. // if adjusted space after
  261. addBlockSpacing(adjust, layoutProps.spaceAfter.getSpace());
  262. curBlockArea = null;
  263. }
  264. /**
  265. * Return an Area which can contain the passed childArea. The childArea
  266. * may not yet have any content, but it has essential traits set.
  267. * In general, if the LayoutManager already has an Area it simply returns
  268. * it. Otherwise, it makes a new Area of the appropriate class.
  269. * It gets a parent area for its area by calling its parent LM.
  270. * Finally, based on the dimensions of the parent area, it initializes
  271. * its own area. This includes setting the content IPD and the maximum
  272. * BPD.
  273. */
  274. public Area getParentArea(Area childArea) {
  275. if (curBlockArea == null) {
  276. curBlockArea = new Block();
  277. // set traits
  278. TraitSetter.addBorders(curBlockArea, borderProps);
  279. TraitSetter.addBackground(curBlockArea, backgroundProps);
  280. // Set up dimensions
  281. // Must get dimensions from parent area
  282. Area parentArea = parentLM.getParentArea(curBlockArea);
  283. int referenceIPD = parentArea.getIPD();
  284. curBlockArea.setIPD(referenceIPD);
  285. curBlockArea.setWidth(referenceIPD);
  286. // Get reference IPD from parentArea
  287. setCurrentArea(curBlockArea); // ??? for generic operations
  288. }
  289. return curBlockArea;
  290. }
  291. public void addChild(Area childArea) {
  292. if (curBlockArea != null) {
  293. if (childArea instanceof LineArea) {
  294. curBlockArea.addLineArea((LineArea) childArea);
  295. } else {
  296. curBlockArea.addBlock((Block) childArea);
  297. }
  298. }
  299. }
  300. public void resetPosition(Position resetPos) {
  301. if (resetPos == null) {
  302. reset(null);
  303. childBreaks.clear();
  304. iStartPos = 0;
  305. } else {
  306. //reset(resetPos);
  307. LayoutManager lm = resetPos.getLM();
  308. }
  309. }
  310. }