The basic idea is to intercept the <x-tad-bigger>gvtRenderingCompleted</x-tad-bigger> event to recover the BufferedImage, and then encode this image to something your Cocoa App can handle, like a PNG.
If your source is from a URL, simply take the JSVGCanvas tutorial on the Batik page as a guideline and change the JSVGCanvas to a JSVGComponent, also, you do not need a Swing component, nor do you need to add the JSVGComponent to any window for it to render the image. And once the gvtRenderingCompleted is called you can recover your image.
I use this method for a JavaClient WebObjects app in Macintosh (although i only show the BufferedImage in a Swing component, and add the PNG back to my SVG as a thumbnail), so i am sure that it works on Max OS X.
>From a previous mail, that did not made it into the archives:
Basically, you need to create a JSVGComponent (you do not need a JSVGCanvas since you are displaying in a AWT) and add the build and render listeners, so you are notified when the data is ready to be fetched. The important one is GVTRenderListener. When the image is fully rendered into a BufferedImage, a GVTRenderingCompleted is triggered and you can fetch the image directly from the Event.
Once the listeners are in place, you need to load the document to your JSVGComponent, you can do so from a String (probably recuperated from a Database), from the filesystem, or from a prebuild org.w3c.dom.Document object. Once you assign the document to the JSVGComponent, simply wait until the event is triggered, and that is it.
Here is an abstract on how to do it.
---------------Start of code
svg = new JSVGComponent();
svg.addSVGDocumentLoaderListener(new SVGDocumentLoaderAdapter() {
public void documentLoadingStarted(SVGDocumentLoaderEvent e) {
}
public void documentLoadingCompleted(SVGDocumentLoaderEvent e) {
//System.out.println("SaveAsDXF-ExportCache succesfully loaded");
}
});
svg.addGVTTreeBuilderListener(new GVTTreeBuilderAdapter() {
public void gvtBuildStarted(GVTTreeBuilderEvent e) {
//System.out.println("SaveAsDXF-ExportCache-GVTBuilder Starting.....");
}
public void gvtBuildCompleted(GVTTreeBuilderEvent event) {
//////////////// If you want access to the G2D elements (generalPath, shapes, rectangles2D, you can
//////////////// get the GVTTree in this place and walk through it. The GVTTree has all your SVG
//////////////// converted into Graphics2D objects.
}
});
<x-tad-bigger>svg.addGVTTreeRendererListener(new GVTTreeRendererAdapter() {
public void gvtRenderingPrepare(GVTTreeRendererEvent e) {
}
public void gvtRenderingCompleted(GVTTreeRendererEvent e) {
////////////////////////////////////
////////////////////////////////////
// In here you can access your image, once the JSVGComponent has loaded
// the XMl and parsed it as SVG and into Graphics2D objects, and finally
// painted them into a BufferedImage.
////////////////////////////////////
BufferedImage e.getImage();
}
});</x-tad-bigger>
//////////////////////////////////// And now, to load your XML into the JSVGComponent you need something like this:
//////////////////////////////////// Please note that this example has it that the XML is already in memory in a String.
//////////////////////////////////// But you can have a FileReader to read from the filesystem also.
/////////////Or you can simply call the jsvgComponent.loadSVGDocument(String URL);
try {
String parser = XMLResourceDescriptor.getXMLParserClassName();
SAXSVGDocumentFactory f = new SAXSVGDocumentFactory(parser);
//////////data is the XML that defines the SVG. Usually a string.
Document doc = f.createSVGDocument(null, new java.io.StringReader((String)data));
////////////"svg" is the JSVGComponent object.
svg.setDocument(doc);
} catch (IOException ex) {
System.out.println("Error de svg."+ex);
}
//-------------------------end of code
Also, code form a mail by Thomas de Wesse, also lost in oblivion, to encode the BufferedImame into a PNG:
Encode it as a PNG and include it in the xlink:href using
the data protocol. This is what the code below does (very
indirectly).
The code to do this more directly (take from
batik.svggen.ImageHandlerBase64Encoder) is something like:
public static final String DATA_PROTOCOL_PNG_PREFIX =
"data:image/png;base64,";
ByteArrayOutputStream os = new ByteArrayOutputStream();
Base64EncoderStream b64Encoder = new Base64EncoderStream(os);
ImageEncoder encoder = new PNGImageEncoder(b64Encoder, null);
encoder.encode(buf);
// Close the b64 encoder stream (terminates b64 streams).
b64Encoder.close();
String encodedPNGString = DATA_PROTOCOL_PNG_PREFIX + os.toString();
----------------End of code to encode as PNG
On Feb 25, 2005, at 2:04 PM, Glen Simmons wrote:
I'm just getting started with Batik (and also with Java, somewhat) and
can't quite figure out where to start. I need to render an SVG and get
an image that I can display in the UI of my app. This is a Mac OS X
Cocoa app, so the JSVGCanvas component is not an option. Calling other
Java code is no problem, though.
From the architecture overview
(http://xml.apache.org/batik/architecture.html), it appears that I
need to drop down to the Renderer module. But I can't figure out how
to load an SVG file into it and get it to render. Is there some sample
code that shows how to do this?
Thanks,
Glen Simmons
---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]