6126N/A/*
6126N/A * Copyright (c) 2013, Oracle and/or its affiliates. All rights reserved.
6126N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
6126N/A *
6126N/A * This code is free software; you can redistribute it and/or modify it
6126N/A * under the terms of the GNU General Public License version 2 only, as
6126N/A * published by the Free Software Foundation.
6126N/A *
6126N/A * This code is distributed in the hope that it will be useful, but WITHOUT
6126N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
6126N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
6126N/A * version 2 for more details (a copy is included in the LICENSE file that
6126N/A * accompanied this code).
6126N/A *
6126N/A * You should have received a copy of the GNU General Public License version
6126N/A * 2 along with this work; if not, write to the Free Software Foundation,
6126N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
6126N/A *
6126N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
6126N/A * or visit www.oracle.com if you need additional information or have any
6126N/A * questions.
6126N/A */
6126N/A/**
6126N/A * @test
6126N/A * @bug 8015606
6126N/A * @summary Test verifies whether a text is rendered correctly to
6126N/A * a custom buffered image.
6126N/A *
6126N/A * @run main RenderToCustomBufferTest
6126N/A */
6126N/A
6126N/Aimport java.awt.Color;
6126N/Aimport java.awt.Font;
6126N/Aimport java.awt.Graphics2D;
6126N/Aimport java.awt.RenderingHints;
6126N/Aimport java.awt.Transparency;
6126N/Aimport java.awt.color.ColorSpace;
6126N/Aimport java.awt.image.BufferedImage;
6126N/Aimport java.awt.image.ColorModel;
6126N/Aimport java.awt.image.ComponentColorModel;
6126N/Aimport java.awt.image.DataBuffer;
6126N/Aimport java.awt.image.WritableRaster;
6126N/A
6126N/Apublic class RenderToCustomBufferTest {
6126N/A public static void main(String[] args) {
6126N/A final BufferedImage dst_custom = createCustomBuffer();
6126N/A final BufferedImage dst_dcm = new BufferedImage(width, height,
6126N/A BufferedImage.TYPE_INT_RGB);
6126N/A
6126N/A renderTo(dst_custom);
6126N/A renderTo(dst_dcm);
6126N/A
6126N/A check(dst_custom, dst_dcm);
6126N/A }
6126N/A
6126N/A private static void check(BufferedImage a, BufferedImage b) {
6126N/A for (int y = 0; y < height; y++) {
6126N/A for (int x = 0; x < width; x++) {
6126N/A int pa = a.getRGB(x, y);
6126N/A int pb = b.getRGB(x, y);
6126N/A
6126N/A if (pa != pb) {
6126N/A String msg = String.format(
6126N/A "Point [%d, %d] has different colors: %08X and %08X",
6126N/A x, y, pa, pb);
6126N/A throw new RuntimeException("Test failed: " + msg);
6126N/A }
6126N/A }
6126N/A }
6126N/A }
6126N/A
6126N/A private static BufferedImage createCustomBuffer() {
6126N/A ColorSpace cs = ColorSpace.getInstance(ColorSpace.CS_sRGB);
6126N/A ColorModel cm = new ComponentColorModel(cs, false, false,
6126N/A Transparency.OPAQUE, DataBuffer.TYPE_FLOAT);
6126N/A WritableRaster wr = cm.createCompatibleWritableRaster(width, height);
6126N/A
6126N/A return new BufferedImage(cm, wr, false, null);
6126N/A }
6126N/A
6126N/A private static void renderTo(BufferedImage dst) {
6126N/A System.out.println("The buffer: " + dst);
6126N/A Graphics2D g = dst.createGraphics();
6126N/A
6126N/A final int w = dst.getWidth();
6126N/A final int h = dst.getHeight();
6126N/A
6126N/A g.setColor(Color.blue);
6126N/A g.fillRect(0, 0, w, h);
6126N/A
6126N/A g.setColor(Color.red);
6126N/A Font f = g.getFont();
6126N/A g.setFont(f.deriveFont(48f));
6126N/A
6126N/A g.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING,
6126N/A RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
6126N/A
6126N/A // NB: this clip ctriggers the problem
6126N/A g.setClip(50, 50, 200, 100);
6126N/A
6126N/A g.drawString("AA Text", 52, 90);
6126N/A
6126N/A g.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING,
6126N/A RenderingHints.VALUE_TEXT_ANTIALIAS_OFF);
6126N/A
6126N/A // NB: this clip ctriggers the problem
6126N/A g.setClip(50, 100, 100, 100);
6126N/A g.drawString("Text", 52, 148);
6126N/A
6126N/A g.dispose();
6126N/A }
6126N/A
6126N/A private static final int width = 230;
6126N/A private static final int height = 150;
6126N/A}