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

FontMetrics methods return different values in Java 11 compared to Java 8

    Details

    • Subcomponent:
      2d
    • Introduced In Version:
      11
    • CPU:
      x86_64
    • OS:
      windows_10

      Description

      ADDITIONAL SYSTEM INFORMATION :
      Window 10
      ** java.vendor = Oracle Corporation
      ** java.version = 11.0.1
      ** java.vm.name = OpenJDK 64-Bit Server VM

      A DESCRIPTION OF THE PROBLEM :
      We use the FontMetrics object to help position text on our customers diagrams where they want it.
      For some fonts, the values obtained from the FontMetrics object remain the same compared with Java 8
      For many other fonts, in Java 11, we can get different values for Ascent,Descent,Height,Leading,MaxAdvance,MaxAscent&MaxDescent values.
      This causes our customer's 'released diagrams' (diagrams that should not be changing) to look different.

      REGRESSION : Last worked in version 8u172

      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      Compile & Run the provided code in Java 8.
      Compile & Run the provided code in Java 11.
      Note that for MANY fonts, we get different values for Ascent,Descent,Height,Leading,MaxAdvance,MaxAscent&MaxDescent values.

      Running the provided code in Java 12ea produces the same result.

      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      We should get the same values as we did in Java 8 for all fonts =>

      ** java.vendor = Oracle Corporation
      ** java.version = 1.8.0_172
      ** java.vm.name = Java HotSpot(TM) 64-Bit Server VM

      Name Family Style Size Ascent Descent Height Leading MaxAdvance MaxAscent MaxDescent stringWidth
      Arial Arial 0 12.0 12 3 15 0 24 12 3 51
      Arial Arial 0 24.0 23 5 29 1 48 23 5 101
      Arial Arial 0 48.0 45 10 56 1 96 45 10 199
      Arial Black Arial Black 0 12.0 14 4 18 0 21 14 4 61
      Arial Black Arial Black 0 24.0 27 8 35 0 42 27 8 123
      Arial Black Arial Black 0 48.0 53 15 68 0 83 53 15 245
      Arial Bold Arial 0 12.0 12 3 15 0 24 12 3 51
      Arial Bold Arial 0 24.0 23 5 29 1 48 23 5 109
      Arial Bold Arial 0 48.0 45 10 56 1 96 45 10 215
      Arial Bold Italic Arial 0 12.0 12 3 15 0 16 12 3 53
      Arial Bold Italic Arial 0 24.0 23 5 29 1 32 23 5 106
      Arial Bold Italic Arial 0 48.0 45 10 56 1 64 45 10 215
      Arial Italic Arial 0 12.0 12 3 15 0 16 12 3 52
      Arial Italic Arial 0 24.0 23 5 29 1 32 23 5 100
      Arial Italic Arial 0 48.0 45 10 56 1 64 45 10 202
      Arial Narrow Arial Narrow 0 12.0 12 3 15 0 13 12 3 41
      Arial Narrow Arial Narrow 0 24.0 23 6 29 0 26 23 6 80
      Arial Narrow Arial Narrow 0 48.0 45 11 56 0 51 45 11 165
      Arial Narrow Bold Arial Narrow 0 12.0 12 3 15 0 13 12 3 44
      Arial Narrow Bold Arial Narrow 0 24.0 23 6 29 0 26 23 6 87
      Arial Narrow Bold Arial Narrow 0 48.0 45 11 56 0 51 45 11 177
      Arial Narrow Bold Italic Arial Narrow 0 12.0 12 3 15 0 13 12 3 44
      Arial Narrow Bold Italic Arial Narrow 0 24.0 23 6 29 0 26 23 6 87
      Arial Narrow Bold Italic Arial Narrow 0 48.0 45 11 56 0 51 45 11 177
      Arial Narrow Italic Arial Narrow 0 12.0 12 3 15 0 13 12 3 39
      Arial Narrow Italic Arial Narrow 0 24.0 23 6 29 0 26 23 6 81
      Arial Narrow Italic Arial Narrow 0 48.0 45 11 56 0 51 45 11 165
      Arial Rounded MT Bold Arial Rounded MT Bold 0 12.0 12 3 15 0 15 12 3 53
      Arial Rounded MT Bold Arial Rounded MT Bold 0 24.0 23 6 29 0 29 23 6 110
      Arial Rounded MT Bold Arial Rounded MT Bold 0 48.0 46 11 57 0 58 46 11 216
      Arial Unicode MS Arial Unicode MS 0 12.0 13 4 17 0 28 13 4 52
      Arial Unicode MS Arial Unicode MS 0 24.0 26 7 33 0 55 26 7 98
      Arial Unicode MS Arial Unicode MS 0 48.0 52 13 65 0 109 52 13 202
      ACTUAL -
      We get different values for FontMetrics, for many fonts in Java 11 & 12ea=>

      ** java.vendor = Oracle Corporation
      ** java.version = 11.0.1
      ** java.vm.name = OpenJDK 64-Bit Server VM

      Name Family Style Size Ascent Descent Height Leading MaxAdvance MaxAscent MaxDescent stringWidth
      Arial Arial 0 12.0 11 3 14 0 24 11 3 51
      Arial Arial 0 24.0 22 6 28 0 48 22 6 101
      Arial Arial 0 48.0 44 11 56 1 96 44 11 199
      Arial Black Arial Black 0 12.0 14 4 18 0 21 14 4 61
      Arial Black Arial Black 0 24.0 27 8 35 0 41 27 8 123
      Arial Black Arial Black 0 48.0 53 15 68 0 83 53 15 245
      Arial Bold Arial 0 12.0 11 3 14 0 24 11 3 51
      Arial Bold Arial 0 24.0 22 6 28 0 48 22 6 109
      Arial Bold Arial 0 48.0 44 11 56 1 96 44 11 215
      Arial Bold Italic Arial 0 12.0 11 3 14 0 16 11 3 53
      Arial Bold Italic Arial 0 24.0 22 6 28 0 32 22 6 106
      Arial Bold Italic Arial 0 48.0 44 11 56 1 64 44 11 215
      Arial Italic Arial 0 12.0 11 3 14 0 16 11 3 52
      Arial Italic Arial 0 24.0 22 6 28 0 32 22 6 100
      Arial Italic Arial 0 48.0 44 11 56 1 64 44 11 202
      Arial Narrow Arial Narrow 0 12.0 12 3 15 0 13 12 3 41
      Arial Narrow Arial Narrow 0 24.0 23 6 29 0 25 23 6 80
      Arial Narrow Arial Narrow 0 48.0 45 11 56 0 51 45 11 165
      Arial Narrow Bold Arial Narrow 0 12.0 12 3 15 0 13 12 3 44
      Arial Narrow Bold Arial Narrow 0 24.0 23 6 29 0 25 23 6 87
      Arial Narrow Bold Arial Narrow 0 48.0 45 11 56 0 51 45 11 177
      Arial Narrow Bold Italic Arial Narrow 0 12.0 12 3 15 0 13 12 3 44
      Arial Narrow Bold Italic Arial Narrow 0 24.0 23 6 29 0 25 23 6 87
      Arial Narrow Bold Italic Arial Narrow 0 48.0 45 11 56 0 51 45 11 177
      Arial Narrow Italic Arial Narrow 0 12.0 12 3 15 0 13 12 3 39
      Arial Narrow Italic Arial Narrow 0 24.0 23 6 29 0 25 23 6 81
      Arial Narrow Italic Arial Narrow 0 48.0 45 11 56 0 51 45 11 165
      Arial Rounded MT Bold Arial Rounded MT Bold 0 12.0 12 3 15 0 15 12 3 53
      Arial Rounded MT Bold Arial Rounded MT Bold 0 24.0 23 6 29 0 29 23 6 110
      Arial Rounded MT Bold Arial Rounded MT Bold 0 48.0 46 11 57 0 58 46 11 216
      Arial Unicode MS Arial Unicode MS 0 12.0 13 4 17 0 27 13 4 52
      Arial Unicode MS Arial Unicode MS 0 24.0 26 7 33 0 54 26 7 98
      Arial Unicode MS Arial Unicode MS 0 48.0 52 13 65 0 108 52 13 202

      ---------- BEGIN SOURCE ----------
      import java.awt.Font;
      import java.awt.FontMetrics;
      import java.awt.Graphics2D;
      import java.awt.GraphicsEnvironment;
      import java.awt.image.BufferedImage;
      import java.util.Arrays;
      import java.util.List;

      public class FontMetricsIssue
      {

      private static final List<Float> sizes = Arrays.asList(12.0f, 24.0f, 48.0f);

      public static void main(String[] args)
      {
      titles();
      List<Font> fontList = getFonts();
      fontList
      .stream()
      .filter(FontMetricsIssue::isArial) // comment out to try with ALL fonts
      .sorted(FontMetricsIssue::comparitor)
      .forEach(FontMetricsIssue::reportFontMetrics);
      }

      private static boolean isArial(Font font)
      {
      return font.getName().contains("Arial");
      }

      private static List<Font> getFonts()
      {
      GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
      Font[] fonts = ge.getAllFonts();
      return Arrays.asList(fonts);
      }

      private static void titles()
      {
      System.err.println( "** java.vendor = " + System.getProperty("java.vendor"));
      System.err.println( "** java.version = " + System.getProperty("java.version"));
      System.err.println( "** java.vm.name = " + System.getProperty("java.vm.name"));
      System.err.println();
      System.err.println();

      System.err.print("Name");
      System.err.print("\tFamily");
      System.err.print("\tStyle");
      System.err.print("\tSize");
      System.err.print("\tAscent");
      System.err.print("\tDescent");
      System.err.print("\tHeight");
      System.err.print("\tLeading");
      System.err.print("\tMaxAdvance");
      System.err.print("\tMaxAscent");
      System.err.print("\tMaxDescent");
      System.err.print("\tstringWidth");
      System.err.println();
      }

      private static int comparitor(Font f1, Font f2)
      {
      int rval = f1.getFontName().compareTo(f2.getFontName());
      if (rval != 0) {
      return rval;
      }
      rval = f1.getFamily().compareTo(f2.getFamily());
      if (rval != 0) {
      return rval;
      }
      rval = f1.getName().compareTo(f2.getName());
      if (rval != 0) {
      return rval;
      }
      Integer i1 = f1.getStyle();
      Integer i2 = f1.getStyle();
      return i1.compareTo(i2);
      }

      private static void reportFontMetrics(Font baseFont)
      {
      sizes
      .stream()
      .forEach(size -> reportFontMetrics(baseFont, size));
      }

      private static void reportFontMetrics(Font baseFont, Float size)
      {
      Font font = baseFont.deriveFont(size);
      BufferedImage img = new BufferedImage(100, 100,
      BufferedImage.TYPE_INT_ARGB);
      Graphics2D g2d = img.createGraphics();
      FontMetrics metrics = g2d.getFontMetrics(font);
      System.err.print(font.getName());
      System.err.print("\t" + font.getFamily());
      System.err.print("\t" + font.getStyle());
      System.err.print("\t" + size);
      System.err.print("\t" + metrics.getAscent());
      System.err.print("\t" + metrics.getDescent());
      System.err.print("\t" + metrics.getHeight());
      System.err.print("\t" + metrics.getLeading());
      System.err.print("\t" + metrics.getMaxAdvance());
      System.err.print("\t" + metrics.getMaxAscent());
      System.err.print("\t" + metrics.getMaxDescent());
      System.err.print("\t" + metrics.stringWidth("Hello you"));
      System.err.println();
      }
      }

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

      CUSTOMER SUBMITTED WORKAROUND :
      None

      It looks like the class T2KFontScaler was removed in Java 11 and FreetypeFontScaler is used instead, so no workaround.

      FREQUENCY : always


        Attachments

          Activity

            People

            • Assignee:
              prr Philip Race
              Reporter:
              webbuggrp Webbug Group
            • Votes:
              0 Vote for this issue
              Watchers:
              5 Start watching this issue

              Dates

              • Created:
                Updated: