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
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
|
/*
* Copyright 1999-2005 The Apache Software Foundation.
*
* Licensed 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: TableLayoutManager.java,v 1.8 2004/03/21 12:03:08 gmazza Exp $ */
package org.apache.fop.layoutmgr.table;
import org.apache.fop.datatypes.Length;
import org.apache.fop.datatypes.PercentBase;
import org.apache.fop.fo.flow.Table;
import org.apache.fop.fo.flow.TableColumn;
import org.apache.fop.fo.properties.TableColLength;
import org.apache.fop.layoutmgr.BlockLevelLayoutManager;
import org.apache.fop.layoutmgr.BlockStackingLayoutManager;
import org.apache.fop.layoutmgr.KnuthElement;
import org.apache.fop.layoutmgr.KnuthGlue;
import org.apache.fop.layoutmgr.KnuthPenalty;
import org.apache.fop.layoutmgr.LayoutManager;
import org.apache.fop.layoutmgr.LeafPosition;
import org.apache.fop.layoutmgr.LayoutContext;
import org.apache.fop.layoutmgr.PositionIterator;
import org.apache.fop.layoutmgr.Position;
import org.apache.fop.layoutmgr.TraitSetter;
import org.apache.fop.area.Area;
import org.apache.fop.area.Block;
import org.apache.fop.traits.MinOptMax;
import org.apache.fop.traits.SpaceVal;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
/**
* LayoutManager for a table FO.
* A table consists of oldColumns, table header, table footer and multiple
* table bodies.
* The header, footer and body add the areas created from the table cells.
* The table then creates areas for the oldColumns, bodies and rows
* the render background.
*/
public class TableLayoutManager extends BlockStackingLayoutManager
implements BlockLevelLayoutManager {
private Table fobj;
private TableContentLayoutManager contentLM;
private ColumnSetup columns = null;
private Block curBlockArea;
private int referenceIPD;
private boolean autoLayout = true;
//TODO space-before|after: handle space-resolution rules
private MinOptMax spaceBefore;
private MinOptMax spaceAfter;
private class SectionPosition extends LeafPosition {
protected List list;
protected SectionPosition(LayoutManager lm, int pos, List l) {
super(lm, pos);
list = l;
}
}
/**
* Create a new table layout manager.
* @param node the table FO
*/
public TableLayoutManager(Table node) {
super(node);
fobj = node;
this.columns = new ColumnSetup(node);
}
/** @return the table FO */
public Table getTable() {
return this.fobj;
}
/**
* @return the column setup for this table.
*/
public ColumnSetup getColumns() {
return this.columns;
}
/** @see org.apache.fop.layoutmgr.AbstractLayoutManager#initProperties() */
protected void initProperties() {
super.initProperties();
spaceBefore = new SpaceVal(fobj.getCommonMarginBlock().spaceBefore).getSpace();
spaceAfter = new SpaceVal(fobj.getCommonMarginBlock().spaceAfter).getSpace();
if (!fobj.isAutoLayout()
&& fobj.getInlineProgressionDimension().getOptimum().getEnum() != EN_AUTO) {
autoLayout = false;
}
}
private int getIPIndents() {
int iIndents = 0;
iIndents += fobj.getCommonMarginBlock().startIndent.getValue();
iIndents += fobj.getCommonMarginBlock().endIndent.getValue();
return iIndents;
}
/**
* @see org.apache.fop.layoutmgr.LayoutManager#getNextKnuthElements(org.apache.fop.layoutmgr.LayoutContext, int)
*/
public LinkedList getNextKnuthElements(LayoutContext context, int alignment) {
//Body curLM; // currently active LM
referenceIPD = context.getRefIPD();
if (fobj.getInlineProgressionDimension().getOptimum().getEnum() != EN_AUTO) {
referenceIPD = fobj.getInlineProgressionDimension().getOptimum().getLength().getValue();
}
if (referenceIPD > context.getRefIPD()) {
log.warn("Allocated IPD exceeds available reference IPD");
}
int contentIPD = referenceIPD - getIPIndents();
MinOptMax stackSize = new MinOptMax();
//Add spacing
if (spaceAfter != null) {
stackSize.add(spaceAfter);
}
if (spaceBefore != null) {
stackSize.add(spaceBefore);
}
fobj.setLayoutDimension(PercentBase.BLOCK_IPD, referenceIPD);
fobj.setLayoutDimension(PercentBase.BLOCK_BPD, context.getStackLimit().opt);
fobj.setLayoutDimension(PercentBase.REFERENCE_AREA_IPD, referenceIPD);
fobj.setLayoutDimension(PercentBase.REFERENCE_AREA_BPD, context.getStackLimit().opt);
// either works out table of column widths or if proportional-column-width function
// is used works out total factor, so that value of single unit can be computed.
int sumCols = 0;
float factors = 0;
for (Iterator i = columns.iterator(); i.hasNext(); ) {
TableColumn column = (TableColumn) i.next();
Length width = column.getColumnWidth();
sumCols += width.getValue();
if (width instanceof TableColLength) {
factors += ((TableColLength) width).getTableUnits();
}
}
// sets TABLE_UNITS in case where one or more oldColumns is defined using
// proportional-column-width
if (sumCols < contentIPD) {
if (fobj.getLayoutDimension(PercentBase.TABLE_UNITS).floatValue() == 0.0) {
fobj.setLayoutDimension(PercentBase.TABLE_UNITS,
(contentIPD - sumCols) / factors);
}
}
LinkedList returnedList = null;
LinkedList contentList = new LinkedList();
LinkedList returnList = new LinkedList();
//Position returnPosition = new NonLeafPosition(this, null);
//Body prevLM = null;
LayoutContext childLC = new LayoutContext(0);
childLC.setStackLimit(
MinOptMax.subtract(context.getStackLimit(),
stackSize));
childLC.setRefIPD(context.getRefIPD());
contentLM = new TableContentLayoutManager(this);
returnedList = contentLM.getNextKnuthElements(childLC, alignment);
if (childLC.isKeepWithNextPending()) {
log.debug("TableContentLM signals pending keep-with-next");
context.setFlags(LayoutContext.KEEP_WITH_NEXT_PENDING);
}
if (childLC.isKeepWithPreviousPending()) {
log.debug("TableContentLM signals pending keep-with-previous");
context.setFlags(LayoutContext.KEEP_WITH_PREVIOUS_PENDING);
}
//Set index values on elements coming from the content LM
Iterator iter = returnedList.iterator();
while (iter.hasNext()) {
KnuthElement el = (KnuthElement)iter.next();
notifyPos(el.getPosition());
}
log.debug(returnedList);
if (returnedList.size() == 1
&& ((KnuthElement) returnedList.getFirst()).isPenalty()
&& ((KnuthPenalty) returnedList.getFirst()).getP() == -KnuthElement.INFINITE) {
// a descendant of this block has break-before
if (returnList.size() == 0) {
// the first child (or its first child ...) has
// break-before;
// all this block, including space before, will be put in
// the
// following page
//FIX ME
//bSpaceBeforeServed = false;
}
contentList.addAll(returnedList);
// "wrap" the Position inside each element
// moving the elements from contentList to returnList
returnedList = new LinkedList();
wrapPositionElements(contentList, returnList);
return returnList;
} else {
/*
if (prevLM != null) {
// there is a block handled by prevLM
// before the one handled by curLM
if (mustKeepTogether()
|| prevLM.mustKeepWithNext()
|| curLM.mustKeepWithPrevious()) {
// add an infinite penalty to forbid a break between
// blocks
contentList.add(new KnuthPenalty(0,
KnuthElement.INFINITE, false,
new Position(this), false));
} else if (!((KnuthElement) contentList.getLast()).isGlue()) {
// add a null penalty to allow a break between blocks
contentList.add(new KnuthPenalty(0, 0, false,
new Position(this), false));
} else {
// the last element in contentList is a glue;
// it is a feasible breakpoint, there is no need to add
// a penalty
}
}*/
contentList.addAll(returnedList);
if (returnedList.size() > 0) {
if (((KnuthElement) returnedList.getLast()).isPenalty()
&& ((KnuthPenalty) returnedList.getLast()).getP() == -KnuthElement.INFINITE) {
// a descendant of this block has break-after
if (false /*curLM.isFinished()*/) {
// there is no other content in this block;
// it's useless to add space after before a page break
setFinished(true);
}
returnedList = new LinkedList();
wrapPositionElements(contentList, returnList);
return returnList;
}
}
}
wrapPositionElements(contentList, returnList);
setFinished(true);
return returnList;
}
/**
* The table area is a reference area that contains areas for
* oldColumns, bodies, rows and the contents are in cells.
*
* @param parentIter the position iterator
* @param layoutContext the layout context for adding areas
*/
public void addAreas(PositionIterator parentIter,
LayoutContext layoutContext) {
getParentArea(null);
getPSLM().addIDToPage(fobj.getId());
// if adjusted space before
double adjust = layoutContext.getSpaceAdjust();
addBlockSpacing(adjust, spaceBefore);
spaceBefore = null;
int startXOffset = fobj.getCommonMarginBlock().startIndent.getValue();
// add column, body then row areas
int tableHeight = 0;
//Body childLM;
LayoutContext lc = new LayoutContext(0);
lc.setRefIPD(referenceIPD - getIPIndents());
contentLM.setStartXOffset(startXOffset);
contentLM.addAreas(parentIter, lc);
tableHeight += contentLM.getUsedBPD();
curBlockArea.setBPD(tableHeight);
if (fobj.isSeparateBorderModel()) {
TraitSetter.addBorders(curBlockArea, fobj.getCommonBorderPaddingBackground());
}
TraitSetter.addBackground(curBlockArea, fobj.getCommonBorderPaddingBackground());
TraitSetter.addMargins(curBlockArea,
fobj.getCommonBorderPaddingBackground(),
fobj.getCommonMarginBlock());
TraitSetter.addBreaks(curBlockArea,
fobj.getBreakBefore(), fobj.getBreakAfter());
flush();
// if adjusted space after
addBlockSpacing(adjust, spaceAfter);
//bodyBreaks.clear();
curBlockArea = null;
}
/**
* Return an Area which can contain the passed childArea. The childArea
* may not yet have any content, but it has essential traits set.
* In general, if the LayoutManager already has an Area it simply returns
* it. Otherwise, it makes a new Area of the appropriate class.
* It gets a parent area for its area by calling its parent LM.
* Finally, based on the dimensions of the parent area, it initializes
* its own area. This includes setting the content IPD and the maximum
* BPD.
*
* @param childArea the child area
* @return the parent area of the child
*/
public Area getParentArea(Area childArea) {
if (curBlockArea == null) {
curBlockArea = new Block();
// Set up dimensions
// Must get dimensions from parent area
/*Area parentArea =*/ parentLM.getParentArea(curBlockArea);
int contentIPD = referenceIPD - getIPIndents();
curBlockArea.setIPD(contentIPD);
setCurrentArea(curBlockArea);
}
return curBlockArea;
}
/**
* Add the child area to this layout manager.
*
* @param childArea the child area to add
*/
public void addChildArea(Area childArea) {
if (curBlockArea != null) {
curBlockArea.addBlock((Block) childArea);
}
}
/**
* Reset the position of this layout manager.
*
* @param resetPos the position to reset to
*/
public void resetPosition(Position resetPos) {
if (resetPos == null) {
reset(null);
}
}
/**
* @see org.apache.fop.layoutmgr.BlockLevelLayoutManager#negotiateBPDAdjustment(int, org.apache.fop.layoutmgr.KnuthElement)
*/
public int negotiateBPDAdjustment(int adj, KnuthElement lastElement) {
// TODO Auto-generated method stub
return 0;
}
/**
* @see org.apache.fop.layoutmgr.BlockLevelLayoutManager#discardSpace(org.apache.fop.layoutmgr.KnuthGlue)
*/
public void discardSpace(KnuthGlue spaceGlue) {
// TODO Auto-generated method stub
}
/**
* @see org.apache.fop.layoutmgr.BlockLevelLayoutManager#mustKeepTogether()
*/
public boolean mustKeepTogether() {
//TODO Keeps will have to be more sophisticated sooner or later
return ((BlockLevelLayoutManager)getParent()).mustKeepTogether()
|| !fobj.getKeepTogether().getWithinPage().isAuto()
|| !fobj.getKeepTogether().getWithinColumn().isAuto();
}
/**
* @see org.apache.fop.layoutmgr.BlockLevelLayoutManager#mustKeepWithPrevious()
*/
public boolean mustKeepWithPrevious() {
return !fobj.getKeepWithPrevious().getWithinPage().isAuto()
|| !fobj.getKeepWithPrevious().getWithinColumn().isAuto();
}
/**
* @see org.apache.fop.layoutmgr.BlockLevelLayoutManager#mustKeepWithNext()
*/
public boolean mustKeepWithNext() {
return !fobj.getKeepWithNext().getWithinPage().isAuto()
|| !fobj.getKeepWithNext().getWithinColumn().isAuto();
}
}
|