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: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to