KickJava   Java API By Example, From Geeks To Geeks.

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


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: ElementListUtils.java 442282 2006-09-11 18:24:35Z jeremias $ */
19
20 package org.apache.fop.layoutmgr;
21
22 import java.util.LinkedList JavaDoc;
23 import java.util.List JavaDoc;
24 import java.util.ListIterator JavaDoc;
25
26 import org.apache.fop.traits.MinOptMax;
27
28 /**
29  * Utilities for Knuth element lists.
30  */

31 public class ElementListUtils {
32
33     /**
34      * Removes all legal breaks in an element list.
35      * @param elements the element list
36      */

37     public static void removeLegalBreaks(LinkedList JavaDoc elements) {
38         ListIterator JavaDoc i = elements.listIterator();
39         while (i.hasNext()) {
40             ListElement el = (ListElement)i.next();
41             if (el.isPenalty()) {
42                 BreakElement breakPoss = (BreakElement)el;
43                 //Convert all penalties no break inhibitors
44
if (breakPoss.getPenaltyValue() < KnuthPenalty.INFINITE) {
45                     breakPoss.setPenaltyValue(KnuthPenalty.INFINITE);
46                 }
47             } else if (el.isGlue()) {
48                 i.previous();
49                 if (el.isBox()) {
50                     i.next();
51                     i.add(new KnuthPenalty(0, KnuthPenalty.INFINITE, false,
52                             null, false));
53                 }
54             }
55         }
56     }
57     
58     /**
59      * Removes legal breaks in an element list. A constraint can be specified to limit the
60      * range in which the breaks are removed. Legal breaks occuring before at least
61      * constraint.opt space is filled will be removed.
62      * @param elements the element list
63      * @param constraint min/opt/max value to restrict the range in which the breaks are removed.
64      * @return true if the opt constraint is bigger than the list contents
65      */

66     public static boolean removeLegalBreaks(LinkedList JavaDoc elements, MinOptMax constraint) {
67         return removeLegalBreaks(elements, constraint.opt);
68     }
69     
70     /**
71      * Removes legal breaks in an element list. A constraint can be specified to limit the
72      * range in which the breaks are removed. Legal breaks occuring before at least
73      * constraint space is filled will be removed.
74      * @param elements the element list
75      * @param constraint value to restrict the range in which the breaks are removed.
76      * @return true if the constraint is bigger than the list contents
77      */

78     public static boolean removeLegalBreaks(LinkedList JavaDoc elements, int constraint) {
79         int len = 0;
80         ListIterator JavaDoc iter = elements.listIterator();
81         while (iter.hasNext()) {
82             ListElement el = (ListElement)iter.next();
83             if (el.isPenalty()) {
84                 KnuthPenalty penalty = (KnuthPenalty)el;
85                 //Convert all penalties to break inhibitors
86
if (penalty.getP() < KnuthPenalty.INFINITE) {
87                     iter.set(new KnuthPenalty(penalty.getW(), KnuthPenalty.INFINITE,
88                             penalty.isFlagged(), penalty.getPosition(), penalty.isAuxiliary()));
89                 }
90             } else if (el.isGlue()) {
91                 KnuthGlue glue = (KnuthGlue)el;
92                 len += glue.getW();
93                 iter.previous();
94                 el = (ListElement)iter.previous();
95                 iter.next();
96                 if (el.isBox()) {
97                     iter.add(new KnuthPenalty(0, KnuthPenalty.INFINITE, false,
98                             null, false));
99                 }
100                 iter.next();
101             } else if (el instanceof BreakElement) {
102                 BreakElement breakEl = (BreakElement)el;
103                 if (breakEl.getPenaltyValue() < KnuthPenalty.INFINITE) {
104                     breakEl.setPenaltyValue(KnuthPenalty.INFINITE);
105                 }
106             } else {
107                 KnuthElement kel = (KnuthElement)el;
108                 len += kel.getW();
109             }
110             if (len >= constraint) {
111                 return false;
112             }
113         }
114         return true;
115     }
116
117     /**
118      * Removes legal breaks in an element list. A constraint can be specified to limit the
119      * range in which the breaks are removed. Legal breaks within the space specified through the
120      * constraint (starting from the end of the element list) will be removed.
121      * @param elements the element list
122      * @param constraint value to restrict the range in which the breaks are removed.
123      * @return true if the constraint is bigger than the list contents
124      */

125     public static boolean removeLegalBreaksFromEnd(LinkedList JavaDoc elements, int constraint) {
126         int len = 0;
127         ListIterator JavaDoc i = elements.listIterator(elements.size());
128         while (i.hasPrevious()) {
129             ListElement el = (ListElement)i.previous();
130             if (el.isPenalty()) {
131                 KnuthPenalty penalty = (KnuthPenalty)el;
132                 //Convert all penalties to break inhibitors
133
if (penalty.getP() < KnuthPenalty.INFINITE) {
134                     i.set(new KnuthPenalty(penalty.getW(), KnuthPenalty.INFINITE,
135                             penalty.isFlagged(), penalty.getPosition(), penalty.isAuxiliary()));
136                 }
137             } else if (el.isGlue()) {
138                 KnuthGlue glue = (KnuthGlue)el;
139                 len += glue.getW();
140                 el = (ListElement)i.previous();
141                 i.next();
142                 if (el.isBox()) {
143                     i.add(new KnuthPenalty(0, KnuthPenalty.INFINITE, false,
144                             null, false));
145                 }
146             } else if (el instanceof BreakElement) {
147                 BreakElement breakEl = (BreakElement)el;
148                 if (breakEl.getPenaltyValue() < KnuthPenalty.INFINITE) {
149                     breakEl.setPenaltyValue(KnuthPenalty.INFINITE);
150                 }
151             } else {
152                 KnuthElement kel = (KnuthElement)el;
153                 len += kel.getW();
154             }
155             if (len >= constraint) {
156                 return false;
157             }
158         }
159         return true;
160     }
161     
162     /**
163      * Calculates the content length of the given element list. Warning: It doesn't take any
164      * stretch and shrink possibilities into account.
165      * @param elems the element list
166      * @param start element at which to start
167      * @param end element at which to stop
168      * @return the content length
169      */

170     public static int calcContentLength(List JavaDoc elems, int start, int end) {
171         ListIterator JavaDoc iter = elems.listIterator(start);
172         int count = end - start + 1;
173         int len = 0;
174         while (iter.hasNext()) {
175             ListElement el = (ListElement)iter.next();
176             if (el.isBox()) {
177                 len += ((KnuthElement)el).getW();
178             } else if (el.isGlue()) {
179                 len += ((KnuthElement)el).getW();
180             } else {
181                 //log.debug("Ignoring penalty: " + el);
182
//ignore penalties
183
}
184             count--;
185             if (count == 0) {
186                 break;
187             }
188         }
189         return len;
190     }
191     
192     /**
193      * Calculates the content length of the given element list. Warning: It doesn't take any
194      * stretch and shrink possibilities into account.
195      * @param elems the element list
196      * @return the content length
197      */

198     public static int calcContentLength(List JavaDoc elems) {
199         return calcContentLength(elems, 0, elems.size() - 1);
200     }
201     
202     /**
203      * Indicates whether the given element list ends with a forced break.
204      * @param elems the element list
205      * @return true if the list ends with a forced break
206      */

207     public static boolean endsWithForcedBreak(LinkedList JavaDoc elems) {
208         ListElement last = (ListElement)elems.getLast();
209         return last.isForcedBreak();
210     }
211     
212     /**
213      * Determines the position of the previous break before the start index on an
214      * element list.
215      * @param elems the element list
216      * @param startIndex the start index
217      * @return the position of the previous break, or -1 if there was no previous break
218      */

219     public static int determinePreviousBreak(List JavaDoc elems, int startIndex) {
220         int prevBreak = startIndex - 1;
221         while (prevBreak >= 0) {
222             KnuthElement el = (KnuthElement)elems.get(prevBreak);
223             if (el.isPenalty() && el.getP() < KnuthElement.INFINITE) {
224                 break;
225             }
226             prevBreak--;
227         }
228         return prevBreak;
229     }
230     
231 }
232
Popular Tags