xmlgraphics-batik-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Baron, Randy {PRG~Basel}" <RANDY.BA...@Roche.COM>
Subject RE: getCharNumAtPosition() and transformations
Date Thu, 03 Apr 2003 14:37:18 GMT
Hi Thomas,
   Great!  Yes, that worked, thanks a lot.

Code snippet looks something like this (in case someone has the same
problem):

 class OnMouseClick implements EventListener {
    public void handleEvent(Event evt) {
        int mx, my;
      // Get mouse click coordinates
        MouseEvent mevt = (MouseEvent) evt;
        mx = mevt.getClientX();
        my = mevt.getClientY();
        SVGElement targ = (SVGElement) mevt.getTarget();
        System.out.println("Mouse X:" + mx + " Y: " + my);
        System.out.println("   Type of event: " + mevt.getType());
        System.out.println("   getTarget: " + mevt.getTarget());
        System.out.println("   getCurrentTarget: "
                    + mevt.getCurrentTarget());
        System.out.println("   Element id = " + targ.getId());

     // if thing clicked on was a text element:
        if (targ instanceof org.apache.batik.dom.svg.SVGOMTextElement) {
            SVGOMTextElement texttarg = (SVGOMTextElement) targ;
     // Try to get character under the position where the mouse was
clicked.
            SVGOMPoint spoint;
            SVGPoint spointInv;
            spoint = new SVGOMPoint(mx, my);
            SVGMatrix svgMatrix = texttarg.getScreenCTM();
            SVGMatrix invMat = svgMatrix.inverse();
            System.out.println("point before getScreenCTM inversing: x =
" +
                        spoint.getX() + " y = " + spoint.getY());
            spointInv = spoint.matrixTransform(invMat);
            System.out.println("point after getScreenCTM inversing: x =
" +
                    spoint.getX() + " y = " + spointInv.getY());
            System.out.println("   try getCharNumAtPosition: " +
                    texttarg.getCharNumAtPosition(spointInv));

            }
        }
    }


-----Original Message-----
From: Thomas E Deweese [mailto:thomas.deweese@kodak.com] 
Sent: Wednesday, April 02, 2003 3:39 PM
To: Batik Users
Subject: RE: getCharNumAtPosition() and transformations

>>>>> "BR" == Baron, Randy {PRG~Basel} <RANDY.BARON@Roche.COM> writes:

BR> hi- I am using the getCharNumAtPosition() method from
BR> SVGOMTextContentElement
BR>
<file:///C:\JavaDev\Other%20Packages\BATIK\batik-docs-1.5beta5\docs\java
BR> doc\org\apache\batik\dom\svg\SVGOMTextContentElement.html> .  

BR> The user clicks on some text on the SVG display, the program gets
BR> the element clicked on from the target of the event, uses the
BR> MouseEvent's getClientX() and getClientY() to pass the point to
BR> getCharNumAtPosition(). 

    getClientX/Y return the mouse click in the viewport coordinate
system (essentially screen pixel locations).  You need to transform it
to the text element's coordinate system.

    This can be done by calling 'getScreenCTM()' on the text element
you want to call 'getCharNumAtPosition()' on, get the inverse of that
affine transform and apply that transform to the getClientX/Y point.

    Take a look at:
        xml-batik/samples/tests/spec/scripting/currentScaleTranslate.svg

    In particular the function 'mouseOver' does almost exactly what
you want.

BR> It works very well...up to a point.  I can get the index of a
BR> character at a given position in the string but if I zoom or pan
BR> or rotate the canvas, getCharNumAtPosition returns either a -1 or,
BR> in some cases, a number corresponding to a kind of virtual
BR> character that would have been on that location if the original
BR> display were still showing.  Does anyone know if this is the
BR> intended behavior?  Is it possible to have it continue finding the
BR> actual character that is clicked on after a transformation?
 


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


Mime
View raw message