JDK-8205046 : Calling Graphics2D.rotate(...) modifies FontMetrics
  • Type: Bug
  • Component: client-libs
  • Sub-Component: 2d
  • Affected Version: 11
  • Priority: P3
  • Status: Closed
  • Resolution: Duplicate
  • Submitted: 2018-06-14
  • Updated: 2018-11-16
  • Resolved: 2018-07-16
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.
JDK 11
11Resolved
Related Reports
Duplicate :  
Relates :  
Description
See test below - the issue occurs with Java 11 EA17 on Windows OS. The font metrics object is modified by calling g2.rotate(...).

Test-----------------------------

import java.awt.FlowLayout;
import java.awt.FontMetrics;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Rectangle;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.SwingUtilities;

public class RotationTest extends JFrame{
  public static void main(String[] args){
    SwingUtilities.invokeLater(() -> new RotationTest());
  }

  public RotationTest(){
    setLayout(new FlowLayout());
    JLabel l = new JLabel("123") {
      @Override
      protected void paintComponent(Graphics g)
      {
        Rectangle textRect = new Rectangle();
        textRect.setBounds(0, 0, getWidth(), getHeight());              
        Graphics2D g2 = (Graphics2D)g;
        
        double origin = (textRect.width-textRect.height)/2; 
        g2.translate(origin, origin);
        FontMetrics fm1 = g.getFontMetrics();
        g2.rotate(Math.toRadians(90), textRect.x+textRect.width/2, textRect.y+textRect.height/2);
        FontMetrics fm2 = g.getFontMetrics();
        assert fm1.getAscent() == fm2.getAscent();
        super.paintComponent(g2);
        g2.rotate(-Math.toRadians(90), textRect.x+textRect.width/2, textRect.y+textRect.height/2);
        g2.translate(-origin, -origin);      
      }
    };
    add(l);

    setTitle(this.getClass().getSimpleName());
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setSize(400, 300);
    setLocationRelativeTo(null);
    setVisible(true);
  }
}

Comments
It is a duplicate of JDK-8139178
16-07-2018

assert fm1.getAscent() == fm2.getAscent(); is a bit strong, since there's no guarantee of getting the same instance in two calls to g.getFontMetrics() without any changes in between. Although we don't have an over-ride of equals either .. and I think there may be an open bug on that. But the issue here is that under the 90 degree rotation we report ascent etc. as zero 0 degrees rotation: sun.font.FontDesignMetrics[font=javax.swing.plaf.FontUIResource[family=Dialog,name=Dialog,style=bold,size=12]ascent=12, descent=3, height=15] 90 degrees rotation : sun.font.FontDesignMetrics[font=javax.swing.plaf.FontUIResource[family=Dialog,name=Dialog,style=bold,size=12]ascent=0, descent=0, height=0] There's an open bug on this too, previously reported against OpenJDK, which always used freetype.
16-07-2018