KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * $Id: SWFRenderer.java,v 1.2 2002/02/15 23:44:28 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.xml.sax.InputSource JavaDoc;
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 /**
71  * This is a implementation of the FOP Renderer interface that places Flash objects
72  * based on FOP data.
73  *
74  * @author Johan "Spocke" Sörlin
75  * @author James Taylor
76  */

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     /**
98      * Constructs a new FOPSWFRenderer
99      */

100
101     public SWFRenderer( ) { }
102
103     /**
104      * Get the script this renderer is drawing into
105      */

106
107     public FOPScriptBuilder getScriptBuilder()
108     {
109         return flashMovie;
110     }
111
112     public void setLinkHandler( String JavaDoc s )
113     {
114         flashMovie.setLinkHandler( s );
115     }
116
117     /**
118      * set up the given FontInfo
119      *
120      * @param fontInfo font info passed from FOP framework
121      */

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 JavaDoc( 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 JavaDoc 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 JavaDoc 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     /**
179      * Set up renderer options -- Empty
180      *
181      * @param options options passed from FOP framework
182      */

183
184     public void setOptions( java.util.Hashtable JavaDoc options ) { }
185
186     /**
187      * Set the producer of the rendering -- Empty
188      *
189      * @param producer producer passed from FOP framework
190      */

191
192     public void setProducer( String JavaDoc producer ) { }
193
194     /**
195      * render the given page.
196      * The stream parameter is ignored.
197      *
198      * @param area_tree area tree passed from FOP framework
199      * @param stream stream to write to, ignored
200      */

201
202     public void render( Page page, OutputStream stream )
203         throws IOException
204     {
205         renderPage( page );
206     }
207
208     /**
209      * render the given area container
210      *
211      * @param area area passed from FOP framework
212      */

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     /**
255      * render the given body area container
256      *
257      * @param area body area container passed from FOP framework
258      */

259
260     public void renderBodyAreaContainer( BodyAreaContainer area )
261     {
262         renderAreaContainer( area.getBeforeFloatReferenceArea() );
263         renderAreaContainer( area.getFootnoteReferenceArea() );
264
265         // Children are span areas? ( see AWTRenderer )
266

267         renderChildren( area );
268     }
269
270     /**
271      * render the given span area
272      *
273      * @param area span area passed from FOP framework
274      */

275
276     public void renderSpanArea( SpanArea area )
277     {
278         // Children are column areas? ( see AWTRenderer )
279

280         renderChildren( area );
281     }
282
283     /**
284      * render the given block area
285      *
286      * @param area span block passed from FOP framework
287      */

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     /**
303      * render the display space area
304      *
305      * @param space display space passed from FOP framework
306      */

307
308     public void renderDisplaySpace( DisplaySpace space )
309     {
310         currentPosY -= space.getSize();
311     }
312
313     /**
314      * render the given SVG area -- Empty
315      *
316      * @param area area passed from FOP framework
317      */

318
319     public void renderSVGArea( SVGArea area )
320     {
321         currentPosX += area.getContentWidth();
322     }
323
324     /**
325      * render a foreign object area -- Empty
326      *
327      * @param area area passed from FOP framework
328      */

329
330     public void renderForeignObjectArea( ForeignObjectArea area ) { }
331
332     /**
333      * render the given image area -- Empty
334      *
335      * @param area area passed from FOP framework
336      */

337
338     public void renderImageArea( ImageArea area )
339     {
340         currentPosY -= area.getHeight();
341     }
342
343     /**
344      * render the given word area
345      *
346      * @param area word area passed from FOP framework
347      */

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     /**
430      * render the given inline space
431      *
432      * @param space inline space passed from FOP framework
433      */

434
435     public void renderInlineSpace( InlineSpace space )
436     {
437         this.currentPosX += space.getSize();
438     }
439
440     /**
441      * render the given line area
442      *
443      * @param area line area passed from FOP framework
444      */

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     /**
478      * render the given page
479      *
480      * @param page page passed from FOP framework
481      */

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     /**
533      * render the leader area
534      *
535      * FIXME: Rule style is currently ignored.
536      *
537      * @param area leader area passed from FOP framework
538      */

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     /**
554      * Renders all child objects of an area.
555      *
556      * @param area area to render children on
557      */

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     /**
571      * Draws a frame of a area, filled or outlined.
572      *
573      * @param area area to draw frame around
574      */

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     // LASZLO
646
public void render(org.apache.fop.layout.AreaTree t, java.io.OutputStream JavaDoc s) {
647     }
648
649
650 }
651
Popular Tags