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
|
/*
* 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$ */
package org.apache.fop.layoutmgr;
import java.util.List;
import java.awt.Point;
import java.awt.geom.Rectangle2D;
import org.apache.fop.area.Area;
import org.apache.fop.area.BlockViewport;
import org.apache.fop.area.Block;
import org.apache.fop.area.PageViewport;
import org.apache.fop.area.Trait;
import org.apache.fop.fo.flow.BlockContainer;
import org.apache.fop.fo.properties.CommonAbsolutePosition;
import org.apache.fop.area.CTM;
import org.apache.fop.datatypes.FODimension;
import org.apache.fop.datatypes.Length;
import org.apache.fop.datatypes.PercentBase;
import org.apache.fop.traits.MinOptMax;
import org.apache.fop.traits.SpaceVal;
/**
* LayoutManager for a block-container FO.
*/
public class BlockContainerLayoutManager extends BlockStackingLayoutManager {
private BlockContainer fobj;
private BlockViewport viewportBlockArea;
private Block referenceArea;
private List childBreaks = new java.util.ArrayList();
private CommonAbsolutePosition abProps;
private FODimension relDims;
private CTM absoluteCTM;
private boolean clip = false;
private Length width;
private Length height;
private int vpContentIPD;
private int vpContentBPD;
private int usedBPD;
// When viewport should grow with the content.
private boolean autoHeight = true;
private int referenceIPD;
/* holds the (one-time use) fo:block space-before
and -after properties. Large fo:blocks are split
into multiple Area.Blocks to accomodate the subsequent
regions (pages) they are placed on. space-before
is applied at the beginning of the first
Block and space-after at the end of the last Block
used in rendering the fo:block.
*/
//TODO space-before|after: handle space-resolution rules
private MinOptMax foBlockSpaceBefore;
private MinOptMax foBlockSpaceAfter;
/**
* Create a new block container layout manager.
* @param node block-container node to create the layout manager for.
*/
public BlockContainerLayoutManager(BlockContainer node) {
super(node);
fobj = node;
}
/**
* @return the currently applicable page viewport
*/
protected PageViewport getPageViewport() {
LayoutManager lm = this;
while (lm != null && !(lm instanceof PageSequenceLayoutManager)) {
lm = lm.getParent();
}
if (lm == null) {
return null;
} else {
return ((PageSequenceLayoutManager)lm).getCurrentPageViewport();
}
}
/**
* @see org.apache.fop.layoutmgr.AbstractLayoutManager#initProperties()
*/
protected void initProperties() {
abProps = fobj.getCommonAbsolutePosition();
foBlockSpaceBefore = new SpaceVal(fobj.getCommonMarginBlock().spaceBefore).getSpace();
foBlockSpaceAfter = new SpaceVal(fobj.getCommonMarginBlock().spaceAfter).getSpace();
boolean rotated = (fobj.getReferenceOrientation() % 180 != 0);
if (rotated) {
height = fobj.getInlineProgressionDimension().getOptimum().getLength();
width = fobj.getBlockProgressionDimension().getOptimum().getLength();
} else {
height = fobj.getBlockProgressionDimension().getOptimum().getLength();
width = fobj.getInlineProgressionDimension().getOptimum().getLength();
}
}
/** @return the content IPD */
protected int getRotatedIPD() {
return fobj.getInlineProgressionDimension().getOptimum().getLength().getValue();
}
private int getSpaceBefore() {
return foBlockSpaceBefore.opt;
}
private int getBPIndents() {
int indents = 0;
indents += fobj.getCommonMarginBlock().spaceBefore.getOptimum().getLength().getValue();
indents += fobj.getCommonMarginBlock().spaceAfter.getOptimum().getLength().getValue();
indents += fobj.getCommonBorderPaddingBackground().getBPPaddingAndBorder(false);
return indents;
}
private int getIPIndents() {
int iIndents = 0;
iIndents += fobj.getCommonMarginBlock().startIndent.getValue();
iIndents += fobj.getCommonMarginBlock().endIndent.getValue();
return iIndents;
}
private boolean isAbsoluteOrFixed() {
return (abProps.absolutePosition == EN_ABSOLUTE)
|| (abProps.absolutePosition == EN_FIXED);
}
private boolean isFixed() {
return (abProps.absolutePosition == EN_FIXED);
}
/**
* @see org.apache.fop.layoutmgr.LayoutManager#getNextBreakPoss(org.apache.fop.layoutmgr.LayoutContext)
*/
public BreakPoss getNextBreakPoss(LayoutContext context) {
if (isAbsoluteOrFixed()) {
return getAbsoluteBreakPoss(context);
}
autoHeight = false;
boolean rotated = (fobj.getReferenceOrientation() % 180 != 0); //vals[0] == 0.0;
referenceIPD = context.getRefIPD();
int maxbpd = context.getStackLimit().opt;
int allocBPD, allocIPD;
if (height.getEnum() != EN_AUTO) {
allocBPD = height.getValue(); //this is the content-height
allocBPD += getBPIndents();
} else {
allocBPD = maxbpd;
autoHeight = true;
}
if (width.getEnum() != EN_AUTO) {
allocIPD = width.getValue(); //this is the content-width
allocIPD += getIPIndents();
} else {
allocIPD = referenceIPD;
}
vpContentBPD = allocBPD - getBPIndents();
vpContentIPD = allocIPD - getIPIndents();
double contentRectOffsetX = 0;
contentRectOffsetX += fobj.getCommonMarginBlock().startIndent.getValue();
double contentRectOffsetY = 0;
//contentRectOffsetY += fobj.getCommonMarginBlock().startIndent.getValue();
//contentRectOffsetY += getSpaceBefore();
contentRectOffsetY += fobj.getCommonBorderPaddingBackground().getBorderBeforeWidth(false);
contentRectOffsetY += fobj.getCommonBorderPaddingBackground().getPaddingBefore(false);
Rectangle2D rect = new Rectangle2D.Double(
contentRectOffsetX, contentRectOffsetY,
vpContentIPD, vpContentBPD);
relDims = new FODimension(0, 0);
absoluteCTM = CTM.getCTMandRelDims(fobj.getReferenceOrientation(),
fobj.getWritingMode(), rect, relDims);
//double[] vals = absoluteCTM.toArray();
MinOptMax stackLimit;
if (rotated) {
// rotated 90 degrees
/*
if (relDims.ipd > context.getRefIPD()) {
relDims.ipd = context.getRefIPD();
}*/
//stackLimit = new MinOptMax(relDims.ipd);
/*
if (width.getEnum() == EN_AUTO) {
relDims.bpd = context.getStackLimit().opt;
}
absoluteCTM = new CTM(vals[0], vals[1], vals[2], vals[3], 0, 0);
*/
//absoluteCTM = new CTM(vals[0], vals[1], vals[2], vals[3], vals[5], vals[4]);
} else {
/*
if (vals[0] == -1.0) {
absoluteCTM = new CTM(vals[0], vals[1], vals[2], vals[3], 0, 0);
}*/
//stackLimit = context.getStackLimit();
}
stackLimit = new MinOptMax(relDims.bpd);
LayoutManager curLM; // currently active LM
MinOptMax stackSize = new MinOptMax();
// if starting add space before
// stackSize.add(spaceBefore);
BreakPoss lastPos = null;
//TODO fix layout dimensions!
fobj.setLayoutDimension(PercentBase.BLOCK_IPD, allocIPD);
fobj.setLayoutDimension(PercentBase.BLOCK_BPD, allocBPD);
fobj.setLayoutDimension(PercentBase.REFERENCE_AREA_IPD, relDims.ipd);
fobj.setLayoutDimension(PercentBase.REFERENCE_AREA_BPD, relDims.bpd);
while ((curLM = getChildLM()) != null) {
// Make break positions and return blocks!
// Set up a LayoutContext
BreakPoss bp;
LayoutContext childLC = new LayoutContext(0);
childLC.setStackLimit(
MinOptMax.subtract(stackLimit,
stackSize));
childLC.setRefIPD(relDims.ipd);
boolean over = false;
while (!curLM.isFinished()) {
if ((bp = curLM.getNextBreakPoss(childLC)) != null) {
stackSize.add(bp.getStackingSize());
if (stackSize.opt > stackLimit.max) {
// reset to last break
if (lastPos != null) {
reset(lastPos.getPosition());
} else {
curLM.resetPosition(null);
}
over = true;
break;
}
lastPos = bp;
childBreaks.add(bp);
if (bp.nextBreakOverflows()) {
over = true;
break;
}
childLC.setStackLimit(MinOptMax.subtract(
stackLimit, stackSize));
}
}
if (!rotated) {
BreakPoss breakPoss;
breakPoss = new BreakPoss(new LeafPosition(this,
childBreaks.size() - 1));
breakPoss.setStackingSize(stackSize);
if (over) {
breakPoss.setFlag(BreakPoss.NEXT_OVERFLOWS, true);
}
return breakPoss;
}
}
setFinished(true);
if (rotated) {
BreakPoss breakPoss;
breakPoss = new BreakPoss(new LeafPosition(this,
childBreaks.size() - 1));
breakPoss.setStackingSize(new MinOptMax(relDims.ipd));
return breakPoss;
}
return null;
}
private Point getAbsOffset() {
int x = 0;
int y = 0;
if (abProps.left.getEnum() != EN_AUTO) {
x = abProps.left.getValue();
}
if (abProps.top.getEnum() != EN_AUTO) {
y = abProps.top.getValue();
}
return new Point(x, y);
}
/**
* Generate and return the next break possibility for absolutely positioned
* block-containers.
* @param context LayoutContext to work with
* @return the next break position
* @see org.apache.fop.layoutmgr.LayoutManager#getNextBreakPoss(org.apache.fop.layoutmgr.LayoutContext)
*/
public BreakPoss getAbsoluteBreakPoss(LayoutContext context) {
LayoutManager curLM; // currently active LM
MinOptMax stackSize = new MinOptMax();
autoHeight = false;
Point offset = getAbsOffset();
int allocBPD, allocIPD;
if (height.getEnum() != EN_AUTO) {
allocBPD = height.getValue(); //this is the content-height
allocBPD += getBPIndents();
} else {
allocBPD = 0;
if (abProps.bottom.getEnum() != EN_AUTO) {
if (isFixed()) {
allocBPD = (int)getPageViewport().getViewArea().getHeight();
} else {
allocBPD = context.getStackLimit().opt;
}
allocBPD -= offset.y;
if (abProps.bottom.getEnum() != EN_AUTO) {
allocBPD -= abProps.bottom.getValue();
}
} else {
autoHeight = true;
}
}
if (width.getEnum() != EN_AUTO) {
allocIPD = width.getValue(); //this is the content-width
allocIPD += getIPIndents();
} else {
if (isFixed()) {
allocIPD = (int)getPageViewport().getViewArea().getWidth();
} else {
allocIPD = context.getRefIPD();
}
if (abProps.left.getEnum() != EN_AUTO) {
allocIPD -= abProps.left.getValue();
}
if (abProps.right.getEnum() != EN_AUTO) {
allocIPD -= abProps.right.getValue();
}
}
vpContentBPD = allocBPD - getBPIndents();
vpContentIPD = allocIPD - getIPIndents();
double contentRectOffsetX = offset.getX();
contentRectOffsetX += fobj.getCommonMarginBlock().startIndent.getValue();
double contentRectOffsetY = offset.getY();
contentRectOffsetY += getSpaceBefore();
contentRectOffsetY += fobj.getCommonBorderPaddingBackground().getBorderBeforeWidth(false);
contentRectOffsetY += fobj.getCommonBorderPaddingBackground().getPaddingBefore(false);
Rectangle2D rect = new Rectangle2D.Double(
contentRectOffsetX, contentRectOffsetY,
vpContentIPD, vpContentBPD);
relDims = new FODimension(0, 0);
absoluteCTM = CTM.getCTMandRelDims(
fobj.getReferenceOrientation(),
fobj.getWritingMode(),
rect, relDims);
while ((curLM = getChildLM()) != null) {
// Make break positions and return blocks!
// Set up a LayoutContext
BreakPoss bp;
LayoutContext childLC = new LayoutContext(0);
childLC.setStackLimit(new MinOptMax(1000000));
childLC.setRefIPD(relDims.ipd);
while (!curLM.isFinished()) {
if ((bp = curLM.getNextBreakPoss(childLC)) != null) {
stackSize.add(bp.getStackingSize());
childBreaks.add(bp);
}
}
}
setFinished(true);
BreakPoss breakPoss = new BreakPoss(
new LeafPosition(this, childBreaks.size() - 1));
// absolutely positioned areas do not contribute
// to the normal stacking
breakPoss.setStackingSize(new MinOptMax(0));
usedBPD = stackSize.opt;
//TODO Maybe check for page overflow when autoHeight=true
if (!autoHeight & (usedBPD > relDims.bpd)) {
log.warn("Contents overflow block-container viewport: clipping");
if (fobj.getOverflow() == EN_HIDDEN) {
clip = true;
} else if (fobj.getOverflow() == EN_ERROR_IF_OVERFLOW) {
//TODO Throw layout exception
clip = true;
}
}
return breakPoss;
}
/**
* @see org.apache.fop.layoutmgr.LayoutManager#addAreas(org.apache.fop.layoutmgr.PositionIterator, org.apache.fop.layoutmgr.LayoutContext)
*/
public void addAreas(PositionIterator parentIter,
LayoutContext layoutContext) {
getParentArea(null);
/* taken from BlockLM, check first if we should use space-before|after traits
double adjust = layoutContext.getSpaceAdjust();
if (!isAbsoluteOrFixed()) {
// if adjusted space before
addBlockSpacing(adjust, foBlockSpaceBefore);
foBlockSpaceBefore = null;
}*/
addID(fobj.getId());
addMarkers(true, true);
LayoutManager childLM;
int iStartPos = 0;
LayoutContext lc = new LayoutContext(0);
while (parentIter.hasNext()) {
LeafPosition lfp = (LeafPosition) parentIter.next();
// Add the block areas to Area
PositionIterator breakPosIter
= new BreakPossPosIter(childBreaks, iStartPos,
lfp.getLeafPos() + 1);
iStartPos = lfp.getLeafPos() + 1;
while ((childLM = breakPosIter.getNextChildLM()) != null) {
childLM.addAreas(breakPosIter, lc);
}
}
flush();
addMarkers(true, true);
/*
if (!isAbsoluteOrFixed()) {
// if adjusted space after
foBlockSpaceAfter = new SpaceVal(fobj.getCommonMarginBlock().spaceAfter).getSpace();
addBlockSpacing(adjust, foBlockSpaceAfter);
}*/
childBreaks.clear();
viewportBlockArea = null;
referenceArea = null;
}
/**
* Get the parent area for children of this block container.
* This returns the current block container area
* and creates it if required.
*
* @see org.apache.fop.layoutmgr.LayoutManager#getParentArea(Area)
*/
public Area getParentArea(Area childArea) {
if (referenceArea == null) {
viewportBlockArea = new BlockViewport();
viewportBlockArea.addTrait(Trait.IS_VIEWPORT_AREA, Boolean.TRUE);
viewportBlockArea.setIPD(vpContentIPD);
if (autoHeight) {
viewportBlockArea.setBPD(0);
} else {
viewportBlockArea.setBPD(vpContentBPD);
}
TraitSetter.addBorders(viewportBlockArea, fobj.getCommonBorderPaddingBackground());
TraitSetter.addBackground(viewportBlockArea, fobj.getCommonBorderPaddingBackground());
TraitSetter.addMargins(viewportBlockArea,
fobj.getCommonBorderPaddingBackground(),
fobj.getCommonMarginBlock());
viewportBlockArea.setCTM(absoluteCTM);
viewportBlockArea.setClip(clip);
if (getSpaceBefore() != 0) {
viewportBlockArea.addTrait(Trait.SPACE_BEFORE, new Integer(getSpaceBefore()));
}
if (foBlockSpaceAfter.opt != 0) {
viewportBlockArea.addTrait(Trait.SPACE_AFTER, new Integer(foBlockSpaceAfter.opt));
}
if (abProps.absolutePosition == EN_ABSOLUTE
|| abProps.absolutePosition == EN_FIXED) {
Point offset = getAbsOffset();
viewportBlockArea.setXOffset(offset.x);
viewportBlockArea.setYOffset(offset.y);
} else {
//nop
}
referenceArea = new Block();
referenceArea.addTrait(Trait.IS_REFERENCE_AREA, Boolean.TRUE);
if (abProps.absolutePosition == EN_ABSOLUTE) {
viewportBlockArea.setPositioning(Block.ABSOLUTE);
} else if (abProps.absolutePosition == EN_FIXED) {
viewportBlockArea.setPositioning(Block.FIXED);
}
// Set up dimensions
// Must get dimensions from parent area
/*Area parentArea =*/ parentLM.getParentArea(referenceArea);
//int referenceIPD = parentArea.getIPD();
referenceArea.setIPD(relDims.ipd);
// Get reference IPD from parentArea
setCurrentArea(viewportBlockArea); // ??? for generic operations
}
return referenceArea;
}
/**
* Add the child to the block container.
*
* @see org.apache.fop.layoutmgr.LayoutManager#addChild(Area)
*/
public void addChild(Area childArea) {
if (referenceArea != null) {
referenceArea.addBlock((Block) childArea);
}
}
/**
* @see org.apache.fop.layoutmgr.LayoutManager#resetPosition(org.apache.fop.layoutmgr.Position)
*/
public void resetPosition(Position resetPos) {
if (resetPos == null) {
reset(null);
}
}
/**
* Force current area to be added to parent area.
* @see org.apache.fop.layoutmgr.AbstractLayoutManager#flush()
*/
protected void flush() {
viewportBlockArea.addBlock(referenceArea, autoHeight);
//Handle display-align now that the used BPD can be determined
usedBPD = referenceArea.getAllocBPD();
if (!autoHeight & (usedBPD > 0)) {
if (fobj.getDisplayAlign() == EN_CENTER) {
viewportBlockArea.setCTM(viewportBlockArea.getCTM().multiply(
new CTM().translate(0, (relDims.bpd - usedBPD) / 2)));
} else if (fobj.getDisplayAlign() == EN_AFTER) {
viewportBlockArea.setCTM(viewportBlockArea.getCTM().multiply(
new CTM().translate(0, (relDims.bpd - usedBPD))));
}
}
// Fake a 0 height for absolute positioned blocks.
int saveBPD = viewportBlockArea.getBPD();
if (viewportBlockArea.getPositioning() == Block.ABSOLUTE) {
viewportBlockArea.setBPD(0);
}
super.flush();
// Restore the right height.
if (viewportBlockArea.getPositioning() == Block.ABSOLUTE) {
viewportBlockArea.setBPD(saveBPD);
}
}
}
|