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

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