|
1 /* |
|
2 * Copyright (c) 2007, 2017, 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 /** |
|
25 * @test |
|
26 * @key headful |
|
27 * @bug 6543815 6601097 8160888 |
|
28 * @summary Image should be sent to printer, no exceptions thrown. |
|
29 * The 3 printouts should have a rectangle which is the minimum |
|
30 * possible margins ie, the margins should be hardware margins |
|
31 * and not java default 1 inch margins. |
|
32 * @run main Margins |
|
33 */ |
|
34 |
|
35 import java.awt.print.PrinterJob; |
|
36 import java.awt.print.Printable; |
|
37 import java.awt.print.PageFormat; |
|
38 import java.awt.print.Paper; |
|
39 import java.awt.print.PrinterException; |
|
40 import java.awt.Graphics; |
|
41 import java.awt.Graphics2D; |
|
42 import java.awt.Color; |
|
43 import java.awt.Robot; |
|
44 import java.awt.event.KeyEvent; |
|
45 |
|
46 public class Margins implements Printable { |
|
47 |
|
48 public static void main(String args[]) throws Exception { |
|
49 Robot robot = new Robot(); |
|
50 Thread t = new Thread (() -> { |
|
51 robot.waitForIdle(); |
|
52 robot.delay(5000); |
|
53 robot.keyPress(KeyEvent.VK_ENTER); |
|
54 robot.keyRelease(KeyEvent.VK_ENTER); |
|
55 robot.waitForIdle(); |
|
56 robot.delay(5000); |
|
57 robot.keyPress(KeyEvent.VK_ENTER); |
|
58 robot.keyRelease(KeyEvent.VK_ENTER); |
|
59 robot.waitForIdle(); |
|
60 robot.delay(5000); |
|
61 robot.keyPress(KeyEvent.VK_ENTER); |
|
62 robot.keyRelease(KeyEvent.VK_ENTER); |
|
63 }); |
|
64 |
|
65 PrinterJob job = PrinterJob.getPrinterJob(); |
|
66 PageFormat pageFormat = job.defaultPage(); |
|
67 Paper paper = pageFormat.getPaper(); |
|
68 double wid = paper.getWidth(); |
|
69 double hgt = paper.getHeight(); |
|
70 paper.setImageableArea(0, -10, wid, hgt); |
|
71 t.start(); |
|
72 |
|
73 pageFormat = job.pageDialog(pageFormat); |
|
74 pageFormat.setPaper(paper); |
|
75 job.setPrintable(new Margins(), pageFormat); |
|
76 try { |
|
77 job.print(); |
|
78 } catch (PrinterException e) { |
|
79 } |
|
80 |
|
81 paper.setImageableArea(0, 0, wid, hgt + 72); |
|
82 pageFormat = job.pageDialog(pageFormat); |
|
83 pageFormat.setPaper(paper); |
|
84 |
|
85 job.setPrintable(new Margins(), pageFormat); |
|
86 try { |
|
87 job.print(); |
|
88 } catch (PrinterException e) { |
|
89 } |
|
90 |
|
91 pageFormat = job.defaultPage(); |
|
92 paper = pageFormat.getPaper(); |
|
93 wid = paper.getWidth(); |
|
94 hgt = paper.getHeight(); |
|
95 |
|
96 paper.setImageableArea(0, -10, -wid, hgt); |
|
97 pageFormat = job.pageDialog(pageFormat); |
|
98 pageFormat.setPaper(paper); |
|
99 |
|
100 job.setPrintable(new Margins(), pageFormat); |
|
101 try { |
|
102 job.print(); |
|
103 } catch (PrinterException e) { |
|
104 } |
|
105 } |
|
106 |
|
107 public int print(Graphics g, PageFormat pf, int page) |
|
108 throws PrinterException { |
|
109 |
|
110 if (page > 0) { |
|
111 return NO_SUCH_PAGE; |
|
112 } |
|
113 int ix = (int)pf.getImageableX(); |
|
114 int iy = (int)pf.getImageableY(); |
|
115 int iw = (int)pf.getImageableWidth(); |
|
116 int ih = (int)pf.getImageableHeight(); |
|
117 System.out.println("ix="+ix+" iy="+iy+" iw="+iw+" ih="+ih); |
|
118 if ((ix < 0) || (iy < 0)) { |
|
119 throw new RuntimeException("Imageable x or y is a negative value."); |
|
120 } |
|
121 |
|
122 |
|
123 Paper paper = pf.getPaper(); |
|
124 int wid = (int)paper.getWidth(); |
|
125 int hgt = (int)paper.getHeight(); |
|
126 System.out.println("wid="+wid+" hgt="+hgt); |
|
127 /* |
|
128 * If imageable width/height is -ve, then print was done with 1" margin |
|
129 * e.g. ix=72 iy=72 iw=451 ih=697 and paper wid=595 |
|
130 * but with fix, we get print with hardware margin e.g. |
|
131 * ix=12, iy=12, iw=571, ih=817 |
|
132 */ |
|
133 if ((wid - iw > 72) || (hgt - ih > 72)) { |
|
134 throw new RuntimeException("Imageable width or height is negative value"); |
|
135 } |
|
136 if ((ix+iw > wid) || (iy+ih > hgt)) { |
|
137 throw new RuntimeException("Printable width or height " |
|
138 + "exceeds paper width or height."); |
|
139 } |
|
140 // runtime checking to see if the margins/printable area |
|
141 // correspond to the entire size of the paper, for now, make it pass |
|
142 // as for linux, the hwmargin is not taken into account - bug6574279 |
|
143 if (ix == 0 && iy == 0 && (ix+iw == wid) && (iy+ih == hgt)) { |
|
144 return PAGE_EXISTS; |
|
145 } |
|
146 |
|
147 Graphics2D g2d = (Graphics2D)g; |
|
148 g2d.translate(ix, iy); |
|
149 g2d.setColor(Color.black); |
|
150 g2d.drawRect(1, 1, iw-2, ih-2); |
|
151 |
|
152 return PAGE_EXISTS; |
|
153 } |
|
154 } |