001: /*
002: * $Id: LazyMorphShape.java,v 1.2 2002/02/15 23:44:28 skavish Exp $
003: *
004: * ==========================================================================
005: *
006: * The JGenerator Software License, Version 1.0
007: *
008: * Copyright (c) 2000 Dmitry Skavish (skavish@usa.net). All rights reserved.
009: *
010: * Redistribution and use in source and binary forms, with or without
011: * modification, are permitted provided that the following conditions are met:
012: *
013: * 1. Redistributions of source code must retain the above copyright
014: * notice, this list of conditions and the following disclaimer.
015: *
016: * 2. Redistributions in binary form must reproduce the above copyright
017: * notice, this list of conditions and the following disclaimer in
018: * the documentation and/or other materials provided with the
019: * distribution.
020: *
021: * 3. The end-user documentation included with the redistribution, if
022: * any, must include the following acknowlegement:
023: * "This product includes software developed by Dmitry Skavish
024: * (skavish@usa.net, http://www.flashgap.com/)."
025: * Alternately, this acknowlegement may appear in the software itself,
026: * if and wherever such third-party acknowlegements normally appear.
027: *
028: * 4. The name "The JGenerator" must not be used to endorse or promote
029: * products derived from this software without prior written permission.
030: * For written permission, please contact skavish@usa.net.
031: *
032: * 5. Products derived from this software may not be called "The JGenerator"
033: * nor may "The JGenerator" appear in their names without prior written
034: * permission of Dmitry Skavish.
035: *
036: * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
037: * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
038: * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
039: * DISCLAIMED. IN NO EVENT SHALL DMITRY SKAVISH OR THE OTHER
040: * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
041: * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
042: * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
043: * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
044: * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
045: * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
046: * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
047: * SUCH DAMAGE.
048: *
049: */
050:
051: package org.openlaszlo.iv.flash.api.shape;
052:
053: import java.awt.geom.*;
054: import java.io.PrintStream;
055:
056: import org.openlaszlo.iv.flash.parser.*;
057: import org.openlaszlo.iv.flash.util.*;
058: import org.openlaszlo.iv.flash.api.*;
059:
060: public class LazyMorphShape extends LazyShape {
061:
062: public Rectangle2D endBounds;
063:
064: public LazyMorphShape() {
065: }
066:
067: private static void extractBitmaps(Parser p, LazyMorphShape shape) {
068: int pos = p.getPos();
069:
070: p.skip(4); // skip offset
071:
072: // Get the number of fills.
073: int nFills = p.getUByte();
074: if (nFills == 255) {
075: nFills = p.getUWord();
076: }
077:
078: // Get each of the fill style.
079: for (int i = 0; i < nFills; i++) {
080: int fillStyle = p.getUByte();
081: if ((fillStyle & 0x10) != 0) { // gradient
082: p.skipMatrix();
083: p.skipMatrix();
084: // Get the number of colors.
085: int nColors = p.getUByte();
086: // Get each of the colors.
087: for (int j = 0; j < nColors; j++) {
088: p.skip(1); // ratio
089: AlphaColor.skip(p);
090: p.skip(1); // ratio
091: AlphaColor.skip(p);
092: }
093: } else if ((fillStyle & 0x40) != 0) { // bitmap
094: int id_pos = p.getPos() - pos;
095: int id = p.getUWord();
096: FlashDef def = p.getDef(id);
097: //System.out.println( "MORPHSHAPE: bitmap found: ID="+id+", def="+def );
098: shape.addBitmap(def, id_pos);
099: p.skipMatrix();
100: p.skipMatrix();
101: } else { // A solid color
102: AlphaColor.skip(p);
103: AlphaColor.skip(p);
104: }
105: }
106: }
107:
108: public static FlashDef parse(Parser p) {
109: FlashFile file = p.getFile();
110: if (file.isFullParsing()) {
111: MorphShape shape = MorphShape.parse(p);
112: return shape;
113: } else {
114: LazyMorphShape shape = new LazyMorphShape();
115: shape.tagCode = p.getTagCode();
116: shape.setID(p.getUWord());
117: shape.bounds = p.getRect();
118: shape.endBounds = p.getRect();
119: // skip the data
120: shape.data = new DataMarker(p.getBuf(), p.getPos(), p
121: .getTagEndPos());
122: extractBitmaps(p, shape);
123: return shape;
124: }
125: }
126:
127: public void write(FlashOutput fob) {
128: fob.writeTag(tagCode, 2 + GeomHelper.getSize(bounds)
129: + GeomHelper.getSize(endBounds) + data.length());
130: fob.writeDefID(this );
131: fob.write(bounds);
132: fob.write(endBounds);
133: int pos = fob.getPos();
134: data.write(fob);
135: if (bitmaps != null) {
136: for (int i = 0; i < bitmaps.size(); i++) {
137: BitmapRef ref = (BitmapRef) bitmaps.elementAt(i);
138: fob.writeWordAt(fob.getDefID(ref.bitmap), pos
139: + ref.offset);
140: }
141: }
142: }
143:
144: public boolean isConstant() {
145: return true;
146: }
147:
148: public Rectangle2D getBounds() {
149: Rectangle2D dst = GeomHelper.newRectangle();
150: Rectangle2D.union(bounds, endBounds, dst);
151: return dst;
152: }
153:
154: public void printContent(PrintStream out, String indent) {
155: out.println(indent + "LazyMorphShape: id=" + getID()
156: + ", 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
172: .clone();
173: return item;
174: }
175:
176: public FlashItem getCopy(ScriptCopier copier) {
177: return copyInto(new LazyMorphShape(), copier);
178: }
179: }
|