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

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