aboutsummaryrefslogtreecommitdiffstats
path: root/src/java/org/apache/fop/layoutmgr/inline/InlineContainerLayoutManager.java
blob: 107461c0a8910bd371245d570517c9e6f131aa7a (plain)
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
/*
 * 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.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.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.fonts.Font;
import org.apache.fop.fonts.FontInfo;
import org.apache.fop.fonts.FontTriplet;
import org.apache.fop.layoutmgr.AbstractLayoutManager;
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.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 alignmentBaseline = EN_BASELINE;
    private int contentAreaIPD;
    private int contentAreaBPD;

    private List<ListElement> childElements;
    private InlineViewport currentViewport;
    private Container referenceArea;

    public InlineContainerLayoutManager(InlineContainer node) {
        super(node);
    }

    @Override
    public void initialize() {
        InlineContainer node = (InlineContainer) fobj;
        borderProps = node.getCommonBorderPaddingBackground();
    }

    @Override
    public List<KnuthSequence> getNextKnuthElements(LayoutContext context, int alignment) {
        determineIPD(context);
        LayoutContext childLC = LayoutContext.offspringOf(context); // TODO copyOf?
        childLC.setRefIPD(contentAreaIPD);
        childElements = getChildKnuthElements(childLC, alignment); // TODO which alignment?
        determineBPD();
        AlignmentContext alignmentContext = makeAlignmentContext(context); // TODO correct?
        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 = ((InlineContainer) fobj).getInlineProgressionDimension();
        Property optimum = ipd.getOptimum(this); // TODO percent base context
        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); // TODO percent base context
        }
    }

    private void determineBPD() {
        LengthRangeProperty bpd = ((InlineContainer) fobj).getBlockProgressionDimension();
        Property optimum = bpd.getOptimum(this); // TODO percent base context
        if (optimum.isAuto()) {
            contentAreaBPD = ElementListUtils.calcContentLength(childElements);
        } else {
            contentAreaBPD = optimum.getLength().getValue(this); // TODO percent base context
        }
    }

    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); // TODO copyOf? newInstance?
            childLC.setRefIPD(layoutContext.getRefIPD());
            @SuppressWarnings("unchecked")
            List<ListElement> childElements = childLM.getNextKnuthElements(childLC, alignmentBaseline);
            allChildElements.addAll(childElements);
            // TODO breaks, keeps, empty content
        }
        SpaceResolver.resolveElementList(allChildElements);
        // TODO break-before, break-after
        return allChildElements;
    }

    @Override
    public void addAreas(PositionIterator posIter, LayoutContext context) {
        Position inlineContainerPosition = null;
        while (posIter.hasNext()) {
            Position pos = posIter.next();
            if (pos.getLM() == this) {
                inlineContainerPosition = pos;
            }
        }
        addId();
//        addMarkersToPage(
//                true,
//                true,
//                lastPos == null || isLast(lastPos));

        if (inlineContainerPosition != null) {
            LayoutManager childLM;
            KnuthPossPosIter childPosIter = new KnuthPossPosIter(childElements);
            while ((childLM = childPosIter.getNextChildLM()) != null) {
                LayoutContext childLC = LayoutContext.copyOf(context); // TODO correct?
                childLM.addAreas(childPosIter, childLC);
            }
        }

//        addMarkersToPage(
//                false,
//                true,
//                lastPos == null || isLast(lastPos));

//        boolean isLast = (context.isLastArea() && prevLM == lastChildLM);
//        context.setFlags(LayoutContext.LAST_AREA, isLast);
    }

    @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);
            currentViewport.setIPD(getContentAreaIPD());
            currentViewport.setBPD(getContentAreaBPD());
            TraitSetter.setProducerID(currentViewport, fobj.getId());
            TraitSetter.addBorders(currentViewport,
                    borderProps,
                    false, false, false, false, this);
            TraitSetter.addPadding(currentViewport,
                    borderProps,
                    false, false, false, false, this);
            TraitSetter.addBackground(currentViewport,
                    borderProps,
                    this);
            currentViewport.setClip(needClip());
            currentViewport.setContentPosition(
                    new java.awt.geom.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 = ((InlineContainer) fobj).getOverflow();
        return (overflow == EN_HIDDEN || overflow == EN_ERROR_IF_OVERFLOW);
    }

    protected AlignmentContext makeAlignmentContext(LayoutContext context) {
        InlineContainer ic = (InlineContainer) fobj;
        FontInfo fi = fobj.getFOEventHandler().getFontInfo();
        FontTriplet[] fontkeys = ic.getCommonFont().getFontState(fi);
        Font fs = fi.getFontInstance(fontkeys[0], ic.getCommonFont().fontSize.getValue(this));
        return new AlignmentContext(fs, ic.getLineHeight().getOptimum(this).getLength().getValue(this), // TODO
                context.getWritingMode());
    }

    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");
    }

}