JDK-6688672 : Italic text is truncated in the Swing components
  • Type: Bug
  • Component: client-libs
  • Sub-Component: javax.swing
  • Affected Version: 6u10
  • Priority: P4
  • Status: Closed
  • Resolution: Cannot Reproduce
  • OS: windows_vista
  • CPU: x86
  • Submitted: 2008-04-15
  • Updated: 2022-12-12
  • Resolved: 2022-12-12
Related Reports
Relates :  
Description
FULL PRODUCT VERSION :
java version "1.6.0_10-beta"
Java(TM) SE Runtime Environment (build 1.6.0_10-beta-b14)
Java HotSpot(TM) Client VM (build 11.0-b11, mixed mode, sharing)

ADDITIONAL OS VERSION INFORMATION :
Microsoft Vista (Microsoft Windows [Version 6.0.6000])

A DESCRIPTION OF THE PROBLEM :
Starting with 6u10 build 13 the font rendering is broken.
The symptons are:
- large italic fonts are truncated at the end
- unicode characters are not rendered properly

In 6u10 build 12 this was working fine.

Especially for programs relying on a proper unicode rendering this is a showstopper.

STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
Run the attached source and compare the rendering on 6u10b12 and 6u10b13-21

EXPECTED VERSUS ACTUAL BEHAVIOR :
EXPECTED -
text being rendered properly
ACTUAL -
See description

REPRODUCIBILITY :
This bug can be reproduced always.

---------- BEGIN SOURCE ----------
import java.awt.EventQueue;
import javax.swing.*;

public class TestFontRendering extends JPanel {

    public TestFontRendering() {
        setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));
        setBorder(BorderFactory.createEmptyBorder(10,10,10,10));
       
        final JLabel labelTitle = new JLabel("<html><font size='+3'><i>Welcome Welcome Welcome</i>");
        final JLabel label1 = new JLabel("\u2780 6u10 b12 works fine");
        final JLabel label2 = new JLabel("\u2781 6u10 b13ff has the following issues");
        final JLabel label3 = new JLabel("\u2782  - headline is truncated at the end");
        final JLabel label4 = new JLabel("\u2783  - unicode bullets are not rendered correctly");
        
        add(labelTitle);
        add(label1);
        add(label2);
        add(label3);
        add(label4);
    }
    
    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (Exception ignore) {
                }
                JFrame frame = new JFrame("Font issues in 6u10 b13ff");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.setContentPane(new TestFontRendering());
                frame.pack();
                frame.setVisible(true);
            }
        });
    }
}

---------- END SOURCE ----------

Comments
EVALUATION The problem is in the fact that Font Metrics, which is used for string width calculation, are not specific to a particular piece of text. There is a related citation from the documentation (http://java.sun.com/products/java-media/2D/reference/faqs/index.html#Q_How_do_I_obtain_font_metrics): "Font wide metrics are not specific to a particular piece of text. It is based on data in the font specified by the font designer to reflect the overall design of the font. Do not expect that, for example, the reported "ascent" of the font will be as high as the highest ascent of any glyph in the font." So, the problem is in sun.font.FontDesignMetrics.stringWidth(). It sometimes returns too short width for a string. There is a test case: ======== Source Begin ========== import javax.swing.*; import java.awt.*; import java.awt.image.BufferedImage; public class Test2D extends JPanel { private BufferedImage bi; public static void main(String[] args) { EventQueue.invokeLater(new Runnable() { public void run() { JFrame frame = new JFrame("JLabel Test"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setContentPane(new Test2D()); frame.setSize(1100, 400); frame.setLocationRelativeTo(null); frame.setVisible(true); } }); } public Test2D() { // Font font = new Font("Dialog", 2, 96); // normal width Font font = new Font("Tahoma", 2, 96); // short width String text = "Welcome Welcome"; // Prepare image bi = new BufferedImage(1000, 300, BufferedImage.TYPE_INT_RGB); Graphics g = bi.getGraphics(); g.setColor(Color.WHITE); g.fillRect(0, 0, 1000, 300); // Draw text g.setColor(Color.BLACK); g.setFont(font); g.drawString(text, 0, 100); // Draw text bounds FontMetrics fm = g.getFontMetrics(font); int width = fm.stringWidth(text); g.setColor(Color.GREEN); g.drawPolyline(new int[]{0, width, width, 0, 0}, new int[]{10, 10, 100, 100, 10}, 5); } protected void paintComponent(Graphics g) { super.paintComponent(g); g.drawImage(bi, 0, 0, null); } } ======== Source End =========
08-07-2008

EVALUATION I found that the problem most likely is in the BasicLabelUI.getPreferredSize() method. It seems that the method returns shorter width than it is needed. I reproduced the problem on Windows XP under Metal L&F. It turned out that the problem depends on the font type. In Windows Classic L&F for labels by default font Tahoma is used. In Metal L&F the default font is Dialog. When I set Tahoma font for the label, the problem appeared in Metal L&F too. There is a new test case: ======== Source Begin ============ import java.awt.*; import javax.swing.*; public class Test extends JPanel { public static void main(String[] args) { EventQueue.invokeLater(new Runnable() { public void run() { JFrame frame = new JFrame("JLabel Test"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setContentPane(new Test()); frame.pack(); frame.setLocationRelativeTo(null); frame.setVisible(true); } }); } public Test() { setLayout(new BoxLayout(this, BoxLayout.Y_AXIS)); // Label width is normal JLabel label = new JLabel("Welcome Welcome"); label.setBorder(BorderFactory.createLineBorder(Color.GREEN)); Font font = new Font("Dialog", 2, 96); label.setFont(font); add(label); // Label width is too short JLabel label2 = new JLabel("Welcome Welcome"); label2.setBorder(BorderFactory.createLineBorder(Color.GREEN)); Font font2 = new Font("Tahoma", 2, 96); label2.setFont(font2); add(label2); } } ======== Source End =============
07-07-2008

EVALUATION I can reproduce the problem with the text truncation on every jdk which I tested: 7b22, 7b14, 6u10b11, 6u10b22, 5u11, 1.4.2_11. Text is truncated only on Windows Look and Feel. Metal and GTK L&Fs work well.
11-06-2008

EVALUATION Problem with unicode characters was recently fixed as part of 6679904. Truncation seems to be releated to usage of incorrect metrics. Problem is reproducible with older builds as well (it is not regression). Passing to Swing team for investigation.
15-04-2008