1 7 8 22 23 package java.awt.font; 24 25 31 32 class TextJustifier { 33 private GlyphJustificationInfo [] info; 34 private int start; 35 private int limit; 36 37 static boolean DEBUG = false; 38 39 43 TextJustifier(GlyphJustificationInfo [] info, int start, int limit) { 44 this.info = info; 45 this.start = start; 46 this.limit = limit; 47 48 if (DEBUG) { 49 System.out.println("start: " + start + ", limit: " + limit); 50 for (int i = start; i < limit; i++) { 51 GlyphJustificationInfo gji = info[i]; 52 System.out.println("w: " + gji.weight + ", gp: " + 53 gji.growPriority + ", gll: " + 54 gji.growLeftLimit + ", grl: " + 55 gji.growRightLimit); 56 } 57 } 58 } 59 60 public static final int MAX_PRIORITY = 3; 61 62 69 public float[] justify(float delta) { 70 float[] deltas = new float[info.length * 2]; 71 72 boolean grow = delta > 0; 73 74 if (DEBUG) 75 System.out.println("delta: " + delta); 76 77 int fallbackPriority = -1; 80 for (int p = 0; delta != 0; p++) { 81 85 boolean lastPass = p > MAX_PRIORITY; 86 if (lastPass) 87 p = fallbackPriority; 88 89 float weight = 0; 91 float gslimit = 0; 92 float absorbweight = 0; 93 for (int i = start; i < limit; i++) { 94 GlyphJustificationInfo gi = info[i]; 95 if ((grow ? gi.growPriority : gi.shrinkPriority) == p) { 96 if (fallbackPriority == -1) { 97 fallbackPriority = p; 98 } 99 100 if (i != start) { weight += gi.weight; 102 if (grow) { 103 gslimit += gi.growLeftLimit; 104 if (gi.growAbsorb) { 105 absorbweight += gi.weight; 106 } 107 } else { 108 gslimit += gi.shrinkLeftLimit; 109 if (gi.shrinkAbsorb) { 110 absorbweight += gi.weight; 111 } 112 } 113 } 114 115 if (i + 1 != limit) { weight += gi.weight; 117 if (grow) { 118 gslimit += gi.growRightLimit; 119 if (gi.growAbsorb) { 120 absorbweight += gi.weight; 121 } 122 } else { 123 gslimit += gi.shrinkRightLimit; 124 if (gi.shrinkAbsorb) { 125 absorbweight += gi.weight; 126 } 127 } 128 } 129 } 130 } 131 132 if (!grow) { 134 gslimit = -gslimit; } 136 boolean hitLimit = (weight == 0) || (!lastPass && ((delta < 0) == (delta < gslimit))); 137 boolean absorbing = hitLimit && absorbweight > 0; 138 139 float weightedDelta = delta / weight; 142 float weightedAbsorb = 0; 143 if (hitLimit && absorbweight > 0) { 144 weightedAbsorb = (delta - gslimit) / absorbweight; 145 } 146 147 if (DEBUG) { 148 System.out.println("pass: " + p + 149 ", d: " + delta + 150 ", l: " + gslimit + 151 ", w: " + weight + 152 ", aw: " + absorbweight + 153 ", wd: " + weightedDelta + 154 ", wa: " + weightedAbsorb + 155 ", hit: " + (hitLimit ? "y" : "n")); 156 } 157 158 int n = start * 2; 160 for (int i = start; i < limit; i++) { 161 GlyphJustificationInfo gi = info[i]; 162 if ((grow ? gi.growPriority : gi.shrinkPriority) == p) { 163 if (i != start) { float d; 165 if (hitLimit) { 166 d = grow ? gi.growLeftLimit : -gi.shrinkLeftLimit; 168 if (absorbing) { 169 d += gi.weight * weightedAbsorb; 171 } 172 } else { 173 d = gi.weight * weightedDelta; 175 } 176 177 deltas[n] += d; 178 } 179 n++; 180 181 if (i + 1 != limit) { float d; 183 if (hitLimit) { 184 d = grow ? gi.growRightLimit : -gi.shrinkRightLimit; 185 if (absorbing) { 186 d += gi.weight * weightedAbsorb; 187 } 188 } else { 189 d = gi.weight * weightedDelta; 190 } 191 192 deltas[n] += d; 193 } 194 n++; 195 } else { 196 n += 2; 197 } 198 } 199 200 if (!lastPass && hitLimit && !absorbing) { 201 delta -= gslimit; 202 } else { 203 delta = 0; } 205 } 206 207 if (DEBUG) { 208 float total = 0; 209 for (int i = 0; i < deltas.length; i++) { 210 total += deltas[i]; 211 System.out.print(deltas[i] + ", "); 212 if (i % 20 == 9) { 213 System.out.println(); 214 } 215 } 216 System.out.println("\ntotal: " + total); 217 System.out.println(); 218 } 219 220 return deltas; 221 } 222 } 223 | Popular Tags |