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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541
  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.ListIterator;
  23. import java.util.Map;
  24. import org.apache.commons.logging.Log;
  25. import org.apache.commons.logging.LogFactory;
  26. import org.apache.fop.datatypes.PercentBaseContext;
  27. import org.apache.fop.fo.Constants;
  28. import org.apache.fop.fo.FObj;
  29. import org.apache.fop.fo.flow.table.EffRow;
  30. import org.apache.fop.fo.flow.table.PrimaryGridUnit;
  31. import org.apache.fop.fo.flow.table.Table;
  32. import org.apache.fop.fo.flow.table.TablePart;
  33. import org.apache.fop.layoutmgr.BreakElement;
  34. import org.apache.fop.layoutmgr.ElementListUtils;
  35. import org.apache.fop.layoutmgr.Keep;
  36. import org.apache.fop.layoutmgr.KnuthBox;
  37. import org.apache.fop.layoutmgr.KnuthElement;
  38. import org.apache.fop.layoutmgr.KnuthGlue;
  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.SpaceResolver.SpaceHandlingBreakPosition;
  45. import org.apache.fop.util.BreakUtil;
  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 final 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;
  63. private boolean headerIsBeingRepeated;
  64. /**
  65. * Main constructor
  66. * @param parent Parent layout manager
  67. */
  68. TableContentLayoutManager(TableLayoutManager parent) {
  69. this.tableLM = parent;
  70. Table table = getTableLM().getTable();
  71. this.bodyIter = new TableRowIterator(table, TableRowIterator.BODY);
  72. if (table.getTableHeader() != null) {
  73. headerIter = new TableRowIterator(table, TableRowIterator.HEADER);
  74. }
  75. if (table.getTableFooter() != null) {
  76. footerIter = new TableRowIterator(table, TableRowIterator.FOOTER);
  77. }
  78. stepper = new TableStepper(this);
  79. }
  80. /**
  81. * @return the table layout manager
  82. */
  83. TableLayoutManager getTableLM() {
  84. return this.tableLM;
  85. }
  86. /** @return true if the table uses the separate border model. */
  87. boolean isSeparateBorderModel() {
  88. return getTableLM().getTable().isSeparateBorderModel();
  89. }
  90. /**
  91. * @return the column setup of this table
  92. */
  93. ColumnSetup getColumns() {
  94. return getTableLM().getColumns();
  95. }
  96. /** @return the net header height */
  97. protected int getHeaderNetHeight() {
  98. return this.headerNetHeight;
  99. }
  100. /** @return the net footer height */
  101. protected int getFooterNetHeight() {
  102. return this.footerNetHeight;
  103. }
  104. /** @return the header element list */
  105. protected LinkedList getHeaderElements() {
  106. return this.headerList;
  107. }
  108. /** @return the footer element list */
  109. protected LinkedList getFooterElements() {
  110. return this.footerList;
  111. }
  112. /**
  113. * Get a sequence of KnuthElements representing the content
  114. * of the node assigned to the LM.
  115. *
  116. * @param context the LayoutContext used to store layout information
  117. * @param alignment the desired text alignment
  118. * @return the list of KnuthElements
  119. * @see org.apache.fop.layoutmgr.LayoutManager#getNextKnuthElements(LayoutContext, int)
  120. */
  121. public List getNextKnuthElements(LayoutContext context, int alignment) {
  122. if (LOG.isDebugEnabled()) {
  123. LOG.debug("==> Columns: " + getTableLM().getColumns());
  124. }
  125. KnuthBox headerAsFirst = null;
  126. KnuthBox headerAsSecondToLast = null;
  127. KnuthBox footerAsLast = null;
  128. if (headerIter != null && headerList == null) {
  129. this.headerList = getKnuthElementsForRowIterator(
  130. headerIter, context, alignment, TableRowIterator.HEADER);
  131. this.headerNetHeight
  132. = ElementListUtils.calcContentLength(this.headerList);
  133. if (LOG.isDebugEnabled()) {
  134. LOG.debug("==> Header: "
  135. + headerNetHeight + " - " + this.headerList);
  136. }
  137. TableHeaderFooterPosition pos = new TableHeaderFooterPosition(
  138. getTableLM(), true, this.headerList);
  139. KnuthBox box = new KnuthBox(headerNetHeight, pos, false);
  140. if (getTableLM().getTable().omitHeaderAtBreak()) {
  141. //We can simply add the table header at the start
  142. //of the whole list
  143. headerAsFirst = box;
  144. } else {
  145. headerAsSecondToLast = box;
  146. }
  147. }
  148. if (footerIter != null && footerList == null) {
  149. this.footerList = getKnuthElementsForRowIterator(
  150. footerIter, context, alignment, TableRowIterator.FOOTER);
  151. this.footerNetHeight
  152. = ElementListUtils.calcContentLength(this.footerList);
  153. if (LOG.isDebugEnabled()) {
  154. LOG.debug("==> Footer: "
  155. + footerNetHeight + " - " + this.footerList);
  156. }
  157. //We can simply add the table footer at the end of the whole list
  158. TableHeaderFooterPosition pos = new TableHeaderFooterPosition(
  159. getTableLM(), false, this.footerList);
  160. KnuthBox box = new KnuthBox(footerNetHeight, pos, false);
  161. footerAsLast = box;
  162. }
  163. LinkedList returnList = getKnuthElementsForRowIterator(
  164. bodyIter, context, alignment, TableRowIterator.BODY);
  165. if (headerAsFirst != null) {
  166. int insertionPoint = 0;
  167. if (returnList.size() > 0 && ((ListElement)returnList.getFirst()).isForcedBreak()) {
  168. insertionPoint++;
  169. }
  170. returnList.add(insertionPoint, headerAsFirst);
  171. } else if (headerAsSecondToLast != null) {
  172. int insertionPoint = returnList.size();
  173. if (returnList.size() > 0 && ((ListElement)returnList.getLast()).isForcedBreak()) {
  174. insertionPoint--;
  175. }
  176. returnList.add(insertionPoint, headerAsSecondToLast);
  177. }
  178. if (footerAsLast != null) {
  179. int insertionPoint = returnList.size();
  180. if (returnList.size() > 0 && ((ListElement)returnList.getLast()).isForcedBreak()) {
  181. insertionPoint--;
  182. }
  183. returnList.add(insertionPoint, footerAsLast);
  184. }
  185. return returnList;
  186. }
  187. /**
  188. * Creates Knuth elements by iterating over a TableRowIterator.
  189. * @param iter TableRowIterator instance to fetch rows from
  190. * @param context Active LayoutContext
  191. * @param alignment alignment indicator
  192. * @param bodyType Indicates what kind of body is being processed
  193. * (BODY, HEADER or FOOTER)
  194. * @return An element list
  195. */
  196. private LinkedList getKnuthElementsForRowIterator(TableRowIterator iter,
  197. LayoutContext context, int alignment, int bodyType) {
  198. LinkedList returnList = new LinkedList();
  199. EffRow[] rowGroup = iter.getNextRowGroup();
  200. // TODO homogenize the handling of keeps and breaks
  201. context.clearKeepsPending();
  202. context.setBreakBefore(Constants.EN_AUTO);
  203. context.setBreakAfter(Constants.EN_AUTO);
  204. Keep keepWithPrevious = Keep.KEEP_AUTO;
  205. int breakBefore = Constants.EN_AUTO;
  206. if (rowGroup != null) {
  207. RowGroupLayoutManager rowGroupLM = new RowGroupLayoutManager(getTableLM(), rowGroup,
  208. stepper);
  209. List nextRowGroupElems = rowGroupLM.getNextKnuthElements(context, alignment, bodyType);
  210. keepWithPrevious = keepWithPrevious.compare(context.getKeepWithPreviousPending());
  211. breakBefore = context.getBreakBefore();
  212. int breakBetween = context.getBreakAfter();
  213. returnList.addAll(nextRowGroupElems);
  214. while ((rowGroup = iter.getNextRowGroup()) != null) {
  215. rowGroupLM = new RowGroupLayoutManager(getTableLM(), rowGroup, stepper);
  216. //Note previous pending keep-with-next and clear the strength
  217. //(as the layout context is reused)
  218. Keep keepWithNextPending = context.getKeepWithNextPending();
  219. context.clearKeepWithNextPending();
  220. //Get elements for next row group
  221. nextRowGroupElems = rowGroupLM.getNextKnuthElements(context, alignment, bodyType);
  222. /*
  223. * The last break element produced by TableStepper (for the previous row
  224. * group) may be used to represent the break between the two row groups.
  225. * Its penalty value and break class must just be overridden by the
  226. * characteristics of the keep or break between the two.
  227. *
  228. * However, we mustn't forget that if the after border of the last row of
  229. * the row group is thicker in the normal case than in the trailing case,
  230. * an additional glue will be appended to the element list. So we may have
  231. * to go two steps backwards in the list.
  232. */
  233. //Determine keep constraints
  234. Keep keep = keepWithNextPending.compare(context.getKeepWithPreviousPending());
  235. context.clearKeepWithPreviousPending();
  236. keep = keep.compare(getTableLM().getKeepTogether());
  237. int penaltyValue = keep.getPenalty();
  238. int breakClass = keep.getContext();
  239. breakBetween = BreakUtil.compareBreakClasses(breakBetween,
  240. context.getBreakBefore());
  241. if (breakBetween != Constants.EN_AUTO) {
  242. penaltyValue = -KnuthElement.INFINITE;
  243. breakClass = breakBetween;
  244. }
  245. BreakElement breakElement;
  246. ListIterator elemIter = returnList.listIterator(returnList.size());
  247. ListElement elem = (ListElement) elemIter.previous();
  248. if (elem instanceof KnuthGlue) {
  249. breakElement = (BreakElement) elemIter.previous();
  250. } else {
  251. breakElement = (BreakElement) elem;
  252. }
  253. breakElement.setPenaltyValue(penaltyValue);
  254. breakElement.setBreakClass(breakClass);
  255. returnList.addAll(nextRowGroupElems);
  256. breakBetween = context.getBreakAfter();
  257. }
  258. }
  259. /*
  260. * The last break produced for the last row-group of this table part must be
  261. * removed, because the breaking after the table will be handled by TableLM.
  262. * Unless the element list ends with a glue, which must be kept to accurately
  263. * represent the content. In such a case the break is simply disabled by setting
  264. * its penalty to infinite.
  265. */
  266. ListIterator elemIter = returnList.listIterator(returnList.size());
  267. ListElement elem = (ListElement) elemIter.previous();
  268. if (elem instanceof KnuthGlue) {
  269. BreakElement breakElement = (BreakElement) elemIter.previous();
  270. breakElement.setPenaltyValue(KnuthElement.INFINITE);
  271. } else {
  272. elemIter.remove();
  273. }
  274. context.updateKeepWithPreviousPending(keepWithPrevious);
  275. context.setBreakBefore(breakBefore);
  276. //fox:widow-content-limit
  277. int widowContentLimit = getTableLM().getTable().getWidowContentLimit().getValue();
  278. if (widowContentLimit != 0 && bodyType == TableRowIterator.BODY) {
  279. ElementListUtils.removeLegalBreaks(returnList, widowContentLimit);
  280. }
  281. //fox:orphan-content-limit
  282. int orphanContentLimit = getTableLM().getTable().getOrphanContentLimit().getValue();
  283. if (orphanContentLimit != 0 && bodyType == TableRowIterator.BODY) {
  284. ElementListUtils.removeLegalBreaksFromEnd(returnList, orphanContentLimit);
  285. }
  286. return returnList;
  287. }
  288. /**
  289. * Returns the X offset of the given grid unit.
  290. * @param gu the grid unit
  291. * @return the requested X offset
  292. */
  293. protected int getXOffsetOfGridUnit(PrimaryGridUnit gu) {
  294. return getXOffsetOfGridUnit(gu.getColIndex(), gu.getCell().getNumberColumnsSpanned());
  295. }
  296. /**
  297. * Returns the X offset of the grid unit in the given column.
  298. * @param colIndex the column index (zero-based)
  299. * @param nrColSpan number columns spanned
  300. * @return the requested X offset
  301. */
  302. protected int getXOffsetOfGridUnit(int colIndex, int nrColSpan) {
  303. return startXOffset + getTableLM().getColumns().getXOffset(colIndex + 1, nrColSpan, getTableLM());
  304. }
  305. /**
  306. * Adds the areas generated by this layout manager to the area tree.
  307. * @param parentIter the position iterator
  308. * @param layoutContext the layout context for adding areas
  309. */
  310. void addAreas(PositionIterator parentIter, LayoutContext layoutContext) {
  311. this.usedBPD = 0;
  312. RowPainter painter = new RowPainter(this, layoutContext);
  313. List tablePositions = new java.util.ArrayList();
  314. List headerElements = null;
  315. List footerElements = null;
  316. Position firstPos = null;
  317. Position lastPos = null;
  318. Position lastCheckPos = null;
  319. while (parentIter.hasNext()) {
  320. Position pos = (Position)parentIter.next();
  321. if (pos instanceof SpaceHandlingBreakPosition) {
  322. //This position has only been needed before addAreas was called, now we need the
  323. //original one created by the layout manager.
  324. pos = ((SpaceHandlingBreakPosition)pos).getOriginalBreakPosition();
  325. }
  326. if (pos == null) {
  327. continue;
  328. }
  329. if (firstPos == null) {
  330. firstPos = pos;
  331. }
  332. lastPos = pos;
  333. if (pos.getIndex() >= 0) {
  334. lastCheckPos = pos;
  335. }
  336. if (pos instanceof TableHeaderFooterPosition) {
  337. TableHeaderFooterPosition thfpos = (TableHeaderFooterPosition)pos;
  338. //these positions need to be unpacked
  339. if (thfpos.header) {
  340. //Positions for header will be added first
  341. headerElements = thfpos.nestedElements;
  342. } else {
  343. //Positions for footers are simply added at the end
  344. footerElements = thfpos.nestedElements;
  345. }
  346. } else if (pos instanceof TableHFPenaltyPosition) {
  347. //ignore for now, see special handling below if break is at a penalty
  348. //Only if the last position in this part/page us such a position it will be used
  349. } else if (pos instanceof TableContentPosition) {
  350. tablePositions.add(pos);
  351. } else {
  352. if (LOG.isDebugEnabled()) {
  353. LOG.debug("Ignoring position: " + pos);
  354. }
  355. }
  356. }
  357. boolean treatFooterAsArtifact = layoutContext.treatAsArtifact();
  358. if (lastPos instanceof TableHFPenaltyPosition) {
  359. TableHFPenaltyPosition penaltyPos = (TableHFPenaltyPosition)lastPos;
  360. LOG.debug("Break at penalty!");
  361. if (penaltyPos.headerElements != null) {
  362. //Header positions for the penalty position are in the last element and need to
  363. //be handled first before all other TableContentPositions
  364. headerElements = penaltyPos.headerElements;
  365. }
  366. if (penaltyPos.footerElements != null) {
  367. footerElements = penaltyPos.footerElements;
  368. treatFooterAsArtifact = true;
  369. }
  370. }
  371. Map markers = getTableLM().getTable().getMarkers();
  372. if (markers != null) {
  373. getTableLM().getCurrentPV().addMarkers(markers,
  374. true, getTableLM().isFirst(firstPos), getTableLM().isLast(lastCheckPos));
  375. }
  376. if (headerElements != null) {
  377. boolean ancestorTreatAsArtifact = layoutContext.treatAsArtifact();
  378. if (headerIsBeingRepeated) {
  379. layoutContext.setTreatAsArtifact(true);
  380. }
  381. //header positions for the last part are the second-to-last element and need to
  382. //be handled first before all other TableContentPositions
  383. addHeaderFooterAreas(headerElements, tableLM.getTable().getTableHeader(), painter,
  384. false);
  385. if (!ancestorTreatAsArtifact) {
  386. headerIsBeingRepeated = true;
  387. }
  388. layoutContext.setTreatAsArtifact(ancestorTreatAsArtifact);
  389. }
  390. if (tablePositions.isEmpty()) {
  391. // TODO make sure this actually never happens
  392. LOG.error("tablePositions empty."
  393. + " Please send your FO file to fop-users@xmlgraphics.apache.org");
  394. } else {
  395. // Here we are sure that posIter iterates only over TableContentPosition instances
  396. addBodyAreas(tablePositions.iterator(), painter, footerElements == null);
  397. }
  398. if (footerElements != null) {
  399. boolean ancestorTreatAsArtifact = layoutContext.treatAsArtifact();
  400. layoutContext.setTreatAsArtifact(treatFooterAsArtifact);
  401. //Positions for footers are simply added at the end
  402. addHeaderFooterAreas(footerElements, tableLM.getTable().getTableFooter(), painter,
  403. true);
  404. layoutContext.setTreatAsArtifact(ancestorTreatAsArtifact);
  405. }
  406. this.usedBPD += painter.getAccumulatedBPD();
  407. if (markers != null) {
  408. getTableLM().getCurrentPV().addMarkers(markers,
  409. false, getTableLM().isFirst(firstPos), getTableLM().isLast(lastCheckPos));
  410. }
  411. }
  412. private void addHeaderFooterAreas(List elements, TablePart part, RowPainter painter,
  413. boolean lastOnPage) {
  414. List lst = new java.util.ArrayList(elements.size());
  415. for (Iterator iter = new KnuthPossPosIter(elements); iter.hasNext();) {
  416. Position pos = (Position) iter.next();
  417. /*
  418. * Unlike for the body the Positions associated to the glues generated by
  419. * TableStepper haven't been removed yet.
  420. */
  421. if (pos instanceof TableContentPosition) {
  422. lst.add((TableContentPosition) pos);
  423. }
  424. }
  425. addTablePartAreas(lst, painter, part, true, true, true, lastOnPage);
  426. }
  427. /**
  428. * Iterates over the positions corresponding to the table's body (which may contain
  429. * several table-body elements!) and adds the corresponding areas.
  430. *
  431. * @param iterator iterator over TableContentPosition elements. Those positions
  432. * correspond to the elements of the body present on the current page
  433. * @param painter
  434. * @param lastOnPage true if the table has no footer (then the last line of the table
  435. * that will be present on the page belongs to the body)
  436. */
  437. private void addBodyAreas(Iterator iterator, RowPainter painter,
  438. boolean lastOnPage) {
  439. painter.startBody();
  440. List lst = new java.util.ArrayList();
  441. TableContentPosition pos = (TableContentPosition) iterator.next();
  442. boolean isFirstPos = pos.getFlag(TableContentPosition.FIRST_IN_ROWGROUP)
  443. && pos.getRow().getFlag(EffRow.FIRST_IN_PART);
  444. TablePart part = pos.getTablePart();
  445. lst.add(pos);
  446. while (iterator.hasNext()) {
  447. pos = (TableContentPosition) iterator.next();
  448. if (pos.getTablePart() != part) {
  449. addTablePartAreas(lst, painter, part, isFirstPos, true, false, false);
  450. isFirstPos = true;
  451. lst.clear();
  452. part = pos.getTablePart();
  453. }
  454. lst.add(pos);
  455. }
  456. boolean isLastPos = pos.getFlag(TableContentPosition.LAST_IN_ROWGROUP)
  457. && pos.getRow().getFlag(EffRow.LAST_IN_PART);
  458. addTablePartAreas(lst, painter, part, isFirstPos, isLastPos, true, lastOnPage);
  459. painter.endBody();
  460. }
  461. /**
  462. * Adds the areas corresponding to a single fo:table-header/footer/body element.
  463. */
  464. private void addTablePartAreas(List positions, RowPainter painter, TablePart body,
  465. boolean isFirstPos, boolean isLastPos, boolean lastInBody, boolean lastOnPage) {
  466. getTableLM().getCurrentPV().addMarkers(body.getMarkers(),
  467. true, isFirstPos, isLastPos);
  468. painter.startTablePart(body);
  469. for (Iterator iter = positions.iterator(); iter.hasNext();) {
  470. painter.handleTableContentPosition((TableContentPosition) iter.next());
  471. }
  472. getTableLM().getCurrentPV().addMarkers(body.getMarkers(),
  473. false, isFirstPos, isLastPos);
  474. painter.endTablePart(lastInBody, lastOnPage);
  475. }
  476. /**
  477. * Sets the overall starting x-offset. Used for proper placement of cells.
  478. * @param startXOffset starting x-offset (table's start-indent)
  479. */
  480. void setStartXOffset(int startXOffset) {
  481. this.startXOffset = startXOffset;
  482. }
  483. /**
  484. * @return the amount of block-progression-dimension used by the content
  485. */
  486. int getUsedBPD() {
  487. return this.usedBPD;
  488. }
  489. // --------- Property Resolution related functions --------- //
  490. /**
  491. * {@inheritDoc}
  492. */
  493. public int getBaseLength(int lengthBase, FObj fobj) {
  494. return tableLM.getBaseLength(lengthBase, fobj);
  495. }
  496. }