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 fireball <sa...@hotmail.com> on 2012/09/27 14:47:35 UTC

getBBox() returns null

I have svg as shown below. Whenever I load this svg and try to get BBox of
its text it returns null. How to properly get the BBox?

Element eltText = document.getElementById("id3");
SVGRect svgRect = ((SVGOMTextElement)eltText).getBBox(); <-- returns null


<svg
   xmlns="http://www.w3.org/2000/svg"
   version="1.1"
   id="id">
  <g
     id="id1"
     transform="matrix(1,0,0,1,0,0)">
    <rect
       id="id2"
       width="10"
       height="10"
       fill="none"
       stroke="#ffffff"/>
    <text
       id="id3"
       x="10"
       y="10"
       font-style="normal"
       text-decoration="none"
       font-weight="normal"
       font-size="12"
       font-family="none"
       fill="red">
       text</text>
  </g>
</svg>



--
View this message in context: http://batik.2283329.n4.nabble.com/getBBox-returns-null-tp4655273.html
Sent from the Batik - Users mailing list archive at Nabble.com.

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


Re: getBBox() returns null

Posted by fireball <sa...@hotmail.com>.
Thank you for your responses Thomas and Jonathan. Your feedback thought me
new ways of thinking...

I was trying to get the text height from BBox... so what I ended up doing is
use the text's font as the height. I asked one of the SVG gurus and they
recommended using the font.

Thanks again.

fireball.



--
View this message in context: http://batik.2283329.n4.nabble.com/getBBox-returns-null-tp4655273p4655279.html
Sent from the Batik - Users mailing list archive at Nabble.com.

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


Re: getBBox() returns null

Posted by jonathan wood <jo...@gmail.com>.
I'd also advise the changes be made in a single runnable as Thomas
suggested.  If you want a proof of concept for that, I'd point to some
rather old, but potentially useful code I wrote awhile ago...seems to be
doing something similar to what you want...look towards the end of the
conversation for the inlined example:

http://batik.2283329.n4.nabble.com/Status-of-FlowRoot-Overflow-Detection-td2979431.html


On Thu, Sep 27, 2012 at 6:04 PM, DeWeese Thomas <th...@gmail.com>wrote:

> Hi fireball,
>         I suggest that you add the content and then set the properties.
>
>         Otherwise the result of getBBox may well be wrong. There are many
> properties that affect how text is rendered that will be inherited where
> the element is inserted in the document, so any calculation you make
> outside of it's final place in the document has a decent chance of being
> wrong.
>         If you add it and set the properties in one Runnable (sent to the
> UpdateManager's RunnableQueue) then it won't be rendered until your
> runnable exits.
>
>         If you really need to do this outside of the document (and I
> strong recommend against it in general) you can check out the link Jonathan
> gave for booting the SVG & CSS Dom.  However once again I must point out
> that for that to give you useful results the CSS context must be the same
> as where it will eventually be inserted into the document.
>
>         Thomas
>
> On Sep 27, 2012, at 10:16 AM, fireball <sa...@hotmail.com> wrote:
>
> > I think I might know what is happening. Correct me if I am wrong.
> >
> > The BBox is computed after the text is rendered. And my understanding of
> > rendering is that it happens when the text element is added to the
> canvas.
> >
> > What I am trying to do here is load an SVG file into a document, set some
> > properties and then add it into the canvas. Since I am trying to set
> > properties before adding to canvas, BBox is not computed yet.
> >
> > Is there a way to do that before adding to canvas?
> >
> >
> >
> > --
> > View this message in context:
> http://batik.2283329.n4.nabble.com/getBBox-returns-null-tp4655273p4655276.html
> > Sent from the Batik - Users mailing list archive at Nabble.com.
> >
> > ---------------------------------------------------------------------
> > To unsubscribe, e-mail: batik-users-unsubscribe@xmlgraphics.apache.org
> > For additional commands, e-mail: batik-users-help@xmlgraphics.apache.org
> >
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: batik-users-unsubscribe@xmlgraphics.apache.org
> For additional commands, e-mail: batik-users-help@xmlgraphics.apache.org
>
>

Re: getBBox() returns null

Posted by DeWeese Thomas <th...@gmail.com>.
Hi fireball,
	I suggest that you add the content and then set the properties.

	Otherwise the result of getBBox may well be wrong. There are many properties that affect how text is rendered that will be inherited where the element is inserted in the document, so any calculation you make outside of it's final place in the document has a decent chance of being wrong.
	If you add it and set the properties in one Runnable (sent to the UpdateManager's RunnableQueue) then it won't be rendered until your runnable exits.

	If you really need to do this outside of the document (and I strong recommend against it in general) you can check out the link Jonathan gave for booting the SVG & CSS Dom.  However once again I must point out that for that to give you useful results the CSS context must be the same as where it will eventually be inserted into the document.

	Thomas

On Sep 27, 2012, at 10:16 AM, fireball <sa...@hotmail.com> wrote:

> I think I might know what is happening. Correct me if I am wrong.
> 
> The BBox is computed after the text is rendered. And my understanding of
> rendering is that it happens when the text element is added to the canvas. 
> 
> What I am trying to do here is load an SVG file into a document, set some
> properties and then add it into the canvas. Since I am trying to set
> properties before adding to canvas, BBox is not computed yet.
> 
> Is there a way to do that before adding to canvas?
> 
> 
> 
> --
> View this message in context: http://batik.2283329.n4.nabble.com/getBBox-returns-null-tp4655273p4655276.html
> Sent from the Batik - Users mailing list archive at Nabble.com.
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: batik-users-unsubscribe@xmlgraphics.apache.org
> For additional commands, e-mail: batik-users-help@xmlgraphics.apache.org
> 


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


Re: getBBox() returns null

Posted by fireball <sa...@hotmail.com>.
I think I might know what is happening. Correct me if I am wrong.

The BBox is computed after the text is rendered. And my understanding of
rendering is that it happens when the text element is added to the canvas. 

What I am trying to do here is load an SVG file into a document, set some
properties and then add it into the canvas. Since I am trying to set
properties before adding to canvas, BBox is not computed yet.

Is there a way to do that before adding to canvas?



--
View this message in context: http://batik.2283329.n4.nabble.com/getBBox-returns-null-tp4655273p4655276.html
Sent from the Batik - Users mailing list archive at Nabble.com.

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


Re: getBBox() returns null

Posted by fireball <sa...@hotmail.com>.
Thanks Jonathan for your reply.

I am using JSVGCanvas and it is set to ALWAYS_DYNAMIC.


...
JSVGCanvas svgCanvas = new JSVGCanvas();
svgCanvas.setDocumentState(JSVGCanvas.ALWAYS_DYNAMIC);
svgCanvas.setDocument(document);
svgCanvas.setDoubleBuffered(true);
svgCanvas.setDoubleBufferedRendering(true);
...



--
View this message in context: http://batik.2283329.n4.nabble.com/getBBox-returns-null-tp4655273p4655275.html
Sent from the Batik - Users mailing list archive at Nabble.com.

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


Re: getBBox() returns null

Posted by jonathan wood <jo...@gmail.com>.
If you are using JSVGCanvas, make sure you set the document state:

canvas.setDocumentState(JSVGCanvas.ALWAYS_DYNAMIC);
*
*
*
*If not using canvas, you'll need to boot the dom:

http://wiki.apache.org/xmlgraphics-batik/BootSvgAndCssDom



On Thu, Sep 27, 2012 at 8:47 AM, fireball <sa...@hotmail.com> wrote:

> I have svg as shown below. Whenever I load this svg and try to get BBox of
> its text it returns null. How to properly get the BBox?
>
> Element eltText = document.getElementById("id3");
> SVGRect svgRect = ((SVGOMTextElement)eltText).getBBox(); <-- returns null
>
>
> <svg
>    xmlns="http://www.w3.org/2000/svg"
>    version="1.1"
>    id="id">
>   <g
>      id="id1"
>      transform="matrix(1,0,0,1,0,0)">
>     <rect
>        id="id2"
>        width="10"
>        height="10"
>        fill="none"
>        stroke="#ffffff"/>
>     <text
>        id="id3"
>        x="10"
>        y="10"
>        font-style="normal"
>        text-decoration="none"
>        font-weight="normal"
>        font-size="12"
>        font-family="none"
>        fill="red">
>        text</text>
>   </g>
> </svg>
>
>
>
> --
> View this message in context:
> http://batik.2283329.n4.nabble.com/getBBox-returns-null-tp4655273.html
> Sent from the Batik - Users mailing list archive at Nabble.com.
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: batik-users-unsubscribe@xmlgraphics.apache.org
> For additional commands, e-mail: batik-users-help@xmlgraphics.apache.org
>
>