KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2
3    Copyright 2001-2003 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 package org.apache.batik.transcoder.wmf.tosvg;
19
20 import java.io.DataInputStream JavaDoc;
21 import java.io.IOException JavaDoc;
22 import java.net.URL JavaDoc;
23 import java.util.Vector JavaDoc;
24
25 import org.apache.batik.transcoder.wmf.WMFConstants;
26
27 /**
28  * Reads a WMF file, including an Aldus Placable Metafile Header.
29  *
30  * @author <a HREF="mailto:luano@asd.ie">Luan O'Carroll</a>
31  * @version $Id: WMFRecordStore.java,v 1.6 2004/08/18 07:15:47 vhardy Exp $
32  */

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

42     public void reset(){
43       numRecords = 0;
44       vpX = 0;
45       vpY = 0;
46       vpW = 1000;
47       vpH = 1000;
48       numObjects = 0;
49       records = new Vector JavaDoc( 20, 20 );
50       objectVector = new Vector JavaDoc();
51     }
52
53     private short readShort( DataInputStream JavaDoc is ) throws IOException JavaDoc{
54         byte js[] = new byte[ 2 ];
55         is.read( js );
56         int iTemp = ((0xff) & js[ 1 ] ) << 8;
57         short i = (short)(0xffff & iTemp);
58         i |= ((0xff) & js[ 0 ] );
59         return i;
60     }
61
62     private int readInt( DataInputStream JavaDoc is ) throws IOException JavaDoc {
63         byte js[] = new byte[ 4 ];
64         is.read( js );
65         int i = ((0xff) & js[ 3 ] ) << 24;
66         i |= ((0xff) & js[ 2 ] ) << 16;
67         i |= ((0xff) & js[ 1 ] ) << 8;
68         i |= ((0xff) & js[ 0 ] );
69         return i;
70     }
71
72     /**
73      * Reads the WMF file from the specified Stream.
74      */

75     public boolean read( DataInputStream JavaDoc is ) throws IOException JavaDoc{
76         reset();
77
78         setReading( true );
79         int dwIsAldus = readInt( is );
80         if ( dwIsAldus == WMFConstants.META_ALDUS_APM ) {
81             // Read the aldus placeable header.
82
/* int key = dwIsAldus; */
83             /* short hmf = */ readShort( is );
84             /* short left = */ readShort( is );
85             /* short top = */ readShort( is );
86             /* short right = */ readShort( is );
87             /* short bottom = */ readShort( is );
88             /* short inch = */ readShort( is );
89             /* int reserved = */ readInt ( is );
90             /* short checksum = */ readShort( is );
91         }
92         else {
93             System.out.println( "Unable to read file, it is not a Aldus Placable Metafile" );
94             setReading( false );
95             return false;
96         }
97
98         /* int mtType = */ readShort( is );
99         /* int mtHeaderSize = */ readShort( is );
100         /* int mtVersion = */ readShort( is );
101         /* int mtSize = */ readInt ( is );
102         int mtNoObjects = readShort( is );
103         /* int mtMaxRecord = */ readInt ( is );
104         /* int mtNoParameters = */ readShort( is );
105
106
107         short functionId = 1;
108         int recSize = 0;
109
110         numRecords = 0;
111
112         numObjects = mtNoObjects;
113         objectVector.ensureCapacity( numObjects );
114         for ( int i = 0; i < numObjects; i++ ) {
115             objectVector.addElement( new GdiObject( i, false ));
116         }
117
118         while ( functionId > 0 ) {
119             recSize = readInt( is );
120             // Subtract size in 16-bit words of recSize and functionId;
121
recSize -= 3;
122             functionId = readShort( is );
123             if ( functionId <= 0 )
124                 break;
125
126             MetaRecord mr = new MetaRecord();
127             switch ( functionId ) {
128             case WMFConstants.META_DRAWTEXT:
129                 {
130                     for ( int i = 0; i < recSize; i++ )
131                         readShort( is );
132                     numRecords--;
133                 }
134                 break;
135
136             case WMFConstants.META_EXTTEXTOUT:
137                 {
138                     int yVal = readShort( is );
139                     int xVal = readShort( is );
140                     int lenText = readInt( is );
141                     int len = 2*(recSize-4);
142                     byte bstr[] = new byte[ lenText ];
143                     //is.read( bstr );
144
int i = 0;
145                     for ( ; i < lenText; i++ )
146                         bstr[ i ] = is.readByte();
147                     for ( ; i < len; i++ )
148                         is.readByte();
149
150                     String JavaDoc str = new String JavaDoc( bstr );
151                     mr = new StringRecord( str );
152                     mr.numPoints = recSize;
153                     mr.functionId = functionId;
154
155                     mr.AddElement( new Integer JavaDoc( xVal ));
156                     mr.AddElement( new Integer JavaDoc( yVal ));
157                     records.addElement( mr );
158                 }
159                 break;
160
161             case WMFConstants.META_TEXTOUT:
162                 {
163                     int len = readShort( is );
164                     byte bstr[] = new byte[ len ];
165                     //is.read( bstr );
166
for ( int i = 0; i < len; i++ )
167                         bstr[ i ] = is.readByte();
168                     int yVal = readShort( is );
169                     int xVal = readShort( is );
170
171                     String JavaDoc str = new String JavaDoc( bstr );
172                     mr = new StringRecord( str );
173                     mr.numPoints = recSize;
174                     mr.functionId = functionId;
175
176                     mr.AddElement( new Integer JavaDoc( xVal ));
177                     mr.AddElement( new Integer JavaDoc( yVal ));
178                     records.addElement( mr );
179                 }
180                 break;
181
182
183             case WMFConstants.META_CREATEFONTINDIRECT:
184                 {
185                     int lfHeight = readShort( is );
186                     /* int lfWidth = */ readShort( is );
187                     /* int lfEscapement = */ readShort( is );
188                     /* int lfOrientation = */ readShort( is );
189                     int lfWeight = readShort( is );
190
191                     int lfItalic = is.readByte();
192                     /* int lfUnderline = */ is.readByte();
193                     /* int lfStrikeOut = */ is.readByte();
194                     /* int lfCharSet = */ is.readByte();
195                     /* int lfOutPrecision = */ is.readByte();
196                     /* int lfClipPrecision = */ is.readByte();
197                     /* int lfQuality = */ is.readByte();
198                     /* int lfPitchAndFamily = */ is.readByte();
199
200                     int len = (2*(recSize-9));//13));
201
byte lfFaceName[] = new byte[ len ];
202                     for ( int i = 0; i < len; i++ )
203                         lfFaceName[ i ] = is.readByte();
204
205
206                     String JavaDoc str = new String JavaDoc( lfFaceName );
207
208                     mr = new StringRecord( str );
209                     mr.numPoints = recSize;
210                     mr.functionId = functionId;
211
212                     mr.AddElement( new Integer JavaDoc( lfHeight ));
213                     mr.AddElement( new Integer JavaDoc( lfItalic ));
214                     mr.AddElement( new Integer JavaDoc( lfWeight ));
215                     records.addElement( mr );
216                 }
217                 break;
218
219             case WMFConstants.META_SETWINDOWORG:
220             case WMFConstants.META_SETWINDOWEXT:
221                 {
222                     mr.numPoints = recSize;
223                     mr.functionId = functionId;
224
225                     int i0 = readShort( is );
226                     int i1 = readShort( is );
227                     mr.AddElement( new Integer JavaDoc( i1 ));
228                     mr.AddElement( new Integer JavaDoc( i0 ));
229                     records.addElement( mr );
230
231                     if ( functionId == WMFConstants.META_SETWINDOWEXT ) {
232                       vpW = i0;
233                       vpH = i1;
234                     }
235                 }
236                 break;
237
238             case WMFConstants.META_CREATEBRUSHINDIRECT:
239                 {
240                     mr.numPoints = recSize;
241                     mr.functionId = functionId;
242
243                     // The style
244
mr.AddElement( new Integer JavaDoc( readShort( is )));
245
246                     int colorref = readInt( is );
247                     int red = colorref & 0xff;
248                     int green = ( colorref & 0xff00 ) >> 8;
249                     int blue = ( colorref & 0xff0000 ) >> 16;
250                     // int flags = ( colorref & 0x3000000 ) >> 24;
251
mr.AddElement( new Integer JavaDoc( red ));
252                     mr.AddElement( new Integer JavaDoc( green ));
253                     mr.AddElement( new Integer JavaDoc( blue ));
254
255                     // The hatch style
256
mr.AddElement( new Integer JavaDoc( readShort( is )));
257
258                     records.addElement( mr );
259                 }
260                 break;
261
262             case WMFConstants.META_CREATEPENINDIRECT:
263                 {
264                     mr.numPoints = recSize;
265                     mr.functionId = functionId;
266
267                     // The style
268
Integer JavaDoc style = new Integer JavaDoc( readShort( is ));
269                     mr.AddElement( style );
270
271                     int width = readShort( is );
272                     int colorref = readInt ( is );
273                     /* int height = */ readShort( is );
274
275                     int red = colorref & 0xff;
276                     int green = ( colorref & 0xff00 ) >> 8;
277                     int blue = ( colorref & 0xff0000 ) >> 16;
278                     // int flags = ( colorref & 0x3000000 ) >> 24;
279
mr.AddElement( new Integer JavaDoc( red ));
280                     mr.AddElement( new Integer JavaDoc( green ));
281                     mr.AddElement( new Integer JavaDoc( blue ));
282
283                     // The pen width
284
mr.AddElement( new Integer JavaDoc( width ));
285
286                     records.addElement( mr );
287                 }
288                 break;
289
290             case WMFConstants.META_SETTEXTCOLOR:
291             case WMFConstants.META_SETBKCOLOR:
292                 {
293                     mr.numPoints = recSize;
294                     mr.functionId = functionId;
295
296                     int colorref = readInt( is );
297                     int red = colorref & 0xff;
298                     int green = ( colorref & 0xff00 ) >> 8;
299                     int blue = ( colorref & 0xff0000 ) >> 16;
300                     // int flags = ( colorref & 0x3000000 ) >> 24;
301
mr.AddElement( new Integer JavaDoc( red ));
302                     mr.AddElement( new Integer JavaDoc( green ));
303                     mr.AddElement( new Integer JavaDoc( blue ));
304                     records.addElement( mr );
305                 }
306                 break;
307
308             case WMFConstants.META_LINETO:
309             case WMFConstants.META_MOVETO:
310                 {
311                     mr.numPoints = recSize;
312                     mr.functionId = functionId;
313
314                     int i0 = readShort( is );
315                     int i1 = readShort( is );
316                     mr.AddElement( new Integer JavaDoc( i1 ));
317                     mr.AddElement( new Integer JavaDoc( i0 ));
318                     records.addElement( mr );
319                 }
320                 break;
321
322             case WMFConstants.META_POLYPOLYGON:
323                 {
324                     mr.numPoints = recSize;
325                     mr.functionId = functionId;
326
327                     int count = readShort( is );
328                     int pts[] = new int[ count ];
329                     int ptCount = 0;
330                     for ( int i = 0; i < count; i++ ) {
331                         pts[ i ] = readShort( is );
332                         ptCount += pts[ i ];
333                     }
334                     mr.AddElement( new Integer JavaDoc( count ));
335
336                     for ( int i = 0; i < count; i++ )
337                         mr.AddElement( new Integer JavaDoc( pts[ i ] ));
338
339                     for ( int i = 0; i < count; i++ ) {
340                         for ( int j = 0; j < pts[ i ]; j++ ) {
341                             mr.AddElement( new Integer JavaDoc( readShort( is )));
342                             mr.AddElement( new Integer JavaDoc( readShort( is )));
343                         }
344                     }
345                     records.addElement( mr );
346                 }
347                 break;
348
349             case WMFConstants.META_POLYGON:
350                 {
351                     mr.numPoints = recSize;
352                     mr.functionId = functionId;
353
354                     int count = readShort( is );
355                     mr.AddElement( new Integer JavaDoc( count ));
356                     for ( int i = 0; i < count; i++ ) {
357                         mr.AddElement( new Integer JavaDoc( readShort( is )));
358                         mr.AddElement( new Integer JavaDoc( readShort( is )));
359                     }
360                     records.addElement( mr );
361                 }
362                 break;
363
364             case WMFConstants.META_ELLIPSE:
365             case WMFConstants.META_INTERSECTCLIPRECT:
366             case WMFConstants.META_RECTANGLE:
367                 {
368                     mr.numPoints = recSize;
369                     mr.functionId = functionId;
370
371                     int i0 = readShort( is );
372                     int i1 = readShort( is );
373                     int i2 = readShort( is );
374                     int i3 = readShort( is );
375                     mr.AddElement( new Integer JavaDoc( i3 ));
376                     mr.AddElement( new Integer JavaDoc( i2 ));
377                     mr.AddElement( new Integer JavaDoc( i1 ));
378                     mr.AddElement( new Integer JavaDoc( i0 ));
379                     records.addElement( mr );
380                 }
381                 break;
382
383             case WMFConstants.META_ROUNDRECT:
384                 {
385                     mr.numPoints = recSize;
386                     mr.functionId = functionId;
387
388                     int i0 = readShort( is );
389                     int i1 = readShort( is );
390                     int i2 = readShort( is );
391                     int i3 = readShort( is );
392                     int i4 = readShort( is );
393                     int i5 = readShort( is );
394                     mr.AddElement( new Integer JavaDoc( i5 ));
395                     mr.AddElement( new Integer JavaDoc( i4 ));
396                     mr.AddElement( new Integer JavaDoc( i3 ));
397                     mr.AddElement( new Integer JavaDoc( i2 ));
398                     mr.AddElement( new Integer JavaDoc( i1 ));
399                     mr.AddElement( new Integer JavaDoc( i0 ));
400                     records.addElement( mr );
401                 }
402                 break;
403
404             case WMFConstants.META_ARC:
405             case WMFConstants.META_PIE:
406                 {
407                     mr.numPoints = recSize;
408                     mr.functionId = functionId;
409
410                     int i0 = readShort( is );
411                     int i1 = readShort( is );
412                     int i2 = readShort( is );
413                     int i3 = readShort( is );
414                     int i4 = readShort( is );
415                     int i5 = readShort( is );
416                     int i6 = readShort( is );
417                     int i7 = readShort( is );
418                     mr.AddElement( new Integer JavaDoc( i7 ));
419                     mr.AddElement( new Integer JavaDoc( i6 ));
420                     mr.AddElement( new Integer JavaDoc( i5 ));
421                     mr.AddElement( new Integer JavaDoc( i4 ));
422                     mr.AddElement( new Integer JavaDoc( i3 ));
423                     mr.AddElement( new Integer JavaDoc( i2 ));
424                     mr.AddElement( new Integer JavaDoc( i1 ));
425                     mr.AddElement( new Integer JavaDoc( i0 ));
426                     records.addElement( mr );
427                 }
428                 break;
429
430             default:
431                 mr.numPoints = recSize;
432                 mr.functionId = functionId;
433
434                 for ( int j = 0; j < recSize; j++ )
435                     mr.AddElement( new Integer JavaDoc( readShort( is )));
436
437                 records.addElement( mr );
438                 break;
439
440             }
441
442             numRecords++;
443         }
444
445         setReading( false );
446         return true;
447     }
448
449     public void addObject( int type, Object JavaDoc obj ){
450         int startIdx = 0;
451         // if ( type == Wmf.PEN ) {
452
// startIdx = 2;
453
// }
454
for ( int i = startIdx; i < numObjects; i++ ) {
455             GdiObject gdi = (GdiObject)objectVector.elementAt( i );
456             if ( gdi.used == false ) {
457                 gdi.Setup( type, obj );
458                 lastObjectIdx = i;
459                 break;
460             }
461         }
462     }
463
464     synchronized void setReading( boolean state ){
465       bReading = state;
466     }
467
468     synchronized boolean isReading(){
469       return bReading;
470     }
471
472     /**
473      * Adds a GdiObject to the internal handle table.
474      * Wmf files specify the index as given in EMF records such as
475      * EMRCREATEPENINDIRECT whereas WMF files always use 0.
476      *
477      * This function should not normally be called by an application.
478      */

479     public void addObjectAt( int type, Object JavaDoc obj, int idx ) {
480       if (( idx == 0 ) || ( idx > numObjects )) {
481         addObject( type, obj );
482         return;
483       }
484       lastObjectIdx = idx;
485       for ( int i = 0; i < numObjects; i++ ) {
486         GdiObject gdi = (GdiObject)objectVector.elementAt( i );
487         if ( i == idx ) {
488           gdi.Setup( type, obj );
489           break;
490         }
491       }
492     }
493
494     /**
495      * Returns the current URL
496      */

497     public URL JavaDoc getUrl() {
498       return url;
499     }
500
501     /**
502      * Sets the current URL
503      */

504     public void setUrl( URL JavaDoc newUrl) {
505       url = newUrl;
506     }
507
508     /**
509      * Returns a GdiObject from the handle table
510      */

511     public GdiObject getObject( int idx ) {
512       return (GdiObject)objectVector.elementAt( idx );
513     }
514
515     /**
516      * Returns a meta record.
517      */

518     public MetaRecord getRecord( int idx ) {
519       return (MetaRecord)records.elementAt( idx );
520     }
521
522     /**
523      * Returns a number of records in the image
524      */

525     public int getNumRecords() {
526       return numRecords;
527     }
528
529     /**
530      * Returns the number of GdiObjects in the handle table
531      */

532     public int getNumObjects() {
533       return numObjects;
534     }
535
536     /**
537      * Returns the viewport x origin
538      */

539     public int getVpX() {
540       return vpX;
541     }
542
543     /**
544      * Returns the viewport y origin
545      */

546     public int getVpY() {
547       return vpY;
548     }
549
550     /**
551      * Returns the viewport width
552      */

553     public int getVpW() {
554       return vpW;
555     }
556
557     /**
558      * Returns the viewport height
559      */

560     public int getVpH() {
561       return vpH;
562     }
563
564     /**
565      * Sets the viewport x origin
566      */

567     public void setVpX( int newValue ) {
568       vpX = newValue;
569     }
570
571     /**
572      * Sets the viewport y origin
573      */

574     public void setVpY( int newValue ) {
575       vpY = newValue;
576     }
577
578     /**
579      * Sets the viewport width
580      */

581     public void setVpW( int newValue ) {
582       vpW = newValue;
583     }
584
585     /**
586      * Sets the viewport height
587      */

588     public void setVpH( int newValue ) {
589       vpH = newValue;
590     }
591
592
593     transient private URL JavaDoc url;
594
595     transient protected int numRecords;
596     transient protected int numObjects;
597     transient public int lastObjectIdx;
598     transient protected int vpX, vpY, vpW, vpH;
599     transient protected Vector JavaDoc records;
600     transient protected Vector JavaDoc objectVector;
601
602     transient protected boolean bReading = false;
603 }
604
Popular Tags