1 17 18 19 20 package org.apache.fop.layoutmgr; 21 22 import java.util.LinkedList ; 23 import java.util.List ; 24 import java.util.ListIterator ; 25 26 import org.apache.fop.traits.MinOptMax; 27 28 31 public class ElementListUtils { 32 33 37 public static void removeLegalBreaks(LinkedList elements) { 38 ListIterator i = elements.listIterator(); 39 while (i.hasNext()) { 40 ListElement el = (ListElement)i.next(); 41 if (el.isPenalty()) { 42 BreakElement breakPoss = (BreakElement)el; 43 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 66 public static boolean removeLegalBreaks(LinkedList elements, MinOptMax constraint) { 67 return removeLegalBreaks(elements, constraint.opt); 68 } 69 70 78 public static boolean removeLegalBreaks(LinkedList elements, int constraint) { 79 int len = 0; 80 ListIterator iter = elements.listIterator(); 81 while (iter.hasNext()) { 82 ListElement el = (ListElement)iter.next(); 83 if (el.isPenalty()) { 84 KnuthPenalty penalty = (KnuthPenalty)el; 85 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 125 public static boolean removeLegalBreaksFromEnd(LinkedList elements, int constraint) { 126 int len = 0; 127 ListIterator 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 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 170 public static int calcContentLength(List elems, int start, int end) { 171 ListIterator 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 } 184 count--; 185 if (count == 0) { 186 break; 187 } 188 } 189 return len; 190 } 191 192 198 public static int calcContentLength(List elems) { 199 return calcContentLength(elems, 0, elems.size() - 1); 200 } 201 202 207 public static boolean endsWithForcedBreak(LinkedList elems) { 208 ListElement last = (ListElement)elems.getLast(); 209 return last.isForcedBreak(); 210 } 211 212 219 public static int determinePreviousBreak(List 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 |