1 /*
   2  * Copyright (c) 2015, 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 6357987 6513889 8023213
  26  * @summary Font/Text APIs should not crash if transform includes NaN
  27  * @author prr
  28  * @run main NaNTransform
  29  */
  30 import java.awt.*;
  31 import java.awt.font.*;
  32 import java.awt.geom.*;
  33 import java.awt.image.*;
  34 
  35 public class NaNTransform {
  36 
  37     private static void testShape(String msg, Shape s) {
  38         if (!(new Area(s).isEmpty())) {
  39             System.out.println(msg+"="+s);
  40             throw new RuntimeException("Warning: expected this to be empty");
  41         }
  42     }
  43 
  44     public static void main(String [] args) {
  45 
  46         float NaN=0f/0f;
  47         float[] vals = new float[6];
  48         for (int i=0;i<6;i++) vals[i]=NaN;
  49         AffineTransform nanTX = new AffineTransform(vals);
  50 
  51         BufferedImage bi = new BufferedImage(1,1,BufferedImage.TYPE_INT_RGB);
  52         Graphics2D g2d = bi.createGraphics();
  53 
  54         g2d.rotate(NaN);
  55 
  56         Font font = g2d.getFont();
  57         FontMetrics fm = g2d.getFontMetrics();
  58         FontRenderContext frc = g2d.getFontRenderContext();
  59 
  60         int adv = fm.stringWidth("ABCDEF");
  61         if (adv != 0) {
  62             System.out.println("strWidth="+adv);
  63             throw new RuntimeException("Warning: expected this to be zero");
  64         }
  65         testShape("strBounds", font.getStringBounds("12345", frc));
  66 
  67         TextLayout tl = new TextLayout("Some text", font, frc);
  68         testShape("tl PixelBounds 1", tl.getPixelBounds(frc, 20, 10));
  69         testShape("tl PixelBounds 2", tl.getPixelBounds(frc, NaN, NaN));
  70         testShape("tl Outline", tl.getOutline(nanTX));
  71 
  72         GlyphVector gv = font.createGlyphVector(frc, "abcdef");
  73         testShape("gv PixelBounds 1", gv.getPixelBounds(frc, 0, 0));
  74         testShape("gv PixelBounds 2", gv.getPixelBounds(frc, NaN, NaN));
  75         testShape("gv Outline", gv.getOutline(NaN, NaN));
  76 
  77         gv.setGlyphTransform(0, nanTX);
  78         testShape("gv PixelBounds 1A", gv.getPixelBounds(frc, 0, 0));
  79         testShape("gv PixelBounds 2A", gv.getPixelBounds(frc, NaN, NaN));
  80         testShape("gv Outline A", gv.getOutline(NaN, NaN));
  81 
  82         g2d.drawString("BOO!", 20, 20);
  83 
  84         Font nanFont;
  85         for (int i=0; i<5000; i++) {
  86             nanFont = font.deriveFont(Float.NaN);
  87             g2d.setFont(nanFont);
  88             g2d.drawString("abc", 20, 20);
  89         }
  90         System.out.println("Test passed (no crash)");
  91     }
  92 }