Index: sources/org/apache/batik/transcoder/ToSVGAbstractTranscoder.java =================================================================== --- sources/org/apache/batik/transcoder/ToSVGAbstractTranscoder.java (revision 833862) +++ sources/org/apache/batik/transcoder/ToSVGAbstractTranscoder.java (working copy) @@ -18,7 +18,6 @@ */ package org.apache.batik.transcoder; -import java.awt.Toolkit; import java.io.IOException; import java.io.OutputStream; import java.io.OutputStreamWriter; @@ -90,13 +89,6 @@ public abstract class ToSVGAbstractTranscoder extends AbstractTranscoder implements SVGConstants { - public static float PIXEL_TO_MILLIMETERS; - public static float PIXEL_PER_INCH; - static { - PIXEL_TO_MILLIMETERS = 25.4f / (float)Toolkit.getDefaultToolkit().getScreenResolution(); - PIXEL_PER_INCH = Toolkit.getDefaultToolkit().getScreenResolution(); - } - public static final int TRANSCODER_ERROR_BASE = 0xff00; public static final int ERROR_NULL_INPUT = TRANSCODER_ERROR_BASE + 0; public static final int ERROR_INCOMPATIBLE_INPUT_TYPE = TRANSCODER_ERROR_BASE + 1; @@ -131,6 +123,12 @@ */ public static final TranscodingHints.Key KEY_YOFFSET = new IntegerKey(); + + /* Keys definition : output resolution (in dots-per-inchs). Best to be equal to the + * screen resolution. + */ + public static final TranscodingHints.Key KEY_OUTPUT_RESOLUTION + = new FloatKey(); /* Keys definition : Define if the characters will be escaped in the output. */ Index: sources/org/apache/batik/transcoder/wmf/tosvg/AbstractWMFReader.java =================================================================== --- sources/org/apache/batik/transcoder/wmf/tosvg/AbstractWMFReader.java (revision 833862) +++ sources/org/apache/batik/transcoder/wmf/tosvg/AbstractWMFReader.java (working copy) @@ -19,6 +19,7 @@ package org.apache.batik.transcoder.wmf.tosvg; +import java.awt.GraphicsEnvironment; import java.awt.Rectangle; import java.awt.Toolkit; import java.awt.geom.Rectangle2D; @@ -36,8 +37,8 @@ public abstract class AbstractWMFReader { // todo should be able to run in headless environment - as is written, will throw exception during init - public static final float PIXEL_PER_INCH = Toolkit.getDefaultToolkit().getScreenResolution(); - public static final float MM_PER_PIXEL = 25.4f / Toolkit.getDefaultToolkit().getScreenResolution(); + public static float pixelPerInch = 96f; + public static float mmPerPixel = 25.4f / pixelPerInch; protected int left, right, top, bottom, width, height, inch; protected float scaleX, scaleY, scaleXY; protected int vpW, vpH, vpX, vpY; @@ -60,7 +61,7 @@ public AbstractWMFReader() { scaleX = 1; scaleY = 1; - scaleXY = 1f; + scaleXY = 1f; left = -1; top = -1; width = -1; @@ -69,14 +70,34 @@ bottom = top + height; numObjects = 0; objectVector = new ArrayList(); + initializeResolution(); } public AbstractWMFReader(int width, int height) { this(); this.width = width; this.height = height; + initializeResolution(); } + private void initializeResolution() { + if (GraphicsEnvironment.isHeadless()) { + pixelPerInch = 96f; + } else { + pixelPerInch = Toolkit.getDefaultToolkit().getScreenResolution(); + } + mmPerPixel = 25.4f / pixelPerInch; + } + + public void setPixelPerInch(float pixelPerInch) { + this.pixelPerInch = pixelPerInch; + mmPerPixel = 25.4f / pixelPerInch; + } + + public float getPixelPerInch() { + return pixelPerInch; + } + /** * Read the next short (2 bytes) value in the DataInputStream. */ @@ -120,34 +141,34 @@ * Returns the viewport width, in inches. */ public float getViewportWidthInch() { - return (float)vpW / (float)inch; + return pixelPerInch * (float)vpW / (float)inch; } /** * Returns the viewport height, in inches. */ public float getViewportHeightInch() { - return (float)vpH / (float)inch; + return pixelPerInch * (float)vpH / (float)inch; } /** Return the number of pixels per unit. */ public float getPixelsPerUnit() { - return PIXEL_PER_INCH / (float)inch; + return pixelPerInch / (float)inch; } /** * Returns the viewport width, in pixels. */ public int getVpW() { - return (int)(PIXEL_PER_INCH * (float)vpW / (float)inch); + return (int)(pixelPerInch * (float)vpW / (float)inch); } /** * Returns the viewport height, in pixels. */ public int getVpH() { - return (int)(PIXEL_PER_INCH * (float)vpH / (float)inch); + return (int)(pixelPerInch * (float)vpH / (float)inch); } /** get the left units in the WMF Metafile. This value is given @@ -215,10 +236,10 @@ /** get the Rectangle defining the viewport of the WMF Metafile, in pixels. */ public Rectangle2D getRectanglePixel() { - float _left = PIXEL_PER_INCH * (float)left / (float)inch; - float _right = PIXEL_PER_INCH * (float)right / (float)inch; - float _top = PIXEL_PER_INCH * (float)top / (float)inch; - float _bottom = PIXEL_PER_INCH * (float)bottom / (float)inch; + float _left = pixelPerInch * (float)left / (float)inch; + float _right = pixelPerInch * (float)right / (float)inch; + float _top = pixelPerInch * (float)top / (float)inch; + float _bottom = pixelPerInch * (float)bottom / (float)inch; Rectangle2D.Float rec = new Rectangle2D.Float(_left, _top, _right - _left, _bottom - _top); @@ -241,31 +262,31 @@ /** get the width of the WMF Metafile, in pixels. */ public int getWidthPixels() { - return (int)(PIXEL_PER_INCH * (float)width / (float)inch); + return (int)(pixelPerInch * (float)width / (float)inch); } /** get the factor to transform Metafile dimensions in pixels */ public float getUnitsToPixels() { - return (PIXEL_PER_INCH / (float)inch); + return (pixelPerInch / (float)inch); } /** get the factor to transform logical units width in pixels */ public float getVpWFactor() { - return (PIXEL_PER_INCH * (float)width / (float)inch) / (float)vpW; + return (pixelPerInch * (float)width / (float)inch) / (float)vpW; } /** get the factor to transform logical units height in pixels */ public float getVpHFactor() { - return (PIXEL_PER_INCH * (float)height / (float)inch) / (float)vpH; + return (pixelPerInch * (float)height / (float)inch) / (float)vpH; } /** get the height of the WMF Metafile, in pixels. */ public int getHeightPixels() { - return (int)(PIXEL_PER_INCH * (float)height / (float)inch); + return (int)(pixelPerInch * (float)height / (float)inch); } /** Return the sign of X coordinates. It is equal to 1 by default, but can be -1 if Index: sources/org/apache/batik/transcoder/wmf/tosvg/WMFHeaderProperties.java =================================================================== --- sources/org/apache/batik/transcoder/wmf/tosvg/WMFHeaderProperties.java (revision 833862) +++ sources/org/apache/batik/transcoder/wmf/tosvg/WMFHeaderProperties.java (working copy) @@ -125,7 +125,7 @@ vpY = 0; startX = 0; startY = 0; - scaleXY = 1f; + scaleXY = 1f; firstEffectivePaint = true; } @@ -160,7 +160,7 @@ // change isotropic if mode is anisotropic if (mapmode == WMFConstants.MM_ANISOTROPIC) isotropic = false; } - break; + break; case WMFConstants.META_SETWINDOWORG: { vpY = readShort( is ); vpX = readShort( is ); @@ -170,7 +170,7 @@ vpH = readShort( is ); vpW = readShort( is ); if (! isotropic) scaleXY = (float)vpW / (float)vpH; - vpW = (int)(vpW * scaleXY); + vpW = (int)(vpW * scaleXY); } break; @@ -445,7 +445,7 @@ float[] _xpts = new float[ count+1 ]; float[] _ypts = new float[ count+1 ]; for ( int i = 0; i < count; i++ ) { - _xpts[i] = readShort( is ) * scaleXY; + _xpts[i] = readShort( is ) * scaleXY; _ypts[i] = readShort( is ); } _xpts[count] = _xpts[0]; @@ -461,7 +461,7 @@ float[] _xpts = new float[ count ]; float[] _ypts = new float[ count ]; for ( int i = 0; i < count; i++ ) { - _xpts[i] = readShort( is ) * scaleXY; + _xpts[i] = readShort( is ) * scaleXY; _ypts[i] = readShort( is ); } Polyline2D pol = new Polyline2D(_xpts, _ypts, count); @@ -529,10 +529,10 @@ readShort( is ); // sx float heightDst = (float)readShort( is ); float widthDst = (float)readShort( is ) * scaleXY; - float dy = (float)readShort( is ) * getVpWFactor() * (float)inch / PIXEL_PER_INCH; - float dx = (float)readShort( is ) * getVpWFactor() * (float)inch / PIXEL_PER_INCH * scaleXY; - widthDst = widthDst * getVpWFactor() * (float)inch / PIXEL_PER_INCH; - heightDst = heightDst * getVpHFactor() * (float)inch / PIXEL_PER_INCH; + float dy = (float)readShort( is ) * getVpWFactor() * (float)inch / pixelPerInch; + float dx = (float)readShort( is ) * getVpWFactor() * (float)inch / pixelPerInch * scaleXY; + widthDst = widthDst * getVpWFactor() * (float)inch / pixelPerInch; + heightDst = heightDst * getVpHFactor() * (float)inch / pixelPerInch; resizeImageBounds((int)dx, (int)dy); resizeImageBounds((int)(dx + widthDst), (int)(dy + heightDst)); @@ -542,42 +542,42 @@ break; case WMFConstants.META_STRETCHDIB: { is.readInt(); // mode - readShort( is ); // usage + readShort( is ); // usage readShort( is ); // heightSrc readShort( is ); // widthSrc readShort( is ); // sy readShort( is ); // sx float heightDst = (float)readShort( is ); - float widthDst = (float)readShort( is ) * scaleXY; - float dy = (float)readShort( is ) * getVpHFactor() * (float)inch / PIXEL_PER_INCH; - float dx = (float)readShort( is ) * getVpHFactor() * (float)inch / PIXEL_PER_INCH * scaleXY; - widthDst = widthDst * getVpWFactor() * (float)inch / PIXEL_PER_INCH; - heightDst = heightDst * getVpHFactor() * (float)inch / PIXEL_PER_INCH; + float widthDst = (float)readShort( is ) * scaleXY; + float dy = (float)readShort( is ) * getVpHFactor() * (float)inch / pixelPerInch; + float dx = (float)readShort( is ) * getVpHFactor() * (float)inch / pixelPerInch * scaleXY; + widthDst = widthDst * getVpWFactor() * (float)inch / pixelPerInch; + heightDst = heightDst * getVpHFactor() * (float)inch / pixelPerInch; resizeImageBounds((int)dx, (int)dy); - resizeImageBounds((int)(dx + widthDst), (int)(dy + heightDst)); + resizeImageBounds((int)(dx + widthDst), (int)(dy + heightDst)); int len = 2*recSize - 22; - byte bitmap[] = new byte[len]; + byte bitmap[] = new byte[len]; for (int i = 0; i < len; i++) bitmap[i] = is.readByte(); } - break; + break; case WMFConstants.META_DIBBITBLT: { - is.readInt(); // mode - readShort( is ); //sy + is.readInt(); // mode + readShort( is ); //sy readShort( is ); //sx readShort( is ); // hdc - float height = readShort( is ) - * (float)inch / PIXEL_PER_INCH * getVpHFactor(); + float height = readShort( is ) + * (float)inch / pixelPerInch * getVpHFactor(); float width = readShort( is ) - * (float)inch / PIXEL_PER_INCH * getVpWFactor() * scaleXY; + * (float)inch / pixelPerInch * getVpWFactor() * scaleXY; float dy = - (float)inch / PIXEL_PER_INCH * getVpHFactor() * readShort( is ); + (float)inch / pixelPerInch * getVpHFactor() * readShort( is ); float dx = - (float)inch / PIXEL_PER_INCH * getVpWFactor() * readShort( is ) * scaleXY; + (float)inch / pixelPerInch * getVpWFactor() * readShort( is ) * scaleXY; resizeImageBounds((int)dx, (int)dy); - resizeImageBounds((int)(dx + width), (int)(dy + height)); + resizeImageBounds((int)(dx + width), (int)(dy + height)); } - break; + break; default: for ( int j = 0; j < recSize; j++ ) readShort(is); @@ -594,7 +594,7 @@ left = vpX + vpW; top = vpY; bottom = vpY + vpH; - } + } resetBounds(); return true; } @@ -617,14 +617,14 @@ * the Metafile, in Metafile Units. */ public int getWidthBoundsUnits() { - return (int)((float)inch * (float)_bwidth / PIXEL_PER_INCH); + return (int)((float)inch * (float)_bwidth / pixelPerInch); } /** @return the height of the Rectangle bounding the figures enclosed in * the Metafile in Metafile Units. */ public int getHeightBoundsUnits() { - return (int)((float)inch * (float)_bheight / PIXEL_PER_INCH); + return (int)((float)inch * (float)_bheight / pixelPerInch); } /** @return the X offset of the Rectangle bounding the figures enclosed in Index: sources/org/apache/batik/transcoder/wmf/tosvg/WMFPainter.java =================================================================== --- sources/org/apache/batik/transcoder/wmf/tosvg/WMFPainter.java (revision 833862) +++ sources/org/apache/batik/transcoder/wmf/tosvg/WMFPainter.java (working copy) @@ -29,7 +29,6 @@ import java.awt.Shape; import java.awt.Stroke; import java.awt.TexturePaint; -import java.awt.Toolkit; import java.awt.font.FontRenderContext; import java.awt.font.TextLayout; import java.awt.geom.AffineTransform; @@ -1056,8 +1055,7 @@ float _width; if (penWidth == 0) _width = 1; else _width = penWidth; - float _scale = (float)Toolkit.getDefaultToolkit().getScreenResolution() / - currentStore.getMetaFileUnitsPerInch(); + float _scale = currentStore.getPixelPerInch() / currentStore.getMetaFileUnitsPerInch(); // need to do this, to put the width in sync with the general scale of the image float factor = scale / _scale; _width = _width * _scale * factor; Index: sources/org/apache/batik/transcoder/wmf/tosvg/WMFTranscoder.java =================================================================== --- sources/org/apache/batik/transcoder/wmf/tosvg/WMFTranscoder.java (revision 833862) +++ sources/org/apache/batik/transcoder/wmf/tosvg/WMFTranscoder.java (working copy) @@ -76,16 +76,22 @@ * of this portion in Metafile units. * *
- *     transcoder.addTranscodingHint(FromWMFTranscoder.KEY_INPUT_WIDTH, new Integer(input_width));
+ *     transcoder.addTranscodingHint(WMFTranscoder.KEY_INPUT_WIDTH, new Integer(input_width));
  *  
* *
  • KEY_WIDTH, KEY_HEIGHT : this Float values allows to force the width and height of the output: * *
    - *     transcoder.addTranscodingHint(FromWMFTranscoder.KEY_WIDTH, new Float(width));
    + *     transcoder.addTranscodingHint(WMFTranscoder.KEY_WIDTH, new Float(width));
      *  
    *
  • + *
  • KEY_OUTPUT_RESOLUTION : dot-per-inch resolution, useful for headless transcoding: * + *
    + *     transcoder.addTranscodingHint(WMFTranscoder.KEY_OUTPUT_RESOLUTION, new Float(resol));
    + *  
    + *
  • + * * * @version $Id$ */ @@ -114,6 +120,14 @@ // Build a RecordStore from the input // WMFRecordStore currentStore = new WMFRecordStore(); + + // set the resolution if associated key is defined + if (hints.containsKey(KEY_OUTPUT_RESOLUTION)) { + float dotperinch = ((Float)hints.get(KEY_OUTPUT_RESOLUTION)).floatValue(); + currentStore.setPixelPerInch(dotperinch); + } + + // Read the input try { currentStore.read(is); } catch (IOException e){