001: /*
002: * $Id: ShapeStyles.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 org.openlaszlo.iv.flash.util.*;
054: import org.openlaszlo.iv.flash.api.*;
055: import org.openlaszlo.iv.flash.parser.*;
056: import java.io.PrintStream;
057:
058: /**
059: * This object represents collection of shape styles.
060: *
061: * @author Dmitry Skavish
062: */
063: public final class ShapeStyles extends FlashItem {
064:
065: /**
066: * Vector of {@link FillStyle}s records
067: */
068: public IVVector fillStyles;
069:
070: /**
071: * Vector of {@link LineStyle}s records.
072: */
073: public IVVector lineStyles;
074:
075: /**
076: * Creates empty shapestyle
077: */
078: public ShapeStyles() {
079: this (new IVVector(), new IVVector());
080: }
081:
082: public ShapeStyles(IVVector fillStyles, IVVector lineStyles) {
083: this .fillStyles = fillStyles;
084: this .lineStyles = lineStyles;
085: }
086:
087: /**
088: * Adds new fillstyle to this array.
089: *
090: * @param fs new fillstyle to be added
091: * @return index of added fillstyle
092: */
093: public int addFillStyle(FillStyle fs) {
094: fillStyles.addElement(fs);
095: return fillStyles.size();
096: }
097:
098: /**
099: * Adds new linestyle to this array.
100: *
101: * @param ls new linestyle to be added
102: * @return index of added linestyle
103: */
104: public int addLineStyle(LineStyle ls) {
105: lineStyles.addElement(ls);
106: return lineStyles.size();
107: }
108:
109: /**
110: * Returns index of specified fillstyle
111: *
112: * @param fs fillstyle which index is to be returned
113: * @return index of specified fillstyle or -1
114: */
115: public int getFillStyleIndex(FillStyle fs) {
116: for (int i = 0; i < fillStyles.size(); i++) {
117: FillStyle ffs = (FillStyle) fillStyles.elementAt(i);
118: if (ffs == fs)
119: return i + 1;
120: }
121: return -1;
122: }
123:
124: /**
125: * Returns index of specified linestyle
126: *
127: * @param ls linestyle which index is to be returned
128: * @return index of specified linestyle or -1
129: */
130: public int getLineStyleIndex(LineStyle ls) {
131: for (int i = 0; i < lineStyles.size(); i++) {
132: LineStyle lls = (LineStyle) lineStyles.elementAt(i);
133: if (lls == ls)
134: return i + 1;
135: }
136: return -1;
137: }
138:
139: /**
140: * Returns line style by its index
141: *
142: * @param index index of line style to be returned
143: * @return line style at specified index
144: */
145: public LineStyle getLineStyle(int index) {
146: return (LineStyle) lineStyles.elementAt(index);
147: }
148:
149: /**
150: * Returns fill style by its index
151: *
152: * @param index index of fill style to be returned
153: * @return fill style at specified index
154: */
155: public FillStyle getFillStyle(int index) {
156: return (FillStyle) fillStyles.elementAt(index);
157: }
158:
159: /**
160: * Returns maximum number of bits required to hold fillstyles' indexes
161: *
162: * @return maximum number of bits required to hold fillstyles' indexes
163: */
164: public int calcNFillBits() {
165: return Util.getMinBitsU(fillStyles.size());
166: }
167:
168: /**
169: * Returns maximum number of bits required to hold linestyles' indexes
170: *
171: * @return maximum number of bits required to hold linestyles' indexes
172: */
173: public int calcNLineBits() {
174: return Util.getMinBitsU(lineStyles.size());
175: }
176:
177: public void collectDeps(DepsCollector dc) {
178: int fillStyleSize = fillStyles.size();
179: for (int i = 0; i < fillStyleSize; i++) {
180: FillStyle fillStyle = (FillStyle) fillStyles.elementAt(i);
181: if (fillStyle.getBitmap() != null)
182: dc.addDep(fillStyle.getBitmap());
183: }
184: }
185:
186: public static ShapeStyles parse(Parser p, boolean withAlpha) {
187: // Get the number of fills.
188: int nFills = p.getUByte();
189: if (nFills == 255) {
190: nFills = p.getUWord();
191: }
192: IVVector fillStyles = new IVVector(nFills);
193:
194: // Get each of the fill style.
195: for (int i = 0; i < nFills; i++) {
196: fillStyles.addElement(FillStyle.parse(p, withAlpha));
197: }
198:
199: int nLines = p.getUByte();
200: if (nLines == 255) {
201: nLines = p.getUWord();
202: }
203: IVVector lineStyles = new IVVector(nLines);
204:
205: // Get each of the line styles.
206: for (int i = 0; i < nLines; i++) {
207: lineStyles.addElement(LineStyle.parse(p, withAlpha));
208: }
209:
210: return new ShapeStyles(fillStyles, lineStyles);
211: }
212:
213: public void write(FlashOutput fob) {
214: int fillStyleSize = fillStyles.size();
215: if (fillStyleSize > 254) {
216: fob.writeByte(255);
217: fob.writeWord(fillStyleSize);
218: } else {
219: fob.writeByte(fillStyleSize);
220: }
221: fillStyles.write(fob);
222: int lineStyleSize = lineStyles.size();
223: if (lineStyleSize > 254) {
224: fob.writeByte(255);
225: fob.writeWord(lineStyleSize);
226: } else {
227: fob.writeByte(lineStyleSize);
228: }
229: lineStyles.write(fob);
230: }
231:
232: public void printContent(PrintStream out, String indent) {
233: out.println(indent + "ShapeStyles:");
234: fillStyles.printContent(out, indent + " ");
235: lineStyles.printContent(out, indent + " ");
236: }
237:
238: protected FlashItem copyInto(FlashItem item, ScriptCopier copier) {
239: ((ShapeStyles) item).fillStyles = fillStyles.getCopy(copier);
240: ((ShapeStyles) item).lineStyles = lineStyles.getCopy(copier);
241: return item;
242: }
243:
244: public FlashItem getCopy(ScriptCopier copier) {
245: return copyInto(new ShapeStyles(null, null), copier);
246: }
247: }
|