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
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
|
/*
* 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.layoutmgr.table;
import java.util.LinkedList;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.fop.datatypes.PercentBaseContext;
import org.apache.fop.fo.FONode;
import org.apache.fop.fo.flow.table.Table;
import org.apache.fop.fo.flow.table.TableCell;
import org.apache.fop.fo.properties.CommonBorderPaddingBackground;
import org.apache.fop.layoutmgr.AreaAdditionUtil;
import org.apache.fop.layoutmgr.BlockLevelLayoutManager;
import org.apache.fop.layoutmgr.BlockStackingLayoutManager;
import org.apache.fop.layoutmgr.BreakElement;
import org.apache.fop.layoutmgr.KnuthElement;
import org.apache.fop.layoutmgr.KnuthGlue;
import org.apache.fop.layoutmgr.KnuthPenalty;
import org.apache.fop.layoutmgr.LayoutContext;
import org.apache.fop.layoutmgr.ListElement;
import org.apache.fop.layoutmgr.PositionIterator;
import org.apache.fop.layoutmgr.Position;
import org.apache.fop.layoutmgr.SpaceResolver;
import org.apache.fop.layoutmgr.TraitSetter;
import org.apache.fop.area.Area;
import org.apache.fop.area.Block;
import org.apache.fop.area.Trait;
import org.apache.fop.traits.MinOptMax;
/**
* LayoutManager for a table-cell FO.
* A cell contains blocks. These blocks fill the cell.
*/
public class TableCellLayoutManager extends BlockStackingLayoutManager
implements BlockLevelLayoutManager {
/**
* logging instance
*/
private static Log log = LogFactory.getLog(TableCellLayoutManager.class);
private PrimaryGridUnit primaryGridUnit;
private Block curBlockArea;
private int inRowIPDOffset;
private int xoffset;
private int yoffset;
private int cellIPD;
private int rowHeight;
private int usedBPD;
private int borderAndPaddingBPD;
private boolean emptyCell = true;
/**
* Create a new Cell layout manager.
* @param node table-cell FO for which to create the LM
* @param pgu primary grid unit for the cell
*/
public TableCellLayoutManager(TableCell node, PrimaryGridUnit pgu) {
super(node);
fobj = node;
this.primaryGridUnit = pgu;
}
/** @return the table-cell FO */
public TableCell getTableCell() {
return (TableCell)this.fobj;
}
private boolean isSeparateBorderModel() {
return getTable().isSeparateBorderModel();
}
/** {@inheritDoc} */
public void initialize() {
borderAndPaddingBPD = 0;
borderAndPaddingBPD += getTableCell()
.getCommonBorderPaddingBackground().getBorderBeforeWidth(false);
borderAndPaddingBPD += getTableCell()
.getCommonBorderPaddingBackground().getBorderAfterWidth(false);
if (!isSeparateBorderModel()) {
borderAndPaddingBPD /= 2;
}
borderAndPaddingBPD += getTableCell().getCommonBorderPaddingBackground()
.getPaddingBefore(false, this);
borderAndPaddingBPD += getTableCell().getCommonBorderPaddingBackground()
.getPaddingAfter(false, this);
}
/**
* @return the table owning this cell
*/
public Table getTable() {
FONode node = fobj.getParent();
while (!(node instanceof Table)) {
node = node.getParent();
}
return (Table)node;
}
/** {@inheritDoc} */
protected int getIPIndents() {
int[] startEndBorderWidths = primaryGridUnit.getStartEndBorderWidths();
startIndent = startEndBorderWidths[0];
endIndent = startEndBorderWidths[1];
if (isSeparateBorderModel()) {
int borderSep = getTable().getBorderSeparation().getLengthPair().getIPD().getLength()
.getValue(this);
startIndent += borderSep / 2;
endIndent += borderSep / 2;
} else {
startIndent /= 2;
endIndent /= 2;
}
startIndent += getTableCell().getCommonBorderPaddingBackground().getPaddingStart(false, this);
endIndent += getTableCell().getCommonBorderPaddingBackground().getPaddingEnd(false, this);
return startIndent + endIndent;
}
/**
* {@inheritDoc}
*/
public LinkedList getNextKnuthElements(LayoutContext context, int alignment) {
MinOptMax stackLimit = new MinOptMax(context.getStackLimit());
referenceIPD = context.getRefIPD();
cellIPD = referenceIPD;
cellIPD -= getIPIndents();
LinkedList returnedList = null;
LinkedList contentList = new LinkedList();
LinkedList returnList = new LinkedList();
BlockLevelLayoutManager curLM; // currently active LM
BlockLevelLayoutManager prevLM = null; // previously active LM
while ((curLM = (BlockLevelLayoutManager) getChildLM()) != null) {
LayoutContext childLC = new LayoutContext(0);
// curLM is a ?
childLC.setStackLimit(MinOptMax.subtract(context
.getStackLimit(), stackLimit));
childLC.setRefIPD(cellIPD);
// get elements from curLM
returnedList = curLM.getNextKnuthElements(childLC, alignment);
if (childLC.isKeepWithNextPending()) {
log.debug("child LM signals pending keep with next");
}
if (contentList.size() == 0 && childLC.isKeepWithPreviousPending()) {
context.setFlags(LayoutContext.KEEP_WITH_PREVIOUS_PENDING);
childLC.setFlags(LayoutContext.KEEP_WITH_PREVIOUS_PENDING, false);
}
if (prevLM != null) {
// there is a block handled by prevLM
// before the one handled by curLM
if (mustKeepTogether()
|| context.isKeepWithNextPending()
|| childLC.isKeepWithPreviousPending()) {
//Clear keep pending flag
context.setFlags(LayoutContext.KEEP_WITH_NEXT_PENDING, false);
childLC.setFlags(LayoutContext.KEEP_WITH_PREVIOUS_PENDING, false);
// add an infinite penalty to forbid a break between
// blocks
contentList.add(new BreakElement(
new Position(this), KnuthElement.INFINITE, context));
//contentList.add(new KnuthPenalty(0,
// KnuthElement.INFINITE, false,
// new Position(this), false));
} else if (!(((ListElement) contentList.getLast()).isGlue()
|| (((ListElement)contentList.getLast()).isPenalty()
&& ((KnuthPenalty)contentList.getLast()).getP() < KnuthElement.INFINITE)
|| (contentList.getLast() instanceof BreakElement
&& ((BreakElement)contentList.getLast()).getPenaltyValue() < KnuthElement.INFINITE))) {
// TODO vh: this is hacky
// The getNextKnuthElements method of TableCellLM must not be called
// twice, otherwise some settings like indents or borders will be
// counted several times and lead to a wrong output. Anyway the
// getNextKnuthElements methods should be called only once eventually
// (i.e., when multi-threading the code), even when there are forced
// breaks.
// If we add a break possibility after a forced break the
// AreaAdditionUtil.addAreas method will act on a sequence starting
// with a SpaceResolver.SpaceHandlingBreakPosition element, having no
// LM associated to it. Thus it will stop early instead of adding
// areas for following Positions. The above test aims at preventing
// such a situation from occuring. add a null penalty to allow a break
// between blocks
contentList.add(new BreakElement(
new Position(this), 0, context));
//contentList.add(new KnuthPenalty(0, 0, false,
// new Position(this), false));
} else {
// the last element in contentList is a feasible breakpoint, there is
// no need to add a penalty
}
}
contentList.addAll(returnedList);
if (returnedList.size() == 0) {
//Avoid NoSuchElementException below (happens with empty blocks)
continue;
}
if (childLC.isKeepWithNextPending()) {
//Clear and propagate
childLC.setFlags(LayoutContext.KEEP_WITH_NEXT_PENDING, false);
context.setFlags(LayoutContext.KEEP_WITH_NEXT_PENDING);
}
prevLM = curLM;
}
returnedList = new LinkedList();
wrapPositionElements(contentList, returnList);
//Space resolution
SpaceResolver.resolveElementList(returnList);
getPSLM().notifyEndOfLayout(((TableCell)getFObj()).getId());
setFinished(true);
return returnList;
}
/**
* Set the y offset of this cell.
* This offset is used to set the absolute position of the cell.
*
* @param off the y direction offset
*/
public void setYOffset(int off) {
yoffset = off;
}
/**
* Set the x offset of this cell (usually the same as its parent row).
* This offset is used to determine the absolute position of the cell.
*
* @param off the x offset
*/
public void setXOffset(int off) {
xoffset = off;
}
/**
* Set the IPD offset of this cell inside the table-row.
* This offset is used to determine the absolute position of the cell.
* @param off the IPD offset
*/
public void setInRowIPDOffset(int off) {
this.inRowIPDOffset = off;
}
/**
* Set the content height for this cell. This method is used during
* addAreas() stage.
*
* @param h the height of the contents of this cell
*/
public void setContentHeight(int h) {
usedBPD = h;
}
/**
* Set the row height that contains this cell. This method is used during
* addAreas() stage.
*
* @param h the height of the row
*/
public void setRowHeight(int h) {
rowHeight = h;
}
/**
* Returns the bpd of the given grid unit.
* @param gu a grid unit belonging to this cell
* @return the content height of the grid unit
*/
private int getContentHeight(GridUnit gu) {
int bpd = rowHeight;
if (isSeparateBorderModel()) {
bpd -= gu.getPrimary().getBorders().getBorderBeforeWidth(false);
bpd -= gu.getPrimary().getBorders().getBorderAfterWidth(false);
} else {
bpd -= gu.getPrimary().getHalfMaxBorderWidth();
}
CommonBorderPaddingBackground cbpb
= gu.getCell().getCommonBorderPaddingBackground();
bpd -= cbpb.getPaddingBefore(false, this);
bpd -= cbpb.getPaddingAfter(false, this);
bpd -= 2 * ((TableLayoutManager)getParent()).getHalfBorderSeparationBPD();
return bpd;
}
/**
* Add the areas for the break points. The cell contains block stacking layout
* managers that add block areas.
*
* <p>In the collapsing-border model, the borders of a cell that spans over several
* rows or columns are drawn separately for each grid unit. Therefore we must know the
* height of each grid row spanned over by the cell. Also, if the cell is broken over
* two pages we must know which spanned grid rows are present on the current page.</p>
*
* @param parentIter the iterator of the break positions
* @param layoutContext the layout context for adding the areas
* @param spannedGridRowHeights in collapsing-border model for a spanning cell, height
* of each spanned grid row
* @param startRow first grid row on the current page spanned over by the cell,
* inclusive
* @param endRow last grid row on the current page spanned over by the cell, exclusive
*/
public void addAreas(PositionIterator parentIter,
LayoutContext layoutContext,
int[] spannedGridRowHeights,
int startRow,
int endRow) {
getParentArea(null);
getPSLM().addIDToPage(getTableCell().getId());
if (isSeparateBorderModel()) {
if (!emptyCell || getTableCell().showEmptyCells()) {
TraitSetter.addBorders(curBlockArea, getTableCell().getCommonBorderPaddingBackground(),
false, false, false, false, this);
TraitSetter.addPadding(curBlockArea, getTableCell().getCommonBorderPaddingBackground(),
false, false, false, false, this);
}
} else {
if (!primaryGridUnit.hasSpanning()) {
//Can set the borders directly if there's no span
boolean[] outer = new boolean[] {
primaryGridUnit.getFlag(GridUnit.FIRST_IN_TABLE),
primaryGridUnit.getFlag(GridUnit.LAST_IN_TABLE),
primaryGridUnit.getFlag(GridUnit.IN_FIRST_COLUMN),
primaryGridUnit.getFlag(GridUnit.IN_LAST_COLUMN)};
TraitSetter.addCollapsingBorders(curBlockArea,
primaryGridUnit.getBorders(), outer, this);
} else {
boolean[] outer = new boolean[4];
int dy = yoffset;
for (int y = startRow; y < endRow; y++) {
GridUnit[] gridUnits = (GridUnit[])primaryGridUnit.getRows().get(y);
int dx = xoffset;
for (int x = 0; x < gridUnits.length; x++) {
GridUnit gu = gridUnits[x];
if (gu.hasBorders()) {
//Blocks for painting grid unit borders
Block block = new Block();
block.addTrait(Trait.IS_REFERENCE_AREA, Boolean.TRUE);
block.setPositioning(Block.ABSOLUTE);
int bpd = spannedGridRowHeights[y - startRow];
bpd -= gu.getBorders().getBorderBeforeWidth(false) / 2;
bpd -= gu.getBorders().getBorderAfterWidth(false) / 2;
block.setBPD(bpd);
if (log.isTraceEnabled()) {
log.trace("pgu: " + primaryGridUnit + "; gu: " + gu + "; yoffset: "
+ (dy - gu.getBorders().getBorderBeforeWidth(false) / 2)
+ "; bpd: " + bpd);
}
int ipd = gu.getColumn().getColumnWidth().getValue(
(PercentBaseContext) getParent());
int borderStartWidth = gu.getBorders().getBorderStartWidth(false) / 2;
ipd -= borderStartWidth;
ipd -= gu.getBorders().getBorderEndWidth(false) / 2;
block.setIPD(ipd);
block.setXOffset(dx + borderStartWidth);
block.setYOffset(dy - gu.getBorders().getBorderBeforeWidth(false) / 2);
outer[0] = gu.getFlag(GridUnit.FIRST_IN_TABLE);
outer[1] = gu.getFlag(GridUnit.LAST_IN_TABLE);
outer[2] = gu.getFlag(GridUnit.IN_FIRST_COLUMN);
outer[3] = gu.getFlag(GridUnit.IN_LAST_COLUMN);
TraitSetter.addCollapsingBorders(block, gu.getBorders(), outer, this);
parentLM.addChildArea(block);
}
dx += gu.getColumn().getColumnWidth().getValue(
(PercentBaseContext) getParent());
}
dy += spannedGridRowHeights[y - startRow];
}
}
}
TraitSetter.addPadding(curBlockArea, primaryGridUnit.getBorders(),
false, false, false, false, this);
//Handle display-align
int contentBPD = getContentHeight(primaryGridUnit);
if (usedBPD < contentBPD) {
if (getTableCell().getDisplayAlign() == EN_CENTER) {
Block space = new Block();
space.setBPD((contentBPD - usedBPD) / 2);
curBlockArea.addBlock(space);
} else if (getTableCell().getDisplayAlign() == EN_AFTER) {
Block space = new Block();
space.setBPD((contentBPD - usedBPD));
curBlockArea.addBlock(space);
}
}
AreaAdditionUtil.addAreas(this, parentIter, layoutContext);
curBlockArea.setBPD(contentBPD);
// Add background after we know the BPD
if (isSeparateBorderModel()) {
if (!emptyCell || getTableCell().showEmptyCells()) {
TraitSetter.addBackground(curBlockArea,
getTableCell().getCommonBorderPaddingBackground(),
this);
}
} else {
TraitSetter.addBackground(curBlockArea,
getTableCell().getCommonBorderPaddingBackground(),
this);
}
flush();
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 to get the parent for
* @return the parent area
*/
public Area getParentArea(Area childArea) {
if (curBlockArea == null) {
curBlockArea = new Block();
curBlockArea.addTrait(Trait.IS_REFERENCE_AREA, Boolean.TRUE);
TraitSetter.setProducerID(curBlockArea, getTableCell().getId());
curBlockArea.setPositioning(Block.ABSOLUTE);
// set position
int borderAdjust = 0;
if (!isSeparateBorderModel()) {
if (primaryGridUnit.hasSpanning()) {
borderAdjust -= primaryGridUnit.getHalfMaxBeforeBorderWidth();
} else {
borderAdjust += primaryGridUnit.getHalfMaxBeforeBorderWidth();
}
} else {
//borderAdjust += primaryGridUnit.getBorders().getBorderBeforeWidth(false);
}
TableLayoutManager tableLM = (TableLayoutManager)getParent();
curBlockArea.setXOffset(xoffset + inRowIPDOffset + startIndent);
curBlockArea.setYOffset(yoffset - borderAdjust
+ tableLM.getHalfBorderSeparationBPD());
curBlockArea.setIPD(cellIPD);
//curBlockArea.setHeight();
// Set up dimensions
/*Area parentArea =*/ parentLM.getParentArea(curBlockArea);
// Get reference IPD from parentArea
setCurrentArea(curBlockArea); // ??? for generic operations
}
return curBlockArea;
}
/**
* Add the child to the cell block area.
*
* @param childArea the child to add to the cell
*/
public void addChildArea(Area childArea) {
if (curBlockArea != null) {
curBlockArea.addBlock((Block) childArea);
}
}
/**
* Reset the position of the layout.
*
* @param resetPos the position to reset to
*/
public void resetPosition(Position resetPos) {
if (resetPos == null) {
reset(null);
}
}
/**
* {@inheritDoc}
*/
public int negotiateBPDAdjustment(int adj, KnuthElement lastElement) {
// TODO Auto-generated method stub
return 0;
}
/**
* {@inheritDoc}
*/
public void discardSpace(KnuthGlue spaceGlue) {
// TODO Auto-generated method stub
}
/**
* {@inheritDoc}
*/
public boolean mustKeepTogether() {
//TODO Keeps will have to be more sophisticated sooner or later
boolean keep = ((BlockLevelLayoutManager)getParent()).mustKeepTogether();
if (primaryGridUnit.getRow() != null) {
keep |= primaryGridUnit.getRow().mustKeepTogether();
}
return keep;
}
/**
* {@inheritDoc}
*/
public boolean mustKeepWithPrevious() {
return false; //TODO FIX ME
/*
return !fobj.getKeepWithPrevious().getWithinPage().isAuto()
|| !fobj.getKeepWithPrevious().getWithinColumn().isAuto();
*/
}
/**
* {@inheritDoc}
*/
public boolean mustKeepWithNext() {
return false; //TODO FIX ME
/*
return !fobj.getKeepWithNext().getWithinPage().isAuto()
|| !fobj.getKeepWithNext().getWithinColumn().isAuto();
*/
}
// --------- Property Resolution related functions --------- //
/**
* Returns the IPD of the content area
* @return the IPD of the content area
*/
public int getContentAreaIPD() {
return cellIPD;
}
/**
* Returns the BPD of the content area
* @return the BPD of the content area
*/
public int getContentAreaBPD() {
if (curBlockArea != null) {
return curBlockArea.getBPD();
} else {
log.error("getContentAreaBPD called on unknown BPD");
return -1;
}
}
/**
* {@inheritDoc}
*/
public boolean getGeneratesReferenceArea() {
return true;
}
/**
* {@inheritDoc}
*/
public boolean getGeneratesBlockArea() {
return true;
}
}
|