6842011: StackOverflowError printing landscape with scale and transform
authorpsadhukhan
Thu, 02 Jun 2016 15:20:31 +0530
changeset 39023 8b91cb9a723e
parent 39022 2ad6ba4c7174
child 39024 9c63f12138b7
6842011: StackOverflowError printing landscape with scale and transform Reviewed-by: prr, flar
jdk/src/java.desktop/share/classes/java/awt/geom/AffineTransform.java
jdk/src/java.desktop/share/classes/sun/print/PSPrinterJob.java
jdk/test/java/awt/print/PrinterJob/LandscapeStackOverflow.java
--- a/jdk/src/java.desktop/share/classes/java/awt/geom/AffineTransform.java	Thu Jun 02 15:16:07 2016 +0530
+++ b/jdk/src/java.desktop/share/classes/java/awt/geom/AffineTransform.java	Thu Jun 02 15:20:31 2016 +0530
@@ -1160,10 +1160,18 @@
     }
 
     /**
-     * Returns the X coordinate scaling element (m00) of the 3x3
-     * affine transformation matrix.
-     * @return a double value that is the X coordinate of the scaling
-     *  element of the affine transformation matrix.
+     * Returns the {@code m00} element of the 3x3 affine transformation matrix.
+     * This matrix factor determines how input X coordinates will affect output
+     * X coordinates and is one element of the scale of the transform.
+     * To measure the full amount by which X coordinates are stretched or
+     * contracted by this transform, use the following code:
+     * <pre>
+     *     Point2D p = new Point2D.Double(1, 0);
+     *     p = tx.deltaTransform(p, p);
+     *     double scaleX = p.distance(0, 0);
+     * </pre>
+     * @return a double value that is {@code m00} element of the
+     *         3x3 affine transformation matrix.
      * @see #getMatrix
      * @since 1.2
      */
@@ -1172,10 +1180,18 @@
     }
 
     /**
-     * Returns the Y coordinate scaling element (m11) of the 3x3
-     * affine transformation matrix.
-     * @return a double value that is the Y coordinate of the scaling
-     *  element of the affine transformation matrix.
+     * Returns the {@code m11} element of the 3x3 affine transformation matrix.
+     * This matrix factor determines how input Y coordinates will affect output
+     * Y coordinates and is one element of the scale of the transform.
+     * To measure the full amount by which Y coordinates are stretched or
+     * contracted by this transform, use the following code:
+     * <pre>
+     *     Point2D p = new Point2D.Double(0, 1);
+     *     p = tx.deltaTransform(p, p);
+     *     double scaleY = p.distance(0, 0);
+     * </pre>
+     * @return a double value that is {@code m11} element of the
+     *         3x3 affine transformation matrix.
      * @see #getMatrix
      * @since 1.2
      */
--- a/jdk/src/java.desktop/share/classes/sun/print/PSPrinterJob.java	Thu Jun 02 15:16:07 2016 +0530
+++ b/jdk/src/java.desktop/share/classes/sun/print/PSPrinterJob.java	Thu Jun 02 15:20:31 2016 +0530
@@ -1933,6 +1933,14 @@
     protected void deviceFill(PathIterator pathIter, Color color,
                               AffineTransform tx, Shape clip) {
 
+        if (Double.isNaN(tx.getScaleX()) ||
+            Double.isNaN(tx.getScaleY()) ||
+            Double.isNaN(tx.getShearX()) ||
+            Double.isNaN(tx.getShearY()) ||
+            Double.isNaN(tx.getTranslateX()) ||
+            Double.isNaN(tx.getTranslateY())) {
+            return;
+        }
         setTransform(tx);
         setClip(clip);
         setColor(color);
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/jdk/test/java/awt/print/PrinterJob/LandscapeStackOverflow.java	Thu Jun 02 15:20:31 2016 +0530
@@ -0,0 +1,94 @@
+/*
+ * Copyright (c) 2016, Oracle and/or its affiliates. All rights reserved.
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
+ *
+ * This code is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License version 2 only, as
+ * published by the Free Software Foundation.
+ *
+ * This code is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
+ * version 2 for more details (a copy is included in the LICENSE file that
+ * accompanied this code).
+ *
+ * You should have received a copy of the GNU General Public License version
+ * 2 along with this work; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
+ * or visit www.oracle.com if you need additional information or have any
+ * questions.
+ */
+/*
+ * @test
+ * @bug 6842011
+ * @summary Test if StackOverflowError occurs during printing landscape with
+ *          scale and transform.
+ * @run main LandscapeStackOverflow
+ */
+import java.awt.Graphics;
+import java.awt.Graphics2D;
+import java.awt.geom.Path2D;
+import java.awt.print.PageFormat;
+import java.awt.print.Printable;
+import java.awt.print.PrinterException;
+import java.awt.print.PrinterJob;
+import javax.print.attribute.HashPrintRequestAttributeSet;
+import javax.print.attribute.PrintRequestAttributeSet;
+import javax.print.attribute.standard.OrientationRequested;
+
+public class LandscapeStackOverflow {
+
+    public static final void main( String[] parameters ) {
+        PrinterJob printjob = PrinterJob.getPrinterJob();
+        printjob.setJobName( "Test Print Job" );
+
+        PrintRequestAttributeSet attributes = new HashPrintRequestAttributeSet();
+        attributes.add( OrientationRequested.LANDSCAPE );
+
+        boolean print = printjob.printDialog( attributes );
+        if( !print ) return;
+
+        try {
+            printjob.setPrintable( new Painter() );
+            printjob.print( attributes );
+        } catch( PrinterException exception ) {
+            exception.printStackTrace();
+        }
+    }
+}
+
+/**
+ * Paints a 2 inch by 2 inch rectangle in the center of the page.
+ */
+class Painter implements Printable {
+
+    public int print( Graphics graphics, PageFormat format, int index ) {
+        Graphics2D g2d = (Graphics2D)graphics;
+
+        double scalex = g2d.getTransform().getScaleX();
+        double scaley = g2d.getTransform().getScaleY();
+
+        double centerx = ( format.getImageableX() +
+                         ( format.getImageableWidth() / 2 ) ) * scalex;
+        double centery = ( format.getImageableY() +
+                         ( format.getImageableHeight() / 2 ) ) * scaley;
+
+        // The following 2 lines cause an error when printing in landscape.
+        g2d.scale( 1 / scalex, 1 / scaley );
+        g2d.translate( centerx, centery );
+
+        Path2D.Double path = new Path2D.Double();
+        path.moveTo( -scalex * 72, -scaley * 72 );
+        path.lineTo( -scalex * 72, scaley * 72 );
+        path.lineTo( scalex * 72, scaley * 72 );
+        path.lineTo( scalex * 72, -scaley * 72 );
+        path.closePath();
+
+        g2d.draw( path );
+
+        return index == 0 ? PAGE_EXISTS : NO_SUCH_PAGE;
+    }
+
+}