You are viewing a plain text version of this content. The canonical link for it is here.
Posted to batik-dev@xmlgraphics.apache.org by vh...@apache.org on 2001/03/16 23:55:00 UTC

cvs commit: xml-batik/sources/org/apache/batik/transcoder/wmf/tosvg RecordStore.java WMFPainter.java WMFRecordStore.java WMFTranscoder.java

vhardy      01/03/16 14:55:00

  Added:       sources/org/apache/batik/transcoder/wmf WMFConstants.java
               sources/org/apache/batik/transcoder/wmf/tosvg
                        RecordStore.java WMFPainter.java
                        WMFRecordStore.java WMFTranscoder.java
  Log:
  Initial commit of the WMF to SVG transcoder.
  
  Revision  Changes    Path
  1.1                  xml-batik/sources/org/apache/batik/transcoder/wmf/WMFConstants.java
  
  Index: WMFConstants.java
  ===================================================================
  /*****************************************************************************
   * Copyright (C) The Apache Software Foundation. All rights reserved.        *
   * ------------------------------------------------------------------------- *
   * This software is published under the terms of the Apache Software License *
   * version 1.1, a copy of which has been included with this distribution in  *
   * the LICENSE file.                                                         *
   *****************************************************************************/
  
  package org.apache.batik.transcoder.wmf;
  
  /**
   * Contains the definitions of WMF constants.
   *
   * @author <a href="mailto:luano@asd.ie">Luan O'Carroll</a>
   * @version $Id: WMFConstants.java,v 1.1 2001/03/16 22:54:58 vhardy Exp $
   */
  public interface WMFConstants
  {
      public static final int META_ALDUS_APM              = 0x9ac6cdd7;
  
      public static final int META_DRAWTEXT               = 0x062F;
      public static final int META_SETBKCOLOR             = 0x0201;
      public static final int META_SETBKMODE              = 0x0102;
      public static final int META_SETMAPMODE             = 0x0103;
      public static final int META_SETROP2                = 0x0104;
      public static final int META_SETRELABS              = 0x0105;
      public static final int META_SETPOLYFILLMODE        = 0x0106;
      public static final int META_SETSTRETCHBLTMODE      = 0x0107;
      public static final int META_SETTEXTCHAREXTRA       = 0x0108;
      public static final int META_SETTEXTCOLOR           = 0x0209;
      public static final int META_SETTEXTJUSTIFICATION   = 0x020A;
      public static final int META_SETWINDOWORG           = 0x020B;
      public static final int META_SETWINDOWORG_EX        = 0x0000; // ???? LOOKS SUSPICIOUS
      public static final int META_SETWINDOWEXT           = 0x020C;
      public static final int META_SETVIEWPORTORG         = 0x020D;
      public static final int META_SETVIEWPORTEXT         = 0x020E;
      public static final int META_OFFSETWINDOWORG        = 0x020F;
      public static final int META_SCALEWINDOWEXT         = 0x0410;
      public static final int META_OFFSETVIEWPORTORG      = 0x0211;
      public static final int META_SCALEVIEWPORTEXT       = 0x0412;
      public static final int META_LINETO                 = 0x0213;
      public static final int META_MOVETO                 = 0x0214;
      public static final int META_EXCLUDECLIPRECT        = 0x0415;
      public static final int META_INTERSECTCLIPRECT      = 0x0416;
      public static final int META_ARC                    = 0x0817;
      public static final int META_ELLIPSE                = 0x0418;
      public static final int META_FLOODFILL              = 0x0419;
      public static final int META_PIE                    = 0x081A;
      public static final int META_RECTANGLE              = 0x041B;
      public static final int META_ROUNDRECT              = 0x061C;
      public static final int META_PATBLT                 = 0x061D;
      public static final int META_SAVEDC                 = 0x001E;
      public static final int META_SETPIXEL               = 0x041F;
      public static final int META_OFFSETCLIPRGN          = 0x0220;
      public static final int META_TEXTOUT                = 0x0521;
      public static final int META_BITBLT                 = 0x0922;
      public static final int META_STRETCHBLT             = 0x0B23;
      public static final int META_POLYGON                = 0x0324;
      public static final int META_POLYLINE               = 0x0325;
      public static final int META_ESCAPE                 = 0x0626;
      public static final int META_RESTOREDC              = 0x0127;
      public static final int META_FILLREGION             = 0x0228;
      public static final int META_FRAMEREGION            = 0x0429;
      public static final int META_INVERTREGION           = 0x012A;
      public static final int META_PAINTREGION            = 0x012B;
      public static final int META_SELECTCLIPREGION       = 0x012C;
      public static final int META_SELECTOBJECT           = 0x012D;
      public static final int META_SETTEXTALIGN           = 0x012E;
      public static final int META_CHORD                  = 0x0830;
      public static final int META_SETMAPPERFLAGS         = 0x0231;
      public static final int META_EXTTEXTOUT             = 0x0a32;
      public static final int META_SETDIBTODEV            = 0x0d33;
      public static final int META_SELECTPALETTE          = 0x0234;
      public static final int META_REALIZEPALETTE         = 0x0035;
      public static final int META_ANIMATEPALETTE         = 0x0436;
      public static final int META_SETPALENTRIES          = 0x0037;
      public static final int META_POLYPOLYGON            = 0x0538;
      public static final int META_RESIZEPALETTE          = 0x0139;
      public static final int META_DIBBITBLT              = 0x0940;
      public static final int META_DIBSTRETCHBLT          = 0x0b41;
      public static final int META_DIBCREATEPATTERNBRUSH  = 0x0142;
      public static final int META_STRETCHDIB             = 0x0f43;
      public static final int META_EXTFLOODFILL           = 0x0548;
      public static final int META_SETLAYOUT              = 0x0149;
      public static final int META_DELETEOBJECT           = 0x01f0;
      public static final int META_CREATEPALETTE          = 0x00f7;
      public static final int META_CREATEPATTERNBRUSH     = 0x01F9;
      public static final int META_CREATEPENINDIRECT      = 0x02FA;
      public static final int META_CREATEFONTINDIRECT     = 0x02FB;
      public static final int META_CREATEBRUSHINDIRECT    = 0x02FC;
      public static final int META_CREATEREGION           = 0x06FF;
      public static final int META_POLYBEZIER16           = 0x1000;
      public static final int META_CREATEBRUSH		  = 0x00F8;
      public static final int META_CREATEBITMAPINDIRECT	  = 0x02FD;
      public static final int META_CREATEBITMAP		  = 0x06FE;
  
      public static final int META_OBJ_WHITE_BRUSH        = 0;
      public static final int META_OBJ_LTGRAY_BRUSH       = 1;
      public static final int META_OBJ_GRAY_BRUSH         = 2;
      public static final int META_OBJ_DKGRAY_BRUSH       = 3;
      public static final int META_OBJ_BLACK_BRUSH        = 4;
      public static final int META_OBJ_NULL_BRUSH         = 5;
      public static final int META_OBJ_HOLLOW_BRUSH       = 5;
      public static final int META_OBJ_WHITE_PEN          = 6;
      public static final int META_OBJ_BLACK_PEN          = 7;
      public static final int META_OBJ_NULL_PEN           = 8;
      public static final int META_OBJ_OEM_FIXED_FONT     = 10;
      public static final int META_OBJ_ANSI_FIXED_FONT    = 11;
      public static final int META_OBJ_ANSI_VAR_FONT      = 12;
      public static final int META_OBJ_SYSTEM_FONT        = 13;
      public static final int META_OBJ_DEVICE_DEFAULT_FONT = 14;
      public static final int META_OBJ_DEFAULT_PALETTE    = 15;
      public static final int META_OBJ_SYSTEM_FIXED_FONT  = 16;
  }
  
  
  
  1.1                  xml-batik/sources/org/apache/batik/transcoder/wmf/tosvg/RecordStore.java
  
  Index: RecordStore.java
  ===================================================================
  /*****************************************************************************
   * Copyright (C) The Apache Software Foundation. All rights reserved.        *
   * ------------------------------------------------------------------------- *
   * This software is published under the terms of the Apache Software License *
   * version 1.1, a copy of which has been included with this distribution in  *
   * the LICENSE file.                                                         *
   *****************************************************************************/
  
  
  package org.apache.batik.transcoder.wmf.tosvg;
  
  import java.io.DataInputStream;
  import java.io.IOException;
  import java.net.URL;
  import java.util.Vector;
  
  import org.apache.batik.transcoder.wmf.WMFConstants;
  
  /**
   * An object that stores the vector graphics records.
   *
   * @author <a href="mailto:luano@asd.ie">Luan O'Carroll</a>
   * @version $Id: RecordStore.java,v 1.1 2001/03/16 22:54:59 vhardy Exp $
   */
  public class RecordStore {
  
      public RecordStore(){
          reset();
      }
  
      /**
       * Resets the internal storage and viewport coordinates.
       */
      public void reset(){
          numRecords = 0;
          vpX = 0;
          vpY = 0;
          vpW = 1000;
          vpH = 1000;
          numObjects = 0;
          records = new Vector( 20, 20 );
          objectVector = new Vector();
      }
  
      synchronized void setReading( boolean state ){
          bReading = state;
      }
  
      synchronized boolean isReading(){
          return bReading;
      }
  
      /**
       * Reads the Wmf file from the specified Stream.
       * A Wmf file can be produced using the GConvert utility found at
       * http://www.asd.ie/Wmf.htm
       *
       * The Wmf format is slightly more compact than the original WMF format and
       * in some cases may produce better handling of colours.
       */
      public boolean read( DataInputStream is ) throws IOException{
          setReading( true );
          reset();
  
          int functionId = 0;
          numRecords = 0;
  
          numObjects = is.readShort();
          objectVector.ensureCapacity( numObjects );
          for ( int i = 0; i < numObjects; i++ ) {
              objectVector.addElement( new GdiObject( i, false ));
          }
  
          while ( functionId != -1 ) {
              functionId = is.readShort();
              if ( functionId == -1 ){
                  break;
              }
  
              MetaRecord mr;
              switch ( functionId ) {
              case WMFConstants.META_TEXTOUT:
              case WMFConstants.META_DRAWTEXT:
              case WMFConstants.META_EXTTEXTOUT:
              case WMFConstants.META_CREATEFONTINDIRECT:{
                  short len = is.readShort();
                  byte[] b = new byte[ len ];
                  for ( int i = 0; i < len; i++ ) {
                      b[ i ] = is.readByte();
                  }
                  String str = new String( b );
                  mr = new StringRecord( str );
              }
              break;
  
              default:
                  mr = new MetaRecord();
                  break;
              }
  
              int numPts = is.readShort();
              mr.numPoints = numPts;
              mr.functionId = functionId;
  
              for ( int j = 0; j < numPts; j++ ){
                  mr.AddElement( new Integer( is.readShort()));
              }
  
              records.addElement( mr );
  
              numRecords++;
          }
  
          setReading( false );
          return true;
      }
  
      /**
       * Adds a GdiObject to the internal handle table.
       * Adds the object at the next free location.
       *
       * This function should not normally be called by an application.
       */
      public void addObject( int type, Object obj )
      {
          for ( int i = 0; i < numObjects; i++ ) {
              GdiObject gdi = (GdiObject)objectVector.elementAt( i );
              if ( gdi.used == false ) {
                  gdi.Setup( type, obj );
                  lastObjectIdx = i;
                  break;
              }
          }
      }
  
      /**
       * Adds a GdiObject to the internal handle table.
       * Wmf files specify the index as given in EMF records such as
       * EMRCREATEPENINDIRECT whereas WMF files always use 0.
       *
       * This function should not normally be called by an application.
       */
      public void addObjectAt( int type, Object obj, int idx ) {
          if (( idx == 0 ) || ( idx > numObjects )) {
              addObject( type, obj );
              return;
          }
          lastObjectIdx = idx;
          for ( int i = 0; i < numObjects; i++ ) {
              GdiObject gdi = (GdiObject)objectVector.elementAt( i );
              if ( i == idx ) {
                  gdi.Setup( type, obj );
                  break;
              }
          }
      }
  
      /**
       * Returns the current URL
       */
      public URL getUrl() {
          return url;
      }
  
      /**
       * Sets the current URL
       */
      public void setUrl( URL newUrl) {
          url = newUrl;
      }
  
      /**
       * Returns a GdiObject from the handle table
       */
      public GdiObject getObject( int idx ) {
          return (GdiObject)objectVector.elementAt( idx );
      }
  
      /**
       * Returns a meta record.
       */
      public MetaRecord getRecord( int idx ) {
          return (MetaRecord)records.elementAt( idx );
      }
  
      /**
       * Returns a number of records in the image
       */
      public int getNumRecords() {
          return numRecords;
      }
  
      /**
       * Returns the number of GdiObjects in the handle table
       */
      public int getNumObjects() {
          return numObjects;
      }
  
      /**
       * Returns the viewport x origin
       */
      public int getVpX() {
          return vpX;
      }
  
      /**
       * Returns the viewport y origin
       */
      public int getVpY() {
          return vpY;
      }
  
      /**
       * Returns the viewport width
       */
      public int getVpW() {
          return vpW;
      }
  
      /**
       * Returns the viewport height
       */
      public int getVpH() {
          return vpH;
      }
  
      /**
       * Sets the viewport x origin
       */
      public void setVpX( int newValue ) {
          vpX = newValue;
      }
  
      /**
       * Sets the viewport y origin
       */
      public void setVpY( int newValue ) {
          vpY = newValue;
      }
  
      /**
       * Sets the viewport width
       */
      public void setVpW( int newValue ) {
          vpW = newValue;
      }
  
      /**
       * Sets the viewport height
       */
      public void setVpH( int newValue ) {
          vpH = newValue;
      }
  
      transient private URL url;
  
      transient protected int numRecords;
      transient protected int numObjects;
      transient public int lastObjectIdx;
      transient protected int vpX, vpY, vpW, vpH;
      transient protected Vector	records;
      transient protected Vector	objectVector;
  
      transient protected boolean bReading = false;
  }
  
  
  
  1.1                  xml-batik/sources/org/apache/batik/transcoder/wmf/tosvg/WMFPainter.java
  
  Index: WMFPainter.java
  ===================================================================
  /*****************************************************************************
   * Copyright (C) The Apache Software Foundation. All rights reserved.        *
   * ------------------------------------------------------------------------- *
   * This software is published under the terms of the Apache Software License *
   * version 1.1, a copy of which has been included with this distribution in  *
   * the LICENSE file.                                                         *
   *****************************************************************************/
  package org.apache.batik.transcoder.wmf.tosvg;
  
  import java.awt.BasicStroke;
  import java.awt.Color;
  import java.awt.Dimension;
  import java.awt.Font;
  import java.awt.Graphics;
  import java.awt.Graphics2D;
  import java.awt.Shape;
  import java.awt.geom.GeneralPath;
  import java.awt.geom.AffineTransform;
  import java.awt.geom.Point2D;
  import java.awt.font.FontRenderContext;
  import java.awt.font.TextLayout;
  import java.io.BufferedInputStream;
  import java.io.DataInputStream;
  import java.io.ObjectInputStream;
  import java.io.ObjectOutputStream;
  import java.io.Serializable;
  import java.io.IOException;
  import java.net.URL;
  import java.net.MalformedURLException;
  import java.util.Stack;
  import java.util.Vector;
  
  import org.apache.batik.transcoder.wmf.WMFConstants;
  
  /**
    * Core class for rendering the WMF image. It is able to render a
    * WMF file in a <tt>Graphics</tt> object.
    *
    *
    * @version $Id: WMFPainter.java,v 1.1 2001/03/16 22:54:59 vhardy Exp $
    * @author <a href="mailto:luano@asd.ie">Luan O'Carroll</a>
    */
  public class WMFPainter {
      private static final String WMF_FILE_EXTENSION = ".wmf";
  
      /**
       * Size of the buffer used for reading input WMF files
       */
      private static final int INPUT_BUFFER_SIZE = 30720;
  
      private static BasicStroke solid
          = new BasicStroke( 1.0f,
                             BasicStroke.CAP_BUTT,
                             BasicStroke.JOIN_ROUND );
  
      /**
       * Basic constructor, initializes the storage.
       */
      public WMFPainter(RecordStore currentStore) {
          setRecordStore(currentStore);
      }
  
      /**
       * Renders the WMF image(s).
       */
      public void paint( Graphics g ) {
          // Objects on DC stack;
          int           fontHeight = 10;
          int           fontAngle = 0;
          int           penWidth = 0;
          int           startX = 0;
          int           startY = 0;
          int           brushObject = -1;
          int           penObject = -1;
          int           fontObject = -1;
          Color         frgdColor;
          Color         bkgdColor;
          Font          font = null;
          int           lastObjectIdx;
          int           vpX, vpY, vpW, vpH;
          Stack         dcStack = new Stack();
  
          int numRecords = currentStore.getNumRecords();
          int numObjects = currentStore.getNumObjects();
          vpX = currentStore.getVpX();
          vpY = currentStore.getVpY();
          vpW = currentStore.getVpW();
          vpH = currentStore.getVpH();
  
          if ( !currentStore.isReading()) {
              GdiObject gdiObj;
              int gdiIndex;
              g.setPaintMode();
  
              brushObject = -1;
              penObject = -1;
              fontObject = -1;
              frgdColor = null;
              bkgdColor = null;
              for ( int i = 0; i < numObjects; i++ ) {
                  gdiObj = currentStore.getObject( i );
                  gdiObj.Clear();
              }
  
              int w = vpW;
              int h = vpH;
  
              g.setColor( Color.white );
              g.fillRect( 0, 0, w, h );
              g.setColor( Color.black );
  
              double scaleX = (double)w / vpW;
              double scaleY = (double)h / vpH;
  
              for ( int iRec = 0; iRec < numRecords; iRec++ ) {
                  MetaRecord mr = currentStore.getRecord( iRec );
  
                  switch ( mr.functionId ) {
                  case WMFConstants.META_SETWINDOWORG:
                      currentStore.setVpX( vpX = -mr.ElementAt( 0 ).intValue());
                      currentStore.setVpY( vpY = -mr.ElementAt( 1 ).intValue());
                      break;
  
                  case WMFConstants.META_SETWINDOWORG_EX: // ???? LOOKS SUSPICIOUS
                  case WMFConstants.META_SETWINDOWEXT:
                      currentStore.setVpW( vpW = mr.ElementAt( 0 ).intValue());
                      currentStore.setVpH( vpH = mr.ElementAt( 1 ).intValue());
                      scaleX = (double)w / vpW;
                      scaleY = (double)h / vpH;
  
                      // Handled in the read function.
                      break;
  
                  case WMFConstants.META_SETVIEWPORTORG:
                  case WMFConstants.META_SETVIEWPORTEXT:
                  case WMFConstants.META_OFFSETWINDOWORG:
                  case WMFConstants.META_SCALEWINDOWEXT:
                  case WMFConstants.META_OFFSETVIEWPORTORG:
                  case WMFConstants.META_SCALEVIEWPORTEXT:
                      break;
  
  
                  case WMFConstants.META_SETPOLYFILLMODE:
                      break;
  
                  case WMFConstants.META_CREATEPENINDIRECT:
                      {
                          int objIndex = 0;
                          try {
                              objIndex = (int)mr.ElementAt( 5 ).intValue();
                          }
                          catch ( Exception e ) {}
                          int penStyle = (int)mr.ElementAt( 0 ).intValue();
                          Color newClr;
                          if ( penStyle == 5 ) {
                              newClr = new Color( 255, 255, 255 );
                              objIndex = numObjects + 8;
                              addObjectAt( currentStore, NULL_PEN, newClr, objIndex );
                          }
                          else {
                              newClr = new Color( (int)mr.ElementAt( 1 ).intValue(),
                                                  (int)mr.ElementAt( 2 ).intValue(),
                                                  (int)mr.ElementAt( 3 ).intValue());
                              addObjectAt( currentStore, PEN, newClr, objIndex );
                          }
                          penWidth = mr.ElementAt( 4 ).intValue();
                      }
                      break;
  
                  case WMFConstants.META_CREATEBRUSHINDIRECT:
                      {
                          int objIndex = 0;
                          try {
                              objIndex = (int)mr.ElementAt( 5 ).intValue();
                          }
                          catch ( Exception e ) {}
                          int brushStyle = (int)mr.ElementAt( 0 ).intValue();
                          if ( brushStyle == 0 ) {
                              addObjectAt( currentStore, BRUSH, new Color( (int)mr.ElementAt( 1 ).intValue(),
                                                                           (int)mr.ElementAt( 2 ).intValue(),
                                                                           (int)mr.ElementAt( 3 ).intValue()),
                                           objIndex );
                          }
                          else
                              addObjectAt( currentStore, NULL_BRUSH, new Color( 0,0,0 ), objIndex );
                      }
                      break;
  
                  case WMFConstants.META_CREATEFONTINDIRECT:
                      {
                          int style =( (int)mr.ElementAt( 1 ).intValue() > 0 ? Font.ITALIC : Font.PLAIN );
                          style |=   ( (int)mr.ElementAt( 2 ).intValue() > 400 ? Font.BOLD : Font.PLAIN );
  
                          int size = (int)( scaleY * ( mr.ElementAt( 0 ).intValue()));
                          String face = ((StringRecord)mr).text;
                          if ( size < 0 )
                              size = (int)((double)size * -1.3 );
                          int objIndex = 0;
                          try {
                              objIndex = (int)mr.ElementAt( 3 ).intValue();
                          }
                          catch ( Exception e ) {}
                          fontHeight = size;
                          //fontAngle = mr.ElementAt( 5 ).intValue();
                          //if ( fontAngle > 0 )
                          //  size = ( size *12 ) / 10;
                          addObjectAt( currentStore, FONT, font = new Font( face, style, size ), objIndex );
                      }
                      break;
  
                  case WMFConstants.META_DIBCREATEPATTERNBRUSH:
                  case WMFConstants.META_CREATEBRUSH:
                  case WMFConstants.META_CREATEPATTERNBRUSH:
                  case WMFConstants.META_CREATEBITMAPINDIRECT:
                  case WMFConstants.META_CREATEBITMAP:
                  case WMFConstants.META_CREATEREGION:
                  case WMFConstants.META_CREATEPALETTE:
                      addObjectAt( currentStore, PALETTE, new Integer( 0 ), 0 );
                      break;
  
                  case WMFConstants.META_SELECTPALETTE:
                  case WMFConstants.META_REALIZEPALETTE:
                  case WMFConstants.META_ANIMATEPALETTE:
                  case WMFConstants.META_SETPALENTRIES:
                  case WMFConstants.META_RESIZEPALETTE:
                      break;
  
                  case WMFConstants.META_SELECTOBJECT:
                      gdiIndex = mr.ElementAt( 0 ).intValue();
                      if (( gdiIndex & 0x80000000 ) != 0 ) // Stock Object
                          break;
                      if ( gdiIndex >= numObjects ) {
                          gdiIndex -= numObjects;
                          switch ( gdiIndex ) {
                          case WMFConstants.META_OBJ_NULL_BRUSH:
                              brushObject = -1;
                              break;
                          case WMFConstants.META_OBJ_NULL_PEN:
                              penObject = -1;
                              break;
                          case WMFConstants.META_OBJ_WHITE_BRUSH:
                          case WMFConstants.META_OBJ_LTGRAY_BRUSH:
                          case WMFConstants.META_OBJ_GRAY_BRUSH:
                          case WMFConstants.META_OBJ_DKGRAY_BRUSH:
                          case WMFConstants.META_OBJ_BLACK_BRUSH:
                          case WMFConstants.META_OBJ_WHITE_PEN:
                          case WMFConstants.META_OBJ_BLACK_PEN:
                          case WMFConstants.META_OBJ_OEM_FIXED_FONT:
                          case WMFConstants.META_OBJ_ANSI_FIXED_FONT:
                          case WMFConstants.META_OBJ_ANSI_VAR_FONT:
                          case WMFConstants.META_OBJ_SYSTEM_FONT:
                          case WMFConstants.META_OBJ_DEVICE_DEFAULT_FONT:
                          case WMFConstants.META_OBJ_DEFAULT_PALETTE:
                          case WMFConstants.META_OBJ_SYSTEM_FIXED_FONT:
                              break;
                          }
                          break;
                      }
                      gdiObj = currentStore.getObject( gdiIndex );
                      if ( !gdiObj.used )
                          break;
                      switch( gdiObj.type ) {
                      case PEN:
                          g.setColor( (Color)gdiObj.obj );
                          penObject = gdiIndex;
                          break;
                      case BRUSH:
                          g.setColor( (Color)gdiObj.obj );
                          brushObject = gdiIndex;
                          break;
                      case FONT:
                          g.setFont( font = (Font)gdiObj.obj );
                          fontObject = gdiIndex;
                          break;
                      case NULL_PEN:
                          penObject = -1;
                          break;
                      case NULL_BRUSH:
                          brushObject = -1;
                          break;
                      }
                      break;
  
                  case WMFConstants.META_DELETEOBJECT:
                      gdiIndex = mr.ElementAt( 0 ).intValue();
                      gdiObj = currentStore.getObject( gdiIndex );
                      if ( gdiIndex == brushObject )
                          brushObject = -1;
                      else if ( gdiIndex == penObject )
                          penObject = -1;
                      else if ( gdiIndex == fontObject )
                          fontObject = -1;
                      gdiObj.Clear();
                      break;
  
                  case WMFConstants.META_POLYPOLYGON:
                      {
                          int numPolygons = mr.ElementAt( 0 ).intValue();
                          int[] pts = new int[ numPolygons ];
                          for ( int ip = 0; ip < numPolygons; ip++ )
                              pts[ ip ] = mr.ElementAt( ip + 1 ).intValue();
  
                          int offset = numPolygons+1;
                          for ( int j = 0; j < numPolygons; j++ ) {
                              int count = pts[ j ];
                              int[] xpts = new int[ count ];
                              int[] ypts = new int[ count ];
                              for ( int k = 0; k < count; k++ ) {
                                  xpts[k] = (int)(  scaleX * ( vpX + mr.ElementAt( offset + k*2 ).intValue()));
                                  ypts[k] = (int)( scaleY * ( vpY + mr.ElementAt( offset + k*2+1 ).intValue()));
                              }
                              offset += count;
                              if ( brushObject >= 0 ) {
                                  setBrushColor( currentStore, g, brushObject );
                                  g.fillPolygon( xpts, ypts, count );
                              }
                              setPenColor( currentStore, g, penObject );
                              g.drawPolygon( xpts, ypts, count );
                          }
                      }
                      break;
  
                  case WMFConstants.META_POLYGON:
                      {
                          int count = mr.ElementAt( 0 ).intValue();
                          int[] _xpts = new int[ count+1 ];
                          int[] _ypts = new int[ count+1 ];
                          for ( int k = 0; k < count; k++ ) {
                              _xpts[k] = (int)( scaleX * ( vpX + mr.ElementAt( k*2+1 ).intValue()));
                              _ypts[k] = (int)( scaleY * ( vpY + mr.ElementAt( k*2+2 ).intValue()));
                          }
                          _xpts[count] = _xpts[0];
                          _ypts[count] = _ypts[0];
                          if ( brushObject >= 0 ) {
                              setBrushColor( currentStore, g, brushObject );
                              g.fillPolygon( _xpts, _ypts, count );
                          }
                          setPenColor( currentStore, g, penObject );
                          g.drawPolygon( _xpts, _ypts, count+1 );
                      }
                      break;
  
                  case WMFConstants.META_MOVETO:
                      startX = (int)( scaleX * ( vpX + mr.ElementAt( 0 ).intValue()));
                      startY = (int)( scaleY * ( vpY + mr.ElementAt( 1 ).intValue()));
                      break;
  
                  case WMFConstants.META_LINETO:
                      {
                          int endX = (int)( scaleX * ( vpX + mr.ElementAt( 0 ).intValue()));
                          int endY = (int)( scaleY * ( vpY + mr.ElementAt( 1 ).intValue()));
                          setPenColor( currentStore, g, penObject );
                          g.drawLine( startX, startY, endX, endY );
                          startX = endX;
                          startY = endY;
                      }
                      break;
  
                  case WMFConstants.META_POLYLINE:
                      {
                          setPenColor( currentStore, g, penObject );
                          int count = mr.ElementAt( 0 ).intValue();
                          int endX, endY;
                          int _startX, _startY;
                          _startX = (int)( scaleX * ( vpX + mr.ElementAt( 1 ).intValue()));
                          _startY = (int)( scaleY * ( vpY + mr.ElementAt( 2 ).intValue()));
                          for ( int j = 1; j < count; j++ ) {
                              endX = (int)( scaleX * ( vpX + mr.ElementAt( j*2+1 ).intValue()));
                              endY = (int)( scaleY * ( vpY + mr.ElementAt( j*2+2 ).intValue()));
                              g.drawLine( _startX, _startY, endX, endY );
                              _startX = endX;
                              _startY = endY;
                          }
                      }
                      break;
  
                  case WMFConstants.META_RECTANGLE:
                      {
                          int x1, y1, x2, y2;
                          x1 = (int)( scaleX * ( vpX + mr.ElementAt( 0 ).intValue()));
                          x2 = (int)( scaleX * ( vpX + mr.ElementAt( 2 ).intValue()));
                          y1 = (int)( scaleY * ( vpY + mr.ElementAt( 1 ).intValue()));
                          y2 = (int)( scaleY * ( vpY + mr.ElementAt( 3 ).intValue()));
  
                          if ( brushObject >= 0 ) {
                              setBrushColor( currentStore, g, brushObject );
                              g.fillRect( x1, y1, x2-x1-1, y2-y1-1 );
                          }
                          setPenColor( currentStore, g, penObject );
                          g.drawRect( x1, y1, x2-x1-1, y2-y1-1 );
                      }
                      break;
  
                  case WMFConstants.META_ROUNDRECT:
                      {
                          int x1, y1, x2, y2, x3, y3;
                          x1 = (int)( scaleX * ( vpX + mr.ElementAt( 0 ).intValue()));
                          x2 = (int)( scaleX * ( vpX + mr.ElementAt( 2 ).intValue()));
                          x3 = (int)( scaleX * ( mr.ElementAt( 4 ).intValue()));
                          y1 = (int)( scaleY * ( vpY + mr.ElementAt( 1 ).intValue()));
                          y2 = (int)( scaleY * ( vpY + mr.ElementAt( 3 ).intValue()));
                          y3 = (int)( scaleY * ( mr.ElementAt( 5 ).intValue()));
  
                          if ( brushObject >= 0 ) {
                              setBrushColor( currentStore, g, brushObject );
                              g.fillRoundRect( x1, y1, x2-x1, y2-y1, x3, y3 );
                          }
                          setPenColor( currentStore, g, penObject );
                          g.drawRoundRect( x1, y1, x2-x1, y2-y1, x3, y3 );
                      }
                      break;
  
                  case WMFConstants.META_ELLIPSE:
                      {
                          int x1, y1, x2, y2;
                          x1 = (int)( scaleX * ( vpX + mr.ElementAt( 0 ).intValue()));
                          x2 = (int)( scaleX * ( vpX + mr.ElementAt( 2 ).intValue()));
                          y1 = (int)( scaleY * ( vpY + mr.ElementAt( 1 ).intValue()));
                          y2 = (int)( scaleY * ( vpY + mr.ElementAt( 3 ).intValue()));
  
                          if ( brushObject >= 0 ) {
                              setBrushColor( currentStore, g, brushObject );
                              g.fillOval( x1, y1, x2-x1, y2-y1 );
                          }
                          setPenColor( currentStore, g, penObject );
                          g.drawOval( x1, y1, x2-x1-1, y2-y1-1 );
                      }
                      break;
  
                  case WMFConstants.META_SETTEXTCOLOR:
                      frgdColor = new Color( (int)mr.ElementAt( 0 ).intValue(),
                                             (int)mr.ElementAt( 1 ).intValue(),
                                             (int)mr.ElementAt( 2 ).intValue());
                      break;
  
                  case WMFConstants.META_SETBKCOLOR:
                      bkgdColor = new Color( (int)mr.ElementAt( 0 ).intValue(),
                                             (int)mr.ElementAt( 1 ).intValue(),
                                             (int)mr.ElementAt( 2 ).intValue());
                      break;
  
                  case WMFConstants.META_EXTTEXTOUT:
                  case WMFConstants.META_TEXTOUT:
                  case WMFConstants.META_DRAWTEXT:
                      try
                          {
                              Graphics2D g2 = (Graphics2D)g;
                              int x, y;
                              x = (int)( scaleX * ( vpX + mr.ElementAt( 0 ).intValue()));
                              y = (int)( fontHeight + scaleY * ( vpY + mr.ElementAt( 1 ).intValue()));
                              if ( frgdColor != null )
                                  g.setColor( frgdColor );
                              else
                                  g.setColor( Color.black );
                              StringRecord sr = (StringRecord)mr;
  
                              FontRenderContext frc = g2.getFontRenderContext();
  
                              Point2D.Double pen = new Point2D.Double( 0, 0 );
                              GeneralPath gp = new GeneralPath( GeneralPath.WIND_NON_ZERO );
                              TextLayout layout = new TextLayout( sr.text, font, frc );
                              pen.y += layout.getAscent();
  
                              if (( fontAngle != 0 ) || sr.text.startsWith("Sono una scala verticale di prevalenza") ) {
                                  AffineTransform at = new AffineTransform();
                                  float width = (float)layout.getBounds().getWidth();
                                  float height = (float)layout.getBounds().getHeight();
  
                                  AffineTransform textAt = new AffineTransform();
                                  textAt.translate( x, y);
                                  textAt.rotate(Math.toRadians(270));
                                  textAt.translate(0, height);
                                  Shape shape = layout.getOutline(textAt);
                                  gp.append( at.createTransformedShape( shape )/*layout.getOutline( null ))*/, false );
                                  g2.draw( shape );
                              }
                              else
                                  g.drawString( sr.text, x, y );
                          }
                      catch ( Exception e )
                          {
                          }
                      break;
  
                  case WMFConstants.META_ARC:
                  case WMFConstants.META_PIE:
                      {
                          int x1, y1, x2, y2, x3, y3, x4, y4;
                          x1 = (int)( scaleX * ( vpX + mr.ElementAt( 0 ).intValue()));
                          x2 = (int)( scaleX * ( vpX + mr.ElementAt( 2 ).intValue()));
                          x3 = (int)( scaleX * ( mr.ElementAt( 4 ).intValue()));
                          x4 = (int)( scaleX * ( mr.ElementAt( 6 ).intValue()));
                          y1 = (int)( scaleY * ( vpY + mr.ElementAt( 1 ).intValue()));
                          y2 = (int)( scaleY * ( vpY + mr.ElementAt( 3 ).intValue()));
                          y3 = (int)( scaleY * ( mr.ElementAt( 5 ).intValue()));
                          y4 = (int)( scaleY * ( mr.ElementAt( 7 ).intValue()));
                          setBrushColor( currentStore, g, brushObject );
  
                          int mx = x1+(x2-x1)/2;
                          int my = y1+(y2-y1)/2;
                          int startAngle = (int)Math.atan( (y3-my)/(x3-mx));
                          int endAngle = (int)Math.atan( (y4-my)/(x4-mx));
                          if ( mr.functionId == 0x0817 )
                              g.drawArc( x1, y1, x2-x1, y2-y1, startAngle, endAngle );
                          else
                              g.fillArc( x1, y1, x2-x1, y2-y1, startAngle, endAngle );
  
                      }
                      break;
  
  
                  case WMFConstants.META_CHORD:
                      break;
  
                  case WMFConstants.META_SAVEDC:
                      dcStack.push( new Integer( penWidth ));
                      dcStack.push( new Integer( startX ));
                      dcStack.push( new Integer( startY ));
                      dcStack.push( new Integer( brushObject ));
                      dcStack.push( new Integer( penObject ));
                      dcStack.push( new Integer( fontObject ));
                      dcStack.push( frgdColor );
                      dcStack.push( bkgdColor );
                      break;
  
                  case WMFConstants.META_RESTOREDC:
                      bkgdColor = (Color)dcStack.pop();
                      frgdColor = (Color)dcStack.pop();
                      fontObject = ((Integer)(dcStack.pop())).intValue();
                      penObject = ((Integer)(dcStack.pop())).intValue();
                      brushObject = ((Integer)(dcStack.pop())).intValue();
                      startY = ((Integer)(dcStack.pop())).intValue();
                      startX = ((Integer)(dcStack.pop())).intValue();
                      penWidth = ((Integer)(dcStack.pop())).intValue();
                      break;
  
                  case WMFConstants.META_POLYBEZIER16:
                      try
                          {
                              Graphics2D g2 = (Graphics2D)g;
                              setPenColor( currentStore, g, penObject );
  
                              int pointCount = mr.ElementAt( 0 ).intValue();
                              int bezierCount = ( pointCount-1 ) / 3;
                              float endX, endY;
                              float cp1X, cp1Y;
                              float cp2X, cp2Y;
                              float _startX, _startY;
                              _startX = (float)( scaleX * ( vpX + mr.ElementAt( 1 ).intValue()));
                              _startY = (float)( scaleY * ( vpY + mr.ElementAt( 2 ).intValue()));
  
                              GeneralPath gp = new GeneralPath( GeneralPath.WIND_NON_ZERO );
                              gp.moveTo( _startX, _startY );
  
                              for ( int j = 0; j < bezierCount; j++ ) {
                                  cp1X = (float)( scaleX * ( vpX + mr.ElementAt( j*6+3 ).intValue()));
                                  cp1Y = (float)( scaleY * ( vpY + mr.ElementAt( j*6+4 ).intValue()));
  
                                  cp2X = (float)( scaleX * ( vpX + mr.ElementAt( j*6+5 ).intValue()));
                                  cp2Y = (float)( scaleY * ( vpY + mr.ElementAt( j*6+6 ).intValue()));
  
                                  endX = (float)( scaleX * ( vpX + mr.ElementAt( j*6+7 ).intValue()));
                                  endY = (float)( scaleY * ( vpY + mr.ElementAt( j*6+8 ).intValue()));
  
                                  gp.curveTo( cp1X, cp1Y, cp2X, cp2Y, endX, endY );
                                  _startX = endX;
                                  _startY = endY;
                              }
                                  //gp.closePath();
                              g2.setStroke( solid );
                              g2.draw( gp );
                          }
                      catch ( Exception e ) {
                          System.out.println( "Unable to draw static text as a 2D graphics context is required" );
                      }
                      break;
  
                  case WMFConstants.META_EXCLUDECLIPRECT:
                  case WMFConstants.META_INTERSECTCLIPRECT:
  
                  case WMFConstants.META_OFFSETCLIPRGN:
                  case WMFConstants.META_SELECTCLIPREGION:
  
                  case WMFConstants.META_SETBKMODE:
                  case WMFConstants.META_SETMAPMODE:
                  case WMFConstants.META_SETROP2:
                  case WMFConstants.META_SETRELABS:
                  case WMFConstants.META_SETSTRETCHBLTMODE:
                  case WMFConstants.META_SETTEXTCHAREXTRA:
                  case WMFConstants.META_SETTEXTJUSTIFICATION:
                  case WMFConstants.META_FLOODFILL:
                  case WMFConstants.META_PATBLT:
                  case WMFConstants.META_SETPIXEL:
                  case WMFConstants.META_BITBLT:
                  case WMFConstants.META_STRETCHBLT:
                  case WMFConstants.META_ESCAPE:
                  case WMFConstants.META_FILLREGION:
                  case WMFConstants.META_FRAMEREGION:
                  case WMFConstants.META_INVERTREGION:
                  case WMFConstants.META_PAINTREGION:
                  case WMFConstants.META_SETTEXTALIGN:
                  case WMFConstants.META_SETMAPPERFLAGS:
                  case WMFConstants.META_SETDIBTODEV:
                  case WMFConstants.META_DIBBITBLT:
                  case WMFConstants.META_DIBSTRETCHBLT:
                  case WMFConstants.META_STRETCHDIB:
                  default:
                      {
                          //int count = sizeof( MetaFunctions ) / sizeof( EMFMETARECORDS );
                          //for ( int i = 0; i < count; i++ ) {
                          //  if ( MetaFunctions[ i ].value == lpMFR->rdFunction ) {
                          //  os << MetaFunctions[ i ].szFuncName;
                          //  break;
                          //  }
                          //}
                      }
                      //os << " ------Unknown Function------";
                      break;
                  }
              }
  
          }
      }
  
      private void setPenColor( RecordStore currentStore, Graphics g, int penObject) {
          if ( penObject >= 0 ) {
              GdiObject gdiObj = currentStore.getObject( penObject );
              g.setColor( (Color)gdiObj.obj );
              penObject = -1;
          }
      }
  
      private void setBrushColor( RecordStore currentStore, Graphics g, int brushObject) {
          if ( brushObject >= 0 ) {
              GdiObject gdiObj = currentStore.getObject( brushObject );
              g.setColor( (Color)gdiObj.obj );
              brushObject = -1;
          }
      }
  
      /**
       * Sets the RecordStore this WMFPainter should use to render
       */
      public void setRecordStore(RecordStore currentStore){
          if(currentStore == null){
              throw new IllegalArgumentException();
          }
  
          this.currentStore = currentStore;
      }
  
      /**
       * Returns the RecordStore this WMFPainter renders
       */
      public RecordStore getRecordStore(){
          return currentStore;
      }
  
      private void addObject( RecordStore currentStore, int type, Object obj ) {
          currentStore.addObject( type, obj );
      }
  
      private void addObjectAt( RecordStore currentStore, int type, Object obj, int idx ) {
          currentStore.addObjectAt( type, obj, idx );
      }
  
      public static final int PEN = 1;
      public static final int BRUSH = 2;
      public static final int FONT = 3;
      public static final int NULL_PEN = 4;
      public static final int NULL_BRUSH = 5;
      public static final int PALETTE = 6;
  
      private RecordStore currentStore;
      transient private boolean bReadingWMF = true;
      transient private BufferedInputStream bufStream = null;
  
  }
  
  
  class MetaRecord /*implements Serializable*/
  {
  	public int	functionId;
  	public int	numPoints;
  
  	private Vector	ptVector;
  
  	public MetaRecord()
  	{
  		ptVector = new Vector();
  	}
  
  	public void EnsureCapacity( int cc )
  	{
  		ptVector.ensureCapacity( cc );
  	}
  
  	public void AddElement( Object obj )
  	{
  		ptVector.addElement( obj );
  	}
  
  	public Integer ElementAt( int offset )
  	{
  		return (Integer)ptVector.elementAt( offset );
  	}
  }
  
  class StringRecord extends MetaRecord /*implements Serializable*/
  {
  	public String	text;
  
  	public StringRecord( String newText )
  	{
  		text = new String( newText );
  	}
  }
  
  class GdiObject /*implements Serializable*/
  {
  	GdiObject( int _id, boolean _used )
  	{
          id = _id;
          used = _used;
          type = 0;
  	}
  
  	public void Clear()
  	{
          used = false;
          type = 0;
  	}
  
  	public void Setup( int _type, Object _obj )
  	{
          obj = _obj;
          type = _type;
          used = true;
  	}
  
  	int id;
  	boolean used;
  	Object obj;
  	int type = 0;
  }
  
  
  
  
  
  1.1                  xml-batik/sources/org/apache/batik/transcoder/wmf/tosvg/WMFRecordStore.java
  
  Index: WMFRecordStore.java
  ===================================================================
  /*****************************************************************************
   * Copyright (C) The Apache Software Foundation. All rights reserved.        *
   * ------------------------------------------------------------------------- *
   * This software is published under the terms of the Apache Software License *
   * version 1.1, a copy of which has been included with this distribution in  *
   * the LICENSE file.                                                         *
   *****************************************************************************/
  
  package org.apache.batik.transcoder.wmf.tosvg;
  
  import java.io.DataInputStream;
  import java.io.IOException;
  import java.net.URL;
  
  import org.apache.batik.transcoder.wmf.WMFConstants;
  
  /**
   * Reads a WMF file, including an Aldus Placable Metafile Header.
   *
   * @author <a href="mailto:luano@asd.ie">Luan O'Carroll</a>
   * @version $Id: WMFRecordStore.java,v 1.1 2001/03/16 22:54:59 vhardy Exp $
   */
  public class WMFRecordStore extends RecordStore implements WMFConstants{
  
      public WMFRecordStore(){
      }
  
      private short readShort( DataInputStream is  ) throws IOException{
          byte js[] = new byte[ 2 ];
          is.read( js );
          int iTemp = ((0xff) & js[ 1 ] ) << 8;
          short i = (short)(0xffff & iTemp);
          i |= ((0xff) & js[ 0 ] );
          return i;
      }
  
      private int readInt( DataInputStream is  ) throws IOException {
          byte js[] = new byte[ 4 ];
          is.read( js );
          int i = ((0xff) & js[ 3 ] ) << 24;
          i |= ((0xff) & js[ 2 ] ) << 16;
          i |= ((0xff) & js[ 1 ] ) << 8;
          i |= ((0xff) & js[ 0 ] );
          return i;
      }
  
      /**
       * Reads the WMF file from the specified Stream.
       */
      public boolean read( DataInputStream is ) throws IOException{
          reset();
  
          setReading( true );
          int dwIsAldus = readInt( is );
          if ( dwIsAldus == WMFConstants.META_ALDUS_APM ) {
              // Read the aldus placeable header.
              int   key = dwIsAldus;
              short hmf = readShort( is );
              short left = readShort( is );
              short top = readShort( is );
              short right = readShort( is );
              short  bottom = readShort( is );
              short inch = readShort( is );
              int   reserved = readInt( is );
              short checksum = readShort( is );
          }
          else {
              System.out.println( "Unable to read file, it is not a Aldus Placable Metafile" );
              setReading( false );
              return false;
          }
  
          int mtType = readShort( is );
          int mtHeaderSize = readShort( is );
          int mtVersion = readShort( is );
          int mtSize = readInt( is );
          int mtNoObjects = readShort( is );
          int mtMaxRecord = readInt( is );
          int mtNoParameters = readShort( is );
  
  
          short functionId = 1;
          int recSize = 0;
          short recData;
  
  
          numRecords = 0;
  
          numObjects = mtNoObjects;
          objectVector.ensureCapacity( numObjects );
          for ( int i = 0; i < numObjects; i++ ) {
              objectVector.addElement( new GdiObject( i, false ));
          }
  
          while ( functionId > 0 ) {
              recSize = readInt( is );
              // Subtract size in 16-bit words of recSize and functionId;
              recSize -= 3;
              functionId = readShort( is );
              if ( functionId <= 0 )
                  break;
  
              MetaRecord mr = new MetaRecord();
              switch ( functionId ) {
              case WMFConstants.META_DRAWTEXT:
                  {
                      for ( int i = 0; i < recSize; i++ )
                          recData = readShort( is );
                      numRecords--;
                  }
                  break;
  
              case WMFConstants.META_EXTTEXTOUT:
                  {
                      int yVal = readShort( is );
                      int xVal = readShort( is );
                      int lenText = readInt( is );
                      int len = 2*(recSize-4);
                      byte bstr[] = new byte[ lenText ];
                      //is.read( bstr );
                      int i = 0;
                      for ( ; i < lenText; i++ )
                          bstr[ i ] = is.readByte();
                      for ( ; i < len; i++ )
                          is.readByte();
  
                      String str = new String( bstr );
                      mr = new StringRecord( str );
                      mr.numPoints = recSize;
                      mr.functionId = functionId;
  
                      mr.AddElement( new Integer( xVal ));
                      mr.AddElement( new Integer( yVal ));
                      records.addElement( mr );
                  }
                  break;
  
              case WMFConstants.META_TEXTOUT:
                  {
                      int len = readShort( is );
                      byte bstr[] = new byte[ len ];
                      //is.read( bstr );
                      for ( int i = 0; i < len; i++ )
                          bstr[ i ] = is.readByte();
                      int yVal = readShort( is );
                      int xVal = readShort( is );
  
                      String str = new String( bstr );
                      mr = new StringRecord( str );
                      mr.numPoints = recSize;
                      mr.functionId = functionId;
  
                      mr.AddElement( new Integer( xVal ));
                      mr.AddElement( new Integer( yVal ));
                      records.addElement( mr );
                  }
                  break;
  
  
              case WMFConstants.META_CREATEFONTINDIRECT:
                  {
                      int lfHeight = readShort( is );
                      int lfWidth = readShort( is );
                      int lfEscapement = readShort( is );
                      int lfOrientation = readShort( is );
                      int lfWeight = readShort( is );
  
                      int lfItalic = is.readByte();
                      int lfUnderline = is.readByte();
                      int lfStrikeOut = is.readByte();
                      int lfCharSet = is.readByte();
                      int lfOutPrecision = is.readByte();
                      int lfClipPrecision = is.readByte();
                      int lfQuality = is.readByte();
                      int lfPitchAndFamily = is.readByte();
  
                      int len = (2*(recSize-9));//13));
                      byte lfFaceName[] = new byte[ len ];
                      byte ch;
                      for ( int i = 0; i < len; i++ )
                          lfFaceName[ i ] = is.readByte();
  
  
                      String str = new String( lfFaceName );
  
                      mr = new StringRecord( str );
                      mr.numPoints = recSize;
                      mr.functionId = functionId;
  
                      mr.AddElement( new Integer( lfHeight ));
                      mr.AddElement( new Integer( lfItalic ));
                      mr.AddElement( new Integer( lfWeight ));
                      records.addElement( mr );
                  }
                  break;
  
              case WMFConstants.META_SETWINDOWORG:
              case WMFConstants.META_SETWINDOWEXT:
                  {
                      mr.numPoints = recSize;
                      mr.functionId = functionId;
  
                      int i0 = readShort( is );
                      int i1 = readShort( is );
                      mr.AddElement( new Integer( i1 ));
                      mr.AddElement( new Integer( i0 ));
                      records.addElement( mr );
                  }
                  break;
  
              case WMFConstants.META_CREATEBRUSHINDIRECT:
                  {
                      mr.numPoints = recSize;
                      mr.functionId = functionId;
  
                      // The style
                      mr.AddElement( new Integer( readShort( is )));
  
                      int colorref =  readInt( is );
                      int red = colorref & 0xff;
                      int green = ( colorref & 0xff00 ) >> 8;
                      int blue = ( colorref & 0xff0000 ) >> 16;
                      int flags = ( colorref & 0x3000000 ) >> 24;
                      mr.AddElement( new Integer( red ));
                      mr.AddElement( new Integer( green ));
                      mr.AddElement( new Integer( blue ));
  
                      // The hatch style
                      mr.AddElement( new Integer( readShort( is )));
  
                      records.addElement( mr );
                  }
                  break;
  
              case WMFConstants.META_CREATEPENINDIRECT:
                  {
                      mr.numPoints = recSize;
                      mr.functionId = functionId;
  
                      // The style
                      Integer style = new Integer( readShort( is ));
                      mr.AddElement( style );
  
                      int width = readShort( is );
                      int colorref =  readInt( is );
                      int height = readShort( is );
  
                      int red = colorref & 0xff;
                      int green = ( colorref & 0xff00 ) >> 8;
                      int blue = ( colorref & 0xff0000 ) >> 16;
                      int flags = ( colorref & 0x3000000 ) >> 24;
                      mr.AddElement( new Integer( red ));
                      mr.AddElement( new Integer( green ));
                      mr.AddElement( new Integer( blue ));
  
                      // The pen width
                      mr.AddElement( new Integer( width ));
  
                      records.addElement( mr );
                  }
                  break;
  
              case WMFConstants.META_SETTEXTCOLOR:
              case WMFConstants.META_SETBKCOLOR:
                  {
                      mr.numPoints = recSize;
                      mr.functionId = functionId;
  
                      int colorref =  readInt( is );
                      int red = colorref & 0xff;
                      int green = ( colorref & 0xff00 ) >> 8;
                      int blue = ( colorref & 0xff0000 ) >> 16;
                      int flags = ( colorref & 0x3000000 ) >> 24;
                      mr.AddElement( new Integer( red ));
                      mr.AddElement( new Integer( green ));
                      mr.AddElement( new Integer( blue ));
                      records.addElement( mr );
                  }
                  break;
  
              case WMFConstants.META_LINETO:
              case WMFConstants.META_MOVETO:
                  {
                      mr.numPoints = recSize;
                      mr.functionId = functionId;
  
                      int i0 = readShort( is );
                      int i1 = readShort( is );
                      mr.AddElement( new Integer( i1 ));
                      mr.AddElement( new Integer( i0 ));
                      records.addElement( mr );
                  }
                  break;
  
              case WMFConstants.META_POLYPOLYGON:
                  {
                      mr.numPoints = recSize;
                      mr.functionId = functionId;
  
                      int count = readShort( is );
                      int pts[] = new int[ count ];
                      int ptCount = 0;
                      for ( int i = 0; i < count; i++ ) {
                          pts[ i ] = readShort( is );
                          ptCount += pts[ i ];
                      }
                      mr.AddElement( new Integer( count ));
  
                      for ( int i = 0; i < count; i++ )
                          mr.AddElement( new Integer( pts[ i ] ));
  
                      int offset = count+1;
                      for ( int i = 0; i < count; i++ ) {
                          for ( int j = 0; j < pts[ i ]; j++ ) {
                              mr.AddElement( new Integer( readShort( is )));
                              mr.AddElement( new Integer( readShort( is )));
                          }
                      }
                      records.addElement( mr );
                  }
                  break;
  
              case WMFConstants.META_POLYGON:
                  {
                      mr.numPoints = recSize;
                      mr.functionId = functionId;
  
                      int count = readShort( is );
                      mr.AddElement( new Integer( count ));
                      for ( int i = 0; i < count; i++ ) {
                          mr.AddElement( new Integer( readShort( is )));
                          mr.AddElement( new Integer( readShort( is )));
                      }
                      records.addElement( mr );
                  }
                  break;
  
              case WMFConstants.META_ELLIPSE:
              case WMFConstants.META_INTERSECTCLIPRECT:
              case WMFConstants.META_RECTANGLE:
                  {
                      mr.numPoints = recSize;
                      mr.functionId = functionId;
  
                      int i0 = readShort( is );
                      int i1 = readShort( is );
                      int i2 = readShort( is );
                      int i3 = readShort( is );
                      mr.AddElement( new Integer( i3 ));
                      mr.AddElement( new Integer( i2 ));
                      mr.AddElement( new Integer( i1 ));
                      mr.AddElement( new Integer( i0 ));
                      records.addElement( mr );
                  }
                  break;
  
              case WMFConstants.META_ROUNDRECT:
                  {
                      mr.numPoints = recSize;
                      mr.functionId = functionId;
  
                      int i0 = readShort( is );
                      int i1 = readShort( is );
                      int i2 = readShort( is );
                      int i3 = readShort( is );
                      int i4 = readShort( is );
                      int i5 = readShort( is );
                      mr.AddElement( new Integer( i5 ));
                      mr.AddElement( new Integer( i4 ));
                      mr.AddElement( new Integer( i3 ));
                      mr.AddElement( new Integer( i2 ));
                      mr.AddElement( new Integer( i1 ));
                      mr.AddElement( new Integer( i0 ));
                      records.addElement( mr );
                  }
                  break;
  
              case WMFConstants.META_ARC:
              case WMFConstants.META_PIE:
                  {
                      mr.numPoints = recSize;
                      mr.functionId = functionId;
  
                      int i0 = readShort( is );
                      int i1 = readShort( is );
                      int i2 = readShort( is );
                      int i3 = readShort( is );
                      int i4 = readShort( is );
                      int i5 = readShort( is );
                      int i6 = readShort( is );
                      int i7 = readShort( is );
                      mr.AddElement( new Integer( i7 ));
                      mr.AddElement( new Integer( i6 ));
                      mr.AddElement( new Integer( i5 ));
                      mr.AddElement( new Integer( i4 ));
                      mr.AddElement( new Integer( i3 ));
                      mr.AddElement( new Integer( i2 ));
                      mr.AddElement( new Integer( i1 ));
                      mr.AddElement( new Integer( i0 ));
                      records.addElement( mr );
                  }
                  break;
  
              default:
                  mr.numPoints = recSize;
                  mr.functionId = functionId;
  
                  for ( int j = 0; j < recSize; j++ )
                      mr.AddElement( new Integer( readShort( is )));
  
                  records.addElement( mr );
                  break;
  
              }
  
              numRecords++;
          }
  
          setReading( false );
          return true;
      }
  
      public void addObject( int type, Object obj ){
          int startIdx = 0;
          //     if ( type == Wmf.PEN ) {
          //       startIdx = 2;
          //     }
          for ( int i = startIdx; i < numObjects; i++ ) {
              GdiObject gdi = (GdiObject)objectVector.elementAt( i );
              if ( gdi.used == false ) {
                  gdi.Setup( type, obj );
                  lastObjectIdx = i;
                  break;
              }
          }
      }
  
  }
  
  
  
  1.1                  xml-batik/sources/org/apache/batik/transcoder/wmf/tosvg/WMFTranscoder.java
  
  Index: WMFTranscoder.java
  ===================================================================
  /*****************************************************************************
   * Copyright (C) The Apache Software Foundation. All rights reserved.        *
   * ------------------------------------------------------------------------- *
   * This software is published under the terms of the Apache Software License *
   * version 1.1, a copy of which has been included with this distribution in  *
   * the LICENSE file.                                                         *
   *****************************************************************************/
  package org.apache.batik.transcoder.wmf.tosvg;
  
  import java.awt.Dimension;
  import java.io.BufferedInputStream;
  import java.io.DataInputStream;
  import java.io.File;
  import java.io.FileOutputStream;
  import java.io.InputStream;
  import java.io.IOException;
  import java.io.OutputStream;
  import java.io.OutputStreamWriter;
  import java.io.Writer;
  import java.net.URL;
  import java.net.URLConnection;
  import java.net.MalformedURLException;
  
  import org.w3c.dom.Document;
  import org.w3c.dom.Element;
  import org.w3c.dom.DOMImplementation;
  
  import org.xml.sax.XMLFilter;
  
  import org.apache.batik.util.SVGConstants;
  
  import org.apache.batik.svggen.SVGGraphics2D;
  import org.apache.batik.dom.svg.SVGDOMImplementation;
  
  import org.apache.batik.transcoder.AbstractTranscoder;
  import org.apache.batik.transcoder.TranscoderInput;
  import org.apache.batik.transcoder.TranscoderOutput;
  import org.apache.batik.transcoder.TranscoderException;
  
  
  /**
   * This class implements the <tt>Transcoder</tt> interface and 
   * can convert a WMF input document into an SVG document.
   *
   * It can use <tt>TranscoderInput</tt> that are either a URI
   * or a <tt>InputStream</tt> or a <tt>Reader</tt>. The 
   * <tt>XMLReader</tt> and <tt>Document</tt> <tt>TranscoderInput</tt>
   * types are not supported.
   *
   * This transcoder can use <tt>TranscoderOutputs</tt> that are
   * of any type except the <tt>XMLFilter</tt> type.
   * 
   * @version $Id: WMFTranscoder.java,v 1.1 2001/03/16 22:54:59 vhardy Exp $
   * @author <a href="mailto:luano@asd.ie">Luan O'Carroll</a>
   */
  public class WMFTranscoder extends AbstractTranscoder 
      implements SVGConstants{
  
      /**
       * Error codes for the WMFTranscoder
       */
      public static final int WMF_TRANSCODER_ERROR_BASE = 0xff00;
      public static final int ERROR_NULL_INPUT = WMF_TRANSCODER_ERROR_BASE + 0;
      public static final int ERROR_INCOMPATIBLE_INPUT_TYPE = WMF_TRANSCODER_ERROR_BASE + 1;
      public static final int ERROR_INCOMPATIBLE_OUTPUT_TYPE = WMF_TRANSCODER_ERROR_BASE + 2;
  
      /**
       * Default constructor
       */
      public WMFTranscoder(){
      }
      
      /**
       * Transcodes the specified input in the specified output.
       * @param input the input to transcode
       * @param output the ouput where to transcode
       * @exception TranscoderException if an error occured while transcoding
       */
      public void transcode(TranscoderInput input, TranscoderOutput output)
          throws TranscoderException {
          //
          // Extract the input
          //
          DataInputStream is = getCompatibleInput(input);
  
          //
          // Build a RecordStore from the input
          //
          RecordStore currentStore = new WMFRecordStore();
          try{
              currentStore.read(is);
          }catch(IOException e){
              handler.fatalError(new TranscoderException(e));
              return;
          }
  
          //
          // Build a painter for the RecordStore
          //
          WMFPainter painter = new WMFPainter(currentStore);
  
          //
          // Use SVGGraphics2D to generate SVG content
          //
          DOMImplementation domImpl 
              = SVGDOMImplementation.getDOMImplementation();
  
          Document doc = domImpl.createDocument(SVG_NAMESPACE_URI, 
                                                SVG_SVG_TAG, null);
  
          SVGGraphics2D svgGenerator = new SVGGraphics2D(doc);
  
          painter.paint(svgGenerator);
  
          //
          // Set the size and viewBox on the output document
          //
          int vpX = currentStore.getVpX();
          int vpY = currentStore.getVpY();
          int vpW = currentStore.getVpW();
          int vpH = currentStore.getVpH();
          svgGenerator.setSVGCanvasSize(new Dimension(vpW, vpH));
  
          Element svgRoot = svgGenerator.getRoot();
          svgRoot.setAttributeNS(null, SVG_VIEW_BOX_ATTRIBUTE,
                                 "" + vpX + " " + vpY + " " +
                                 vpW + " " + vpH );
  
          //
          // Now, write the SVG content to the output
          //
          writeSVGToOutput(svgGenerator, svgRoot, output);
      }
  
      /**
       * Writes the SVG content held by the svgGenerator to the 
       * <tt>TranscoderOutput</tt>.
       */
      private void writeSVGToOutput(SVGGraphics2D svgGenerator,
                                    Element svgRoot,
                                    TranscoderOutput output) 
          throws TranscoderException {
          // XMLFilter
          XMLFilter xmlFilter = output.getXMLFilter();
          if(xmlFilter != null){
              handler.fatalError(new TranscoderException("" + ERROR_INCOMPATIBLE_OUTPUT_TYPE));
          }
  
          // <!> FIX ME: SHOULD HANDLE DOCUMENT INPUT
          Document doc = output.getDocument();
          if(doc != null){
              handler.fatalError(new TranscoderException("" + ERROR_INCOMPATIBLE_OUTPUT_TYPE));
          }
  
          try{
              // Output stream
              OutputStream os = output.getOutputStream();
              if( os != null ){
                  svgGenerator.stream(svgRoot, new OutputStreamWriter(os));
                  return;
              }
              
              // Writer
              Writer wr = output.getWriter();
              if( wr != null ){
                  svgGenerator.stream(svgRoot, wr);
                  return;
              }
              
              // URI
              String uri = output.getURI();
              if( uri != null ){
                  try{
                      URL url = new URL(uri);
                      URLConnection urlCnx = url.openConnection();
                      os = urlCnx.getOutputStream();
                      svgGenerator.stream(svgRoot, new OutputStreamWriter(os));
                      return;
                  }catch(MalformedURLException e){
                      handler.fatalError(new TranscoderException(e));
                  }catch(IOException e){
                      handler.fatalError(new TranscoderException(e));
                  }
              }
          }catch(IOException e){
              throw new TranscoderException(e);
          }
  
          throw new TranscoderException("" + ERROR_INCOMPATIBLE_OUTPUT_TYPE);        
          
      }
  
      /**
       * Checks that the input is one of URI or an <tt>InputStream</tt>
       * returns it as a DataInputStream
       */
      private DataInputStream getCompatibleInput(TranscoderInput input)
          throws TranscoderException {
          // Cannot deal with null input
          if(input == null){
              handler.fatalError(new TranscoderException("" + ERROR_NULL_INPUT));
          }
  
          // Can deal with InputStream
          InputStream in = input.getInputStream();
          if(in != null){
              return new DataInputStream(new BufferedInputStream(in));
          }
  
          // Can deal with URI
          String uri = input.getURI();
          if(uri != null){
              try{
                  URL url = new URL(uri);
                  in = url.openStream();
                  return new DataInputStream(new BufferedInputStream(in));
              }catch(MalformedURLException e){
                  handler.fatalError(new TranscoderException(e));
              }catch(IOException e){
                  handler.fatalError(new TranscoderException(e));
              }
          }
  
          handler.fatalError(new TranscoderException("" + ERROR_INCOMPATIBLE_INPUT_TYPE));
          return null;
      }
  
      public static final String USAGE = "The WMFTranscoder converts a WMF document into an SVG document. \n" +
          "This simple application generates SVG documents that have the same name, but a where the .wmf extension \n" +
          "is replaced with .svg. To run the application, type the following at the command line: \n" +
          "java org.apache.batik.transcoder.wmf.tosvg.WMFTranscoder fileName [fileName]+";
  
      public static final String WMF_EXTENSION = ".wmf";
      public static final String SVG_EXTENSION = ".svg";
  
      /**
       * Unit testing : Illustrates how the transcoder might be used.
       */
      public static void main(String args[]) throws TranscoderException {
          if(args.length < 1){
              System.err.println(USAGE);
              System.exit(1);
          }
  
          WMFTranscoder transcoder = new WMFTranscoder();
          int nFiles = args.length;
          
          for(int i=0; i<nFiles; i++){
              String fileName = args[i];
              if(!fileName.toLowerCase().endsWith(WMF_EXTENSION)){
                  System.err.println(args[i] + " does not have the " + WMF_EXTENSION + " extension. It is ignored");
              }
              else{
                  System.out.print("Processing : " + args[i] + "...");
                  String outputFileName = fileName.substring(0, fileName.toLowerCase().indexOf(WMF_EXTENSION)) + SVG_EXTENSION;
                  File inputFile = new File(fileName);
                  File outputFile = new File(outputFileName);
                  try{
                      TranscoderInput input = new TranscoderInput(inputFile.toURL().toString());
                      TranscoderOutput output = new TranscoderOutput(new FileOutputStream(outputFile));
                      transcoder.transcode(input, output);
                  }catch(MalformedURLException e){
                      throw new TranscoderException(e);
                  }catch(IOException e){
                      throw new TranscoderException(e);
                  }
                  System.out.println(".... Done");
              }
          }
  
          System.exit(0);
      }
  }
  
  
  

---------------------------------------------------------------------
To unsubscribe, e-mail: batik-dev-unsubscribe@xml.apache.org
For additional commands, e-mail: batik-dev-help@xml.apache.org