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
26  * @bug 4271596
27  * @bug 4460699
28  * @summary Rotated text printing
29  * @author prr
30  * @run main/manual PrintRotatedText
31  */
32 
33 /* Text is drawn as spokes of a wheel with both a uniform scale and
34  * a non-uniform scale.
35  * The test is checking whether the implementation properly handles this
36  * and in particular that asking win32 GDI to draw text rotated works
37  * properly.
38  *
39  */
40 import java.awt.*;
41 import java.awt.event.*;
42 import java.awt.font.*;
43 import java.awt.geom.*;
44 import java.awt.print.*;
45 
46 public class PrintRotatedText extends Frame implements ActionListener {
47  static String fontname="Lucida Sans Regular"; // our font
48  private TextCanvas c;
49 
main(String args[])50  public static void main(String args[]) {
51 
52     PrintRotatedText f = new PrintRotatedText();
53     f.show();
54  }
55 
PrintRotatedText()56  public PrintRotatedText() {
57     super("JDK 1.2 Text Printing");
58 
59     String []fonts = GraphicsEnvironment.getLocalGraphicsEnvironment().getAvailableFontFamilyNames();
60     for (int i=0;i<fonts.length;i++) {
61        if (fonts[i].equals("Times New Roman")) {
62          fontname = "Times New Roman";
63        }
64     }
65     c = new TextCanvas();
66     add("Center", c);
67 
68     Button printButton = new Button("Print");
69     printButton.addActionListener(this);
70     add("South", printButton);
71 
72     addWindowListener(new WindowAdapter() {
73        public void windowClosing(WindowEvent e) {
74              System.exit(0);
75             }
76     });
77 
78     pack();
79  }
80 
actionPerformed(ActionEvent e)81  public void actionPerformed(ActionEvent e) {
82 
83    PrinterJob pj = PrinterJob.getPrinterJob();
84 
85    if (pj != null && pj.printDialog()) {
86 
87        pj.setPageable(c);
88        try {
89             pj.print();
90       } catch (PrinterException pe) {
91       } finally {
92          System.err.println("PRINT RETURNED");
93       }
94    }
95  }
96 
97  class TextCanvas extends Panel implements Pageable, Printable {
98 
99     public static final int MAXPAGE = 8;
100     // public static final String extra ="\u0391A\u2200B\u2702C\u2778D";
101     public static final String extra ="\u0394\u03A9ABCD";
102     public String estr=extra;
103 
getNumberOfPages()104     public int getNumberOfPages() {
105         return MAXPAGE;
106     }
107 
getPageFormat(int pageIndex)108     public PageFormat getPageFormat(int pageIndex) {
109        if (pageIndex > MAXPAGE) throw new IndexOutOfBoundsException();
110        PageFormat pf = new PageFormat();
111        Paper p = pf.getPaper();
112        p.setImageableArea(36, 36, p.getWidth()-72, p.getHeight()-72);
113        pf.setPaper(p);
114 
115 /*
116        if (pageIndex==1)
117          pf.setOrientation(PageFormat.LANDSCAPE);
118        else if (pageIndex==2)
119          pf.setOrientation(PageFormat.REVERSE_LANDSCAPE);
120 */
121 
122        return pf;
123     }
124 
getPrintable(int pageIndex)125     public Printable getPrintable(int pageIndex) {
126        if (pageIndex > MAXPAGE) throw new IndexOutOfBoundsException();
127        return this;
128     }
129 
print(Graphics g, PageFormat pgFmt, int pgIndex)130     public int print(Graphics g, PageFormat pgFmt, int pgIndex) {
131 System.out.println("****"+pgIndex);
132         double iw = pgFmt.getImageableWidth();
133         double ih = pgFmt.getImageableHeight();
134         Graphics2D g2d = (Graphics2D)g;
135         g2d.translate(pgFmt.getImageableX(), pgFmt.getImageableY());
136         //g2d.drawString("top left of page format",20,20 );
137         int modulo = pgIndex % 4;
138         int divvy = pgIndex / 4;
139         if (divvy != 0 ) {
140            g2d.setFont(new Font(fontname,Font.PLAIN, 18));
141            estr = "";
142         } else {
143            estr = extra;
144         }
145 
146         int xs = 1;
147         int ys = 1;
148 
149         if (modulo == 1) {
150             xs = -1;
151         }
152         if (modulo == 2) {
153             ys = -1;
154         }
155         if (modulo == 3) {
156             xs = -1;
157             ys = -1;
158         }
159 
160         g2d.translate(iw*0.25, ih*0.2);
161         drawTheText((Graphics2D)g2d.create(), xs*1.0,ys* 1.0);
162         g2d.translate(iw*0.25, ih*0.2);
163         drawTheText((Graphics2D)g2d.create(), xs*1.0,ys* 1.5);
164         g2d.translate(-iw*0.2, ih*0.3);
165         drawTheText((Graphics2D)g2d.create(), xs*1.5, ys*1.0);
166 
167         return Printable.PAGE_EXISTS;
168     }
169 
drawTheText(Graphics2D g2d, double sx, double sy)170    private void drawTheText(Graphics2D g2d, double sx, double sy) {
171       double mat[]= new double[6];
172 
173       g2d.drawOval(-75,-75,150,150);
174       int degrees = 30;
175       for (int i=0;i<360;i=i+degrees) {
176           AffineTransform saveXfm = g2d.getTransform();
177           g2d.scale(sx, sy);
178           int ttype = g2d.getTransform().getType();
179           String s = "ANGLE="+i;
180           s +=estr;
181           g2d.drawString(s, 20, 0);
182           FontRenderContext frc = g2d.getFontRenderContext();
183           Font f = g2d.getFont();
184           Rectangle2D r2d = f.getStringBounds(s, frc);
185           g2d.drawLine(20, 1, 20+(int)r2d.getWidth(), 1);
186           g2d.scale(1.0/sx, 1.0/sy);
187           g2d.setTransform(saveXfm);
188 
189           g2d.rotate(Math.toRadians(degrees));
190       }
191    }
192 
paint(Graphics g)193     public void paint(Graphics g) {
194       g.translate(200,200);
195       g.setFont(new Font("serif", Font.PLAIN, 12));
196       drawTheText((Graphics2D)g, 1.0, 1.5);
197     }
198 
getPreferredSize()199      public Dimension getPreferredSize() {
200         return new Dimension(400, 400);
201     }
202  }
203 
204 }
205