KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > openlaszlo > iv > flash > fop > FOPScriptBuilder


1 /*
2  * $Id: FOPScriptBuilder.java,v 1.4 2002/03/19 22:19:55 skavish Exp $
3  *
4  * ==========================================================================
5
6  * The JGenerator Software License, Version 1.0
7  *
8  * Copyright (c) 2000 Dmitry Skavish (skavish@usa.net). All rights reserved.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions are met:
12  *
13  * 1. Redistributions of source code must retain the above copyright
14  * notice, this list of conditions and the following disclaimer.
15  *
16  * 2. Redistributions in binary form must reproduce the above copyright
17  * notice, this list of conditions and the following disclaimer in
18  * the documentation and/or other materials provided with the
19  * distribution.
20  *
21  * 3. The end-user documentation included with the redistribution, if
22  * any, must include the following acknowlegement:
23  * "This product includes software developed by Dmitry Skavish
24  * (skavish@usa.net, http://www.flashgap.com/)."
25  * Alternately, this acknowlegement may appear in the software itself,
26  * if and wherever such third-party acknowlegements normally appear.
27  *
28  * 4. The name "The JGenerator" must not be used to endorse or promote
29  * products derived from this software without prior written permission.
30  * For written permission, please contact skavish@usa.net.
31  *
32  * 5. Products derived from this software may not be called "The JGenerator"
33  * nor may "The JGenerator" appear in their names without prior written
34  * permission of Dmitry Skavish.
35  *
36  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
37  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
38  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
39  * DISCLAIMED. IN NO EVENT SHALL DMITRY SKAVISH OR THE OTHER
40  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
41  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
42  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
43  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
44  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
45  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
46  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
47  * SUCH DAMAGE.
48  *
49  */

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 /**
67  * This is a helper class for the SWFRenderer, this does all
68  * JGen specific calls, like drawing lines etc etc.
69  *
70  * @author Johan "Spocke" Sörlin
71  * @author James Taylor
72  */

73
74 public class FOPScriptBuilder
75 {
76     private Script script;
77     private Frame currentFrame;
78
79     // Name of actionscript function to handle links ( should take one
80
// string argument )
81

82     private String JavaDoc linkHandler = null;
83
84     private Font currentFont;
85
86     private int layerCount = 2;
87
88     // Dimensions of the current page
89

90     private int pageWidth = 0;
91     private int pageHeight = 0;
92
93     // Dimensions of the largest page
94

95     private int maxPageWidth = 0;
96     private int maxPageHeight = 0;
97
98     /**
99      * Constructs a new FOPScriptBuilder
100      *
101      */

102
103     public FOPScriptBuilder()
104     {
105         script = new Script( 0 );
106     }
107
108     /**
109      * Sets the name of the function used to handle links. This should be a
110      * function defined on the timeline containing this script.
111      *
112      * FIXME: Need to evaluate other ways to do this, to allow the most
113      * flexibility
114      */

115
116     public void setLinkHandler( String JavaDoc 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         // Start with a blank frame
132

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     /**
147      * Adds a new line shape to the FlashMovie.
148      *
149      * @param x1 x1 position in FOP points
150      * @param y1 y1 position in FOP points
151      * @param x2 x2 position in FOP points
152      * @param y1 y1 position in FOP points
153      * @param th thickness in FOP points
154      * @param r red color channel
155      * @param g green color channel
156      * @param b blue color channel
157      */

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         // Translate y axis geometry
169

170         y1 = pageHeight - y1;
171         y2 = pageHeight - y2;
172
173         // Build shape
174

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     /**
185      * Adds a new filled rectangle shape to the FlashMovie.
186      *
187      * @param x x position in FOP points
188      * @param y y position in FOP points
189      * @param w width in FOP points
190      * @param h height in FOP points
191      * @param r red color channel
192      * @param g green color channel
193      * @param b blue color channel
194      */

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         // Translate y axis geometry
204

205         h = - h;
206         y = pageHeight - y;
207
208         // Build shape
209

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 JavaDoc 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         // Hit area
233

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         // Up state -- good for debug
249

250         // ButtonRecord upState =
251
// new ButtonRecord( ButtonRecord.Up, shape, 1, shapeMatrix, CXForm.newDefault(true) );
252

253         // button.addButtonRecord( upState );
254

255         // Action
256

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     /**
283      * Adds a new text element to the FlashMovie.
284      *
285      * @param string String to place in text element
286      * @param pos_x x position in FOP points
287      * @param pos_y y position in FOP points
288      * @param r red color channel
289      * @param g green color channel
290      * @param b blue color channel
291      * @param fontMetric font metric
292      * @param size font size in FOP points
293      * @param underlined underlined text element
294      */

295
296     public void addText( String JavaDoc 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         // Translate y axis geometry
311

312         y = pageHeight - y;
313
314         /*
315
316         // DEBUG: This draws the boxes where words should be rendered, for
317         // debugging the baseline position
318
319         Shape shape = new Shape();
320
321         shape.setFillStyle0( FillStyle.newSolid( new AlphaColor( 64, 64, 128 ) ) );
322
323         Rect movieRect = new Rect( x, y, x + width, y - size );
324         shape.drawRectangle( movieRect );
325         shape.setBounds( movieRect );
326
327         currentFrame.addInstance( shape, layerCount++, new Matrix(), null );
328
329         */

330
331         // Build text
332

333         currentFont = fontMetric.getFont();
334
335         // FIXME: This is a gross hack! It appears that when fop draws a text
336
// at a given y position it expects that position to correspond
337
// to the baseline. However in flash, the position correponds to
338
// the bottom of the EM square. This seems to compensate well
339
// for all fonts ( that have been tested ) but I don't know WHY!
340

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     /**
359      * Convert millipoints ( unit of formatting used by most parts of FOP ) to
360      * twixels ( unit used by flash ).
361      *
362      * 1000 points = 1 pixel = 20 twixels, thus 1 point = .02 twixels
363      */

364
365     private int millipointsToTwixels( int points )
366     {
367         return Math.round( points * 0.02f );
368     }
369
370 }
371
Popular Tags