public static Matrix linearGradientMatrix(ScalableGradientNode gradient,Rect pathBounds){
  FXGMatrix matrix=new FXGMatrix();
  MatrixNode mtxNode=gradient.getMatrixNode();
  if (mtxNode != null) {
    matrix.translate(GRADIENT_DIMENSION / 2.0,GRADIENT_DIMENSION / 2.0);
    matrix.scale(1.0 / GRADIENT_DIMENSION,1.0 / GRADIENT_DIMENSION);
    FXGMatrix nodeMatrix=new FXGMatrix(mtxNode);
    matrix.concat(nodeMatrix);
    return matrix.toSWFMatrix();
  }
  double width=(pathBounds.xMax - pathBounds.xMin) / (double)SwfConstants.TWIPS_PER_PIXEL;
  double height=(pathBounds.yMax - pathBounds.yMin) / (double)SwfConstants.TWIPS_PER_PIXEL;
  double scaleX=gradient.getScaleX();
  double rotation=gradient.getRotation();
  double tx=gradient.getX();
  double ty=gradient.getY();
  if (Double.isNaN(scaleX)) {
    if (rotation % 90 != 0) {
      double normalizedAngle=rotation % 360;
      if (normalizedAngle < 0)       normalizedAngle+=360;
      normalizedAngle%=180;
      if (normalizedAngle > 90)       normalizedAngle=180 - normalizedAngle;
      double side=width;
      double hypotenuse=Math.sqrt(width * width + height * height);
      double hypotenuseAngle=Math.acos(width / hypotenuse) * 180 / Math.PI;
      if (normalizedAngle > hypotenuseAngle) {
        normalizedAngle=90 - normalizedAngle;
        side=height;
      }
      scaleX=side / Math.cos(normalizedAngle / 180 * Math.PI);
    }
 else {
      scaleX=(rotation % 180) == 0 ? width : height;
    }
  }
  if (!Double.isNaN(tx) && Double.isNaN(ty))   ty=0;
  if (Double.isNaN(tx) && !Double.isNaN(ty))   tx=0;
  if (!Double.isNaN(tx) && !Double.isNaN(ty))   matrix.translate(SwfConstants.GRADIENT_SQUARE / (2.0 * SwfConstants.TWIPS_PER_PIXEL),SwfConstants.GRADIENT_SQUARE / (2.0 * SwfConstants.TWIPS_PER_PIXEL));
  if (Math.abs(scaleX) < 2)   scaleX=(scaleX < 0) ? -2 : 2;
  scaleX=(scaleX * SwfConstants.TWIPS_PER_PIXEL) / SwfConstants.GRADIENT_SQUARE;
  matrix.scale(scaleX,1);
  if (!Double.isNaN(rotation))   matrix.rotate(rotation);
  if (Double.isNaN(tx))   tx=width / 2.0 + pathBounds.xMin / (double)SwfConstants.TWIPS_PER_PIXEL;
  if (Double.isNaN(ty))   ty=height / 2.0 + +pathBounds.yMin / (double)SwfConstants.TWIPS_PER_PIXEL;
  matrix.translate(tx,ty);
  return matrix.toSWFMatrix();
}

public static Matrix linearGradientMatrix ( ScalableGradientNode gradient , Rect pathBounds ) { FXGMatrix matrix = new FXGMatrix ( ) ; MatrixNode mtxNode = gradient . getMatrixNode ( ) ; if ( mtxNode != null ) { matrix . translate ( GRADIENT_DIMENSION / 2.0 , GRADIENT_DIMENSION / 2.0 ) ; matrix . scale ( 1.0 / GRADIENT_DIMENSION , 1.0 / GRADIENT_DIMENSION ) ; FXGMatrix nodeMatrix = new FXGMatrix ( mtxNode ) ; matrix . concat ( nodeMatrix ) ; return matrix . toSWFMatrix ( ) ; } double width = ( pathBounds . xMax - pathBounds . xMin ) / ( double ) SwfConstants . TWIPS_PER_PIXEL ; double height = ( pathBounds . yMax - pathBounds . yMin ) / ( double ) SwfConstants . TWIPS_PER_PIXEL ; double scaleX = gradient . getScaleX ( ) ; double rotation = gradient . getRotation ( ) ; double tx = gradient . getX ( ) ; double ty = gradient . getY ( ) ; if ( Double . isNaN ( scaleX ) ) { if ( rotation % 90 != 0 ) { double normalizedAngle = rotation % 360 ; if ( normalizedAngle < 0 ) normalizedAngle += 360 ; normalizedAngle %= 180 ; if ( normalizedAngle > 90 ) normalizedAngle = 180 - normalizedAngle ; double side = width ; double hypotenuse = Math . sqrt ( width * width + height * height ) ; double hypotenuseAngle = Math . acos ( width / hypotenuse ) * 180 / Math . PI ; if ( normalizedAngle > hypotenuseAngle ) { normalizedAngle = 90 - normalizedAngle ; side = height ; } scaleX = side / Math . cos ( normalizedAngle / 180 * Math . PI ) ; } else { scaleX = ( rotation % 180 ) == 0 ? width : height ; } } if ( ! Double . isNaN ( tx ) && Double . isNaN ( ty ) ) ty = 0 ; if ( Double . isNaN ( tx ) && ! Double . isNaN ( ty ) ) tx = 0 ; if ( ! Double . isNaN ( tx ) && ! Double . isNaN ( ty ) ) matrix . translate ( SwfConstants . GRADIENT_SQUARE / ( 2.0 * SwfConstants . TWIPS_PER_PIXEL ) , SwfConstants . GRADIENT_SQUARE / ( 2.0 * SwfConstants . TWIPS_PER_PIXEL ) ) ; if ( Math . abs ( scaleX ) < 2 ) scaleX = ( scaleX < 0 ) ? - 2 : 2 ; scaleX = ( scaleX * SwfConstants . TWIPS_PER_PIXEL ) / SwfConstants . GRADIENT_SQUARE ; matrix . scale ( scaleX , 1 ) ; if ( ! Double . isNaN ( rotation ) ) matrix . rotate ( rotation ) ; if ( Double . isNaN ( tx ) ) tx = width / 2.0 + pathBounds . xMin / ( double ) SwfConstants . TWIPS_PER_PIXEL ; if ( Double . isNaN ( ty ) ) ty = height / 2.0 + + pathBounds . yMin / ( double ) SwfConstants . TWIPS_PER_PIXEL ; matrix . translate ( tx , ty ) ; return matrix . toSWFMatrix ( ) ; }
method to generate a transform matrix for the linear gradient strokes and fills . first the translation and scaling is applied and later the rotation is applied . the scaling applied is the user specified value or in its absence the dimensions of the geometry . this is multiplied by the quotient of the number or twips per pixel and the number of twips on the screen in a given orientation to get the correct scale fraction .
