KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * $Id: LazyShape.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 LazyShape extends FlashDef {
61
62     public static class BitmapRef {
63         public FlashDef bitmap;
64         public int offset;
65         public BitmapRef( FlashDef bitmap, int offset ) {
66             this.bitmap = bitmap;
67             this.offset = offset;
68         }
69     }
70
71     protected IVVector bitmaps;
72     protected DataMarker data;
73     protected Rectangle2D bounds;
74     protected int tagCode;
75
76     public LazyShape() {}
77
78     public int getTag() {
79         return tagCode;
80     }
81
82     public static FlashDef parse( Parser p ) {
83         FlashFile file = p.getFile();
84         if( file.isFullParsing() ) {
85             Shape shape = Shape.parse(p);
86             return shape;
87         } else {
88             LazyShape shape = new LazyShape();
89             shape.tagCode = p.getTagCode();
90             shape.setID( p.getUWord() );
91             shape.bounds = p.getRect();
92             // skip the data
93
shape.data = new DataMarker( p.getBuf(), p.getPos(), p.getTagEndPos() );
94             extractBitmaps( p, shape );
95             return shape;
96         }
97     }
98
99     public void write( FlashOutput fob ) {
100         fob.writeTag( tagCode, 2+GeomHelper.getSize(bounds)+data.length() );
101         fob.writeDefID( this );
102         fob.write(bounds);
103         int pos = fob.getPos();
104         data.write( fob );
105         if( bitmaps != null ) {
106             for( int i=0; i<bitmaps.size(); i++ ) {
107                 BitmapRef ref = (BitmapRef) bitmaps.elementAt(i);
108                 int bitmapID;
109                 if( ref.bitmap == null ) { // 02/08/01 by sd
110
bitmapID = 0xffff;
111                 } else {
112                     bitmapID = fob.getDefID(ref.bitmap);
113                 }
114                 fob.writeWordAt( bitmapID, pos+ref.offset );
115             }
116         }
117     }
118
119     private static void parseShapeStyle( Parser p, LazyShape shape, boolean withAlpha, int pos ) {
120         // Get the number of fills.
121
int nFills = p.getUByte();
122         if( nFills == 255 ) {
123             nFills = p.getUWord();
124         }
125
126         // Get each of the fill style.
127
for( int i=0; i<nFills; i++ ) {
128             int fillStyle = p.getUByte();
129             if( (fillStyle&0x10) != 0 ) { // gradient
130
// Get the gradient matrix.
131
p.skipMatrix();
132                 // Get the number of colors.
133
int nColors = p.getUByte();
134                 // Get each of the colors.
135
for( int j=0; j<nColors; j++ ) {
136                     p.skip(1); // ratio
137
Color.skip(p, withAlpha);
138                 }
139             } else if( (fillStyle&0x40) != 0 ) { // bitmap
140
int id_pos = p.getPos()-pos;
141                 int id = p.getUWord(); // id may be equal to 0xffff, I don't know what it means
142
FlashDef def = p.getDef(id);
143                 shape.addBitmap(def, id_pos);
144                 p.skipMatrix();
145             } else { // A solid color
146
Color.skip(p, withAlpha);
147             }
148         }
149
150         int nLines = p.getUByte();
151         if( nLines == 255 ) {
152             nLines = p.getUWord();
153         }
154         // Get each of the line styles.
155
for( int i=0; i<nLines; i++ ) {
156             p.skip(2); // width
157
Color.skip(p, withAlpha);
158         }
159
160     }
161
162     private static void extractBitmaps( Parser p, LazyShape shape ) {
163         int pos = p.getPos();
164
165         boolean withAlpha = shape.getTag() == Tag.DEFINESHAPE3;
166
167         parseShapeStyle( p, shape, withAlpha, pos );
168
169         int nBits = p.getUByte();
170         int nFillBits = (nBits&0xf0)>>4;
171         int nLineBits = nBits&0x0f;
172         // parse shape records
173
p.initBits();
174         for(;;) {
175             if( p.getBool() ) { // edge record
176
if( p.getBool() ) { // stright edge
177
int nb = p.getBits(4)+2;
178                     if( p.getBool() ) {
179                         p.skipBits(nb+nb); // general line
180
} else {
181                         p.skipBits(1+nb); // horiz or vert line
182
}
183
184                 } else { // curved edge
185
int nb = p.getBits(4)+2;
186                     p.skipBits(nb*4);
187                 }
188             } else { // non-edge record
189
int flags = p.getBits(5);
190                 if( flags == 0 ) break; // end record
191
if( (flags & StyleChangeRecord.MOVETO) != 0 ) {
192                     int nMoveBits = p.getBits(5);
193                     p.skipBits(nMoveBits+nMoveBits);
194                 }
195                 if( (flags & StyleChangeRecord.FILLSTYLE0) != 0 ) {
196                     p.skipBits(nFillBits);
197                 }
198                 if( (flags & StyleChangeRecord.FILLSTYLE1) != 0 ) {
199                     p.skipBits(nFillBits);
200                 }
201                 if( (flags & StyleChangeRecord.LINESTYLE) != 0 ) {
202                     p.skipBits(nLineBits);
203                 }
204                 if( (flags & StyleChangeRecord.NEW_STYLES) != 0 ) {
205                     parseShapeStyle( p, shape, withAlpha, pos );
206                     nBits = p.getUByte();
207                     nFillBits = (nBits&0xf0)>>4;
208                     nLineBits = nBits&0x0f;
209                     p.initBits();
210                 }
211                 if( (flags&0x80) != 0 ) {
212                     break;
213                 }
214             }
215         }
216     }
217
218     public void printContent( PrintStream JavaDoc out, String JavaDoc indent ) {
219         out.println( indent+"LazyShape("+Tag.tagNames[tagCode]+"): id="+getID()+", name='"+getName()+"'" );
220         out.println( indent+" "+bounds );
221         if( bitmaps != null ) {
222             out.print( indent+" bitmaps used: " );
223             for( int i=0; i<bitmaps.size(); i++ ) {
224                 BitmapRef ref = (BitmapRef) bitmaps.elementAt(i);
225                 out.print( "id["+i+"]="+(ref.bitmap!=null?ref.bitmap.getID():0xffff)+" " );
226             }
227             out.println();
228         }
229     }
230
231     public boolean isConstant() {
232         return true;
233     }
234
235     public Rectangle2D getBounds() {
236         return bounds;
237     }
238
239     public void collectDeps( DepsCollector dc ) {
240         if( bitmaps != null ) {
241             for( int i=0; i<bitmaps.size(); i++ ) {
242                 BitmapRef ref = (BitmapRef) bitmaps.elementAt(i);
243                 if( ref.bitmap != null ) dc.addDep( ref.bitmap );
244             }
245         }
246     }
247
248     public void addBitmap( FlashDef def, int offset ) {
249         if( bitmaps == null ) bitmaps = new IVVector();
250         bitmaps.addElement( new BitmapRef(def, offset) );
251     }
252
253     protected FlashItem copyInto( FlashItem item, ScriptCopier copier ) {
254         super.copyInto( item, copier );
255         ((LazyShape)item).data = (DataMarker) data.getCopy();
256         ((LazyShape)item).tagCode = tagCode;
257         ((LazyShape)item).bounds = (Rectangle2D) bounds.clone();
258         if( bitmaps != null ) {
259             IVVector v = new IVVector(bitmaps.size());
260             for( int i=0; i<bitmaps.size(); i++ ) {
261                 BitmapRef ref = (BitmapRef) bitmaps.elementAt(i);
262                 v.addElement( new BitmapRef( copier.copy(ref.bitmap), ref.offset ) );
263             }
264             ((LazyShape)item).bitmaps = v;
265         }
266         return item;
267     }
268
269     public FlashItem getCopy( ScriptCopier copier ) {
270         return copyInto( new LazyShape(), copier );
271     }
272 }
273
Popular Tags