test/jdk/java/awt/font/Rotate/RotatedFontMetricsTest.java
author bae
Fri, 30 Nov 2018 23:21:05 +0300
changeset 52838 df92f1126c58
permissions -rw-r--r--
8139178: Wrong fontMetrics when printing in Landscape (OpenJDK) Reviewed-by: prr
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
52838
df92f1126c58 8139178: Wrong fontMetrics when printing in Landscape (OpenJDK)
bae
parents:
diff changeset
     1
/*
df92f1126c58 8139178: Wrong fontMetrics when printing in Landscape (OpenJDK)
bae
parents:
diff changeset
     2
 * Copyright (c) 2018, Oracle and/or its affiliates. All rights reserved.
df92f1126c58 8139178: Wrong fontMetrics when printing in Landscape (OpenJDK)
bae
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
df92f1126c58 8139178: Wrong fontMetrics when printing in Landscape (OpenJDK)
bae
parents:
diff changeset
     4
 *
df92f1126c58 8139178: Wrong fontMetrics when printing in Landscape (OpenJDK)
bae
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
df92f1126c58 8139178: Wrong fontMetrics when printing in Landscape (OpenJDK)
bae
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
df92f1126c58 8139178: Wrong fontMetrics when printing in Landscape (OpenJDK)
bae
parents:
diff changeset
     7
 * published by the Free Software Foundation.
df92f1126c58 8139178: Wrong fontMetrics when printing in Landscape (OpenJDK)
bae
parents:
diff changeset
     8
 *
df92f1126c58 8139178: Wrong fontMetrics when printing in Landscape (OpenJDK)
bae
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
df92f1126c58 8139178: Wrong fontMetrics when printing in Landscape (OpenJDK)
bae
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
df92f1126c58 8139178: Wrong fontMetrics when printing in Landscape (OpenJDK)
bae
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
df92f1126c58 8139178: Wrong fontMetrics when printing in Landscape (OpenJDK)
bae
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
df92f1126c58 8139178: Wrong fontMetrics when printing in Landscape (OpenJDK)
bae
parents:
diff changeset
    13
 * accompanied this code).
df92f1126c58 8139178: Wrong fontMetrics when printing in Landscape (OpenJDK)
bae
parents:
diff changeset
    14
 *
df92f1126c58 8139178: Wrong fontMetrics when printing in Landscape (OpenJDK)
bae
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
df92f1126c58 8139178: Wrong fontMetrics when printing in Landscape (OpenJDK)
bae
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
df92f1126c58 8139178: Wrong fontMetrics when printing in Landscape (OpenJDK)
bae
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
df92f1126c58 8139178: Wrong fontMetrics when printing in Landscape (OpenJDK)
bae
parents:
diff changeset
    18
 *
df92f1126c58 8139178: Wrong fontMetrics when printing in Landscape (OpenJDK)
bae
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
df92f1126c58 8139178: Wrong fontMetrics when printing in Landscape (OpenJDK)
bae
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
df92f1126c58 8139178: Wrong fontMetrics when printing in Landscape (OpenJDK)
bae
parents:
diff changeset
    21
 * questions.
df92f1126c58 8139178: Wrong fontMetrics when printing in Landscape (OpenJDK)
bae
parents:
diff changeset
    22
 */
df92f1126c58 8139178: Wrong fontMetrics when printing in Landscape (OpenJDK)
bae
parents:
diff changeset
    23
df92f1126c58 8139178: Wrong fontMetrics when printing in Landscape (OpenJDK)
bae
parents:
diff changeset
    24
/*
df92f1126c58 8139178: Wrong fontMetrics when printing in Landscape (OpenJDK)
bae
parents:
diff changeset
    25
 * @test    RotatedFontMetricsTest
df92f1126c58 8139178: Wrong fontMetrics when printing in Landscape (OpenJDK)
bae
parents:
diff changeset
    26
 * @bug     8139178
df92f1126c58 8139178: Wrong fontMetrics when printing in Landscape (OpenJDK)
bae
parents:
diff changeset
    27
 * @summary This test verifies that rotation does not affect font metrics.
df92f1126c58 8139178: Wrong fontMetrics when printing in Landscape (OpenJDK)
bae
parents:
diff changeset
    28
 * @run     main RotatedFontMetricsTest
df92f1126c58 8139178: Wrong fontMetrics when printing in Landscape (OpenJDK)
bae
parents:
diff changeset
    29
 */
df92f1126c58 8139178: Wrong fontMetrics when printing in Landscape (OpenJDK)
bae
parents:
diff changeset
    30
df92f1126c58 8139178: Wrong fontMetrics when printing in Landscape (OpenJDK)
bae
parents:
diff changeset
    31
import java.awt.Font;
df92f1126c58 8139178: Wrong fontMetrics when printing in Landscape (OpenJDK)
bae
parents:
diff changeset
    32
import java.awt.FontMetrics;
df92f1126c58 8139178: Wrong fontMetrics when printing in Landscape (OpenJDK)
bae
parents:
diff changeset
    33
import java.awt.Graphics2D;
df92f1126c58 8139178: Wrong fontMetrics when printing in Landscape (OpenJDK)
bae
parents:
diff changeset
    34
import java.awt.image.BufferedImage;
df92f1126c58 8139178: Wrong fontMetrics when printing in Landscape (OpenJDK)
bae
parents:
diff changeset
    35
df92f1126c58 8139178: Wrong fontMetrics when printing in Landscape (OpenJDK)
bae
parents:
diff changeset
    36
public class RotatedFontMetricsTest {
df92f1126c58 8139178: Wrong fontMetrics when printing in Landscape (OpenJDK)
bae
parents:
diff changeset
    37
    static final int FONT_SIZE = Integer.getInteger("font.size", 20);
df92f1126c58 8139178: Wrong fontMetrics when printing in Landscape (OpenJDK)
bae
parents:
diff changeset
    38
df92f1126c58 8139178: Wrong fontMetrics when printing in Landscape (OpenJDK)
bae
parents:
diff changeset
    39
    public static void main(String ... args) {
df92f1126c58 8139178: Wrong fontMetrics when printing in Landscape (OpenJDK)
bae
parents:
diff changeset
    40
        Font font = new Font(Font.DIALOG, Font.PLAIN, FONT_SIZE);
df92f1126c58 8139178: Wrong fontMetrics when printing in Landscape (OpenJDK)
bae
parents:
diff changeset
    41
        Graphics2D g2d = createGraphics();
df92f1126c58 8139178: Wrong fontMetrics when printing in Landscape (OpenJDK)
bae
parents:
diff changeset
    42
df92f1126c58 8139178: Wrong fontMetrics when printing in Landscape (OpenJDK)
bae
parents:
diff changeset
    43
        FontMetrics ref = null;
df92f1126c58 8139178: Wrong fontMetrics when printing in Landscape (OpenJDK)
bae
parents:
diff changeset
    44
        RuntimeException failure = null;
df92f1126c58 8139178: Wrong fontMetrics when printing in Landscape (OpenJDK)
bae
parents:
diff changeset
    45
        for (int a = 0; a < 360; a += 15) {
df92f1126c58 8139178: Wrong fontMetrics when printing in Landscape (OpenJDK)
bae
parents:
diff changeset
    46
            Graphics2D g = (Graphics2D)g2d.create();
df92f1126c58 8139178: Wrong fontMetrics when printing in Landscape (OpenJDK)
bae
parents:
diff changeset
    47
            g.rotate(Math.toRadians(a));
df92f1126c58 8139178: Wrong fontMetrics when printing in Landscape (OpenJDK)
bae
parents:
diff changeset
    48
            FontMetrics m = g.getFontMetrics(font);
df92f1126c58 8139178: Wrong fontMetrics when printing in Landscape (OpenJDK)
bae
parents:
diff changeset
    49
            g.dispose();
df92f1126c58 8139178: Wrong fontMetrics when printing in Landscape (OpenJDK)
bae
parents:
diff changeset
    50
df92f1126c58 8139178: Wrong fontMetrics when printing in Landscape (OpenJDK)
bae
parents:
diff changeset
    51
            boolean status = true;
df92f1126c58 8139178: Wrong fontMetrics when printing in Landscape (OpenJDK)
bae
parents:
diff changeset
    52
            if (ref == null) {
df92f1126c58 8139178: Wrong fontMetrics when printing in Landscape (OpenJDK)
bae
parents:
diff changeset
    53
                ref = m;
df92f1126c58 8139178: Wrong fontMetrics when printing in Landscape (OpenJDK)
bae
parents:
diff changeset
    54
            } else {
df92f1126c58 8139178: Wrong fontMetrics when printing in Landscape (OpenJDK)
bae
parents:
diff changeset
    55
                status = ref.getAscent() == m.getAscent() &&
df92f1126c58 8139178: Wrong fontMetrics when printing in Landscape (OpenJDK)
bae
parents:
diff changeset
    56
                        ref.getDescent() == m.getDescent() &&
df92f1126c58 8139178: Wrong fontMetrics when printing in Landscape (OpenJDK)
bae
parents:
diff changeset
    57
                        ref.getLeading() == m.getLeading() &&
df92f1126c58 8139178: Wrong fontMetrics when printing in Landscape (OpenJDK)
bae
parents:
diff changeset
    58
                        ref.getMaxAdvance() == m.getMaxAdvance();
df92f1126c58 8139178: Wrong fontMetrics when printing in Landscape (OpenJDK)
bae
parents:
diff changeset
    59
            }
df92f1126c58 8139178: Wrong fontMetrics when printing in Landscape (OpenJDK)
bae
parents:
diff changeset
    60
df92f1126c58 8139178: Wrong fontMetrics when printing in Landscape (OpenJDK)
bae
parents:
diff changeset
    61
            System.out.printf("Metrics a%d, d%d, l%d, m%d (%d) %s\n",
df92f1126c58 8139178: Wrong fontMetrics when printing in Landscape (OpenJDK)
bae
parents:
diff changeset
    62
                    m.getAscent(), m.getDescent(), m.getLeading(), m.getMaxAdvance(),
df92f1126c58 8139178: Wrong fontMetrics when printing in Landscape (OpenJDK)
bae
parents:
diff changeset
    63
                    (int)a, status ? "OK" : "FAIL");
df92f1126c58 8139178: Wrong fontMetrics when printing in Landscape (OpenJDK)
bae
parents:
diff changeset
    64
df92f1126c58 8139178: Wrong fontMetrics when printing in Landscape (OpenJDK)
bae
parents:
diff changeset
    65
            if (!status && failure == null) {
df92f1126c58 8139178: Wrong fontMetrics when printing in Landscape (OpenJDK)
bae
parents:
diff changeset
    66
                failure = new RuntimeException("Font metrics differ for angle " + a);
df92f1126c58 8139178: Wrong fontMetrics when printing in Landscape (OpenJDK)
bae
parents:
diff changeset
    67
            }
df92f1126c58 8139178: Wrong fontMetrics when printing in Landscape (OpenJDK)
bae
parents:
diff changeset
    68
        }
df92f1126c58 8139178: Wrong fontMetrics when printing in Landscape (OpenJDK)
bae
parents:
diff changeset
    69
        if (failure != null) {
df92f1126c58 8139178: Wrong fontMetrics when printing in Landscape (OpenJDK)
bae
parents:
diff changeset
    70
            throw failure;
df92f1126c58 8139178: Wrong fontMetrics when printing in Landscape (OpenJDK)
bae
parents:
diff changeset
    71
        }
df92f1126c58 8139178: Wrong fontMetrics when printing in Landscape (OpenJDK)
bae
parents:
diff changeset
    72
        System.out.println("done");
df92f1126c58 8139178: Wrong fontMetrics when printing in Landscape (OpenJDK)
bae
parents:
diff changeset
    73
    }
df92f1126c58 8139178: Wrong fontMetrics when printing in Landscape (OpenJDK)
bae
parents:
diff changeset
    74
df92f1126c58 8139178: Wrong fontMetrics when printing in Landscape (OpenJDK)
bae
parents:
diff changeset
    75
    private static Graphics2D createGraphics() {
df92f1126c58 8139178: Wrong fontMetrics when printing in Landscape (OpenJDK)
bae
parents:
diff changeset
    76
        BufferedImage dst = new BufferedImage(100, 100, BufferedImage.TYPE_INT_RGB);
df92f1126c58 8139178: Wrong fontMetrics when printing in Landscape (OpenJDK)
bae
parents:
diff changeset
    77
        return dst.createGraphics();
df92f1126c58 8139178: Wrong fontMetrics when printing in Landscape (OpenJDK)
bae
parents:
diff changeset
    78
    }
df92f1126c58 8139178: Wrong fontMetrics when printing in Landscape (OpenJDK)
bae
parents:
diff changeset
    79
}