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 Ri...@umapinc.com on 2003/03/25 17:40:11 UTC

RE: AW: svg element's bounding box error

First of all, thanks Thomas for your reply.

I am using SVGLocatable to get the bounding box of the svg elements as 
follows:


String parser = XMLResourceDescriptor.getXMLParserClassName(); 
SAXSVGDocumentFactory f = new SAXSVGDocumentFactory(parser);
Document doc = f.createDocument(uri);                //******** create 
document from svg file
 
Element mainmap = doc.getElementById("mainmap");
NodeList svgNodes = mainmap.getElementsByTagName("path"); //******** 
retrieve needed svg elements from the document
for (int i=0; i<svgNodes.getLength(); i++) 
{
        Element subElemNode = (Element)svgNodes.item(i);// Get element
                if (subElemNode instanceof SVGLocatable)
         {
                        SVGRect bb = 
((SVGLocatable)subElemNode).getBBox(); //********** get the bounding box 
of the elements
                System.out.println(bb.getX());     //*********** ---> Line 
45 
                 } 
 } 


When I print the Id of the elements it shows the correct output but when I 
get the BBox of that element and try to print it out it gives the 
following run-time error(line45):

exception: null
java.lang.NullPointerException
        at org.apache.batik.dom.svg.SVGLocatableSupport$1.getX(Unknown 
Source)
        at CreateSpatial.main(CreateSpatial.java:45)

Any tips on this??

Thanks,
Riyaz.



*********************
Hi Jan, Riyaz,

   So the first question to ask when you say 'bounding box' is the
bounding box in what coordinate system?  And how important is it that
the bounding box be 'tight'?

   So Jan gives one way of doing it which seems to try and get the
bounds in screen coords, (but the code assumes there are no transforms
between the element and the root of the SVG tree - this can be fixed
by including the results of node.getGlobalTransform()).

   You could also just use the SVGLocatable interface on the DOM
Element (see the SVG specification).  This would generally be
preferable but if you need tight bounds in any coordinate system other
than the user coordinate system you will have to use the various
GraphicsNode methods (like getTransformedBounds).

RE: AW: svg element's bounding box error

Posted by Thomas E Deweese <th...@kodak.com>.
>>>>> "R" == Riyaz  <Ri...@umapinc.com> writes:

R> First of all, thanks Thomas for your reply.  I am using
R> SVGLocatable to get the bounding box of the svg elements as
R> follows:

   You need to use the bridge package (GVTBuilder) to build the
rendering tree.  Without the rendering tree Batik doesn't have any
geometrical information available.  Remember to tell the bridge to
treat the document as dynamic (otherwise the rendering tree will be
created independent of the DOM tree).

R> String parser = XMLResourceDescriptor.getXMLParserClassName();
R> SAXSVGDocumentFactory f = new SAXSVGDocumentFactory(parser);
R> Document doc = f.createDocument(uri); //******** create document
R> from svg file
 
R> Element mainmap = doc.getElementById("mainmap"); NodeList svgNodes
R> = mainmap.getElementsByTagName("path"); //******** retrieve needed
R> svg elements from the document for (int i=0;
R> i<svgNodes.getLength(); i++) { Element subElemNode =
R> (Element)svgNodes.item(i);// Get element if (subElemNode instanceof
R> SVGLocatable) { SVGRect bb = ((SVGLocatable)subElemNode).getBBox();
R> //********** get the bounding box of the elements
R> System.out.println(bb.getX()); //*********** ---> Line 45 } }


R> When I print the Id of the elements it shows the correct output but
R> when I get the BBox of that element and try to print it out it
R> gives the following run-time error(line45):

R> exception: null java.lang.NullPointerException at
R> org.apache.batik.dom.svg.SVGLocatableSupport$1.getX(Unknown Source)
R> at CreateSpatial.main(CreateSpatial.java:45)

R> Any tips on this??

R> Thanks, Riyaz.



R> ********************* Hi Jan, Riyaz,

R>    So the first question to ask when you say 'bounding box' is the
R> bounding box in what coordinate system?  And how important is it
R> that the bounding box be 'tight'?

R>    So Jan gives one way of doing it which seems to try and get the
R> bounds in screen coords, (but the code assumes there are no
R> transforms between the element and the root of the SVG tree - this
R> can be fixed by including the results of
R> node.getGlobalTransform()).

R>    You could also just use the SVGLocatable interface on the DOM
R> Element (see the SVG specification).  This would generally be
R> preferable but if you need tight bounds in any coordinate system
R> other than the user coordinate system you will have to use the
R> various GraphicsNode methods (like getTransformedBounds).



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