7160052: GlyphVector.setGlyphPosition can throw an exception on valid input
authorpsadhukhan
Tue, 08 Dec 2015 11:25:47 +0300
changeset 34802 f8f0def6f107
parent 34801 a7740dae1f3a
child 34803 02d97673db58
7160052: GlyphVector.setGlyphPosition can throw an exception on valid input Reviewed-by: jdv, serb
jdk/src/java.desktop/share/classes/sun/font/StandardGlyphVector.java
jdk/test/java/awt/font/GlyphVector/TestStandardGlyphVectorBug.java
--- a/jdk/src/java.desktop/share/classes/sun/font/StandardGlyphVector.java	Sat Dec 05 09:48:43 2015 -0800
+++ b/jdk/src/java.desktop/share/classes/sun/font/StandardGlyphVector.java	Tue Dec 08 11:25:47 2015 +0300
@@ -445,13 +445,19 @@
     }
 
     public void setGlyphPosition(int ix, Point2D pos) {
+        if (ix < 0 || ix > glyphs.length) {
+            throw new IndexOutOfBoundsException("ix = " + ix);
+        }
+
         initPositions();
 
         int ix2 = ix << 1;
         positions[ix2] = (float)pos.getX();
         positions[ix2 + 1] = (float)pos.getY();
 
-        clearCaches(ix);
+        if (ix < glyphs.length) {
+            clearCaches(ix);
+        }
         addFlags(FLAG_HAS_POSITION_ADJUSTMENTS);
     }
 
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/jdk/test/java/awt/font/GlyphVector/TestStandardGlyphVectorBug.java	Tue Dec 08 11:25:47 2015 +0300
@@ -0,0 +1,58 @@
+/*
+ * 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.
+ */
+import java.awt.Font;
+import java.awt.font.FontRenderContext;
+import java.awt.font.GlyphVector;
+import java.awt.geom.AffineTransform;
+import java.awt.geom.Point2D;
+
+/**
+ * @test
+ * @bug 7160052
+ * @run main TestStandardGlyphVectorBug
+ * @summary GlyphVector.setGlyphPosition should not throw an exception on valid input
+ */
+public class TestStandardGlyphVectorBug
+{
+    public static void main(String[] args)
+    {
+        Font defaultFont = new Font(null);
+        FontRenderContext defaultFrc = new FontRenderContext(new AffineTransform(),
+                                                             true, true);
+        GlyphVector gv = defaultFont.createGlyphVector(defaultFrc, "test");
+
+        //this causes the bounds to be cached
+        //which is necessary to trigger the bug
+        gv.getGlyphLogicalBounds(0);
+
+        //this correctly gets the position of the overall advance
+        Point2D glyphPosition = gv.getGlyphPosition(gv.getNumGlyphs());
+
+        // this sets the position of the overall advance,
+        // but also incorrectly tries to clear the bounds cache
+        // of a specific glyph indexed by the glyphIndex parameter
+        // even if the glyphIndex represents the overall advance
+        // (i.e. if glyphIndex == getNumGlyphs())
+        gv.setGlyphPosition(gv.getNumGlyphs(), glyphPosition);
+    }
+}