Hi Rob,

[EMAIL PROTECTED] wrote:

Hello, I tried to include a viewbox-attribute when exporting a Graphics2D to SVG using batik.

I tried to include a viewbox via the batik-examples as given on the website (and as suggested in above links), using the given StyleSheetStyleHandler:
[...]
        /** According to [EMAIL PROTECTED] do setPrecision(int precision)
         * to bypass the otherwise occurring java.lang.NullPointerException
         * at 
org.apache.batik.svggen.SVGGeneratorContext.doubleString(SVGGeneratorContext.java:420)
         * due to "a bug in doubleString where we did not initialize the DecimalFormat 
object".
         **/
        ctx.setPrecision(3); // 3 is SVG-default as mentioned in 
SVGGeneratorContext.java.

This bug has been fixed for a while so you shouldn't have to do this (although it doesn't hurt much).

//something to set size of xml-document: ctx.setComment("Generated by The Visual Language with Batik SVG Generator");
// Create an instance of the SVG Generator with context and document
SVGGraphics2D svg2d = new SVGGraphics2D(ctx, false);
// add a style sheet to the definition section
Element root = svg2d.getRoot();
Element defs = root.getElementById(SVGSyntax.ID_PREFIX_GENERIC_DEFS);

getElementById is also on the org.w3c.dom.svg.SVGSVGElement interface, which is the real type of 'root'.

       Element style = 
document.createElementNS(SVGSyntax.SVG_NAMESPACE_URI,SVGSyntax.SVG_STYLE_TAG);
       style.setAttributeNS(null, SVGSyntax.SVG_TYPE_ATTRIBUTE, "text/css");
       style.setAttributeNS(null, "viewBox", "0, 0, 600, 400" );

This should probably be 'root' that you set 'viewBox' on not the 'style' element.

       style.appendChild(styleSheet);
       defs.appendChild(style);

There is no real need for 'style' to be a child of defs, I normally stick under the root SVG element ('root' in your code).

But the compiler (in JDK 1.5) doesn't accept:

root.getElementById(SVGSyntax.ID_PREFIX_GENERIC_DEFS);

(the method belongs to Document, not Element...)

How can I proceed ...?

See above.


--------------------------------------------------------------------- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]



Reply via email to