> I'm not sure why you think the JSVGCanvas is not a 'true' Component.
Ignorance probably. What I meant was that to get a tree of sub
components I have to fetch a GVTTree rather than relying on a
structure such as:
JComponent parent = new SomeJComponent()
JComponent someChild = new SomeOtherJComponent()
parent.add(someChild);
someChild.add(new anotherchildJComp());
etc.
which freehep relies upon.
> The real question is if you are trying to simply transcode why are you
> creating a component. I would look at subclassing
> batik.transcoder.SVGAbstractTranscoder this class will read an SVG file
> and build the GVT tree. Then you can more or less just call
> 'paint' on the root GVT node passing in the FreeHEP EMF Graphics2D.
The SVGAbstractTranscoder is a good idea. Beforehand though, I got
something working with the JComponent approach which I'll include here
for anyone interested. Its a cut and paste modification of the
JSVGCanvas code with two files (1) SimpleEMFWriter.java (main
program); and (2) SVG2EMF.java (helper with the GVTTree) [apols for
the spew of inline code here]
// SVG2EMF.java
import org.apache.batik.gvt.GVTTreeWalker;
import org.apache.batik.swing.JSVGCanvas;
import org.apache.batik.swing.svg.GVTTreeBuilderEvent;
import org.apache.batik.swing.svg.GVTTreeBuilderListener;
import org.freehep.graphics2d.VectorGraphics;
import org.freehep.graphicsio.emf.EMFGraphics2D;
import javax.swing.*;
import java.awt.*;
import java.io.File;
import java.io.FileNotFoundException;
public class SVG2EMF
extends JComponent
implements GVTTreeBuilderListener {
private Graphics2D g = null;
JSVGCanvas svgCanvas = new JSVGCanvas();
GVTTreeWalker gvtTreeWalker = null;
String URI;
public SVG2EMF() {
}
public void setURI(String URI) {
this.URI = URI;
svgCanvas.addGVTTreeBuilderListener(this);
svgCanvas.setURI(URI);
}
public void export(File file) throws FileNotFoundException {
VectorGraphics v = new EMFGraphics2D(
file, (Dimension) svgCanvas.getSVGDocumentSize());
v.startExport();
this.print(v);
v.endExport();
}
public void paint(Graphics g) {
this.g = (Graphics2D) g;
if (gvtTreeWalker != null) {
gvtTreeWalker.getRoot().paint((Graphics2D) this.g);
}
}
public void gvtBuildStarted(GVTTreeBuilderEvent event) {
//To change body of implemented methods use File | Settings |
File Templates.
}
public void gvtBuildCompleted(GVTTreeBuilderEvent e) {
gvtTreeWalker = new GVTTreeWalker(
svgCanvas.getGraphicsNode());
super.repaint();
}
public void gvtBuildCancelled(GVTTreeBuilderEvent event) {
//To change body of implemented methods use File | Settings |
File Templates.
}
public void gvtBuildFailed(GVTTreeBuilderEvent event) {
//To change body of implemented methods use File | Settings |
File Templates.
}
}
// -- SimpleEMFWriter.java
import org.freehep.graphicsio.emf.EMFExportFileType;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.File;
import java.io.IOException;
public class SimpleEMFWriter {
public static void main(String[] args) {
JFrame f = new JFrame("Batik");
SimpleEMFWriter app = new SimpleEMFWriter(f);
f.getContentPane().add(app.createComponents());
f.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
f.setSize(400, 400);
f.setVisible(true);
}
JFrame frame;
JButton button = new JButton("Load...");
JButton saveButton = new JButton("Save EMF");
JLabel label = new JLabel();
SVG2EMF svgObj = new SVG2EMF();
final EMFExportFileType emfExportFileType = new EMFExportFileType();
public SimpleEMFWriter(JFrame f) {
frame = f;
}
public JComponent createComponents() {
final JPanel panel = new JPanel(new BorderLayout());
JPanel p = new JPanel(new FlowLayout(FlowLayout.LEFT));
p.add(button);
p.add(label);
JPanel southPanel = new JPanel(new FlowLayout(FlowLayout.LEFT));
southPanel.add(saveButton);
panel.add("North", p);
panel.add("Center", svgObj);
panel.add("South", southPanel);
// Set the button action.
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae) {
JFileChooser fc = new JFileChooser(".");
int choice = fc.showOpenDialog(panel);
if (choice == JFileChooser.APPROVE_OPTION) {
File f = fc.getSelectedFile();
try {
svgObj.setURI(f.toURL().toString());
} catch (IOException ex) {
ex.printStackTrace();
}
}
}
});
saveButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae) {
try {
svgObj.export(new File("c:\\filename.emf"));
} catch (IOException e) {
e.printStackTrace(); //To change body of catch
statement use File | Settings | File Templates.
}
}
});
return panel;
}
}
> > Any takers to create a cut and paste example?
>
> Sorry I don't have cut and paste code but I will try and help you
> create such code ;)
Nice :)
Even though the above works for simple svgs, complex ones (the ones I
need to convert) don't work. Things like boxes with a certain edge
shade/font missing. Can't really tell without doing more tests but
I'm thinking that there is something wrong with the freehep conversion
(or the styles/fonts batik uses for SVG might be nonstandard?). Not
sure if anyone has encountered this but I'll try and look at the
freehep forums and dig up (and post back) any answers.
If I don't get this licked I *could* convert SVG->PS/PDF->EMF via
Apache FOP and pstoedit (or similar), but it seems like a horrible
kludge and requires lots of dlls etc on the pstoedit side (results are
near perfect though). Argh ... Surely someone has a nice (conversion
perfect) complete (pref Java) solution? Damn M$ and their closed
standards.
Cheers,
Anthony
---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]