1 /*
   2  * Copyright (c) 2016, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 /*
  24  * @test
  25  * @bug 6842011
  26  * @summary Test if StackOverflowError occurs during printing landscape with 
  27  *          scale and transform.
  28  * @run main LandscapeStackOverflow
  29  */
  30 import java.awt.Graphics;
  31 import java.awt.Graphics2D;
  32 import java.awt.geom.Path2D;
  33 import java.awt.print.PageFormat;
  34 import java.awt.print.Printable;
  35 import java.awt.print.PrinterException;
  36 import java.awt.print.PrinterJob;
  37 import javax.print.attribute.HashPrintRequestAttributeSet;
  38 import javax.print.attribute.PrintRequestAttributeSet;
  39 import javax.print.attribute.standard.OrientationRequested;
  40 
  41 public class LandscapeStackOverflow {
  42 
  43     public static final void main( String[] parameters ) {
  44         PrinterJob printjob = PrinterJob.getPrinterJob();
  45         printjob.setJobName( "Test Print Job" );
  46 
  47         PrintRequestAttributeSet attributes = new HashPrintRequestAttributeSet();
  48         attributes.add( OrientationRequested.LANDSCAPE );
  49 
  50         boolean print = printjob.printDialog( attributes );
  51         if( !print ) return;
  52 
  53         try {
  54             printjob.setPrintable( new Painter() );
  55             printjob.print( attributes );
  56         } catch( PrinterException exception ) {
  57             exception.printStackTrace();
  58         }
  59     }
  60 }
  61 
  62 /**
  63  * Paints a 2 inch by 2 inch rectangle in the center of the page.
  64  */
  65 class Painter implements Printable {
  66 
  67     public int print( Graphics graphics, PageFormat format, int index ) {
  68         Graphics2D g2d = (Graphics2D)graphics;
  69 
  70         double scalex = g2d.getTransform().getScaleX();
  71         double scaley = g2d.getTransform().getScaleY();
  72 
  73         double centerx = ( format.getImageableX() + 
  74                          ( format.getImageableWidth() / 2 ) ) * scalex;
  75         double centery = ( format.getImageableY() + 
  76                          ( format.getImageableHeight() / 2 ) ) * scaley;
  77 
  78         // The following 2 lines cause an error when printing in landscape.
  79         g2d.scale( 1 / scalex, 1 / scaley );
  80         g2d.translate( centerx, centery );
  81 
  82         Path2D.Double path = new Path2D.Double();
  83         path.moveTo( -scalex * 72, -scaley * 72 );
  84         path.lineTo( -scalex * 72, scaley * 72 );
  85         path.lineTo( scalex * 72, scaley * 72 );
  86         path.lineTo( scalex * 72, -scaley * 72 );
  87         path.closePath();
  88 
  89         g2d.draw( path );
  90 
  91         return index == 0 ? PAGE_EXISTS : NO_SUCH_PAGE;
  92     }
  93 
  94 }