001: /*
002: * $Id: StrightEdgeRecord.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 java.io.PrintStream;
056:
057: /**
058: * Straight-edge record.
059: * <P>
060: * The straight-edge record stores the edge as an X-Y delta.
061: * The delta is added to the current drawing position,
062: * and this becomes the new drawing position.
063: * The edge is rendered between the old and new drawing positions.
064: * <P>
065: * Straight-edge records support three types of line:
066: * <OL>
067: * <LI>General lines.
068: * <LI>Horizontal lines.
069: * <LI>Vertical lines.
070: * </OL>
071: * General lines store both X & Y deltas, the horizontal and vertical
072: * lines store only the X delta and Y delta respectively.
073: *
074: * @author Dmitry Skavish
075: */
076: public final class StrightEdgeRecord extends FlashItem {
077:
078: public static final int GENERAL_LINE = 0;
079: public static final int VERT_LINE = 1;
080: public static final int HORIZ_LINE = 2;
081:
082: private int type; // type of this record
083: private int deltaX; // delta X
084: private int deltaY; // delta Y
085:
086: public StrightEdgeRecord() {
087: }
088:
089: public int getType() {
090: return type;
091: }
092:
093: public void setType(int type) {
094: this .type = type;
095: }
096:
097: public int getDeltaX() {
098: return deltaX;
099: }
100:
101: public void setDeltaX(int deltaX) {
102: this .deltaX = deltaX;
103: }
104:
105: public int getDeltaY() {
106: return deltaY;
107: }
108:
109: public void setDeltaY(int deltaY) {
110: this .deltaY = deltaY;
111: }
112:
113: /**
114: * Creates general line
115: *
116: * @param deltaX new X delta
117: * @param deltaY new Y delta
118: * @return general line
119: */
120: public static StrightEdgeRecord newLine(int deltaX, int deltaY) {
121: StrightEdgeRecord sr = new StrightEdgeRecord();
122: sr.setType(GENERAL_LINE);
123: sr.setDeltaX(deltaX);
124: sr.setDeltaY(deltaY);
125: return sr;
126: }
127:
128: /**
129: * Creates horizontal line
130: *
131: * @param deltaX new X delta
132: * @return horizontal line
133: */
134: public static StrightEdgeRecord newHLine(int deltaX) {
135: StrightEdgeRecord sr = new StrightEdgeRecord();
136: sr.setType(HORIZ_LINE);
137: sr.setDeltaX(deltaX);
138: return sr;
139: }
140:
141: /**
142: * Creates vertical line
143: *
144: * @param deltaY new Y delta
145: * @return vertical line
146: */
147: public static StrightEdgeRecord newVLine(int deltaY) {
148: StrightEdgeRecord sr = new StrightEdgeRecord();
149: sr.setType(VERT_LINE);
150: sr.setDeltaY(deltaY);
151: return sr;
152: }
153:
154: public void write(FlashOutput fob) {
155: fob.writeBits(0x3, 2);
156: switch (type) {
157: case GENERAL_LINE: {
158: int nBits = Util.getMinBitsS(Util.getMax(deltaX, deltaY));
159: if (nBits < 3)
160: nBits = 3;
161: fob.writeBits(nBits - 2, 4);
162: fob.writeBit(1);
163: fob.writeBits(deltaX, nBits);
164: fob.writeBits(deltaY, nBits);
165: break;
166: }
167: case VERT_LINE: {
168: int nBits = Util.getMinBitsS(deltaY);
169: if (nBits < 3)
170: nBits = 3;
171: fob.writeBits(nBits - 2, 4);
172: fob.writeBit(0);
173: fob.writeBit(1);
174: fob.writeBits(deltaY, nBits);
175: break;
176: }
177: case HORIZ_LINE: {
178: int nBits = Util.getMinBitsS(deltaX);
179: if (nBits < 3)
180: nBits = 3;
181: fob.writeBits(nBits - 2, 4);
182: fob.writeBit(0);
183: fob.writeBit(0);
184: fob.writeBits(deltaX, nBits);
185: break;
186: }
187: }
188: }
189:
190: public void printContent(PrintStream out, String indent) {
191: switch (type) {
192: case GENERAL_LINE:
193: out.println(indent + " line (" + deltaX + "," + deltaY
194: + ")");
195: break;
196: case VERT_LINE:
197: out.println(indent + " vline (" + deltaY + ")");
198: break;
199: case HORIZ_LINE:
200: out.println(indent + " hline (" + deltaX + ")");
201: break;
202: }
203: }
204:
205: protected FlashItem copyInto(FlashItem item, ScriptCopier copier) {
206: ((StrightEdgeRecord) item).type = type;
207: ((StrightEdgeRecord) item).deltaX = deltaX;
208: ((StrightEdgeRecord) item).deltaY = deltaY;
209: return item;
210: }
211:
212: public FlashItem getCopy(ScriptCopier copier) {
213: return copyInto(new StrightEdgeRecord(), copier);
214: }
215: }
|