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 import java.awt.*;
25 import java.awt.print.*;
26 import java.awt.GraphicsEnvironment;
27 
28 public class PrintFontStyle {
main(String[] args)29     public static void main(String[] args) {
30 
31         String[] instructions =
32         {
33             "You must have a printer available to perform this test and should use Win 98.",
34             "This bug is system dependent and is not always reproducible.",
35             " ",
36             "A passing test will have all text printed with correct font style.",
37         };
38 
39         Sysout.createDialog( );
40         Sysout.printInstructions( instructions );
41 
42         PrinterJob pj=PrinterJob.getPrinterJob();
43         pj.setPrintable(new FontPrintable());
44         if (pj.printDialog())
45             {
46                 try { pj.print(); }
47                 catch (PrinterException e) {
48                     System.out.println(e);
49                 }
50             }
51     }
52 }
53 
54 class FontPrintable
55     implements Printable {
56 
print(Graphics g, PageFormat pf, int pageIndex)57     public int print(Graphics g, PageFormat pf, int pageIndex) {
58         if (pageIndex != 0) return NO_SUCH_PAGE;
59         Graphics2D g2= (Graphics2D)g;
60 
61         g2.setPaint(Color.black);
62 
63         GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
64         String[] fontList = ge.getAvailableFontFamilyNames();
65         g2.setFont (new Font ("Arial", Font.PLAIN, 20));
66         g2.drawString("Arial - Plain", 144, 120);
67         g2.setFont (new Font ("Arial", Font.BOLD, 20));
68         g2.drawString("Arial - Bold", 144, 145);
69         g2.setFont (new Font ("Arial", Font.ITALIC, 20));
70         g2.drawString("Arial - Italic", 144, 170);
71         g2.setFont (new Font ("Times New Roman", Font.PLAIN, 20));
72         g2.drawString("Times New Roman - Plain", 144, 195);
73         g2.setFont (new Font ("Times New Roman", Font.BOLD, 20));
74         g2.drawString("Times New Roman - Bold", 144, 220);
75         g2.setFont (new Font ("Times New Roman", Font.ITALIC, 20));
76         g2.drawString("Times New Roman - Italic", 144, 245);
77 
78         return PAGE_EXISTS;
79     }
80 }
81 
82 class Sysout {
83    private static TestDialog dialog;
84 
createDialogWithInstructions( String[] instructions )85    public static void createDialogWithInstructions( String[] instructions )
86     {
87       dialog = new TestDialog( new Frame(), "Instructions" );
88       dialog.printInstructions( instructions );
89       dialog.show();
90       println( "Any messages for the tester will display here." );
91     }
92 
createDialog( )93    public static void createDialog( )
94     {
95       dialog = new TestDialog( new Frame(), "Instructions" );
96       String[] defInstr = { "Instructions will appear here. ", "" } ;
97       dialog.printInstructions( defInstr );
98       dialog.show();
99       println( "Any messages for the tester will display here." );
100     }
101 
102 
printInstructions( String[] instructions )103    public static void printInstructions( String[] instructions )
104     {
105       dialog.printInstructions( instructions );
106     }
107 
108 
println( String messageIn )109    public static void println( String messageIn )
110     {
111       dialog.displayMessage( messageIn );
112     }
113 
114 }// Sysout  class
115 
116 /**
117   This is part of the standard test machinery.  It provides a place for the
118    test instructions to be displayed, and a place for interactive messages
119    to the user to be displayed.
120   To have the test instructions displayed, see Sysout.
121   To have a message to the user be displayed, see Sysout.
122   Do not call anything in this dialog directly.
123   */
124 class TestDialog extends Dialog {
125 
126    TextArea instructionsText;
127    TextArea messageText;
128    int maxStringLength = 80;
129 
130    //DO NOT call this directly, go through Sysout
TestDialog( Frame frame, String name )131    public TestDialog( Frame frame, String name )
132     {
133       super( frame, name );
134       int scrollBoth = TextArea.SCROLLBARS_BOTH;
135       instructionsText = new TextArea( "", 15, maxStringLength, scrollBoth );
136       add( "North", instructionsText );
137 
138       messageText = new TextArea( "", 5, maxStringLength, scrollBoth );
139       add("Center", messageText);
140 
141       pack();
142 
143       show();
144     }// TestDialog()
145 
146    //DO NOT call this directly, go through Sysout
printInstructions( String[] instructions )147    public void printInstructions( String[] instructions )
148     {
149       //Clear out any current instructions
150       instructionsText.setText( "" );
151 
152       //Go down array of instruction strings
153 
154       String printStr, remainingStr;
155       for( int i=0; i < instructions.length; i++ )
156        {
157          //chop up each into pieces maxSringLength long
158          remainingStr = instructions[ i ];
159          while( remainingStr.length() > 0 )
160           {
161             //if longer than max then chop off first max chars to print
162             if( remainingStr.length() >= maxStringLength )
163              {
164                //Try to chop on a word boundary
165                int posOfSpace = remainingStr.
166                   lastIndexOf( ' ', maxStringLength - 1 );
167 
168                if( posOfSpace <= 0 ) posOfSpace = maxStringLength - 1;
169 
170                printStr = remainingStr.substring( 0, posOfSpace + 1 );
171                remainingStr = remainingStr.substring( posOfSpace + 1 );
172              }
173             //else just print
174             else
175              {
176                printStr = remainingStr;
177                remainingStr = "";
178              }
179 
180             instructionsText.append( printStr + "\n" );
181 
182           }// while
183 
184        }// for
185 
186     }//printInstructions()
187 
188    //DO NOT call this directly, go through Sysout
displayMessage( String messageIn )189    public void displayMessage( String messageIn )
190     {
191       messageText.append( messageIn + "\n" );
192     }
193 
194  }// TestDialog  class
195