1 /*
   2  * Copyright (c) 2007, 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 1.2 02/05/15
  25    @bug 4810363 4924441
  26    @key printer
  27    @run main DeviceScale
  28    @summary check the peek scale is the same as the device scale, and that the
  29    clips are also the same
  30 */
  31 import java.io.*;
  32 import java.net.*;
  33 import java.awt.*;
  34 import java.awt.geom.*;
  35 import java.awt.print.*;
  36 import javax.print.attribute.*;
  37 import javax.print.attribute.standard.*;
  38 
  39 public class DeviceScale implements Printable {
  40 
  41     boolean firstTime = true;
  42     double sx, sy;
  43     Shape clip, firstClip;
  44 
  45     public int print(Graphics g, PageFormat pf, int pageIndex)  {
  46         Graphics2D g2 = (Graphics2D)g;
  47         if (pageIndex>=1) {
  48                 return Printable.NO_SUCH_PAGE;
  49         }
  50         AffineTransform at = g2.getTransform();
  51         System.out.println(at);
  52         clip = g2.getClip();
  53         System.out.println(clip);
  54         if (firstTime)  {
  55             firstTime = false;
  56             sx = Math.abs(at.getScaleX());
  57             sy = Math.abs(at.getScaleY());
  58             firstClip = clip;
  59         } else {
  60             double newSx = Math.abs(at.getScaleX());
  61             double newSy = Math.abs(at.getScaleY());
  62             if (Math.abs(sx - newSx) > 0.1 ||
  63                 Math.abs(sy - newSy) > 0.1) {
  64                 throw new RuntimeException("different scale, was "+
  65                                            sx+","+sy+" now " +
  66                                            newSx+","+ newSy);
  67             }
  68             if (!clip.equals(firstClip)) {
  69                 throw new RuntimeException("different clip, was "+ firstClip +
  70                                            " now "+ clip);
  71             }
  72         }
  73         return Printable.PAGE_EXISTS;
  74     }
  75 
  76     public static void doit(OrientationRequested o) throws Exception {
  77         PrinterJob  pj = PrinterJob.getPrinterJob();
  78         if (pj.getPrintService() == null) {
  79           System.out.println("No print service found.");
  80           return;
  81         }
  82         pj.setPrintable(new DeviceScale());
  83         PrintRequestAttributeSet aset = new HashPrintRequestAttributeSet();
  84         aset.add(o);
  85         String fileName = "out.prn";
  86         File f = new File(fileName);
  87         f.deleteOnExit();
  88         URI dest = f.toURI();
  89         aset.add(new Destination(dest));
  90         pj.print(aset);
  91     }
  92 
  93 
  94     public static void main(String arg[]) throws Exception {
  95 
  96         doit(OrientationRequested.PORTRAIT);
  97         doit(OrientationRequested.LANDSCAPE);
  98         doit(OrientationRequested.REVERSE_LANDSCAPE);
  99 
 100     }
 101 
 102 }