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 Egon Willighagen <eg...@subdimension.com> on 2000/12/06 08:09:17 UTC

Possible bug in Batik?

cc: to JChemPaint developers maillist


Hi all, 

i am new to this list, but like your libs very much.
I saw the announcement of beta 1 and directly integrated it
with JChemPaint, a 2D chemical editor (in Java):

   http://jchempaint.sourceforg.net/

But, unfortunetely, the generated SVG code does not render
in the Batik SVGViewer. 

The SVG source is in JChemPaint.java, of which fragments below. (The complete
code for this program can be found in the SourceForge CVS.)

Part of build.xml (for Ant):

<project name="JChemPaint" default="compile" basedir=".">
  <property name="build.compiler" value="jikes"/>
  <property name="classpath" 
value="/usr/local/jdk1.2/jre/lib/rt.jar:jar/aelfred.jar:jar/cml.jar:jar/compchem.jar:jar/jai_codec.jar:jar/jai_core.jar:jar/junit.jar:jar/sax.jar:jar/vecmath1.1-1.12.jar:jar/jmdraw.jar:jar/batik-awt-util.jar:jar/batik-util.jar:jar/batik-dom.jar:jar/xerces.jar" 
/>

  <target name="compile">
    <javac srcdir="." destdir="." classpath="${classpath}"
           optimize="on" deprecation="on"/>
  </target>
</project>

Fragments of JChemPaint.java:

 /* JChemPaint.java
 */
import org.apache.batik.util.awt.svg.SVGGraphics2D;
import org.apache.batik.dom.GenericDOMImplementation;
import org.w3c.dom.Document;
import org.w3c.dom.DOMImplementation;

                        if (filename.toLowerCase().endsWith("svg")) {
			    DOMImplementation domImpl = 
GenericDOMImplementation.getDOMImplementation();
			    Document document = domImpl.createDocument(null, "svg", null);
			    SVGGraphics2D svgGenerator = new SVGGraphics2D(document);
			    jcpf.jcpp.paint((Graphics)svgGenerator);
			    boolean useCSS = false;
			    try {
				Writer out = new OutputStreamWriter(new FileOutputStream(filename), 
"UTF-8");
				svgGenerator.stream(out, useCSS);
			    } catch (FileNotFoundException g) {
				System.out.println("File not found for SVG output: " + filename);
			    } catch (UnsupportedEncodingException f) {
				System.out.println("Encoding UTF-8 not supported.");
			    } catch (IOException h) {
				System.out.println("Error while writen SVG.");
			    }
			    return;
			}

/** end fragments **/

Note that jcpp is of class JChemPaintPanel with the procedure paint():
   	public void paint(Graphics g)

I guessed that since Graphics2D is subclassed from Graphics, this should work
as well.

Everything compiles and SVG code is generated. BUT the SVG code gives this 
error when rendered with: 	java -jar batik-svgviewer.jar

"The attribute 'xlink:href' on <image> can not reference elements within an 
SVG file"

The with JChemPaint generated SVG code is (2D structure of caffeine, which 
seemed appropriate):

<?xml version="1.0" encoding="UTF-8"?>

<!DOCTYPE svg PUBLIC '-//W3C//DTD SVG 20001102//EN' 
'http://www.w3.org/TR/2000/CR-SVG-20001102/DTD/svg-20001102.dtd'>
<svg fill-opacity="1" font-weight="normal" color-rendering="auto" 
stroke-miterlimit="10" stroke-linejoin="miter" fill="black" 
stroke-linecap="square" width="2147483647" image-rendering="auto" 
stroke-opacity="1" height="2147483647" stroke-dashoffset="0" font-size="12pt" 
stroke="black" text-rendering="auto" stroke-dasharray="none" 
font-family="&apos;Lucida Sans&apos;" color-interpolation="sRGB" 
shape-rendering="auto" stroke-width="1" font-style="normal">
  <!--Generated by the Java 2D API Graphics2D SVG Generator, Sun Microsystems 
Inc.-->
  <defs id="genericDefs" />
  <g>
    <g font-family="sans-serif">
      <image y="0" filter="none" clip-path="none" width="700" opacity="1" 
xlink:href="BufferedImage@602d307f: type = 1 DirectColorModel: rmask=ff0000 
gmask=ff00 bmask=ff amask=0 IntegerInterleavedRaster: width = 700 height = 
600 #Bands = 3 #DataElements 1 xOff = 0 yOff = 0 dataOffset[0] 0" 
height="600" x="0" />
    </g>
    <g font-family="sans-serif">
      <line fill="none" filter="none" clip-path="none" y1="115" y2="146" 
opacity="1" x1="198" x2="180" />
      <line fill="none" filter="none" clip-path="none" y1="117" y2="117" 
opacity="1" x1="198" x2="234" />
      <line fill="none" filter="none" clip-path="none" y1="112" y2="112" 
opacity="1" x1="198" x2="234" />
      <line fill="none" filter="none" clip-path="none" y1="115" y2="83" 
opacity="1" x1="198" x2="180" />
      <line fill="none" filter="none" clip-path="none" y1="146" y2="146" 
opacity="1" x1="180" x2="143" />
      <line fill="none" filter="none" clip-path="none" y1="150" y2="150" 
opacity="1" x1="147" x2="176" />
      <line fill="none" filter="none" clip-path="none" y1="146" y2="180" 
opacity="1" x1="180" x2="191" />
      <line fill="none" filter="none" clip-path="none" y1="146" y2="115" 
opacity="1" x1="143" x2="125" />
      <line fill="none" filter="none" clip-path="none" y1="146" y2="180" 
opacity="1" x1="143" x2="132" />
      <line fill="none" filter="none" clip-path="none" y1="115" y2="115" 
opacity="1" x1="125" x2="89" />
      <line fill="none" filter="none" clip-path="none" y1="115" y2="83" 
opacity="1" x1="125" x2="143" />
      <line fill="none" filter="none" clip-path="none" y1="82" y2="51" 
opacity="1" x1="146" x2="128" />
      <line fill="none" filter="none" clip-path="none" y1="85" y2="53" 
opacity="1" x1="141" x2="123" />
      <line fill="none" filter="none" clip-path="none" y1="83" y2="83" 
opacity="1" x1="143" x2="180" />
      <line fill="none" filter="none" clip-path="none" y1="83" y2="52" 
opacity="1" x1="180" x2="198" />
      <line fill="none" filter="none" clip-path="none" y1="180" y2="191" 
opacity="1" x1="191" x2="225" />
      <line fill="none" filter="none" clip-path="none" y1="180" y2="201" 
opacity="1" x1="191" x2="162" />
      <line fill="none" filter="none" clip-path="none" y1="201" y2="180" 
opacity="1" x1="162" x2="132" />
      <line fill="none" filter="none" clip-path="none" y1="178" y2="195" 
opacity="1" x1="138" x2="161" />
      <rect stroke="none" fill="white" clip-path="none" height="12" x="118" 
y="108" opacity="1" filter="none" width="12" />
      <text y="123" stroke="none" filter="none" clip-path="none" opacity="1" 
x="121">N
      </text>
      <rect stroke="none" fill="white" clip-path="none" height="12" x="172" 
y="77" opacity="1" filter="none" width="12" />
      <text y="91" stroke="none" filter="none" clip-path="none" opacity="1" 
x="176">N
      </text>
      <rect stroke="none" fill="white" clip-path="none" height="12" x="183" 
y="174" opacity="1" filter="none" width="12" />
      <text y="188" stroke="none" filter="none" clip-path="none" opacity="1" 
x="187">N
      </text>
      <rect stroke="none" fill="white" clip-path="none" height="12" x="125" 
y="174" opacity="1" filter="none" width="12" />
      <text y="188" stroke="none" filter="none" clip-path="none" opacity="1" 
x="128">N
      </text>
      <rect stroke="none" fill="white" clip-path="none" height="12" x="225" 
y="108" opacity="1" filter="none" width="12" />
      <text y="123" stroke="none" filter="none" clip-path="none" opacity="1" 
x="229">O
      </text>
      <rect stroke="none" fill="white" clip-path="none" height="12" x="116" 
y="46" opacity="1" filter="none" width="12" />
      <text y="60" stroke="none" filter="none" clip-path="none" opacity="1" 
x="120">O
      </text>
    </g>
  </g>
</svg>

I hope someone can help me with this problem.

Yours, 

Egon Willighagen

Re: [Jchempaint-devel] Possible bug in Batik?

Posted by Christoph Steinbeck <st...@ice.mpg.de>.
Thomas E Deweese wrote:
> 
> >>>>> "EW" == Egon Willighagen <eg...@subdimension.com> writes:
> 
> EW> cc: to JChemPaint developers maillist Hi all,
> 
> EW> i am new to this list, but like your libs very much.  I saw the
> EW> announcement of beta 1 and directly integrated it with JChemPaint,
> EW> a 2D chemical editor (in Java):
> 
> EW> But, unfortunetely, the generated SVG code does not render in the
> EW> Batik SVGViewer.
> 
> EW> The SVG source is in JChemPaint.java, of which fragments
> EW> below. (The complete code for this program can be found in the
> EW> SourceForge CVS.)
> 
> [...]
> 
> EW> Everything compiles and SVG code is generated. BUT the SVG code
> EW> gives this error when rendered with: java -jar batik-svgviewer.jar
> 
> EW> "The attribute 'xlink:href' on <image> can not reference elements
> EW> within an SVG file"
> 
> EW> The with JChemPaint generated SVG code is (2D structure of
> EW> caffeine, which seemed appropriate):
> 
>     The problem is that JChemPaint appears to draw an image as the
> first thing.  The default ImageHandler class for the SVGGraphics2D
> simply sets the xlink:href to the result of 'toString()' on the image
> being drawn (a fairly bogus thing to do :).  A more correct thing to
> do would be to write the image to a stream (using JPEG or PNG) then
> encode it as base64 data, and embed the result in the SVG file, then
> reference that in the href.
> 
>     We don't do that yet :)
> 
>     Even if we did do that I don't think you would really get what you
> are hoping for since the 'background' image would then be fixed at a
> particular resolution.  If this is what you want, then in all
> likelyhood, we will eventually do this for you.
> 
>     In your case though I'm guessing you will want to dig a bit deeper
> and try to capture with the SVGGraphics2D the sequence of drawing
> commands that are used to build the 'background' image as well.  If
> the background drawing code does not use Graphics2D (such as directly
> manipulating the contents of the BufferedImage), then you are probably
> out of luck as far as getting a 'pure' SVG file (meaning no embedded
> pixel data).
> 
>     Incidentally, if I comment out the <image [...] /> tag I do get
> the basic structure with The O's and N's and single and double bonds
> drawn in, however I'm guessing these are supposed to overlay a drawing
> of the atoms.

No, actually that's exactly what JChemPaint does. A line drawing of
single, double, triple bonds with, at least in this case, element
symbols of all heavy atoms except Carbon. So from, what you say, it
seems we are not so far away from our goal. 

Cheers, 

Chris

--
Dr. Christoph Steinbeck (http://www.ice.mpg.de/departments/ChemInf)
MPI of Chemical Ecology, Carl-Zeiss-Promenade 10, 07745 Jena, Germany
Tel: +49(0)3641 643644 - Fax: +49(0)3641 643665

What is man but that lofty spirit - that sense of enterprise. 
... Kirk, "I, Mudd," stardate 4513.3..

Possible bug in Batik?

Posted by Thomas E Deweese <th...@kodak.com>.
>>>>> "TD" == Thomas E Deweese <th...@kodak.com> writes:

>>>>> "EW" == Egon Willighagen <eg...@subdimension.com> writes:
EW> cc: to JChemPaint developers maillist Hi all,

EW> "The attribute 'xlink:href' on <image> can not reference elements
EW> within an SVG file"

EW> The with JChemPaint generated SVG code is (2D structure of
EW> caffeine, which seemed appropriate):

TD>     The problem is that JChemPaint appears to draw an image as the
TD> first thing.  The default ImageHandler class for the SVGGraphics2D
TD> simply sets the xlink:href to the result of 'toString()' on the
TD> image being drawn (a fairly bogus thing to do :).  A more correct
TD> thing to do would be to write the image to a stream (using JPEG or
TD> PNG) then encode it as base64 data, and embed the result in the
TD> SVG file, then reference that in the href.

TD>     We don't do that yet :)

    Ok, I'm a idot :)

    We do do this (well not quite but close enough).  What you want to
do is when you create the SVGGraphics2D provide either an
ImageHandlerJPEGEncoder or an ImageHandlerPNGEncoder:

  SVGGraphics2D svgGenerator = new SVGGraphics2D
    (document,
     new ImageHandlerPNGEncoder("directory/for/images", "url/for/dir/or null"),
     new DefaultExtensionHandler(), 
     false);

    But I suspect you do want to try and capture the drawing commands
to that image if possible, that will generate a "better" SVG file.

    Sorry about the temporary confusion, Vincent Hardy is the real
expert on this and I was trying to fill in for him.

    Hope this helps even more :)

Possible bug in Batik?

Posted by Thomas E Deweese <th...@kodak.com>.
>>>>> "EW" == Egon Willighagen <eg...@subdimension.com> writes:

EW> cc: to JChemPaint developers maillist Hi all,

EW> i am new to this list, but like your libs very much.  I saw the
EW> announcement of beta 1 and directly integrated it with JChemPaint,
EW> a 2D chemical editor (in Java):

EW> But, unfortunetely, the generated SVG code does not render in the
EW> Batik SVGViewer.

EW> The SVG source is in JChemPaint.java, of which fragments
EW> below. (The complete code for this program can be found in the
EW> SourceForge CVS.)

[...]

EW> Everything compiles and SVG code is generated. BUT the SVG code
EW> gives this error when rendered with: java -jar batik-svgviewer.jar

EW> "The attribute 'xlink:href' on <image> can not reference elements
EW> within an SVG file"

EW> The with JChemPaint generated SVG code is (2D structure of
EW> caffeine, which seemed appropriate):

    The problem is that JChemPaint appears to draw an image as the
first thing.  The default ImageHandler class for the SVGGraphics2D
simply sets the xlink:href to the result of 'toString()' on the image
being drawn (a fairly bogus thing to do :).  A more correct thing to
do would be to write the image to a stream (using JPEG or PNG) then
encode it as base64 data, and embed the result in the SVG file, then
reference that in the href.

    We don't do that yet :)

    Even if we did do that I don't think you would really get what you
are hoping for since the 'background' image would then be fixed at a
particular resolution.  If this is what you want, then in all
likelyhood, we will eventually do this for you.

    In your case though I'm guessing you will want to dig a bit deeper
and try to capture with the SVGGraphics2D the sequence of drawing
commands that are used to build the 'background' image as well.  If
the background drawing code does not use Graphics2D (such as directly
manipulating the contents of the BufferedImage), then you are probably
out of luck as far as getting a 'pure' SVG file (meaning no embedded
pixel data).

    Incidentally, if I comment out the <image [...] /> tag I do get
the basic structure with The O's and N's and single and double bonds
drawn in, however I'm guessing these are supposed to overlay a drawing
of the atoms.

    Hope this helps.