Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

BodyAreaContainer.java 14KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444
  1. /*-- $Id$ --
  2. ============================================================================
  3. The Apache Software License, Version 1.1
  4. ============================================================================
  5. Copyright (C) 1999 The Apache Software Foundation. All rights reserved.
  6. Redistribution and use in source and binary forms, with or without modifica-
  7. tion, are permitted provided that the following conditions are met:
  8. 1. Redistributions of source code must retain the above copyright notice,
  9. this list of conditions and the following disclaimer.
  10. 2. Redistributions in binary form must reproduce the above copyright notice,
  11. this list of conditions and the following disclaimer in the documentation
  12. and/or other materials provided with the distribution.
  13. 3. The end-user documentation included with the redistribution, if any, must
  14. include the following acknowledgment: "This product includes software
  15. developed by the Apache Software Foundation (http://www.apache.org/)."
  16. Alternately, this acknowledgment may appear in the software itself, if
  17. and wherever such third-party acknowledgments normally appear.
  18. 4. The names "Fop" and "Apache Software Foundation" must not be used to
  19. endorse or promote products derived from this software without prior
  20. written permission. For written permission, please contact
  21. apache@apache.org.
  22. 5. Products derived from this software may not be called "Apache", nor may
  23. "Apache" appear in their name, without prior written permission of the
  24. Apache Software Foundation.
  25. THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES,
  26. INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
  27. FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  28. APACHE SOFTWARE FOUNDATION OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
  29. INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLU-
  30. DING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
  31. OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
  32. ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  33. (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  34. THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  35. This software consists of voluntary contributions made by many individuals
  36. on behalf of the Apache Software Foundation and was originally created by
  37. James Tauber <jtauber@jtauber.com>. For more information on the Apache
  38. Software Foundation, please see <http://www.apache.org/>.
  39. */
  40. package org.apache.fop.layout;
  41. // FOP
  42. import org.apache.fop.render.Renderer;
  43. import org.apache.fop.fo.properties.*;
  44. import org.apache.fop.datatypes.IDReferences;
  45. import org.apache.fop.apps.FOPException;
  46. import org.apache.fop.fo.FObj;
  47. import org.apache.fop.fo.flow.Block;
  48. import org.apache.fop.fo.flow.BlockContainer;
  49. // Java
  50. import java.util.Vector;
  51. import java.util.Enumeration;
  52. public class BodyAreaContainer extends Area {
  53. // dimensions for the 'region-reference-area'
  54. private int xPosition; // should be able to take value 'left' and 'right' too
  55. private int yPosition; // should be able to take value 'top' and 'bottom' too
  56. private int position;
  57. // the column-count and column-gap
  58. private int columnCount;
  59. private int columnGap;
  60. // the 3 primary reference areas
  61. private AreaContainer mainReferenceArea;
  62. private AreaContainer beforeFloatReferenceArea;
  63. private AreaContainer footnoteReferenceArea;
  64. // current heights
  65. private int mainRefAreaHeight;
  66. private int beforeFloatRefAreaHeight;
  67. private int footnoteRefAreaHeight;
  68. // reference area yPositions
  69. private int mainYPosition;
  70. private int beforeFloatYPosition;
  71. private int footnoteYPosition;
  72. // the start FO in case of rollback
  73. private FObj startFO;
  74. private boolean isNewSpanArea;
  75. // keeps track of footnote state for multiple layouts
  76. private int footnoteState = 0;
  77. public BodyAreaContainer(FontState fontState, int xPosition, int yPosition,
  78. int allocationWidth, int maxHeight, int position,
  79. int columnCount, int columnGap) {
  80. super(fontState, allocationWidth, maxHeight);
  81. this.xPosition = xPosition;
  82. this.yPosition = yPosition;
  83. this.position = position;
  84. this.columnCount = columnCount;
  85. this.columnGap = columnGap;
  86. // create the primary reference areas
  87. beforeFloatRefAreaHeight = 0;
  88. footnoteRefAreaHeight = 0;
  89. mainRefAreaHeight = maxHeight - beforeFloatRefAreaHeight - footnoteRefAreaHeight;
  90. beforeFloatReferenceArea = new AreaContainer(fontState, xPosition, yPosition,
  91. allocationWidth, beforeFloatRefAreaHeight, Position.ABSOLUTE);
  92. this.addChild(beforeFloatReferenceArea);
  93. mainReferenceArea = new AreaContainer(fontState, xPosition, yPosition,
  94. allocationWidth, mainRefAreaHeight, Position.ABSOLUTE);
  95. this.addChild(mainReferenceArea);
  96. int footnoteRefAreaYPosition = yPosition - mainRefAreaHeight;
  97. footnoteReferenceArea = new AreaContainer(fontState, xPosition, footnoteRefAreaYPosition,
  98. allocationWidth, footnoteRefAreaHeight, Position.ABSOLUTE);
  99. this.addChild(footnoteReferenceArea);
  100. // all padding and border-width must be 0
  101. //setBorderAndPadding(new BorderAndPadding());
  102. // setPadding(0, 0, 0, 0);
  103. // setBorderWidth(0, 0, 0, 0);
  104. }
  105. public void render(Renderer renderer) {
  106. renderer.renderBodyAreaContainer(this);
  107. }
  108. public int getPosition() {
  109. return position;
  110. }
  111. public int getXPosition() {
  112. return xPosition + getPaddingLeft() + getBorderLeftWidth();
  113. }
  114. public void setXPosition(int value)
  115. {
  116. xPosition=value;
  117. }
  118. public int getYPosition() {
  119. return yPosition + getPaddingTop() + getBorderTopWidth();
  120. }
  121. public void setYPosition(int value)
  122. {
  123. yPosition=value;
  124. }
  125. public AreaContainer getMainReferenceArea()
  126. {
  127. return mainReferenceArea;
  128. }
  129. public AreaContainer getBeforeFloatReferenceArea()
  130. {
  131. return beforeFloatReferenceArea;
  132. }
  133. public AreaContainer getFootnoteReferenceArea()
  134. {
  135. return footnoteReferenceArea;
  136. }
  137. public void setIDReferences(IDReferences idReferences) {
  138. mainReferenceArea.setIDReferences(idReferences);
  139. }
  140. public IDReferences getIDReferences() {
  141. return mainReferenceArea.getIDReferences();
  142. }
  143. /**
  144. * Depending on the column-count of the next FO, determine whether
  145. * a new span area needs to be constructed or not, and return the
  146. * appropriate ColumnArea.
  147. * The next cut of this method should also inspect the FO to see
  148. * whether the area to be returned ought not to be the footnote
  149. * or before-float reference area.
  150. * @param fo The next formatting object
  151. * @returns the next column area (possibly the current one)
  152. */
  153. private static int counter = 0;
  154. public AreaContainer getNextArea(FObj fo)
  155. throws FOPException
  156. {
  157. isNewSpanArea = false;
  158. int span = Span.NONE;
  159. if (fo instanceof Block)
  160. span = ((Block)fo).getSpan();
  161. else if (fo instanceof BlockContainer)
  162. span = ((BlockContainer)fo).getSpan();
  163. if (this.mainReferenceArea.getChildren().isEmpty())
  164. {
  165. if (span == Span.ALL)
  166. return addSpanArea(1);
  167. else
  168. return addSpanArea(columnCount);
  169. }
  170. Vector spanAreas = this.mainReferenceArea.getChildren();
  171. SpanArea spanArea = (SpanArea)spanAreas.elementAt(spanAreas.size()-1);
  172. if ((span == Span.ALL) && (spanArea.getColumnCount() == 1))
  173. {
  174. // return the single column area in the same span area
  175. return spanArea.getCurrentColumnArea();
  176. }
  177. else if ((span == Span.NONE) && (spanArea.getColumnCount() == columnCount))
  178. {
  179. // return the current column area in the same span area
  180. return spanArea.getCurrentColumnArea();
  181. }
  182. else if (span == Span.ALL)
  183. {
  184. // create new span area with one column; return column area
  185. return addSpanArea(1);
  186. }
  187. else if (span == Span.NONE)
  188. {
  189. // create new span area with multiple columns; return first column area
  190. return addSpanArea(columnCount);
  191. }
  192. else
  193. {
  194. throw new FOPException("BodyAreaContainer::getNextArea(): Span attribute messed up");
  195. }
  196. }
  197. /**
  198. * Add a new span area with specified number of column areas.
  199. * @param numColumns The number of column areas
  200. * @returns AreaContainer The next column area
  201. */
  202. private AreaContainer addSpanArea( int numColumns )
  203. {
  204. resetHeights();
  205. // create span area and child column-areas, using whatever
  206. // height remains after existing span areas (in the main
  207. // reference area).
  208. int spanAreaYPosition = getYPosition() - this.mainReferenceArea.getContentHeight();
  209. SpanArea spanArea = new SpanArea(fontState,
  210. getXPosition(), spanAreaYPosition,
  211. allocationWidth, getRemainingHeight(),
  212. numColumns, columnGap);
  213. this.mainReferenceArea.addChild(spanArea);
  214. spanArea.setPage(this.getPage());
  215. this.isNewSpanArea = true;
  216. return spanArea.getCurrentColumnArea();
  217. }
  218. /**
  219. * This almost does what getNewArea() does, without actually
  220. * returning an area. These 2 methods can be reworked.
  221. * @param fo The next formatting object
  222. * @returns boolean True if we need to balance.
  223. */
  224. public boolean isBalancingRequired(FObj fo)
  225. {
  226. if (this.mainReferenceArea.getChildren().isEmpty())
  227. return false;
  228. Vector spanAreas = this.mainReferenceArea.getChildren();
  229. SpanArea spanArea = (SpanArea)spanAreas.elementAt(spanAreas.size()-1);
  230. if (spanArea.isBalanced())
  231. return false;
  232. int span = Span.NONE;
  233. if (fo instanceof Block)
  234. span = ((Block)fo).getSpan();
  235. else if (fo instanceof BlockContainer)
  236. span = ((BlockContainer)fo).getSpan();
  237. if ((span == Span.ALL) && (spanArea.getColumnCount() == 1))
  238. return false;
  239. else if ((span == Span.NONE) && (spanArea.getColumnCount() == columnCount))
  240. return false;
  241. else if (span == Span.ALL)
  242. return true;
  243. else if (span == Span.NONE)
  244. return false;
  245. else
  246. return false;
  247. }
  248. /**
  249. * This is where the balancing algorithm lives, or gets called.
  250. * Right now it's primitive: get the total content height in all
  251. * columns, divide by the column count, and add a heuristic
  252. * safety factor.
  253. * Then the previous (unbalanced) span area is removed, and a new
  254. * one added with the computed max height.
  255. */
  256. public void resetSpanArea()
  257. {
  258. Vector spanAreas = this.mainReferenceArea.getChildren();
  259. SpanArea spanArea = (SpanArea)spanAreas.elementAt(spanAreas.size()-1);
  260. if (!spanArea.isBalanced())
  261. {
  262. // span area maintains a record of the total height of
  263. // laid-out content in the previous (first) attempt
  264. int newHeight = spanArea.getTotalContentHeight() / spanArea.getColumnCount();
  265. newHeight += 2*15600; // ???
  266. this.mainReferenceArea.removeChild(spanArea);
  267. resetHeights();
  268. SpanArea newSpanArea = new SpanArea(fontState,
  269. getXPosition(), spanArea.getYPosition(),
  270. allocationWidth, newHeight,
  271. spanArea.getColumnCount(), columnGap);
  272. this.mainReferenceArea.addChild(newSpanArea);
  273. newSpanArea.setPage(this.getPage());
  274. newSpanArea.setIsBalanced();
  275. this.isNewSpanArea = true;
  276. }
  277. else
  278. {
  279. throw new IllegalStateException("Trying to balance balanced area");
  280. }
  281. }
  282. /**
  283. * Determine remaining height for new span area. Needs to be
  284. * modified for footnote and before-float reference areas when
  285. * those are supported.
  286. * @returns int The remaining available height in millipoints.
  287. */
  288. public int getRemainingHeight()
  289. {
  290. return this.mainReferenceArea.getMaxHeight() -
  291. this.mainReferenceArea.getContentHeight();
  292. }
  293. /**
  294. * Used by resetSpanArea() and addSpanArea() to adjust the main
  295. * reference area height before creating a new span.
  296. */
  297. private void resetHeights()
  298. {
  299. int totalHeight = 0;
  300. for (Enumeration e = this.mainReferenceArea.getChildren().elements(); e.hasMoreElements(); )
  301. {
  302. SpanArea spanArea = (SpanArea)e.nextElement();
  303. int spanContentHeight = spanArea.getMaxContentHeight();
  304. int spanMaxHeight = spanArea.getMaxHeight();
  305. totalHeight += (spanContentHeight < spanMaxHeight) ? spanContentHeight: spanMaxHeight;
  306. }
  307. this.mainReferenceArea.setHeight(totalHeight);
  308. }
  309. /**
  310. * Used in Flow when layout returns incomplete.
  311. * @returns boolean Is this the last column in this span?
  312. */
  313. public boolean isLastColumn()
  314. {
  315. Vector spanAreas = this.mainReferenceArea.getChildren();
  316. SpanArea spanArea = (SpanArea)spanAreas.elementAt(spanAreas.size()-1);
  317. return spanArea.isLastColumn();
  318. }
  319. /**
  320. * This variable is unset by getNextArea(), is set by addSpanArea(),
  321. * and <i>may</i> be set by resetSpanArea().
  322. * @returns boolean Is the span area new or not?
  323. */
  324. public boolean isNewSpanArea()
  325. {
  326. return isNewSpanArea;
  327. }
  328. public AreaContainer getCurrentColumnArea()
  329. {
  330. Vector spanAreas = this.mainReferenceArea.getChildren();
  331. SpanArea spanArea = (SpanArea)spanAreas.elementAt(spanAreas.size()-1);
  332. return spanArea.getCurrentColumnArea();
  333. }
  334. public int getFootnoteState()
  335. {
  336. return footnoteState;
  337. }
  338. public boolean needsFootnoteAdjusting()
  339. {
  340. footnoteYPosition = footnoteReferenceArea.getYPosition();
  341. switch(footnoteState) {
  342. case 0:
  343. resetHeights();
  344. if(footnoteReferenceArea.getHeight() > 0
  345. && mainYPosition + mainReferenceArea.getHeight() > footnoteYPosition) {
  346. return true;
  347. }
  348. case 1:
  349. break;
  350. }
  351. return false;
  352. }
  353. public void adjustFootnoteArea()
  354. {
  355. footnoteState++;
  356. if(footnoteState == 1) {
  357. mainReferenceArea.setMaxHeight(footnoteReferenceArea.getYPosition() - mainYPosition);
  358. footnoteYPosition = footnoteReferenceArea.getYPosition();
  359. footnoteReferenceArea.setMaxHeight(footnoteReferenceArea.getHeight());
  360. Vector childs = footnoteReferenceArea.getChildren();
  361. for(Enumeration en = childs.elements(); en.hasMoreElements(); ) {
  362. Object obj = en.nextElement();
  363. if(obj instanceof Area) {
  364. Area childArea = (Area)obj;
  365. footnoteReferenceArea.removeChild(childArea);
  366. }
  367. }
  368. getPage().setPendingFootnotes(null);
  369. }
  370. }
  371. protected static void resetMaxHeight(Area ar, int change) {
  372. ar.setMaxHeight(change);
  373. Vector childs = ar.getChildren();
  374. for(Enumeration en = childs.elements(); en.hasMoreElements(); ) {
  375. Object obj = en.nextElement();
  376. if(obj instanceof Area) {
  377. Area childArea = (Area)obj;
  378. resetMaxHeight(childArea, change);
  379. }
  380. }
  381. }
  382. }