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