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  * @key headful printer
  26  * @bug 6842011 8158758
  27  * @summary Test if StackOverflowError occurs during printing landscape with
  28  *          scale and transform.
  29  * @run main LandscapeStackOverflow
  30  */
  31 import java.awt.Graphics;
  32 import java.awt.Graphics2D;
  33 import java.awt.geom.Path2D;
  34 import java.awt.print.PageFormat;
  35 import java.awt.print.Printable;
  36 import java.awt.print.PrinterException;
  37 import java.awt.print.PrinterJob;
  38 import javax.print.attribute.HashPrintRequestAttributeSet;
  39 import javax.print.attribute.PrintRequestAttributeSet;
  40 import javax.print.attribute.standard.OrientationRequested;
  41 
  42 public class LandscapeStackOverflow {
  43 
  44     public static final void main( String[] parameters ) {
  45         PrinterJob printjob = PrinterJob.getPrinterJob();
  46         printjob.setJobName( "Test Print Job" );
  47 
  48         PrintRequestAttributeSet attributes = new HashPrintRequestAttributeSet();
  49         attributes.add( OrientationRequested.LANDSCAPE );
  50 
  51         try {
  52             printjob.setPrintable( new Painter() );
  53             printjob.print( attributes );
  54         } catch( PrinterException exception ) {
  55             exception.printStackTrace();
  56         }
  57     }
  58 }
  59 
  60 /**
  61  * Paints a 2 inch by 2 inch rectangle in the center of the page.
  62  */
  63 class Painter implements Printable {
  64 
  65     public int print( Graphics graphics, PageFormat format, int index ) {
  66         Graphics2D g2d = (Graphics2D)graphics;
  67 
  68         double scalex = g2d.getTransform().getScaleX();
  69         double scaley = g2d.getTransform().getScaleY();
  70 
  71         double centerx = ( format.getImageableX() +
  72                          ( format.getImageableWidth() / 2 ) ) * scalex;
  73         double centery = ( format.getImageableY() +
  74                          ( format.getImageableHeight() / 2 ) ) * scaley;
  75 
  76         // The following 2 lines cause an error when printing in landscape.
  77         g2d.scale( 1 / scalex, 1 / scaley );
  78         g2d.translate( centerx, centery );
  79 
  80         Path2D.Double path = new Path2D.Double();
  81         path.moveTo( -scalex * 72, -scaley * 72 );
  82         path.lineTo( -scalex * 72, scaley * 72 );
  83         path.lineTo( scalex * 72, scaley * 72 );
  84         path.lineTo( scalex * 72, -scaley * 72 );
  85         path.closePath();
  86 
  87         g2d.draw( path );
  88 
  89         return index == 0 ? PAGE_EXISTS : NO_SUCH_PAGE;
  90     }
  91 
  92 }