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
|
/*
* $Id$
* Copyright (C) 2002 The Apache Software Foundation. All rights reserved.
* For details on use and redistribution please refer to the
* LICENSE file included with these sources.
*/
package org.apache.fop.layoutmgr.list;
import org.apache.fop.fo.PropertyManager;
import org.apache.fop.layoutmgr.BlockStackingLayoutManager;
import org.apache.fop.layoutmgr.LayoutManager;
import org.apache.fop.layoutmgr.LeafPosition;
import org.apache.fop.layoutmgr.BreakPoss;
import org.apache.fop.layoutmgr.LayoutContext;
import org.apache.fop.layoutmgr.PositionIterator;
import org.apache.fop.layoutmgr.BreakPossPosIter;
import org.apache.fop.layoutmgr.Position;
import org.apache.fop.fo.FObj;
import org.apache.fop.area.Area;
import org.apache.fop.area.Block;
import org.apache.fop.area.MinOptMax;
import org.apache.fop.layout.BorderAndPadding;
import org.apache.fop.layout.BackgroundProps;
import java.util.Iterator;
import java.util.ArrayList;
import java.util.List;
/**
* LayoutManager for a list-item FO.
* The list item contains a list item label and a list item body.
*/
public class ListItemLayoutManager extends BlockStackingLayoutManager {
private Item label;
private Item body;
private Block curBlockArea;
private List cellList = null;
private List columns = null;
private int listItemHeight;
private BorderAndPadding borderProps = null;
private BackgroundProps backgroundProps;
private class ItemPosition extends LeafPosition {
protected List cellBreaks;
protected ItemPosition(LayoutManager lm, int pos, List l) {
super(lm, pos);
cellBreaks = l;
}
}
/**
* Create a new list item layout manager.
*
* @param fobj the list-item formatting object
*/
public ListItemLayoutManager(FObj fobj) {
super(fobj);
}
protected void initProperties(PropertyManager propMgr) {
borderProps = propMgr.getBorderAndPadding();
backgroundProps = propMgr.getBackgroundProps();
}
public void setLabel(Item item) {
label = item;
label.setParentLM(this);
}
public void setBody(Item item) {
body = item;
body.setParentLM(this);
}
/**
* Get the next break possibility.
*
* @param context the layout context for getting breaks
* @return the next break possibility
*/
public BreakPoss getNextBreakPoss(LayoutContext context) {
Item curLM; // currently active LM
BreakPoss lastPos = null;
ArrayList breakList = new ArrayList();
int min = 0;
int opt = 0;
int max = 0;
int stage = 0;
while (true) {
if(stage == 0) {
curLM = label;
} else if (stage == 1) {
curLM = body;
} else {
break;
}
ArrayList childBreaks = new ArrayList();
MinOptMax stackSize = new MinOptMax();
// Set up a LayoutContext
// the ipd is from the current column
int ipd = context.getRefIPD();
BreakPoss bp;
LayoutContext childLC = new LayoutContext(0);
childLC.setStackLimit(
MinOptMax.subtract(context.getStackLimit(),
stackSize));
if (stage == 0) {
childLC.setRefIPD(24000);
} else if (stage == 1) {
childLC.setRefIPD(context.getRefIPD() - 24000);
}
stage++;
while (!curLM.isFinished()) {
if ((bp = curLM.getNextBreakPoss(childLC)) != null) {
stackSize.add(bp.getStackingSize());
if (stackSize.opt > context.getStackLimit().max) {
// reset to last break
if (lastPos != null) {
curLM.resetPosition(lastPos.getPosition());
} else {
curLM.resetPosition(null);
}
break;
} else {
lastPos = bp;
}
childBreaks.add(bp);
childLC.setStackLimit(MinOptMax.subtract(
context.getStackLimit(), stackSize));
}
}
// the min is the maximum min of the label and body
if (stackSize.min > min) {
min = stackSize.min;
}
// the optimum is the minimum of all optimums
if (stackSize.opt > opt) {
opt = stackSize.opt;
}
// the maximum is the largest maximum
if (stackSize.max > max) {
max = stackSize.max;
}
breakList.add(childBreaks);
}
listItemHeight = opt;
MinOptMax itemSize = new MinOptMax(min, opt, max);
setFinished(true);
ItemPosition rp = new ItemPosition(this, breakList.size() - 1, breakList);
BreakPoss breakPoss = new BreakPoss(rp);
breakPoss.setStackingSize(itemSize);
return breakPoss;
}
/**
* Add the areas for the break points.
* This sets the offset of each cell as it is added.
*
* @param parentIter the position iterator
* @param layoutContext the layout context for adding areas
*/
public void addAreas(PositionIterator parentIter,
LayoutContext layoutContext) {
getParentArea(null);
addID();
Item childLM;
LayoutContext lc = new LayoutContext(0);
while (parentIter.hasNext()) {
ItemPosition lfp = (ItemPosition) parentIter.next();
// Add the block areas to Area
for (Iterator iter = lfp.cellBreaks.iterator(); iter.hasNext();) {
List cellsbr = (List)iter.next();
PositionIterator breakPosIter;
breakPosIter = new BreakPossPosIter(cellsbr, 0, cellsbr.size());
while ((childLM = (Item)breakPosIter.getNextChildLM()) != null) {
if(childLM == body) {
childLM.setXOffset(24000);
}
childLM.addAreas(breakPosIter, lc);
}
}
}
curBlockArea.setHeight(listItemHeight);
flush();
}
/**
* Get the height of the list item after adjusting.
* Should only be called after adding the list item areas.
*
* @return the height of this list item after adjustment
*/
public int getListItemHeight() {
return listItemHeight;
}
/**
* 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 are for the child
*/
public Area getParentArea(Area childArea) {
if (curBlockArea == null) {
curBlockArea = new Block();
// Set up dimensions
Area parentArea = parentLM.getParentArea(curBlockArea);
int referenceIPD = parentArea.getIPD();
curBlockArea.setIPD(referenceIPD);
curBlockArea.setWidth(referenceIPD);
// Get reference IPD from parentArea
setCurrentArea(curBlockArea); // ??? for generic operations
}
return curBlockArea;
}
/**
* Add the child.
* Rows return the areas returned by the child elements.
* This simply adds the area to the parent layout manager.
*
* @param childArea the child area
* @return unused
*/
public boolean addChild(Area childArea) {
if (curBlockArea != null) {
curBlockArea.addBlock((Block) childArea);
return false;
}
return false;
}
/**
* Reset the position of this layout manager.
*
* @param resetPos the position to reset to
*/
public void resetPosition(Position resetPos) {
if (resetPos == null) {
reset(null);
}
}
}
|