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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511
  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.ListIterator;
  24. import java.util.Map;
  25. import org.apache.commons.logging.Log;
  26. import org.apache.commons.logging.LogFactory;
  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.PrimaryGridUnit;
  32. import org.apache.fop.fo.flow.table.Table;
  33. import org.apache.fop.fo.flow.table.TableBody;
  34. import org.apache.fop.layoutmgr.BlockLevelLayoutManager;
  35. import org.apache.fop.layoutmgr.BreakElement;
  36. import org.apache.fop.layoutmgr.ElementListUtils;
  37. import org.apache.fop.layoutmgr.KeepUtil;
  38. import org.apache.fop.layoutmgr.KnuthBox;
  39. import org.apache.fop.layoutmgr.KnuthElement;
  40. import org.apache.fop.layoutmgr.KnuthGlue;
  41. import org.apache.fop.layoutmgr.KnuthPossPosIter;
  42. import org.apache.fop.layoutmgr.LayoutContext;
  43. import org.apache.fop.layoutmgr.ListElement;
  44. import org.apache.fop.layoutmgr.Position;
  45. import org.apache.fop.layoutmgr.PositionIterator;
  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.clearKeepsPending();
  195. context.setBreakBefore(Constants.EN_AUTO);
  196. context.setBreakAfter(Constants.EN_AUTO);
  197. int keepWithPrevious = BlockLevelLayoutManager.KEEP_AUTO;
  198. int breakBefore = Constants.EN_AUTO;
  199. if (rowGroup != null) {
  200. RowGroupLayoutManager rowGroupLM = new RowGroupLayoutManager(getTableLM(), rowGroup,
  201. stepper);
  202. List nextRowGroupElems = rowGroupLM.getNextKnuthElements(context, alignment, bodyType);
  203. keepWithPrevious = Math.max(keepWithPrevious, context.getKeepWithPreviousPending());
  204. breakBefore = context.getBreakBefore();
  205. int breakBetween = context.getBreakAfter();
  206. returnList.addAll(nextRowGroupElems);
  207. while ((rowGroup = iter.getNextRowGroup()) != null) {
  208. rowGroupLM = new RowGroupLayoutManager(getTableLM(), rowGroup, stepper);
  209. //Note previous pending keep-with-next and clear the strength
  210. //(as the layout context is reused)
  211. int keepWithNextPending = context.getKeepWithNextPending();
  212. context.clearKeepWithNextPending();
  213. //Get elements for next row group
  214. nextRowGroupElems = rowGroupLM.getNextKnuthElements(context, alignment, bodyType);
  215. /*
  216. * The last break element produced by TableStepper (for the previous row
  217. * group) may be used to represent the break between the two row groups.
  218. * Its penalty value and break class must just be overridden by the
  219. * characteristics of the keep or break between the two.
  220. *
  221. * However, we mustn't forget that if the after border of the last row of
  222. * the row group is thicker in the normal case than in the trailing case,
  223. * an additional glue will be appended to the element list. So we may have
  224. * to go two steps backwards in the list.
  225. */
  226. //Determine keep constraints
  227. int penaltyStrength = BlockLevelLayoutManager.KEEP_AUTO;
  228. penaltyStrength = Math.max(penaltyStrength, keepWithNextPending);
  229. penaltyStrength = Math.max(penaltyStrength, context.getKeepWithPreviousPending());
  230. context.clearKeepWithPreviousPending();
  231. penaltyStrength = Math.max(penaltyStrength, getTableLM().getKeepTogetherStrength());
  232. int penaltyValue = KeepUtil.getPenaltyForKeep(penaltyStrength);
  233. breakBetween = BreakUtil.compareBreakClasses(breakBetween,
  234. context.getBreakBefore());
  235. if (breakBetween != Constants.EN_AUTO) {
  236. penaltyValue = -KnuthElement.INFINITE;
  237. }
  238. BreakElement breakElement;
  239. ListIterator elemIter = returnList.listIterator(returnList.size());
  240. ListElement elem = (ListElement) elemIter.previous();
  241. if (elem instanceof KnuthGlue) {
  242. breakElement = (BreakElement) elemIter.previous();
  243. } else {
  244. breakElement = (BreakElement) elem;
  245. }
  246. breakElement.setPenaltyValue(penaltyValue);
  247. breakElement.setBreakClass(breakBetween);
  248. returnList.addAll(nextRowGroupElems);
  249. breakBetween = context.getBreakAfter();
  250. }
  251. }
  252. /*
  253. * The last break produced for the last row-group of this table part must be
  254. * removed, because the breaking after the table will be handled by TableLM.
  255. * Unless the element list ends with a glue, which must be kept to accurately
  256. * represent the content. In such a case the break is simply disabled by setting
  257. * its penalty to infinite.
  258. */
  259. ListIterator elemIter = returnList.listIterator(returnList.size());
  260. ListElement elem = (ListElement) elemIter.previous();
  261. if (elem instanceof KnuthGlue) {
  262. BreakElement breakElement = (BreakElement) elemIter.previous();
  263. breakElement.setPenaltyValue(KnuthElement.INFINITE);
  264. } else {
  265. elemIter.remove();
  266. }
  267. context.updateKeepWithPreviousPending(keepWithPrevious);
  268. context.setBreakBefore(breakBefore);
  269. //fox:widow-content-limit
  270. int widowContentLimit = getTableLM().getTable().getWidowContentLimit().getValue();
  271. if (widowContentLimit != 0 && bodyType == TableRowIterator.BODY) {
  272. ElementListUtils.removeLegalBreaks(returnList, widowContentLimit);
  273. }
  274. //fox:orphan-content-limit
  275. int orphanContentLimit = getTableLM().getTable().getOrphanContentLimit().getValue();
  276. if (orphanContentLimit != 0 && bodyType == TableRowIterator.BODY) {
  277. ElementListUtils.removeLegalBreaksFromEnd(returnList, orphanContentLimit);
  278. }
  279. return returnList;
  280. }
  281. /**
  282. * Retuns the X offset of the given grid unit.
  283. * @param gu the grid unit
  284. * @return the requested X offset
  285. */
  286. protected int getXOffsetOfGridUnit(PrimaryGridUnit gu) {
  287. int col = gu.getColIndex();
  288. return startXOffset + getTableLM().getColumns().getXOffset(col + 1, getTableLM());
  289. }
  290. /**
  291. * Adds the areas generated by this layout manager to the area tree.
  292. * @param parentIter the position iterator
  293. * @param layoutContext the layout context for adding areas
  294. */
  295. void addAreas(PositionIterator parentIter, LayoutContext layoutContext) {
  296. this.usedBPD = 0;
  297. RowPainter painter = new RowPainter(this, layoutContext);
  298. List tablePositions = new ArrayList();
  299. List headerElements = null;
  300. List footerElements = null;
  301. Position firstPos = null;
  302. Position lastPos = null;
  303. Position lastCheckPos = null;
  304. while (parentIter.hasNext()) {
  305. Position pos = (Position)parentIter.next();
  306. if (pos instanceof SpaceHandlingBreakPosition) {
  307. //This position has only been needed before addAreas was called, now we need the
  308. //original one created by the layout manager.
  309. pos = ((SpaceHandlingBreakPosition)pos).getOriginalBreakPosition();
  310. }
  311. if (pos == null) {
  312. continue;
  313. }
  314. if (firstPos == null) {
  315. firstPos = pos;
  316. }
  317. lastPos = pos;
  318. if (pos.getIndex() >= 0) {
  319. lastCheckPos = pos;
  320. }
  321. if (pos instanceof TableHeaderFooterPosition) {
  322. TableHeaderFooterPosition thfpos = (TableHeaderFooterPosition)pos;
  323. //these positions need to be unpacked
  324. if (thfpos.header) {
  325. //Positions for header will be added first
  326. headerElements = thfpos.nestedElements;
  327. } else {
  328. //Positions for footers are simply added at the end
  329. footerElements = thfpos.nestedElements;
  330. }
  331. } else if (pos instanceof TableHFPenaltyPosition) {
  332. //ignore for now, see special handling below if break is at a penalty
  333. //Only if the last position in this part/page us such a position it will be used
  334. } else if (pos instanceof TableContentPosition) {
  335. tablePositions.add(pos);
  336. } else {
  337. if (log.isDebugEnabled()) {
  338. log.debug("Ignoring position: " + pos);
  339. }
  340. }
  341. }
  342. if (lastPos instanceof TableHFPenaltyPosition) {
  343. TableHFPenaltyPosition penaltyPos = (TableHFPenaltyPosition)lastPos;
  344. log.debug("Break at penalty!");
  345. if (penaltyPos.headerElements != null) {
  346. //Header positions for the penalty position are in the last element and need to
  347. //be handled first before all other TableContentPositions
  348. headerElements = penaltyPos.headerElements;
  349. }
  350. if (penaltyPos.footerElements != null) {
  351. footerElements = penaltyPos.footerElements;
  352. }
  353. }
  354. Map markers = getTableLM().getTable().getMarkers();
  355. if (markers != null) {
  356. getTableLM().getCurrentPV().addMarkers(markers,
  357. true, getTableLM().isFirst(firstPos), getTableLM().isLast(lastCheckPos));
  358. }
  359. if (headerElements != null) {
  360. //header positions for the last part are the second-to-last element and need to
  361. //be handled first before all other TableContentPositions
  362. addHeaderFooterAreas(headerElements, tableLM.getTable().getTableHeader(), painter,
  363. false);
  364. }
  365. if (tablePositions.isEmpty()) {
  366. // TODO make sure this actually never happens
  367. log.error("tablePositions empty."
  368. + " Please send your FO file to fop-users@xmlgraphics.apache.org");
  369. } else {
  370. // Here we are sure that posIter iterates only over TableContentPosition instances
  371. addBodyAreas(tablePositions.iterator(), painter, footerElements == null);
  372. }
  373. if (footerElements != null) {
  374. //Positions for footers are simply added at the end
  375. addHeaderFooterAreas(footerElements, tableLM.getTable().getTableFooter(), painter,
  376. true);
  377. }
  378. this.usedBPD += painter.getAccumulatedBPD();
  379. if (markers != null) {
  380. getTableLM().getCurrentPV().addMarkers(markers,
  381. false, getTableLM().isFirst(firstPos), getTableLM().isLast(lastCheckPos));
  382. }
  383. }
  384. private void addHeaderFooterAreas(List elements, TableBody part, RowPainter painter,
  385. boolean lastOnPage) {
  386. List lst = new ArrayList(elements.size());
  387. for (Iterator iter = new KnuthPossPosIter(elements); iter.hasNext();) {
  388. Position pos = (Position) iter.next();
  389. /*
  390. * Unlike for the body the Positions associated to the glues generated by
  391. * TableStepper haven't been removed yet.
  392. */
  393. if (pos instanceof TableContentPosition) {
  394. lst.add((TableContentPosition) pos);
  395. }
  396. }
  397. addTablePartAreas(lst, painter, part, true, true, true, lastOnPage);
  398. }
  399. /**
  400. * Iterates over the positions corresponding to the table's body (which may contain
  401. * several table-body elements!) and adds the corresponding areas.
  402. *
  403. * @param iterator iterator over TableContentPosition elements. Those positions
  404. * correspond to the elements of the body present on the current page
  405. * @param painter
  406. * @param lastOnPage true if the table has no footer (then the last line of the table
  407. * that will be present on the page belongs to the body)
  408. */
  409. private void addBodyAreas(Iterator iterator, RowPainter painter,
  410. boolean lastOnPage) {
  411. painter.startBody();
  412. List lst = new ArrayList();
  413. TableContentPosition pos = (TableContentPosition) iterator.next();
  414. boolean isFirstPos = pos.getFlag(TableContentPosition.FIRST_IN_ROWGROUP)
  415. && pos.getRow().getFlag(EffRow.FIRST_IN_PART);
  416. TableBody body = pos.getTableBody();
  417. lst.add(pos);
  418. while (iterator.hasNext()) {
  419. pos = (TableContentPosition) iterator.next();
  420. if (pos.getTableBody() != body) {
  421. addTablePartAreas(lst, painter, body, isFirstPos, true, false, false);
  422. isFirstPos = true;
  423. lst.clear();
  424. body = pos.getTableBody();
  425. }
  426. lst.add(pos);
  427. }
  428. boolean isLastPos = pos.getFlag(TableContentPosition.LAST_IN_ROWGROUP)
  429. && pos.getRow().getFlag(EffRow.LAST_IN_PART);
  430. addTablePartAreas(lst, painter, body, isFirstPos, isLastPos, true, lastOnPage);
  431. painter.endBody();
  432. }
  433. /**
  434. * Adds the areas corresponding to a single fo:table-header/footer/body element.
  435. */
  436. private void addTablePartAreas(List positions, RowPainter painter, TableBody body,
  437. boolean isFirstPos, boolean isLastPos, boolean lastInBody, boolean lastOnPage) {
  438. getTableLM().getCurrentPV().addMarkers(body.getMarkers(),
  439. true, isFirstPos, isLastPos);
  440. painter.startTablePart(body);
  441. for (Iterator iter = positions.iterator(); iter.hasNext();) {
  442. painter.handleTableContentPosition((TableContentPosition) iter.next());
  443. }
  444. getTableLM().getCurrentPV().addMarkers(body.getMarkers(),
  445. false, isFirstPos, isLastPos);
  446. painter.endTablePart(lastInBody, lastOnPage);
  447. }
  448. /**
  449. * Sets the overall starting x-offset. Used for proper placement of cells.
  450. * @param startXOffset starting x-offset (table's start-indent)
  451. */
  452. void setStartXOffset(int startXOffset) {
  453. this.startXOffset = startXOffset;
  454. }
  455. /**
  456. * @return the amount of block-progression-dimension used by the content
  457. */
  458. int getUsedBPD() {
  459. return this.usedBPD;
  460. }
  461. // --------- Property Resolution related functions --------- //
  462. /**
  463. * {@inheritDoc}
  464. */
  465. public int getBaseLength(int lengthBase, FObj fobj) {
  466. return tableLM.getBaseLength(lengthBase, fobj);
  467. }
  468. }