xmlgraphics-batik-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Stefán Freyr Stefánsson <stef...@althingi.is>
Subject SVGLocatable.getBBox() difficulties
Date Thu, 04 Sep 2003 13:47:12 GMT
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

It was pointed out to me by Mr. Thomas DeWeese that I could use 
SVGLocatable.getBBox() to find the location and size of elements in a SVG 
document.

I did manage to get it to work... in a way... but I'm still having some 
problems and I'm hoping that someone can explain some things for me.

First off: Does anybody know where I can find JavaDoc for the org.w3c.dom.svg 
stuff?  There are JavaDoc pages for that package on the Batik web site but 
there is no text... just the skeleton html.

But now for what I've managed to do and the problems I'm running into:

I have the following SVG file:

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/
xlink" xmlns:embla="http://www.althingi.is/2003/embla" version="1" x="0" 
y="0" viewBox="0 0 2300 900">
    <defs>
        <g id="chair" style="stroke:black;stroke-width:1.5;">
            <rect width="400" height="400" rx="50" ry="50" x="0" y="0"/>
        </g>
    </defs>

    <g fill="none">
        <use xlink:href="#chair" x="50" y="50" embla:chair-id="1"/>
        <use xlink:href="#chair" x="500" y="150" embla:chair-id="2"/>
    </g>
</svg>

What I want to do is create dynamic layers so that I can add information on 
top of the "chairs" that are defined.  For this I need to pre-process the 
file and create a <g> element with <svg> elements that are defined "on top" 
of each chair.  So what I'll have is essentially:
<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/
xlink" xmlns:embla="http://www.althingi.is/2003/embla" version="1" x="0" 
y="0" viewBox="0 0 2300 900">
    <defs>
        <g id="chair" style="stroke:black;stroke-width:1.5;">
            <rect width="400" height="400" rx="50" ry="50" x="0" y="0"/>
        </g>
    </defs>

    <g fill="none">
        <use xlink:href="#chair" x="50" y="50" embla:chair-id="1"/>
        <use xlink:href="#chair" x="500" y="150" embla:chair-id="2"/>
    </g>

    <g embla:layer-id="voting-layer">
        <svg x="50" y="50" width="400" height="400">
            <!-- Here, the voting layer will put some stuff that it needs -->
        </svg>
        <svg x="500" y="150" width="400" height="400">
            <!-- Here, the voting layer will put some stuff that it needs -->
        </svg>
    </g>

    .... (more layers possibly) ...
</svg>

So I've been trying to parse the original file and get the <use> elements that 
have the "embla:chair-id" attribute in order to find out where I should put 
the <svg> elements in each of the layers.

Now, there is no problem finding the elements and all but I'm having problems 
working with the getBBox() method to get the correct location, width and 
height.

Here is a code snippet:
    ArrayList chairs = getAllChairElements( root );

    for( int i = 0; i < chairs.size(); i++ )
    {
        System.out.println( chairs.get( i ).getClass() );

        SVGLocatable element = (SVGLocatable) chairs.get( i );
        SVGRect bbox = element.getBBox();

        System.out.println( "\tUse Element Location: (" + 
                bbox.getX() + ", " + bbox.getY() + "); " +
                "Width: " + bbox.getWidth() + "; " +
                "Height: " + bbox.getHeight() );
    }

But here's what this code will write out:
class org.apache.batik.dom.svg.SVGOMUseElement
	Use Element Location: (-0.75, -0.75); Width: 401.5; Height: 401.5
class org.apache.batik.dom.svg.SVGOMUseElement
	Use Element Location: (-0.75, -0.75); Width: 401.5; Height: 401.5
class org.apache.batik.dom.svg.SVGOMUseElement
	Use Element Location: (-0.75, -0.75); Width: 401.5; Height: 401.5
class org.apache.batik.dom.svg.SVGOMUseElement
	Use Element Location: (-0.75, -0.75); Width: 401.5; Height: 401.5
class org.apache.batik.dom.svg.SVGOMUseElement
	Use Element Location: (-0.75, -0.75); Width: 401.5; Height: 401.5

So... can anybody help me out with this a bit?  I've managed to understand 
that the -0.75 is because that's the half of the line width... so a line 
width of 4 would result in (-2.0, -2.0) to be displayed... so this seems to 
be the "offset" from the actual defined location for x and y of the object.  
The height is basically correct... the rectangles are 401.5 in height and 
width.  But this is not what I'm looking for! :o( Can anybody help me find 
the actual location of the elements on the canvas?

Another thing: Is there any way of calculating this without actually having to 
draw this thing to screen?  Is there any "non-gui" version of this that 
actually renders the image without displaying it to the screen?  The reason I 
ask this is that this processing should be performed once in our system and 
should be done centrally... that is on a server that will then serve the 
produced image to the clients.  But I don't want to make the server have to 
have a window environment.

I hope someone can help.

Kind regards, Stefan Freyr.

p.s. sorry for the long post.
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.2 (GNU/Linux)

iQEVAwUBP1dCYL0ge6mq4AL2AQJhNQf/SctDf5Fft+INQHo/nMNmuOccfdwIm+c0
nvQ08p6hJD8tgRNRoaAzFzZUEg0kR1iDWFMG/Mrpve2srP5GQ6ggvrm1yxUPj/t8
AIl8QehZmJw+//GVoiJ6qENYdg4r8k4kIEFFnKGJ3eurRpPZXkmppEqS69Tf9hvg
r12JJvzEn5w+W3fGNKPIVS8w4qwkAUBLlPs/iDuLyqh2Z5rmn9CnjJaD2fffkSUD
9brTKcwMaRPHJj6pIBNpuRXu0pEyIw6p6ZVbew36U65bKy0pgG+iQqAYo67p4i8t
idHakIXBskjztgekF/d+HnCUhFZa9Hdci7AiJLjMIkjdt01zV0ZK1w==
=iJnJ
-----END PGP SIGNATURE-----


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


Mime
View raw message