Permalink
Browse files

Fix VectorWriter to use commands of VectorGraphics2D 0.12.

  • Loading branch information...
eseifert committed Jul 29, 2017
1 parent e1b7d62 commit fd9d0a333db3374463be986e358c4f89fdf9f173
Showing with 47 additions and 44 deletions.
  1. +47 −44 gral-core/src/main/java/de/erichseifert/gral/io/plots/VectorWriter.java
@@ -25,7 +25,6 @@
import java.awt.geom.Rectangle2D;
import java.io.IOException;
import java.io.OutputStream;
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.text.MessageFormat;
import java.util.HashMap;
@@ -55,63 +54,54 @@
*/
public class VectorWriter extends IOCapabilitiesStorage
implements DrawableWriter {
/** Mapping of MIME type string to {@code Graphics2D}lementation. */
private static final Map<String, Class<?>> graphics;
/** Mapping of MIME type string to {@code Processor} implementation. */
private static final Map<String, Class<?>> processors;
/** Java package that contains the VecorGraphics2D package. */
private static final String VECTORGRAPHICS2D_PACKAGE =
"de.erichseifert.vectorgraphics2d"; //$NON-NLS-1$
static {
graphics = new HashMap<>();
Class<?> cls;
processors = new HashMap<>();
try {
cls = Class.forName(VECTORGRAPHICS2D_PACKAGE
+ ".EPSGraphics2D"); //$NON-NLS-1$
Class<?> cls = Class.forName(VECTORGRAPHICS2D_PACKAGE + ".eps.EPSProcessor"); //$NON-NLS-1$
addCapabilities(new IOCapabilities(
"EPS", //$NON-NLS-1$
Messages.getString("ImageIO.epsDescription"), //$NON-NLS-1$
"application/postscript", //$NON-NLS-1$
new String[] {"eps", "epsf", "epsi"} //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
));
graphics.put("application/postscript", cls); //$NON-NLS-1$
processors.put("application/postscript", cls); //$NON-NLS-1$
} catch (ClassNotFoundException e) {
cls = null;
}
try {
cls = Class.forName(VECTORGRAPHICS2D_PACKAGE
+ ".PDFGraphics2D"); //$NON-NLS-1$
Class<?> cls = Class.forName(VECTORGRAPHICS2D_PACKAGE + ".pdf.PDFProcessor"); //$NON-NLS-1$
addCapabilities(new IOCapabilities(
"PDF", //$NON-NLS-1$
Messages.getString("ImageIO.pdfDescription"), //$NON-NLS-1$
"application/pdf", //$NON-NLS-1$
new String[] {"pdf"} //$NON-NLS-1$
));
graphics.put("application/pdf", cls); //$NON-NLS-1$
processors.put("application/pdf", cls); //$NON-NLS-1$
} catch (ClassNotFoundException e) {
cls = null;
}
try {
cls = Class.forName(VECTORGRAPHICS2D_PACKAGE
+ ".SVGGraphics2D"); //$NON-NLS-1$
Class<?> cls = Class.forName(VECTORGRAPHICS2D_PACKAGE + ".svg.SVGProcessor"); //$NON-NLS-1$
addCapabilities(new IOCapabilities(
"SVG", //$NON-NLS-1$
Messages.getString("ImageIO.svgDescription"), //$NON-NLS-1$
"image/svg+xml", //$NON-NLS-1$
new String[] {"svg", "svgz"} //$NON-NLS-1$ //$NON-NLS-2$
));
graphics.put("image/svg+xml", cls); //$NON-NLS-1$
processors.put("image/svg+xml", cls); //$NON-NLS-1$
} catch (ClassNotFoundException e) {
cls = null;
}
}
/** Current data format as MIME type string. */
private final String mimeType;
/** Current {@code Graphics2D} implementation used for rendering. */
private final Class<? extends Graphics2D> graphicsClass;
/**
* Creates a new {@code VectorWriter} object with the specified
@@ -121,14 +111,7 @@
@SuppressWarnings("unchecked")
protected VectorWriter(String mimeType) {
this.mimeType = mimeType;
Class<? extends Graphics2D> gfxCls;
try {
gfxCls = (Class<? extends Graphics2D>) graphics.get(mimeType);
} catch (ClassCastException e) {
gfxCls = null;
}
graphicsClass = gfxCls;
if (graphicsClass == null) {
if (!processors.containsKey(mimeType)) {
throw new IllegalArgumentException(MessageFormat.format(
"Unsupported file format: {0}", mimeType)); //$NON-NLS-1$
}
@@ -160,25 +143,45 @@ public void write(Drawable d, OutputStream destination,
public void write(Drawable d, OutputStream destination,
double x, double y, double width, double height)
throws IOException {
try {
// Create instance of export class
Constructor<? extends Graphics2D> constructor =
graphicsClass.getConstructor(
double.class, double.class, double.class, double.class);
Graphics2D g = constructor.newInstance(x, y, width, height);
// Temporary change size of drawable
Rectangle2D boundsOld = d.getBounds();
d.setBounds(x, y, width, height);
// Output data
Rectangle2D boundsOld = d.getBounds();
d.setBounds(x, y, width, height);
DrawingContext context =
new DrawingContext(g, Quality.QUALITY, Target.VECTOR);
d.draw(context);
byte[] data = (byte[]) graphicsClass.getMethod(
"getBytes").invoke(g); //$NON-NLS-1$
destination.write(data);
d.setBounds(boundsOld);
} catch (SecurityException | InvocationTargetException | IllegalAccessException | InstantiationException | IllegalArgumentException | NoSuchMethodException e) {
try {
// Create an instance of Graphics2D implementation
Class<?> vg2dClass = Class.forName(VECTORGRAPHICS2D_PACKAGE +
".VectorGraphics2D"); //$NON-NLS-1$
Graphics2D g = (Graphics2D) vg2dClass.newInstance();
// Paint the Drawable instance
d.draw(new DrawingContext(g, Quality.QUALITY, Target.VECTOR));
// Create corresponding VectorGraphics2D processor instance
Class<?> processorClass = processors.get(mimeType);
Object processor = processorClass.newInstance();
// Get sequence of commands
Class<?> commandSequenceClass = Class.forName(VECTORGRAPHICS2D_PACKAGE +
".intermediate.CommandSequence"); //$NON-NLS-1$
Object commands = vg2dClass.getMethod("getCommands").invoke(g); //$NON-NLS-1$
// Define page size
Class<?> pageSizeClass = Class.forName(VECTORGRAPHICS2D_PACKAGE +
".util.PageSize"); //$NON-NLS-1$
Object pageSize = pageSizeClass
.getDeclaredConstructor(Double.TYPE, Double.TYPE, Double.TYPE, Double.TYPE)
.newInstance(x, y, width, height);
// Get document from commands with defined page size
Object document = processorClass
.getMethod("getDocument", commandSequenceClass, pageSizeClass) //$NON-NLS-1$
.invoke(processor, commands, pageSize);
// Write document to destination stream
Class<?> documentClass = Class.forName(VECTORGRAPHICS2D_PACKAGE +
".Document"); //$NON-NLS-1$
documentClass.getMethod("writeTo", OutputStream.class) //$NON-NLS-1$
.invoke(document, destination);
} catch (ClassNotFoundException | SecurityException | InvocationTargetException |
IllegalAccessException | InstantiationException | IllegalArgumentException |
NoSuchMethodException e) {
throw new IllegalStateException(e);
} finally {
d.setBounds(boundsOld);
}
}

0 comments on commit fd9d0a3

Please sign in to comment.