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