1 18 package org.apache.batik.gvt.text; 19 20 import java.text.AttributedCharacterIterator ; 21 import java.text.AttributedString ; 22 import java.text.CharacterIterator ; 23 import java.text.StringCharacterIterator ; 24 import java.util.ArrayList ; 25 import java.util.HashMap ; 26 import java.util.HashSet ; 27 import java.util.Map ; 28 import java.util.Set ; 29 30 42 43 public class GVTACIImpl 44 implements GVTAttributedCharacterIterator { 45 46 private String simpleString; 47 private Set allAttributes; 48 private ArrayList mapList; 49 private static int START_RUN = 2; 50 private static int END_RUN = 3; 51 private static int MID_RUN = 1; 52 private static int SINGLETON = 0; 53 private int[] charInRun; 54 private CharacterIterator iter = null; 55 private int currentIndex = -1; 56 57 61 public GVTACIImpl() { 62 simpleString = ""; 63 buildAttributeTables(); 64 } 65 66 71 public GVTACIImpl(AttributedCharacterIterator aci) { 72 buildAttributeTables(aci); 73 } 74 75 78 public void setString(String s) { 79 simpleString = s; 80 iter = new StringCharacterIterator (simpleString); 81 buildAttributeTables(); 82 } 83 84 87 public void setString(AttributedString s) { 88 iter = s.getIterator(); 89 buildAttributeTables((AttributedCharacterIterator ) iter); 90 } 91 92 105 public void setAttributeArray 106 (GVTAttributedCharacterIterator.TextAttribute attr, 107 Object [] attValues, int beginIndex, int endIndex) { 108 109 beginIndex = Math.max(beginIndex, 0); 110 endIndex = Math.min(endIndex, simpleString.length()); 111 if (charInRun[beginIndex] == END_RUN) { 112 if (charInRun[beginIndex - 1] == MID_RUN) { 113 charInRun[beginIndex - 1] = END_RUN; 114 } else { 115 charInRun[beginIndex - 1] = SINGLETON; 116 } 117 } 118 if (charInRun[endIndex + 1] == END_RUN) { 119 charInRun[endIndex + 1] = SINGLETON; 120 } else if (charInRun[endIndex + 1] == MID_RUN) { 121 charInRun[endIndex + 1] = START_RUN; 122 } 123 for (int i = beginIndex; i <= endIndex; ++i) { 124 charInRun[i] = SINGLETON; 125 int n = Math.min(i, attValues.length - 1); 126 ((Map ) mapList.get(i)).put(attr, attValues[n]); 127 } 128 } 129 130 132 135 public Set getAllAttributeKeys() { 136 return allAttributes; 137 } 138 139 143 public Object getAttribute(AttributedCharacterIterator.Attribute attribute) 144 { 145 return getAttributes().get(attribute); 146 } 147 148 152 public Map getAttributes() { 153 return (Map ) mapList.get(currentIndex); 154 } 155 156 161 public int getRunLimit() { 162 int ndx = currentIndex; 163 do { 164 ++ndx; 165 } while (charInRun[ndx] == MID_RUN); 166 return ndx; 167 } 168 169 174 public int getRunLimit(AttributedCharacterIterator.Attribute attribute) { 175 int ndx = currentIndex; 176 Object value = getAttributes().get(attribute); 177 178 if (value == null) { 180 do { 181 ++ndx; 182 } while (((Map ) mapList.get(ndx)).get(attribute) == null); 183 } else { 184 do { 185 ++ndx; 186 } while (value.equals(((Map ) mapList.get(ndx)).get(attribute))); 187 } 188 return ndx; 189 } 190 191 192 197 public int getRunLimit(Set attributes) { 198 int ndx = currentIndex; 199 do { 200 ++ndx; 201 } while (attributes.equals(mapList.get(ndx))); 202 return ndx; 203 } 204 205 209 public int getRunStart() { 210 int ndx = currentIndex; 211 while (charInRun[ndx] == MID_RUN) --ndx; 212 return ndx; 213 } 214 215 221 public int getRunStart(AttributedCharacterIterator.Attribute attribute) { 222 int ndx = currentIndex - 1; 223 Object value = getAttributes().get(attribute); 224 225 try { 227 if (value == null) { 228 while (((Map ) mapList.get(ndx - 1)).get(attribute) == null) 229 --ndx; 230 } else { 231 while (value.equals( 232 ((Map ) mapList.get(ndx - 1)).get(attribute)) ) 233 --ndx; 234 } 235 } catch(IndexOutOfBoundsException e) { 236 } 237 return ndx; 238 } 239 240 246 public int getRunStart(Set attributes) { 247 int ndx = currentIndex; 248 try { 249 while (attributes.equals(mapList.get(ndx - 1))) --ndx; 250 } catch(IndexOutOfBoundsException e) { 251 } 252 return ndx; 253 } 254 255 257 260 public Object clone() { 261 GVTAttributedCharacterIterator cloneACI = 262 new GVTACIImpl(this); 263 return cloneACI; 264 } 265 266 271 public char current() { 272 return iter.current(); 273 } 274 275 280 public char first() { 281 return iter.first(); 282 } 283 284 288 public int getBeginIndex() { 289 return iter.getBeginIndex(); 290 } 291 292 296 public int getEndIndex() { 297 return iter.getEndIndex(); 298 } 299 300 304 public int getIndex() { 305 return iter.getIndex(); 306 } 307 308 313 public char last() { 314 return iter.last(); 315 } 316 317 322 public char next() { 323 return iter.next(); 324 } 325 326 331 public char previous() { 332 return iter.previous(); 333 } 334 335 341 public char setIndex(int position) { 342 return iter.setIndex(position); 343 } 344 345 347 private void buildAttributeTables() { 348 allAttributes = new HashSet (); 349 mapList = new ArrayList (simpleString.length()); 350 charInRun = new int[simpleString.length()]; 351 for (int i = 0; i < charInRun.length; ++i) { 352 charInRun[i] = SINGLETON; 353 357 mapList.set(i, new HashMap ()); 358 } 359 } 360 361 private void buildAttributeTables(AttributedCharacterIterator aci) { 362 allAttributes = aci.getAllAttributeKeys(); 363 int length = aci.getEndIndex() - aci.getBeginIndex(); 364 mapList = new ArrayList (length); 365 charInRun = new int[length]; 366 char c = aci.first(); 367 char chars[] = new char[length]; 368 for (int i = 0; i < length; ++i) { 369 chars[i] = c; 370 charInRun[i] = SINGLETON; 371 375 mapList.set(i, new HashMap (aci.getAttributes())); 376 c = aci.next(); 377 } 378 simpleString = new String (chars); 379 } 380 381 383 388 public class TransformAttributeFilter implements 389 GVTAttributedCharacterIterator.AttributeFilter { 390 391 396 public AttributedCharacterIterator 397 mutateAttributes(AttributedCharacterIterator aci) { 398 return aci; 400 } 401 } 402 } 403 | Popular Tags |