1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
|
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/* $Id$ */
package org.apache.fop.fo.flow.table;
import java.util.LinkedList;
import java.util.List;
import org.apache.fop.fo.properties.CommonBorderPaddingBackground;
import org.apache.fop.layoutmgr.ElementListUtils;
import org.apache.fop.layoutmgr.table.TableCellLayoutManager;
/**
* This class represents a primary grid unit of a spanned cell. This is the "before-start"
* (top-left, usually) grid unit of the span.
*/
public class PrimaryGridUnit extends GridUnit {
/** Cell layout manager. */
private TableCellLayoutManager cellLM;
/** List of Knuth elements representing the contents of the cell. */
private LinkedList elements;
/** Index of the row where this cell starts. */
private int rowIndex;
/** Index of the column where this cell starts. */
private int colIndex;
/** Links to the spanned grid units. (List of GridUnit arrays, one array represents a row) */
private List rows;
/** The calculated size of the cell's content. (cached value) */
private int contentLength = -1;
private boolean isSeparateBorderModel;
private int halfBorderSeparationBPD;
/**
* Creates a new primary grid unit.
*
* @param cell table cell which occupies this grid unit
* @param row the table-row element this grid unit belongs to (if any)
* @param colIndex index of the column this grid unit belongs to, zero-based
*/
PrimaryGridUnit(TableCell cell, TableRow row, int colIndex) {
super(cell, row, 0, 0);
this.colIndex = colIndex;
this.isSeparateBorderModel = cell.getTable().isSeparateBorderModel(); // TODO
this.halfBorderSeparationBPD = cell.getTable().getBorderSeparation().getBPD().getLength()
.getValue() / 2; // TODO
}
public TableCellLayoutManager getCellLM() {
assert cellLM != null;
return cellLM;
}
/** {@inheritDoc} */
public PrimaryGridUnit getPrimary() {
return this;
}
/** {@inheritDoc} */
public boolean isPrimary() {
return true;
}
/**
* Sets the Knuth elements for the table cell containing this grid unit.
*
* @param elements a list of ListElement (?)
*/
public void setElements(LinkedList elements) {
this.elements = elements;
}
public LinkedList getElements() {
return this.elements;
}
/**
* Returns the widths of the border-before and -after for this cell. In the separate
* border model the border-separation is included. In the collapsing model only half
* of them is counted, since the other halves belong to the neighbouring cells; also,
* the returned value is the maximum of the segments of each applicable grid unit.
*
* @return the sum of the before and after border widths
*/
public int getBeforeAfterBorderWidth() {
return getBeforeBorderWidth(0, ConditionalBorder.NORMAL)
+ getAfterBorderWidth(ConditionalBorder.NORMAL);
}
/**
* Returns the width of the before-border for the given row-span of this cell. In the
* separate border model half of the border-separation is included. In the collapsing
* model only half of the border is counted, since the other half belongs to the
* preceding cell; also, the returned value is the maximum of the segments of each
* applicable grid unit.
*
* @param rowIndex index of the span for which the border must be computed, 0-based
* @param which one of {@link ConditionalBorder#NORMAL},
* {@link ConditionalBorder#LEADING_TRAILING} or {@link ConditionalBorder#REST}
* @return the before border width
*/
public int getBeforeBorderWidth(int rowIndex, int which) {
if (isSeparateBorderModel) {
if (getCell() == null) {
return 0;
} else {
CommonBorderPaddingBackground cellBorders = getCell()
.getCommonBorderPaddingBackground();
switch (which) {
case ConditionalBorder.NORMAL:
case ConditionalBorder.LEADING_TRAILING:
return cellBorders.getBorderBeforeWidth(false) + halfBorderSeparationBPD;
case ConditionalBorder.REST:
if (cellBorders.getBorderInfo(CommonBorderPaddingBackground.BEFORE).getWidth()
.isDiscard()) {
return 0;
} else {
return cellBorders.getBorderBeforeWidth(true) + halfBorderSeparationBPD;
}
default:
assert false;
return 0;
}
}
} else {
int width = 0;
GridUnit[] row = (GridUnit[]) rows.get(rowIndex);
for (int i = 0; i < row.length; i++) {
width = Math.max(width,
row[i].getBorderBefore(which).getRetainedWidth());
}
return width / 2;
}
}
/**
* Returns the width of the before-after for the given row-span of this cell. In the
* separate border model half of the border-separation is included. In the collapsing
* model only half of the border is counted, since the other half belongs to the
* following cell; also, the returned value is the maximum of the segments of each
* applicable grid unit.
*
* @param rowIndex index of the span for which the border must be computed, 0-based
* @param which one of {@link ConditionalBorder#NORMAL},
* {@link ConditionalBorder#LEADING_TRAILING} or {@link ConditionalBorder#REST}
* @return the after border width
*/
public int getAfterBorderWidth(int rowIndex, int which) {
if (isSeparateBorderModel) {
if (getCell() == null) {
return 0;
} else {
CommonBorderPaddingBackground cellBorders = getCell()
.getCommonBorderPaddingBackground();
switch (which) {
case ConditionalBorder.NORMAL:
case ConditionalBorder.LEADING_TRAILING:
return cellBorders.getBorderAfterWidth(false) + halfBorderSeparationBPD;
case ConditionalBorder.REST:
if (cellBorders.getBorderInfo(CommonBorderPaddingBackground.AFTER).getWidth()
.isDiscard()) {
return 0;
} else {
return cellBorders.getBorderAfterWidth(true) + halfBorderSeparationBPD;
}
default:
assert false;
return 0;
}
}
} else {
int width = 0;
GridUnit[] row = (GridUnit[]) rows.get(rowIndex);
for (int i = 0; i < row.length; i++) {
width = Math.max(width,
row[i].getBorderAfter(which).getRetainedWidth());
}
return width / 2;
}
}
/**
* Returns the width of the before-after for the last row-span of this cell. See
* {@link #getAfterBorderWidth(int, int)}.
*
* @param which one of {@link ConditionalBorder#NORMAL},
* {@link ConditionalBorder#LEADING_TRAILING} or {@link ConditionalBorder#REST}
* @return the after border width
*/
public int getAfterBorderWidth(int which) {
return getAfterBorderWidth(getCell().getNumberRowsSpanned() - 1, which);
}
/** @return the length of the cell content. */
public int getContentLength() {
if (contentLength < 0) {
contentLength = ElementListUtils.calcContentLength(elements);
}
return contentLength;
}
/** @return true if cell/row has an explicit BPD/height */
public boolean hasBPD() {
if (!getCell().getBlockProgressionDimension().getOptimum(null).isAuto()) {
return true;
}
if (getRow() != null
&& !getRow().getBlockProgressionDimension().getOptimum(null).isAuto()) {
return true;
}
return false;
}
/**
* Returns the grid units belonging to the same span as this one.
*
* @return a list of GridUnit[], each array corresponds to a row
*/
public List getRows() {
return this.rows;
}
public void addRow(GridUnit[] row) {
if (rows == null) {
rows = new java.util.ArrayList();
}
rows.add(row);
}
void setRowIndex(int rowIndex) {
this.rowIndex = rowIndex;
}
/**
* Returns the index of the row this grid unit belongs to. This is the index, in the
* enclosing table part, of the first row spanned by the cell. Note that if the table
* has several table-body children, then the index grows continuously across them;
* they are considered to form one single part, the "body of the table".
*
* @return the index of the row this grid unit belongs to, 0-based.
*/
public int getRowIndex() {
return rowIndex;
}
/**
* Returns the index of the column this grid unit belongs to.
*
* @return the column index, 0-based
*/
public int getColIndex() {
return colIndex;
}
/**
* Returns the widths of the start- and end-borders of the span this grid unit belongs
* to.
*
* @return a two-element array containing the widths of the start-border then the
* end-border
*/
public int[] getStartEndBorderWidths() {
int[] widths = new int[2];
if (getCell() == null) {
return widths;
} else if (getCell().getTable().isSeparateBorderModel()) {
widths[0] = getCell().getCommonBorderPaddingBackground().getBorderStartWidth(false);
widths[1] = getCell().getCommonBorderPaddingBackground().getBorderEndWidth(false);
} else {
for (int i = 0; i < rows.size(); i++) {
GridUnit[] gridUnits = (GridUnit[])rows.get(i);
widths[0] = Math.max(widths[0],
gridUnits[0].borderStart.getBorderInfo().getRetainedWidth());
widths[1] = Math.max(widths[1], gridUnits[gridUnits.length - 1].borderEnd
.getBorderInfo().getRetainedWidth());
}
}
return widths;
}
/** {@inheritDoc} */
public String toString() {
StringBuffer sb = new StringBuffer(super.toString());
sb.append(" rowIndex=").append(rowIndex);
sb.append(" colIndex=").append(colIndex);
return sb.toString();
}
/** @return true if this cell spans over more than one grid unit. */
public boolean hasSpanning() {
return (getCell().getNumberColumnsSpanned() > 1)
|| (getCell().getNumberRowsSpanned() > 1);
}
/**
* Creates a cellLM for the corresponding table-cell. A new one must be created
* for each new static-content (TODO).
*/
public void createCellLM() {
cellLM = new TableCellLayoutManager(cell, this);
}
}
|