JDK-8214429 : Preferred width of a Component is incorrect when using a custom Font
  • Type: Bug
  • Component: client-libs
  • Sub-Component: 2d
  • Affected Version: 11,12
  • Priority: P3
  • Status: Closed
  • Resolution: Duplicate
  • OS: windows_10
  • CPU: x86_64
  • Submitted: 2018-11-26
  • Updated: 2020-08-25
  • Resolved: 2020-08-25
The Version table provides details related to the release that this issue/RFE will be addressed.

Unresolved : Release in which this issue/RFE will be addressed.
Resolved: Release in which this issue/RFE has been resolved.
Fixed : Release in which this issue/RFE has been fixed. The release containing this fix may be available for download as an Early Access Release or a General Availability Release.

To download the current JDK release, click here.
Other
tbdResolved
Related Reports
Duplicate :  
Relates :  
Description
ADDITIONAL SYSTEM INFORMATION :
Java 11 on any system/OS.

A DESCRIPTION OF THE PROBLEM :
A regression related to FontMetrics was introduced in Java 11 when a custom Font is set on a Component.  This causes the Component to size itself incorrectly leading to cut off text.   This problem does not exist in Java 8.

REGRESSION : Last worked in version 8u181

STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
A test program is provided below.  Run on Java 11 and run on Java 8 to see the difference.   If you actually render the label, the text will be cut off in Java 11 as the preferred size of the component is computed incorrectly.


EXPECTED VERSUS ACTUAL BEHAVIOR :
EXPECTED -
Run the test program on Java 8, the preferred size is correctly shown as 44.

String bounds: java.awt.geom.Rectangle2D$Float[x=0.0,y=-9.004395,w=44.0,h=10.863281]
Preferred size: java.awt.Dimension[width=44,height=11]

ACTUAL -
Run the test program on Java 11, the preferred size is incorrectly shown as 40.

String bounds: java.awt.geom.Rectangle2D$Float[x=0.0,y=-9.004395,w=44.0,h=10.863281]
Preferred size: java.awt.Dimension[width=40,height=11]


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

public class Test {
  public static void main(final String args[]) {
        String str = "Used: N/A";
        JLabel label = new JLabel(str);
        label.setFont(new Font("Tahoma", Font.PLAIN, 9));

        System.err.println("String bounds: " + label.getFontMetrics(label.getFont()).getStringBounds(str, label.getGraphics()));
        System.err.println("Preferred size: " + label.getPreferredSize());
    }
}

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

CUSTOMER SUBMITTED WORKAROUND :
You could override getPreferredSize() on all components that might have a Font set on them, e.g. JLabel, JButton, etc.   This isn't really a workable solution.   We have encountered this in 3rd party libraries for which we cannot change the behavior.

FREQUENCY : always



Comments
This is no longer reproducible in 11.0.6 which has the fix for JDK-8217731. Also disabling that fix with export FREETYPE_PROPERTIES=interpreter-version causes the problem to reappear. The odd value there is because a strstr call had a bug fixed in a later release.
25-08-2020

I'm sure this is NOT a regression of "import freetype". It is related to USING freetype at all which is a distinct issue. And why is it a regression ? If freetype measures the text differently that may be an artifact of freetype. Until it is investigated we don't know and it may just be a "difference" that you will need to live with.
29-11-2018

Regression of JDK-8193017 $ /cygdrive/d/Vbox_Shared/JDK/jdk11-b05/bin/java Test String bounds: java.awt.geom.Rectangle2D$Float[x=0.0,y=-9.004395,w=44.0,h=10.863281] Preferred size: java.awt.Dimension[width=40,height=11] PRSADHUK-IN+prsadhuk@PRSADHUK-IN /cygdrive/d/jdk10/client/build/windows-x86_64-normal-server-fastdebug $ /cygdrive/d/Vbox_Shared/JDK/jdk11-b05/bin/java -Dsun.java2d.font.scaler=t2k Test String bounds: java.awt.geom.Rectangle2D$Float[x=0.0,y=-9.004395,w=44.0,h=10.863281] Preferred size: java.awt.Dimension[width=44,height=11]
29-11-2018

Incorrect preferred width of a Component is recorded when using a custom Font. This is a regression introduced in JDK 11. Results: ========== JDK 8u191: OK 10.0.2: OK 11: Fail 11.0.1: Fail 12 ea b20: Fail To verify, run the attached test case with respective JDK versions. JDK-11 ======= $ java Test String bounds: java.awt.geom.Rectangle2D$Float[x=0.0,y=-9.004395,w=44.0,h=10.863281] Preferred size: java.awt.Dimension[width=40,height=11] JDK 10.0.2: ============= $ java Test String bounds: java.awt.geom.Rectangle2D$Float[x=0.0,y=-9.004395,w=44.0,h=10.863281] Preferred size: java.awt.Dimension[width=44,height=11]
28-11-2018