6925760: Scaled graphics can cause overlapped LCD mode strings on Windows for pixel size > 48
authorprr
Fri, 14 Jan 2011 12:10:45 -0800
changeset 7938 970d0e025e57
parent 7937 3de0a7f48b19
child 7939 4b122be94252
6925760: Scaled graphics can cause overlapped LCD mode strings on Windows for pixel size > 48 Reviewed-by: igor, jgodinez
jdk/src/share/classes/sun/font/FileFontStrike.java
jdk/test/java/awt/FontClass/LCDScale.java
--- a/jdk/src/share/classes/sun/font/FileFontStrike.java	Fri Jan 14 11:43:36 2011 -0800
+++ b/jdk/src/share/classes/sun/font/FileFontStrike.java	Fri Jan 14 12:10:45 2011 -0800
@@ -566,10 +566,44 @@
         if (glyphCode >= INVISIBLE_GLYPHS) {
             return 0f;
         }
+
+        /* Notes on the (getUserAdv == false) case.
+         *
+         * Setting getUserAdv == false is internal to this class.
+         * If there's no graphics transform we can let
+         * getGlyphAdvance take its course, and potentially caching in
+         * advances arrays, except for signalling that
+         * getUserAdv == false means there is no need to create an image.
+         * It is possible that code already calculated the user advance,
+         * and it is desirable to take advantage of that work.
+         * But, if there's a transform and we want device advance, we
+         * can't use any values cached in the advances arrays - unless
+         * first re-transform them into device space using 'desc.devTx'.
+         * invertDevTx is null if the graphics transform is identity,
+         * a translate, or non-invertible. The latter case should
+         * not ever occur in the getUserAdv == false path.
+         * In other words its either null, or the inversion of a
+         * simple uniform scale. If its null, we can populate and
+         * use the advance caches as normal.
+         *
+         * If we don't find a cached value, obtain the device advance and
+         * return it. This will get stashed on the image by the caller and any
+         * subsequent metrics calls will be able to use it as is the case
+         * whenever an image is what is initially requested.
+         *
+         * Don't query if there's a value cached on the image, since this
+         * getUserAdv==false code path is entered solely when none exists.
+         */
         if (horizontalAdvances != null) {
             advance = horizontalAdvances[glyphCode];
             if (advance != Float.MAX_VALUE) {
-                return advance;
+                if (!getUserAdv && invertDevTx != null) {
+                    Point2D.Float metrics = new Point2D.Float(advance, 0f);
+                    desc.devTx.deltaTransform(metrics, metrics);
+                    return metrics.x;
+                } else {
+                    return advance;
+                }
             }
         } else if (segmentedCache && segHorizontalAdvances != null) {
             int segIndex = glyphCode >> SEGSHIFT;
@@ -577,11 +611,23 @@
             if (subArray != null) {
                 advance = subArray[glyphCode % SEGSIZE];
                 if (advance != Float.MAX_VALUE) {
-                    return advance;
+                    if (!getUserAdv && invertDevTx != null) {
+                        Point2D.Float metrics = new Point2D.Float(advance, 0f);
+                        desc.devTx.deltaTransform(metrics, metrics);
+                        return metrics.x;
+                    } else {
+                        return advance;
+                    }
                 }
             }
         }
 
+        if (!getUserAdv && invertDevTx != null) {
+            Point2D.Float metrics = new Point2D.Float();
+            fileFont.getGlyphMetrics(pScalerContext, glyphCode, metrics);
+            return metrics.x;
+        }
+
         if (invertDevTx != null || !getUserAdv) {
             /* If there is a device transform need x & y advance to
              * transform back into user space.
@@ -725,7 +771,7 @@
         return getGlyphMetrics(glyphCode, true);
     }
 
-    private Point2D.Float getGlyphMetrics(int glyphCode, boolean getUserAdv) {
+    private Point2D.Float getGlyphMetrics(int glyphCode, boolean getImage) {
         Point2D.Float metrics = new Point2D.Float();
 
         // !!! or do we force sgv user glyphs?
@@ -733,7 +779,7 @@
             return metrics;
         }
         long glyphPtr;
-        if (getImageWithAdvance && getUserAdv) {
+        if (getImageWithAdvance && getImage) {
             /* A heuristic optimisation says that for most cases its
              * worthwhile retrieving the image at the same time as the
              * metrics. So here we get the image data even if its not
@@ -750,9 +796,9 @@
             metrics.y = StrikeCache.unsafe.getFloat
                 (glyphPtr + StrikeCache.yAdvanceOffset);
             /* advance is currently in device space, need to convert back
-             * into user space, unless getUserAdv == false.
+             * into user space.
              * This must not include the translation component. */
-            if (invertDevTx != null && getUserAdv) {
+            if (invertDevTx != null) {
                 invertDevTx.deltaTransform(metrics, metrics);
             }
         } else {
@@ -781,9 +827,9 @@
             if (value == null) {
                 fileFont.getGlyphMetrics(pScalerContext, glyphCode, metrics);
                 /* advance is currently in device space, need to convert back
-                 * into user space, unless getUserAdv == false.
+                 * into user space.
                  */
-                if (invertDevTx != null && getUserAdv) {
+                if (invertDevTx != null) {
                     invertDevTx.deltaTransform(metrics, metrics);
                 }
                 value = new Point2D.Float(metrics.x, metrics.y);
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/jdk/test/java/awt/FontClass/LCDScale.java	Fri Jan 14 12:10:45 2011 -0800
@@ -0,0 +1,83 @@
+/*
+ * Copyright (c) 2010, 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.
+ */
+
+/*
+ *
+ * @bug 6925760
+ * @summary Scaled graphics causes overlapped LCD glyphs on Windows
+ */
+
+import java.awt.*;
+import java.awt.font.*;
+import java.awt.geom.*;
+
+public class LCDScale extends Component {
+
+    public static void main(String args[]) {
+        Frame f = new Frame("TL TEST");
+        LCDScale td = new LCDScale();
+        f.add("Center", td);
+        f.pack(); f.setVisible(true);
+    }
+
+
+    public LCDScale() {
+        super();
+    }
+
+    public Dimension getPreferredSize() {
+        return new Dimension(500,500);
+    }
+
+    public void paint(Graphics g) {
+        Graphics2D g2d = (Graphics2D)g;
+        g2d.setRenderingHint(
+                 RenderingHints.KEY_TEXT_ANTIALIASING,
+                 RenderingHints.VALUE_TEXT_ANTIALIAS_LCD_HRGB);
+
+        Font f = new Font("Dialog", Font.PLAIN, 40);
+        g.setFont(f);
+        FontRenderContext frc = g2d.getFontRenderContext();
+        GlyphVector gv = f.createGlyphVector(frc, "Help");
+        g2d.drawGlyphVector(gv, 10f, 50f);
+        Rectangle2D bds1 = gv.getLogicalBounds();
+
+        f = new Font("Arial", Font.PLAIN, 25);
+        g.setFont(f);
+        double s = 2.0;
+        AffineTransform tx = AffineTransform.getScaleInstance(s,s);
+        g2d.transform(tx);
+        frc = g2d.getFontRenderContext();
+        gv = f.createGlyphVector(frc, "Help");
+        g2d.drawGlyphVector(gv, 10f, 100f);
+        Rectangle2D bds2 = gv.getLogicalBounds();
+
+        System.out.println(bds1);
+        System.out.println(bds2);
+        if (bds2.getWidth()*s < bds1.getWidth()) {
+            throw new RuntimeException("Bounds too small");
+        }
+    }
+}
+
+