aboutsummaryrefslogtreecommitdiff
path: root/test/java/awt/Graphics2D/DrawString/DrawRotatedStringUsingRotatedFont.java
blob: 9648e68888ea315cb0cc412a2b8379fcfd554177 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
/*
 * Copyright (c) 2014, Oracle and/or its affiliates. All rights reserved.
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 *
 * This code is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License version 2 only, as
 * published by the Free Software Foundation.
 *
 * This code is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * version 2 for more details (a copy is included in the LICENSE file that
 * accompanied this code).
 *
 * You should have received a copy of the GNU General Public License version
 * 2 along with this work; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 *
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 * or visit www.oracle.com if you need additional information or have any
 * questions.
 */

import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.geom.AffineTransform;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;

import static java.awt.image.BufferedImage.TYPE_INT_RGB;
import static java.lang.Math.toRadians;

/**
 * @test
 * @bug 8065373
 * @summary Verifies that we get correct direction, when draw rotated string.
 * @author Sergey Bylokhov
 * @run main DrawRotatedStringUsingRotatedFont
 */
public final class DrawRotatedStringUsingRotatedFont {

    private static final int SIZE = 500;
    private static final String STR = "MMMMMMMMMMMMMMMM";

    private static AffineTransform[] txs = {
                            AffineTransform.getRotateInstance(toRadians(00)),
                            AffineTransform.getRotateInstance(toRadians(45)),
                            AffineTransform.getRotateInstance(toRadians(-45)),
                            AffineTransform.getRotateInstance(toRadians(90)),
                            AffineTransform.getRotateInstance(toRadians(-90)),
                            AffineTransform.getRotateInstance(toRadians(135)),
                            AffineTransform.getRotateInstance(toRadians(-135)),
                            AffineTransform.getRotateInstance(toRadians(180)),
                            AffineTransform.getRotateInstance(toRadians(-180)),
                            AffineTransform.getRotateInstance(toRadians(225)),
                            AffineTransform.getRotateInstance(toRadians(-225)),
                            AffineTransform.getRotateInstance(toRadians(270)),
                            AffineTransform.getRotateInstance(toRadians(-270)),
                            AffineTransform.getRotateInstance(toRadians(315)),
                            AffineTransform.getRotateInstance(toRadians(-315)),
                            AffineTransform.getRotateInstance(toRadians(360)),
                            AffineTransform.getRotateInstance(toRadians(-360))
    };

    public static void main(final String[] args) throws IOException {
        for (final AffineTransform tx2 : txs) {
            for (final AffineTransform tx1 : txs) {
                for (final boolean aa : new boolean[]{true, false}) {
                    final BufferedImage bi1 = createImage(aa, tx1, tx2);
                    final BufferedImage bi2 = createImage(aa, tx2, tx1);
                    compareImage(bi1, bi2);
                    fillTextArea(bi1, tx1, tx2);
                    fillTextArea(bi2, tx2, tx1);
                    checkColors(bi1, bi2);
                }
            }
        }
        System.out.println("Passed");
    }

    /**
     * Compares two images.
     */
    private static void compareImage(final BufferedImage bi1,
                                     final BufferedImage bi2)
            throws IOException {
        for (int i = 0; i < SIZE; ++i) {
            for (int j = 0; j < SIZE; ++j) {
                if (bi1.getRGB(i, j) != bi2.getRGB(i, j)) {
                    ImageIO.write(bi1, "png", new File("image1.png"));
                    ImageIO.write(bi2, "png", new File("image2.png"));
                    throw new RuntimeException("Failed: wrong text location");
                }
            }
        }
    }

    /**
     * Checks an image color. RED and GREEN are allowed only.
     */
    private static void checkColors(final BufferedImage bi1,
                                    final BufferedImage bi2)
            throws IOException {
        for (int i = 0; i < SIZE; ++i) {
            for (int j = 0; j < SIZE; ++j) {
                final int rgb1 = bi1.getRGB(i, j);
                final int rgb2 = bi2.getRGB(i, j);
                if (rgb1 != rgb2 || rgb1 != 0xFFFF0000 && rgb1 != 0xFF00FF00) {
                    ImageIO.write(bi1, "png", new File("image1.png"));
                    ImageIO.write(bi2, "png", new File("image2.png"));
                    throw new RuntimeException("Failed: wrong text location");
                }
            }
        }
    }

    /**
     * Creates an BufferedImage and draws a text, using two transformations,
     * one for graphics and one for font.
     */
    private static BufferedImage createImage(final boolean aa,
                                             final AffineTransform gtx,
                                             final AffineTransform ftx) {
        final BufferedImage bi = new BufferedImage(SIZE, SIZE, TYPE_INT_RGB);
        final Graphics2D bg = bi.createGraphics();
        bg.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
                            aa ? RenderingHints.VALUE_ANTIALIAS_ON
                               : RenderingHints.VALUE_ANTIALIAS_OFF);
        bg.setColor(Color.RED);
        bg.fillRect(0, 0, SIZE, SIZE);
        bg.translate(100, 100);
        bg.transform(gtx);
        bg.setColor(Color.BLACK);
        bg.setFont(bg.getFont().deriveFont(20.0f).deriveFont(ftx));
        bg.drawString(STR, 0, 0);
        bg.dispose();
        return bi;
    }

    /**
     * Fills the area of text using green solid color.
     */
    private static void fillTextArea(final BufferedImage bi,
                                     final AffineTransform tx1,
                                     final AffineTransform tx2) {
        final Graphics2D bg = bi.createGraphics();
        bg.translate(100, 100);
        bg.transform(tx1);
        bg.transform(tx2);
        bg.setColor(Color.GREEN);
        final Font font = bg.getFont().deriveFont(20.0f);
        bg.setFont(font);
        bg.fill(font.getStringBounds(STR, bg.getFontRenderContext()));
        bg.dispose();
    }
}