1 18 package org.apache.batik.transcoder.wmf.tosvg; 19 20 import java.io.DataInputStream ; 21 import java.io.IOException ; 22 import java.net.URL ; 23 import java.util.Vector ; 24 25 import org.apache.batik.transcoder.wmf.WMFConstants; 26 27 33 public class WMFRecordStore implements WMFConstants{ 34 35 public WMFRecordStore(){ 36 reset(); 37 } 38 39 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 ( 20, 20 ); 50 objectVector = new Vector (); 51 } 52 53 private short readShort( DataInputStream is ) throws IOException { 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 is ) throws IOException { 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 75 public boolean read( DataInputStream is ) throws IOException { 76 reset(); 77 78 setReading( true ); 79 int dwIsAldus = readInt( is ); 80 if ( dwIsAldus == WMFConstants.META_ALDUS_APM ) { 81 83 readShort( is ); 84 readShort( is ); 85 readShort( is ); 86 readShort( is ); 87 readShort( is ); 88 readShort( is ); 89 readInt ( is ); 90 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 readShort( is ); 99 readShort( is ); 100 readShort( is ); 101 readInt ( is ); 102 int mtNoObjects = readShort( is ); 103 readInt ( is ); 104 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 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 int i = 0; 145 for ( ; i < lenText; i++ ) 146 bstr[ i ] = is.readByte(); 147 for ( ; i < len; i++ ) 148 is.readByte(); 149 150 String str = new String ( bstr ); 151 mr = new StringRecord( str ); 152 mr.numPoints = recSize; 153 mr.functionId = functionId; 154 155 mr.AddElement( new Integer ( xVal )); 156 mr.AddElement( new Integer ( 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 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 str = new String ( bstr ); 172 mr = new StringRecord( str ); 173 mr.numPoints = recSize; 174 mr.functionId = functionId; 175 176 mr.AddElement( new Integer ( xVal )); 177 mr.AddElement( new Integer ( yVal )); 178 records.addElement( mr ); 179 } 180 break; 181 182 183 case WMFConstants.META_CREATEFONTINDIRECT: 184 { 185 int lfHeight = readShort( is ); 186 readShort( is ); 187 readShort( is ); 188 readShort( is ); 189 int lfWeight = readShort( is ); 190 191 int lfItalic = is.readByte(); 192 is.readByte(); 193 is.readByte(); 194 is.readByte(); 195 is.readByte(); 196 is.readByte(); 197 is.readByte(); 198 is.readByte(); 199 200 int len = (2*(recSize-9)); byte lfFaceName[] = new byte[ len ]; 202 for ( int i = 0; i < len; i++ ) 203 lfFaceName[ i ] = is.readByte(); 204 205 206 String str = new String ( lfFaceName ); 207 208 mr = new StringRecord( str ); 209 mr.numPoints = recSize; 210 mr.functionId = functionId; 211 212 mr.AddElement( new Integer ( lfHeight )); 213 mr.AddElement( new Integer ( lfItalic )); 214 mr.AddElement( new Integer ( 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 ( i1 )); 228 mr.AddElement( new Integer ( 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 mr.AddElement( new Integer ( 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 mr.AddElement( new Integer ( red )); 252 mr.AddElement( new Integer ( green )); 253 mr.AddElement( new Integer ( blue )); 254 255 mr.AddElement( new Integer ( 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 Integer style = new Integer ( readShort( is )); 269 mr.AddElement( style ); 270 271 int width = readShort( is ); 272 int colorref = readInt ( is ); 273 readShort( is ); 274 275 int red = colorref & 0xff; 276 int green = ( colorref & 0xff00 ) >> 8; 277 int blue = ( colorref & 0xff0000 ) >> 16; 278 mr.AddElement( new Integer ( red )); 280 mr.AddElement( new Integer ( green )); 281 mr.AddElement( new Integer ( blue )); 282 283 mr.AddElement( new Integer ( 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 mr.AddElement( new Integer ( red )); 302 mr.AddElement( new Integer ( green )); 303 mr.AddElement( new Integer ( 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 ( i1 )); 317 mr.AddElement( new Integer ( 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 ( count )); 335 336 for ( int i = 0; i < count; i++ ) 337 mr.AddElement( new Integer ( 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 ( readShort( is ))); 342 mr.AddElement( new Integer ( 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 ( count )); 356 for ( int i = 0; i < count; i++ ) { 357 mr.AddElement( new Integer ( readShort( is ))); 358 mr.AddElement( new Integer ( 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 ( i3 )); 376 mr.AddElement( new Integer ( i2 )); 377 mr.AddElement( new Integer ( i1 )); 378 mr.AddElement( new Integer ( 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 ( i5 )); 395 mr.AddElement( new Integer ( i4 )); 396 mr.AddElement( new Integer ( i3 )); 397 mr.AddElement( new Integer ( i2 )); 398 mr.AddElement( new Integer ( i1 )); 399 mr.AddElement( new Integer ( 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 ( i7 )); 419 mr.AddElement( new Integer ( i6 )); 420 mr.AddElement( new Integer ( i5 )); 421 mr.AddElement( new Integer ( i4 )); 422 mr.AddElement( new Integer ( i3 )); 423 mr.AddElement( new Integer ( i2 )); 424 mr.AddElement( new Integer ( i1 )); 425 mr.AddElement( new Integer ( 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 ( 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 obj ){ 450 int startIdx = 0; 451 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 479 public void addObjectAt( int type, Object 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 497 public URL getUrl() { 498 return url; 499 } 500 501 504 public void setUrl( URL newUrl) { 505 url = newUrl; 506 } 507 508 511 public GdiObject getObject( int idx ) { 512 return (GdiObject)objectVector.elementAt( idx ); 513 } 514 515 518 public MetaRecord getRecord( int idx ) { 519 return (MetaRecord)records.elementAt( idx ); 520 } 521 522 525 public int getNumRecords() { 526 return numRecords; 527 } 528 529 532 public int getNumObjects() { 533 return numObjects; 534 } 535 536 539 public int getVpX() { 540 return vpX; 541 } 542 543 546 public int getVpY() { 547 return vpY; 548 } 549 550 553 public int getVpW() { 554 return vpW; 555 } 556 557 560 public int getVpH() { 561 return vpH; 562 } 563 564 567 public void setVpX( int newValue ) { 568 vpX = newValue; 569 } 570 571 574 public void setVpY( int newValue ) { 575 vpY = newValue; 576 } 577 578 581 public void setVpW( int newValue ) { 582 vpW = newValue; 583 } 584 585 588 public void setVpH( int newValue ) { 589 vpH = newValue; 590 } 591 592 593 transient private URL 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 records; 600 transient protected Vector objectVector; 601 602 transient protected boolean bReading = false; 603 } 604 | Popular Tags |