Just like to contribute to the forum...
        
Here's a class to take into account the parent elements with transforms. It
preConcatenates them all together into one which can then be applied to the
points of the element to get its true co-ordinates in the viewport.

If a parent doesnt have a transform then the default Identity transform is
used which has no effect (a bit like multiplying everything by 1, which
gives the same value)

   <svg>
    <g transform="A">
          <g transform="B">
            <g transform="C">
              <g transform="D">
                <!-- graphics elements go here -->
              </g>
            </g>
          </g>
        </g>
  </svg>
  
  This class will
  
  first look at the graphics element itself to see if it has a transform,
lets call this D
  
  then it will look at its parent, to see if that has a transform
  in this case it does, C
  
  therefore so far the resultant transform would be E x D
  the parent transform is *pre*concatenated to give E x D i.e. NOT
postconcatenated to give E x D
  
   and so on e.g. next parent up, is B so the resultant transform is C x D x
E
   
   and so on until A x B x C x D x E
   
   
   here's an example
         
        <g transform="translate(-10,-20)">
          <g transform="scale(2)">
            <g transform="rotate(45)">
              <g transform="translate(5,10)">
                <!-- graphics elements go here -->
              </g>
            </g>
          </g>
        </g>
        
        is equivalent to
        
        <g transform="translate(-10,-20) scale(2) rotate(45) translate(5,10)">
  <!-- graphics elements go here -->
</g>

The code collapses/bottom-up cascades through the elements.

See also: http://www.w3.org/TR/SVG/coords.html







import java.awt.geom.AffineTransform;




import org.w3c.dom.Element;




import org.apache.batik.parser.AWTTransformProducer;

public class SVGCombinedTransformProducer {


        
        
        
        public static AffineTransform getCurrentTransform( Element element )
          {

            // get parent 
            // if parent is <g> get transform, continue until parent svg
                
                Element currentElement = element;
            Element parentElement;
            AffineTransform currentAt = null, preConcatenatedAt = null;
            boolean svgElementReached = false;

            
            String transformString = null;
            
            System.out.println( "element: " + currentElement.getTagName() );
            
            if ( currentElement.getTagName() == "svg" )
            {
                  svgElementReached = true;
            }
            else // lets start off then
            {
                transformString = currentElement.getAttribute("transform");

                System.out.println( "transformString: " + transformString );
                
                
                if ( ( transformString != "" ) && ( transformString != null ) )
                {
                  preConcatenatedAt = 
AWTTransformProducer.createAffineTransform(
transformString );
                }
                else
                {
                        preConcatenatedAt = new AffineTransform();
                }
            }
            
            if ( preConcatenatedAt != null )
            {
            while ( !svgElementReached )
            {
              parentElement = 
(org.w3c.dom.Element)currentElement.getParentNode();
              currentElement = parentElement;
              
              System.out.println( "parentElement = " + 
parentElement.getTagName()
);
              
              if ( currentElement.getTagName() == "svg" )
              {
                svgElementReached = true;
              }
              else
              {
                transformString = currentElement.getAttribute("transform");
                
                System.out.println( "transformString = " + transformString );
              
                if ( ( transformString != "" ) && ( transformString != null ) )
                {
                  currentAt = AWTTransformProducer.createAffineTransform(
transformString );
                  
                preConcatenatedAt.preConcatenate( currentAt );
                }
              }
            }
            }
            
            if ( preConcatenatedAt == null ) preConcatenatedAt = new
AffineTransform();
             
            return preConcatenatedAt;
          }
        
        
}

-- 
View this message in context: 
http://www.nabble.com/Solution%3A-Code-to-combine-parent-elements-transform-applied-to-element-tp16032287p16032287.html
Sent from the Batik - Users mailing list archive at Nabble.com.


---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to