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.

TableContentLayoutManager.java 20KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497
  1. /*
  2. * Licensed to the Apache Software Foundation (ASF) under one or more
  3. * contributor license agreements. See the NOTICE file distributed with
  4. * this work for additional information regarding copyright ownership.
  5. * The ASF licenses this file to You under the Apache License, Version 2.0
  6. * (the "License"); you may not use this file except in compliance with
  7. * the License. You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. */
  17. /* $Id$ */
  18. package org.apache.fop.layoutmgr.table;
  19. import java.util.Iterator;
  20. import java.util.LinkedList;
  21. import java.util.List;
  22. import java.util.Map;
  23. import org.apache.commons.logging.Log;
  24. import org.apache.commons.logging.LogFactory;
  25. import org.apache.fop.area.Block;
  26. import org.apache.fop.area.Trait;
  27. import org.apache.fop.datatypes.PercentBaseContext;
  28. import org.apache.fop.fo.Constants;
  29. import org.apache.fop.fo.FObj;
  30. import org.apache.fop.fo.flow.table.EffRow;
  31. import org.apache.fop.fo.flow.table.GridUnit;
  32. import org.apache.fop.fo.flow.table.Table;
  33. import org.apache.fop.fo.flow.table.TableBody;
  34. import org.apache.fop.fo.flow.table.TableRow;
  35. import org.apache.fop.layoutmgr.BreakElement;
  36. import org.apache.fop.layoutmgr.ElementListUtils;
  37. import org.apache.fop.layoutmgr.KnuthBox;
  38. import org.apache.fop.layoutmgr.KnuthPenalty;
  39. import org.apache.fop.layoutmgr.KnuthPossPosIter;
  40. import org.apache.fop.layoutmgr.LayoutContext;
  41. import org.apache.fop.layoutmgr.ListElement;
  42. import org.apache.fop.layoutmgr.Position;
  43. import org.apache.fop.layoutmgr.PositionIterator;
  44. import org.apache.fop.layoutmgr.TraitSetter;
  45. import org.apache.fop.layoutmgr.SpaceResolver.SpaceHandlingBreakPosition;
  46. /**
  47. * Layout manager for table contents, particularly managing the creation of combined element lists.
  48. */
  49. public class TableContentLayoutManager implements PercentBaseContext {
  50. /** Logger **/
  51. private static Log log = LogFactory.getLog(TableContentLayoutManager.class);
  52. private TableLayoutManager tableLM;
  53. private TableRowIterator bodyIter;
  54. private TableRowIterator headerIter;
  55. private TableRowIterator footerIter;
  56. private LinkedList headerList;
  57. private LinkedList footerList;
  58. private int headerNetHeight = 0;
  59. private int footerNetHeight = 0;
  60. private int startXOffset;
  61. private int usedBPD;
  62. private TableStepper stepper = new TableStepper(this);
  63. /**
  64. * Main constructor
  65. * @param parent Parent layout manager
  66. */
  67. TableContentLayoutManager(TableLayoutManager parent) {
  68. this.tableLM = parent;
  69. Table table = getTableLM().getTable();
  70. this.bodyIter = new TableRowIterator(table, TableRowIterator.BODY);
  71. if (table.getTableHeader() != null) {
  72. headerIter = new TableRowIterator(table, TableRowIterator.HEADER);
  73. }
  74. if (table.getTableFooter() != null) {
  75. footerIter = new TableRowIterator(table, TableRowIterator.FOOTER);
  76. }
  77. }
  78. /**
  79. * @return the table layout manager
  80. */
  81. TableLayoutManager getTableLM() {
  82. return this.tableLM;
  83. }
  84. /** @return true if the table uses the separate border model. */
  85. boolean isSeparateBorderModel() {
  86. return getTableLM().getTable().isSeparateBorderModel();
  87. }
  88. /**
  89. * @return the column setup of this table
  90. */
  91. ColumnSetup getColumns() {
  92. return getTableLM().getColumns();
  93. }
  94. /** @return the net header height */
  95. protected int getHeaderNetHeight() {
  96. return this.headerNetHeight;
  97. }
  98. /** @return the net footer height */
  99. protected int getFooterNetHeight() {
  100. return this.footerNetHeight;
  101. }
  102. /** @return the header element list */
  103. protected LinkedList getHeaderElements() {
  104. return this.headerList;
  105. }
  106. /** @return the footer element list */
  107. protected LinkedList getFooterElements() {
  108. return this.footerList;
  109. }
  110. /** @see org.apache.fop.layoutmgr.LayoutManager#getNextKnuthElements(LayoutContext, int) */
  111. public LinkedList getNextKnuthElements(LayoutContext context, int alignment) {
  112. if (log.isDebugEnabled()) {
  113. log.debug("==> Columns: " + getTableLM().getColumns());
  114. }
  115. KnuthBox headerAsFirst = null;
  116. KnuthBox headerAsSecondToLast = null;
  117. KnuthBox footerAsLast = null;
  118. if (headerIter != null && headerList == null) {
  119. this.headerList = getKnuthElementsForRowIterator(
  120. headerIter, context, alignment, TableRowIterator.HEADER);
  121. this.headerNetHeight
  122. = ElementListUtils.calcContentLength(this.headerList);
  123. if (log.isDebugEnabled()) {
  124. log.debug("==> Header: "
  125. + headerNetHeight + " - " + this.headerList);
  126. }
  127. TableHeaderFooterPosition pos = new TableHeaderFooterPosition(
  128. getTableLM(), true, this.headerList);
  129. KnuthBox box = new KnuthBox(headerNetHeight, pos, false);
  130. if (getTableLM().getTable().omitHeaderAtBreak()) {
  131. //We can simply add the table header at the start
  132. //of the whole list
  133. headerAsFirst = box;
  134. } else {
  135. headerAsSecondToLast = box;
  136. }
  137. }
  138. if (footerIter != null && footerList == null) {
  139. this.footerList = getKnuthElementsForRowIterator(
  140. footerIter, context, alignment, TableRowIterator.FOOTER);
  141. this.footerNetHeight
  142. = ElementListUtils.calcContentLength(this.footerList);
  143. if (log.isDebugEnabled()) {
  144. log.debug("==> Footer: "
  145. + footerNetHeight + " - " + this.footerList);
  146. }
  147. //We can simply add the table footer at the end of the whole list
  148. TableHeaderFooterPosition pos = new TableHeaderFooterPosition(
  149. getTableLM(), false, this.footerList);
  150. KnuthBox box = new KnuthBox(footerNetHeight, pos, false);
  151. footerAsLast = box;
  152. }
  153. LinkedList returnList = getKnuthElementsForRowIterator(
  154. bodyIter, context, alignment, TableRowIterator.BODY);
  155. if (headerAsFirst != null) {
  156. int insertionPoint = 0;
  157. if (returnList.size() > 0 && ((ListElement)returnList.getFirst()).isForcedBreak()) {
  158. insertionPoint++;
  159. }
  160. returnList.add(insertionPoint, headerAsFirst);
  161. } else if (headerAsSecondToLast != null) {
  162. int insertionPoint = returnList.size();
  163. if (returnList.size() > 0 && ((ListElement)returnList.getLast()).isForcedBreak()) {
  164. insertionPoint--;
  165. }
  166. returnList.add(insertionPoint, headerAsSecondToLast);
  167. }
  168. if (footerAsLast != null) {
  169. int insertionPoint = returnList.size();
  170. if (returnList.size() > 0 && ((ListElement)returnList.getLast()).isForcedBreak()) {
  171. insertionPoint--;
  172. }
  173. returnList.add(insertionPoint, footerAsLast);
  174. }
  175. return returnList;
  176. }
  177. /**
  178. * Creates Knuth elements by iterating over a TableRowIterator.
  179. * @param iter TableRowIterator instance to fetch rows from
  180. * @param context Active LayoutContext
  181. * @param alignment alignment indicator
  182. * @param bodyType Indicates what kind of body is being processed
  183. * (BODY, HEADER or FOOTER)
  184. * @return An element list
  185. */
  186. private LinkedList getKnuthElementsForRowIterator(TableRowIterator iter,
  187. LayoutContext context, int alignment, int bodyType) {
  188. LinkedList returnList = new LinkedList();
  189. EffRow[] rowGroup = null;
  190. int breakBetween = Constants.EN_AUTO;
  191. while ((rowGroup = iter.getNextRowGroup()) != null) {
  192. RowGroupLayoutManager rowGroupLM = new RowGroupLayoutManager(getTableLM(), rowGroup,
  193. stepper);
  194. if (breakBetween == Constants.EN_AUTO) {
  195. // TODO improve
  196. breakBetween = rowGroupLM.getBreakBefore();
  197. }
  198. if (breakBetween != Constants.EN_AUTO) {
  199. if (returnList.size() > 0) {
  200. BreakElement breakPoss = (BreakElement) returnList.getLast();
  201. breakPoss.setPenaltyValue(-KnuthPenalty.INFINITE);
  202. breakPoss.setBreakClass(breakBetween);
  203. } else {
  204. returnList.add(new BreakElement(new Position(tableLM),
  205. 0, -KnuthPenalty.INFINITE, breakBetween, context));
  206. }
  207. }
  208. returnList.addAll(rowGroupLM.getNextKnuthElements(context, alignment, bodyType));
  209. breakBetween = rowGroupLM.getBreakAfter();
  210. }
  211. // Break after the table's last row
  212. // TODO should eventually be handled at the table level
  213. if (breakBetween != Constants.EN_AUTO) {
  214. if (returnList.size() > 0 && ((ListElement) returnList.getLast()).isPenalty()) {
  215. // May be a glue if the unbroken height is greater than the broken heights
  216. BreakElement breakPoss = (BreakElement) returnList.getLast();
  217. breakPoss.setPenaltyValue(-KnuthPenalty.INFINITE);
  218. breakPoss.setBreakClass(breakBetween);
  219. } else {
  220. returnList.add(new BreakElement(new Position(tableLM),
  221. 0, -KnuthPenalty.INFINITE, breakBetween, context));
  222. }
  223. }
  224. if (returnList.size() > 0) {
  225. //Remove the last penalty produced by the combining algorithm (see TableStepper),
  226. //for the last step
  227. ListElement last = (ListElement)returnList.getLast();
  228. if (last.isPenalty() || last instanceof BreakElement) {
  229. if (!last.isForcedBreak()) {
  230. //Only remove if we don't signal a forced break
  231. returnList.removeLast();
  232. }
  233. }
  234. }
  235. //fox:widow-content-limit
  236. int widowContentLimit = getTableLM().getTable().getWidowContentLimit().getValue();
  237. if (widowContentLimit != 0 && bodyType == TableRowIterator.BODY) {
  238. ElementListUtils.removeLegalBreaks(returnList, widowContentLimit);
  239. }
  240. //fox:orphan-content-limit
  241. int orphanContentLimit = getTableLM().getTable().getOrphanContentLimit().getValue();
  242. if (orphanContentLimit != 0 && bodyType == TableRowIterator.BODY) {
  243. ElementListUtils.removeLegalBreaksFromEnd(returnList, orphanContentLimit);
  244. }
  245. return returnList;
  246. }
  247. /**
  248. * Retuns the X offset of the given grid unit.
  249. * @param gu the grid unit
  250. * @return the requested X offset
  251. */
  252. protected int getXOffsetOfGridUnit(GridUnit gu) {
  253. int col = gu.getStartCol();
  254. return startXOffset + getTableLM().getColumns().getXOffset(col + 1, getTableLM());
  255. }
  256. /**
  257. * Adds the areas generated by this layout manager to the area tree.
  258. * @param parentIter the position iterator
  259. * @param layoutContext the layout context for adding areas
  260. */
  261. void addAreas(PositionIterator parentIter, LayoutContext layoutContext) {
  262. this.usedBPD = 0;
  263. RowPainter painter = new RowPainter(this, layoutContext);
  264. List positions = new java.util.ArrayList();
  265. List headerElements = null;
  266. List footerElements = null;
  267. Position firstPos = null;
  268. Position lastPos = null;
  269. Position lastCheckPos = null;
  270. while (parentIter.hasNext()) {
  271. Position pos = (Position)parentIter.next();
  272. if (pos instanceof SpaceHandlingBreakPosition) {
  273. //This position has only been needed before addAreas was called, now we need the
  274. //original one created by the layout manager.
  275. pos = ((SpaceHandlingBreakPosition)pos).getOriginalBreakPosition();
  276. }
  277. if (pos == null) {
  278. continue;
  279. }
  280. if (firstPos == null) {
  281. firstPos = pos;
  282. }
  283. lastPos = pos;
  284. if (pos.getIndex() >= 0) {
  285. lastCheckPos = pos;
  286. }
  287. if (pos instanceof TableHeaderFooterPosition) {
  288. TableHeaderFooterPosition thfpos = (TableHeaderFooterPosition)pos;
  289. //these positions need to be unpacked
  290. if (thfpos.header) {
  291. //Positions for header will be added first
  292. headerElements = thfpos.nestedElements;
  293. } else {
  294. //Positions for footers are simply added at the end
  295. footerElements = thfpos.nestedElements;
  296. }
  297. } else if (pos instanceof TableHFPenaltyPosition) {
  298. //ignore for now, see special handling below if break is at a penalty
  299. //Only if the last position in this part/page us such a position it will be used
  300. } else {
  301. //leave order as is for the rest
  302. positions.add(pos);
  303. }
  304. }
  305. if (lastPos instanceof TableHFPenaltyPosition) {
  306. TableHFPenaltyPosition penaltyPos = (TableHFPenaltyPosition)lastPos;
  307. log.debug("Break at penalty!");
  308. if (penaltyPos.headerElements != null) {
  309. //Header positions for the penalty position are in the last element and need to
  310. //be handled first before all other TableContentPositions
  311. headerElements = penaltyPos.headerElements;
  312. }
  313. if (penaltyPos.footerElements != null) {
  314. footerElements = penaltyPos.footerElements;
  315. }
  316. }
  317. Map markers = getTableLM().getTable().getMarkers();
  318. if (markers != null) {
  319. getTableLM().getCurrentPV().addMarkers(markers,
  320. true, getTableLM().isFirst(firstPos), getTableLM().isLast(lastCheckPos));
  321. }
  322. if (headerElements != null) {
  323. //header positions for the last part are the second-to-last element and need to
  324. //be handled first before all other TableContentPositions
  325. PositionIterator nestedIter = new KnuthPossPosIter(headerElements);
  326. iterateAndPaintPositions(nestedIter, painter);
  327. }
  328. //Iterate over all steps
  329. Iterator posIter = positions.iterator();
  330. iterateAndPaintPositions(posIter, painter);
  331. if (footerElements != null) {
  332. //Positions for footers are simply added at the end
  333. PositionIterator nestedIter = new KnuthPossPosIter(footerElements);
  334. iterateAndPaintPositions(nestedIter, painter);
  335. }
  336. this.usedBPD += painter.getAccumulatedBPD();
  337. if (markers != null) {
  338. getTableLM().getCurrentPV().addMarkers(markers,
  339. false, getTableLM().isFirst(firstPos), getTableLM().isLast(lastCheckPos));
  340. }
  341. }
  342. /**
  343. * Iterates over a part of the table (header, footer, body) and paints the related
  344. * elements.
  345. *
  346. * @param iterator iterator over Position elements. Those positions correspond to the
  347. * elements of the table present on the current page
  348. * @param painter
  349. */
  350. private void iterateAndPaintPositions(Iterator iterator, RowPainter painter) {
  351. List lst = new java.util.ArrayList();
  352. boolean firstPos = false;
  353. TableBody body = null;
  354. while (iterator.hasNext()) {
  355. Position pos = (Position)iterator.next();
  356. if (pos instanceof TableContentPosition) {
  357. TableContentPosition tcpos = (TableContentPosition)pos;
  358. lst.add(tcpos);
  359. CellPart part = (CellPart)tcpos.cellParts.get(0);
  360. if (body == null) {
  361. body = part.pgu.getBody();
  362. }
  363. if (tcpos.getFlag(TableContentPosition.FIRST_IN_ROWGROUP)
  364. && tcpos.row.getFlag(EffRow.FIRST_IN_PART)) {
  365. firstPos = true;
  366. }
  367. if (tcpos.getFlag(TableContentPosition.LAST_IN_ROWGROUP)
  368. && tcpos.row.getFlag(EffRow.LAST_IN_PART)) {
  369. log.trace("LAST_IN_ROWGROUP + LAST_IN_PART");
  370. handleMarkersAndPositions(lst, body, firstPos, true, painter);
  371. //reset
  372. firstPos = false;
  373. body = null;
  374. lst.clear();
  375. }
  376. } else {
  377. if (log.isDebugEnabled()) {
  378. log.debug("Ignoring position: " + pos);
  379. }
  380. }
  381. }
  382. if (body != null) {
  383. // Entering this block means that the end of the current table-part hasn't
  384. // been reached (otherwise it would have been caught by the test above). So
  385. // lastPos is necessarily false
  386. handleMarkersAndPositions(lst, body, firstPos, false, painter);
  387. }
  388. painter.addAreasAndFlushRow(true);
  389. }
  390. private void handleMarkersAndPositions(List positions, TableBody body, boolean firstPos,
  391. boolean lastPos, RowPainter painter) {
  392. getTableLM().getCurrentPV().addMarkers(body.getMarkers(),
  393. true, firstPos, lastPos);
  394. int size = positions.size();
  395. for (int i = 0; i < size; i++) {
  396. painter.handleTableContentPosition((TableContentPosition)positions.get(i));
  397. }
  398. getTableLM().getCurrentPV().addMarkers(body.getMarkers(),
  399. false, firstPos, lastPos);
  400. }
  401. /**
  402. * Get the area for a row for background.
  403. * @param row the table-row object or null
  404. * @return the row area or null if there's no background to paint
  405. */
  406. Block getRowArea(TableRow row) {
  407. if (row == null || !row.getCommonBorderPaddingBackground().hasBackground()) {
  408. return null;
  409. } else {
  410. Block block = new Block();
  411. block.addTrait(Trait.IS_REFERENCE_AREA, Boolean.TRUE);
  412. block.setPositioning(Block.ABSOLUTE);
  413. return block;
  414. }
  415. }
  416. /**
  417. * Adds the area for the row background if any.
  418. * @param row row for which to generate the background
  419. * @param bpd block-progression-dimension of the row
  420. * @param ipd inline-progression-dimension of the row
  421. * @param yoffset Y offset at which to paint
  422. */
  423. void addRowBackgroundArea(TableRow row, int bpd, int ipd, int yoffset) {
  424. //Add row background if any
  425. Block rowBackground = getRowArea(row);
  426. if (rowBackground != null) {
  427. rowBackground.setBPD(bpd);
  428. rowBackground.setIPD(ipd);
  429. rowBackground.setXOffset(this.startXOffset);
  430. rowBackground.setYOffset(yoffset);
  431. getTableLM().addChildArea(rowBackground);
  432. TraitSetter.addBackground(rowBackground,
  433. row.getCommonBorderPaddingBackground(), getTableLM());
  434. }
  435. }
  436. /**
  437. * Sets the overall starting x-offset. Used for proper placement of cells.
  438. * @param startXOffset starting x-offset (table's start-indent)
  439. */
  440. void setStartXOffset(int startXOffset) {
  441. this.startXOffset = startXOffset;
  442. }
  443. /**
  444. * @return the amount of block-progression-dimension used by the content
  445. */
  446. int getUsedBPD() {
  447. return this.usedBPD;
  448. }
  449. // --------- Property Resolution related functions --------- //
  450. /**
  451. * {@inheritDoc}
  452. */
  453. public int getBaseLength(int lengthBase, FObj fobj) {
  454. return tableLM.getBaseLength(lengthBase, fobj);
  455. }
  456. }