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.

GridUnit.java 16KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475
  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.fo.flow.table;
  19. import org.apache.fop.fo.properties.CommonBorderPaddingBackground;
  20. import org.apache.fop.fo.properties.CommonBorderPaddingBackground.BorderInfo;
  21. import org.apache.fop.layoutmgr.table.CollapsingBorderModel;
  22. /**
  23. * This class represents one grid unit inside a table.
  24. */
  25. public class GridUnit {
  26. /**
  27. * Indicates that the grid unit is in the first row of the table part (header, footer,
  28. * body).
  29. */
  30. public static final int FIRST_IN_PART = 0;
  31. /**
  32. * Indicates that the grid unit is in the last row of the table part (header, footer,
  33. * body).
  34. */
  35. public static final int LAST_IN_PART = 1;
  36. /** Indicates that the primary grid unit has a pending keep-with-next. */
  37. public static final int KEEP_WITH_NEXT_PENDING = 2;
  38. /** Indicates that the primary grid unit has a pending keep-with-previous. */
  39. public static final int KEEP_WITH_PREVIOUS_PENDING = 3;
  40. /** Primary grid unit */
  41. private PrimaryGridUnit primary;
  42. /** Table cell which occupies this grid unit */
  43. protected TableCell cell;
  44. /** Table row occupied by this grid unit (may be null). */
  45. private TableRow row;
  46. /** index of grid unit within cell in column direction */
  47. private int colSpanIndex;
  48. /** index of grid unit within cell in row direction */
  49. private int rowSpanIndex;
  50. /** flags for the grid unit */
  51. private byte flags = 0;
  52. /** the border-before specification */
  53. ConditionalBorder borderBefore;
  54. /** the border-after specification */
  55. ConditionalBorder borderAfter;
  56. /** the border-start specification */
  57. BorderSpecification borderStart;
  58. /** the border-end specification */
  59. BorderSpecification borderEnd;
  60. /** The border model helper associated with the table */
  61. protected CollapsingBorderModel collapsingBorderModel;
  62. /**
  63. * Creates a new grid unit.
  64. *
  65. * @param table the containing table
  66. * @param colSpanIndex index of this grid unit in the span, in column direction
  67. * @param rowSpanIndex index of this grid unit in the span, in row direction
  68. */
  69. protected GridUnit(Table table, int colSpanIndex, int rowSpanIndex) {
  70. this(colSpanIndex, rowSpanIndex);
  71. setBorders(table);
  72. }
  73. /**
  74. * Creates a new grid unit.
  75. *
  76. * @param cell table cell which occupies this grid unit
  77. * @param colSpanIndex index of this grid unit in the span, in column direction
  78. * @param rowSpanIndex index of this grid unit in the span, in row direction
  79. */
  80. protected GridUnit(TableCell cell, int colSpanIndex, int rowSpanIndex) {
  81. this(colSpanIndex, rowSpanIndex);
  82. this.cell = cell;
  83. setBorders(cell.getTable());
  84. }
  85. /**
  86. * Creates a new grid unit.
  87. *
  88. * @param primary the before-start grid unit of the cell containing this grid unit
  89. * @param colSpanIndex index of this grid unit in the span, in column direction
  90. * @param rowSpanIndex index of this grid unit in the span, in row direction
  91. */
  92. GridUnit(PrimaryGridUnit primary, int colSpanIndex, int rowSpanIndex) {
  93. this(primary.getCell(), colSpanIndex, rowSpanIndex);
  94. this.primary = primary;
  95. }
  96. private GridUnit(int colSpanIndex, int rowSpanIndex) {
  97. this.colSpanIndex = colSpanIndex;
  98. this.rowSpanIndex = rowSpanIndex;
  99. }
  100. private void setBorders(Table table/*TODO*/) {
  101. if (!table.isSeparateBorderModel()) {
  102. collapsingBorderModel = CollapsingBorderModel.getBorderModelFor(table
  103. .getBorderCollapse());
  104. setBordersFromCell();
  105. }
  106. }
  107. /**
  108. * Prepares the borders of this grid unit for upcoming resolution, in the collapsing
  109. * model.
  110. */
  111. protected void setBordersFromCell() {
  112. borderBefore = cell.borderBefore.copy();
  113. if (rowSpanIndex > 0) {
  114. borderBefore.normal = BorderSpecification.getDefaultBorder();
  115. }
  116. borderAfter = cell.borderAfter.copy();
  117. if (!isLastGridUnitRowSpan()) {
  118. borderAfter.normal = BorderSpecification.getDefaultBorder();
  119. }
  120. if (colSpanIndex == 0) {
  121. borderStart = cell.borderStart;
  122. } else {
  123. borderStart = BorderSpecification.getDefaultBorder();
  124. }
  125. if (isLastGridUnitColSpan()) {
  126. borderEnd = cell.borderEnd;
  127. } else {
  128. borderEnd = BorderSpecification.getDefaultBorder();
  129. }
  130. }
  131. /**
  132. * Returns the table cell associated with this grid unit.
  133. * @return the table cell
  134. */
  135. public TableCell getCell() {
  136. return cell;
  137. }
  138. /**
  139. * Returns the fo:table-row element (if any) this grid unit belongs to.
  140. *
  141. * @return the row containing this grid unit, or null if there is no fo:table-row
  142. * element in the corresponding table-part
  143. */
  144. public TableRow getRow() {
  145. return row;
  146. }
  147. void setRow(TableRow row) {
  148. this.row = row;
  149. }
  150. /**
  151. * Returns the before-start grid unit of the cell containing this grid unit.
  152. *
  153. * @return the before-start grid unit of the cell containing this grid unit.
  154. */
  155. public PrimaryGridUnit getPrimary() {
  156. return primary;
  157. }
  158. /**
  159. * Is this grid unit the before-start grid unit of the cell?
  160. *
  161. * @return true if this grid unit is the before-start grid unit of the cell
  162. */
  163. public boolean isPrimary() {
  164. return false;
  165. }
  166. /**
  167. * Does this grid unit belong to an empty cell?
  168. *
  169. * @return true if this grid unit belongs to an empty cell
  170. */
  171. public boolean isEmpty() {
  172. return cell == null;
  173. }
  174. /** @return true if the grid unit is the last in column spanning direction */
  175. public boolean isLastGridUnitColSpan() {
  176. return (colSpanIndex == cell.getNumberColumnsSpanned() - 1);
  177. }
  178. /** @return true if the grid unit is the last in row spanning direction */
  179. public boolean isLastGridUnitRowSpan() {
  180. return (rowSpanIndex == cell.getNumberRowsSpanned() - 1);
  181. }
  182. /**
  183. * @return the index of the grid unit inside a cell in row direction
  184. */
  185. public int getRowSpanIndex() {
  186. return rowSpanIndex;
  187. }
  188. /**
  189. * @return the index of the grid unit inside a cell in column direction
  190. */
  191. public int getColSpanIndex() {
  192. return colSpanIndex;
  193. }
  194. /**
  195. * Returns the resolved border-before of this grid unit, in the collapsing-border
  196. * model.
  197. *
  198. * @param which one of {@link ConditionalBorder#NORMAL},
  199. * {@link ConditionalBorder#LEADING_TRAILING} or {@link ConditionalBorder#REST}
  200. * @return the corresponding border
  201. */
  202. public BorderInfo getBorderBefore(int which) {
  203. switch (which) {
  204. case ConditionalBorder.NORMAL:
  205. return borderBefore.normal.getBorderInfo();
  206. case ConditionalBorder.LEADING_TRAILING:
  207. return borderBefore.leadingTrailing.getBorderInfo();
  208. case ConditionalBorder.REST:
  209. return borderBefore.rest.getBorderInfo();
  210. default:
  211. assert false;
  212. return null;
  213. }
  214. }
  215. /**
  216. * Returns the resolved border-after of this grid unit, in the collapsing-border
  217. * model.
  218. *
  219. * @param which one of {@link ConditionalBorder#NORMAL},
  220. * {@link ConditionalBorder#LEADING_TRAILING} or {@link ConditionalBorder#REST}
  221. * @return the corresponding border
  222. */
  223. public BorderInfo getBorderAfter(int which) {
  224. switch (which) {
  225. case ConditionalBorder.NORMAL:
  226. return borderAfter.normal.getBorderInfo();
  227. case ConditionalBorder.LEADING_TRAILING:
  228. return borderAfter.leadingTrailing.getBorderInfo();
  229. case ConditionalBorder.REST:
  230. return borderAfter.rest.getBorderInfo();
  231. default:
  232. assert false;
  233. return null;
  234. }
  235. }
  236. /**
  237. * Returns the resolved border-start of this grid unit, in the collapsing-border
  238. * model.
  239. *
  240. * @return the corresponding border
  241. */
  242. public BorderInfo getBorderStart() {
  243. return borderStart.getBorderInfo();
  244. }
  245. /**
  246. * Returns the resolved border-end of this grid unit, in the collapsing-border
  247. * model.
  248. *
  249. * @return the corresponding border
  250. */
  251. public BorderInfo getBorderEnd() {
  252. return borderEnd.getBorderInfo();
  253. }
  254. /**
  255. * Resolve collapsing borders for the given cell. Used in case of the collapsing
  256. * border model.
  257. *
  258. * @param other neighbouring grid unit
  259. * @param side the side to resolve (one of
  260. * CommonBorderPaddingBackground.BEFORE|AFTER|START|END)
  261. */
  262. void resolveBorder(GridUnit other, int side) {
  263. switch (side) {
  264. case CommonBorderPaddingBackground.BEFORE:
  265. borderBefore.resolve(other.borderAfter, true, false, false);
  266. break;
  267. case CommonBorderPaddingBackground.AFTER:
  268. borderAfter.resolve(other.borderBefore, true, false, false);
  269. break;
  270. case CommonBorderPaddingBackground.START:
  271. BorderSpecification resolvedBorder = collapsingBorderModel.determineWinner(
  272. borderStart, other.borderEnd);
  273. if (resolvedBorder != null) {
  274. this.borderStart = resolvedBorder;
  275. other.borderEnd = resolvedBorder;
  276. }
  277. break;
  278. case CommonBorderPaddingBackground.END:
  279. resolvedBorder = collapsingBorderModel.determineWinner(
  280. borderEnd, other.borderStart);
  281. if (resolvedBorder != null) {
  282. this.borderEnd = resolvedBorder;
  283. other.borderStart = resolvedBorder;
  284. }
  285. break;
  286. default: assert false;
  287. }
  288. }
  289. /**
  290. * For the given side, integrates in the conflict resolution the border segment of the
  291. * given parent element.
  292. *
  293. * @param side the side to consider (either CommonBorderPaddingBackground.BEFORE or
  294. * AFTER)
  295. * @param parent a table element whose corresponding border coincides on the given
  296. * side
  297. */
  298. void integrateBorderSegment(int side, TableFObj parent, boolean withNormal,
  299. boolean withLeadingTrailing, boolean withRest) {
  300. switch (side) {
  301. case CommonBorderPaddingBackground.BEFORE:
  302. borderBefore.integrateSegment(parent.borderBefore, withNormal,
  303. withLeadingTrailing, withRest);
  304. break;
  305. case CommonBorderPaddingBackground.AFTER:
  306. borderAfter.integrateSegment(parent.borderAfter, withNormal,
  307. withLeadingTrailing, withRest);
  308. break;
  309. default: assert false;
  310. }
  311. }
  312. /**
  313. * For the given side, integrates in the conflict resolution the border segment of the
  314. * given parent element.
  315. *
  316. * @param side the side to consider (one of
  317. * CommonBorderPaddingBackground.BEFORE|AFTER|START|END)
  318. * @param parent a table element whose corresponding border coincides on the given side
  319. */
  320. void integrateBorderSegment(int side, TableFObj parent) {
  321. switch (side) {
  322. case CommonBorderPaddingBackground.BEFORE:
  323. case CommonBorderPaddingBackground.AFTER:
  324. integrateBorderSegment(side, parent, true, true, true);
  325. break;
  326. case CommonBorderPaddingBackground.START:
  327. borderStart = collapsingBorderModel.determineWinner(borderStart,
  328. parent.borderStart);
  329. break;
  330. case CommonBorderPaddingBackground.END:
  331. borderEnd = collapsingBorderModel.determineWinner(borderEnd,
  332. parent.borderEnd);
  333. break;
  334. default: assert false;
  335. }
  336. }
  337. /**
  338. * For the given side, integrates in the conflict resolution the given border segment.
  339. *
  340. * @param side the side to consider (one of CommonBorderPaddingBackground.START|END)
  341. * @param segment a border specification to integrate at the given side
  342. */
  343. void integrateBorderSegment(int side, BorderSpecification segment) {
  344. switch(side) {
  345. case CommonBorderPaddingBackground.START:
  346. borderStart = collapsingBorderModel.determineWinner(borderStart, segment);
  347. break;
  348. case CommonBorderPaddingBackground.END:
  349. borderEnd = collapsingBorderModel.determineWinner(borderEnd, segment);
  350. break;
  351. default: assert false;
  352. }
  353. }
  354. void integrateCompetingBorder(int side, ConditionalBorder competitor,
  355. boolean withNormal, boolean withLeadingTrailing, boolean withRest) {
  356. switch (side) {
  357. case CommonBorderPaddingBackground.BEFORE:
  358. borderBefore.integrateCompetingSegment(competitor, withNormal,
  359. withLeadingTrailing, withRest);
  360. break;
  361. case CommonBorderPaddingBackground.AFTER:
  362. borderAfter.integrateCompetingSegment(competitor, withNormal,
  363. withLeadingTrailing, withRest);
  364. break;
  365. default: assert false;
  366. }
  367. }
  368. /**
  369. * Returns a flag for this GridUnit.
  370. *
  371. * @param which the requested flag
  372. * @return the value of the flag
  373. */
  374. public boolean getFlag(int which) {
  375. return (flags & (1 << which)) != 0;
  376. }
  377. /**
  378. * Sets a flag on a GridUnit.
  379. *
  380. * @param which the flag to set
  381. * @param value the new value for the flag
  382. */
  383. public void setFlag(int which, boolean value) {
  384. if (value) {
  385. flags |= (1 << which); // set flag
  386. } else {
  387. flags &= ~(1 << which); // clear flag
  388. }
  389. }
  390. /**
  391. * Sets the given flag on this grid unit.
  392. *
  393. * @param which the flag to set
  394. */
  395. public void setFlag(int which) {
  396. setFlag(which, true);
  397. }
  398. /** {@inheritDoc} */
  399. public String toString() {
  400. StringBuffer buffer = new StringBuffer();
  401. if (isEmpty()) {
  402. buffer.append("EMPTY");
  403. } else if (isPrimary()) {
  404. buffer.append("Primary");
  405. }
  406. buffer.append("GridUnit:");
  407. if (colSpanIndex > 0) {
  408. buffer.append(" colSpan=").append(colSpanIndex);
  409. if (isLastGridUnitColSpan()) {
  410. buffer.append("(last)");
  411. }
  412. }
  413. if (rowSpanIndex > 0) {
  414. buffer.append(" rowSpan=").append(rowSpanIndex);
  415. if (isLastGridUnitRowSpan()) {
  416. buffer.append("(last)");
  417. }
  418. }
  419. if (!isPrimary() && getPrimary() != null) {
  420. buffer.append(" primary=").append(getPrimary().getRowIndex());
  421. buffer.append("/").append(getPrimary().getColIndex());
  422. if (getPrimary().getCell() != null) {
  423. buffer.append(" id=" + getPrimary().getCell().getId());
  424. }
  425. }
  426. buffer.append(" flags=").append(Integer.toBinaryString(flags));
  427. return buffer.toString();
  428. }
  429. }