KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > fop > layoutmgr > InlineKnuthSequence


1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License. You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */

17
18 /* $Id: InlineKnuthSequence.java 426576 2006-07-28 15:44:37Z jeremias $ */
19
20 package org.apache.fop.layoutmgr;
21
22 import java.util.LinkedList JavaDoc;
23 import java.util.List JavaDoc;
24
25 import org.apache.fop.layoutmgr.inline.InlineLevelLayoutManager;
26 import org.apache.fop.layoutmgr.inline.KnuthInlineBox;
27
28
29 /**
30  * Represents a list of inline Knuth elements.
31  * If closed, it represents all elements of a Knuth paragraph.
32  */

33 public class InlineKnuthSequence extends KnuthSequence {
34
35     private boolean isClosed = false;
36
37     /**
38      * Creates a new and empty list.
39      */

40     public InlineKnuthSequence() {
41         super();
42     }
43     
44     /**
45      * Creates a new list from an existing list.
46      * @param list The list from which to create the new list.
47      */

48     public InlineKnuthSequence(List JavaDoc list) {
49         super(list);
50     }
51
52     /**
53      * Is this an inline or a block sequence?
54      * @return false
55      */

56     public boolean isInlineSequence() {
57         return true;
58     }
59
60     /* (non-Javadoc)
61      * @see org.apache.fop.layoutmgr.KnuthSequence#canAppendSequence(org.apache.fop.layoutmgr.KnuthSequence)
62      */

63     public boolean canAppendSequence(KnuthSequence sequence) {
64         return sequence.isInlineSequence() && !isClosed;
65     }
66
67     /* (non-Javadoc)
68      * @see org.apache.fop.layoutmgr.KnuthSequence#appendSequence(org.apache.fop.layoutmgr.KnuthSequence)
69      */

70     public boolean appendSequence(KnuthSequence sequence) {
71         if (!canAppendSequence(sequence)) {
72             return false;
73         }
74         // does the first element of the first paragraph add to an existing word?
75
ListElement lastOldElement, firstNewElement;
76         lastOldElement = getLast();
77         firstNewElement = sequence.getElement(0);
78         if (firstNewElement.isBox() && !((KnuthElement) firstNewElement).isAuxiliary()
79                 && lastOldElement.isBox() && ((KnuthElement) lastOldElement).getW() != 0) {
80             addALetterSpace();
81         }
82         addAll(sequence);
83         return true;
84     }
85
86     /* (non-Javadoc)
87      * @see KnuthSequence#appendSequence(KnuthSequence, boolean, BreakElement)
88      */

89     public boolean appendSequence(KnuthSequence sequence, boolean keepTogether,
90                                   BreakElement breakElement) {
91         return appendSequence(sequence);
92     }
93
94         
95     /* (non-Javadoc)
96      * @see org.apache.fop.layoutmgr.KnuthSequence#endSequence()
97      */

98     public KnuthSequence endSequence() {
99         if (!isClosed) {
100             add(new KnuthPenalty(0, -KnuthElement.INFINITE, false, null, false));
101             isClosed = true;
102         }
103         return this;
104     }
105
106     public void addALetterSpace() {
107         KnuthBox prevBox = (KnuthBox) getLast();
108         if (prevBox.isAuxiliary()
109             && (size() < 4
110                 || !getElement(size() - 2).isGlue()
111                 || !getElement(size() - 3).isPenalty()
112                 || !getElement(size() - 4).isBox()
113                )
114            ) {
115             // Not the sequence we are expecting
116
return;
117         }
118         removeLast();
119         LinkedList JavaDoc oldList = new LinkedList JavaDoc();
120         // if there are two consecutive KnuthBoxes the
121
// first one does not represent a whole word,
122
// so it must be given one more letter space
123
if (!prevBox.isAuxiliary()) {
124             // if letter spacing is constant,
125
// only prevBox needs to be replaced;
126
oldList.add(prevBox);
127         } else {
128             // prevBox is the last element
129
// in the sub-sequence
130
// <box> <aux penalty> <aux glue> <aux box>
131
// the letter space is added to <aux glue>,
132
// while the other elements are not changed
133
oldList.add(prevBox);
134             oldList.addFirst((KnuthGlue) removeLast());
135             oldList.addFirst((KnuthPenalty) removeLast());
136             oldList.addFirst((KnuthBox) removeLast());
137         }
138         // adding a letter space could involve, according to the text
139
// represented by oldList, replacing a glue element or adding
140
// new elements
141
addAll(((InlineLevelLayoutManager)
142                      prevBox.getLayoutManager())
143                     .addALetterSpaceTo(oldList));
144         // prevBox may not be a KnuthInlineBox;
145
// this may happen if it is a padding box; see bug 39571.
146
if ( prevBox instanceof KnuthInlineBox && ((KnuthInlineBox) prevBox).isAnchor()) {
147             // prevBox represents a footnote citation: copy footnote info
148
// from prevBox to the new box
149
KnuthInlineBox newBox = (KnuthInlineBox) getLast();
150             newBox.setFootnoteBodyLM(((KnuthInlineBox) prevBox).getFootnoteBodyLM());
151         }
152     }
153
154 }
155
Popular Tags