'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 1:[1]
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);
Solution 2:[2]
The workaround I found for now is this:
Element tlg = svgGraphics.getTopLevelGroup();
tlg.setAttribute("shape-rendering","crispEdges");
svgGraphics.setTopLevelGroup(tlg);
The graphics render crisp now, though, as the code suggests, the attribute is applied as a style to the top level <g/>
element.
I look forward to seeing cleaner implementations.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | sigpwned |
Solution 2 | George Profenza |