xmlgraphics-batik-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Nierop, P. van" <p.van.nie...@vu.nl>
Subject Problem using path objects (SVGOMPathElement) parsed from svg file
Date Thu, 22 Nov 2012 11:04:35 GMT
Hello,

I have only recently started using Batik and this is my first post to the Batik maillist.

I am experiencing problems working with paths. This step-by-step description gives you an
idea about what I am trying to achieve.

1. I read an exisiting svg file that contains a path element
2. Get path information
3. Draw shapes along the path

This is my svg file (named "basedrawing.svg", generated with inkscape, but modifed to remove
all clutter):
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<svg
   xmlns:dc="http://purl.org/dc/elements/1.1/"
   xmlns:cc="http://creativecommons.org/ns#"
   xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
   xmlns:svg="http://www.w3.org/2000/svg"
   xmlns="http://www.w3.org/2000/svg"
   >
    <path
        id="test"
        d="m 162.875,146.15625 -0.0312,1 c 162.80372,4.28431 255.5724,32.82885 307.625,60.28125
26.0263,13.7262 41.8838,27.18577 51.21875,37.1875 9.33495,10.00173 12.125,16.5 12.125,16.5
l 0.9375,-0.375 c 0,0 -2.9287,-6.72495 -12.34375,-16.8125 -9.4151,-10.08755 -25.35224,-23.6012
-51.4688,-37.375 C 418.70439,179.0149 325.78557,150.44337 162.875,146.15625 z"
    />
</svg>
This file is rendered correctly in the Firefox webbrowser.

My next goal is to read in this svg file and generate a object representation of the path
element (try to make it a valid SVGOMPathElement instance) This is the java code with which
I try to read the svg:

import java.io.File;
import java.io.IOException;
import java.net.URI;
import org.apache.batik.dom.svg.SAXSVGDocumentFactory;
import org.apache.batik.dom.svg.SVGOMPathElement;
import org.apache.batik.util.XMLResourceDescriptor;
import org.w3c.dom.Element;
import org.w3c.dom.svg.SVGDocument;

public class TestSVGDomParser {

    public static void main(String[] args) throws IOException {

        URI fileURI = new File("basedrawing.svg").toURI();

        String parser = XMLResourceDescriptor.getXMLParserClassName();
        SAXSVGDocumentFactory f = new SAXSVGDocumentFactory(parser);
        SVGDocument doc = (SVGDocument) f.createDocument( fileURI.toString() );

        // try to get a path using the unique identifier
        Element e = doc.getElementById("test");

        // cast to path
        SVGOMPathElement path = (SVGOMPathElement) e;

        // works fine
        System.out.println( path.getAttribute("d") );

        // does not work, throws a null pointer exception
        System.out.println( path.getTotalLength() );
    }

}
The problem I think is that the cast of the e (instance of Element) to path (instance of SVGOMPathElement)
does not trigger the internal interpretation of the 'd' attribute. I conclude from this that
I do not follow the proper procedures to build the object representation of the svg document.

Could someone advise me on how to proceed?

Thanks in advance,

Pim


--
*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-

   Pim van Nierop

   Center for Neurogenomics and Cognitive Research
   Faculty of Earth and Life Sciences
   Vrije Universiteit
   Amsterdam
   Email    pim.van.nierop@cncr.vu.nl
   Tel.     +31 (0)20 5987114
   Fax.     +31 (0)20 5987112

*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-


Mime
View raw message