001: /*
002: * @(#)Jmf.java 2.00 28/4/1999
003: *
004: * Copyright (c) 1997-9 Axiom Software Development Ltd..
005: * All Rights Reserved.
006: *
007: * Permission to use, copy, modify, and distribute this
008: * software and its documentation for NON-COMMERCIAL purposes
009: * and without fee is hereby granted provided that this
010: * copyright notice appears in all copies. Please refer to
011: * the file "copyright.html" for further important copyright
012: * and licensing information.
013: *
014: * ASD MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE
015: * SUITABILITY OF THE SOFTWARE, EITHER EXPRESS OR IMPLIED,
016: * INCLUDING BUT NOT LIMITED TO THE IMPLIED WARRANTIES OF
017: * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, OR
018: * NON-INFRINGEMENT. ASD SHALL NOT BE LIABLE FOR ANY DAMAGES
019: * SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR
020: * DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES.
021: * $Revision: 1.12 $
022: */
023: package net.xoetrope.xui.wmf;
024:
025: import java.io.DataInputStream;
026: import java.io.IOException;
027: import java.net.URL;
028: import java.util.Vector;
029:
030: /**
031: * An object that stores the vector graphics records.
032: * @version 2.00
033: */
034: public class RecordStore {
035:
036: public RecordStore() {
037: reset();
038: }
039:
040: /**
041: * Resets the internal storage and viewport coordinates.
042: */
043: public void reset() {
044: numRecords = 0;
045: vpX = 0;
046: vpY = 0;
047: vpW = 1000;
048: vpH = 1000;
049: numObjects = 0;
050: records = new Vector(20, 20);
051: objectVector = new Vector();
052: }
053:
054: synchronized void setReading(boolean state) {
055: bReading = state;
056: }
057:
058: synchronized boolean isReading() {
059: return bReading;
060: }
061:
062: /**
063: * Reads the JMF file from the specified Stream.
064: * A JMF file can be produced using the GConvert utility found at
065: * http://www.asd.ie/jmf.htm
066: *
067: * The JMF format is slightly more compact than the original WMF format and
068: * in some cases may produce better handling of colours.
069: */
070: public boolean read(DataInputStream is) throws IOException {
071: setReading(true);
072: reset();
073:
074: int functionId = 0;
075: numRecords = 0;
076:
077: numObjects = is.readShort();
078: objectVector.ensureCapacity(numObjects);
079: for (int i = 0; i < numObjects; i++) {
080: objectVector.addElement(new GdiObject(i, false));
081: }
082:
083: while (functionId != -1) {
084: functionId = is.readShort();
085: if (functionId == -1)
086: break;
087:
088: MetaRecord mr;
089: switch (functionId) {
090: case 0x0521:
091: case 0x062F:
092: case 0x0a32:
093: case 0x02FB: {
094: short len = is.readShort();
095: byte[] b = new byte[len];
096: for (int i = 0; i < len; i++)
097: b[i] = is.readByte();
098: String str = new String(b);
099: mr = new StringRecord(str);
100: }
101: break;
102:
103: default:
104: mr = new MetaRecord();
105: break;
106: }
107:
108: int numPts = is.readShort();
109: mr.numPoints = numPts;
110: mr.functionId = functionId;
111:
112: for (int j = 0; j < numPts; j++)
113: mr.AddElement(new Integer(is.readShort()));
114:
115: records.addElement(mr);
116:
117: numRecords++;
118: }
119:
120: setReading(false);
121: return true;
122: }
123:
124: /**
125: * Adds a GdiObject to the internal handle table.
126: * Adds the object at the next free location.
127: *
128: * This function should not normally be called by an application.
129: */
130: public int addObject(int type, Object obj) {
131: for (int i = 0; i < numObjects; i++) {
132: GdiObject gdi = (GdiObject) objectVector.elementAt(i);
133: if (gdi.used == false) {
134: gdi.Setup(type, obj);
135: lastObjectIdx = i;
136: return i;
137: }
138: }
139: return -1;
140: }
141:
142: /**
143: * Adds a GdiObject to the internal handle table.
144: * JMF files specify the index as given in EMF records such as
145: * EMRCREATEPENINDIRECT whereas WMF files always use 0.
146: *
147: * This function should not normally be called by an application.
148: */
149: public int addObjectAt(int type, Object obj, int idx) {
150: if ((idx == 0) || (idx > numObjects)) {
151: return addObject(type, obj);
152: }
153: lastObjectIdx = idx;
154: for (int i = 0; i < numObjects; i++) {
155: GdiObject gdi = (GdiObject) objectVector.elementAt(i);
156: if (i == idx) {
157: gdi.Setup(type, obj);
158: return i;
159: }
160: }
161: return -1;
162: }
163:
164: /**
165: * Returns the current URL
166: */
167: public URL getUrl() {
168: return url;
169: }
170:
171: /**
172: * Sets the current URL
173: */
174: public void setUrl(URL newUrl) {
175: url = newUrl;
176: }
177:
178: /**
179: * Returns a GdiObject from the handle table
180: */
181: public GdiObject getObject(int idx) {
182: return (GdiObject) objectVector.elementAt(idx);
183: }
184:
185: /**
186: * Returns a meta record.
187: */
188: public MetaRecord getRecord(int idx) {
189: return (MetaRecord) records.elementAt(idx);
190: }
191:
192: /**
193: * Returns a number of records in the image
194: */
195: public int getNumRecords() {
196: return numRecords;
197: }
198:
199: /**
200: * Returns the number of GdiObjects in the handle table
201: */
202: public int getNumObjects() {
203: return numObjects;
204: }
205:
206: /**
207: * Returns the viewport x origin
208: */
209: public int getVpX() {
210: return vpX;
211: }
212:
213: /**
214: * Returns the viewport y origin
215: */
216: public int getVpY() {
217: return vpY;
218: }
219:
220: /**
221: * Returns the viewport width
222: */
223: public int getVpW() {
224: return vpW;
225: }
226:
227: /**
228: * Returns the viewport height
229: */
230: public int getVpH() {
231: return vpH;
232: }
233:
234: /**
235: * Sets the viewport x origin
236: */
237: public void setVpX(int newValue) {
238: vpX = newValue;
239: }
240:
241: /**
242: * Sets the viewport y origin
243: */
244: public void setVpY(int newValue) {
245: vpY = newValue;
246: }
247:
248: /**
249: * Sets the viewport width
250: */
251: public void setVpW(int newValue) {
252: vpW = newValue;
253: }
254:
255: /**
256: * Sets the viewport height
257: */
258: public void setVpH(int newValue) {
259: vpH = newValue;
260: }
261:
262: transient private URL url;
263:
264: transient protected int numRecords;
265: transient protected int numObjects;
266: transient public int lastObjectIdx;
267: transient protected int vpX, vpY, vpW, vpH;
268: transient protected Vector records;
269: transient protected Vector objectVector;
270:
271: transient protected boolean bReading = false;
272: }
|