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 14KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391
  1. /*
  2. * Copyright 1999-2004 The Apache Software Foundation.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. /* $Id$ */
  17. package org.apache.fop.layoutmgr;
  18. import java.util.List;
  19. import java.awt.geom.Rectangle2D;
  20. import org.apache.fop.area.Area;
  21. import org.apache.fop.area.BlockViewport;
  22. import org.apache.fop.area.Block;
  23. import org.apache.fop.fo.PropertyManager;
  24. import org.apache.fop.fo.properties.CommonAbsolutePosition;
  25. import org.apache.fop.fo.properties.CommonBorderAndPadding;
  26. import org.apache.fop.fo.properties.CommonMarginBlock;
  27. import org.apache.fop.fo.PropertyList;
  28. import org.apache.fop.area.CTM;
  29. import org.apache.fop.datatypes.FODimension;
  30. import org.apache.fop.datatypes.Length;
  31. import org.apache.fop.datatypes.PercentBase;
  32. import org.apache.fop.traits.MinOptMax;
  33. /**
  34. * LayoutManager for a block FO.
  35. */
  36. public class BlockContainerLayoutManager extends BlockStackingLayoutManager {
  37. private BlockViewport viewportBlockArea;
  38. private Block curBlockArea;
  39. private List childBreaks = new java.util.ArrayList();
  40. private CommonAbsolutePosition abProps;
  41. private CommonBorderAndPadding borderProps;
  42. private CommonMarginBlock marginProps;
  43. private FODimension relDims;
  44. private CTM absoluteCTM;
  45. private boolean clip = false;
  46. private int overflow;
  47. private PropertyManager propManager;
  48. private Length width;
  49. private Length height;
  50. // When viewport should grow with the content.
  51. private boolean autoHeight = true;
  52. /**
  53. * Create a new block container layout manager.
  54. */
  55. public BlockContainerLayoutManager() {
  56. }
  57. public void setOverflow(int of) {
  58. overflow = of;
  59. }
  60. protected void initProperties(PropertyManager pm) {
  61. propManager = pm;
  62. abProps = pm.getAbsolutePositionProps();
  63. if (abProps.absolutePosition == AbsolutePosition.ABSOLUTE) {
  64. Rectangle2D rect = new Rectangle2D.Double(abProps.left,
  65. abProps.top, abProps.right - abProps.left,
  66. abProps.bottom - abProps.top);
  67. relDims = new FODimension(0, 0);
  68. absoluteCTM = CTM.getCTMandRelDims(pm.getAbsRefOrient(),
  69. pm.getWritingMode(), rect, relDims);
  70. }
  71. marginProps = pm.getMarginProps();
  72. borderProps = pm.getBorderAndPadding();
  73. height = pm.getPropertyList().get(PR_BLOCK_PROGRESSION_DIMENSION | CP_OPTIMUM).getLength();
  74. width = pm.getPropertyList().get(PR_INLINE_PROGRESSION_DIMENSION | CP_OPTIMUM).getLength();
  75. }
  76. protected int getRotatedIPD() {
  77. PropertyList props = propManager.getPropertyList();
  78. int height = props.get(PR_HEIGHT).getLength().getValue();
  79. height = props.get(PR_INLINE_PROGRESSION_DIMENSION | CP_OPTIMUM).getLength().getValue();
  80. return height;
  81. }
  82. public BreakPoss getNextBreakPoss(LayoutContext context) {
  83. if (abProps.absolutePosition == AbsolutePosition.ABSOLUTE) {
  84. return getAbsoluteBreakPoss(context);
  85. }
  86. int bIndents = borderProps.getBPPaddingAndBorder(false);
  87. int iIndents = marginProps.startIndent + marginProps.endIndent;
  88. int ipd = context.getRefIPD();
  89. int bpd = context.getStackLimit().opt;
  90. if (!width.isAuto()) {
  91. ipd = width.getValue() + iIndents;
  92. }
  93. if (!height.isAuto()) {
  94. bpd = height.getValue() + bIndents;
  95. }
  96. Rectangle2D rect = new Rectangle2D.Double(0, 0, ipd, bpd);
  97. relDims = new FODimension(0, 0);
  98. absoluteCTM = CTM.getCTMandRelDims(propManager.getAbsRefOrient(),
  99. propManager.getWritingMode(), rect, relDims);
  100. double[] vals = absoluteCTM.toArray();
  101. ipd -= iIndents;
  102. MinOptMax stackLimit;
  103. boolean rotated = vals[0] == 0.0;
  104. if (rotated) {
  105. // rotated 90 degrees
  106. if (relDims.ipd > context.getRefIPD()) {
  107. relDims.ipd = context.getRefIPD();
  108. }
  109. stackLimit = new MinOptMax(relDims.ipd);
  110. if (width.isAuto()) {
  111. relDims.bpd = context.getStackLimit().opt;
  112. }
  113. absoluteCTM = new CTM(vals[0], vals[1], vals[2], vals[3], 0, 0);
  114. } else {
  115. if (vals[0] == -1.0) {
  116. absoluteCTM = new CTM(vals[0], vals[1], vals[2], vals[3], 0, 0);
  117. }
  118. stackLimit = context.getStackLimit();
  119. }
  120. LayoutProcessor curLM ; // currently active LM
  121. MinOptMax stackSize = new MinOptMax();
  122. // if starting add space before
  123. // stackSize.add(spaceBefore);
  124. BreakPoss lastPos = null;
  125. fobj.setLayoutDimension(PercentBase.BLOCK_IPD, ipd);
  126. fobj.setLayoutDimension(PercentBase.BLOCK_BPD, bpd - bIndents);
  127. fobj.setLayoutDimension(PercentBase.REFERENCE_AREA_IPD, ipd);
  128. fobj.setLayoutDimension(PercentBase.REFERENCE_AREA_BPD, bpd - bIndents);
  129. while ((curLM = getChildLM()) != null) {
  130. // Make break positions and return blocks!
  131. // Set up a LayoutContext
  132. BreakPoss bp;
  133. LayoutContext childLC = new LayoutContext(0);
  134. childLC.setStackLimit(
  135. MinOptMax.subtract(stackLimit,
  136. stackSize));
  137. childLC.setRefIPD(ipd);
  138. boolean over = false;
  139. while (!curLM.isFinished()) {
  140. if ((bp = curLM.getNextBreakPoss(childLC)) != null) {
  141. stackSize.add(bp.getStackingSize());
  142. if (stackSize.opt > stackLimit.max) {
  143. // reset to last break
  144. if (lastPos != null) {
  145. reset(lastPos.getPosition());
  146. } else {
  147. curLM.resetPosition(null);
  148. }
  149. over = true;
  150. break;
  151. }
  152. lastPos = bp;
  153. childBreaks.add(bp);
  154. if (bp.nextBreakOverflows()) {
  155. over = true;
  156. break;
  157. }
  158. childLC.setStackLimit(MinOptMax.subtract(
  159. stackLimit, stackSize));
  160. }
  161. }
  162. if (!rotated) {
  163. BreakPoss breakPoss;
  164. breakPoss = new BreakPoss(new LeafPosition(this,
  165. childBreaks.size() - 1));
  166. breakPoss.setStackingSize(stackSize);
  167. if (over) {
  168. breakPoss.setFlag(BreakPoss.NEXT_OVERFLOWS, true);
  169. }
  170. return breakPoss;
  171. }
  172. }
  173. setFinished(true);
  174. if (rotated) {
  175. BreakPoss breakPoss;
  176. breakPoss = new BreakPoss(new LeafPosition(this,
  177. childBreaks.size() - 1));
  178. breakPoss.setStackingSize(new MinOptMax(ipd));
  179. return breakPoss;
  180. }
  181. return null;
  182. }
  183. public BreakPoss getAbsoluteBreakPoss(LayoutContext context) {
  184. LayoutProcessor curLM ; // currently active LM
  185. MinOptMax stackSize = new MinOptMax();
  186. int ipd = relDims.ipd;
  187. while ((curLM = getChildLM()) != null) {
  188. // Make break positions and return blocks!
  189. // Set up a LayoutContext
  190. BreakPoss bp;
  191. LayoutContext childLC = new LayoutContext(0);
  192. childLC.setStackLimit(new MinOptMax(1000000));
  193. childLC.setRefIPD(ipd);
  194. while (!curLM.isFinished()) {
  195. if ((bp = curLM.getNextBreakPoss(childLC)) != null) {
  196. stackSize.add(bp.getStackingSize());
  197. childBreaks.add(bp);
  198. }
  199. }
  200. }
  201. setFinished(true);
  202. BreakPoss breakPoss = new BreakPoss(
  203. new LeafPosition(this, childBreaks.size() - 1));
  204. // absolutely positioned areas do not contribute
  205. // to the normal stacking
  206. breakPoss.setStackingSize(new MinOptMax(0));
  207. if (stackSize.opt > relDims.bpd) {
  208. if (overflow == Overflow.HIDDEN) {
  209. clip = true;
  210. } else if (overflow == Overflow.ERROR_IF_OVERFLOW) {
  211. getLogger().error("contents overflows block-container viewport: clipping");
  212. clip = true;
  213. }
  214. }
  215. return breakPoss;
  216. }
  217. public void addAreas(PositionIterator parentIter,
  218. LayoutContext layoutContext) {
  219. getParentArea(null);
  220. addID();
  221. addMarkers(true, true);
  222. LayoutProcessor childLM;
  223. int iStartPos = 0;
  224. LayoutContext lc = new LayoutContext(0);
  225. while (parentIter.hasNext()) {
  226. LeafPosition lfp = (LeafPosition) parentIter.next();
  227. // Add the block areas to Area
  228. PositionIterator breakPosIter =
  229. new BreakPossPosIter(childBreaks, iStartPos,
  230. lfp.getLeafPos() + 1);
  231. iStartPos = lfp.getLeafPos() + 1;
  232. while ((childLM = breakPosIter.getNextChildLM()) != null) {
  233. childLM.addAreas(breakPosIter, lc);
  234. }
  235. }
  236. flush();
  237. addMarkers(true, true);
  238. childBreaks.clear();
  239. viewportBlockArea = null;
  240. curBlockArea = null;
  241. }
  242. /**
  243. * Get the parent area for children of this block container.
  244. * This returns the current block container area
  245. * and creates it if required.
  246. *
  247. * @see org.apache.fop.layoutmgr.LayoutProcessor#getParentArea(Area)
  248. */
  249. public Area getParentArea(Area childArea) {
  250. if (curBlockArea == null) {
  251. viewportBlockArea = new BlockViewport();
  252. TraitSetter.addBorders(viewportBlockArea,
  253. propManager.getBorderAndPadding());
  254. TraitSetter.addBackground(viewportBlockArea,
  255. propManager.getBackgroundProps());
  256. if (abProps.absolutePosition == AbsolutePosition.ABSOLUTE) {
  257. viewportBlockArea.setXOffset(abProps.left);
  258. viewportBlockArea.setYOffset(abProps.top);
  259. viewportBlockArea.setWidth(abProps.right - abProps.left);
  260. viewportBlockArea.setHeight(abProps.bottom - abProps.top);
  261. viewportBlockArea.setCTM(absoluteCTM);
  262. viewportBlockArea.setClip(clip);
  263. autoHeight = false;
  264. } else {
  265. double[] vals = absoluteCTM.toArray();
  266. boolean rotated = vals[0] == 0.0;
  267. if (rotated) {
  268. viewportBlockArea.setWidth(relDims.ipd);
  269. viewportBlockArea.setHeight(relDims.bpd);
  270. viewportBlockArea.setCTM(absoluteCTM);
  271. viewportBlockArea.setClip(clip);
  272. autoHeight = false;
  273. } else if (vals[0] == -1.0) {
  274. // need to set bpd to actual size for rotation
  275. // and stacking
  276. viewportBlockArea.setWidth(relDims.ipd);
  277. if (!height.isAuto()) {
  278. viewportBlockArea.setHeight(relDims.bpd);
  279. autoHeight = false;
  280. }
  281. viewportBlockArea.setCTM(absoluteCTM);
  282. viewportBlockArea.setClip(clip);
  283. } else {
  284. viewportBlockArea.setWidth(relDims.ipd);
  285. if (!height.isAuto()) {
  286. viewportBlockArea.setHeight(relDims.bpd);
  287. autoHeight = false;
  288. }
  289. }
  290. }
  291. curBlockArea = new Block();
  292. if (abProps.absolutePosition == AbsolutePosition.ABSOLUTE) {
  293. viewportBlockArea.setPositioning(Block.ABSOLUTE);
  294. }
  295. // Set up dimensions
  296. // Must get dimensions from parent area
  297. Area parentArea = parentLM.getParentArea(curBlockArea);
  298. //int referenceIPD = parentArea.getIPD();
  299. curBlockArea.setIPD(relDims.ipd);
  300. // Get reference IPD from parentArea
  301. setCurrentArea(viewportBlockArea); // ??? for generic operations
  302. }
  303. return curBlockArea;
  304. }
  305. /**
  306. * Add the child to the block container.
  307. *
  308. * @see org.apache.fop.layoutmgr.LayoutProcessor#addChild(Area)
  309. */
  310. public void addChild(Area childArea) {
  311. if (curBlockArea != null) {
  312. curBlockArea.addBlock((Block) childArea);
  313. }
  314. }
  315. public void resetPosition(Position resetPos) {
  316. if (resetPos == null) {
  317. reset(null);
  318. }
  319. }
  320. /*
  321. * Force current area to be added to parent area.
  322. */
  323. protected void flush() {
  324. viewportBlockArea.addBlock(curBlockArea, autoHeight);
  325. // Fake a 0 height for absolute positioned blocks.
  326. int height = viewportBlockArea.getHeight();
  327. if (viewportBlockArea.getPositioning() == Block.ABSOLUTE) {
  328. viewportBlockArea.setHeight(0);
  329. }
  330. super.flush();
  331. // Restore the right height.
  332. if (viewportBlockArea.getPositioning() == Block.ABSOLUTE) {
  333. viewportBlockArea.setHeight(height);
  334. }
  335. }
  336. }