1 17 18 19 20 package org.apache.fop.layoutmgr.inline; 21 22 import java.util.List ; 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 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 breakPositions; 37 38 private Possibility(int lc, double dem) { 39 lineCount = lc; 40 demerits = dem; 41 breakPositions = new java.util.ArrayList (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 breakPositions.add(0, pos); 57 } 58 59 private Position getBreakPosition(int i) { 60 return (Position)breakPositions.get(i); 61 } 62 } 63 64 private List possibilitiesList; 65 private List 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 (); 74 savedPossibilities = new java.util.ArrayList (); 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 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 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 (); 111 } 112 113 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 possibilitiesList.add(0, restoredPossibility); 124 minimumIndex = 0; 126 optimumIndex ++; 128 maximumIndex ++; 129 chosenIndex ++; 130 } else if (restoredPossibility.getLineCount() > getMaxLineCount()) { 131 possibilitiesList.add(possibilitiesList.size(), restoredPossibility); 134 maximumIndex = possibilitiesList.size() - 1; 136 index = maximumIndex; 137 } else { 138 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 log.error("LineLayoutPossibilities restorePossibilities()," 148 + " min= " + getMinLineCount() 149 + " max= " + getMaxLineCount() 150 + " restored= " + restoredPossibility.getLineCount()); 151 return; 152 } 153 } 154 if (savedOptLineCount == 0 && getDemerits(optimumIndex) > restoredPossibility.getDemerits() 156 || savedOptLineCount != 0 && restoredPossibility.getLineCount() == savedOptLineCount) { 157 optimumIndex = index; 158 chosenIndex = optimumIndex; 159 } 160 } 161 } 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 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 |