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
|
/*
* $Id$
* Copyright (C) 2001 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;
import org.apache.fop.fo.FObj;
import org.apache.fop.area.Area;
import org.apache.fop.area.LineArea;
import org.apache.fop.area.MinOptMax;
import org.apache.fop.area.inline.InlineArea;
import org.apache.fop.fo.properties.VerticalAlign;
import org.apache.fop.area.inline.Word;
import org.apache.fop.area.inline.Space;
import org.apache.fop.area.inline.Character;
import java.util.ListIterator;
import java.util.List;
import java.util.ArrayList;
import java.util.Iterator;
/**
* LayoutManager for lines. It builds one or more lines containing
* inline areas generated by its sub layout managers.
*
* The line layout manager does the following things:
* receives a list of inline creating layout managers
* adds the inline areas retrieved from the child layout managers
* finds the best line break position
* adds complete line to parent
* stores the starting position for each line in case of recreation
* if ipd not changed but line contains resolved values (eg. page number), redoes from that line
* when freeing memory, release all layout managers and inline areas before current position
* As each child layout manager is used it gets the start, end and normal references for id area, footnotes, floats, links, colour-back properties
* first line properties are set and used by the child when retrieving the inline area(s)
*
* Hyphenation is handled by asking the child to split the words then this
* adds the hyph char. If redone then exra char ignored.
*
* How do we handle Unicode BIDI?
*/
public class LineLayoutManager extends AbstractLayoutManager {
private LineInfo currentLine = null;
private boolean bFirstLine = true;
private MinOptMax totalIPD;
// the following values must be set by the block
// these are the dominant basline and lineheight values
private int lineHeight;
private int lead;
private int follow;
List lmList;
List lines = new ArrayList();
private LayoutPos bestPos = null;
private MinOptMax bestIPD = null;
static class LineInfo {
LayoutPos startPos;
LineArea area;
boolean hasResolved = false;
boolean noJustify = false;
// footnotes, floats?
}
public LineLayoutManager(FObj fobjBlock, List lms, int lh, int l, int f) {
super(fobjBlock);
lmList = lms;
lineHeight = lh;
lead = l;
follow = f;
}
public int getContentIPD() {
return parentLM.getContentIPD();
}
/**
* Call child layout managers to generate content as long as they
* generate inline areas. If a block-level generating LM is found,
* finish any line being filled and return to the parent LM.
*/
public boolean generateAreas() {
// if a side float is added and the line contains content
// where the ipd depends on the line width then restart
// the line with the adjusted length
while (curPos.lmIndex < lmList.size()) {
LeafNodeLayoutManager curLM =
(LeafNodeLayoutManager) lmList.get(curPos.lmIndex);
curLM.setParentLM(this);
LeafNodeLayoutManager nextLM = null;
if (curPos.lmIndex + 1 < lmList.size()) {
nextLM = (LeafNodeLayoutManager) lmList.get(
curPos.lmIndex + 1);
while (nextLM.size() == 0) {
lmList.remove(curPos.lmIndex + 1);
if (curPos.lmIndex + 1 == lmList.size()) {
nextLM = null;
break;
}
nextLM = (LeafNodeLayoutManager) lmList.get(
curPos.lmIndex + 1);
}
}
if (nextLM != null) {
nextLM.setParentLM(this);
}
if (curLM.resolved()) {
currentLine.hasResolved = true;
}
while (curPos.subIndex < curLM.size()) {
InlineArea ia = curLM.get(curPos.subIndex);
InlineArea next = null;
if (curPos.subIndex + 1 < curLM.size()) {
next = curLM.get(curPos.subIndex + 1);
} else if (curPos.lmIndex + 1 < lmList.size()) {
if (nextLM != null) {
next = nextLM.get(0);
}
}
if (currentLine != null && !currentLine.noJustify &&
(curPos.subIndex + 1 == curLM.size() &&
curPos.lmIndex + 1 == lmList.size())) {
currentLine.noJustify = true;
}
if (addChild(ia, next)) {
if (flush()) {
return true;
}
}
// flush final line in same context as other lines
// handle last line concepts
if (curPos.subIndex + 1 == curLM.size() &&
curPos.lmIndex + 1 == lmList.size()) {
if (flush()) {
return true;
}
if (curPos.subIndex + 1 == curLM.size() &&
curPos.lmIndex + 1 == lmList.size()) {
return false;
}
}
curPos.subIndex++;
}
curPos.lmIndex++;
curPos.subIndex = 0;
}
return false;
}
/**
* Align and position curLine and add it to parentContainer.
* Set curLine to null.
*/
public boolean flush() {
if (currentLine != null) {
// Adjust spacing as necessary
adjustSpacing();
verticalAlign(currentLine.area);
boolean res = parentLM.addChild(currentLine.area);
lines.add(currentLine);
currentLine = null;
bestPos = null;
bestIPD = null;
return res;
}
return false;
}
/**
* Do the ipd adjustment for stretch areas etc.
* Consecutive spaces need to be collapsed if possible.
* should this be on the line area so it can finish resolved areas?
*/
private void adjustSpacing() {
List inlineAreas = currentLine.area.getInlineAreas();
// group text elements to split at hyphen if available
// remove collapsable spaces at start or end on line
// backtrack to best position
while (true) {
if (curPos.lmIndex == bestPos.lmIndex &&
curPos.subIndex == bestPos.subIndex) {
break;
}
InlineArea inline =
(InlineArea) inlineAreas.get(inlineAreas.size() - 1);
MinOptMax ipd = inline.getAllocationIPD();
totalIPD.subtract(ipd);
inlineAreas.remove(inlineAreas.size() - 1);
currentLine.noJustify = false;
curPos.subIndex--;
if (curPos.subIndex == -1) {
curPos.lmIndex--;
LeafNodeLayoutManager curLM =
(LeafNodeLayoutManager) lmList.get( curPos.lmIndex);
curPos.subIndex = curLM.size() - 1;
}
}
// for justify also stretch spaces to fill
// stretch to best match
float percentAdjust = 0;
boolean maxSide = false;
int realWidth = bestIPD.opt;
if (bestIPD.opt > parentLM.getContentIPD()) {
if (bestIPD.opt - parentLM.getContentIPD() <
(bestIPD.max - bestIPD.opt)) {
percentAdjust = (bestIPD.opt - parentLM.getContentIPD()) /
(float)(bestIPD.max - bestIPD.opt);
realWidth = parentLM.getContentIPD();
} else {
percentAdjust = 1;
realWidth = bestIPD.max;
}
maxSide = true;
} else {
if (parentLM.getContentIPD() - bestIPD.opt <
bestIPD.opt - bestIPD.min) {
percentAdjust = (parentLM.getContentIPD() - bestIPD.opt) /
(float)(bestIPD.opt - bestIPD.min);
realWidth = parentLM.getContentIPD();
} else {
percentAdjust = 1;
realWidth = bestIPD.min;
}
}
if (percentAdjust > 0) {
for (Iterator iter = inlineAreas.iterator(); iter.hasNext();) {
InlineArea inline = (InlineArea) iter.next();
int width;
MinOptMax iipd = inline.getAllocationIPD();
if (!maxSide) {
width = iipd.opt +
(int)((iipd.max - iipd.opt) * percentAdjust);
} else {
width = iipd.opt -
(int)((iipd.opt - iipd.min) * percentAdjust);
}
inline.setWidth(width);
}
}
// don't justify lines ending with U+000A or last line
if (/*justify && */!currentLine.noJustify &&
realWidth != parentLM.getContentIPD()) {
ArrayList spaces = new ArrayList();
for (Iterator iter = inlineAreas.iterator(); iter.hasNext();) {
InlineArea inline = (InlineArea) iter.next();
if (inline instanceof Space /* && !((Space)inline).fixed*/) {
spaces.add(inline);
}
}
for (Iterator iter = spaces.iterator(); iter.hasNext();) {
Space space = (Space) iter.next();
space.setWidth(space.getWidth() +
(parentLM.getContentIPD() - realWidth) /
spaces.size());
}
}
}
protected void verticalAlign(LineArea lineArea) {
int maxHeight = lineHeight;
List inlineAreas = lineArea.getInlineAreas();
// get smallest possible offset to before edge
// this depends on the height of no and middle alignments
int before = lead;
int after = follow;
int halfLeading = (lineHeight - lead - follow) / 2;
before += halfLeading;
for (Iterator iter = inlineAreas.iterator(); iter.hasNext();) {
InlineArea inline = (InlineArea) iter.next();
LayoutInfo info = inline.info;
int al;
int ld = inline.getHeight();
if (info != null) {
al = info.alignment;
ld = info.lead;
} else {
al = VerticalAlign.BASELINE;
}
if (al == VerticalAlign.BASELINE) {
if (ld > before) {
before = ld;
}
if (inline.getHeight() > before) {
before = inline.getHeight();
}
} else if (al == VerticalAlign.MIDDLE) {
if (inline.getHeight() / 2 + lead / 2 > before) {
before = inline.getHeight() / 2 + lead / 2;
}
if (inline.getHeight() / 2 - lead / 2 > after) {
after = inline.getHeight() / 2 - lead / 2;
}
} else if (al == VerticalAlign.TOP) {
} else if (al == VerticalAlign.BOTTOM) {
}
}
// then align all before, no and middle alignment
for (Iterator iter = inlineAreas.iterator(); iter.hasNext();) {
InlineArea inline = (InlineArea) iter.next();
LayoutInfo info = inline.info;
int al;
int ld = inline.getHeight();
boolean bloffset = false;
if (info != null) {
al = info.alignment;
ld = info.lead;
bloffset = info.blOffset;
} else {
al = VerticalAlign.BASELINE;
}
if (al == VerticalAlign.BASELINE) {
// the offset position for text is the baseline
if (bloffset) {
inline.setOffset(before);
} else {
inline.setOffset(before - ld);
}
if (inline.getHeight() - ld > after) {
after = inline.getHeight() - ld;
}
} else if (al == VerticalAlign.MIDDLE) {
inline.setOffset(before - inline.getHeight() / 2 -
lead / 2);
} else if (al == VerticalAlign.TOP) {
inline.setOffset(0);
if (inline.getHeight() - before > after) {
after = inline.getHeight() - before;
}
} else if (al == VerticalAlign.BOTTOM) {
if (inline.getHeight() - before > after) {
after = inline.getHeight() - before;
}
}
}
// after alignment depends on maximum height of before
// and middle alignments
for (Iterator iter = inlineAreas.iterator(); iter.hasNext();) {
InlineArea inline = (InlineArea) iter.next();
LayoutInfo info = inline.info;
int al;
if (info != null) {
al = info.alignment;
} else {
al = VerticalAlign.BASELINE;
}
if (al == VerticalAlign.BASELINE) {
} else if (al == VerticalAlign.MIDDLE) {
} else if (al == VerticalAlign.TOP) {
} else if (al == VerticalAlign.BOTTOM) {
inline.setOffset(before + after - inline.getHeight());
}
}
if (before + after > maxHeight) {
lineArea.setHeight(before + after);
} else {
lineArea.setHeight(maxHeight);
}
}
/**
* Return current lineArea or generate a new one if necessary.
*/
public Area getParentArea(Area childArea) {
if (currentLine.area == null) {
createLine();
}
return currentLine.area;
}
protected void createLine() {
currentLine = new LineInfo();
currentLine.startPos = curPos;
currentLine.area = new LineArea();
/* Set line IPD from parentArea
* This accounts for indents. What about first line indent?
* Should we set an "isFirst" flag on the lineArea to signal
* that to the parent (Block) LM? That's where indent property
* information will be managed.
*/
Area parent = parentLM.getParentArea(currentLine.area);
// currentLine.area.setContentIPD(parent.getContentIPD());
// totalIPD = new MinOptMax();
// OR???
totalIPD = new MinOptMax();
this.bFirstLine = false;
}
/**
* Called by child LayoutManager when it has filled one of its areas.
* See if the area will fit in the current container.
* If so, add it.
* This should also handle floats if childArea is an anchor.
* @param childArea the area to add: should be an InlineArea subclass!
*/
public boolean addChild(InlineArea inlineArea, InlineArea nextArea) {
if (currentLine == null) {
createLine();
}
// add side floats first
int pIPD = parentLM.getContentIPD();
currentLine.area.addInlineArea(inlineArea);
totalIPD.add(inlineArea.getAllocationIPD());
LayoutInfo info = inlineArea.info;
if (info == null) {
info = new LayoutInfo();
}
LayoutInfo ninfo;
if (nextArea != null && nextArea.info != null) {
ninfo = nextArea.info;
} else {
ninfo = new LayoutInfo();
}
// the best pos cannot be before the first area
if (bestPos == null || bestIPD == null) {
bestPos = new LayoutPos();
bestPos.lmIndex = curPos.lmIndex;
bestPos.subIndex = curPos.subIndex;
MinOptMax imop = inlineArea.getAllocationIPD();
bestIPD = new MinOptMax(imop.min, imop.opt, imop.max);
} else {
// bestPos changed only when it can break
// before/after a space or other atomic inlines
// check keep-with on this and next
// since chars are optimized as words we cannot assume a
// word is complete and therefore hyphenate or break after
// side floats effect the available ipd but do not add to line
if (!ninfo.keepPrev && !info.keepNext &&
!(info.isText && ninfo.isText)) {
if (Math.abs(bestIPD.opt - pIPD) >
Math.abs(totalIPD.opt - pIPD) &&
(totalIPD.min <= pIPD)) {
bestPos.lmIndex = curPos.lmIndex;
bestPos.subIndex = curPos.subIndex;
bestIPD = new MinOptMax(totalIPD.min, totalIPD.opt,
totalIPD.max);
}
}
}
// Forced line break after this area (ex. ends with LF in nowrap)
if (info.breakAfter) {
currentLine.noJustify = true;
return true;
}
if (totalIPD.min > pIPD) {
return true;
}
return false;
}
public boolean addChild(Area childArea) {
return false;
}
}
|