You are viewing a plain text version of this content. The canonical link for it is here.
Posted to batik-users@xmlgraphics.apache.org by ra...@audiofarm.de on 2003/08/20 13:25:55 UTC

Odd ImageTranscoder behaviour with 'visibility' attributes

Hi gang, 

this is driving me nuts ;o( Once you have passed a SVG document to the
ImageTranscoder, modifying the 'visibility'-attributes has no impact
anymore on the transcoder output. It only has impact on the output when
you modify the attributes before passing the document object the first
time.

I know there are some points to remember when using JSVGCanvas [ see
http://koala.ilog.fr/batik/mlists/batik-users/archives/msg03051.html ],
however I can't seem to find the reason for this odd behaviour when
using the ImageTranscoder.

Here is a modified DOMRasterizer-Example taken from Batik's website to
illustrate the problem (see source comments for more information)

Any ideas or workarounds ?

ralf ...

-------------------------------------------------------------------------------------------
DOMRasterizer.java
-------------------------------------------------------------------------------------------

import java.io.FileOutputStream;
import java.io.OutputStream;

import org.apache.batik.dom.svg.SVGDOMImplementation;
import org.apache.batik.transcoder.TranscoderInput;
import org.apache.batik.transcoder.TranscoderOutput;
import org.apache.batik.transcoder.image.JPEGTranscoder;
import org.w3c.dom.DOMImplementation;
import org.w3c.dom.Document;
import org.w3c.dom.Element;

public class DOMRasterizer {

	public Document createDocument() {
		DOMImplementation impl = SVGDOMImplementation.getDOMImplementation();
		String svgNS = SVGDOMImplementation.SVG_NAMESPACE_URI;
		Document document = impl.createDocument(svgNS, "svg", null);
		Element root = document.getDocumentElement();
		root.setAttributeNS(null, "width", "450");
		root.setAttributeNS(null, "height", "500");

		Element e;
		e = document.createElementNS(svgNS, "rect");
		e.setAttributeNS(null, "id", "rect");
		e.setAttributeNS(null, "x", "10");
		e.setAttributeNS(null, "y", "10");
		e.setAttributeNS(null, "width", "200");
		e.setAttributeNS(null, "height", "300");
		e.setAttributeNS(null, "style",
"fill:red;stroke:black;stroke-width:4");
		root.appendChild(e);

		e = document.createElementNS(svgNS, "circle");
		e.setAttributeNS(null, "id", "circle");
		e.setAttributeNS(null, "cx", "225");
		e.setAttributeNS(null, "cy", "250");
		e.setAttributeNS(null, "r", "100");
		e.setAttributeNS(null, "style", "fill:green;fill-opacity:.5");
		root.appendChild(e);

		return document;
	}

	public void save(Document document, String filename) throws Exception {
		JPEGTranscoder t = new JPEGTranscoder();
		t.addTranscodingHint(JPEGTranscoder.KEY_QUALITY, new Float(.8));
		TranscoderInput input = new TranscoderInput(document);
		OutputStream ostream = new FileOutputStream(filename);
		TranscoderOutput output = new TranscoderOutput(ostream);
		t.transcode(input, output);
		ostream.flush();
		ostream.close();
	}

	public static void main(String[] args) throws Exception {
		
		DOMRasterizer rasterizer = new DOMRasterizer();
		Document document = rasterizer.createDocument();
		
		/*
		 * FIRST RENDERING
		 */		
		document.getElementById("rect").setAttribute("visibility", "visible");
		document.getElementById("circle").setAttribute("visibility",
"hidden");
		
		/* 
		 * This JPEG contains the rectangular shape only, which is fine
		 */
		rasterizer.save(document, "rect.jpg"); 
		
		/*
		 * SECOND RENDERING
		 */
		
		/* 
		 * ===============================================================
		 * PROBLEM: Setting the visibility-attributes has no impact if the
		 * SVG document was passed to the transcoder a *second* time
		 * ===============================================================
		 */
		document.getElementById("rect").setAttribute("visibility", "hidden");
		document.getElementById("circle").setAttribute("visibility",
"visible");
		
		// This JPEG contains the rectangular shape only, but it should show
the circle
		rasterizer.save(document, "circle.jpg");
		
		System.out.println("Ready");
		
		System.exit(0);
	}
}

-------------------------------------------------------------------------------------------

---------------------------------------------------------------------
To unsubscribe, e-mail: batik-users-unsubscribe@xml.apache.org
For additional commands, e-mail: batik-users-help@xml.apache.org


Re: Re: Odd ImageTranscoder behaviour with 'visibility' attributes

Posted by ra...@audiofarm.de.
> Fortunately the fix is very simple, add the following at:

Wow Thomas, this does the trick indeed :o)

Thanks a lot!

ralf ...

---------------------------------------------------------------------
To unsubscribe, e-mail: batik-users-unsubscribe@xml.apache.org
For additional commands, e-mail: batik-users-help@xml.apache.org


Re: Odd ImageTranscoder behaviour with 'visibility' attributes

Posted by Thomas DeWeese <Th...@Kodak.com>.
ralf@audiofarm.de wrote:
> Hi gang, 
> 
> this is driving me nuts ;o( Once you have passed a SVG document to the
> ImageTranscoder, modifying the 'visibility'-attributes has no impact
> anymore on the transcoder output. It only has impact on the output when
> you modify the attributes before passing the document object the first
> time.
> 
> I know there are some points to remember when using JSVGCanvas [ see
> http://koala.ilog.fr/batik/mlists/batik-users/archives/msg03051.html ],
> however I can't seem to find the reason for this odd behaviour when
> using the ImageTranscoder.
> 
> Here is a modified DOMRasterizer-Example taken from Batik's website to
> illustrate the problem (see source comments for more information)
> 
> Any ideas or workarounds ?

   The problem is that the Transcoder was not cleaning out the ViewCSS
it associated with the Document, and since it wasn't constructed in a dynamic
context the ViewCSS wasn't updated as the DOM changed (visibility is a CSS
property so the value comes from the ViewCSS).  Fortunately the fix is very
simple, add the following at:
  xml-batik/sources/org/apache/batik/transcoder/SVGAbstractTranscoder.java:299

    this.root = gvtRoot;
+  ctx.dispose
}

   This should be the last line of:
     protected void transcode(Document document,
                              String uri,
                              TranscoderOutput output)

   I'll commit this to CVS later tonight.





---------------------------------------------------------------------
To unsubscribe, e-mail: batik-users-unsubscribe@xml.apache.org
For additional commands, e-mail: batik-users-help@xml.apache.org