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 8158758
  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         try {
  51             printjob.setPrintable( new Painter() );
  52             printjob.print( attributes );
  53         } catch( PrinterException exception ) {
  54             exception.printStackTrace();
  55         }
  56     }
  57 }
  58 
  59 /**
  60  * Paints a 2 inch by 2 inch rectangle in the center of the page.
  61  */
  62 class Painter implements Printable {
  63 
  64     public int print( Graphics graphics, PageFormat format, int index ) {
  65         Graphics2D g2d = (Graphics2D)graphics;
  66 
  67         double scalex = g2d.getTransform().getScaleX();
  68         double scaley = g2d.getTransform().getScaleY();
  69 
  70         double centerx = ( format.getImageableX() +
  71                          ( format.getImageableWidth() / 2 ) ) * scalex;
  72         double centery = ( format.getImageableY() +
  73                          ( format.getImageableHeight() / 2 ) ) * scaley;
  74 
  75         // The following 2 lines cause an error when printing in landscape.
  76         g2d.scale( 1 / scalex, 1 / scaley );
  77         g2d.translate( centerx, centery );
  78 
  79         Path2D.Double path = new Path2D.Double();
  80         path.moveTo( -scalex * 72, -scaley * 72 );
  81         path.lineTo( -scalex * 72, scaley * 72 );
  82         path.lineTo( scalex * 72, scaley * 72 );
  83         path.lineTo( scalex * 72, -scaley * 72 );
  84         path.closePath();
  85 
  86         g2d.draw( path );
  87 
  88         return index == 0 ? PAGE_EXISTS : NO_SUCH_PAGE;
  89     }
  90 
  91 }