Search code examples
javasvgbatik

How to set the shape rendering attribute to crispEdges using Apache Batik SVG 1.8


I'm trying to get Apache Batik 1.8 to output

shape-rendering="crispEdges"

instead of the default

shape-rendering="auto"

in the root <svg /> node.

I've spotted SVGConstants.SVG_SHAPE_RENDERING_ATTRIBUTE and SVGConstants#SVG_CRISP_EDGES_VALUE, but I'm unsure how to set these attributes to a SVGGraphics2D instance.

I've attempted something like suing a SVGGraphics2D(named SVGGraphics2D):

//svgGraphics.setRenderingHint(SVGRenderingHints.SVG_SHAPE_RENDERING_ATTRIBUTE,
//                             SVGRenderingHints.SVG_CRISP_EDGES_VALUE);

However it's java.awt.RenderingHints SVGGraphics2D.setRenderingHints expects.

I've also tried:

svgGraphics.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
    svgGraphics.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
    svgGraphics.setRenderingHint(RenderingHints.KEY_ALPHA_INTERPOLATION, RenderingHints.VALUE_ALPHA_INTERPOLATION_QUALITY);
    svgGraphics.setRenderingHint(RenderingHints.KEY_COLOR_RENDERING, RenderingHints.VALUE_COLOR_RENDER_QUALITY);

however none of the above change the render-style attribute to 'crispEdges' in the output SVG file.

(I'm looking into Document at the moment, hopefully I can manually change the attribute via XML/strings. I've also spotted a few related classes in the source code, but I can't seem to spot a good example/test)

What's the most elegant way to change render-style to crispEdges ?


Solution

  • I was able to get this working through the Graphics2D interface. Per Batik code, the following Graphics2D code adds shape-rendering="crispEdges" to SVG:

    g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_OFF);