001: /*
002: * $Id: MorphShapeStyles.java,v 1.1 2002/02/15 23:46:26 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 MorphShapeStyles extends FlashItem {
064:
065: /**
066: * Vector of {@link MorphFillStyle}s records
067: */
068: public IVVector fillStyles;
069:
070: /**
071: * Vector of {@link MorphLineStyle}s records.
072: */
073: public IVVector lineStyles;
074:
075: /**
076: * Creates empty shapestyle
077: */
078: public MorphShapeStyles() {
079: this (new IVVector(), new IVVector());
080: }
081:
082: public MorphShapeStyles(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(MorphFillStyle 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(MorphLineStyle 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(MorphFillStyle fs) {
116: for (int i = 0; i < fillStyles.size(); i++) {
117: MorphFillStyle ffs = (MorphFillStyle) fillStyles
118: .elementAt(i);
119: if (ffs == fs)
120: return i + 1;
121: }
122: return -1;
123: }
124:
125: /**
126: * Returns index of specified linestyle
127: *
128: * @param ls linestyle which index is to be returned
129: * @return index of specified linestyle or -1
130: */
131: public int getLineStyleIndex(MorphLineStyle ls) {
132: for (int i = 0; i < lineStyles.size(); i++) {
133: MorphLineStyle lls = (MorphLineStyle) lineStyles
134: .elementAt(i);
135: if (lls == ls)
136: return i + 1;
137: }
138: return -1;
139: }
140:
141: /**
142: * Returns line style by its index
143: *
144: * @param index index of line style to be returned
145: * @return line style at specified index
146: */
147: public MorphLineStyle getLineStyle(int index) {
148: return (MorphLineStyle) lineStyles.elementAt(index);
149: }
150:
151: /**
152: * Returns fill style by its index
153: *
154: * @param index index of fill style to be returned
155: * @return fill style at specified index
156: */
157: public MorphFillStyle getFillStyle(int index) {
158: return (MorphFillStyle) fillStyles.elementAt(index);
159: }
160:
161: /**
162: * Returns maximum number of bits required to hold fillstyles' indexes
163: *
164: * @return maximum number of bits required to hold fillstyles' indexes
165: */
166: public int calcNFillBits() {
167: return Util.getMinBitsU(fillStyles.size());
168: }
169:
170: /**
171: * Returns maximum number of bits required to hold linestyles' indexes
172: *
173: * @return maximum number of bits required to hold linestyles' indexes
174: */
175: public int calcNLineBits() {
176: return Util.getMinBitsU(lineStyles.size());
177: }
178:
179: public void collectDeps(DepsCollector dc) {
180: int fillStyleSize = fillStyles.size();
181: for (int i = 0; i < fillStyleSize; i++) {
182: MorphFillStyle fillStyle = (MorphFillStyle) fillStyles
183: .elementAt(i);
184: if (fillStyle.getBitmap() != null)
185: dc.addDep(fillStyle.getBitmap());
186: }
187: }
188:
189: public void write(FlashOutput fob) {
190: int fillStyleSize = fillStyles.size();
191: if (fillStyleSize > 254) {
192: fob.writeByte(255);
193: fob.writeWord(fillStyleSize);
194: } else {
195: fob.writeByte(fillStyleSize);
196: }
197: fillStyles.write(fob);
198: int lineStyleSize = lineStyles.size();
199: if (lineStyleSize > 254) {
200: fob.writeByte(255);
201: fob.writeWord(lineStyleSize);
202: } else {
203: fob.writeByte(lineStyleSize);
204: }
205: lineStyles.write(fob);
206: }
207:
208: public static MorphShapeStyles parse(Parser p) {
209: // Get the number of fills.
210: int nFills = p.getUByte();
211: if (nFills == 255) {
212: nFills = p.getUWord();
213: }
214: IVVector fillStyles = new IVVector(nFills);
215:
216: // Get each of the fill style.
217: for (int i = 0; i < nFills; i++) {
218: fillStyles.addElement(MorphFillStyle.parse(p));
219: }
220:
221: int nLines = p.getUByte();
222: if (nLines == 255) {
223: nLines = p.getUWord();
224: }
225: IVVector lineStyles = new IVVector(nLines);
226:
227: // Get each of the line styles.
228: for (int i = 0; i < nLines; i++) {
229: lineStyles.addElement(MorphLineStyle.parse(p));
230: }
231:
232: return new MorphShapeStyles(fillStyles, lineStyles);
233: }
234:
235: public void printContent(PrintStream out, String indent) {
236: out.println(indent + "MorphShapeStyles:");
237: fillStyles.printContent(out, indent + " ");
238: lineStyles.printContent(out, indent + " ");
239: }
240:
241: protected FlashItem copyInto(FlashItem item, ScriptCopier copier) {
242: ((MorphShapeStyles) item).fillStyles = fillStyles
243: .getCopy(copier);
244: ((MorphShapeStyles) item).lineStyles = lineStyles
245: .getCopy(copier);
246: return item;
247: }
248:
249: public FlashItem getCopy(ScriptCopier copier) {
250: return copyInto(new MorphShapeStyles(null, null), copier);
251: }
252: }
|