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