* @param doc the SVG document
* @throws IOException In case of an I/O error while painting the image
*/
protected void renderSVGDocument(final RendererContext context,
final Document doc) throws IOException {
final RendererContextWrapper wrappedContext = RendererContext.wrapRendererContext(context);
int x = wrappedContext.getCurrentXPosition();
int y = wrappedContext.getCurrentYPosition();
//Prepare
SVGUserAgent ua = new SVGUserAgent(
context.getUserAgent().getSourcePixelUnitToMillimeter(),
new AffineTransform());
GVTBuilder builder = new GVTBuilder();
final BridgeContext ctx = new BridgeContext(ua);
//Build the GVT tree
final GraphicsNode root;
try {
root = builder.build(ctx, doc);
} catch (Exception e) {
log.error("SVG graphic could not be built: " + e.getMessage(), e);
return;
}
//Create the painter
Graphics2DImagePainter painter = new Graphics2DImagePainter() {
public void paint(Graphics2D g2d, Rectangle2D area) {
// If no viewbox is defined in the svg file, a viewbox of 100x100 is
// assumed, as defined in SVGUserAgent.getViewportSize()
float iw = (float) ctx.getDocumentSize().getWidth();
float ih = (float) ctx.getDocumentSize().getHeight();
float w = (float) area.getWidth();
float h = (float) area.getHeight();
g2d.scale(w / iw, h / ih);
root.paint(g2d);
}
public Dimension getImageSize() {
return new Dimension(wrappedContext.getWidth(), wrappedContext.getHeight());
}
};
//Let the painter paint the SVG on the Graphics2D instance
Graphics2DAdapter adapter = context.getRenderer().getGraphics2DAdapter();
adapter.paintImage(painter, context,
x, y, wrappedContext.getWidth(), wrappedContext.getHeight());
}