1 50 51 package org.openlaszlo.iv.flash.api.text; 52 53 import java.io.*; 54 import java.util.*; 55 import java.awt.geom.*; 56 57 import org.openlaszlo.iv.flash.parser.*; 58 import org.openlaszlo.iv.flash.util.*; 59 60 import org.openlaszlo.iv.flash.api.*; 61 import org.openlaszlo.iv.flash.api.text.*; 62 import org.openlaszlo.iv.flash.context.Context; 63 64 71 public final class Text extends FlashDef implements TextBlock { 72 73 public static final int PROPERTY_CONTROLLED = 0; 74 public static final int MM_STYLE = 1; 75 public static final int JG_STYLE = 2; 76 77 private boolean withAlpha; private Rectangle2D bounds; private Rectangle2D genBounds; private int boundsStyle = PROPERTY_CONTROLLED; private AffineTransform matrix; private IVVector items = new IVVector(); private TextLayout myLayout; 85 public Text() {} 86 87 public Text( boolean withAlpha ) { 88 this.withAlpha = withAlpha; 89 } 90 91 public static Text newText() { 92 Text t = new Text(true); 93 t.matrix = new AffineTransform(); 94 return t; 95 } 96 97 public int getTag() { 98 if( withAlpha ) return Tag.DEFINETEXT2; 99 return Tag.DEFINETEXT; 100 } 101 102 105 108 public void layout() { 109 if( myLayout != null ) return; 110 doLayout(); 111 } 112 113 120 public IVVector getTextRecords( Font font ) { 121 layout(); 122 return myLayout.getTextRecords( font ); 123 } 124 125 130 public IVVector getAllTextRecords() { 131 layout(); 132 return myLayout.getAllTextRecords(); 133 } 134 135 144 public void changeFont( Font old_font, Font new_font ) { 145 layout(); 146 myLayout.changeFont(old_font, new_font); 147 } 148 149 151 public void setBoundsStyle( int boundsStyle ) { 152 this.boundsStyle = boundsStyle; 153 } 154 155 public int getBoundsStyle() { 156 return boundsStyle; 157 } 158 159 protected void doLayout() { 160 myLayout = new TextLayout(this,genBounds!=null?genBounds:bounds); 161 myLayout.layout(); 162 } 163 164 public void collectDeps( DepsCollector dc ) { 165 for( int i=0; i<items.size(); i++ ) { 166 TextItem t = (TextItem) items.elementAt(i); 167 if( t.font != null ) dc.addDep( t.font ); 168 } 169 } 170 171 public void collectFonts( FontsCollector fc ) { 172 for( int i=0; i<items.size(); i++ ) { 173 TextItem t = (TextItem) items.elementAt(i); 174 if( t.font != null ) fc.addFont( t.font, this ); 175 } 176 } 177 178 public void apply( Context context ) { 179 if( myLayout != null ) return; 180 super.apply( context ); 181 for( int i=0; i<items.size(); i++ ) { 182 TextItem t = (TextItem) items.elementAt(i); 183 t.apply( context ); 184 } 185 doLayout(); 187 } 188 189 public boolean isConstant() { 190 for( int i=0; i<items.size(); i++ ) { 191 TextItem t = (TextItem) items.elementAt(i); 192 if( !t.isConstant() ) return false; 193 } 194 return true; 195 } 196 197 public Rectangle2D getBounds() { 198 layout(); 200 return bounds; 201 } 202 203 public void setGenBounds( Rectangle2D bounds ) { 204 this.genBounds = bounds; 205 } 206 207 public void setBounds( Rectangle2D bounds ) { 208 this.bounds = bounds; 209 } 210 211 public void setBounds( int x, int y, int width, int height ) { 212 setBounds( GeomHelper.newRectangle(x,y,width,height) ); 213 } 214 215 220 public IVVector getTextItems() { 221 return items; 222 } 223 224 229 public void setTextItems( IVVector items ) { 230 this.items = items; 231 } 232 233 239 public TextItem addTextItem( TextItem item ) { 240 items.addElement(item); 241 return item; 242 } 243 244 public void setMatrix( AffineTransform matrix ) { 245 this.matrix = matrix; 246 } 247 248 public AffineTransform getMatrix() { 249 return matrix; 250 } 251 252 262 public static FlashDef parse( Parser p, boolean withAlpha ) { 263 if( !p.getFile().isTemplate() ) { 264 return LazyText.parse( p, withAlpha ); 265 } else { 266 Text text = new Text( withAlpha ); 267 text.setID( p.getUWord() ); 269 text.bounds = p.getRect(); 271 text.matrix = p.getMatrix(); 272 273 p.skipLastTag(); 275 276 return text; 277 } 278 } 279 280 285 public void parseGenText( Parser p ) { 286 genBounds = p.getRect(); 287 288 TextItem item = new TextItem(); 289 boolean wasText = false; 290 for(;;) { 291 int code = p.getUByte(); 292 if( code == 0 ) { 293 items.addElement( item ); 294 break; 295 } 296 if( (code&0x80) != 0 ) { 297 if( wasText ) { 299 items.addElement( item ); 301 item = (TextItem) item.getCopy(null); 302 item.text = null; 303 wasText = false; 304 } 305 code = code&0x7f; 306 switch( code ) { 307 case 0: item.style = p.getUByte(); 309 break; 310 case 1: item.font = ((FontDef)p.getDef(p.getUWord())).getFont(); 312 break; 313 case 2: item.height = p.getUWord(); 315 break; 316 case 3: item.color = Color.parseRGBA(p); 318 break; 319 case 4: item.script = p.getUByte(); 321 break; 322 case 5: item.kerning = p.getWord(); 324 break; 325 case 8: item.align = p.getUByte(); 327 break; 328 case 9: item.indent = p.getWord(); 330 break; 331 case 10: item.marginleft = p.getWord(); 333 break; 334 case 11: item.marginright = p.getWord(); 336 break; 337 case 12: item.linesp = p.getWord(); 339 break; 340 default: 341 Log.logRB( Resource.UNKNOWNGENTEXT, new Object [] {Util.b2h(code)} ); 343 p.getUByte(); 344 break; 345 } 346 } else { 347 348 String encoding = p.getFile().getEncoding(); 349 int length = code*2; 350 String mystr; 351 352 if( item.font != null && (item.font.flags&Font.UNICODE) != 0 ) { 353 byte[] buf = p.getTempByteBuf(length); 356 int size = gen2DBCSbytes(p.getBuf(), p.getPos(), length, buf); 357 if( encoding == null ) { 358 mystr = new String (buf, 0, size); 359 } else { 360 try { 361 mystr = new String (buf, 0, size, encoding); 362 } catch( UnsupportedEncodingException e ) { 363 Log.log(e); 364 mystr = new String (buf, 0, size); 365 } 366 } 367 } else { 369 char[] buf = p.getTempCharBuf(length/2); 372 int size = gen2DBCSchars(p.getBuf(), p.getPos(), length, buf); 373 mystr = new String (buf, 0, size); 374 } 376 377 item.text = item.text == null? mystr: item.text+mystr; 378 379 p.skip(length); 380 381 wasText = true; 382 } 383 } 384 } 385 386 399 public static int gen2DBCSbytes( byte[] buf, int pos, int length, byte[] outbuf ) { 400 int size = 0; 401 for( int i=0; i<length; i+=2 ) { 402 byte hi = buf[pos++]; 403 byte lo = buf[pos++]; 404 if( lo == 0 ) { 405 outbuf[size++] = hi; 406 } else { 407 outbuf[size+1] = hi; 408 outbuf[size] = lo; 409 size+=2; 410 } 411 } 412 return size; 413 } 414 415 427 public static int gen2DBCSchars( byte[] buf, int pos, int length, char[] outbuf ) { 428 int size = 0; 429 for( int i=0; i<length; i+=2 ) { 430 int hi = buf[pos++]&0xff; 431 int lo = buf[pos++]&0xff; 432 if( lo == 0 ) { 433 outbuf[size++] = (char) hi; 434 } else { 435 outbuf[size++] = (char) ((hi<<8) | lo); 436 } 437 } 438 return size; 439 } 440 441 public void write( FlashOutput fob ) { 442 write(fob, this); 443 } 444 445 public void write( FlashOutput fob, FlashDef def ) { 446 int start = fob.getPos(); fob.skip( 6 ); fob.writeDefID(def); 449 450 layout(); 451 fob.write(bounds); 452 fob.write(matrix); 453 454 myLayout.write(fob); 455 456 int size = fob.getPos()-start-6; fob.writeLongTagAt(Tag.DEFINETEXT2, size, start ); 459 } 460 461 public void printContent( PrintStream out, String indent ) { 462 out.println( indent+"Text: id="+getID() ); 463 out.println( indent+" bounds: "+bounds ); 464 out.println( indent+" genBounds: "+genBounds ); 465 out.println( indent+" "+matrix ); 466 467 TextItem last = new TextItem(); 468 for( int i=0; i<items.size(); i++ ) { 469 TextItem t = (TextItem) items.elementAt(i); 470 out.println( indent+" TextItem: '"+t.text+"'" ); 471 if( t.font != last.font ) out.println( indent+" font : '"+t.font.getFontName()+"'" ); 472 if( t.height != last.height ) out.println( indent+" height: "+t.height ); 473 if( t.style != last.style ) out.println( indent+" style : "+t.style ); 474 if( t.align != last.align ) out.println( indent+" align : "+t.align ); 475 if( t.indent != last.indent ) out.println( indent+" indent: "+t.indent ); 476 if( t.linesp != last.linesp ) out.println( indent+" linesp: "+t.linesp ); 477 if( t.kerning != last.kerning ) out.println( indent+" kerning: "+t.kerning ); 478 if( t.script != last.script ) out.println( indent+" script: "+t.script ); 479 if( t.marginleft != last.marginleft ) out.println( indent+" margin left: "+t.marginleft ); 480 if( t.marginright != last.marginright ) out.println( indent+" margin right : "+t.marginright ); 481 if( t.color != last.color ) t.color.printContent(out, indent+" "); 482 last = t; 483 } 484 } 485 486 protected FlashItem copyInto( FlashItem item, ScriptCopier copier ) { 487 super.copyInto( item, copier ); 488 ((Text)item).withAlpha = withAlpha; 489 ((Text)item).bounds = (Rectangle2D) bounds.clone(); 490 ((Text)item).genBounds = genBounds==null?null:(Rectangle2D)genBounds.clone(); 491 ((Text)item).matrix = (AffineTransform) matrix.clone(); 492 ((Text)item).items = items.getCopy(copier); 493 ((Text)item).myLayout = myLayout==null?null:myLayout.getCopy(copier); 494 return item; 495 } 496 497 public FlashItem getCopy( ScriptCopier copier ) { 498 return copyInto( new Text(), copier ); 499 } 500 } 501 | Popular Tags |