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
|
/*-- $Id$ --
============================================================================
The Apache Software License, Version 1.1
============================================================================
Copyright (C) 1999 The Apache Software Foundation. All rights reserved.
Redistribution and use in source and binary forms, with or without modifica-
tion, are permitted provided that the following conditions are met:
1. Redistributions of source code must retain the above copyright notice,
this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.
3. The end-user documentation included with the redistribution, if any, must
include the following acknowledgment: "This product includes software
developed by the Apache Software Foundation (http://www.apache.org/)."
Alternately, this acknowledgment may appear in the software itself, if
and wherever such third-party acknowledgments normally appear.
4. The names "Fop" and "Apache Software Foundation" must not be used to
endorse or promote products derived from this software without prior
written permission. For written permission, please contact
apache@apache.org.
5. Products derived from this software may not be called "Apache", nor may
"Apache" appear in their name, without prior written permission of the
Apache Software Foundation.
THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES,
INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
APACHE SOFTWARE FOUNDATION OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLU-
DING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
This software consists of voluntary contributions made by many individuals
on behalf of the Apache Software Foundation and was originally created by
James Tauber <jtauber@jtauber.com>. For more information on the Apache
Software Foundation, please see <http://www.apache.org/>.
*/
package org.apache.fop.layout;
// FOP
import org.apache.fop.render.Renderer;
import org.apache.fop.fo.properties.*;
import org.apache.fop.datatypes.IDReferences;
import org.apache.fop.apps.FOPException;
import org.apache.fop.fo.FObj;
import org.apache.fop.fo.flow.Block;
import org.apache.fop.fo.flow.BlockContainer;
// Java
import java.util.Vector;
import java.util.Enumeration;
public class BodyAreaContainer extends Area {
// dimensions for the 'region-reference-area'
private int xPosition; // should be able to take value 'left' and 'right' too
private int yPosition; // should be able to take value 'top' and 'bottom' too
private int position;
// the column-count and column-gap
private int columnCount;
private int columnGap;
// the 3 primary reference areas
private AreaContainer mainReferenceArea;
private AreaContainer beforeFloatReferenceArea;
private AreaContainer footnoteReferenceArea;
// current heights
private int mainRefAreaHeight;
private int beforeFloatRefAreaHeight;
private int footnoteRefAreaHeight;
// reference area yPositions
private int mainYPosition;
private int beforeFloatYPosition;
private int footnoteYPosition;
// the start FO in case of rollback
private FObj startFO;
private boolean isNewSpanArea;
// keeps track of footnote state for multiple layouts
private int footnoteState = 0;
public BodyAreaContainer(FontState fontState, int xPosition, int yPosition,
int allocationWidth, int maxHeight, int position,
int columnCount, int columnGap) {
super(fontState, allocationWidth, maxHeight);
this.xPosition = xPosition;
this.yPosition = yPosition;
this.position = position;
this.columnCount = columnCount;
this.columnGap = columnGap;
// create the primary reference areas
beforeFloatRefAreaHeight = 0;
footnoteRefAreaHeight = 0;
mainRefAreaHeight = maxHeight - beforeFloatRefAreaHeight - footnoteRefAreaHeight;
beforeFloatReferenceArea = new AreaContainer(fontState, xPosition, yPosition,
allocationWidth, beforeFloatRefAreaHeight, Position.ABSOLUTE);
this.addChild(beforeFloatReferenceArea);
mainReferenceArea = new AreaContainer(fontState, xPosition, yPosition,
allocationWidth, mainRefAreaHeight, Position.ABSOLUTE);
this.addChild(mainReferenceArea);
int footnoteRefAreaYPosition = yPosition - mainRefAreaHeight;
footnoteReferenceArea = new AreaContainer(fontState, xPosition, footnoteRefAreaYPosition,
allocationWidth, footnoteRefAreaHeight, Position.ABSOLUTE);
this.addChild(footnoteReferenceArea);
// all padding and border-width must be 0
//setBorderAndPadding(new BorderAndPadding());
// setPadding(0, 0, 0, 0);
// setBorderWidth(0, 0, 0, 0);
}
public void render(Renderer renderer) {
renderer.renderBodyAreaContainer(this);
}
public int getPosition() {
return position;
}
public int getXPosition() {
return xPosition + getPaddingLeft() + getBorderLeftWidth();
}
public void setXPosition(int value)
{
xPosition=value;
}
public int getYPosition() {
return yPosition + getPaddingTop() + getBorderTopWidth();
}
public void setYPosition(int value)
{
yPosition=value;
}
public AreaContainer getMainReferenceArea()
{
return mainReferenceArea;
}
public AreaContainer getBeforeFloatReferenceArea()
{
return beforeFloatReferenceArea;
}
public AreaContainer getFootnoteReferenceArea()
{
return footnoteReferenceArea;
}
public void setIDReferences(IDReferences idReferences) {
mainReferenceArea.setIDReferences(idReferences);
}
public IDReferences getIDReferences() {
return mainReferenceArea.getIDReferences();
}
/**
* Depending on the column-count of the next FO, determine whether
* a new span area needs to be constructed or not, and return the
* appropriate ColumnArea.
* The next cut of this method should also inspect the FO to see
* whether the area to be returned ought not to be the footnote
* or before-float reference area.
* @param fo The next formatting object
* @returns the next column area (possibly the current one)
*/
private static int counter = 0;
public AreaContainer getNextArea(FObj fo)
throws FOPException
{
isNewSpanArea = false;
int span = Span.NONE;
if (fo instanceof Block)
span = ((Block)fo).getSpan();
else if (fo instanceof BlockContainer)
span = ((BlockContainer)fo).getSpan();
if (this.mainReferenceArea.getChildren().isEmpty())
{
if (span == Span.ALL)
return addSpanArea(1);
else
return addSpanArea(columnCount);
}
Vector spanAreas = this.mainReferenceArea.getChildren();
SpanArea spanArea = (SpanArea)spanAreas.elementAt(spanAreas.size()-1);
if ((span == Span.ALL) && (spanArea.getColumnCount() == 1))
{
// return the single column area in the same span area
return spanArea.getCurrentColumnArea();
}
else if ((span == Span.NONE) && (spanArea.getColumnCount() == columnCount))
{
// return the current column area in the same span area
return spanArea.getCurrentColumnArea();
}
else if (span == Span.ALL)
{
// create new span area with one column; return column area
return addSpanArea(1);
}
else if (span == Span.NONE)
{
// create new span area with multiple columns; return first column area
return addSpanArea(columnCount);
}
else
{
throw new FOPException("BodyAreaContainer::getNextArea(): Span attribute messed up");
}
}
/**
* Add a new span area with specified number of column areas.
* @param numColumns The number of column areas
* @returns AreaContainer The next column area
*/
private AreaContainer addSpanArea( int numColumns )
{
resetHeights();
// create span area and child column-areas, using whatever
// height remains after existing span areas (in the main
// reference area).
int spanAreaYPosition = getYPosition() - this.mainReferenceArea.getContentHeight();
SpanArea spanArea = new SpanArea(fontState,
getXPosition(), spanAreaYPosition,
allocationWidth, getRemainingHeight(),
numColumns, columnGap);
this.mainReferenceArea.addChild(spanArea);
spanArea.setPage(this.getPage());
this.isNewSpanArea = true;
return spanArea.getCurrentColumnArea();
}
/**
* This almost does what getNewArea() does, without actually
* returning an area. These 2 methods can be reworked.
* @param fo The next formatting object
* @returns boolean True if we need to balance.
*/
public boolean isBalancingRequired(FObj fo)
{
if (this.mainReferenceArea.getChildren().isEmpty())
return false;
Vector spanAreas = this.mainReferenceArea.getChildren();
SpanArea spanArea = (SpanArea)spanAreas.elementAt(spanAreas.size()-1);
if (spanArea.isBalanced())
return false;
int span = Span.NONE;
if (fo instanceof Block)
span = ((Block)fo).getSpan();
else if (fo instanceof BlockContainer)
span = ((BlockContainer)fo).getSpan();
if ((span == Span.ALL) && (spanArea.getColumnCount() == 1))
return false;
else if ((span == Span.NONE) && (spanArea.getColumnCount() == columnCount))
return false;
else if (span == Span.ALL)
return true;
else if (span == Span.NONE)
return false;
else
return false;
}
/**
* This is where the balancing algorithm lives, or gets called.
* Right now it's primitive: get the total content height in all
* columns, divide by the column count, and add a heuristic
* safety factor.
* Then the previous (unbalanced) span area is removed, and a new
* one added with the computed max height.
*/
public void resetSpanArea()
{
Vector spanAreas = this.mainReferenceArea.getChildren();
SpanArea spanArea = (SpanArea)spanAreas.elementAt(spanAreas.size()-1);
if (!spanArea.isBalanced())
{
// span area maintains a record of the total height of
// laid-out content in the previous (first) attempt
int newHeight = spanArea.getTotalContentHeight() / spanArea.getColumnCount();
newHeight += 2*15600; // ???
this.mainReferenceArea.removeChild(spanArea);
resetHeights();
SpanArea newSpanArea = new SpanArea(fontState,
getXPosition(), spanArea.getYPosition(),
allocationWidth, newHeight,
spanArea.getColumnCount(), columnGap);
this.mainReferenceArea.addChild(newSpanArea);
newSpanArea.setPage(this.getPage());
newSpanArea.setIsBalanced();
this.isNewSpanArea = true;
}
else
{
System.err.println("Trying to balance balanced area");
System.exit(0);
}
}
/**
* Determine remaining height for new span area. Needs to be
* modified for footnote and before-float reference areas when
* those are supported.
* @returns int The remaining available height in millipoints.
*/
public int getRemainingHeight()
{
return this.mainReferenceArea.getMaxHeight() -
this.mainReferenceArea.getContentHeight();
}
/**
* Used by resetSpanArea() and addSpanArea() to adjust the main
* reference area height before creating a new span.
*/
private void resetHeights()
{
int totalHeight = 0;
for (Enumeration e = this.mainReferenceArea.getChildren().elements(); e.hasMoreElements(); )
{
SpanArea spanArea = (SpanArea)e.nextElement();
int spanContentHeight = spanArea.getMaxContentHeight();
int spanMaxHeight = spanArea.getMaxHeight();
totalHeight += (spanContentHeight < spanMaxHeight) ? spanContentHeight: spanMaxHeight;
}
this.mainReferenceArea.setHeight(totalHeight);
}
/**
* Used in Flow when layout returns incomplete.
* @returns boolean Is this the last column in this span?
*/
public boolean isLastColumn()
{
Vector spanAreas = this.mainReferenceArea.getChildren();
SpanArea spanArea = (SpanArea)spanAreas.elementAt(spanAreas.size()-1);
return spanArea.isLastColumn();
}
/**
* This variable is unset by getNextArea(), is set by addSpanArea(),
* and <i>may</i> be set by resetSpanArea().
* @returns boolean Is the span area new or not?
*/
public boolean isNewSpanArea()
{
return isNewSpanArea;
}
public AreaContainer getCurrentColumnArea()
{
Vector spanAreas = this.mainReferenceArea.getChildren();
SpanArea spanArea = (SpanArea)spanAreas.elementAt(spanAreas.size()-1);
return spanArea.getCurrentColumnArea();
}
public int getFootnoteState()
{
return footnoteState;
}
public boolean needsFootnoteAdjusting()
{
footnoteYPosition = footnoteReferenceArea.getYPosition();
switch(footnoteState) {
case 0:
resetHeights();
if(footnoteReferenceArea.getHeight() > 0
&& mainYPosition + mainReferenceArea.getHeight() > footnoteYPosition) {
return true;
}
case 1:
break;
}
return false;
}
public void adjustFootnoteArea()
{
footnoteState++;
if(footnoteState == 1) {
mainReferenceArea.setMaxHeight(footnoteReferenceArea.getYPosition() - mainYPosition);
footnoteYPosition = footnoteReferenceArea.getYPosition();
footnoteReferenceArea.setMaxHeight(footnoteReferenceArea.getHeight());
Vector childs = footnoteReferenceArea.getChildren();
for(Enumeration en = childs.elements(); en.hasMoreElements(); ) {
Object obj = en.nextElement();
if(obj instanceof Area) {
Area childArea = (Area)obj;
footnoteReferenceArea.removeChild(childArea);
}
}
getPage().setPendingFootnotes(null);
}
}
protected static void resetMaxHeight(Area ar, int change) {
ar.setMaxHeight(change);
Vector childs = ar.getChildren();
for(Enumeration en = childs.elements(); en.hasMoreElements(); ) {
Object obj = en.nextElement();
if(obj instanceof Area) {
Area childArea = (Area)obj;
resetMaxHeight(childArea, change);
}
}
}
}
|