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 th...@kodak.com on 2008/02/06 01:50:38 UTC

Re: How to get SVGOMTextElement text length?

Hi Qian,

"Qian Li" <qi...@googlemail.com> wrote on 12/21/2007 09:36:02 AM:

> (SVGOMTextElement) basicText = (SVGOMTextElement)
> jSVGCanvas.getSVGDocument().createElementNS(svgNS,
> SVGConstants.SVG_TEXT_TAG);
> 
> Do you think it's correct? 

   You don't append 'basicText' to the document.  So 
it won't have a bbox.  Also...

> However getBBox method still give me null.
> I think I didn't boot the CSS and SVG DOM correctly. Would you please
> explain more or give me some more completed sample code of booting the
> CSS and SVG DOM's.

    Well the code appears to be trying to have the canvas
boot the CSS and SVG DOM.  If you want to do that you need 
to wait until the canvas was done booting the CSS and SVG DOM's,
since it is asynchronous.  So you would need to attach a listener 
for the onload event, and query the bbox in the callback.

    Unless you really want to display the text in the canvas
you are much better off following the link I provided and
using that synchronous code to boot the SVG/CSS DOM for you.

> On Dec 19, 2007 10:50 AM,  <th...@kodak.com> wrote:
> >
> > Hi Qian,
> >
> > "Qian Li" <qi...@googlemail.com> wrote on 12/13/2007 09:32:35 AM:
> >
> >
> >  > I have a SVGOMTextElement, I want to know how to get the text 
length.
> >  > I have tried both getBBox() and getComputedTextLength(), they both
> >  > have null values. Would anyone please tell me if I should set 
anything
> >  > before using these methods?
> >
> >    You can't get information on text element's like this
> > independent of a document.  So you should use the
> > batik.dom.svg.SVGDOMImplementation to create your Document.
> > Then use that document to create your text elements
> > (createElementNS).  You must append the text elements to
> > the document (appendChild).
> >
> >    Then before calling getBBox you must boot the CSS and SVG
> > DOM's:
> >         http://wiki.apache.org/xmlgraphics-batik/BootSvgAndCssDom
> >
> >
> > >
> >  > Thanks very much.
> >  > Qian
> >  >
> >  > Here is my code:
> >  > -----------------------------------------------------
> >  > SVGOMTextElement basicText = new SVGOMTextElement(null, svgDocument 
);
> >  > basicText.setTextContent(text);
> >  >
> >  > System.out.println("[SVGText::setText]  BBox 
="+basicText.getBBox());
> >  > System.out.println("[SVGText::setText]  text
> >  > length="+basicText.getComputedTextLength());
> >  > ------------------------------------------------------
> >  >
> >  > Here is the exceptions
> >  > ------------------------------------------------------
> >  > [SVGText::setText]  BBox =null
> >  > null
> >  > java.lang.NullPointerException
> >  >         at org.apache.batik.dom.svg.SVGTextContentSupport.
> >  > getComputedTextLength(SVGTextContentSupport.java:240)
> >  >         at org.apache.batik.dom.svg.SVGOMTextContentElement.
> >  > getComputedTextLength(SVGOMTextContentElement.java:186)
> >  >         at com.gulfstream.software.constable.graphics.SVGText.
> >  > setText(SVGText.java:112)
> >  >         at com.gulfstream.software.constable.graphics.SVGText.
> >  > <init>(SVGText.java:52)
> >  >         at com.gulfstream.software.harris.plugins.AdditionPlugin.
> >  > renderSVG(AdditionPlugin.java:88)
> >  >         at com.gulfstream.software.constable.ide.MondrianSVGCanvas.
> >  > drop(MondrianSVGCanvas.java:264)
> >  >         at java.awt.dnd.DropTarget.drop(DropTarget.java:430)
> >  >         at sun.awt.dnd.SunDropTargetContextPeer.
> >  > processDropMessage(SunDropTargetContextPeer.java:500)
> >  >         at sun.awt.X11.XDropTargetContextPeer.
> >  > processDropMessage(XDropTargetContextPeer.java:165)
> >  >         at sun.awt.dnd.SunDropTargetContextPeer$EventDispatcher.
> >  > dispatchDropEvent(SunDropTargetContextPeer.java:812)
> >  >         at sun.awt.dnd.SunDropTargetContextPeer$EventDispatcher.
> >  > dispatchEvent(SunDropTargetContextPeer.java:736)
> >  >         at
> > sun.awt.dnd.SunDropTargetEvent.dispatch(SunDropTargetEvent.java:30)
> >  >         at 
java.awt.Component.dispatchEventImpl(Component.java:4267)
> >  >         at 
java.awt.Container.dispatchEventImpl(Container.java:2116)
> >  >         at java.awt.Component.dispatchEvent(Component.java:4240)
> >  >         at java.awt.LightweightDispatcher.
> >  > retargetMouseEvent(Container.java:4322)
> >  >         at java.awt.LightweightDispatcher.
> >  > processDropTargetEvent(Container.java:4057)
> >  >         at
> > java.awt.LightweightDispatcher.dispatchEvent(Container.java:3911)
> >  >         at 
java.awt.Container.dispatchEventImpl(Container.java:2102)
> >  >         at java.awt.Window.dispatchEventImpl(Window.java:2429)
> >  >         at java.awt.Component.dispatchEvent(Component.java:4240)
> >  >         at java.awt.EventQueue.dispatchEvent(EventQueue.java:599)
> >  >         at java.awt.EventDispatchThread.
> >  > pumpOneEventForFilters(EventDispatchThread.java:273)
> >  >         at java.awt.EventDispatchThread.
> >  > pumpEventsForFilter(EventDispatchThread.java:183)
> >  >         at java.awt.EventDispatchThread.
> >  > pumpEventsForHierarchy(EventDispatchThread.java:173)
> >  >         at java.awt.EventDispatchThread.
> >  > pumpEvents(EventDispatchThread.java:168)
> >  >         at java.awt.EventDispatchThread.
> >  > pumpEvents(EventDispatchThread.java:160)
> >  >         at 
java.awt.EventDispatchThread.run(EventDispatchThread.java:121)
> >  >
> >  > 
---------------------------------------------------------------------
> >  > 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
>