1 16 17 18 19 package com.lowagie.text.pdf.hyphenation; 20 21 import java.io.InputStream ; 22 import java.util.ArrayList ; 23 import java.util.HashMap ; 24 25 32 public class HyphenationTree extends TernaryTree 33 implements PatternConsumer { 34 35 private static final long serialVersionUID = -7763254239309429432L; 36 37 40 protected ByteVector vspace; 41 42 45 protected HashMap stoplist; 46 47 50 protected TernaryTree classmap; 51 52 55 private transient TernaryTree ivalues; 56 57 public HyphenationTree() { 58 stoplist = new HashMap (23); classmap = new TernaryTree(); 60 vspace = new ByteVector(); 61 vspace.alloc(1); } 63 64 73 protected int packValues(String values) { 74 int i, n = values.length(); 75 int m = (n & 1) == 1 ? (n >> 1) + 2 : (n >> 1) + 1; 76 int offset = vspace.alloc(m); 77 byte[] va = vspace.getArray(); 78 for (i = 0; i < n; i++) { 79 int j = i >> 1; 80 byte v = (byte)((values.charAt(i) - '0' + 1) & 0x0f); 81 if ((i & 1) == 1) { 82 va[j + offset] = (byte)(va[j + offset] | v); 83 } else { 84 va[j + offset] = (byte)(v << 4); } 86 } 87 va[m - 1 + offset] = 0; return offset; 89 } 90 91 protected String unpackValues(int k) { 92 StringBuffer buf = new StringBuffer (); 93 byte v = vspace.get(k++); 94 while (v != 0) { 95 char c = (char)((v >>> 4) - 1 + '0'); 96 buf.append(c); 97 c = (char)(v & 0x0f); 98 if (c == 0) { 99 break; 100 } 101 c = (char)(c - 1 + '0'); 102 buf.append(c); 103 v = vspace.get(k++); 104 } 105 return buf.toString(); 106 } 107 108 public void loadSimplePatterns(InputStream stream) { 109 SimplePatternParser pp = new SimplePatternParser(); 110 ivalues = new TernaryTree(); 111 112 pp.parse(stream, this); 113 114 trimToSize(); 117 vspace.trimToSize(); 118 classmap.trimToSize(); 119 120 ivalues = null; 122 } 123 124 125 public String findPattern(String pat) { 126 int k = super.find(pat); 127 if (k >= 0) { 128 return unpackValues(k); 129 } 130 return ""; 131 } 132 133 137 protected int hstrcmp(char[] s, int si, char[] t, int ti) { 138 for (; s[si] == t[ti]; si++, ti++) { 139 if (s[si] == 0) { 140 return 0; 141 } 142 } 143 if (t[ti] == 0) { 144 return 0; 145 } 146 return s[si] - t[ti]; 147 } 148 149 protected byte[] getValues(int k) { 150 StringBuffer buf = new StringBuffer (); 151 byte v = vspace.get(k++); 152 while (v != 0) { 153 char c = (char)((v >>> 4) - 1); 154 buf.append(c); 155 c = (char)(v & 0x0f); 156 if (c == 0) { 157 break; 158 } 159 c = (char)(c - 1); 160 buf.append(c); 161 v = vspace.get(k++); 162 } 163 byte[] res = new byte[buf.length()]; 164 for (int i = 0; i < res.length; i++) { 165 res[i] = (byte)buf.charAt(i); 166 } 167 return res; 168 } 169 170 194 protected void searchPatterns(char[] word, int index, byte[] il) { 195 byte[] values; 196 int i = index; 197 char p, q; 198 char sp = word[i]; 199 p = root; 200 201 while (p > 0 && p < sc.length) { 202 if (sc[p] == 0xFFFF) { 203 if (hstrcmp(word, i, kv.getArray(), lo[p]) == 0) { 204 values = getValues(eq[p]); int j = index; 206 for (int k = 0; k < values.length; k++) { 207 if (j < il.length && values[k] > il[j]) { 208 il[j] = values[k]; 209 } 210 j++; 211 } 212 } 213 return; 214 } 215 int d = sp - sc[p]; 216 if (d == 0) { 217 if (sp == 0) { 218 break; 219 } 220 sp = word[++i]; 221 p = eq[p]; 222 q = p; 223 224 while (q > 0 && q < sc.length) { 227 if (sc[q] == 0xFFFF) { break; 229 } 230 if (sc[q] == 0) { 231 values = getValues(eq[q]); 232 int j = index; 233 for (int k = 0; k < values.length; k++) { 234 if (j < il.length && values[k] > il[j]) { 235 il[j] = values[k]; 236 } 237 j++; 238 } 239 break; 240 } else { 241 q = lo[q]; 242 243 248 } 249 } 250 } else { 251 p = d < 0 ? lo[p] : hi[p]; 252 } 253 } 254 } 255 256 266 public Hyphenation hyphenate(String word, int remainCharCount, 267 int pushCharCount) { 268 char[] w = word.toCharArray(); 269 return hyphenate(w, 0, w.length, remainCharCount, pushCharCount); 270 } 271 272 294 295 307 public Hyphenation hyphenate(char[] w, int offset, int len, 308 int remainCharCount, int pushCharCount) { 309 int i; 310 char[] word = new char[len + 3]; 311 312 char[] c = new char[2]; 314 int iIgnoreAtBeginning = 0; 315 int iLength = len; 316 boolean bEndOfLetters = false; 317 for (i = 1; i <= len; i++) { 318 c[0] = w[offset + i - 1]; 319 int nc = classmap.find(c, 0); 320 if (nc < 0) { if (i == (1 + iIgnoreAtBeginning)) { 322 iIgnoreAtBeginning ++; 324 } else { 325 bEndOfLetters = true; 327 } 328 iLength --; 329 } else { 330 if (!bEndOfLetters) { 331 word[i - iIgnoreAtBeginning] = (char)nc; 332 } else { 333 return null; 334 } 335 } 336 } 337 len = iLength; 338 if (len < (remainCharCount + pushCharCount)) { 339 return null; 341 } 342 int[] result = new int[len + 1]; 343 int k = 0; 344 345 String sw = new String (word, 1, len); 347 if (stoplist.containsKey(sw)) { 348 ArrayList hw = (ArrayList )stoplist.get(sw); 350 int j = 0; 351 for (i = 0; i < hw.size(); i++) { 352 Object o = hw.get(i); 353 if (o instanceof String ) { 356 j += ((String )o).length(); 357 if (j >= remainCharCount && j < (len - pushCharCount)) { 358 result[k++] = j + iIgnoreAtBeginning; 359 } 360 } 361 } 362 } else { 363 word[0] = '.'; word[len + 1] = '.'; word[len + 2] = 0; byte[] il = new byte[len + 3]; for (i = 0; i < len + 1; i++) { 369 searchPatterns(word, i, il); 370 } 371 372 for (i = 0; i < len; i++) { 377 if (((il[i + 1] & 1) == 1) && i >= remainCharCount 378 && i <= (len - pushCharCount)) { 379 result[k++] = i + iIgnoreAtBeginning; 380 } 381 } 382 } 383 384 385 if (k > 0) { 386 int[] res = new int[k]; 388 System.arraycopy(result, 0, res, 0, k); 389 return new Hyphenation(new String (w, offset, len), res); 390 } else { 391 return null; 392 } 393 } 394 395 407 public void addClass(String chargroup) { 408 if (chargroup.length() > 0) { 409 char equivChar = chargroup.charAt(0); 410 char[] key = new char[2]; 411 key[1] = 0; 412 for (int i = 0; i < chargroup.length(); i++) { 413 key[0] = chargroup.charAt(i); 414 classmap.insert(key, 0, equivChar); 415 } 416 } 417 } 418 419 427 public void addException(String word, ArrayList hyphenatedword) { 428 stoplist.put(word, hyphenatedword); 429 } 430 431 441 public void addPattern(String pattern, String ivalue) { 442 int k = ivalues.find(ivalue); 443 if (k <= 0) { 444 k = packValues(ivalue); 445 ivalues.insert(ivalue, (char)k); 446 } 447 insert(pattern, (char)k); 448 } 449 450 public void printStats() { 451 System.out.println("Value space size = " 452 + Integer.toString(vspace.length())); 453 super.printStats(); 454 } 455 } 456 | Popular Tags |