KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > fop > layoutmgr > inline > LineLayoutPossibilities


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: LineLayoutPossibilities.java 426576 2006-07-28 15:44:37Z jeremias $ */
19
20 package org.apache.fop.layoutmgr.inline;
21
22 import java.util.List JavaDoc;
23
24 import org.apache.commons.logging.Log;
25 import org.apache.commons.logging.LogFactory;
26 import org.apache.fop.layoutmgr.Position;
27
28 public class LineLayoutPossibilities {
29
30     /** logger instance */
31     protected static Log log = LogFactory.getLog(LineLayoutPossibilities.class);
32     
33     private class Possibility {
34         private int lineCount;
35         private double demerits;
36         private List JavaDoc breakPositions;
37
38         private Possibility(int lc, double dem) {
39             lineCount = lc;
40             demerits = dem;
41             breakPositions = new java.util.ArrayList JavaDoc(lc);
42         }
43
44         private int getLineCount() {
45             return lineCount;
46         }
47
48         private double getDemerits() {
49             return demerits;
50         }
51
52         private void addBreakPosition(Position pos) {
53             // Positions are always added with index 0 because
54
// they are created backward, from the last one to
55
// the first one
56
breakPositions.add(0, pos);
57         }
58
59         private Position getBreakPosition(int i) {
60             return (Position)breakPositions.get(i);
61         }
62     }
63
64     private List JavaDoc possibilitiesList;
65     private List JavaDoc savedPossibilities;
66     private int minimumIndex;
67     private int optimumIndex;
68     private int maximumIndex;
69     private int chosenIndex;
70     private int savedOptLineCount;
71
72     public LineLayoutPossibilities() {
73         possibilitiesList = new java.util.ArrayList JavaDoc();
74         savedPossibilities = new java.util.ArrayList JavaDoc();
75         optimumIndex = -1;
76     }
77  
78     public void addPossibility(int ln, double dem) {
79         possibilitiesList.add(new Possibility(ln, dem));
80         if (possibilitiesList.size() == 1) {
81             // first Possibility added
82
minimumIndex = 0;
83             optimumIndex = 0;
84             maximumIndex = 0;
85             chosenIndex = 0;
86         } else {
87             if (dem < ((Possibility)possibilitiesList.get(optimumIndex)).getDemerits()) {
88                 optimumIndex = possibilitiesList.size() - 1;
89                 chosenIndex = optimumIndex;
90             }
91             if (ln < ((Possibility)possibilitiesList.get(minimumIndex)).getLineCount()) {
92                 minimumIndex = possibilitiesList.size() - 1;
93             }
94             if (ln > ((Possibility)possibilitiesList.get(maximumIndex)).getLineCount()) {
95                 maximumIndex = possibilitiesList.size() - 1;
96             }
97         }
98     }
99
100     /* save in a different array the computed Possibilities,
101      * so possibilitiesList is ready to store different Possibilities
102      */

103     public void savePossibilities(boolean bSaveOptLineCount) {
104         if (bSaveOptLineCount) {
105             savedOptLineCount = getOptLineCount();
106         } else {
107             savedOptLineCount = 0;
108         }
109         savedPossibilities = possibilitiesList;
110         possibilitiesList = new java.util.ArrayList JavaDoc();
111     }
112
113     /* replace the Possibilities stored in possibilitiesList with
114      * the ones stored in savedPossibilities and having the same line number
115      */

116     public void restorePossibilities() {
117         int index = 0;
118         while (savedPossibilities.size() > 0) {
119             Possibility restoredPossibility = (Possibility) savedPossibilities.remove(0);
120             if (restoredPossibility.getLineCount() < getMinLineCount()) {
121                 // if the line number of restoredPossibility is less than the minimum one,
122
// add restoredPossibility at the beginning of the list
123
possibilitiesList.add(0, restoredPossibility);
124                 // update minimumIndex
125
minimumIndex = 0;
126                 // shift the other indexes;
127
optimumIndex ++;
128                 maximumIndex ++;
129                 chosenIndex ++;
130             } else if (restoredPossibility.getLineCount() > getMaxLineCount()) {
131                 // if the line number of restoredPossibility is greater than the maximum one,
132
// add restoredPossibility at the end of the list
133
possibilitiesList.add(possibilitiesList.size(), restoredPossibility);
134                 // update maximumIndex
135
maximumIndex = possibilitiesList.size() - 1;
136                 index = maximumIndex;
137             } else {
138                 // find the index of the Possibility that will be replaced
139
while (index < maximumIndex
140                        && getLineCount(index) < restoredPossibility.getLineCount()) {
141                     index ++;
142                 }
143                 if (getLineCount(index) == restoredPossibility.getLineCount()) {
144                     possibilitiesList.set(index, restoredPossibility);
145                 } else {
146                     // this should not happen
147
log.error("LineLayoutPossibilities restorePossibilities(),"
148                         + " min= " + getMinLineCount()
149                         + " max= " + getMaxLineCount()
150                         + " restored= " + restoredPossibility.getLineCount());
151                     return;
152                 }
153             }
154             // update optimumIndex and chosenIndex
155
if (savedOptLineCount == 0 && getDemerits(optimumIndex) > restoredPossibility.getDemerits()
156                 || savedOptLineCount != 0 && restoredPossibility.getLineCount() == savedOptLineCount) {
157                 optimumIndex = index;
158                 chosenIndex = optimumIndex;
159             }
160         }
161         //log.debug(">> minLineCount = " + getMinLineCount()
162
// + " optLineCount = " + getOptLineCount() + " maxLineCount() = " + getMaxLineCount());
163
}
164
165     public void addBreakPosition(Position pos, int i) {
166         ((Possibility)possibilitiesList.get(i)).addBreakPosition(pos);
167     }
168
169     public boolean canUseMoreLines() {
170         return (getOptLineCount() < getMaxLineCount());
171     }
172
173     public boolean canUseLessLines() {
174         return (getMinLineCount() < getOptLineCount());
175     }
176
177     public int getMinLineCount() {
178         return getLineCount(minimumIndex);
179     }
180
181     public int getOptLineCount() {
182         return getLineCount(optimumIndex);
183     }
184
185     public int getMaxLineCount() {
186         return getLineCount(maximumIndex);
187     }
188
189     public int getChosenLineCount() {
190         return getLineCount(chosenIndex);
191     }
192
193     public int getLineCount(int i) {
194         return ((Possibility)possibilitiesList.get(i)).getLineCount();
195     }
196
197     public double getChosenDemerits() {
198         return getDemerits(chosenIndex);
199     }
200
201     public double getDemerits(int i) {
202         return ((Possibility)possibilitiesList.get(i)).getDemerits();
203     }
204
205     public int getPossibilitiesNumber() {
206         return possibilitiesList.size();
207     }
208
209     public Position getChosenPosition(int i) {
210         return ((Possibility)possibilitiesList.get(chosenIndex)).getBreakPosition(i);
211     }
212
213     public int applyLineCountAdjustment(int adj) {
214         if (adj >= (getMinLineCount() - getChosenLineCount())
215             && adj <= (getMaxLineCount() - getChosenLineCount())
216             && getLineCount(chosenIndex + adj) == getChosenLineCount() + adj) {
217             chosenIndex += adj;
218             log.debug("chosenLineCount= " + (getChosenLineCount() - adj) + " adjustment= " + adj
219                                + " => chosenLineCount= " + getLineCount(chosenIndex));
220             return adj;
221         } else {
222             // this should not happen!
223
log.warn("Cannot apply the desired line count adjustment.");
224             return 0;
225         }
226     }
227
228     public void printAll() {
229         System.out.println("++++++++++");
230         System.out.println(" " + possibilitiesList.size() + " possibility':");
231         for (int i = 0; i < possibilitiesList.size(); i ++) {
232             System.out.println(" " + ((Possibility)possibilitiesList.get(i)).getLineCount()
233                                + (i == optimumIndex ? " *" : "")
234                                + (i == minimumIndex ? " -" : "")
235                                + (i == maximumIndex ? " +" : ""));
236         }
237         System.out.println("++++++++++");
238     }
239 }
240
Popular Tags