Path: blob/master/test/jdk/java/awt/FontClass/NaNTransform.java
41152 views
/*1* Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/22/**23* @test24* @bug 6357987 6513889 802321325* @summary Font/Text APIs should not crash if transform includes NaN26* @author prr27* @run main NaNTransform28*/29import java.awt.*;30import java.awt.font.*;31import java.awt.geom.*;32import java.awt.image.*;3334public class NaNTransform {3536private static void testShape(String msg, Shape s) {37if (!(new Area(s).isEmpty())) {38System.out.println(msg+"="+s);39throw new RuntimeException("Warning: expected this to be empty");40}41}4243public static void main(String [] args) {4445float NaN=0f/0f;46float[] vals = new float[6];47for (int i=0;i<6;i++) vals[i]=NaN;48AffineTransform nanTX = new AffineTransform(vals);4950BufferedImage bi = new BufferedImage(1,1,BufferedImage.TYPE_INT_RGB);51Graphics2D g2d = bi.createGraphics();5253g2d.rotate(NaN);5455Font font = g2d.getFont();56FontMetrics fm = g2d.getFontMetrics();57FontRenderContext frc = g2d.getFontRenderContext();5859int adv = fm.stringWidth("ABCDEF");60if (adv != 0) {61System.out.println("strWidth="+adv);62throw new RuntimeException("Warning: expected this to be zero");63}64testShape("strBounds", font.getStringBounds("12345", frc));6566TextLayout tl = new TextLayout("Some text", font, frc);67testShape("tl PixelBounds 1", tl.getPixelBounds(frc, 20, 10));68testShape("tl PixelBounds 2", tl.getPixelBounds(frc, NaN, NaN));69testShape("tl Outline", tl.getOutline(nanTX));7071GlyphVector gv = font.createGlyphVector(frc, "abcdef");72testShape("gv PixelBounds 1", gv.getPixelBounds(frc, 0, 0));73testShape("gv PixelBounds 2", gv.getPixelBounds(frc, NaN, NaN));74testShape("gv Outline", gv.getOutline(NaN, NaN));7576gv.setGlyphTransform(0, nanTX);77testShape("gv PixelBounds 1A", gv.getPixelBounds(frc, 0, 0));78testShape("gv PixelBounds 2A", gv.getPixelBounds(frc, NaN, NaN));79testShape("gv Outline A", gv.getOutline(NaN, NaN));8081g2d.drawString("BOO!", 20, 20);8283Font nanFont;84for (int i=0; i<5000; i++) {85nanFont = font.deriveFont(Float.NaN);86g2d.setFont(nanFont);87g2d.drawString("abc", 20, 20);88}89System.out.println("Test passed (no crash)");90}91}929394