1/*
2 * Copyright (c) 2013, 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 * @test
25 * @bug     8015606
26 * @summary Test verifies whether a text is rendered correctly to
27 *          a custom buffered image.
28 *
29 * @run     main RenderToCustomBufferTest
30 */
31
32import java.awt.Color;
33import java.awt.Font;
34import java.awt.Graphics2D;
35import java.awt.RenderingHints;
36import java.awt.Transparency;
37import java.awt.color.ColorSpace;
38import java.awt.image.BufferedImage;
39import java.awt.image.ColorModel;
40import java.awt.image.ComponentColorModel;
41import java.awt.image.DataBuffer;
42import java.awt.image.WritableRaster;
43
44public class RenderToCustomBufferTest {
45     public static void main(String[] args) {
46        final BufferedImage dst_custom = createCustomBuffer();
47        final BufferedImage dst_dcm = new BufferedImage(width, height,
48                BufferedImage.TYPE_INT_RGB);
49
50        renderTo(dst_custom);
51        renderTo(dst_dcm);
52
53        check(dst_custom, dst_dcm);
54    }
55
56    private static void check(BufferedImage a, BufferedImage b) {
57        for (int y = 0; y < height; y++) {
58            for (int x = 0; x < width; x++) {
59                int pa = a.getRGB(x, y);
60                int pb = b.getRGB(x, y);
61
62                if (pa != pb) {
63                    String msg = String.format(
64                        "Point [%d, %d] has different colors: %08X and %08X",
65                        x, y, pa, pb);
66                    throw new RuntimeException("Test failed: " + msg);
67                }
68            }
69        }
70    }
71
72    private static BufferedImage createCustomBuffer() {
73        ColorSpace cs = ColorSpace.getInstance(ColorSpace.CS_sRGB);
74        ColorModel cm = new ComponentColorModel(cs, false, false,
75                Transparency.OPAQUE, DataBuffer.TYPE_FLOAT);
76        WritableRaster wr = cm.createCompatibleWritableRaster(width, height);
77
78        return new BufferedImage(cm, wr, false, null);
79    }
80
81    private static void renderTo(BufferedImage dst) {
82        System.out.println("The buffer: " + dst);
83        Graphics2D g = dst.createGraphics();
84
85        final int w = dst.getWidth();
86        final int h = dst.getHeight();
87
88        g.setColor(Color.blue);
89        g.fillRect(0, 0, w, h);
90
91        g.setColor(Color.red);
92        Font f = g.getFont();
93        g.setFont(f.deriveFont(48f));
94
95        g.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING,
96               RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
97
98        // NB: this clip ctriggers the problem
99        g.setClip(50, 50, 200, 100);
100
101        g.drawString("AA Text", 52, 90);
102
103        g.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING,
104               RenderingHints.VALUE_TEXT_ANTIALIAS_OFF);
105
106        // NB: this clip ctriggers the problem
107        g.setClip(50, 100, 100, 100);
108        g.drawString("Text", 52, 148);
109
110        g.dispose();
111    }
112
113    private static final int width = 230;
114    private static final int height = 150;
115}
116