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 4480930
27 * @summary TextLayout prints as filled shapes
28 * @author prr
29 * @run main/manual PrintTextLayout
30 */
31
32/* This is a MANUAL test and must be run on a system with a printer
33 * configured.
34 */
35
36import java.io.*;
37import java.awt.*;
38import java.awt.event.*;
39import java.awt.font.*;
40import java.awt.geom.*;
41import java.awt.print.*;
42import javax.print.attribute.*;
43import javax.print.attribute.standard.*;
44
45public class PrintTextLayout implements Printable {
46    static String[] fontnames = {
47        "Lucida Sans",
48        "Lucida Bright",
49        "Lucida Sans Typewriter",
50        "SansSerif",
51        "Serif",
52        "Monospaced",
53     };
54
55    static String text =
56    "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ 1234567890";
57
58    public static void main(String args[]) {
59        PrinterJob pj = PrinterJob.getPrinterJob();
60
61        if (pj != null) {
62            PrintRequestAttributeSet aset = new HashPrintRequestAttributeSet();
63            aset.add(new Destination((new File("./out.ps")).toURI()));
64            PageFormat pf = pj.defaultPage();
65            Paper p = pf.getPaper();
66            // Extend imageable width to reduce likelihood end of text
67            // is clipped as we'd like to see the end of the line.
68            p.setImageableArea(p.getImageableX(), p.getImageableY(),
69                               p.getWidth()-p.getImageableX(),
70                               p.getImageableHeight());
71            pf.setPaper(p);
72            pj.setPrintable( new PrintTextLayout(), pf);
73            try {
74                pj.print(aset);
75            } catch (PrinterException pe) {
76                pe.printStackTrace();
77            } finally {
78            }
79        }
80    }
81
82    public int print(Graphics g, PageFormat pgFmt, int pgIndex) {
83        if (pgIndex > 0) return Printable.NO_SUCH_PAGE;
84
85        double iw = pgFmt.getImageableWidth();
86        double ih = pgFmt.getImageableHeight();
87        Graphics2D g2d = (Graphics2D)g;
88        g2d.translate(pgFmt.getImageableX(), pgFmt.getImageableY()+50);
89
90        float ypos = 20f;
91        for (int f=0; f< fontnames.length; f++) {
92            for (int s=0;s<4;s++) {
93                Font font = new Font(fontnames[f], s, 12);
94                ypos = drawText(g2d, font, ypos);
95            }
96        }
97        return Printable.PAGE_EXISTS;
98    }
99
100    float drawText(Graphics2D g2d, Font font, float ypos) {
101        int x = 10;
102        /* Set the graphics font to something odd before using TL so
103         * can be sure it picks up the font from the TL, not the graphics */
104        Font f1 = new Font("serif", Font.ITALIC, 1);
105        g2d.setFont(f1);
106        FontRenderContext frc = new FontRenderContext(null, false, false);
107        TextLayout tl = new TextLayout(text ,font, frc);
108        float ascent = tl.getAscent();
109        int dpos = (int)(ypos+ascent);
110        tl.draw(g2d, x, dpos);
111        int dpos2 = (int)(ypos+ascent+tl.getDescent());
112        g2d.drawLine(x, dpos2, x+(int)tl.getAdvance(), dpos2);
113        float tlHeight = tl.getAscent()+tl.getDescent()+tl.getLeading();
114        return ypos+tlHeight;
115    }
116}
117