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.

TableCell.java 17KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462
  1. /*
  2. * -- $Id$ --
  3. * Copyright (C) 2001 The Apache Software Foundation. All rights reserved.
  4. * For details on use and redistribution please refer to the
  5. * LICENSE file included with these sources.
  6. */
  7. package org.apache.fop.fo.flow;
  8. // FOP
  9. import org.apache.fop.fo.*;
  10. import org.apache.fop.fo.properties.*;
  11. import org.apache.fop.layout.*;
  12. import org.apache.fop.apps.FOPException;
  13. import org.apache.fop.datatypes.*;
  14. import org.xml.sax.Attributes;
  15. public class TableCell extends FObj {
  16. // int spaceBefore;
  17. // int spaceAfter;
  18. ColorType backgroundColor;
  19. String id;
  20. int numColumnsSpanned;
  21. int numRowsSpanned;
  22. int iColNumber = -1; // uninitialized
  23. /**
  24. * Offset of content rectangle in inline-progression-direction,
  25. * relative to table.
  26. */
  27. protected int startOffset;
  28. /**
  29. * Dimension of allocation rectangle in inline-progression-direction,
  30. * determined by the width of the column(s) occupied by the cell
  31. */
  32. protected int width;
  33. /**
  34. * Offset of content rectangle, in block-progression-direction,
  35. * relative to the row.
  36. */
  37. protected int beforeOffset = 0;
  38. /**
  39. * Offset of content rectangle, in inline-progression-direction,
  40. * relative to the column start edge.
  41. */
  42. protected int startAdjust = 0;
  43. /**
  44. * Adjust to theoretical column width to obtain content width
  45. * relative to the column start edge.
  46. */
  47. protected int widthAdjust = 0;
  48. /* For collapsed border style */
  49. protected int borderHeight = 0;
  50. /**
  51. * Minimum ontent height of cell.
  52. */
  53. protected int minCellHeight = 0;
  54. protected int height = 0;
  55. protected int top; // Ypos of cell ???
  56. protected int verticalAlign;
  57. protected boolean bRelativeAlign = false;
  58. // boolean setup = false;
  59. boolean bSepBorders = true;
  60. /**
  61. * Set to true if all content completely laid out.
  62. */
  63. boolean bDone=false;
  64. /**
  65. * Border separation value in the block-progression dimension.
  66. * Used in calculating cells height.
  67. */
  68. int m_borderSeparation = 0;
  69. AreaContainer cellArea;
  70. public TableCell(FONode parent) {
  71. super(parent);
  72. }
  73. public void handleAttrs(Attributes attlist) throws FOPException {
  74. super.handleAttrs(attlist);
  75. doSetup(); // init some basic property values
  76. }
  77. // Set position relative to table (set by body?)
  78. public void setStartOffset(int offset) {
  79. startOffset = offset;
  80. }
  81. // Initially same as the column width containg this cell or the
  82. // sum of the spanned columns if numColumnsSpanned > 1
  83. public void setWidth(int width) {
  84. this.width = width;
  85. }
  86. public int getColumnNumber() {
  87. return iColNumber;
  88. }
  89. public int getNumColumnsSpanned() {
  90. return numColumnsSpanned;
  91. }
  92. public int getNumRowsSpanned() {
  93. return numRowsSpanned;
  94. }
  95. public void doSetup() // throws FOPException
  96. {
  97. // Common Accessibility Properties
  98. AccessibilityProps mAccProps = propMgr.getAccessibilityProps();
  99. // Common Aural Properties
  100. AuralProps mAurProps = propMgr.getAuralProps();
  101. // Common Border, Padding, and Background Properties
  102. BorderAndPadding bap = propMgr.getBorderAndPadding();
  103. BackgroundProps bProps = propMgr.getBackgroundProps();
  104. // Common Relative Position Properties
  105. RelativePositionProps mRelProps = propMgr.getRelativePositionProps();
  106. // this.properties.get("border-after-precedence");
  107. // this.properties.get("border-before-precendence");
  108. // this.properties.get("border-end-precendence");
  109. // this.properties.get("border-start-precendence");
  110. // this.properties.get("block-progression-dimension");
  111. // this.properties.get("column-number");
  112. // this.properties.get("display-align");
  113. // this.properties.get("relative-align");
  114. // this.properties.get("empty-cells");
  115. // this.properties.get("ends-row");
  116. // this.properties.get("height");
  117. // this.properties.get("id");
  118. // this.properties.get("number-columns-spanned");
  119. // this.properties.get("number-rows-spanned");
  120. // this.properties.get("starts-row");
  121. // this.properties.get("width");
  122. this.iColNumber =
  123. properties.get("column-number").getNumber().intValue();
  124. if (iColNumber < 0) {
  125. iColNumber = 0;
  126. }
  127. this.numColumnsSpanned =
  128. this.properties.get("number-columns-spanned").getNumber().intValue();
  129. if (numColumnsSpanned < 1) {
  130. numColumnsSpanned = 1;
  131. }
  132. this.numRowsSpanned =
  133. this.properties.get("number-rows-spanned").getNumber().intValue();
  134. if (numRowsSpanned < 1) {
  135. numRowsSpanned = 1;
  136. }
  137. this.backgroundColor =
  138. this.properties.get("background-color").getColorType();
  139. this.id = this.properties.get("id").getString();
  140. bSepBorders = (this.properties.get("border-collapse").getEnum()
  141. == BorderCollapse.SEPARATE);
  142. calcBorders(propMgr.getBorderAndPadding());
  143. // Vertical cell alignment
  144. verticalAlign = this.properties.get("display-align").getEnum();
  145. if (verticalAlign == DisplayAlign.AUTO) {
  146. // Depends on all cells starting in row
  147. bRelativeAlign = true;
  148. verticalAlign = this.properties.get("relative-align").getEnum();
  149. } else
  150. bRelativeAlign = false; // Align on a per-cell basis
  151. this.minCellHeight =
  152. this.properties.get("height").getLength().mvalue();
  153. }
  154. public Status layout(Area area) throws FOPException {
  155. int originalAbsoluteHeight = area.getAbsoluteHeight();
  156. if (this.marker == BREAK_AFTER) {
  157. return new Status(Status.OK);
  158. }
  159. if (this.marker == START) {
  160. // if (!setup) {
  161. // doSetup(area);
  162. // }
  163. // Calculate cell borders
  164. // calcBorders(propMgr.getBorderAndPadding());
  165. area.getIDReferences().createID(id);
  166. this.marker = 0;
  167. this.bDone=false;
  168. }
  169. /*
  170. * if ((spaceBefore != 0) && (this.marker ==0)) {
  171. * area.increaseHeight(spaceBefore);
  172. * }
  173. */
  174. if (marker == 0) {
  175. // configure id
  176. area.getIDReferences().configureID(id, area);
  177. }
  178. // int spaceLeft = area.spaceLeft() - m_borderSeparation/2 + borderHeight/2 ;
  179. int spaceLeft = area.spaceLeft() - m_borderSeparation;
  180. // The Area position defines the content rectangle! Borders
  181. // and padding are outside of this rectangle.
  182. this.cellArea =
  183. new AreaContainer(propMgr.getFontState(area.getFontInfo()),
  184. startOffset + startAdjust, beforeOffset,
  185. width - widthAdjust, spaceLeft,
  186. Position.RELATIVE);
  187. cellArea.foCreator = this; // G Seshadri
  188. cellArea.setPage(area.getPage());
  189. try {
  190. cellArea.setBorderAndPadding((BorderAndPadding)
  191. propMgr.getBorderAndPadding().clone());
  192. } catch (CloneNotSupportedException e) {
  193. System.err.println("Can't clone BorderAndPadding: " + e) ;
  194. cellArea.setBorderAndPadding(propMgr.getBorderAndPadding());
  195. }
  196. cellArea.setBackgroundColor(this.backgroundColor);
  197. cellArea.start();
  198. cellArea.setAbsoluteHeight(area.getAbsoluteHeight()); // ???
  199. cellArea.setIDReferences(area.getIDReferences());
  200. // ******** CHECK THIS: we've changed startOffset (KL)
  201. cellArea.setTableCellXOffset(startOffset);
  202. int numChildren = this.children.size();
  203. for (int i = this.marker; bDone==false && i < numChildren; i++) {
  204. FObj fo = (FObj)children.get(i);
  205. fo.setIsInTableCell();
  206. fo.forceWidth(width); // ???
  207. // Overflows may cause a row to be re-layedout,
  208. // need to pass already processed content.
  209. this.marker = i;
  210. Status status;
  211. if ((status = fo.layout(cellArea)).isIncomplete()) {
  212. // this.marker = i;
  213. if ((i == 0) && (status.getCode() == Status.AREA_FULL_NONE)) {
  214. return new Status(Status.AREA_FULL_NONE);
  215. } else {
  216. // hani Elabed 11/21/2000
  217. area.addChild(cellArea);
  218. // area.setAbsoluteHeight(cellArea.getAbsoluteHeight());
  219. return new Status(Status.AREA_FULL_SOME);
  220. }
  221. }
  222. area.setMaxHeight(area.getMaxHeight() - spaceLeft
  223. + this.cellArea.getMaxHeight());
  224. }
  225. this.bDone=true;
  226. cellArea.end();
  227. area.addChild(cellArea);
  228. // Adjust for minimum cell content height
  229. if (minCellHeight > cellArea.getContentHeight()) {
  230. cellArea.setHeight(minCellHeight);
  231. }
  232. // This is the allocation height of the cell (including borders
  233. // and padding
  234. // ALSO need to include offsets if using "separate borders"
  235. height = cellArea.getHeight();
  236. top = cellArea.getCurrentYPosition(); // CHECK THIS!!!
  237. // reset absoluteHeight to beginning of row
  238. // area.setHeight(cellArea.getHeight() + spaceBefore + spaceAfter);
  239. // I don't think we should do this here (KL) !!!
  240. // area.setHeight(cellArea.getHeight());
  241. // area.setAbsoluteHeight(originalAbsoluteHeight);
  242. return new Status(Status.OK);
  243. }
  244. /**
  245. * Return the allocation height of the cell area.
  246. * Note: called by TableRow.
  247. * We adjust the actual allocation height of the area by the value
  248. * of border separation (for separate borders) or border height
  249. * adjustment for collapse style (because current scheme makes cell
  250. * overestimate the allocation height).
  251. */
  252. public int getHeight() {
  253. return cellArea.getHeight() + m_borderSeparation - borderHeight;
  254. }
  255. /**
  256. * Set the final size of cell content rectangles to the actual row height
  257. * and to vertically align the actual content within the cell rectangle.
  258. * @param h Height of this row in the grid which is based on
  259. * the allocation height of all the cells in the row, including any
  260. * border separation values.
  261. */
  262. public void setRowHeight(int h) {
  263. int delta = h - getHeight();
  264. // cellArea.increaseHeight(h + borderHeight/2 - cellArea.getHeight());
  265. if (bRelativeAlign) {
  266. // Must get info for all cells starting in row!
  267. // verticalAlign can be BEFORE or BASELINE
  268. // For now just treat like "before"
  269. cellArea.increaseHeight(delta);
  270. } else if (delta > 0) {
  271. BorderAndPadding cellBP = cellArea.getBorderAndPadding();
  272. switch (verticalAlign) {
  273. case DisplayAlign.CENTER:
  274. // Increase cell padding before and after and change
  275. // "Y" position of content rectangle
  276. cellArea.shiftYPosition(delta / 2);
  277. cellBP.setPaddingLength(BorderAndPadding.TOP,
  278. cellBP.getPaddingTop(false)
  279. + delta / 2);
  280. cellBP.setPaddingLength(BorderAndPadding.BOTTOM,
  281. cellBP.getPaddingBottom(false)
  282. + delta - delta / 2);
  283. break;
  284. case DisplayAlign.AFTER:
  285. // Increase cell padding before and change
  286. // "Y" position of content rectangle
  287. cellBP.setPaddingLength(BorderAndPadding.TOP,
  288. cellBP.getPaddingTop(false) + delta);
  289. cellArea.shiftYPosition(delta);
  290. break;
  291. case DisplayAlign.BEFORE:
  292. // cellArea.increaseHeight(delta);
  293. cellBP.setPaddingLength(BorderAndPadding.BOTTOM,
  294. cellBP.getPaddingBottom(false)
  295. + delta);
  296. default: // OK
  297. break;
  298. }
  299. }
  300. }
  301. /**
  302. * Calculate cell border and padding, including offset of content
  303. * rectangle from the theoretical grid position.
  304. */
  305. private void calcBorders(BorderAndPadding bp) {
  306. if (this.bSepBorders) {
  307. /*
  308. * Easy case.
  309. * Cell border is the property specified directly on cell.
  310. * Offset content rect by half the border-separation value,
  311. * in addition to the border and padding values. Note:
  312. * border-separate should only be specified on the table object,
  313. * but it inherits.
  314. */
  315. int iSep =
  316. properties.get("border-separation.inline-progression-direction").getLength().mvalue();
  317. this.startAdjust = iSep / 2 + bp.getBorderLeftWidth(false)
  318. + bp.getPaddingLeft(false);
  319. /*
  320. * int contentOffset = iSep + bp.getBorderStartWidth(false) +
  321. * bp.getPaddingStart(false);
  322. */
  323. this.widthAdjust = startAdjust + iSep - iSep / 2
  324. + bp.getBorderRightWidth(false)
  325. + bp.getPaddingRight(false);
  326. // bp.getBorderEndWidth(false) + bp.getPaddingEnd(false);
  327. // Offset of content rectangle in the block-progression direction
  328. m_borderSeparation =
  329. properties.get("border-separation.block-progression-direction").getLength().mvalue();
  330. this.beforeOffset = m_borderSeparation / 2
  331. + bp.getBorderTopWidth(false)
  332. + bp.getPaddingTop(false);
  333. // bp.getBorderBeforeWidth(false) + bp.getPaddingBefore(false);
  334. } else {
  335. // System.err.println("Collapse borders");
  336. /*
  337. * Hard case.
  338. * Cell border is combination of other cell borders, or table
  339. * border for edge cells. Also seems to border values specified
  340. * on row and column FO in the table (if I read CR correclty.)
  341. */
  342. // Set up before and after borders, taking into account row
  343. // and table border properties.
  344. // ??? What about table-body, header,footer
  345. /*
  346. * We can't calculate before and after because we aren't sure
  347. * whether this row will be the first or last in its area, due
  348. * to redoing break decisions (at least in the "new" architecture.)
  349. * So in the general case, we will calculate two possible values:
  350. * the first/last one and the "middle" one.
  351. * Example: border-before
  352. * 1. If the cell is in the first row in the first table body, it
  353. * will combine with the last row of the header, or with the
  354. * top (before) table border if there is no header.
  355. * 2. Otherwise there are two cases:
  356. * a. the row is first in its (non-first) Area.
  357. * The border can combine with either:
  358. * i. the last row of table-header and its cells, or
  359. * ii. the table before border (no table-header or it is
  360. * omitted on non-first Areas).
  361. * b. the row isn't first in its Area.
  362. * The border combines with the border of the previous
  363. * row and the cells which end in that row.
  364. */
  365. /*
  366. * if-first
  367. * Calculate the effective border of the cell before-border,
  368. * it's parent row before-border, the last header row after-border,
  369. * the after border of the cell(s) which end in the last header
  370. * row.
  371. */
  372. /*
  373. * if-not-first
  374. * Calculate the effective border of the cell before-border,
  375. * it's parent row before-border, the previous row after-border,
  376. * the after border of the cell(s) which end in the previous
  377. * row.
  378. */
  379. /* ivan demakov */
  380. int borderStart = bp.getBorderLeftWidth(false);
  381. int borderEnd = bp.getBorderRightWidth(false);
  382. int borderBefore = bp.getBorderTopWidth(false);
  383. int borderAfter = bp.getBorderBottomWidth(false);
  384. this.startAdjust = borderStart / 2 + bp.getPaddingLeft(false);
  385. this.widthAdjust = startAdjust + borderEnd / 2
  386. + bp.getPaddingRight(false);
  387. this.beforeOffset = borderBefore / 2 + bp.getPaddingTop(false);
  388. // Half border height to fix overestimate of area size!
  389. this.borderHeight = (borderBefore + borderAfter) / 2;
  390. }
  391. }
  392. }