Re:Re: include viewbox in svg generation
Thanks for the fast reply, it made me ralise that the whole shebang of stylesheet etc (taken from the example) could be omitted. The main problem was that the output was written twice to the same file, doubling the headers, one with the viewbox set, the other not. The problem had to do with the order of calls. I had to take out the line that used to create the output svg2d.stream(outWriter, useCss); and move the root.setAttribute() to after I had rendered the java graphics to the SVGGraphics2D svg2d.Then the call svg2d.stream(root,outWriter, useCss); did what I wanted it to do: a squiggle-readable file reacting to resizing the viewing frame. relevant body of the method: // BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream(file)); // Get a DOMImplementation DOMImplementation domImpl = GenericDOMImplementation.getDOMImplementation(); // Create an instance of org.w3c.dom.Document Document document = domImpl.createDocument(null, svg, null); // set context (i.e parameters) of the document SVGGeneratorContext ctx = SVGGeneratorContext.createDefault(document); // Create an instance of the SVG Generator with context and document SVGGraphics2D svg2d = new SVGGraphics2D(ctx, false); // stream out SVG to the standard output using UTF-8 // character to byte encoding boolean useCSS = true; // we want to use CSS style attribute Writer outWriter = new OutputStreamWriter(out, UTF-8); // Ask the canvas to render into the SVG Graphics2D implementation canvas.paintSVG(svg2d); // adjust header to make resulting image resize with its 'viewbox' Element root= svg2d.getRoot(); root.setAttributeNS(null, viewBox, 0, 0,+ canvas.getWidth() + , + canvas.getHeight() ); // write this root with rest to out svg2d.stream(root,outWriter,useCSS); out.close(); } - To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
include viewbox in svg generation
Hello, I tried to include a viewbox-attribute when exporting a Graphics2D to SVG using batik. I found some hints in the archive when searching 'viewbox' http://koala.ilog.fr/batik/mlists/batik-users/archives/msg01038.html http://koala.ilog.fr/batik/mlists/batik-users/archives/msg01040.html http://koala.ilog.fr/batik/mlists/batik-users/archives/msg01041.html http://koala.ilog.fr/batik/mlists/batik-users/archives/msg01043.html http://www.mail-archive.com/[EMAIL PROTECTED]/msg05387.html http://www.mail-archive.com/[EMAIL PROTECTED]/msg05389.html but they did not help me that much further. 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: -begin copy public void createSVG( DrawingCanvas comp2SVG, File file) throws Exception { setCanvas(comp2SVG); //exception will be trhown by code below, has to be caught by user BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream(file)); // Get a DOMImplementation DOMImplementation domImpl = GenericDOMImplementation.getDOMImplementation(); // Create an instance of org.w3c.dom.Document // String svgNS = SVGSyntax.SVG_NAMESPACE_URI;//http://www.w3.org/2000/svg;//SVGDOMImplementation.SVG_NAMESPACE_URI; Document document = domImpl.createDocument(null, svg, null); // set context (i.e parameters) of the document SVGGeneratorContext ctx = SVGGeneratorContext.createDefault(document); CDATASection styleSheet = document.createCDATASection(); ctx.setStyleHandler(new StyleSheetStyleHandler(styleSheet)); /** 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. //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); 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 ); style.appendChild(styleSheet); defs.appendChild(style); // Finally, stream out SVG to the standard output using UTF-8 // character to byte encoding boolean useCSS = true; // we want to use CSS style attribute Writer outWriter = new OutputStreamWriter(out, UTF-8); // Ask the canvas to render into the SVG Graphics2D implementation //if(canvas==null) return; //would never happen because of setCanvas(); canvas.paintSVG(svg2d); canvas.repaint(); svg2d.stream(outWriter, useCSS); out.close(); } end class 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 ...? rob - To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
Re: include viewbox in svg generation
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]