KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > batik > transcoder > wmf > tosvg > RecordStore


1 /*
2
3    Copyright 2001 The Apache Software Foundation
4
5    Licensed under the Apache License, Version 2.0 (the "License");
6    you may not use this file except in compliance with the License.
7    You may obtain a copy of the License at
8
9        http://www.apache.org/licenses/LICENSE-2.0
10
11    Unless required by applicable law or agreed to in writing, software
12    distributed under the License is distributed on an "AS IS" BASIS,
13    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14    See the License for the specific language governing permissions and
15    limitations under the License.
16
17  */

18
19 package org.apache.batik.transcoder.wmf.tosvg;
20
21 import java.io.DataInputStream JavaDoc;
22 import java.io.IOException JavaDoc;
23 import java.net.URL JavaDoc;
24 import java.util.Vector JavaDoc;
25
26 import org.apache.batik.transcoder.wmf.WMFConstants;
27
28 /**
29  * An object that stores the vector graphics records.
30  *
31  * @author <a HREF="mailto:luano@asd.ie">Luan O'Carroll</a>
32  * @version $Id: RecordStore.java,v 1.3 2004/08/18 07:15:46 vhardy Exp $
33  */

34 public class RecordStore {
35
36     public RecordStore(){
37         reset();
38     }
39
40     /**
41      * Resets the internal storage and viewport coordinates.
42      */

43     public void reset(){
44         numRecords = 0;
45         vpX = 0;
46         vpY = 0;
47         vpW = 1000;
48         vpH = 1000;
49         numObjects = 0;
50         records = new Vector JavaDoc( 20, 20 );
51         objectVector = new Vector JavaDoc();
52     }
53
54     synchronized void setReading( boolean state ){
55         bReading = state;
56     }
57
58     synchronized boolean isReading(){
59         return bReading;
60     }
61
62     /**
63      * Reads the Wmf file from the specified Stream.
64      * A Wmf file can be produced using the GConvert utility found at
65      * http://www.asd.ie/Wmf.htm
66      *
67      * The Wmf format is slightly more compact than the original WMF format and
68      * in some cases may produce better handling of colours.
69      */

70     public boolean read( DataInputStream JavaDoc is ) throws IOException JavaDoc{
71         setReading( true );
72         reset();
73
74         int functionId = 0;
75         numRecords = 0;
76
77         numObjects = is.readShort();
78         objectVector.ensureCapacity( numObjects );
79         for ( int i = 0; i < numObjects; i++ ) {
80             objectVector.addElement( new GdiObject( i, false ));
81         }
82
83         while ( functionId != -1 ) {
84             functionId = is.readShort();
85             if ( functionId == -1 ){
86                 break;
87             }
88
89             MetaRecord mr;
90             switch ( functionId ) {
91             case WMFConstants.META_TEXTOUT:
92             case WMFConstants.META_DRAWTEXT:
93             case WMFConstants.META_EXTTEXTOUT:
94             case WMFConstants.META_CREATEFONTINDIRECT:{
95                 short len = is.readShort();
96                 byte[] b = new byte[ len ];
97                 for ( int i = 0; i < len; i++ ) {
98                     b[ i ] = is.readByte();
99                 }
100                 String JavaDoc str = new String JavaDoc( b );
101                 mr = new StringRecord( str );
102             }
103             break;
104
105             default:
106                 mr = new MetaRecord();
107                 break;
108             }
109
110             int numPts = is.readShort();
111             mr.numPoints = numPts;
112             mr.functionId = functionId;
113
114             for ( int j = 0; j < numPts; j++ ){
115                 mr.AddElement( new Integer JavaDoc( is.readShort()));
116             }
117
118             records.addElement( mr );
119
120             numRecords++;
121         }
122
123         setReading( false );
124         return true;
125     }
126
127     /**
128      * Adds a GdiObject to the internal handle table.
129      * Adds the object at the next free location.
130      *
131      * This function should not normally be called by an application.
132      */

133     public void addObject( int type, Object JavaDoc obj )
134     {
135         for ( int i = 0; i < numObjects; i++ ) {
136             GdiObject gdi = (GdiObject)objectVector.elementAt( i );
137             if ( gdi.used == false ) {
138                 gdi.Setup( type, obj );
139                 lastObjectIdx = i;
140                 break;
141             }
142         }
143     }
144
145     /**
146      * Adds a GdiObject to the internal handle table.
147      * Wmf files specify the index as given in EMF records such as
148      * EMRCREATEPENINDIRECT whereas WMF files always use 0.
149      *
150      * This function should not normally be called by an application.
151      */

152     public void addObjectAt( int type, Object JavaDoc obj, int idx ) {
153         if (( idx == 0 ) || ( idx > numObjects )) {
154             addObject( type, obj );
155             return;
156         }
157         lastObjectIdx = idx;
158         for ( int i = 0; i < numObjects; i++ ) {
159             GdiObject gdi = (GdiObject)objectVector.elementAt( i );
160             if ( i == idx ) {
161                 gdi.Setup( type, obj );
162                 break;
163             }
164         }
165     }
166
167     /**
168      * Returns the current URL
169      */

170     public URL JavaDoc getUrl() {
171         return url;
172     }
173
174     /**
175      * Sets the current URL
176      */

177     public void setUrl( URL JavaDoc newUrl) {
178         url = newUrl;
179     }
180
181     /**
182      * Returns a GdiObject from the handle table
183      */

184     public GdiObject getObject( int idx ) {
185         return (GdiObject)objectVector.elementAt( idx );
186     }
187
188     /**
189      * Returns a meta record.
190      */

191     public MetaRecord getRecord( int idx ) {
192         return (MetaRecord)records.elementAt( idx );
193     }
194
195     /**
196      * Returns a number of records in the image
197      */

198     public int getNumRecords() {
199         return numRecords;
200     }
201
202     /**
203      * Returns the number of GdiObjects in the handle table
204      */

205     public int getNumObjects() {
206         return numObjects;
207     }
208
209     /**
210      * Returns the viewport x origin
211      */

212     public int getVpX() {
213         return vpX;
214     }
215
216     /**
217      * Returns the viewport y origin
218      */

219     public int getVpY() {
220         return vpY;
221     }
222
223     /**
224      * Returns the viewport width
225      */

226     public int getVpW() {
227         return vpW;
228     }
229
230     /**
231      * Returns the viewport height
232      */

233     public int getVpH() {
234         return vpH;
235     }
236
237     /**
238      * Sets the viewport x origin
239      */

240     public void setVpX( int newValue ) {
241         vpX = newValue;
242     }
243
244     /**
245      * Sets the viewport y origin
246      */

247     public void setVpY( int newValue ) {
248         vpY = newValue;
249     }
250
251     /**
252      * Sets the viewport width
253      */

254     public void setVpW( int newValue ) {
255         vpW = newValue;
256     }
257
258     /**
259      * Sets the viewport height
260      */

261     public void setVpH( int newValue ) {
262         vpH = newValue;
263     }
264
265     transient private URL JavaDoc url;
266
267     transient protected int numRecords;
268     transient protected int numObjects;
269     transient public int lastObjectIdx;
270     transient protected int vpX, vpY, vpW, vpH;
271     transient protected Vector JavaDoc records;
272     transient protected Vector JavaDoc objectVector;
273
274     transient protected boolean bReading = false;
275 }
276
Popular Tags