xmlgraphics-batik-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Thomas E Deweese <thomas.dewe...@kodak.com>
Subject RE: getCharNumAtPosition() and transformations
Date Thu, 03 Apr 2003 14:59:55 GMT
>>>>> "RB" == Baron, Randy {PRG~Basel} <RANDY.BARON@Roche.COM> writes:

RB> Hi Thomas, Great!  Yes, that worked, thanks a lot.

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

    Thanks for posting the code snippit.  In return I thought I might
point out a few things that could be done a little better.  In
particular your code won't handle cases where the element is a tspan
or textPath you might want to try something like (this is in ECMA
Script):

  while ((tgt.namespaceURI == svgNS) && 
         ((tgt.localName == "tspan") ||
          (tgt.localName == "textPath"))) { 
      tgt = tgt.parentNode; 
      if (!tgt) return; 
  }

   To walk up the tree till you reach something that isn't a tspan or
a textPath element.

   Then you should replace:

     if (targ instanceof org.apache.batik.dom.svg.SVGOMTextElement)

   With:

     if (targ instanceof org.w3c.dom.svg.SVGTextElement)

   The W3C are the one's that are defining the interfaces you are
using, you really shouldn't use stuff that is specific to our
implementation (I don't think you are - but this makes sure :).

   Similarly you should use SVGDocument.createSVGPoint() rather than
directly instantiating a Batik SVGOMPoint (you can get the document
from any element).

   Anyways, hope this helps.

--- Original code ---

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

            }
        }
    }

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

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

BR> hi- I am using the getCharNumAtPosition() method from
BR> SVGOMTextContentElement
BR> 
RB> <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().

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

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

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

RB>     In particular the function 'mouseOver' does almost exactly
RB> 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?
 


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


RB> ---------------------------------------------------------------------
RB> To unsubscribe, e-mail: batik-users-unsubscribe@xml.apache.org For
RB> 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