001: /*
002: * $Id: FillStyle.java,v 1.3 2002/03/02 01:39:44 awason 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: import org.openlaszlo.iv.flash.util.*;
056: import org.openlaszlo.iv.flash.api.*;
057: import org.openlaszlo.iv.flash.parser.*;
058: import org.openlaszlo.iv.flash.api.image.*;
059:
060: /**
061: * FillStyle of Shape.
062: * <P>
063: * SWF supports three basic types of fills for a shape.
064: * <UL>
065: * <LI>Solid fill - A simple RGBA color that fills a portion of a shape.
066: * An alpha value of 255 means a completely opaque fill.
067: * An alpha value of zero means a completely transparent fill.
068: * Any alpha between 0 and 255 will be partially transparent.
069: * <LI>Gradient Fill - A gradient fill can be either a linear or a radial gradient.
070: * See {@link org.openlaszlo.iv.flash.api.Gradient}s for an in depth description of how gradients are defined.
071: * <LI>Bitmap fill - Bitmap fills refer to a bitmap characterId.
072: * There are two styles: clipped and tiled. A clipped bitmap fill repeats the color on
073: * the edge of a bitmap if the fill extends beyond the edge of the bitmap.
074: * A tiled fill repeats the bitmap if the fill extends beyond the edge of the bitmap.
075: * </UL>
076: *
077: * @author Dmitry Skavish
078: */
079: public final class FillStyle extends FlashItem {
080:
081: public static final int SOLID = 0x00;
082: public static final int LINEAR_GRADIENT = 0x10;
083: public static final int RADIAL_GRADIENT = 0x12;
084: public static final int TILED_BITMAP = 0x40;
085: public static final int CLIPPED_BITMAP = 0x41;
086:
087: private int type; // type of this fillstyle
088: private Color color; // color of this fillstyle
089: private AffineTransform matrix; // matrix of this fillstyle
090: private Gradient gradient; // gradient of this fillstyle
091: private Bitmap bitmap; // bitmap of this fillstyle
092:
093: public FillStyle() {
094: }
095:
096: public int getType() {
097: return type;
098: }
099:
100: public void setType(int type) {
101: this .type = type;
102: }
103:
104: public Color getColor() {
105: return color;
106: }
107:
108: public void setColor(Color color) {
109: this .color = color;
110: }
111:
112: public AffineTransform getMatrix() {
113: return matrix;
114: }
115:
116: public void setMatrix(AffineTransform matrix) {
117: this .matrix = matrix;
118: }
119:
120: public Gradient getGraduent() {
121: return gradient;
122: }
123:
124: public void setGradient(Gradient gradient) {
125: this .gradient = gradient;
126: }
127:
128: public Bitmap getBitmap() {
129: return bitmap;
130: }
131:
132: public void setBitmap(Bitmap bitmap) {
133: this .bitmap = bitmap;
134: }
135:
136: /**
137: * Creates new solid fill.
138: *
139: * @param color color of solid fill
140: * @return solid fillstyle
141: */
142: public static FillStyle newSolid(Color color) {
143: FillStyle fs = new FillStyle();
144: fs.setColor(color);
145: fs.setType(SOLID);
146: return fs;
147: }
148:
149: /**
150: * Creates new linear gradient fill.
151: *
152: * @param gradient gradient of gradient fill
153: * @param matrix matrix of gradient fill
154: * @return linear gradient fill
155: */
156: public static FillStyle newLinearGradient(Gradient gradient,
157: AffineTransform matrix) {
158: FillStyle fs = new FillStyle();
159: fs.setGradient(gradient);
160: fs.setMatrix(matrix);
161: fs.setType(LINEAR_GRADIENT);
162: return fs;
163: }
164:
165: /**
166: * Creates new radial gradient fill.
167: *
168: * @param gradient gradient of gradient fill
169: * @param matrix matrix of gradient fill
170: * @return radial gradient fill
171: */
172: public static FillStyle newRadialGradient(Gradient gradient,
173: AffineTransform matrix) {
174: FillStyle fs = new FillStyle();
175: fs.setGradient(gradient);
176: fs.setMatrix(matrix);
177: fs.setType(RADIAL_GRADIENT);
178: return fs;
179: }
180:
181: /**
182: * Creates new tiled bitmap fill.
183: *
184: * @param bitmap bitmap of bitmap fill
185: * @param matrix matrix of bitmap fill
186: * @return tiled bitmap fill
187: */
188: public static FillStyle newTiledBitmap(Bitmap bitmap,
189: AffineTransform matrix) {
190: FillStyle fs = new FillStyle();
191: fs.setBitmap(bitmap);
192: fs.setMatrix(matrix);
193: fs.setType(TILED_BITMAP);
194: return fs;
195: }
196:
197: /**
198: * Creates new clipped bitmap fill.
199: *
200: * @param bitmap bitmap of bitmap fill
201: * @param matrix matrix of bitmap fill
202: * @return clipped bitmap fill
203: */
204: public static FillStyle newClippedBitmap(Bitmap bitmap,
205: AffineTransform matrix) {
206: FillStyle fs = new FillStyle();
207: fs.setBitmap(bitmap);
208: fs.setMatrix(matrix);
209: fs.setType(CLIPPED_BITMAP);
210: return fs;
211: }
212:
213: /**
214: * Creates new tiled bitmap fill with default tranformation matrix.
215: *
216: * @param bitmap bitmap of bitmap fill
217: * @return tiled bitmap fill
218: */
219: public static FillStyle newTiledBitmap(Bitmap bitmap) {
220: AffineTransform matrix = AffineTransform.getScaleInstance(20.0,
221: 20.0);
222: return newTiledBitmap(bitmap, matrix);
223: }
224:
225: /**
226: * Creates new clipped bitmap fill with default tranformation matrix.
227: *
228: * @param bitmap bitmap of bitmap fill
229: * @return clipped bitmap fill
230: */
231: public static FillStyle newClippedBitmap(Bitmap bitmap) {
232: AffineTransform matrix = AffineTransform.getScaleInstance(20.0,
233: 20.0);
234: return newClippedBitmap(bitmap, matrix);
235: }
236:
237: public static FillStyle parse(Parser p, boolean withAlpha) {
238: FillStyle fs = new FillStyle();
239: int type = fs.type = p.getUByte();
240:
241: if ((type & 0x10) != 0) { // gradient
242:
243: // Get the gradient matrix.
244: fs.matrix = p.getMatrix();
245: // get gradient itself
246: fs.gradient = Gradient.parse(p, withAlpha);
247:
248: } else if ((type & 0x40) != 0) { // bitmap
249:
250: int id = p.getUWord(); // id may be equal to 0xffff, I don't know what it means
251: fs.bitmap = (Bitmap) p.getDef(id);
252: fs.matrix = p.getMatrix();
253:
254: } else { // A solid color
255:
256: fs.color = Color.parse(p, withAlpha);
257: }
258:
259: return fs;
260: }
261:
262: public void write(FlashOutput fob) {
263: fob.writeByte(type);
264: switch (type) {
265: case SOLID:
266: Shape shape = (Shape) fob.getUserData();
267: if (shape.isWithAlpha()) {
268: color.writeRGBA(fob);
269: } else {
270: color.writeRGB(fob);
271: }
272: break;
273: case LINEAR_GRADIENT:
274: case RADIAL_GRADIENT:
275: fob.write(matrix);
276: gradient.write(fob);
277: break;
278: case TILED_BITMAP:
279: case CLIPPED_BITMAP:
280: if (bitmap == null) {
281: fob.writeWord(0xffff);
282: } else {
283: fob.writeDefID(bitmap);
284: }
285: fob.write(matrix);
286: break;
287: }
288: }
289:
290: public void printContent(PrintStream out, String indent) {
291: switch (type) {
292: case SOLID:
293: out.println(indent + "FillStyle (SOLID):");
294: color.printContent(out, indent + " ");
295: break;
296: case LINEAR_GRADIENT:
297: out.println(indent + "FillStyle (LINEAR_GRADIENT):");
298: out.println(indent + " " + matrix);
299: gradient.printContent(out, indent + " ");
300: break;
301: case RADIAL_GRADIENT:
302: out.println(indent + "FillStyle (RADIAL_GRADIENT):");
303: out.println(indent + " " + matrix);
304: gradient.printContent(out, indent + " ");
305: break;
306: case TILED_BITMAP:
307: out.println(indent + "FillStyle (TILED_BITMAP):");
308: out.println(indent + " " + matrix);
309: out.println(indent + " bitmapID="
310: + (bitmap == null ? 0xffff : bitmap.getID()));
311: break;
312: case CLIPPED_BITMAP:
313: out.println(indent + "FillStyle (CLIPPED_BITMAP):");
314: out.println(indent + " " + matrix);
315: out.println(indent + " bitmapID="
316: + (bitmap == null ? 0xffff : bitmap.getID()));
317: break;
318: }
319: }
320:
321: protected FlashItem copyInto(FlashItem item, ScriptCopier copier) {
322: ((FillStyle) item).type = type;
323: switch (type) {
324: case SOLID:
325: ((FillStyle) item).color = (Color) color.getCopy(copier);
326: break;
327: case LINEAR_GRADIENT:
328: case RADIAL_GRADIENT:
329: ((FillStyle) item).gradient = (Gradient) gradient
330: .getCopy(copier);
331: ((FillStyle) item).matrix = (AffineTransform) matrix
332: .clone();
333: break;
334: case TILED_BITMAP:
335: case CLIPPED_BITMAP:
336: ((FillStyle) item).bitmap = (Bitmap) copier.copy(bitmap);
337: ((FillStyle) item).matrix = (AffineTransform) matrix
338: .clone();
339: break;
340: }
341: return item;
342: }
343:
344: public FlashItem getCopy(ScriptCopier copier) {
345: return copyInto(new FillStyle(), copier);
346: }
347: }
|