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 import java.awt.Color;
  25 import java.awt.Graphics2D;
  26 import java.awt.GraphicsConfiguration;
  27 import java.awt.GraphicsEnvironment;
  28 import java.awt.GradientPaint;
  29 import java.awt.geom.Point2D;
  30 
  31 import java.awt.Font;
  32 
  33 import java.awt.image.BufferedImage;
  34 import java.awt.image.VolatileImage;
  35 
  36 
  37 /*
  38  * @test
  39  * @bug 7189452 8024767
  40  * @summary Check if source offset for text rendering is handled correctly
  41  *          (shouldn't see the text on a similarly colored background).
  42  * @author a.stepanov
  43  * @run main/othervm -Dsun.java2d.xrender=True TextRenderingTest
  44  */
  45 
  46 public class TextRenderingTest {
  47 
  48     private static final int width  = 450;
  49     private static final int height = 150;
  50 
  51     public static void main(final String[] args) {
  52 
  53         GraphicsEnvironment ge =
  54             GraphicsEnvironment.getLocalGraphicsEnvironment();
  55         GraphicsConfiguration gc =
  56             ge.getDefaultScreenDevice().getDefaultConfiguration();
  57         VolatileImage vi = gc.createCompatibleVolatileImage(width, height);
  58 
  59         while (true) {
  60             vi.validate(gc);
  61             Graphics2D g2d = vi.createGraphics();
  62             g2d.setColor(Color.white);
  63             g2d.fillRect(0, 0, width, height);
  64 
  65             g2d.setPaint(new GradientPaint(
  66                              new Point2D.Float(0, height / 2), Color.white,
  67                              new Point2D.Float(width, height / 2), Color.black));
  68             g2d.fillRect(0, 0, width, height);
  69 
  70             String fnt = g2d.getFont().getFamily();
  71             g2d.setFont(new Font(fnt, Font.PLAIN, 100));
  72             g2d.drawString("IIIIIIIIII", 100, 100); // draw text with offset
  73 
  74             g2d.dispose();
  75 
  76             if (vi.validate(gc) != VolatileImage.IMAGE_OK) {
  77                 try {
  78                     Thread.sleep(100);
  79                 } catch (InterruptedException e) {}
  80                 continue;
  81             }
  82 
  83             if (vi.contentsLost()) {
  84                 try {
  85                     Thread.sleep(100);
  86                 } catch (InterruptedException e) {}
  87                 continue;
  88             }
  89 
  90             break;
  91         }
  92 
  93         BufferedImage bi = vi.getSnapshot();
  94 
  95         // the text shifted shouldn't be visible onto a painted rectangle!
  96         // so the check: color component (blue) must decrease monotonously
  97 
  98         int prev = Integer.MAX_VALUE;
  99         for (int x = 0; x < width; ++x) {
 100             int color = bi.getRGB(x, height / 2);
 101             int b = color & 0xFF;
 102 
 103             if (b > prev) {
 104                 throw new RuntimeException("test failed: can see the text rendered!");
 105             }
 106 
 107             prev = b;
 108         }
 109     }
 110 }
 111