[OpenJDK 2D-Dev] Bug with stringWidth on Retina

Konstantin Bulenkov kb at jetbrains.com
Fri Sep 13 01:14:38 UTC 2013


Hi everyone,

I've got a problem with the latest release of JDK7 (1.7.0_40-b43). Sometimes, method FontMetrics.stringWidth(String) returns wrong values on Retina devices. I wrote a small test demonstrating the problem:

import javax.swing.*;
import java.awt.*;
 
public class TextWidthTest extends JFrame {
  public TextWidthTest() throws HeadlessException {
    super("Text Width Test in JDK 1.7.0_40");
    setDefaultCloseOperation(EXIT_ON_CLOSE);
    setSize(280, 40);
    final String str = "nnnnnnnnnnnnnnnnnnnn.txt";
    JComponent customComp = new JComponent() {
      @Override
      protected void paintComponent(Graphics g) {
        super.paintComponent(g);
        FontMetrics fm = getFontMetrics(getFont());
        g.setColor(Color.orange);
        g.fillRect(0, 0, fm.stringWidth(str), fm.getHeight());
        g.setColor(Color.blue);
        g.drawString(str, 0, fm.getHeight());
      }
    };
    Font font = new Font("Lucida Grande", Font.PLAIN, 12);
    customComp.setFont(font);
    getContentPane().add(customComp);
    setVisible(true);
  }
 
  public static void main(String[] args) {
    new TextWidthTest();
  }
}

I've attached some pictures here http://bulenkov.com/2013/09/13/bug-with-stringwidth-in-jdk-1-7-0_40/ 

Does anybody know any workaround or a fix for the problem?

Thanks

--
Konstantin Bulenkov
IntelliJ Platform Front-end Lead
JetBrains
http://twitter.com/bulenkov




More information about the 2d-dev mailing list