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

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