8015085: [macosx] Label shortening via " ... " broken when String contains combining diaeresis
authorserb
Mon, 02 Feb 2015 18:21:24 +0300
changeset 29004 96b5dc94d927
parent 29003 7505cfb1cadf
child 29005 7d7a78defc8f
8015085: [macosx] Label shortening via " ... " broken when String contains combining diaeresis Reviewed-by: alexsch, azvegint
jdk/src/java.desktop/share/classes/sun/swing/SwingUtilities2.java
jdk/test/javax/swing/SwingUtilities/TestBadBreak/TestBadBreak.java
--- a/jdk/src/java.desktop/share/classes/sun/swing/SwingUtilities2.java	Fri Jan 30 13:27:33 2015 +0300
+++ b/jdk/src/java.desktop/share/classes/sun/swing/SwingUtilities2.java	Mon Feb 02 18:21:24 2015 +0300
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2002, 2014, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2002, 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
@@ -31,6 +31,7 @@
 import java.awt.event.*;
 import java.awt.font.*;
 import java.awt.print.PrinterGraphics;
+import java.text.BreakIterator;
 import java.text.CharacterIterator;
 import java.text.AttributedCharacterIterator;
 import java.text.AttributedString;
@@ -461,16 +462,15 @@
             }
         }
         if (needsTextLayout) {
-            FontRenderContext frc = getFontRenderContext(c, fm);
             AttributedString aString = new AttributedString(string);
             if (c != null) {
                 aString.addAttribute(TextAttribute.NUMERIC_SHAPING,
                         c.getClientProperty(TextAttribute.NUMERIC_SHAPING));
             }
-            LineBreakMeasurer measurer =
-                new LineBreakMeasurer(aString.getIterator(), frc);
-            int nChars = measurer.nextOffset(availTextWidth);
-            string = string.substring(0, nChars);
+            LineBreakMeasurer measurer = new LineBreakMeasurer(
+                    aString.getIterator(), BreakIterator.getCharacterInstance(),
+                    getFontRenderContext(c, fm));
+            string = string.substring(0, measurer.nextOffset(availTextWidth));
 
         }
         return string + clipString;
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/jdk/test/javax/swing/SwingUtilities/TestBadBreak/TestBadBreak.java	Mon Feb 02 18:21:24 2015 +0300
@@ -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.
+ */
+
+import java.awt.Graphics;
+import java.awt.Graphics2D;
+import java.awt.Robot;
+import java.awt.image.BufferedImage;
+import java.io.File;
+
+import javax.imageio.ImageIO;
+import javax.swing.JFrame;
+import javax.swing.JLabel;
+import javax.swing.SwingUtilities;
+
+import static java.awt.image.BufferedImage.TYPE_INT_ARGB;
+
+/**
+ * @test
+ * @bug 8015085
+ * @summary Shortening via " ... " is broken for Strings containing a combining
+ *          diaeresis.
+ * @author Sergey Bylokhov
+ */
+public class TestBadBreak {
+
+    static JFrame frame;
+    static Robot robot;
+    static final String withCombiningDiaeresis =    "123p://.aaaaaaaaaaaaaaaaaaaaaa.123/a\u0308" ;
+    static final String withoutCombiningDiaeresis = "123p://.aaaaaaaaaaaaaaaaaaaaaa.123/\u00E4" ;
+
+    public static void main(final String[] args) throws Exception {
+        robot = new Robot();
+        final BufferedImage bi1 = new BufferedImage(200, 90, TYPE_INT_ARGB);
+        final BufferedImage bi2 = new BufferedImage(200, 90, TYPE_INT_ARGB);
+        test(withCombiningDiaeresis, bi1);
+        test(withoutCombiningDiaeresis, bi2);
+        for (int x = 0; x < bi1.getWidth(); ++x) {
+            for (int y = 0; y < bi1.getHeight(); ++y) {
+                if (bi1.getRGB(x, y) != bi2.getRGB(x, y)) {
+                    ImageIO.write(bi1, "png", new File("image1.png"));
+                    ImageIO.write(bi2, "png", new File("image2.png"));
+                    throw new RuntimeException("Wrong color");
+                }
+            }
+        }
+    }
+
+    private static void test(final String text, final BufferedImage i1)
+            throws Exception {
+        SwingUtilities.invokeAndWait(new Runnable() {
+            @Override
+            public void run() {
+                frame = new JFrame();
+                final JLabel label = new JLabel(text) {
+                    @Override
+                    protected void paintComponent(Graphics g) {
+                        Graphics2D g2d = i1.createGraphics();
+                        super.paintComponent(g2d);
+                        g2d.dispose();
+                    }
+                };
+                frame.getContentPane().add(label);
+                frame.setBounds(200, 200, 200, 90);
+            }
+        });
+        robot.waitForIdle();
+        SwingUtilities.invokeAndWait(() -> frame.setVisible(true));
+        robot.waitForIdle();
+        SwingUtilities.invokeAndWait(frame::dispose);
+        robot.waitForIdle();
+    }
+}