1 50 51 package org.openlaszlo.iv.flash.api.text; 52 53 import org.openlaszlo.iv.flash.parser.*; 54 import org.openlaszlo.iv.flash.util.*; 55 import org.openlaszlo.iv.flash.api.*; 56 import java.io.*; 57 import java.util.*; 58 59 67 public final class TextRecord extends FlashItem { 68 69 private int size; private char[] text; private int[] indexes; private int[] advances; 74 public TextRecord() {} 75 76 81 public TextRecord( int maxSize ) { 82 text = new char[ maxSize ]; 83 indexes = new int[ maxSize ]; 84 advances = new int[ maxSize ]; 85 size = 0; 86 } 87 88 92 public char[] getText() { return text; } 93 public int[] getIndexes() { return indexes; } 94 public int[] getAdvances() { return advances; } 95 96 102 public char getChar( int i ) { 103 return text[i]; 104 } 105 106 112 public int getIndex( int i ) { 113 return indexes[i]; 114 } 115 116 122 public int getAdvance( int i ) { 123 return advances[i]; 124 } 125 126 132 public void setChar( int i, char ch ) { 133 text[i] = ch; 134 } 135 136 142 public void setIndex( int i, int index ) { 143 indexes[i] = index; 144 } 145 146 152 public void setAdvance( int i, int advance ) { 153 advances[i] = advance; 154 } 155 156 161 public int getSize() { 162 return size; 163 } 164 165 170 public void setSize( int size ) { 171 this.size = size; 172 } 173 174 179 public int getWidth() { 180 int width = 0; 181 for( int i=0; i<size; i++ ) { 182 width += advances[i]; 183 } 184 return width; 185 } 186 187 192 public void updateIndexes( Font font ) { 193 for( int i=0; i<size; i++ ) { 194 char ch = text[i]; 195 int idx = font.getIndex(ch); 197 if( idx == -1 ) idx = 0; indexes[i] = idx; 200 } 201 } 202 203 211 public void add( int index, int advance ) { 212 indexes[size] = index; 213 advances[size] = advance; 214 size++; 215 } 216 217 226 public void add( char ch, int index, int advance ) { 227 text[size] = ch; 228 add( index, advance ); 229 } 230 231 238 public int trimEnd() { 239 int i; 240 int w = 0; 241 for( i=size; --i>=0; ) { 242 if( !Character.isWhitespace(text[i]) ) break; 243 w += advances[i]; 244 } 245 size = i+1; 246 return w; 247 } 248 249 256 public int trimStart() { 257 int i; 258 int w = 0; 259 for( i=0; i<size; i++ ) { 260 if( !Character.isWhitespace(text[i]) ) break; 261 w += advances[i]; 262 } 263 if( i != 0 ) { 264 size -= i; 265 System.arraycopy( text, i, text, 0, size ); 266 System.arraycopy( advances, i, advances, 0, size ); 267 System.arraycopy( indexes, i, indexes, 0, size ); 268 } 269 return w; 270 } 271 272 277 public int getMaxIndex() { 278 return Util.getMax(indexes, size); 279 } 280 281 286 public int getMaxAdvance() { 287 return Util.getMax(advances, size); 288 } 289 290 299 public void write( FlashOutput fob ) { 300 int[] nbits = (int[]) fob.getUserData(); 301 int nGlyphBits = nbits[0]; 302 int nAdvanceBits = nbits[1]; 303 fob.writeByte(size); 304 for( int k=0; k<size; k++ ) { 305 fob.writeBits(indexes[k], nGlyphBits); 306 fob.writeBits(advances[k], nAdvanceBits); 307 } 308 fob.flushBits(); 309 } 310 311 public void printContent( PrintStream out, String indent ) { 312 out.println( indent+"TextRecord: nGlyphs="+size ); 313 for( int k=0; k<size; k++ ) { 314 out.println( indent+" char["+k+"]='"+text[k]+"', advance["+k+"]="+advances[k] ); 315 } 316 } 317 318 protected FlashItem copyInto( FlashItem item, ScriptCopier copier ) { 319 char[] txt = new char[text.length]; 320 System.arraycopy(text, 0, txt, 0, text.length); 321 int[] ind = new int[indexes.length]; 322 System.arraycopy(indexes, 0, ind, 0, indexes.length); 323 int[] adv = new int[advances.length]; 324 System.arraycopy(advances, 0, adv, 0, advances.length); 325 ((TextRecord)item).size = size; 326 ((TextRecord)item).text = txt; 327 ((TextRecord)item).indexes = ind; 328 ((TextRecord)item).advances = adv; 329 return item; 330 } 331 332 public FlashItem getCopy( ScriptCopier copier ) { 333 return copyInto( new TextRecord(), copier ); 334 } 335 } 336 337 | Popular Tags |