Skip to content
Browse files

Fixed HeadlessException by introducing Platform.getScreenResolution().

git-svn-id: https://svn.apache.org/repos/asf/xmlgraphics/batik/trunk@1076146 13f79535-47bb-0310-9956-ffa450edef68
  • Loading branch information...
1 parent a5e1a45 commit 6ebe0589ed267c0d25a455b22ecd03ed894be2c2 @jmaerki jmaerki committed
View
13 sources/org/apache/batik/transcoder/ToSVGAbstractTranscoder.java
@@ -18,7 +18,6 @@ Licensed to the Apache Software Foundation (ASF) under one or more
*/
package org.apache.batik.transcoder;
-import java.awt.Toolkit;
import java.io.IOException;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
@@ -32,6 +31,7 @@ Licensed to the Apache Software Foundation (ASF) under one or more
import org.apache.batik.transcoder.keys.BooleanKey;
import org.apache.batik.transcoder.keys.FloatKey;
import org.apache.batik.transcoder.keys.IntegerKey;
+import org.apache.batik.util.Platform;
import org.apache.batik.util.SVGConstants;
import org.xml.sax.XMLFilter;
@@ -93,8 +93,8 @@ Licensed to the Apache Software Foundation (ASF) under one or more
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();
+ PIXEL_TO_MILLIMETERS = 25.4f / (float)Platform.getScreenResolution();
+ PIXEL_PER_INCH = Platform.getScreenResolution();
}
public static final int TRANSCODER_ERROR_BASE = 0xff00;
@@ -153,7 +153,9 @@ protected Document createDocument(TranscoderOutput output) {
DOMImplementation domImpl = SVGDOMImplementation.getDOMImplementation();
doc = domImpl.createDocument(SVG_NAMESPACE_URI, SVG_SVG_TAG, null);
- } else doc = output.getDocument();
+ } else {
+ doc = output.getDocument();
+ }
return doc;
}
@@ -184,8 +186,9 @@ protected void writeSVGToOutput(SVGGraphics2D svgGenerator, Element svgRoot,
try {
boolean escaped = false;
- if (hints.containsKey(KEY_ESCAPED))
+ if (hints.containsKey(KEY_ESCAPED)) {
escaped = ((Boolean)hints.get(KEY_ESCAPED)).booleanValue();
+ }
// Output stream
OutputStream os = output.getOutputStream();
if (os != null) {
View
27 sources/org/apache/batik/transcoder/wmf/tosvg/AbstractWMFReader.java
@@ -20,7 +20,6 @@ Licensed to the Apache Software Foundation (ASF) under one or more
package org.apache.batik.transcoder.wmf.tosvg;
import java.awt.Rectangle;
-import java.awt.Toolkit;
import java.awt.geom.Rectangle2D;
import java.io.DataInputStream;
import java.io.IOException;
@@ -28,6 +27,7 @@ Licensed to the Apache Software Foundation (ASF) under one or more
import java.util.ArrayList;
import org.apache.batik.transcoder.wmf.WMFConstants;
+import org.apache.batik.util.Platform;
/**
* This class provides a general framework to read WMF Metafiles.
@@ -35,9 +35,8 @@ Licensed to the Apache Software Foundation (ASF) under one or more
*/
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 final float PIXEL_PER_INCH = Platform.getScreenResolution();
+ public static final float MM_PER_PIXEL = 25.4f / Platform.getScreenResolution();
protected int left, right, top, bottom, width, height, inch;
protected float scaleX, scaleY, scaleXY;
protected int vpW, vpH, vpX, vpY;
@@ -47,7 +46,7 @@ Licensed to the Apache Software Foundation (ASF) under one or more
protected volatile boolean bReading = false;
protected boolean isAldus = false;
- protected boolean isotropic = true;
+ protected boolean isotropic = true;
protected int mtType, mtHeaderSize, mtVersion, mtSize, mtNoObjects;
protected int mtMaxRecord, mtNoParameters;
@@ -60,7 +59,7 @@ Licensed to the Apache Software Foundation (ASF) under one or more
public AbstractWMFReader() {
scaleX = 1;
scaleY = 1;
- scaleXY = 1f;
+ scaleXY = 1f;
left = -1;
top = -1;
width = -1;
@@ -77,7 +76,7 @@ public AbstractWMFReader(int width, int height) {
this.height = height;
}
- /**
+ /**
* Read the next short (2 bytes) value in the DataInputStream.
*/
protected short readShort(DataInputStream is) throws IOException {
@@ -89,9 +88,9 @@ protected short readShort(DataInputStream is) throws IOException {
return i;
}
- /**
+ /**
* Read the next int (4 bytes) value in the DataInputStream.
- */
+ */
protected int readInt( DataInputStream is) throws IOException {
byte js[] = new byte[ 4 ];
is.readFully(js);
@@ -362,7 +361,7 @@ public void read(DataInputStream is) throws IOException {
if ( dwIsAldus == WMFConstants.META_ALDUS_APM ) {
// Read the aldus placeable header.
int key = dwIsAldus;
- isAldus = true;
+ isAldus = true;
readShort( is ); // metafile handle, always zero
left = readShort( is );
top = readShort( is );
@@ -388,14 +387,14 @@ public void read(DataInputStream is) throws IOException {
width = right - left;
height = bottom - top;
-
+
// read the beginning of the header
mtType = readShort( is );
- mtHeaderSize = readShort( is );
+ mtHeaderSize = readShort( is );
} else {
// read the beginning of the header, the first int corresponds to the first two parameters
- mtType = ((dwIsAldus << 16) >> 16);
- mtHeaderSize = dwIsAldus >> 16;
+ mtType = ((dwIsAldus << 16) >> 16);
+ mtHeaderSize = dwIsAldus >> 16;
}
mtVersion = readShort( is );
View
150 sources/org/apache/batik/transcoder/wmf/tosvg/WMFPainter.java
@@ -29,7 +29,6 @@ Licensed to the Apache Software Foundation (ASF) under one or more
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;
@@ -52,6 +51,7 @@ Licensed to the Apache Software Foundation (ASF) under one or more
import org.apache.batik.ext.awt.geom.Polygon2D;
import org.apache.batik.ext.awt.geom.Polyline2D;
import org.apache.batik.transcoder.wmf.WMFConstants;
+import org.apache.batik.util.Platform;
/**
* Core class for rendering the WMF image. It is able to render a
@@ -244,13 +244,14 @@ public void paint( Graphics g ) {
} else if (brushStyle == WMFConstants.BS_HATCHED) {
int hatch = mr.elementAt( 4 );
Paint paint;
- if (! opaque)
+ if (! opaque) {
paint = TextureFactory.getInstance().getTexture(hatch, clr);
- else
+ } else {
paint = TextureFactory.getInstance().getTexture(hatch, clr, bkgdColor);
- if (paint != null)
+ }
+ if (paint != null) {
objIndex = addObjectAt( currentStore, BRUSH, paint, objIndex );
- else {
+ } else {
clr = Color.black;
objIndex = addObjectAt( currentStore, NULL_BRUSH, clr, objIndex );
}
@@ -276,11 +277,18 @@ public void paint( Graphics g ) {
int d = 0;
while ((d < face.length()) &&
((Character.isLetterOrDigit(face.charAt(d))) ||
- (Character.isWhitespace(face.charAt(d))))) d++;
- if (d > 0) face = face.substring(0,d);
- else face = "System";
+ (Character.isWhitespace(face.charAt(d))))) {
+ d++;
+ }
+ if (d > 0) {
+ face = face.substring(0,d);
+ } else {
+ face = "System";
+ }
- if ( size < 0 ) size = -size /* * -1.3 */;
+ if ( size < 0 ) {
+ size = -size /* * -1.3 */;
+ }
int objIndex = 0;
fontHeight = size;
@@ -322,8 +330,9 @@ public void paint( Graphics g ) {
case WMFConstants.META_SELECTOBJECT:
gdiIndex = mr.elementAt( 0 );
- if (( gdiIndex & 0x80000000 ) != 0 ) // Stock Object
+ if (( gdiIndex & 0x80000000 ) != 0 ) { // Stock Object
break;
+ }
if ( gdiIndex >= numObjects ) {
gdiIndex -= numObjects;
@@ -353,18 +362,22 @@ public void paint( Graphics g ) {
break;
}
gdiObj = currentStore.getObject( gdiIndex );
- if ( !gdiObj.used )
+ if ( !gdiObj.used ) {
break;
+ }
switch( gdiObj.type ) {
case PEN:
g2d.setColor( (Color)gdiObj.obj );
penObject = gdiIndex;
break;
case BRUSH:
- if (gdiObj.obj instanceof Color) g2d.setColor( (Color)gdiObj.obj );
- else if (gdiObj.obj instanceof Paint) {
+ if (gdiObj.obj instanceof Color) {
+ g2d.setColor( (Color)gdiObj.obj );
+ } else if (gdiObj.obj instanceof Paint) {
g2d.setPaint((Paint)gdiObj.obj);
- } else g2d.setPaint(getPaint((byte[])(gdiObj.obj)));
+ } else {
+ g2d.setPaint(getPaint((byte[])(gdiObj.obj)));
+ }
brushObject = gdiIndex;
break;
case FONT: {
@@ -386,9 +399,13 @@ else if (gdiObj.obj instanceof Paint) {
case WMFConstants.META_DELETEOBJECT:
gdiIndex = mr.elementAt( 0 );
gdiObj = currentStore.getObject( gdiIndex );
- if ( gdiIndex == brushObject ) brushObject = -1;
- else if ( gdiIndex == penObject ) penObject = -1;
- else if ( gdiIndex == fontObject ) fontObject = -1;
+ if ( gdiIndex == brushObject ) {
+ brushObject = -1;
+ } else if ( gdiIndex == penObject ) {
+ penObject = -1;
+ } else if ( gdiIndex == fontObject ) {
+ fontObject = -1;
+ }
gdiObj.clear();
break;
@@ -396,8 +413,9 @@ else if (gdiObj.obj instanceof Paint) {
{
int numPolygons = mr.elementAt( 0 );
int[] pts = new int[ numPolygons ];
- for ( int ip = 0; ip < numPolygons; ip++ )
+ for ( int ip = 0; ip < numPolygons; ip++ ) {
pts[ ip ] = mr.elementAt( ip + 1 );
+ }
int offset = numPolygons+1;
List v = new ArrayList( numPolygons );
@@ -547,8 +565,11 @@ else if (gdiObj.obj instanceof Paint) {
float x = scaleX * ( vpX + xOffset + mr.elementAt( 0 ) );
float y = scaleY * ( vpY + yOffset + mr.elementAt( 1 ) );
- if ( frgdColor != null ) g2d.setColor( frgdColor );
- else g2d.setColor( Color.black );
+ if ( frgdColor != null ) {
+ g2d.setColor( frgdColor );
+ } else {
+ g2d.setColor( Color.black );
+ }
FontRenderContext frc = g2d.getFontRenderContext();
@@ -577,7 +598,9 @@ else if (gdiObj.obj instanceof Paint) {
drawString(flag, g2d,
getCharacterIterator(g2d, sr, wmfFont, currentHorizAlign),
x, y, layout, wmfFont, currentHorizAlign);
- if (clipped) g2d.setClip(clip);
+ if (clipped) {
+ g2d.setClip(clip);
+ }
} catch ( Exception e ) {
}
break;
@@ -590,8 +613,11 @@ else if (gdiObj.obj instanceof Paint) {
float x = scaleX * ( vpX + xOffset + mr.elementAt( 0 ) );
float y = scaleY * ( vpY + yOffset + mr.elementAt( 1 ) );
- if ( frgdColor != null ) g2d.setColor( frgdColor );
- else g2d.setColor( Color.black );
+ if ( frgdColor != null ) {
+ g2d.setColor( frgdColor );
+ } else {
+ g2d.setColor( Color.black );
+ }
FontRenderContext frc = g2d.getFontRenderContext();
@@ -631,32 +657,36 @@ else if (gdiObj.obj instanceof Paint) {
double endAngle = - Math.toDegrees( Math.atan2(yend - cy, xend - cx) );
double extentAngle = endAngle - startAngle;
- if (extentAngle < 0) extentAngle += 360;
- if (startAngle < 0) startAngle +=360;
+ if (extentAngle < 0) {
+ extentAngle += 360;
+ }
+ if (startAngle < 0) {
+ startAngle +=360;
+ }
+
-
Arc2D.Double arc;
switch(mr.functionId) {
case WMFConstants.META_ARC:
- arc = new Arc2D.Double(left, top,
+ arc = new Arc2D.Double(left, top,
right - left, bottom - top,
- startAngle, extentAngle,
+ startAngle, extentAngle,
Arc2D.OPEN);
g2d.draw(arc);
break;
case WMFConstants.META_PIE:
- arc = new Arc2D.Double(left, top,
+ arc = new Arc2D.Double(left, top,
right - left, bottom - top,
- startAngle, extentAngle,
+ startAngle, extentAngle,
Arc2D.PIE);
paint(brushObject, penObject, arc, g2d);
- // g2d.fill(arc);
+ // g2d.fill(arc);
break;
case WMFConstants.META_CHORD:
- arc = new Arc2D.Double(left, top,
- right - left,
+ arc = new Arc2D.Double(left, top,
+ right - left,
bottom - top,
- startAngle, extentAngle,
+ startAngle, extentAngle,
Arc2D.CHORD);
paint(brushObject, penObject, arc, g2d);
}
@@ -839,10 +869,10 @@ else if (gdiObj.obj instanceof Paint) {
int width = mr.elementAt( 2 );
int sy = mr.elementAt( 3 );
int sx = mr.elementAt( 4 );
- float dy = conv * currentStore.getVpWFactor() *
+ float dy = conv * currentStore.getVpWFactor() *
(vpY + yOffset + (float)mr.elementAt( 7 ));
- float dx = conv * currentStore.getVpHFactor() *
- (vpX + xOffset + (float)mr.elementAt( 8 ));
+ float dx = conv * currentStore.getVpHFactor() *
+ (vpX + xOffset + (float)mr.elementAt( 8 ));
float heightDst = (float)(mr.elementAt( 5 ));
float widthDst = (float)(mr.elementAt( 6 ));
widthDst = widthDst * conv * currentStore.getVpWFactor();
@@ -859,11 +889,11 @@ else if (gdiObj.obj instanceof Paint) {
//g2d.setComposite(AlphaComposite.SrcOver);
g2d.drawImage(img, (int)dx, (int)dy, (int)(dx + widthDst),
(int)(dy + heightDst), sx, sy, sx + width,
- sy + height, observer);
+ sy + height, observer);
}
- }
+ }
}
- break;
+ break;
case WMFConstants.META_DIBBITBLT:
{
int rop = mr.ElementAt( 0 ).intValue();
@@ -965,7 +995,9 @@ private void drawString(int flag, Graphics2D g2d, AttributedCharacterIterator at
float x, float y, TextLayout layout, WMFFont wmfFont, int align) {
if (wmfFont.escape == 0) {
- if (flag != -1) fillTextBackground(-1, flag, g2d, x, y, 0, layout);
+ if (flag != -1) {
+ fillTextBackground(-1, flag, g2d, x, y, 0, layout);
+ }
float width = (float)(layout.getBounds().getWidth());
if (align == WMFConstants.TA_CENTER) {
g2d.drawString(ati, x-width/2, y);
@@ -990,7 +1022,9 @@ private void drawString(int flag, Graphics2D g2d, AttributedCharacterIterator at
g2d.translate(0, height/2);
g2d.rotate(angle, x, y);
}
- if (flag != -1) fillTextBackground(align, flag, g2d, x, y, width, layout);
+ if (flag != -1) {
+ fillTextBackground(align, flag, g2d, x, y, width, layout);
+ }
Stroke _st = g2d.getStroke();
g2d.setStroke(textSolid);
g2d.drawString(ati, x, y);
@@ -1003,8 +1037,11 @@ private void fillTextBackground(int align, int flag, Graphics2D g2d, float x, fl
float width, TextLayout layout) {
float _x = x;
- if (align == WMFConstants.TA_CENTER) _x = x-width/2;
- else if (align == WMFConstants.TA_RIGHT) _x = x - width;
+ if (align == WMFConstants.TA_CENTER) {
+ _x = x-width/2;
+ } else if (align == WMFConstants.TA_RIGHT) {
+ _x = x - width;
+ }
if ((flag & WMFConstants.ETO_OPAQUE) != 0) {
Color c = g2d.getColor();
@@ -1040,9 +1077,9 @@ private void drawPolyPolygon(Graphics2D g2d, List pols) {
*/
private void fillPolyPolygon(Graphics2D g2d, List pols) {
// if there is only one Polygon, there is no need of a path
- if (pols.size() == 1) g2d.fill((Polygon2D)(pols.get(0)));
- // the real stuff : we create an EVEN_ODD path, and add all the Shapes to it
- else {
+ if (pols.size() == 1) {
+ g2d.fill((Polygon2D)(pols.get(0)));
+ } else {
GeneralPath path = new GeneralPath(GeneralPath.WIND_EVEN_ODD);
for (int i = 0; i < pols.size(); i++) {
Polygon2D pol = (Polygon2D)(pols.get(i));
@@ -1054,9 +1091,12 @@ private void fillPolyPolygon(Graphics2D g2d, List pols) {
private void setStroke(Graphics2D g2d, int penStyle, float penWidth, float scale) {
float _width;
- if (penWidth == 0) _width = 1;
- else _width = penWidth;
- float _scale = (float)Toolkit.getDefaultToolkit().getScreenResolution() /
+ if (penWidth == 0) {
+ _width = 1;
+ } else {
+ _width = penWidth;
+ }
+ float _scale = (float)Platform.getScreenResolution() /
currentStore.getMetaFileUnitsPerInch();
// need to do this, to put the width in sync with the general scale of the image
float factor = scale / _scale;
@@ -1109,9 +1149,13 @@ private int getHorizontalAlignement(int align) {
private void setBrushPaint( WMFRecordStore currentStore, Graphics2D g2d, int brushObject) {
if ( brushObject >= 0 ) {
GdiObject gdiObj = currentStore.getObject( brushObject );
- if (gdiObj.obj instanceof Color) g2d.setColor( (Color)gdiObj.obj );
- else if (gdiObj.obj instanceof Paint) g2d.setPaint( (Paint)gdiObj.obj );
- else g2d.setPaint(getPaint((byte[])gdiObj.obj));
+ if (gdiObj.obj instanceof Color) {
+ g2d.setColor( (Color)gdiObj.obj );
+ } else if (gdiObj.obj instanceof Paint) {
+ g2d.setPaint( (Paint)gdiObj.obj );
+ } else {
+ g2d.setPaint(getPaint((byte[])gdiObj.obj));
+ }
brushObject = -1;
}
}
View
19 sources/org/apache/batik/util/Platform.java
@@ -18,8 +18,9 @@ Licensed to the Apache Software Foundation (ASF) under one or more
*/
package org.apache.batik.util;
-import java.awt.Frame;
-import java.lang.reflect.Method;
+import java.awt.GraphicsEnvironment;
+import java.awt.HeadlessException;
+import java.awt.Toolkit;
/**
* Platform specific functionality.
@@ -34,4 +35,18 @@ Licensed to the Apache Software Foundation (ASF) under one or more
*/
public static boolean isOSX =
System.getProperty("os.name").equals("Mac OS X");
+
+ /**
+ * Returns the active screen resolution. If Batik is run in a headless environment, this
+ * method will return the default "96" unlike {@link Toolkit#getScreenResolution()} which
+ * will throw a {@link HeadlessException}.
+ * @return the screen resolution in dpi
+ */
+ public static int getScreenResolution() {
+ if (GraphicsEnvironment.isHeadless()) {
+ return 96;
+ } else {
+ return Toolkit.getDefaultToolkit().getScreenResolution();
+ }
+ }
}

0 comments on commit 6ebe058

Please sign in to comment.
Something went wrong with that request. Please try again.