jdk/test/java/awt/print/PrinterJob/PrintGlyphVectorTest.java
author psadhukhan
Wed, 20 Apr 2016 10:59:23 +0530
changeset 37706 818b9c789c6c
parent 23010 6dadb192ad81
permissions -rw-r--r--
6197099: PrinterJob.getUserName() throws a security exception when user.name permission is not given Reviewed-by: prr, jdv
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
21929
48575f27a3cb 8029204: Printing a GlyphVector on Windows ignores position of first glyph
prr
parents:
diff changeset
     1
/*
23010
6dadb192ad81 8029235: Update copyright year to match last edit in jdk8 jdk repository for 2013
lana
parents: 21929
diff changeset
     2
 * Copyright (c) 1999, 2013, Oracle and/or its affiliates. All rights reserved.
21929
48575f27a3cb 8029204: Printing a GlyphVector on Windows ignores position of first glyph
prr
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
48575f27a3cb 8029204: Printing a GlyphVector on Windows ignores position of first glyph
prr
parents:
diff changeset
     4
 *
48575f27a3cb 8029204: Printing a GlyphVector on Windows ignores position of first glyph
prr
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
48575f27a3cb 8029204: Printing a GlyphVector on Windows ignores position of first glyph
prr
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
48575f27a3cb 8029204: Printing a GlyphVector on Windows ignores position of first glyph
prr
parents:
diff changeset
     7
 * published by the Free Software Foundation.
48575f27a3cb 8029204: Printing a GlyphVector on Windows ignores position of first glyph
prr
parents:
diff changeset
     8
 *
48575f27a3cb 8029204: Printing a GlyphVector on Windows ignores position of first glyph
prr
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
48575f27a3cb 8029204: Printing a GlyphVector on Windows ignores position of first glyph
prr
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
48575f27a3cb 8029204: Printing a GlyphVector on Windows ignores position of first glyph
prr
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
48575f27a3cb 8029204: Printing a GlyphVector on Windows ignores position of first glyph
prr
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
48575f27a3cb 8029204: Printing a GlyphVector on Windows ignores position of first glyph
prr
parents:
diff changeset
    13
 * accompanied this code).
48575f27a3cb 8029204: Printing a GlyphVector on Windows ignores position of first glyph
prr
parents:
diff changeset
    14
 *
48575f27a3cb 8029204: Printing a GlyphVector on Windows ignores position of first glyph
prr
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
48575f27a3cb 8029204: Printing a GlyphVector on Windows ignores position of first glyph
prr
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
48575f27a3cb 8029204: Printing a GlyphVector on Windows ignores position of first glyph
prr
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
48575f27a3cb 8029204: Printing a GlyphVector on Windows ignores position of first glyph
prr
parents:
diff changeset
    18
 *
48575f27a3cb 8029204: Printing a GlyphVector on Windows ignores position of first glyph
prr
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
48575f27a3cb 8029204: Printing a GlyphVector on Windows ignores position of first glyph
prr
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
48575f27a3cb 8029204: Printing a GlyphVector on Windows ignores position of first glyph
prr
parents:
diff changeset
    21
 * questions.
48575f27a3cb 8029204: Printing a GlyphVector on Windows ignores position of first glyph
prr
parents:
diff changeset
    22
 */
48575f27a3cb 8029204: Printing a GlyphVector on Windows ignores position of first glyph
prr
parents:
diff changeset
    23
48575f27a3cb 8029204: Printing a GlyphVector on Windows ignores position of first glyph
prr
parents:
diff changeset
    24
/*
48575f27a3cb 8029204: Printing a GlyphVector on Windows ignores position of first glyph
prr
parents:
diff changeset
    25
 * @test
48575f27a3cb 8029204: Printing a GlyphVector on Windows ignores position of first glyph
prr
parents:
diff changeset
    26
 * @bug 8029204
48575f27a3cb 8029204: Printing a GlyphVector on Windows ignores position of first glyph
prr
parents:
diff changeset
    27
 * @summary Tests GlyphVector is printed in the correct location
48575f27a3cb 8029204: Printing a GlyphVector on Windows ignores position of first glyph
prr
parents:
diff changeset
    28
 * @run main/manual=yesno PrintGlyphVectorTest
48575f27a3cb 8029204: Printing a GlyphVector on Windows ignores position of first glyph
prr
parents:
diff changeset
    29
 */
48575f27a3cb 8029204: Printing a GlyphVector on Windows ignores position of first glyph
prr
parents:
diff changeset
    30
48575f27a3cb 8029204: Printing a GlyphVector on Windows ignores position of first glyph
prr
parents:
diff changeset
    31
import java.io.*;
48575f27a3cb 8029204: Printing a GlyphVector on Windows ignores position of first glyph
prr
parents:
diff changeset
    32
import java.awt.*;
48575f27a3cb 8029204: Printing a GlyphVector on Windows ignores position of first glyph
prr
parents:
diff changeset
    33
import java.awt.event.*;
48575f27a3cb 8029204: Printing a GlyphVector on Windows ignores position of first glyph
prr
parents:
diff changeset
    34
import java.awt.font.*;
48575f27a3cb 8029204: Printing a GlyphVector on Windows ignores position of first glyph
prr
parents:
diff changeset
    35
import java.awt.geom.*;
48575f27a3cb 8029204: Printing a GlyphVector on Windows ignores position of first glyph
prr
parents:
diff changeset
    36
import java.awt.print.*;
48575f27a3cb 8029204: Printing a GlyphVector on Windows ignores position of first glyph
prr
parents:
diff changeset
    37
48575f27a3cb 8029204: Printing a GlyphVector on Windows ignores position of first glyph
prr
parents:
diff changeset
    38
public class PrintGlyphVectorTest extends Component implements Printable {
48575f27a3cb 8029204: Printing a GlyphVector on Windows ignores position of first glyph
prr
parents:
diff changeset
    39
48575f27a3cb 8029204: Printing a GlyphVector on Windows ignores position of first glyph
prr
parents:
diff changeset
    40
    public void drawGVs(Graphics g) {
48575f27a3cb 8029204: Printing a GlyphVector on Windows ignores position of first glyph
prr
parents:
diff changeset
    41
48575f27a3cb 8029204: Printing a GlyphVector on Windows ignores position of first glyph
prr
parents:
diff changeset
    42
        String testString = "0123456789abcdefghijklm";
48575f27a3cb 8029204: Printing a GlyphVector on Windows ignores position of first glyph
prr
parents:
diff changeset
    43
        Graphics2D g2d = (Graphics2D)g;
48575f27a3cb 8029204: Printing a GlyphVector on Windows ignores position of first glyph
prr
parents:
diff changeset
    44
        g2d.setColor(Color.black);
48575f27a3cb 8029204: Printing a GlyphVector on Windows ignores position of first glyph
prr
parents:
diff changeset
    45
        Font font = new Font("SansSerif", Font.PLAIN, 30);
48575f27a3cb 8029204: Printing a GlyphVector on Windows ignores position of first glyph
prr
parents:
diff changeset
    46
        FontRenderContext frc = g2d.getFontRenderContext();
48575f27a3cb 8029204: Printing a GlyphVector on Windows ignores position of first glyph
prr
parents:
diff changeset
    47
        GlyphVector v = font.createGlyphVector(frc, testString);
48575f27a3cb 8029204: Printing a GlyphVector on Windows ignores position of first glyph
prr
parents:
diff changeset
    48
48575f27a3cb 8029204: Printing a GlyphVector on Windows ignores position of first glyph
prr
parents:
diff changeset
    49
48575f27a3cb 8029204: Printing a GlyphVector on Windows ignores position of first glyph
prr
parents:
diff changeset
    50
        float x = 50f,
48575f27a3cb 8029204: Printing a GlyphVector on Windows ignores position of first glyph
prr
parents:
diff changeset
    51
              y = 50f;
48575f27a3cb 8029204: Printing a GlyphVector on Windows ignores position of first glyph
prr
parents:
diff changeset
    52
48575f27a3cb 8029204: Printing a GlyphVector on Windows ignores position of first glyph
prr
parents:
diff changeset
    53
        g2d.drawGlyphVector(v, x, y);
48575f27a3cb 8029204: Printing a GlyphVector on Windows ignores position of first glyph
prr
parents:
diff changeset
    54
        Rectangle2D r = v.getVisualBounds();
48575f27a3cb 8029204: Printing a GlyphVector on Windows ignores position of first glyph
prr
parents:
diff changeset
    55
        r.setRect(r.getX()+x, r.getY()+y, r.getWidth(), r.getHeight());
48575f27a3cb 8029204: Printing a GlyphVector on Windows ignores position of first glyph
prr
parents:
diff changeset
    56
        g2d.draw(r);
48575f27a3cb 8029204: Printing a GlyphVector on Windows ignores position of first glyph
prr
parents:
diff changeset
    57
48575f27a3cb 8029204: Printing a GlyphVector on Windows ignores position of first glyph
prr
parents:
diff changeset
    58
        Point2D p; // .Float p = new Point2D.Float();
48575f27a3cb 8029204: Printing a GlyphVector on Windows ignores position of first glyph
prr
parents:
diff changeset
    59
        for (int i = 0; i < v.getNumGlyphs(); i++) {
48575f27a3cb 8029204: Printing a GlyphVector on Windows ignores position of first glyph
prr
parents:
diff changeset
    60
            p = v.getGlyphPosition(i);
48575f27a3cb 8029204: Printing a GlyphVector on Windows ignores position of first glyph
prr
parents:
diff changeset
    61
            p.setLocation(p.getX()+50, p.getY());
48575f27a3cb 8029204: Printing a GlyphVector on Windows ignores position of first glyph
prr
parents:
diff changeset
    62
            v.setGlyphPosition(i, p);
48575f27a3cb 8029204: Printing a GlyphVector on Windows ignores position of first glyph
prr
parents:
diff changeset
    63
        }
48575f27a3cb 8029204: Printing a GlyphVector on Windows ignores position of first glyph
prr
parents:
diff changeset
    64
48575f27a3cb 8029204: Printing a GlyphVector on Windows ignores position of first glyph
prr
parents:
diff changeset
    65
        x = 0;
48575f27a3cb 8029204: Printing a GlyphVector on Windows ignores position of first glyph
prr
parents:
diff changeset
    66
        y+= 50;
48575f27a3cb 8029204: Printing a GlyphVector on Windows ignores position of first glyph
prr
parents:
diff changeset
    67
48575f27a3cb 8029204: Printing a GlyphVector on Windows ignores position of first glyph
prr
parents:
diff changeset
    68
        g2d.drawGlyphVector(v, x, y);
48575f27a3cb 8029204: Printing a GlyphVector on Windows ignores position of first glyph
prr
parents:
diff changeset
    69
        r = v.getVisualBounds();
48575f27a3cb 8029204: Printing a GlyphVector on Windows ignores position of first glyph
prr
parents:
diff changeset
    70
        r.setRect(r.getX()+x, r.getY()+y, r.getWidth(), r.getHeight());
48575f27a3cb 8029204: Printing a GlyphVector on Windows ignores position of first glyph
prr
parents:
diff changeset
    71
        g2d.draw(r);
48575f27a3cb 8029204: Printing a GlyphVector on Windows ignores position of first glyph
prr
parents:
diff changeset
    72
48575f27a3cb 8029204: Printing a GlyphVector on Windows ignores position of first glyph
prr
parents:
diff changeset
    73
48575f27a3cb 8029204: Printing a GlyphVector on Windows ignores position of first glyph
prr
parents:
diff changeset
    74
48575f27a3cb 8029204: Printing a GlyphVector on Windows ignores position of first glyph
prr
parents:
diff changeset
    75
    }
48575f27a3cb 8029204: Printing a GlyphVector on Windows ignores position of first glyph
prr
parents:
diff changeset
    76
48575f27a3cb 8029204: Printing a GlyphVector on Windows ignores position of first glyph
prr
parents:
diff changeset
    77
     public void paint(Graphics g) {
48575f27a3cb 8029204: Printing a GlyphVector on Windows ignores position of first glyph
prr
parents:
diff changeset
    78
       g.setColor(Color.white);
48575f27a3cb 8029204: Printing a GlyphVector on Windows ignores position of first glyph
prr
parents:
diff changeset
    79
       g.fillRect(0,0,getSize().width, getSize().height);
48575f27a3cb 8029204: Printing a GlyphVector on Windows ignores position of first glyph
prr
parents:
diff changeset
    80
       drawGVs(g);
48575f27a3cb 8029204: Printing a GlyphVector on Windows ignores position of first glyph
prr
parents:
diff changeset
    81
     }
48575f27a3cb 8029204: Printing a GlyphVector on Windows ignores position of first glyph
prr
parents:
diff changeset
    82
48575f27a3cb 8029204: Printing a GlyphVector on Windows ignores position of first glyph
prr
parents:
diff changeset
    83
    public Dimension getPreferredSize() {
48575f27a3cb 8029204: Printing a GlyphVector on Windows ignores position of first glyph
prr
parents:
diff changeset
    84
        return new Dimension(600,200);
48575f27a3cb 8029204: Printing a GlyphVector on Windows ignores position of first glyph
prr
parents:
diff changeset
    85
    }
48575f27a3cb 8029204: Printing a GlyphVector on Windows ignores position of first glyph
prr
parents:
diff changeset
    86
48575f27a3cb 8029204: Printing a GlyphVector on Windows ignores position of first glyph
prr
parents:
diff changeset
    87
    public int print(Graphics g, PageFormat pf, int pageIndex) {
48575f27a3cb 8029204: Printing a GlyphVector on Windows ignores position of first glyph
prr
parents:
diff changeset
    88
48575f27a3cb 8029204: Printing a GlyphVector on Windows ignores position of first glyph
prr
parents:
diff changeset
    89
        if (pageIndex > 0) {
48575f27a3cb 8029204: Printing a GlyphVector on Windows ignores position of first glyph
prr
parents:
diff changeset
    90
            return Printable.NO_SUCH_PAGE;
48575f27a3cb 8029204: Printing a GlyphVector on Windows ignores position of first glyph
prr
parents:
diff changeset
    91
        }
48575f27a3cb 8029204: Printing a GlyphVector on Windows ignores position of first glyph
prr
parents:
diff changeset
    92
48575f27a3cb 8029204: Printing a GlyphVector on Windows ignores position of first glyph
prr
parents:
diff changeset
    93
        Graphics2D g2d = (Graphics2D)g;
48575f27a3cb 8029204: Printing a GlyphVector on Windows ignores position of first glyph
prr
parents:
diff changeset
    94
        g2d.translate(pf.getImageableX(), pf.getImageableY());
48575f27a3cb 8029204: Printing a GlyphVector on Windows ignores position of first glyph
prr
parents:
diff changeset
    95
        drawGVs(g2d);
48575f27a3cb 8029204: Printing a GlyphVector on Windows ignores position of first glyph
prr
parents:
diff changeset
    96
48575f27a3cb 8029204: Printing a GlyphVector on Windows ignores position of first glyph
prr
parents:
diff changeset
    97
        return Printable.PAGE_EXISTS;
48575f27a3cb 8029204: Printing a GlyphVector on Windows ignores position of first glyph
prr
parents:
diff changeset
    98
    }
48575f27a3cb 8029204: Printing a GlyphVector on Windows ignores position of first glyph
prr
parents:
diff changeset
    99
48575f27a3cb 8029204: Printing a GlyphVector on Windows ignores position of first glyph
prr
parents:
diff changeset
   100
48575f27a3cb 8029204: Printing a GlyphVector on Windows ignores position of first glyph
prr
parents:
diff changeset
   101
    public static void main(String arg[]) throws Exception {
48575f27a3cb 8029204: Printing a GlyphVector on Windows ignores position of first glyph
prr
parents:
diff changeset
   102
48575f27a3cb 8029204: Printing a GlyphVector on Windows ignores position of first glyph
prr
parents:
diff changeset
   103
       Frame f = new Frame();
48575f27a3cb 8029204: Printing a GlyphVector on Windows ignores position of first glyph
prr
parents:
diff changeset
   104
       PrintGlyphVectorTest pvt = new PrintGlyphVectorTest();
48575f27a3cb 8029204: Printing a GlyphVector on Windows ignores position of first glyph
prr
parents:
diff changeset
   105
       f.add("Center", pvt);
48575f27a3cb 8029204: Printing a GlyphVector on Windows ignores position of first glyph
prr
parents:
diff changeset
   106
       f.add("South", new PrintInstructions());
48575f27a3cb 8029204: Printing a GlyphVector on Windows ignores position of first glyph
prr
parents:
diff changeset
   107
       f.pack();
48575f27a3cb 8029204: Printing a GlyphVector on Windows ignores position of first glyph
prr
parents:
diff changeset
   108
       f.show();
48575f27a3cb 8029204: Printing a GlyphVector on Windows ignores position of first glyph
prr
parents:
diff changeset
   109
48575f27a3cb 8029204: Printing a GlyphVector on Windows ignores position of first glyph
prr
parents:
diff changeset
   110
48575f27a3cb 8029204: Printing a GlyphVector on Windows ignores position of first glyph
prr
parents:
diff changeset
   111
    }
48575f27a3cb 8029204: Printing a GlyphVector on Windows ignores position of first glyph
prr
parents:
diff changeset
   112
}
48575f27a3cb 8029204: Printing a GlyphVector on Windows ignores position of first glyph
prr
parents:
diff changeset
   113
48575f27a3cb 8029204: Printing a GlyphVector on Windows ignores position of first glyph
prr
parents:
diff changeset
   114
class PrintInstructions extends Panel implements ActionListener {
48575f27a3cb 8029204: Printing a GlyphVector on Windows ignores position of first glyph
prr
parents:
diff changeset
   115
48575f27a3cb 8029204: Printing a GlyphVector on Windows ignores position of first glyph
prr
parents:
diff changeset
   116
   static final String INSTRUCTIONS =
48575f27a3cb 8029204: Printing a GlyphVector on Windows ignores position of first glyph
prr
parents:
diff changeset
   117
       "You must have a printer installed for this test.\n" +
48575f27a3cb 8029204: Printing a GlyphVector on Windows ignores position of first glyph
prr
parents:
diff changeset
   118
       "Press the PRINT button below and OK the print dialog\n" +
48575f27a3cb 8029204: Printing a GlyphVector on Windows ignores position of first glyph
prr
parents:
diff changeset
   119
       "Retrieve the output and compare the printed and on-screen text\n" +
48575f27a3cb 8029204: Printing a GlyphVector on Windows ignores position of first glyph
prr
parents:
diff changeset
   120
       " to confirm that in both cases the text is aligned and the boxes\n" +
48575f27a3cb 8029204: Printing a GlyphVector on Windows ignores position of first glyph
prr
parents:
diff changeset
   121
       "are around the text, not offset from the text.";
48575f27a3cb 8029204: Printing a GlyphVector on Windows ignores position of first glyph
prr
parents:
diff changeset
   122
48575f27a3cb 8029204: Printing a GlyphVector on Windows ignores position of first glyph
prr
parents:
diff changeset
   123
  PrintInstructions() {
48575f27a3cb 8029204: Printing a GlyphVector on Windows ignores position of first glyph
prr
parents:
diff changeset
   124
48575f27a3cb 8029204: Printing a GlyphVector on Windows ignores position of first glyph
prr
parents:
diff changeset
   125
     setLayout(new GridLayout(2,1));
48575f27a3cb 8029204: Printing a GlyphVector on Windows ignores position of first glyph
prr
parents:
diff changeset
   126
     TextArea t = new TextArea(INSTRUCTIONS, 8, 80);
48575f27a3cb 8029204: Printing a GlyphVector on Windows ignores position of first glyph
prr
parents:
diff changeset
   127
     add(t);
48575f27a3cb 8029204: Printing a GlyphVector on Windows ignores position of first glyph
prr
parents:
diff changeset
   128
     Button b = new Button("PRINT");
48575f27a3cb 8029204: Printing a GlyphVector on Windows ignores position of first glyph
prr
parents:
diff changeset
   129
     b.setFont(new Font("Dialog", Font.BOLD, 30));
48575f27a3cb 8029204: Printing a GlyphVector on Windows ignores position of first glyph
prr
parents:
diff changeset
   130
     b.addActionListener(this);
48575f27a3cb 8029204: Printing a GlyphVector on Windows ignores position of first glyph
prr
parents:
diff changeset
   131
     add(b);
48575f27a3cb 8029204: Printing a GlyphVector on Windows ignores position of first glyph
prr
parents:
diff changeset
   132
  }
48575f27a3cb 8029204: Printing a GlyphVector on Windows ignores position of first glyph
prr
parents:
diff changeset
   133
48575f27a3cb 8029204: Printing a GlyphVector on Windows ignores position of first glyph
prr
parents:
diff changeset
   134
  public void actionPerformed(ActionEvent e) {
48575f27a3cb 8029204: Printing a GlyphVector on Windows ignores position of first glyph
prr
parents:
diff changeset
   135
       PrinterJob pj = PrinterJob.getPrinterJob();
48575f27a3cb 8029204: Printing a GlyphVector on Windows ignores position of first glyph
prr
parents:
diff changeset
   136
       if (pj == null ||
48575f27a3cb 8029204: Printing a GlyphVector on Windows ignores position of first glyph
prr
parents:
diff changeset
   137
           pj.getPrintService() == null ||
48575f27a3cb 8029204: Printing a GlyphVector on Windows ignores position of first glyph
prr
parents:
diff changeset
   138
           !pj.printDialog()) {
48575f27a3cb 8029204: Printing a GlyphVector on Windows ignores position of first glyph
prr
parents:
diff changeset
   139
           return;
48575f27a3cb 8029204: Printing a GlyphVector on Windows ignores position of first glyph
prr
parents:
diff changeset
   140
       }
48575f27a3cb 8029204: Printing a GlyphVector on Windows ignores position of first glyph
prr
parents:
diff changeset
   141
48575f27a3cb 8029204: Printing a GlyphVector on Windows ignores position of first glyph
prr
parents:
diff changeset
   142
       pj.setPrintable(new PrintGlyphVectorTest());
48575f27a3cb 8029204: Printing a GlyphVector on Windows ignores position of first glyph
prr
parents:
diff changeset
   143
       try {
48575f27a3cb 8029204: Printing a GlyphVector on Windows ignores position of first glyph
prr
parents:
diff changeset
   144
           pj.print();
48575f27a3cb 8029204: Printing a GlyphVector on Windows ignores position of first glyph
prr
parents:
diff changeset
   145
       } catch (PrinterException ex) {
48575f27a3cb 8029204: Printing a GlyphVector on Windows ignores position of first glyph
prr
parents:
diff changeset
   146
           System.err.println(ex);
48575f27a3cb 8029204: Printing a GlyphVector on Windows ignores position of first glyph
prr
parents:
diff changeset
   147
       }
48575f27a3cb 8029204: Printing a GlyphVector on Windows ignores position of first glyph
prr
parents:
diff changeset
   148
  }
48575f27a3cb 8029204: Printing a GlyphVector on Windows ignores position of first glyph
prr
parents:
diff changeset
   149
48575f27a3cb 8029204: Printing a GlyphVector on Windows ignores position of first glyph
prr
parents:
diff changeset
   150
}