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

java.awt.FontMetrics getAscent gives incorrect value for large fonts on Windows

    Details

    • CPU:
      x86_64
    • OS:
      windows_10

      Description

      A DESCRIPTION OF THE PROBLEM :
      When you have a large font with size ~1e5, then java.awt.FontMetrics returns 0.0 on Windows. On Mac, and Linux it seems to give the correct result.

      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      1. Create a new font with size ~1e5 (in my case , for e.g. 133333)
      2. Get the font's line metrics.
      3. Get the line metric's ascent.
      4. Observe the values in Windows vs Mac. On Windows, ascent value is 0.0, which is incorrect.

      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      A non-zero value. The difference can be seen when running the above code from Windows as well as Mac.

      ---------- BEGIN SOURCE ----------
      package com.example.helloworld;

      /**
       * Created by Abhinav Srinivasan on 6/28/2018.
      */


      import java.awt.Color;
      import java.awt.Container;
      import java.awt.Font;
      import java.awt.Graphics;
      import java.awt.Graphics2D;
      import java.awt.RenderingHints;
      import java.awt.font.FontRenderContext;
      import java.awt.font.LineMetrics;
      import java.awt.geom.Line2D;

      import javax.swing.JComponent;
      import javax.swing.JFrame;

      public class HelloWorld {
          public static void main(String[] args) {
              JFrame jf = new JFrame("Demo");
              Container cp = jf.getContentPane();
              MyCanvas tl = new MyCanvas();
              cp.add(tl);
              jf.setSize(600, 400);
              jf.setVisible(true);
          }
      }

      class MyCanvas extends JComponent {

          public void paint(Graphics g) {
              Graphics2D g2 = (Graphics2D) g;

              g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
              //Font font = new Font("Serif", Font.PLAIN, 72);
              Font font = new Font("Serif", Font.PLAIN, 133333);
              g2.setFont(font);

              String s = "This is a sample string";
              float x = 50, y = 150;

              FontRenderContext frc = g2.getFontRenderContext();
              float width = (float) font.getStringBounds(s, frc).getWidth();
              Line2D baseline = new Line2D.Float(x, y, x + width, y);
              g2.setPaint(Color.lightGray);
              g2.draw(baseline);

              // Draw the ascent.
              LineMetrics lm = font.getLineMetrics(s, frc);
              // Print the LineMetrics
              System.out.println(lm.getAscent());

              Line2D ascent = new Line2D.Float(x, y - lm.getAscent(), x + width, y - lm.getAscent());
              g2.draw(ascent);

              // Draw the descent.
              Line2D descent = new Line2D.Float(x, y + lm.getDescent(), x + width, y + lm.getDescent());
              g2.draw(descent);

              // Draw the leading.
              Line2D leading = new Line2D.Float(x, y + lm.getDescent() + lm.getLeading(), x + width, y
                      + lm.getDescent() + lm.getLeading());
              g2.draw(leading);

              // Render the string.
              g2.setPaint(Color.black);
              //g2.drawString(s, x, y);
              g2.drawString(String.valueOf(lm.getAscent()), x, y);
          }

      }


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

      FREQUENCY : always


        Attachments

          Activity

            People

            • Assignee:
              bvaidya Balchandra Vaidya
              Reporter:
              webbuggrp Webbug Group
            • Votes:
              0 Vote for this issue
              Watchers:
              3 Start watching this issue

              Dates

              • Created:
                Updated:
                Resolved: