8023213: [macosx] closed/java/awt/FontClass/NaNTransform.java fails on MacOS X 10.9
authorpsadhukhan
Thu, 14 Jan 2016 13:24:22 +0530
changeset 35671 20db05a43cb7
parent 35670 320e424b168c
child 35672 c97d62614abc
8023213: [macosx] closed/java/awt/FontClass/NaNTransform.java fails on MacOS X 10.9 Reviewed-by: prr, serb
jdk/src/java.desktop/macosx/native/libawt_lwawt/font/CGGlyphImages.m
jdk/src/java.desktop/macosx/native/libawt_lwawt/font/CGGlyphOutlines.m
jdk/test/java/awt/FontClass/DrawStringWithInfiniteXform.java
jdk/test/java/awt/FontClass/NaNTransform.java
--- a/jdk/src/java.desktop/macosx/native/libawt_lwawt/font/CGGlyphImages.m	Wed Jan 13 11:56:46 2016 +0530
+++ b/jdk/src/java.desktop/macosx/native/libawt_lwawt/font/CGGlyphImages.m	Thu Jan 14 13:24:22 2016 +0530
@@ -562,6 +562,17 @@
     (CGGI_GlyphCanvas *canvas, const CGGlyph glyph,
      GlyphInfo *info, const CGGI_RenderingMode *mode)
 {
+    if (isnan(info->topLeftX) || isnan(info->topLeftY)) {
+        // Explicitly set glyphInfo width/height to be 0 to ensure
+        // zero length glyph image is copied into GlyphInfo from canvas
+        info->width = 0;
+        info->height = 0;
+
+        // copy the "empty" glyph from the canvas into the info
+        (*mode->glyphDescriptor->copyFxnPtr)(canvas, info);
+        return;
+    }
+
     // clean the canvas
     CGGI_ClearCanvas(canvas, info);
 
@@ -570,7 +581,6 @@
                                -info->topLeftX,
                                canvas->image->height + info->topLeftY,
                                &glyph, 1);
-
     // copy the glyph from the canvas into the info
     (*mode->glyphDescriptor->copyFxnPtr)(canvas, info);
 }
--- a/jdk/src/java.desktop/macosx/native/libawt_lwawt/font/CGGlyphOutlines.m	Wed Jan 13 11:56:46 2016 +0530
+++ b/jdk/src/java.desktop/macosx/native/libawt_lwawt/font/CGGlyphOutlines.m	Thu Jan 14 13:24:22 2016 +0530
@@ -259,6 +259,10 @@
 
     OSStatus status = noErr;
 
+    if ( isnan(tx->a) || isnan(tx->b) || isnan(tx->c) ||
+         isnan(tx->d) || isnan(tx->tx) || isnan(tx->ty)) {
+        return status;
+    }
     glyphs = glyphs + inStartIndex;
 //    advanceArray = advanceArray + inStartIndex; // TODO(cpc): use advance
 
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/jdk/test/java/awt/FontClass/DrawStringWithInfiniteXform.java	Thu Jan 14 13:24:22 2016 +0530
@@ -0,0 +1,78 @@
+/*
+ * Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
+ *
+ * This code is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License version 2 only, as
+ * published by the Free Software Foundation.
+ *
+ * This code is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
+ * version 2 for more details (a copy is included in the LICENSE file that
+ * accompanied this code).
+ *
+ * You should have received a copy of the GNU General Public License version
+ * 2 along with this work; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
+ * or visit www.oracle.com if you need additional information or have any
+ * questions.
+ */
+/*
+ * @test
+ * @bug 8023213
+ * @summary Font/Text APIs should not crash/takes long time
+ *          if transform includes INIFINITY
+ * @run main DrawStringWithInfiniteXform
+ */
+import java.awt.*;
+import java.awt.font.*;
+import java.awt.geom.*;
+import java.awt.image.*;
+import java.util.Timer;
+import java.util.TimerTask;
+
+public class DrawStringWithInfiniteXform {
+    Timer timer;
+    boolean done;
+    class ScheduleTask extends TimerTask {
+        public void run() {
+            timer.cancel();
+            if (!done) {
+                throw new
+                RuntimeException("drawString with InfiniteXform transform takes long time");
+            }
+        }
+    }
+    public DrawStringWithInfiniteXform() {
+        timer = new Timer();
+        timer.schedule(new ScheduleTask(), 10000);
+    }
+
+    public static void main(String [] args) {
+        DrawStringWithInfiniteXform test = new DrawStringWithInfiniteXform();
+        test.start();
+    }
+
+    private void start() {
+        float[] vals = new float[6];
+        for (int i=0;i<6;i++) vals[i]=Float.POSITIVE_INFINITY;
+        AffineTransform nanTX = new AffineTransform(vals);
+
+        BufferedImage bi = new BufferedImage(1,1,BufferedImage.TYPE_INT_RGB);
+        Graphics2D g2d = bi.createGraphics();
+
+        g2d.rotate(Float.POSITIVE_INFINITY);
+        Font font = g2d.getFont();
+        Font xfiniteFont;
+        for (int i=0; i<2000; i++) {
+            xfiniteFont = font.deriveFont(Float.POSITIVE_INFINITY);
+            g2d.setFont(xfiniteFont);
+            g2d.drawString("abc", 20, 20);
+        }
+        done = true;
+        System.out.println("Test passed");
+    }
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/jdk/test/java/awt/FontClass/NaNTransform.java	Thu Jan 14 13:24:22 2016 +0530
@@ -0,0 +1,92 @@
+/*
+ * Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
+ *
+ * This code is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License version 2 only, as
+ * published by the Free Software Foundation.
+ *
+ * This code is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
+ * version 2 for more details (a copy is included in the LICENSE file that
+ * accompanied this code).
+ *
+ * You should have received a copy of the GNU General Public License version
+ * 2 along with this work; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
+ * or visit www.oracle.com if you need additional information or have any
+ * questions.
+ */
+/**
+ * @test
+ * @bug 6357987 6513889 8023213
+ * @summary Font/Text APIs should not crash if transform includes NaN
+ * @author prr
+ * @run main NaNTransform
+ */
+import java.awt.*;
+import java.awt.font.*;
+import java.awt.geom.*;
+import java.awt.image.*;
+
+public class NaNTransform {
+
+    private static void testShape(String msg, Shape s) {
+        if (!(new Area(s).isEmpty())) {
+            System.out.println(msg+"="+s);
+            throw new RuntimeException("Warning: expected this to be empty");
+        }
+    }
+
+    public static void main(String [] args) {
+
+        float NaN=0f/0f;
+        float[] vals = new float[6];
+        for (int i=0;i<6;i++) vals[i]=NaN;
+        AffineTransform nanTX = new AffineTransform(vals);
+
+        BufferedImage bi = new BufferedImage(1,1,BufferedImage.TYPE_INT_RGB);
+        Graphics2D g2d = bi.createGraphics();
+
+        g2d.rotate(NaN);
+
+        Font font = g2d.getFont();
+        FontMetrics fm = g2d.getFontMetrics();
+        FontRenderContext frc = g2d.getFontRenderContext();
+
+        int adv = fm.stringWidth("ABCDEF");
+        if (adv != 0) {
+            System.out.println("strWidth="+adv);
+            throw new RuntimeException("Warning: expected this to be zero");
+        }
+        testShape("strBounds", font.getStringBounds("12345", frc));
+
+        TextLayout tl = new TextLayout("Some text", font, frc);
+        testShape("tl PixelBounds 1", tl.getPixelBounds(frc, 20, 10));
+        testShape("tl PixelBounds 2", tl.getPixelBounds(frc, NaN, NaN));
+        testShape("tl Outline", tl.getOutline(nanTX));
+
+        GlyphVector gv = font.createGlyphVector(frc, "abcdef");
+        testShape("gv PixelBounds 1", gv.getPixelBounds(frc, 0, 0));
+        testShape("gv PixelBounds 2", gv.getPixelBounds(frc, NaN, NaN));
+        testShape("gv Outline", gv.getOutline(NaN, NaN));
+
+        gv.setGlyphTransform(0, nanTX);
+        testShape("gv PixelBounds 1A", gv.getPixelBounds(frc, 0, 0));
+        testShape("gv PixelBounds 2A", gv.getPixelBounds(frc, NaN, NaN));
+        testShape("gv Outline A", gv.getOutline(NaN, NaN));
+
+        g2d.drawString("BOO!", 20, 20);
+
+        Font nanFont;
+        for (int i=0; i<5000; i++) {
+            nanFont = font.deriveFont(Float.NaN);
+            g2d.setFont(nanFont);
+            g2d.drawString("abc", 20, 20);
+        }
+        System.out.println("Test passed (no crash)");
+    }
+}