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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358
  1. /*
  2. * $Id$
  3. * ============================================================================
  4. * The Apache Software License, Version 1.1
  5. * ============================================================================
  6. *
  7. * Copyright (C) 1999-2003 The Apache Software Foundation. All rights reserved.
  8. *
  9. * Redistribution and use in source and binary forms, with or without modifica-
  10. * tion, are permitted provided that the following conditions are met:
  11. *
  12. * 1. Redistributions of source code must retain the above copyright notice,
  13. * this list of conditions and the following disclaimer.
  14. *
  15. * 2. Redistributions in binary form must reproduce the above copyright notice,
  16. * this list of conditions and the following disclaimer in the documentation
  17. * and/or other materials provided with the distribution.
  18. *
  19. * 3. The end-user documentation included with the redistribution, if any, must
  20. * include the following acknowledgment: "This product includes software
  21. * developed by the Apache Software Foundation (http://www.apache.org/)."
  22. * Alternately, this acknowledgment may appear in the software itself, if
  23. * and wherever such third-party acknowledgments normally appear.
  24. *
  25. * 4. The names "FOP" and "Apache Software Foundation" must not be used to
  26. * endorse or promote products derived from this software without prior
  27. * written permission. For written permission, please contact
  28. * apache@apache.org.
  29. *
  30. * 5. Products derived from this software may not be called "Apache", nor may
  31. * "Apache" appear in their name, without prior written permission of the
  32. * Apache Software Foundation.
  33. *
  34. * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES,
  35. * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
  36. * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  37. * APACHE SOFTWARE FOUNDATION OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
  38. * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLU-
  39. * DING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
  40. * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
  41. * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  42. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  43. * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  44. * ============================================================================
  45. *
  46. * This software consists of voluntary contributions made by many individuals
  47. * on behalf of the Apache Software Foundation and was originally created by
  48. * James Tauber <jtauber@jtauber.com>. For more information on the Apache
  49. * Software Foundation, please see <http://www.apache.org/>.
  50. */
  51. package org.apache.fop.fo.flow;
  52. // Java
  53. import java.util.List;
  54. // XML
  55. import org.xml.sax.Attributes;
  56. // FOP
  57. import org.apache.fop.apps.FOPException;
  58. import org.apache.fop.datatypes.ColorType;
  59. import org.apache.fop.fo.FONode;
  60. import org.apache.fop.fo.FObj;
  61. import org.apache.fop.fo.properties.BorderCollapse;
  62. import org.apache.fop.fo.properties.DisplayAlign;
  63. import org.apache.fop.layout.AccessibilityProps;
  64. import org.apache.fop.layout.AuralProps;
  65. import org.apache.fop.layout.BackgroundProps;
  66. import org.apache.fop.layout.BorderAndPadding;
  67. import org.apache.fop.layout.RelativePositionProps;
  68. import org.apache.fop.layoutmgr.table.Cell;
  69. public class TableCell extends FObj {
  70. // private int spaceBefore;
  71. // private int spaceAfter;
  72. private ColorType backgroundColor;
  73. private int numColumnsSpanned;
  74. private int numRowsSpanned;
  75. private int iColNumber = -1; // uninitialized
  76. /**
  77. * Offset of content rectangle in inline-progression-direction,
  78. * relative to table.
  79. */
  80. protected int startOffset;
  81. /**
  82. * Dimension of allocation rectangle in inline-progression-direction,
  83. * determined by the width of the column(s) occupied by the cell
  84. */
  85. protected int width;
  86. /**
  87. * Offset of content rectangle, in block-progression-direction,
  88. * relative to the row.
  89. */
  90. protected int beforeOffset = 0;
  91. /**
  92. * Offset of content rectangle, in inline-progression-direction,
  93. * relative to the column start edge.
  94. */
  95. protected int startAdjust = 0;
  96. /**
  97. * Adjust to theoretical column width to obtain content width
  98. * relative to the column start edge.
  99. */
  100. protected int widthAdjust = 0;
  101. /* For collapsed border style */
  102. protected int borderHeight = 0;
  103. /** Minimum ontent height of cell. */
  104. protected int minCellHeight = 0;
  105. /** Height of cell */
  106. protected int height = 0;
  107. /** Ypos of cell ??? */
  108. protected int top;
  109. protected int verticalAlign;
  110. protected boolean bRelativeAlign = false;
  111. // boolean setup = false;
  112. private boolean bSepBorders = true;
  113. /**
  114. * Set to true if all content completely laid out.
  115. */
  116. private boolean bDone = false;
  117. /**
  118. * Border separation value in the block-progression dimension.
  119. * Used in calculating cells height.
  120. */
  121. private int borderSeparation = 0;
  122. public TableCell(FONode parent) {
  123. super(parent);
  124. }
  125. public void handleAttrs(Attributes attlist) throws FOPException {
  126. super.handleAttrs(attlist);
  127. doSetup(); // init some basic property values
  128. }
  129. /**
  130. */
  131. public void addLayoutManager(List list) {
  132. Cell clm = new Cell();
  133. clm.setUserAgent(getUserAgent());
  134. clm.setFObj(this);
  135. list.add(clm);
  136. }
  137. /**
  138. * Set position relative to table (set by body?)
  139. */
  140. public void setStartOffset(int offset) {
  141. startOffset = offset;
  142. }
  143. // Initially same as the column width containg this cell or the
  144. // sum of the spanned columns if numColumnsSpanned > 1
  145. public void setWidth(int width) {
  146. this.width = width;
  147. }
  148. public int getColumnNumber() {
  149. return iColNumber;
  150. }
  151. public int getNumColumnsSpanned() {
  152. return numColumnsSpanned;
  153. }
  154. public int getNumRowsSpanned() {
  155. return numRowsSpanned;
  156. }
  157. public void doSetup() {
  158. // Common Accessibility Properties
  159. AccessibilityProps mAccProps = propMgr.getAccessibilityProps();
  160. // Common Aural Properties
  161. AuralProps mAurProps = propMgr.getAuralProps();
  162. // Common Border, Padding, and Background Properties
  163. BorderAndPadding bap = propMgr.getBorderAndPadding();
  164. BackgroundProps bProps = propMgr.getBackgroundProps();
  165. // Common Relative Position Properties
  166. RelativePositionProps mRelProps = propMgr.getRelativePositionProps();
  167. // this.properties.get("border-after-precedence");
  168. // this.properties.get("border-before-precendence");
  169. // this.properties.get("border-end-precendence");
  170. // this.properties.get("border-start-precendence");
  171. // this.properties.get("block-progression-dimension");
  172. // this.properties.get("column-number");
  173. // this.properties.get("display-align");
  174. // this.properties.get("relative-align");
  175. // this.properties.get("empty-cells");
  176. // this.properties.get("ends-row");
  177. // this.properties.get("height");
  178. setupID();
  179. // this.properties.get("number-columns-spanned");
  180. // this.properties.get("number-rows-spanned");
  181. // this.properties.get("starts-row");
  182. // this.properties.get("width");
  183. this.iColNumber =
  184. properties.get("column-number").getNumber().intValue();
  185. if (iColNumber < 0) {
  186. iColNumber = 0;
  187. }
  188. this.numColumnsSpanned =
  189. this.properties.get("number-columns-spanned").getNumber().intValue();
  190. if (numColumnsSpanned < 1) {
  191. numColumnsSpanned = 1;
  192. }
  193. this.numRowsSpanned =
  194. this.properties.get("number-rows-spanned").getNumber().intValue();
  195. if (numRowsSpanned < 1) {
  196. numRowsSpanned = 1;
  197. }
  198. this.backgroundColor =
  199. this.properties.get("background-color").getColorType();
  200. bSepBorders = (this.properties.get("border-collapse").getEnum()
  201. == BorderCollapse.SEPARATE);
  202. calcBorders(propMgr.getBorderAndPadding());
  203. // Vertical cell alignment
  204. verticalAlign = this.properties.get("display-align").getEnum();
  205. if (verticalAlign == DisplayAlign.AUTO) {
  206. // Depends on all cells starting in row
  207. bRelativeAlign = true;
  208. verticalAlign = this.properties.get("relative-align").getEnum();
  209. } else {
  210. bRelativeAlign = false; // Align on a per-cell basis
  211. }
  212. this.minCellHeight =
  213. this.properties.get("height").getLength().getValue();
  214. }
  215. /**
  216. * Calculate cell border and padding, including offset of content
  217. * rectangle from the theoretical grid position.
  218. */
  219. private void calcBorders(BorderAndPadding bp) {
  220. if (this.bSepBorders) {
  221. /*
  222. * Easy case.
  223. * Cell border is the property specified directly on cell.
  224. * Offset content rect by half the border-separation value,
  225. * in addition to the border and padding values. Note:
  226. * border-separate should only be specified on the table object,
  227. * but it inherits.
  228. */
  229. int iSep = properties.get(
  230. "border-separation.inline-progression-direction").getLength().getValue();
  231. this.startAdjust = iSep / 2 + bp.getBorderLeftWidth(false)
  232. + bp.getPaddingLeft(false);
  233. /*
  234. * int contentOffset = iSep + bp.getBorderStartWidth(false) +
  235. * bp.getPaddingStart(false);
  236. */
  237. this.widthAdjust = startAdjust + iSep - iSep / 2
  238. + bp.getBorderRightWidth(false)
  239. + bp.getPaddingRight(false);
  240. // bp.getBorderEndWidth(false) + bp.getPaddingEnd(false);
  241. // Offset of content rectangle in the block-progression direction
  242. borderSeparation = properties.get(
  243. "border-separation.block-progression-direction").getLength().getValue();
  244. this.beforeOffset = borderSeparation / 2
  245. + bp.getBorderTopWidth(false)
  246. + bp.getPaddingTop(false);
  247. // bp.getBorderBeforeWidth(false) + bp.getPaddingBefore(false);
  248. } else {
  249. // System.err.println("Collapse borders");
  250. /*
  251. * Hard case.
  252. * Cell border is combination of other cell borders, or table
  253. * border for edge cells. Also seems to border values specified
  254. * on row and column FO in the table (if I read CR correclty.)
  255. */
  256. // Set up before and after borders, taking into account row
  257. // and table border properties.
  258. // ??? What about table-body, header,footer
  259. /*
  260. * We can't calculate before and after because we aren't sure
  261. * whether this row will be the first or last in its area, due
  262. * to redoing break decisions (at least in the "new" architecture.)
  263. * So in the general case, we will calculate two possible values:
  264. * the first/last one and the "middle" one.
  265. * Example: border-before
  266. * 1. If the cell is in the first row in the first table body, it
  267. * will combine with the last row of the header, or with the
  268. * top (before) table border if there is no header.
  269. * 2. Otherwise there are two cases:
  270. * a. the row is first in its (non-first) Area.
  271. * The border can combine with either:
  272. * i. the last row of table-header and its cells, or
  273. * ii. the table before border (no table-header or it is
  274. * omitted on non-first Areas).
  275. * b. the row isn't first in its Area.
  276. * The border combines with the border of the previous
  277. * row and the cells which end in that row.
  278. */
  279. /*
  280. * if-first
  281. * Calculate the effective border of the cell before-border,
  282. * it's parent row before-border, the last header row after-border,
  283. * the after border of the cell(s) which end in the last header
  284. * row.
  285. */
  286. /*
  287. * if-not-first
  288. * Calculate the effective border of the cell before-border,
  289. * it's parent row before-border, the previous row after-border,
  290. * the after border of the cell(s) which end in the previous
  291. * row.
  292. */
  293. /* ivan demakov */
  294. int borderStart = bp.getBorderLeftWidth(false);
  295. int borderEnd = bp.getBorderRightWidth(false);
  296. int borderBefore = bp.getBorderTopWidth(false);
  297. int borderAfter = bp.getBorderBottomWidth(false);
  298. this.startAdjust = borderStart / 2 + bp.getPaddingLeft(false);
  299. this.widthAdjust = startAdjust + borderEnd / 2
  300. + bp.getPaddingRight(false);
  301. this.beforeOffset = borderBefore / 2 + bp.getPaddingTop(false);
  302. // Half border height to fix overestimate of area size!
  303. this.borderHeight = (borderBefore + borderAfter) / 2;
  304. }
  305. }
  306. protected boolean containsMarkers() {
  307. return true;
  308. }
  309. }