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

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