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 Bibek Sahu <sc...@dodds.net> on 2003/12/30 22:58:38 UTC

Getting location of text in SVG

Hello all,

I'm wondering how one /should/ go about getting the position of a
character (e.g., in a TextNode).  Currently, I'm trying to use the
BridgeContext to get a Bridge for the node, then using
TextBridge.getStartPositionOfChar(int) to get its location.  If I do that,
though, I get a NullPointerException somewhere deep within the guts of
Batik:

        public void updateCaret()
        {
            SVGTextElementBridge textBridge = (SVGTextElementBridge) bridgeContext.getBridge(bridgeContext.getElement(caretMark.getTextNode()));

//            Rectangle2D charBounds = textBridge.getExtentOfChar(caretPosition);
            Point2D charStart = textBridge.getStartPositionOfChar(caretPosition);

            caretOverlay.setShape(new Line2D.Double(0, 0, charStart.getX(), charStart.getY()));
            svgEditor.repaint();
        }



results in:



java.lang.NullPointerException
        at org.apache.batik.bridge.SVGTextElementBridge.getStartPositionOfChar(SVGTextElementBridge.java:2316)
        at org.apache.batik.bridge.SVGTextElementBridge.getStartPositionOfChar(SVGTextElementBridge.java:2128)
        at org.globalcircle.svg.modehandlers.TextModeHandler$EditableTextSelector.updateCaret(TextModeHandler.java:132)
        at org.globalcircle.svg.modehandlers.TextModeHandler$EditableTextSelector.keyPressed(TextModeHandler.java:238)
        at org.globalcircle.svg.modehandlers.TextModeHandler$EditableTextSelectionManager$KeyListener.keyPressed(TextModeHandler.java:50)
        at org.apache.batik.gvt.event.AWTEventDispatcher.processKeyEvent(AWTEventDispatcher.java:550)
        at org.apache.batik.gvt.event.AWTEventDispatcher.dispatchKeyEvent(AWTEventDispatcher.java:369)
        at org.apache.batik.gvt.event.AWTEventDispatcher.dispatchEvent(AWTEventDispatcher.java:357)
        at org.apache.batik.gvt.event.AWTEventDispatcher.keyPressed(AWTEventDispatcher.java:220)
        at org.apache.batik.swing.svg.JSVGComponent$SVGListener$10.run(JSVGComponent.java:1758)
        at org.apache.batik.util.RunnableQueue.run(RunnableQueue.java:162)
        at java.lang.Thread.run(Thread.java:534)


(using Batik-1.5.1rc2; but the same thing happens with Batik-1.5).

Thank you for any and all help.  It is appreciated.


Sincerely,
Bob Sahu


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


Re: Getting location of text in SVG

Posted by Thomas DeWeese <Th...@Kodak.com>.
Bibek Sahu wrote:
> Hello all,
> 
> I'm wondering how one /should/ go about getting the position of a
> character (e.g., in a TextNode).  Currently, I'm trying to use the
> BridgeContext to get a Bridge for the node, then using
> TextBridge.getStartPositionOfChar(int) to get its location.  If I do that,
> though, I get a NullPointerException somewhere deep within the guts of
> Batik:

    The problem here is:

       Element e = bridgeContext.getElement(caretMark.getTextNode());
       SVGTextElementBridge textBridge = (SVGTextElementBridge)
                 bridgeContext.getBridge(e);

    The second line will get the 'prototype' bridge for text elements
(in a dynamic document this bridge is cloned for each text element).
For the rest of the code to work you need the Bridge that was actually
used to build that TextNode.  You could get that by using:

     SVGTextElementBridge textBridge = (SVGTextElementBridge)
	         ((SVGOMElement)e).getSVGContext();

    Of course it is much easier to just use the Text Content Element
DOM on the 'e' element:

      org.w3c.dom.svg.SVGTextContentElement tce =
                 (SVGTextContentElement)e;

      tce.getStartPositionOfChar(caretPosition);

     The origin of most of the methods in SVGTextElementBridge is
the DOM calls on SVGTextContentElement.

     So once again the DOM is really the easiest way to work with
this.  It also has the advantage of being 'standard' meaning that
your code would not be subject to the whims of the Batik
implementors :)


>         public void updateCaret()
>         {
>             
> 
> //            Rectangle2D charBounds = textBridge.getExtentOfChar(caretPosition);
>             Point2D charStart = textBridge.getStartPositionOfChar(caretPosition);
> 
>             caretOverlay.setShape(new Line2D.Double(0, 0, charStart.getX(), charStart.getY()));
>             svgEditor.repaint();
>         }
> 
> 
> 
> results in:
> 
> 
> 
> java.lang.NullPointerException
>         at org.apache.batik.bridge.SVGTextElementBridge.getStartPositionOfChar(SVGTextElementBridge.java:2316)
>         at org.apache.batik.bridge.SVGTextElementBridge.getStartPositionOfChar(SVGTextElementBridge.java:2128)
>         at org.globalcircle.svg.modehandlers.TextModeHandler$EditableTextSelector.updateCaret(TextModeHandler.java:132)
>         at org.globalcircle.svg.modehandlers.TextModeHandler$EditableTextSelector.keyPressed(TextModeHandler.java:238)
>         at org.globalcircle.svg.modehandlers.TextModeHandler$EditableTextSelectionManager$KeyListener.keyPressed(TextModeHandler.java:50)
>         at org.apache.batik.gvt.event.AWTEventDispatcher.processKeyEvent(AWTEventDispatcher.java:550)
>         at org.apache.batik.gvt.event.AWTEventDispatcher.dispatchKeyEvent(AWTEventDispatcher.java:369)
>         at org.apache.batik.gvt.event.AWTEventDispatcher.dispatchEvent(AWTEventDispatcher.java:357)
>         at org.apache.batik.gvt.event.AWTEventDispatcher.keyPressed(AWTEventDispatcher.java:220)
>         at org.apache.batik.swing.svg.JSVGComponent$SVGListener$10.run(JSVGComponent.java:1758)
>         at org.apache.batik.util.RunnableQueue.run(RunnableQueue.java:162)
>         at java.lang.Thread.run(Thread.java:534)
> 
> 
> (using Batik-1.5.1rc2; but the same thing happens with Batik-1.5).
> 
> Thank you for any and all help.  It is appreciated.
> 
> 
> Sincerely,
> Bob Sahu
> 
> 
> ---------------------------------------------------------------------
> 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