test/jdk/java/awt/font/Rotate/RotatedTextTest.java
changeset 50346 9e530b150333
equal deleted inserted replaced
50345:276cb4b17f79 50346:9e530b150333
       
     1 /*
       
     2  * Copyright (c) 2018, 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 RotatedTextTest
       
    26  * @bug 8203485
       
    27  * @summary This test verifies that rotated text preserves the width.
       
    28  * @run main RotatedTextTest
       
    29  */
       
    30 
       
    31 import java.awt.Color;
       
    32 import java.awt.Dimension;
       
    33 import java.awt.Font;
       
    34 import java.awt.FontMetrics;
       
    35 import java.awt.Graphics;
       
    36 import java.awt.Graphics2D;
       
    37 import java.awt.geom.Rectangle2D;
       
    38 import java.awt.image.BufferedImage;
       
    39 import java.io.BufferedOutputStream;
       
    40 import java.io.File;
       
    41 import java.io.FileOutputStream;
       
    42 import javax.imageio.ImageIO;
       
    43 
       
    44 public class RotatedTextTest
       
    45 {
       
    46     static final int size = 720;
       
    47     static final Font fnt = new Font(Font.SERIF, Font.PLAIN, 12);
       
    48     static final String text = "The quick brown fox jumps over the lazy dog";
       
    49 
       
    50     static void drawRotatedText(Graphics g) {
       
    51         Graphics2D g2d = (Graphics2D)g;
       
    52 
       
    53         g2d.setFont(fnt);
       
    54 
       
    55         FontMetrics metrics = g2d.getFontMetrics();
       
    56         Rectangle2D bounds = metrics.getStringBounds(text, g2d);
       
    57 
       
    58 
       
    59         g2d.setColor(Color.black);
       
    60         g2d.fillRect(0, 0, size, size);
       
    61 
       
    62         g2d.setColor(Color.white);
       
    63 
       
    64         float[] angles = new float[] { 0, 15, 45, 60, 90, -45, -90, -135, -180, 135, };
       
    65 
       
    66         for (float a : angles) {
       
    67             Graphics2D rotated = (Graphics2D)g2d.create();
       
    68 
       
    69             rotated.translate(size/2, size/2);
       
    70             rotated.rotate(Math.toRadians(a));
       
    71             rotated.translate(30, 0);
       
    72 
       
    73             int width_original = metrics.stringWidth(text);
       
    74             int width_rotated = rotated.getFontMetrics().stringWidth(text);
       
    75 
       
    76             rotated.drawString(text, 0, 0);
       
    77             rotated.drawString(String.format("  %.0f", a), width_original, 0);
       
    78 
       
    79             rotated.setColor(Color.green);
       
    80             rotated.draw(bounds);
       
    81 
       
    82             System.out.printf("Angle: %.0f, width diff: %d\n", a, (width_rotated - width_original));
       
    83 
       
    84             rotated.dispose();
       
    85 
       
    86             if (width_rotated != width_original) {
       
    87                 throw new RuntimeException("Test failed for angle " + a);
       
    88             }
       
    89         }
       
    90     }
       
    91 
       
    92     public static void main(String args[])
       
    93     {
       
    94         final BufferedImage dst = new BufferedImage(size, size, BufferedImage.TYPE_INT_RGB);
       
    95         Graphics g = dst.createGraphics();
       
    96 
       
    97         try {
       
    98             drawRotatedText(g);
       
    99         } finally {
       
   100             g.dispose();
       
   101             writeToFile(dst);
       
   102         }
       
   103     }
       
   104 
       
   105     static final String title;
       
   106     static final String file;
       
   107 
       
   108     static {
       
   109         String vendorName = System.getProperty("java.vendor");
       
   110         String version = System.getProperty("java.version");
       
   111         String runtimeName = System.getProperty("java.runtime.name");
       
   112 
       
   113 
       
   114         int index = runtimeName.indexOf(" Runtime Environment");
       
   115         runtimeName = runtimeName.substring(0, index).trim();
       
   116 
       
   117         title = vendorName + ", " + runtimeName + ", " + version;
       
   118         file = "rotated-text-" + title.replace(", ", "-")
       
   119                 .replace(" ", "-") + ".png";
       
   120     }
       
   121 
       
   122     private static void writeToFile(final BufferedImage bi) {
       
   123         File imageFile = new File(file);
       
   124         FileOutputStream imageOutStream;
       
   125         BufferedOutputStream imageBOS = null;
       
   126         try {
       
   127             imageOutStream = new FileOutputStream(imageFile);
       
   128             imageBOS = new BufferedOutputStream(imageOutStream);
       
   129 
       
   130             ImageIO.setUseCache(false);
       
   131             ImageIO.write(bi, "png", imageBOS);
       
   132 
       
   133             imageBOS.close();
       
   134             imageOutStream.close();
       
   135         }
       
   136         catch (Exception e) {
       
   137             System.out.println(e.getMessage());
       
   138         }
       
   139     }
       
   140 }