Flamingo SVG trancoder enhancements

August 24th, 2008

The demo applications for the Flamingo JRibbon component make heavy use of the SVG icons to make sure that the icons can be scaled without losing visual details. While Flamingo provides Batik-based implementation of the ResizableIcon interface, this has proven to place a noticeable burden on CPU during the application startup phase. One of the ways to address this performance issue is to use the SVG-Java2D transcoder that is part of Flamingo core distribution.

There’s nothing like eating your own dog food, and while trying to use the transcoded Java2D class to replace the runtime SVG decoding i found that it was lacking some of the basic functionality. The most glaring one was lack of API to query the default bounds of the SVG image. This is now exposed in the Java2D class with two static methods – getOrigWidth and getOrigHeight.

The second enhancement to the org.jvnet.flamingo.svg.SvgTranscoder class is the new setJavaToImplementResizableIconInterface(boolean) method. If you call this method prior to calling the transcode(), the resulting class will implement the org.jvnet.flamingo.common.icon.ResizableIcon interface, making it ready to be used instead of the org.jvnet.flamingo.svg.SvgBatikResizableIcon class. This comes handy since the heavy transcoding is done offline, and the size of the compiled class is comparable with the size of the compressed SVG file (especially if you turn the debugging info off). You can see the difference yourself by running the ribbon demo below – once the application frame is shown, all the icons are immediately visible and you can start interacting with it.

To change the package name of the generated Java2D class, call the setJavaPackageName(String) method prior to transcode() call. The last addition is the sample test.svg.SvgBatchConverter class that shows how to run the SVG-Java2D conversion in batch mode on all SVG files in a given folder.

Using the offline transcoding has another advantage – you don’t need to bundle the Batik jars that are around 3-4MB heavy. One point left to your consideration is the issue of the license of the original SVG images and its implications on the transcoded Java2D classes.