private static Log log = LogFactory.getLog(ImageConverterSVG2G2D.class);
/** {@inheritDoc} */
public Image convert(final Image src, Map hints) throws ImageException {
checkSourceFlavor(src);
final ImageXMLDOM svg = (ImageXMLDOM)src;
if (!SVGDOMImplementation.SVG_NAMESPACE_URI.equals(svg.getRootNamespace())) {
throw new IllegalArgumentException("XML DOM is not in the SVG namespace: "
+ svg.getRootNamespace());
}
//Prepare
float pxToMillimeter = UnitConv.IN2MM / GraphicsConstants.DEFAULT_DPI;
Number ptm = (Number)hints.get(ImageProcessingHints.SOURCE_RESOLUTION);
if (ptm != null) {
pxToMillimeter = (float)(UnitConv.IN2MM / ptm.doubleValue());
}
UserAgent ua = createBatikUserAgent(pxToMillimeter);
GVTBuilder builder = new GVTBuilder();
final ImageManager imageManager = (ImageManager)hints.get(
ImageProcessingHints.IMAGE_MANAGER);
final ImageSessionContext sessionContext = (ImageSessionContext)hints.get(
ImageProcessingHints.IMAGE_SESSION_CONTEXT);
boolean useEnhancedBridgeContext = (imageManager != null && sessionContext != null);
final BridgeContext ctx = (useEnhancedBridgeContext
? new GenericFOPBridgeContext(ua, null, imageManager, sessionContext)
: new BridgeContext(ua));
Document doc = svg.getDocument();
//Cloning SVG DOM as Batik attaches non-thread-safe facilities (like the CSS engine)
//to it.
Document clonedDoc = BatikUtil.cloneSVGDocument(doc);
//Build the GVT tree
final GraphicsNode root;
try {
root = builder.build(ctx, clonedDoc);
} catch (Exception e) {
throw new ImageException("GVT tree could not be built for SVG graphic", e);
}
//Create the painter
int width = svg.getSize().getWidthMpt();
int height = svg.getSize().getHeightMpt();
Dimension imageSize = new Dimension(width, height);
Graphics2DImagePainter painter = createPainter(ctx, root, imageSize);
//Create g2d image
ImageInfo imageInfo = src.getInfo();