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.Font;
  26 import java.awt.Graphics2D;
  27 import java.awt.image.BufferedImage;
  28 import java.io.File;
  29 import java.io.IOException;
  30 
  31 import javax.imageio.ImageIO;
  32 
  33 /**
  34  * @test
  35  * @bug 8013569
  36  * @author Sergey Bylokhov
  37  */
  38 public final class IncorrectTextSize {
  39 
  40     static final int scale = 2;
  41     static final int width = 1200;
  42     static final int height = 100;
  43     static BufferedImage bi = new BufferedImage(width, height,
  44                                                 BufferedImage.TYPE_INT_ARGB);
  45     static final String TEXT = "The quick brown fox jumps over the lazy dog"
  46             + "The quick brown fox jumps over the lazy dog";
  47 
  48     public static void main(final String[] args) throws IOException {
  49         for (int  point = 5; point < 11; ++point) {
  50             Graphics2D g2d = bi.createGraphics();
  51             g2d.setFont(new Font(Font.DIALOG, Font.PLAIN, point));
  52             g2d.scale(scale, scale);
  53             g2d.setColor(Color.WHITE);
  54             g2d.fillRect(0, 0, width, height);
  55             g2d.setColor(Color.green);
  56             g2d.drawString(TEXT, 0, 20);
  57             int length = g2d.getFontMetrics().stringWidth(TEXT);
  58             if (length < 0) {
  59                 throw new RuntimeException("Negative length");
  60             }
  61             for (int i = (length + 1) * scale; i < width; ++i) {
  62                 for (int j = 0; j < height; ++j) {
  63                     if (bi.getRGB(i, j) != Color.white.getRGB()) {
  64                         g2d.drawLine(length, 0, length, height);
  65                         ImageIO.write(bi, "png", new File("image.png"));
  66                         System.out.println("length = " + length);
  67                         System.err.println("Wrong color at x=" + i + ",y=" + j);
  68                         System.err.println("Color is:" + new Color(bi.getRGB(i,
  69                                                                              j)));
  70                         throw new RuntimeException("Test failed.");
  71                     }
  72                 }
  73             }
  74             g2d.dispose();
  75         }
  76     }
  77 }