Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-8185261

Font fallback sometimes doesn't work in Swing text components

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Unresolved
    • Icon: P4 P4
    • tbd
    • 6, 7, 8, 9
    • client-libs
    • 2d
    • windows_10

      Reproducer:

      {code}
      import javax.swing.*;
      import java.awt.*;

      public class Test {
          public static void main(String[] args) {
              SwingUtilities.invokeLater(() -> {
                  JFrame frame = new JFrame();
                  JEditorPane editorPane = new JEditorPane("text/html",
                          "<html><head><style>body {font-family:'Segoe UI'; font-size:12pt;}</style></head><body>\u4e2d</body></html>");
                  // if the following line is commented out, font fallback will work as expected
                  editorPane.getFontMetrics(new Font("Segoe UI", Font.PLAIN, 12));
                  frame.add(editorPane);
                  frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
                  frame.setSize(200, 100);
                  frame.setLocationRelativeTo(null);
                  frame.setVisible(true);
              });
          }
      }
      {code}

      Expected result: Chinese hieroglyph is displayed in the text area.
      Actual result: 'missing glyph' box is displayed in the text area.

       If the line, containing 'getFontMetrics' invocation is commented out or removed, sample program produces expected result.

      Issue is caused by equals/hashCode methods in java.awt.Font not distinguishing 'physical' font instances and font-fallback-enabled ones (generated by sun.font.FontUtilities.getCompositeFontUIResource), so font metrics caches in sun.font.FontDesignMetrics and javax.swing.text.GlyphPainter1 get confused.

            prr Philip Race
            dbatrak Dmitry Batrak
            Votes:
            0 Vote for this issue
            Watchers:
            3 Start watching this issue

              Created:
              Updated: