1 50 51 package org.openlaszlo.iv.flash.fop; 52 53 import org.openlaszlo.iv.flash.api.*; 54 import org.openlaszlo.iv.flash.api.shape.*; 55 import org.openlaszlo.iv.flash.api.action.*; 56 import org.openlaszlo.iv.flash.api.button.*; 57 import org.openlaszlo.iv.flash.api.text.*; 58 import org.openlaszlo.iv.flash.parser.*; 59 import org.openlaszlo.iv.flash.util.*; 60 61 62 import java.io.*; 63 import java.util.*; 64 import java.awt.geom.*; 65 66 73 74 public class FOPScriptBuilder 75 { 76 private Script script; 77 private Frame currentFrame; 78 79 82 private String linkHandler = null; 83 84 private Font currentFont; 85 86 private int layerCount = 2; 87 88 90 private int pageWidth = 0; 91 private int pageHeight = 0; 92 93 95 private int maxPageWidth = 0; 96 private int maxPageHeight = 0; 97 98 102 103 public FOPScriptBuilder() 104 { 105 script = new Script( 0 ); 106 } 107 108 115 116 public void setLinkHandler( String methodName ) 117 { 118 linkHandler = methodName; 119 } 120 121 public void startPage( int width, int height ) 122 { 123 pageWidth = millipointsToTwixels( width ); 124 pageHeight = millipointsToTwixels( height ); 125 126 maxPageWidth = ( pageWidth > maxPageWidth ) ? pageWidth : maxPageWidth; 127 maxPageHeight = ( pageHeight > maxPageHeight ) ? pageHeight : maxPageHeight; 128 129 currentFrame = script.newFrame(); 130 131 133 script.removeAllInstances( currentFrame ); 134 } 135 136 public Script getScript() 137 { 138 return script; 139 } 140 141 public Rectangle2D getMaxPageBounds() 142 { 143 return GeomHelper.newRectangle( 0, 0, maxPageWidth, maxPageHeight ); 144 } 145 146 158 159 public void addLine( int x1, int y1, int x2, int y2, int th, float r, float g, float b ) 160 { 161 x1 = millipointsToTwixels( x1 ); 162 y1 = millipointsToTwixels( y1 ); 163 x2 = millipointsToTwixels( x2 ); 164 y2 = millipointsToTwixels( y2 ); 165 166 th = millipointsToTwixels( th ); 167 168 170 y1 = pageHeight - y1; 171 y2 = pageHeight - y2; 172 173 175 Shape shape = new Shape(); 176 177 shape.setLineStyle( new LineStyle( th, new AlphaColor( r, g, b ) ) ); 178 shape.drawLine( x1, y2, x2, y2 ); 179 shape.setBounds( x1, y1, x2-x1, y2-y1 ); 180 181 currentFrame.addInstance( shape, layerCount++, new AffineTransform(), null ); 182 } 183 184 195 196 public void addRect( int x, int y, int w, int h, float r, float g, float b ) 197 { 198 x = millipointsToTwixels( x ); 199 y = millipointsToTwixels( y ); 200 w = millipointsToTwixels( w ); 201 h = millipointsToTwixels( h ); 202 203 205 h = - h; 206 y = pageHeight - y; 207 208 210 Shape shape = new Shape(); 211 212 shape.setFillStyle0( FillStyle.newSolid( new AlphaColor( r, g, b ) ) ); 213 214 Rectangle2D movieRect = GeomHelper.newRectangle( x, y, w, h ); 215 shape.drawRectangle( movieRect ); 216 shape.setBounds( movieRect ); 217 218 currentFrame.addInstance( shape, layerCount++, new AffineTransform(), null ); 219 } 220 221 public void addLink( int x, int y, int w, int h, String destination ) 222 { 223 x = millipointsToTwixels( x ); 224 y = millipointsToTwixels( y ); 225 w = millipointsToTwixels( w ); 226 h = millipointsToTwixels( h ); 227 228 y = pageHeight - y; 229 230 Button2 button = new Button2(); 231 232 234 Shape shape = new Shape(); 235 236 shape.setFillStyle0( FillStyle.newSolid( new AlphaColor(0x31, 0x63, 0x9c, 0x60) )); 237 Rectangle2D r = GeomHelper.newRectangle( 0, 0, w, h ); 238 shape.drawRectangle( r ); 239 shape.setBounds( r ); 240 241 AffineTransform shapeMatrix = new AffineTransform(); 242 243 ButtonRecord hitState = 244 new ButtonRecord( ButtonRecord.HitTest, shape, 1, shapeMatrix, CXForm.newIdentity( true ) ); 245 246 button.addButtonRecord( hitState ); 247 248 250 253 255 257 Program p = new Program(); 258 259 if ( linkHandler != null ) 260 { 261 p.push( destination ); 262 p.push( 1 ); 263 p.push( "_parent" ); 264 p.eval(); 265 p.push( linkHandler ); 266 p.callMethod(); 267 } 268 else 269 { 270 p.getURL( destination, "_blank" ); 271 } 272 273 ActionCondition onRelease = new ActionCondition( ActionCondition.OverDownToOverUp, p ); 274 275 button.addActionCondition( onRelease ); 276 277 AffineTransform instMatrix = AffineTransform.getTranslateInstance(x,y); 278 279 currentFrame.addInstance( button, layerCount++, instMatrix, null ); 280 } 281 282 295 296 public void addText( String string, 297 int x, int y, 298 float r, float g, float b, 299 SWFFontMetric fontMetric, 300 int size, 301 int width ) 302 { 303 x = millipointsToTwixels( x ); 304 y = millipointsToTwixels( y ); 305 306 size = millipointsToTwixels( size ); 307 width = millipointsToTwixels( width ); 308 309 310 312 y = pageHeight - y; 313 314 330 331 333 currentFont = fontMetric.getFont(); 334 335 341 y += ( currentFont.descent - currentFont.leading ) / 5; 342 343 Text text = Text.newText(); 344 345 TextItem item = new TextItem( string, currentFont, size, new AlphaColor( r, g, b ) ); 346 347 text.addTextItem( item ); 348 349 text.setBounds( 0, 0, width + 10, size ); 350 351 AffineTransform position = AffineTransform.getTranslateInstance(x, y-size); 352 353 currentFrame.addInstance( text, layerCount++, position, null ); 354 } 355 356 357 358 364 365 private int millipointsToTwixels( int points ) 366 { 367 return Math.round( points * 0.02f ); 368 } 369 370 } 371 | Popular Tags |