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 /**
25  * @test PrintTextPane.java
26  * @key headful printer
27  * @bug 6452415 6570471
28  * @summary Test that swing text prints using GDI printer fonts.
29  * @author prr: area=PrinterJob
30  * @run main PrintTextPane
31  */
32 
33 import java.io.*;
34 import java.net.*;
35 import java.awt.*;
36 import java.awt.event.*;
37 import javax.print.attribute.*;
38 import javax.print.attribute.standard.*;
39 import javax.swing.*;
40 import javax.swing.text.*;
41 import java.awt.print.*;
42 
43 public class PrintTextPane extends JTextPane implements Printable {
44 
45    static String text = "Twinkle twinkle little star, \n" +
46                         "How I wonder what you are. \n" +
47                         "Up above the world so high, \n" +
48                         "Like a diamond in the sky. \n" +
49                         "Twinkle, twinkle, little star, \n" +
50                         "How I wonder what you are!\n";
51 
print(Graphics g, PageFormat pf, int page)52     public int print(Graphics g, PageFormat pf, int page)
53                                  throws PrinterException {
54         if (page > 0) {
55             return NO_SUCH_PAGE;
56         }
57         Graphics2D g2d = (Graphics2D)g;
58         g2d.translate(pf.getImageableX(), pf.getImageableY());
59         printAll(g);
60         return PAGE_EXISTS;
61     }
62 
printPane(PrintRequestAttributeSet aset)63     public void printPane(PrintRequestAttributeSet aset) {
64         try {
65              print(null, null, false, null, aset, false);
66          } catch (PrinterException ex) {
67                throw new RuntimeException(ex);
68          }
69     }
70 
printPaneJob(PrintRequestAttributeSet aset)71     public void printPaneJob(PrintRequestAttributeSet aset) {
72          PrinterJob job = PrinterJob.getPrinterJob();
73          job.setPrintable(this);
74          try {
75              job.print(aset);
76          } catch (PrinterException ex) {
77              throw new RuntimeException(ex);
78          }
79     }
80 
PrintTextPane(String fontFamily)81     public PrintTextPane(String fontFamily) {
82         super();
83         SimpleAttributeSet aset = new SimpleAttributeSet();
84         StyleConstants.setFontFamily(aset, fontFamily);
85         setCharacterAttributes(aset, false);
86         setText(text+text+text+text+text+text+text+text);
87     }
88 
main(String args[])89     public static void main(String args[]) throws Exception {
90 
91         String os = System.getProperty("os.name");
92 
93         if (!os.startsWith("Windows")) {
94              return;
95         }
96 
97         PrinterJob job = PrinterJob.getPrinterJob();
98         if (job.getPrintService() == null) {
99             System.err.println("Warning: no printers, skipping test");
100             return;
101         }
102         JFrame f = new JFrame("Print Text Pane1");
103         f.addWindowListener(new WindowAdapter() {
104            public void windowClosing(WindowEvent e) {System.exit(0);}
105         });
106         PrintTextPane monoPane = new PrintTextPane("Monospaced");
107         f.add("East", monoPane);
108         PrintRequestAttributeSet aset = new HashPrintRequestAttributeSet();
109         PrintTextPane courPane = new PrintTextPane("Courier New");
110         f.add("West", courPane);
111         f.pack();
112         f.setVisible(true);
113 
114         File spoolFile = File.createTempFile("CourText", ".prn");
115         System.out.println(spoolFile);
116         Destination dest = new Destination(spoolFile.toURI());
117         aset.add(dest);
118         courPane.printPane(aset);
119         long courLen = spoolFile.length();
120         System.out.println("CourText="+spoolFile.length());
121         spoolFile.delete();
122 
123         spoolFile = File.createTempFile("MonoText", ".prn");
124         System.out.println(spoolFile);
125         dest = new Destination(spoolFile.toURI());
126         aset.add(dest);
127         monoPane.printPane(aset);
128         long monoLen = spoolFile.length();
129         System.out.println("MonoText="+spoolFile.length());
130         spoolFile.delete();
131 
132         if (courLen > 2 * monoLen) {
133             throw new RuntimeException("Shapes being printed?");
134         }
135 
136         spoolFile = File.createTempFile("CourJob", ".prn");
137         System.out.println(spoolFile);
138         dest = new Destination(spoolFile.toURI());
139         aset.add(dest);
140         courPane.printPaneJob(aset);
141         courLen = spoolFile.length();
142         System.out.println("CourJob="+spoolFile.length());
143         spoolFile.delete();
144 
145         spoolFile = File.createTempFile("MonoJob", ".prn");
146         System.out.println(spoolFile);
147         dest = new Destination(spoolFile.toURI());
148         aset.add(dest);
149         monoPane.printPaneJob(aset);
150         monoLen = spoolFile.length();
151         System.out.println("MonoJob="+spoolFile.length());
152         spoolFile.delete();
153 
154         if (courLen > 2 * monoLen) {
155             throw new RuntimeException("Shapes being printed?");
156         }
157 
158     }
159 }
160