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
|
/*
* 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.inline;
import java.awt.geom.Rectangle2D;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import org.apache.fop.area.Area;
import org.apache.fop.area.Trait;
import org.apache.fop.area.inline.Container;
import org.apache.fop.area.inline.InlineViewport;
import org.apache.fop.datatypes.Length;
import org.apache.fop.datatypes.LengthBase;
import org.apache.fop.datatypes.SimplePercentBaseContext;
import org.apache.fop.fo.Constants;
import org.apache.fop.fo.flow.InlineContainer;
import org.apache.fop.fo.properties.CommonBorderPaddingBackground;
import org.apache.fop.fo.properties.LengthRangeProperty;
import org.apache.fop.fo.properties.Property;
import org.apache.fop.layoutmgr.AbstractLayoutManager;
import org.apache.fop.layoutmgr.AreaAdditionUtil;
import org.apache.fop.layoutmgr.BlockLevelEventProducer;
import org.apache.fop.layoutmgr.ElementListUtils;
import org.apache.fop.layoutmgr.InlineKnuthSequence;
import org.apache.fop.layoutmgr.KnuthPossPosIter;
import org.apache.fop.layoutmgr.KnuthSequence;
import org.apache.fop.layoutmgr.LayoutContext;
import org.apache.fop.layoutmgr.LayoutManager;
import org.apache.fop.layoutmgr.ListElement;
import org.apache.fop.layoutmgr.NonLeafPosition;
import org.apache.fop.layoutmgr.Position;
import org.apache.fop.layoutmgr.PositionIterator;
import org.apache.fop.layoutmgr.SpaceResolver;
import org.apache.fop.layoutmgr.TraitSetter;
/**
* This creates a single inline container area after
* laying out the child block areas. All footnotes, floats
* and id areas are maintained for later retrieval.
*/
public class InlineContainerLayoutManager extends AbstractLayoutManager implements InlineLevelLayoutManager {
private CommonBorderPaddingBackground borderProps;
private int contentAreaIPD;
private int contentAreaBPD;
private List<ListElement> childElements;
private int ipdOverflow;
private AlignmentContext alignmentContext;
private InlineViewport currentViewport;
private Container referenceArea;
public InlineContainerLayoutManager(InlineContainer node) {
super(node);
}
@Override
public void initialize() {
InlineContainer node = (InlineContainer) fobj;
borderProps = node.getCommonBorderPaddingBackground();
}
private InlineContainer getInlineContainer() {
assert fobj instanceof InlineContainer;
return (InlineContainer) fobj;
}
@Override
public List<KnuthSequence> getNextKnuthElements(LayoutContext context, int alignment) {
determineIPD(context);
childElements = getChildKnuthElements(context, alignment);
determineBPD();
alignmentContext = makeAlignmentContext(context);
Position position = new Position(this, 0);
KnuthSequence knuthSequence = new InlineKnuthSequence();
knuthSequence.add(new KnuthInlineBox(contentAreaIPD, alignmentContext, position, false));
List<KnuthSequence> knuthElements = new ArrayList<KnuthSequence>(1);
knuthElements.add(knuthSequence);
setFinished(true);
return knuthElements;
}
private void determineIPD(LayoutContext layoutContext) {
LengthRangeProperty ipd = getInlineContainer().getInlineProgressionDimension();
Property optimum = ipd.getOptimum(this);
if (optimum.isAuto()) {
contentAreaIPD = layoutContext.getRefIPD();
InlineLevelEventProducer eventProducer = InlineLevelEventProducer.Provider.get(
fobj.getUserAgent().getEventBroadcaster());
eventProducer.inlineContainerAutoIPDNotSupported(this, contentAreaIPD / 1000f);
} else {
contentAreaIPD = optimum.getLength().getValue(this);
}
}
private List<ListElement> getChildKnuthElements(LayoutContext layoutContext, int alignment) {
List<ListElement> allChildElements = new LinkedList<ListElement>();
LayoutManager childLM;
while ((childLM = getChildLM()) != null) {
LayoutContext childLC = LayoutContext.offspringOf(layoutContext);
childLC.setRefIPD(contentAreaIPD);
@SuppressWarnings("unchecked")
List<ListElement> childElements = childLM.getNextKnuthElements(childLC, alignment);
allChildElements.addAll(childElements);
}
handleIPDOverflow();
wrapPositions(allChildElements);
SpaceResolver.resolveElementList(allChildElements);
SpaceResolver.performConditionalsNotification(allChildElements, 0, allChildElements.size() - 1, -1);
return allChildElements;
}
private void determineBPD() {
LengthRangeProperty bpd = getInlineContainer().getBlockProgressionDimension();
Property optimum = bpd.getOptimum(this);
int actualBPD = ElementListUtils.calcContentLength(childElements);
if (optimum.isAuto()) {
contentAreaBPD = actualBPD;
} else {
double bpdValue = optimum.getLength().getNumericValue(this);
if (bpdValue < 0) {
contentAreaBPD = actualBPD;
} else {
contentAreaBPD = (int) Math.round(bpdValue);
if (contentAreaBPD < actualBPD) {
BlockLevelEventProducer eventProducer = getBlockLevelEventProducer();
eventProducer.viewportBPDOverflow(this, fobj.getName(),
actualBPD - contentAreaBPD, needClip(), canRecoverFromOverflow(),
fobj.getLocator());
}
}
}
}
protected AlignmentContext makeAlignmentContext(LayoutContext context) {
InlineContainer ic = (InlineContainer) fobj;
AlignmentContext ac = new AlignmentContext(contentAreaBPD,
ic.getAlignmentAdjust(), ic.getAlignmentBaseline(),
ic.getBaselineShift(), ic.getDominantBaseline(),
context.getAlignmentContext());
int baselineOffset = getAlignmentPoint(ac.getDominantBaselineIdentifier());
ac.resizeLine(contentAreaBPD, baselineOffset);
return ac;
}
private void handleIPDOverflow() {
if (ipdOverflow > 0) {
BlockLevelEventProducer eventProducer = getBlockLevelEventProducer();
eventProducer.viewportIPDOverflow(this, fobj.getName(),
ipdOverflow, needClip(), canRecoverFromOverflow(),
fobj.getLocator());
}
}
private void wrapPositions(List<ListElement> elements) {
for (ListElement element : elements) {
Position position = new NonLeafPosition(this, element.getPosition());
notifyPos(position);
element.setPosition(position);
}
}
private BlockLevelEventProducer getBlockLevelEventProducer() {
return BlockLevelEventProducer.Provider.get(fobj.getUserAgent().getEventBroadcaster());
}
private boolean canRecoverFromOverflow() {
return getInlineContainer().getOverflow() != EN_ERROR_IF_OVERFLOW;
}
private int getAlignmentPoint(int dominantBaseline) {
Length alignmentAdjust = getInlineContainer().getAlignmentAdjust();
int baseline = alignmentAdjust.getEnum();
if (baseline == Constants.EN_AUTO) {
return getInlineContainerBaselineOffset(getInlineContainer().getAlignmentBaseline());
} else if (baseline == Constants.EN_BASELINE) {
return getInlineContainerBaselineOffset(dominantBaseline);
} else if (baseline != 0) {
return getInlineContainerBaselineOffset(baseline);
} else {
int baselineOffset = getInlineContainerBaselineOffset(dominantBaseline);
int lineHeight = getInlineContainer().getLineHeight().getOptimum(this).getLength().getValue(this);
int adjust = alignmentAdjust.getValue(
new SimplePercentBaseContext(null, LengthBase.ALIGNMENT_ADJUST, lineHeight));
return baselineOffset + adjust;
}
}
private int getInlineContainerBaselineOffset(int property) {
switch (property) {
case Constants.EN_BEFORE_EDGE:
case Constants.EN_TEXT_BEFORE_EDGE:
return 0;
case Constants.EN_AFTER_EDGE:
case Constants.EN_TEXT_AFTER_EDGE:
return contentAreaBPD;
case Constants.EN_MIDDLE:
case Constants.EN_CENTRAL:
case Constants.EN_MATHEMATICAL:
return contentAreaBPD / 2;
case Constants.EN_IDEOGRAPHIC:
return contentAreaBPD * 7 / 10;
case Constants.EN_ALPHABETIC:
return contentAreaBPD * 6 / 10;
case Constants.EN_HANGING:
return contentAreaBPD * 2 / 10;
case Constants.EN_AUTO:
case Constants.EN_BASELINE:
return hasLineAreaDescendant() ? getBaselineOffset() : contentAreaBPD;
default:
throw new AssertionError("Unknown baseline value: " + property);
}
}
@Override
public boolean getGeneratesReferenceArea() {
return true;
}
@Override
public void addAreas(PositionIterator posIter, LayoutContext context) {
Position inlineContainerPosition = null;
while (posIter.hasNext()) {
/*
* Should iterate only once, but hasNext must be called twice for its
* side-effects to apply and the iterator to switch to the next LM.
*/
assert inlineContainerPosition == null;
inlineContainerPosition = posIter.next();
assert inlineContainerPosition.getLM() == this;
}
assert inlineContainerPosition != null;
KnuthPossPosIter childPosIter = new KnuthPossPosIter(childElements);
AreaAdditionUtil.addAreas(this, childPosIter, context);
}
@Override
public Area getParentArea(Area childArea) {
if (referenceArea == null) {
referenceArea = new Container();
referenceArea.addTrait(Trait.IS_REFERENCE_AREA, Boolean.TRUE);
TraitSetter.setProducerID(referenceArea, fobj.getId());
referenceArea.setIPD(contentAreaIPD);
currentViewport = new InlineViewport(referenceArea);
currentViewport.addTrait(Trait.IS_VIEWPORT_AREA, Boolean.TRUE);
TraitSetter.setProducerID(currentViewport, fobj.getId());
currentViewport.setBlockProgressionOffset(alignmentContext.getOffset());
currentViewport.setIPD(getContentAreaIPD());
currentViewport.setBPD(getContentAreaBPD());
TraitSetter.addBackground(currentViewport, borderProps, this);
currentViewport.setClip(needClip());
currentViewport.setContentPosition(
new Rectangle2D.Float(0, 0, getContentAreaIPD(), getContentAreaBPD()));
getParent().addChildArea(currentViewport);
}
return referenceArea;
}
@Override
public int getContentAreaIPD() {
return contentAreaIPD;
}
@Override
public int getContentAreaBPD() {
return contentAreaBPD;
}
@Override
public void addChildArea(Area childArea) {
referenceArea.addChildArea(childArea);
}
private boolean needClip() {
int overflow = getInlineContainer().getOverflow();
return (overflow == EN_HIDDEN || overflow == EN_ERROR_IF_OVERFLOW);
}
public boolean handleOverflow(int milliPoints) {
ipdOverflow = Math.max(ipdOverflow, milliPoints);
return true;
}
public List addALetterSpaceTo(List oldList) {
throw new UnsupportedOperationException("Not implemented");
}
public List addALetterSpaceTo(List oldList, int depth) {
throw new UnsupportedOperationException("Not implemented");
}
public String getWordChars(Position pos) {
throw new UnsupportedOperationException("Not implemented");
}
public void hyphenate(Position pos, HyphContext hyphContext) {
throw new UnsupportedOperationException("Not implemented");
}
public boolean applyChanges(List oldList) {
throw new UnsupportedOperationException("Not implemented");
}
public boolean applyChanges(List oldList, int depth) {
throw new UnsupportedOperationException("Not implemented");
}
public List getChangedKnuthElements(List oldList, int alignment, int depth) {
throw new UnsupportedOperationException("Not implemented");
}
}
|