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 Urs Reupke <ur...@gmx.net> on 2004/09/08 10:43:32 UTC

SVGContext is null

Hi,
yesterday I hit upon a problem when trying to get the computed text length by calling SVGOMTextElement.getComputedTextLength() - a 
NullPointerException occured, which, as close scrutiny revealed, had it's origin in the missing SVGContext of the element.
Now, I am somewhat at a loss - I thought an elements context was implicitly set upon creation, no need for me to tamper with it, so 
I have no idea how to solve the problem. Do I need to set the svgContext manually?
The exception occurs in method breakText(SVGOMTextElement), which is called by afterLoad() - see code below.
Can you give me a pointer how to solve the problem?

Thanks a lot
-Urs

Code Excerpt:

private void breakText(SVGOMTextElement text) {
        float textLength=text.getComputedTextLength();    //This is where things go haywire
        if textLength<120{...}
        else if textLength>120 {...}
        ...}

protected void afterLoad() {
    SVGOMTextElement currentNode;
    this.graphic=canvas.getSVGDocument();
    NodeList textNodes = graphic.getElementsByTagName("text");
    int number=textNodes.getLength();
    for(int i = 0; i < number; i++) {
        currentNode=(SVGOMTextElement) textNodes.item(i);
        //Check whether the Text Element is in the right group.
        if (((SVGOMGElement)currentNode.getParentNode().getParentNode().getParentNode()).getAttribute("id").matches("cascade")) {
            addOnClick(currentNode); //adds an EventListener to handle "OnClick" events.
            breakText(currentNode); //breaks the text if it is to long
        }
    }
}


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


Re: SVGContext is null

Posted by Thomas DeWeese <Th...@Kodak.com>.
Hi Urs,

Urs Reupke wrote:

> I do not, however, understand exactly what I have done by creating the 
> BridgeContext and calling GVTBuilder.build -
> do you know about the details and could possibly explain what happens 
> within the program?
> Up to know, I thought GVT was an internal representation of
> my SVGs, one that I did neither need, nor was meant, to care about.

    What you have done is built the GVT tree for the SVG Document
(as well as doing all the CSS stuff). Without the GVT tree, which
handles all the rendering, we can't provide much of the SVG DOM
as most of it deals with the geometry.  When you ask for the bounding
box (or text length) from the DOM it calls over to the GVT tree
(through the SVGContext) to get the answer.

    We don't build the GVT as soon as you parse the document partly
because we need/want extra information (like the UserAgent), and
partly because there are cases where you might want to just use it as
Core DOM for a while before turning on SVGness (for example for use
with XSLT).

    In general you can just ignore the GVT tree, but if you want to
use SVG DOM methods you must construct it.


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


Re: SVGContext is null

Posted by Urs Reupke <ur...@gmx.net>.
Thank you Thomas,

>    No, but if you are loading a 'static' SVG document (no script
> tags or event attributes) then you need to tell Batik that you still
> want access to the SVGDOM (which is provided by the context objects).
> This can be done in the canvas with setDocumentState(ALWAYS_DYNAMIC),
> or if you are just loading the document yourself you need to use
> the GVTBuilder and the BridgeContext (the BridgeContext has a
> method 'setDynamicState' - which can take the value 'DYNAMIC').

Your advice - the second part - helped solving the problem.

I do not, however, understand exactly what I have done by 
creating the BridgeContext and calling GVTBuilder.build -
do you know about the details and could possibly explain 
what happens within the program?
Up to know, I thought GVT was an internal representation of
my SVGs, one that I did neither need, nor was meant, to care about.

Thanks again
-Urs

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


Re: SVGContext is null

Posted by Thomas DeWeese <Th...@Kodak.com>.
Hi Urs,

Urs Reupke wrote:

> yesterday I hit upon a problem when trying to get the computed text 
> length by calling SVGOMTextElement.getComputedTextLength() - a 
> NullPointerException occured, which, as close scrutiny revealed, had 
> it's origin in the missing SVGContext of the element.

> Now, I am somewhat at a loss - I thought an elements context was 
> implicitly set upon creation, no need for me to tamper with it, so I 
> have no idea how to solve the problem. Do I need to set the svgContext 
> manually?

    No, but if you are loading a 'static' SVG document (no script
tags or event attributes) then you need to tell Batik that you still
want access to the SVGDOM (which is provided by the context objects).
This can be done in the canvas with setDocumentState(ALWAYS_DYNAMIC),
or if you are just loading the document yourself you need to use
the GVTBuilder and the BridgeContext (the BridgeContext has a
method 'setDynamicState' - which can take the value 'DYNAMIC').

> The exception occurs in method breakText(SVGOMTextElement), which is 
> called by afterLoad() - see code below.
> Can you give me a pointer how to solve the problem?
> 
> Thanks a lot
> -Urs
> 
> Code Excerpt:
> 
> private void breakText(SVGOMTextElement text) {
>        float textLength=text.getComputedTextLength();    //This is where 
> things go haywire
>        if textLength<120{...}
>        else if textLength>120 {...}
>        ...}
> 
> protected void afterLoad() {
>    SVGOMTextElement currentNode;
>    this.graphic=canvas.getSVGDocument();
>    NodeList textNodes = graphic.getElementsByTagName("text");
>    int number=textNodes.getLength();
>    for(int i = 0; i < number; i++) {
>        currentNode=(SVGOMTextElement) textNodes.item(i);
>        //Check whether the Text Element is in the right group.
>        if 
> (((SVGOMGElement)currentNode.getParentNode().getParentNode().getParentNode()).getAttribute("id").matches("cascade")) 
> {
>            addOnClick(currentNode); //adds an EventListener to handle 
> "OnClick" events.
>            breakText(currentNode); //breaks the text if it is to long
>        }
>    }
> }
> 
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: batik-users-unsubscribe@xml.apache.org
> For additional commands, e-mail: batik-users-help@xml.apache.org
> 


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