Discussion:
SVGTextElement.getComputedTextLength()
Kristian Spangsege
2005-01-26 10:48:50 UTC
Permalink
Hi

According to the SVG conformance page, the following method is supported
by Batik:

SVGTextElement.getComputedTextLength()

Yet I have problems with it.
I would appreciate if someone could point out for me what I'm doing wrong.

I'm using Batik 1.5.1.

The Code:
----------------------------------------------------------------------

import org.w3c.dom.svg.*;
import org.apache.batik.dom.*;
import org.apache.batik.dom.svg.*;

public class Test
{
public static void main(String[] args)
{
SVGOMDocument doc =
(SVGOMDocument)SVGDOMImplementation.getDOMImplementation().createDocument(SVGDOMImplementation.SVG_NAMESPACE_URI,
"svg", null);

SVGTextElement text =
(SVGTextElement)doc.createElementNS(SVGDOMImplementation.SVG_NAMESPACE_URI,
"text");
text.appendChild(doc.createTextNode("foo"));

System.err.println(text.getComputedTextLength());
}
}

----------------------------------------------------------------------

I get this error:

----------------------------------------------------------------------

Exception in thread "main" java.lang.NullPointerException
at
org.apache.batik.dom.svg.SVGTextContentSupport.getComputedTextLength(Unknown
Source)
at
org.apache.batik.dom.svg.SVGOMTextContentElement.getComputedTextLength(Unknown
Source)
at Test.main(Test.java:14)

----------------------------------------------------------------------

Regards and thanks in advance,
Kristian Spangsege
Kristian Spangsege
2005-01-26 12:27:43 UTC
Permalink
Alright, I found a posting on this list, that explained the matter to
some extent:

http://mail-archives.apache.org/eyebrowse/ReadMsg?listName=batik-***@xml.apache.org&msgId=2030753

Still I'm left with a "chicken and egg" problem.

All I need to do is to create an SVG document with a piece of text at a
specific position and followed by a vertical separation line. Problem is
that I need to know the width of the text before I can place the
vertical seperation line.

It seems wierd to me, if I'm supposed to first add the separation line
at some random position, then fire up the DOM, and then calculate the
desired position of the separation line.

- Is there another method of inquiring on the length of a certain string
of characters?

- When the sole purpose is to generate an on-disk document, would it be
a resonable approach to actually fire up the DOM with an empty document
then construct the SVG elements as needed. I assume that in this case it
is legal to call getComputedTextLength() immediately after constructing
the SVGTextElement.

Regards and thanks in advance,
Kristian Spangsege
Post by Kristian Spangsege
Hi
According to the SVG conformance page, the following method is supported
SVGTextElement.getComputedTextLength()
Yet I have problems with it.
I would appreciate if someone could point out for me what I'm doing wrong.
I'm using Batik 1.5.1.
----------------------------------------------------------------------
import org.w3c.dom.svg.*;
import org.apache.batik.dom.*;
import org.apache.batik.dom.svg.*;
public class Test
{
public static void main(String[] args)
{
SVGOMDocument doc =
(SVGOMDocument)SVGDOMImplementation.getDOMImplementation().createDocument(SVGDOMImplementation.SVG_NAMESPACE_URI,
"svg", null);
SVGTextElement text =
(SVGTextElement)doc.createElementNS(SVGDOMImplementation.SVG_NAMESPACE_URI,
"text");
text.appendChild(doc.createTextNode("foo"));
System.err.println(text.getComputedTextLength());
}
}
----------------------------------------------------------------------
----------------------------------------------------------------------
Exception in thread "main" java.lang.NullPointerException
at
org.apache.batik.dom.svg.SVGTextContentSupport.getComputedTextLength(Unknown
Source)
at
org.apache.batik.dom.svg.SVGOMTextContentElement.getComputedTextLength(Unknown
Source)
at Test.main(Test.java:14)
----------------------------------------------------------------------
Regards and thanks in advance,
Kristian Spangsege
Cameron McCormack
2005-01-26 13:28:47 UTC
Permalink
Hi Kristian.
Post by Kristian Spangsege
Still I'm left with a "chicken and egg" problem.
All I need to do is to create an SVG document with a piece of text at a
specific position and followed by a vertical separation line. Problem is
that I need to know the width of the text before I can place the
vertical seperation line.
It seems wierd to me, if I'm supposed to first add the separation line
at some random position, then fire up the DOM, and then calculate the
desired position of the separation line.
Well no, you can create the document with the text element, build the
GVT tree, and then get the text size and add the line element.
Post by Kristian Spangsege
- Is there another method of inquiring on the length of a certain string
of characters?
You can use getBBox(), but of course, this also needs to be done after
the GVT tree has been built and only if the document is in the
ALWAYS_DYNAMIC state.
Post by Kristian Spangsege
- When the sole purpose is to generate an on-disk document, would it be
a resonable approach to actually fire up the DOM with an empty document
then construct the SVG elements as needed. I assume that in this case it
is legal to call getComputedTextLength() immediately after constructing
the SVGTextElement.
Yes, to get the dimensions of the text you will have to have the
graphics tree built. Starting with an empty SVG element or with a
document with your text element already in it shouldn't matter too much.
(Though there will be a very slight overhead when adding the text
element to the document whose graphics tree has already been built, but
nothing noticeable.)

Cameron
--
Cameron McCormack
| Web: http://mcc.id.au/
| ICQ: 26955922
Loading...