1 50 51 package org.openlaszlo.iv.flash.fop; 52 53 import org.xml.sax.InputSource ; 54 55 import org.apache.fop.configuration.*; 56 import org.apache.fop.fo.properties.*; 57 import org.apache.fop.layout.*; 58 import org.apache.fop.layout.inline.*; 59 import org.apache.fop.messaging.MessageHandler; 60 import org.apache.fop.datatypes.*; 61 import org.apache.fop.image.*; 62 import org.apache.fop.svg.*; 63 import org.apache.fop.render.pdf.*; 64 import org.apache.fop.viewer.*; 65 import org.apache.fop.apps.*; 66 67 import java.io.*; 68 import java.util.*; 69 70 77 78 public class SWFRenderer implements org.apache.fop.render.Renderer 79 { 80 private int currentPosX = 0; 81 private int currentPosY = 0; 82 private int currentACPosX = 0; 83 private int currentPage = 0; 84 private int movieWidth = 0; 85 private int movieHeight = 0; 86 private AreaTree tree; 87 private Hashtable fontNames; 88 private Hashtable fontStyles; 89 90 private FOPScriptBuilder flashMovie = new FOPScriptBuilder(); 91 92 public void startRenderer( OutputStream outputStream ) throws IOException { } 93 94 95 public void stopRenderer( OutputStream outputStream ) throws IOException { } 96 97 100 101 public SWFRenderer( ) { } 102 103 106 107 public FOPScriptBuilder getScriptBuilder() 108 { 109 return flashMovie; 110 } 111 112 public void setLinkHandler( String s ) 113 { 114 flashMovie.setLinkHandler( s ); 115 } 116 117 122 123 public void setupFontInfo( org.apache.fop.layout.FontInfo fontInfo ) 124 { 125 org.apache.fop.render.pdf.FontSetup.setup( fontInfo ); 126 127 ConfigurationReader reader = 128 new ConfigurationReader ( 129 new InputSource ( org.openlaszlo.iv.flash.util.Util.getInstallDir() 130 + java.io.File.separator 131 + org.openlaszlo.iv.flash.util.PropertyManager 132 .getProperty( "org.openlaszlo.iv.flash.fopConfig" ) ) ); 133 134 try 135 { 136 reader.start(); 137 } 138 catch ( org.apache.fop.apps.FOPException error ) 139 { 140 MessageHandler.errorln( "Unable to read user configuration file." ); 141 } 142 143 String internalName = null; 144 int fontNumber = 0; 145 146 Vector fontInfos = Configuration.getFonts(); 147 148 if ( fontInfos == null ) 149 { 150 return ; 151 } 152 153 for ( Enumeration e = fontInfos.elements(); e.hasMoreElements(); ) 154 { 155 org.apache.fop.configuration.FontInfo configFontInfo = 156 ( org.apache.fop.configuration.FontInfo ) e.nextElement(); 157 158 String fontFile = configFontInfo.getEmbedFile(); 159 160 internalName = "F" + ( fontNumber ++ ); 161 162 fontInfo.addMetrics( internalName, new SWFFontMetric( fontFile ) ); 163 164 Vector triplets = configFontInfo.getFontTriplets(); 165 166 for ( Enumeration t = triplets.elements(); t.hasMoreElements(); ) 167 { 168 FontTriplet triplet = ( FontTriplet ) t.nextElement(); 169 170 fontInfo.addFontProperties( internalName, 171 triplet.getName(), 172 triplet.getStyle(), 173 triplet.getWeight() ); 174 } 175 } 176 } 177 178 183 184 public void setOptions( java.util.Hashtable options ) { } 185 186 191 192 public void setProducer( String producer ) { } 193 194 201 202 public void render( Page page, OutputStream stream ) 203 throws IOException 204 { 205 renderPage( page ); 206 } 207 208 213 214 public void renderAreaContainer( AreaContainer area ) 215 { 216 int oldX = this.currentACPosX; 217 int oldY = this.currentPosY; 218 219 if ( area.getPosition() == Position.ABSOLUTE ) 220 { 221 this.currentPosY = area.getYPosition() 222 - ( 2 * area.getPaddingTop() ) 223 - ( 2 * area.getBorderTopWidth() ); 224 225 this.currentACPosX = area.getXPosition(); 226 } 227 else if ( area.getPosition() == Position.RELATIVE ) 228 { 229 this.currentPosY -= area.getYPosition(); 230 this.currentACPosX += area.getXPosition(); 231 } 232 else if ( area.getPosition() == Position.STATIC ) 233 { 234 this.currentPosY -= ( area.getPaddingTop() 235 + area.getBorderTopWidth() ); 236 237 this.currentACPosX += ( area.getPaddingLeft() 238 + area.getBorderLeftWidth() ); 239 } 240 241 drawFrame( area ); 242 243 renderChildren( area ); 244 245 this.currentACPosX = oldX; 246 this.currentPosY = oldY; 247 248 if ( area.getPosition() == Position.STATIC ) 249 { 250 this.currentPosY -= area.getHeight(); 251 } 252 } 253 254 259 260 public void renderBodyAreaContainer( BodyAreaContainer area ) 261 { 262 renderAreaContainer( area.getBeforeFloatReferenceArea() ); 263 renderAreaContainer( area.getFootnoteReferenceArea() ); 264 265 267 renderChildren( area ); 268 } 269 270 275 276 public void renderSpanArea( SpanArea area ) 277 { 278 280 renderChildren( area ); 281 } 282 283 288 289 public void renderBlockArea( BlockArea area ) 290 { 291 this.currentPosY -= ( area.getPaddingTop() 292 + area.getBorderTopWidth() ); 293 294 drawFrame( area ); 295 296 renderChildren( area ); 297 298 this.currentPosY -= ( area.getPaddingBottom() 299 + area.getBorderBottomWidth() ); 300 } 301 302 307 308 public void renderDisplaySpace( DisplaySpace space ) 309 { 310 currentPosY -= space.getSize(); 311 } 312 313 318 319 public void renderSVGArea( SVGArea area ) 320 { 321 currentPosX += area.getContentWidth(); 322 } 323 324 329 330 public void renderForeignObjectArea( ForeignObjectArea area ) { } 331 332 337 338 public void renderImageArea( ImageArea area ) 339 { 340 currentPosY -= area.getHeight(); 341 } 342 343 348 349 public void renderWordArea( WordArea area ) 350 { 351 FontState fontState = area.getFontState(); 352 353 SWFFontMetric fontMetric; 354 355 try 356 { 357 fontMetric = ( SWFFontMetric ) 358 fontState.getFontInfo().getMetricsFor( fontState.getFontName() ); 359 } 360 catch ( FOPException e ) 361 { 362 MessageHandler.errorln( "Failed to get metrics for font \"" 363 + fontState.getFontName() 364 + "\", using default font." ); 365 366 fontMetric = new SWFFontMetric( "Arial.fft" ); 367 } 368 369 flashMovie.addText( area.getText(), 370 this.currentPosX, 371 this.currentPosY, 372 area.getRed(), 373 area.getGreen(), 374 area.getBlue(), 375 fontMetric, 376 fontState.getFontSize(), 377 area.getContentWidth() ); 378 379 addWordAreaLines( area, fontState.getFontSize() ); 380 381 this.currentPosX += area.getContentWidth(); 382 } 383 384 protected void addWordAreaLines( WordArea area, 385 int size ) 386 { 387 int y; 388 389 if ( area.getUnderlined() ) 390 { 391 y = currentPosY - size / 14; 392 393 flashMovie.addRect( currentPosX, 394 y, 395 area.getContentWidth(), 396 - size / 14, 397 area.getRed(), 398 area.getBlue(), 399 area.getGreen() ); 400 } 401 402 if ( area.getOverlined() ) 403 { 404 y = currentPosY + area.getFontState().getAscender() + 2 * ( size / 14 ); 405 406 flashMovie.addRect( currentPosX, 407 y, 408 area.getContentWidth(), 409 size / 14, 410 area.getRed(), 411 area.getBlue(), 412 area.getGreen() ); 413 } 414 415 if ( area.getLineThrough() ) 416 { 417 y = currentPosY + area.getFontState().getAscender() / 2; 418 419 flashMovie.addRect( currentPosX, 420 y, 421 area.getContentWidth(), 422 - size / 14, 423 area.getRed(), 424 area.getBlue(), 425 area.getGreen() ); 426 } 427 } 428 429 434 435 public void renderInlineSpace( InlineSpace space ) 436 { 437 this.currentPosX += space.getSize(); 438 } 439 440 445 446 public void renderLineArea( LineArea area ) 447 { 448 int x = currentACPosX + area.getStartIndent(); 449 int y = currentPosY; 450 int w = area.getContentWidth(); 451 int h = area.getHeight(); 452 453 currentPosY -= area.getPlacementOffset(); 454 currentPosX = x; 455 456 Enumeration e = area.getChildren().elements(); 457 458 while ( e.hasMoreElements() ) 459 { 460 org.apache.fop.layout.Box box = ( org.apache.fop.layout.Box ) e.nextElement(); 461 462 if ( box instanceof InlineArea ) 463 { 464 currentPosY = y - ( ( InlineArea ) box ).getYOffset(); 465 } 466 else 467 { 468 currentPosY = y - area.getPlacementOffset(); 469 } 470 471 box.render( this ); 472 } 473 474 currentPosY = y - h; 475 } 476 477 482 483 public void renderPage( Page page ) 484 { 485 BodyAreaContainer body; 486 AreaContainer before, after; 487 488 flashMovie.startPage( page.getWidth(), page.getHeight() ); 489 490 body = page.getBody(); 491 before = page.getBefore(); 492 after = page.getAfter(); 493 494 renderBodyAreaContainer( body ); 495 496 if ( before != null ) 497 { 498 renderAreaContainer( before ); 499 } 500 501 if ( after != null ) 502 { 503 renderAreaContainer( after ); 504 } 505 506 if ( page.hasLinks() ) 507 { 508 Enumeration e = page.getLinkSets().elements(); 509 510 while ( e.hasMoreElements() ) 511 { 512 LinkSet linkSet = ( LinkSet ) e.nextElement(); 513 514 linkSet.align(); 515 516 Enumeration f = linkSet.getRects().elements(); 517 518 while ( f.hasMoreElements() ) 519 { 520 LinkedRectangle rect = ( LinkedRectangle ) f.nextElement(); 521 522 flashMovie.addLink( rect.getX(), 523 rect.getY(), 524 rect.getWidth(), 525 rect.getHeight(), 526 linkSet.getDest() ); 527 } 528 } 529 } 530 } 531 532 539 540 public void renderLeaderArea( LeaderArea area ) 541 { 542 flashMovie.addRect( currentPosX, 543 currentPosY, 544 area.getLeaderLength(), 545 area.getRuleThickness(), 546 area.getRed(), 547 area.getGreen(), 548 area.getBlue() ); 549 550 this.currentPosX += area.getContentWidth(); 551 } 552 553 558 559 protected void renderChildren( Area area ) 560 { 561 Enumeration e = area.getChildren().elements(); 562 563 while ( e.hasMoreElements() ) 564 { 565 org.apache.fop.layout.Box box = ( org.apache.fop.layout.Box ) e.nextElement(); 566 box.render( this ); 567 } 568 } 569 570 575 576 private void drawFrame( Area area ) 577 { 578 int x = this.currentACPosX; 579 int y = this.currentPosY; 580 int h = area.getContentHeight(); 581 int w = area.getContentWidth(); 582 583 if ( area instanceof BlockArea ) 584 { 585 x += ( ( BlockArea ) area ).getStartIndent(); 586 } 587 588 ColorType bg = area.getBackgroundColor(); 589 590 x = x - area.getPaddingLeft(); 591 y = y + area.getPaddingTop(); 592 w = w + area.getPaddingLeft() + area.getPaddingRight(); 593 h = h + area.getPaddingTop() + area.getPaddingBottom(); 594 595 if ( ( bg != null ) && ( bg.alpha() == 0 ) ) 596 { 597 flashMovie.addRect( x, y - h, w, h, bg.red(), bg.green(), bg.blue() ); 598 } 599 600 BorderAndPadding bp = area.getBorderAndPadding(); 601 602 if ( bp != null ) 603 { 604 x = x - area.getBorderLeftWidth(); 605 y = y + area.getBorderTopWidth(); 606 w = w + area.getBorderLeftWidth() + area.getBorderRightWidth(); 607 h = h + area.getBorderTopWidth() + area.getBorderBottomWidth(); 608 609 ColorType borderColor; 610 611 if ( area.getBorderTopWidth() != 0 ) 612 { 613 borderColor = bp.getBorderColor( BorderAndPadding.TOP ); 614 615 flashMovie.addRect( x, y, w, - area.getBorderTopWidth(), 616 borderColor.red(), borderColor.green(), borderColor.blue() ); 617 } 618 619 if ( area.getBorderLeftWidth() != 0 ) 620 { 621 borderColor = bp.getBorderColor( BorderAndPadding.LEFT ); 622 623 flashMovie.addRect( x, y, area.getBorderLeftWidth(), - h, 624 borderColor.red(), borderColor.green(), borderColor.blue() ); 625 } 626 627 if ( area.getBorderRightWidth() != 0 ) 628 { 629 borderColor = bp.getBorderColor( BorderAndPadding.RIGHT ); 630 631 flashMovie.addRect( x + w, y, - area.getBorderRightWidth(), - h, 632 borderColor.red(), borderColor.green(), borderColor.blue() ); 633 } 634 635 if ( area.getBorderBottomWidth( ) != 0 ) 636 { 637 borderColor = bp.getBorderColor( BorderAndPadding.BOTTOM ); 638 639 flashMovie.addRect( x, y - h, w, area.getBorderBottomWidth(), 640 borderColor.red(), borderColor.green(), borderColor.blue() ); 641 } 642 } 643 } 644 645 public void render(org.apache.fop.layout.AreaTree t, java.io.OutputStream s) { 647 } 648 649 650 } 651 | Popular Tags |