KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > openlaszlo > iv > flash > api > shape > LazyMorphShape


1 /*
2  * $Id: LazyMorphShape.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.api.shape;
52
53 import java.awt.geom.*;
54 import java.io.PrintStream JavaDoc;
55
56 import org.openlaszlo.iv.flash.parser.*;
57 import org.openlaszlo.iv.flash.util.*;
58 import org.openlaszlo.iv.flash.api.*;
59
60 public class LazyMorphShape extends LazyShape {
61
62     public Rectangle2D endBounds;
63
64     public LazyMorphShape() {}
65
66     private static void extractBitmaps( Parser p, LazyMorphShape shape ) {
67         int pos = p.getPos();
68
69         p.skip(4); // skip offset
70

71         // Get the number of fills.
72
int nFills = p.getUByte();
73         if( nFills == 255 ) {
74             nFills = p.getUWord();
75         }
76
77         // Get each of the fill style.
78
for( int i=0; i<nFills; i++ ) {
79             int fillStyle = p.getUByte();
80             if( (fillStyle&0x10) != 0 ) { // gradient
81
p.skipMatrix();
82                 p.skipMatrix();
83                 // Get the number of colors.
84
int nColors = p.getUByte();
85                 // Get each of the colors.
86
for( int j=0; j<nColors; j++ ) {
87                     p.skip(1); // ratio
88
AlphaColor.skip(p);
89                     p.skip(1); // ratio
90
AlphaColor.skip(p);
91                 }
92             } else if( (fillStyle&0x40) != 0 ) { // bitmap
93
int id_pos = p.getPos()-pos;
94                 int id = p.getUWord();
95                 FlashDef def = p.getDef(id);
96                 //System.out.println( "MORPHSHAPE: bitmap found: ID="+id+", def="+def );
97
shape.addBitmap(def, id_pos);
98                 p.skipMatrix();
99                 p.skipMatrix();
100             } else { // A solid color
101
AlphaColor.skip(p);
102                 AlphaColor.skip(p);
103             }
104         }
105     }
106
107     public static FlashDef parse( Parser p ) {
108         FlashFile file = p.getFile();
109         if( file.isFullParsing() ) {
110             MorphShape shape = MorphShape.parse(p);
111             return shape;
112         } else {
113             LazyMorphShape shape = new LazyMorphShape();
114             shape.tagCode = p.getTagCode();
115             shape.setID( p.getUWord() );
116             shape.bounds = p.getRect();
117             shape.endBounds = p.getRect();
118             // skip the data
119
shape.data = new DataMarker( p.getBuf(), p.getPos(), p.getTagEndPos() );
120             extractBitmaps( p, shape );
121             return shape;
122         }
123     }
124
125     public void write( FlashOutput fob ) {
126         fob.writeTag( tagCode,
127             2 +
128             GeomHelper.getSize(bounds) +
129             GeomHelper.getSize(endBounds) +
130             data.length()
131         );
132         fob.writeDefID( this );
133         fob.write(bounds);
134         fob.write(endBounds);
135         int pos = fob.getPos();
136         data.write( fob );
137         if( bitmaps != null ) {
138             for( int i=0; i<bitmaps.size(); i++ ) {
139                 BitmapRef ref = (BitmapRef) bitmaps.elementAt(i);
140                 fob.writeWordAt( fob.getDefID(ref.bitmap), pos+ref.offset );
141             }
142         }
143     }
144
145     public boolean isConstant() {
146         return true;
147     }
148
149     public Rectangle2D getBounds() {
150         Rectangle2D dst = GeomHelper.newRectangle();
151         Rectangle2D.union( bounds, endBounds, dst );
152         return dst;
153     }
154
155     public void printContent( PrintStream JavaDoc out, String JavaDoc indent ) {
156         out.println( indent+"LazyMorphShape: id="+getID()+", name='"+getName()+"'" );
157         out.println( indent+" "+bounds );
158         out.println( indent+" "+endBounds );
159         if( bitmaps != null ) {
160             out.print( indent+" bitmaps used: " );
161             for( int i=0; i<bitmaps.size(); i++ ) {
162                 BitmapRef ref = (BitmapRef) bitmaps.elementAt(i);
163                 out.print( "id["+i+"]="+ref.bitmap.getID()+" " );
164             }
165             out.println();
166         }
167     }
168
169     protected FlashItem copyInto( FlashItem item, ScriptCopier copier ) {
170         super.copyInto( item, copier );
171         ((LazyMorphShape)item).endBounds = (Rectangle2D) endBounds.clone();
172         return item;
173     }
174
175     public FlashItem getCopy( ScriptCopier copier ) {
176         return copyInto( new LazyMorphShape(), copier );
177     }
178 }
179
Popular Tags