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

[macos] Sub/Superscript text is rendered with incorrect spacing between characters

    Details

    • Subcomponent:
      2d
    • Introduced In Version:
      9
    • CPU:
      x86
    • OS:
      os_x

      Description

      ADDITIONAL SYSTEM INFORMATION :
      Tested on OS X 10.12, the latest AdoptOpenJDK 8, 10, 12 builds.

      A DESCRIPTION OF THE PROBLEM :
      Using TextLayout, subscripted and superscripted text is rendered with incorrect character spacing. Looks like character width is doubled. The code example below works correct on Java 8. Test fails on Java 10 and 12.
      The issue only appears with some fonts, for example, Times, Helvetica. But Times New Roman font works OK.

      Is there any workaround now?

      REGRESSION : Last worked in version 8u212

      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      Draw sub/superscript text using Graphics2D with certain fonts.


      ---------- BEGIN SOURCE ----------
      import java.awt.Color;
      import java.awt.Font;
      import java.awt.Graphics2D;
      import java.awt.Rectangle;
      import java.awt.RenderingHints;
      import java.awt.font.FontRenderContext;
      import java.awt.font.TextAttribute;
      import java.awt.font.TextLayout;
      import java.awt.geom.AffineTransform;
      import java.awt.image.BufferedImage;
      import java.io.File;
      import java.io.IOException;
      import java.text.AttributedString;
      import javax.imageio.ImageIO;

      public class SuperscriptTest
      {
        public static void main(String[] args)
        {
          int width = 400;
          int height = 200;
          BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);

          AttributedString string = new AttributedString("TestString");
          string.addAttribute(TextAttribute.FAMILY, "Times");
          string.addAttribute(TextAttribute.SIZE, 32);
          string.addAttribute(TextAttribute.SUPERSCRIPT, TextAttribute.SUPERSCRIPT_SUB, 4, 10);
          TextLayout textLayout = new TextLayout(string.getIterator(), new FontRenderContext(new AffineTransform(), RenderingHints.VALUE_TEXT_ANTIALIAS_ON, RenderingHints.VALUE_FRACTIONALMETRICS_ON));

          Graphics2D graphics2D = (Graphics2D) image.getGraphics();
          graphics2D.setColor(Color.white);
          graphics2D.fillRect(0, 0, width, height);
          graphics2D.setColor(Color.black);

          Rectangle rect = textLayout.getBounds().getBounds();
          textLayout.draw(graphics2D, (width - (rect.width - rect.x)) / 2, (height - rect.height - rect.y) / 2);
          
          graphics2D.setFont(new Font(Font.DIALOG, Font.PLAIN, 14));
          graphics2D.drawString(System.getProperty("java.vm.name") + " " + System.getProperty("java.version"), 10, 180);
          graphics2D.dispose();

          try
          {
            ImageIO.write(image, "png", new File("FontTest.png"));
          }
          catch (IOException e)
          {
            e.printStackTrace();
          }
        }
      }

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

      FREQUENCY : always


        Attachments

          Activity

            People

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

              Dates

              • Created:
                Updated: