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 David Eason <he...@webneed.net> on 2002/03/21 00:07:58 UTC

createElementNS and and getComputedTextLength()

Hello!

I took a look at the link below which has some code for generating a rect
element:
http://xml.apache.org/batik/domapi.html

// create the rectangle
Element rectangle = doc.createElementNS(svgNS, "rect");
rectangle.setAttributeNS(null, "x", "10");
rectangle.setAttributeNS(null, "y", "20");
rectangle.setAttributeNS(null, "width", "100");
rectangle.setAttributeNS(null, "height", "50");
rectangle.setAttributeNS(null, "style", "fill:red");

My question is do I use createElementNS to create an SVG text element, or is
that what createTextNode is for?

In fact, I assumed the later since createElementNS returns an Element, and I
was not able to figure out how to to set the text that goes in between
<text> and </text>. However, neither Element nor Text has the crucial method
getComputedTextLength(), which I believe I need to get the width of the
string in pixels. Hence in my code below you will see a "dummy" cast of Text
to SVGOMTextContentElement, which of course causes an exception. In fact,
SVGOMTextContentElement is abstract anyway, I was unable to find anything
concrete that contains getComputedTextLength(). In fact now I believe I must
have misunderstood the purpose of the getComputedTextLength() method, but I
still have this crazy dream of outputting a non-constant string of text, and
I hope someone smarter than me will be happy to steer me in the right
direction! I joined this list yesterday just so I could ask this question,
and hopefully I will be able to help someone else out too one day. Thanks in
advance for a reply.


This will show you what I am trying to do:

    public int stringWidth(String name)
    {
      DOMImplementation impl = SVGDOMImplementation.getDOMImplementation();
      Document doc = impl.createDocument("http://www.w3.org/2000/svg",
"svg", null);
      org.w3c.dom.Text someText = doc.createTextNode(name);
      /* flag and avold "not initialized" error */
      float someTextLen = -1;
      try
      /* bad cast - want to cast Text to an object with
getComputedTextLength() */
      {
        someTextLen = ((SVGOMTextContentElement)
someText).getComputedTextLength();
      }
      catch (java.lang.ClassCastException CastawayEx)
      {
        System.out.println("org.w3c.dom.Text cannot be cast " +
            "to org.apache.batik.dom.svg.SVGOMTextContentElement");
      }
      return (int) someTextLen;

    }

Thanks in advance,

Dave







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


RE: createElementNS and and getComputedTextLength()

Posted by David Eason <he...@webneed.net>.
Thanks for the clarification and workarounds. I will pursue both of these
strategies
to see which one meets my needs.

Dave

-----Original Message-----
From: Thomas E Deweese [mailto:thomas.deweese@kodak.com]
Sent: Thursday, March 21, 2002 7:18 AM
To: Batik Users
Subject: RE: createElementNS and <text> and getComputedTextLength()


>>>>> "TK" == Thierry Kormann <tk...@ilog.fr> writes:

>> However, neither Element nor Text has the crucial method
>> getComputedTextLength(), which I believe I need to get the width of
>> the string in pixels. Hence in my code below you will see a "dummy"
>> cast of Text

   [...]

TK> 1. it's not supported in batik yet
TK> 2. that's not so easy to implement (we have to nearly render the
TK> text element to give you access to that information. That means
TK> that all the font family per character, glyph, ligatures... must
TK> be resolved at build time !)

    Yup, it's hard...

    Might I suggest using the filter hack I mentioned a few days ago.
You can construct a filter using ObjectBounding Box an FeFlood to do
simple backgrounds for text...

    Hope this helps.

---------------------------------------------------------------------
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


RE: createElementNS and and getComputedTextLength()

Posted by Thomas E Deweese <th...@kodak.com>.
>>>>> "TK" == Thierry Kormann <tk...@ilog.fr> writes:

>> However, neither Element nor Text has the crucial method
>> getComputedTextLength(), which I believe I need to get the width of
>> the string in pixels. Hence in my code below you will see a "dummy"
>> cast of Text

   [...]

TK> 1. it's not supported in batik yet 
TK> 2. that's not so easy to implement (we have to nearly render the
TK> text element to give you access to that information. That means
TK> that all the font family per character, glyph, ligatures... must
TK> be resolved at build time !)

    Yup, it's hard...

    Might I suggest using the filter hack I mentioned a few days ago.
You can construct a filter using ObjectBounding Box an FeFlood to do
simple backgrounds for text...

    Hope this helps.

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


RE: createElementNS and and getComputedTextLength()

Posted by David Eason <he...@webneed.net>.
Looked through the code you suggested and also JSVGComponent.java. Just want
to clarify my understanding of the bridge workaround to get a text string
pixel width. Tell me where I am right and wrong in my assumptions. I am
currently assuming the following:

1.   that I need to use the constructor BridgeContext(UserAgent userAgent)

2.   that I therefore need to write a UserAgent, which I will try to cobble
together by studying JSVGComponent.java and ImageTranscoder.java UserAgent
inner classes.

3.   that I need to put in something along the lines of the following:

  DOMImplementation impl = SVGDOMImplementation.getDOMImplementation();
  Document doc = impl.createDocument(SVGDOMImplementation.SVG_NAMESPACE_URI,
"svg", null);
  // do I need a Document to use the bridge for the specific purpose of
getting
  // the pixel width for a text node?
  org.w3c.dom.Element textEl = doc.createElementNS(svgNS, "text");
  org.w3c.dom.Text someText = doc.createTextNode("Hello world");
  GVTBuilder builder = new GVTBuilder();
  BridgeContext ctx = new BridgeContext(new MyUserAgent());
  GraphicsNode gvtRoot = builder.build(ctx, textEl);
  // or GraphicsNode gvtRoot builder.build(ctx, SVGDocument(doc));
  int stringWidth = gvtRoot.getBounds().getWidth();
  // or traverse to TextNode from gvtRoot (how?) and apply
getBounds().getWidth from there

Any reality check for my assumptions will be most appreciated!

Another workaround that occurred to me is just to use a monospaced font for
now. However I was wondering if I was anywhere close to being on the right
track.

Thanks in advance.

Dave


-----Original Message-----
From: Thierry Kormann [mailto:tkormann@ilog.fr]
Sent: Thursday, March 21, 2002 3:39 AM
To: Batik Users
Subject: RE: createElementNS and <text> and getComputedTextLength()


As a workaround, you can try to build the <text> element, using the bridge
module (see GVTBuilder and the ImageTranscoder for example to see how it
works) - then get the gvt.TextNode (the graphic representation of your
<text> node) and you will have access to its bounds...

I will try to implement getComputedTextLength ASAP when we will work on the
SVG DOM.

Thierry.


---------------------------------------------------------------------
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


RE: createElementNS and and getComputedTextLength()

Posted by Thierry Kormann <tk...@ilog.fr>.
> My question is do I use createElementNS to create an SVG text
> element, or is
> that what createTextNode is for?

You have to use both.
createElementNS to create the <text> element
createTextNode to create the content of the text node (the real text)
you have to add the TextNode as a child of the <text> Element.

Just use the DOM as you write your XML
<text>bla</text> means bla is a TextNode that is a child of the <text>
element.

> However, neither Element nor Text has the
> crucial method
> getComputedTextLength(), which I believe I need to get the width of the
> string in pixels. Hence in my code below you will see a "dummy"
> cast of Text

I understand the need but it's really tricky to answer that question.
First, you will find no method on DOM Core to get that information - you
will have to use the SVG DOM. The idea is to cast your <text> Element to an
SVGTextContentElement and will get the getComputedTextLength() method.

The problem is that:

1. it's not supported in batik yet
2. that's not so easy to implement (we have to nearly render the text
element to give you access to that information. That means that all the font
family per character, glyph, ligatures... must be resolved at build time !)

--

As a workaround, you can try to build the <text> element, using the bridge
module (see GVTBuilder and the ImageTranscoder for example to see how it
works) - then get the gvt.TextNode (the graphic representation of your
<text> node) and you will have access to its bounds...

I will try to implement getComputedTextLength ASAP when we will work on the
SVG DOM.

Thierry.


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