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.

BlockContainerLayoutManager.java 13KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346
  1. /*
  2. * $Id: BlockContainerLayoutManager.java,v 1.13 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.List;
  53. import java.awt.geom.Rectangle2D;
  54. import org.apache.fop.area.Area;
  55. import org.apache.fop.area.BlockViewport;
  56. import org.apache.fop.area.Block;
  57. import org.apache.fop.fo.PropertyManager;
  58. import org.apache.fop.fo.properties.CommonAbsolutePosition;
  59. import org.apache.fop.fo.properties.AbsolutePosition;
  60. import org.apache.fop.fo.properties.Overflow;
  61. import org.apache.fop.fo.PropertyList;
  62. import org.apache.fop.area.CTM;
  63. import org.apache.fop.datatypes.FODimension;
  64. import org.apache.fop.traits.MinOptMax;
  65. /**
  66. * LayoutManager for a block FO.
  67. */
  68. public class BlockContainerLayoutManager extends BlockStackingLayoutManager {
  69. private BlockViewport viewportBlockArea;
  70. private Block curBlockArea;
  71. private List childBreaks = new java.util.ArrayList();
  72. private CommonAbsolutePosition abProps;
  73. private FODimension relDims;
  74. private CTM absoluteCTM;
  75. private boolean clip = false;
  76. private int overflow;
  77. private PropertyManager propManager;
  78. /**
  79. * Create a new block container layout manager.
  80. */
  81. public BlockContainerLayoutManager() {
  82. }
  83. public void setOverflow(int of) {
  84. overflow = of;
  85. }
  86. protected void initProperties(PropertyManager pm) {
  87. propManager = pm;
  88. abProps = pm.getAbsolutePositionProps();
  89. if (abProps.absolutePosition == AbsolutePosition.ABSOLUTE) {
  90. Rectangle2D rect = new Rectangle2D.Double(abProps.left,
  91. abProps.top, abProps.right - abProps.left,
  92. abProps.bottom - abProps.top);
  93. relDims = new FODimension(0, 0);
  94. absoluteCTM = CTM.getCTMandRelDims(pm.getAbsRefOrient(),
  95. pm.getWritingMode(), rect, relDims);
  96. }
  97. }
  98. protected int getRotatedIPD() {
  99. PropertyList props = propManager.getProperties();
  100. int height = props.get("height").getLength().getValue();
  101. height = props.get("inline-progression-dimension.optimum").getLength().getValue();
  102. return height;
  103. }
  104. public BreakPoss getNextBreakPoss(LayoutContext context) {
  105. if (abProps.absolutePosition == AbsolutePosition.ABSOLUTE) {
  106. return getAbsoluteBreakPoss(context);
  107. }
  108. Rectangle2D rect = new Rectangle2D.Double(0, 0, context.getRefIPD(),
  109. context.getStackLimit().opt);
  110. relDims = new FODimension(0, 0);
  111. absoluteCTM = CTM.getCTMandRelDims(propManager.getAbsRefOrient(),
  112. propManager.getWritingMode(), rect, relDims);
  113. double[] vals = absoluteCTM.toArray();
  114. MinOptMax stackLimit;
  115. int ipd = context.getRefIPD();
  116. boolean rotated = vals[0] == 0.0;
  117. if (rotated) {
  118. // rotated 90 degrees
  119. stackLimit = new MinOptMax(1000000);
  120. ipd = getRotatedIPD();
  121. absoluteCTM = new CTM(vals[0], vals[1], vals[2], vals[3], 0, 0);
  122. } else {
  123. if (vals[0] == -1.0) {
  124. absoluteCTM = new CTM(vals[0], vals[1], vals[2], vals[3], 0, 0);
  125. }
  126. stackLimit = context.getStackLimit();
  127. }
  128. LayoutProcessor curLM ; // currently active LM
  129. MinOptMax stackSize = new MinOptMax();
  130. // if starting add space before
  131. // stackSize.add(spaceBefore);
  132. BreakPoss lastPos = null;
  133. while ((curLM = getChildLM()) != null) {
  134. // Make break positions and return blocks!
  135. // Set up a LayoutContext
  136. BreakPoss bp;
  137. LayoutContext childLC = new LayoutContext(0);
  138. childLC.setStackLimit(
  139. MinOptMax.subtract(stackLimit,
  140. stackSize));
  141. childLC.setRefIPD(ipd);
  142. while (!curLM.isFinished()) {
  143. if ((bp = curLM.getNextBreakPoss(childLC)) != null) {
  144. stackSize.add(bp.getStackingSize());
  145. if (stackSize.opt > stackLimit.max) {
  146. // reset to last break
  147. if (lastPos != null) {
  148. reset(lastPos.getPosition());
  149. } else {
  150. curLM.resetPosition(null);
  151. }
  152. break;
  153. }
  154. lastPos = bp;
  155. childBreaks.add(bp);
  156. childLC.setStackLimit(MinOptMax.subtract(
  157. stackLimit, stackSize));
  158. }
  159. }
  160. if (!rotated) {
  161. BreakPoss breakPoss;
  162. breakPoss = new BreakPoss(new LeafPosition(this,
  163. childBreaks.size() - 1));
  164. breakPoss.setStackingSize(stackSize);
  165. return breakPoss;
  166. }
  167. }
  168. setFinished(true);
  169. if (rotated) {
  170. BreakPoss breakPoss;
  171. breakPoss = new BreakPoss(new LeafPosition(this,
  172. childBreaks.size() - 1));
  173. breakPoss.setStackingSize(new MinOptMax(ipd));
  174. return breakPoss;
  175. }
  176. return null;
  177. }
  178. public BreakPoss getAbsoluteBreakPoss(LayoutContext context) {
  179. LayoutProcessor curLM ; // currently active LM
  180. MinOptMax stackSize = new MinOptMax();
  181. int ipd = relDims.ipd;
  182. while ((curLM = getChildLM()) != null) {
  183. // Make break positions and return blocks!
  184. // Set up a LayoutContext
  185. BreakPoss bp;
  186. LayoutContext childLC = new LayoutContext(0);
  187. childLC.setStackLimit(new MinOptMax(1000000));
  188. childLC.setRefIPD(ipd);
  189. while (!curLM.isFinished()) {
  190. if ((bp = curLM.getNextBreakPoss(childLC)) != null) {
  191. stackSize.add(bp.getStackingSize());
  192. childBreaks.add(bp);
  193. }
  194. }
  195. }
  196. setFinished(true);
  197. BreakPoss breakPoss = new BreakPoss(
  198. new LeafPosition(this, childBreaks.size() - 1));
  199. // absolutely positioned areas do not contribute
  200. // to the normal stacking
  201. breakPoss.setStackingSize(new MinOptMax(0));
  202. if (stackSize.opt > relDims.bpd) {
  203. if (overflow == Overflow.HIDDEN) {
  204. clip = true;
  205. } else if (overflow == Overflow.ERROR_IF_OVERFLOW) {
  206. getLogger().error("contents overflows block-container viewport: clipping");
  207. clip = true;
  208. }
  209. }
  210. return breakPoss;
  211. }
  212. public void addAreas(PositionIterator parentIter,
  213. LayoutContext layoutContext) {
  214. getParentArea(null);
  215. addID();
  216. addMarkers(true, true);
  217. LayoutProcessor childLM;
  218. int iStartPos = 0;
  219. LayoutContext lc = new LayoutContext(0);
  220. while (parentIter.hasNext()) {
  221. LeafPosition lfp = (LeafPosition) parentIter.next();
  222. // Add the block areas to Area
  223. PositionIterator breakPosIter =
  224. new BreakPossPosIter(childBreaks, iStartPos,
  225. lfp.getLeafPos() + 1);
  226. iStartPos = lfp.getLeafPos() + 1;
  227. while ((childLM = breakPosIter.getNextChildLM()) != null) {
  228. childLM.addAreas(breakPosIter, lc);
  229. }
  230. }
  231. flush();
  232. addMarkers(true, true);
  233. childBreaks.clear();
  234. viewportBlockArea = null;
  235. curBlockArea = null;
  236. }
  237. /**
  238. * Get the parent area for children of this block container.
  239. * This returns the current block container area
  240. * and creates it if required.
  241. *
  242. * @see org.apache.fop.layoutmgr.LayoutProcessor#getParentArea(Area)
  243. */
  244. public Area getParentArea(Area childArea) {
  245. if (curBlockArea == null) {
  246. viewportBlockArea = new BlockViewport();
  247. if (abProps.absolutePosition == AbsolutePosition.ABSOLUTE) {
  248. viewportBlockArea.setXOffset(abProps.left);
  249. viewportBlockArea.setYOffset(abProps.top);
  250. viewportBlockArea.setWidth(abProps.right - abProps.left);
  251. viewportBlockArea.setHeight(abProps.bottom - abProps.top);
  252. viewportBlockArea.setCTM(absoluteCTM);
  253. viewportBlockArea.setClip(clip);
  254. } else {
  255. double[] vals = absoluteCTM.toArray();
  256. boolean rotated = vals[0] == 0.0;
  257. if (rotated) {
  258. viewportBlockArea.setWidth(relDims.bpd);
  259. viewportBlockArea.setHeight(getRotatedIPD());
  260. viewportBlockArea.setCTM(absoluteCTM);
  261. viewportBlockArea.setClip(clip);
  262. } else if (vals[0] == -1.0) {
  263. // need to set bpd to actual size for rotation
  264. // and stacking
  265. viewportBlockArea.setWidth(relDims.ipd);
  266. viewportBlockArea.setWidth(relDims.bpd);
  267. viewportBlockArea.setCTM(absoluteCTM);
  268. viewportBlockArea.setClip(clip);
  269. }
  270. }
  271. curBlockArea = new Block();
  272. viewportBlockArea.addBlock(curBlockArea);
  273. if (abProps.absolutePosition == AbsolutePosition.ABSOLUTE) {
  274. viewportBlockArea.setPositioning(Block.ABSOLUTE);
  275. }
  276. // Set up dimensions
  277. // Must get dimensions from parent area
  278. Area parentArea = parentLM.getParentArea(curBlockArea);
  279. int referenceIPD = parentArea.getIPD();
  280. curBlockArea.setIPD(referenceIPD);
  281. // Get reference IPD from parentArea
  282. setCurrentArea(viewportBlockArea); // ??? for generic operations
  283. }
  284. return curBlockArea;
  285. }
  286. /**
  287. * Add the child to the block container.
  288. *
  289. * @see org.apache.fop.layoutmgr.LayoutProcessor#addChild(Area)
  290. */
  291. public void addChild(Area childArea) {
  292. if (curBlockArea != null) {
  293. curBlockArea.addBlock((Block) childArea);
  294. }
  295. }
  296. public void resetPosition(Position resetPos) {
  297. if (resetPos == null) {
  298. reset(null);
  299. }
  300. }
  301. }