JDK-8227828 : [macos] Sub/Superscript text is rendered with incorrect spacing between characters
  • Type: Bug
  • Component: client-libs
  • Sub-Component: 2d
  • Affected Version: 9,10,11,13,14
  • Priority: P3
  • Status: Closed
  • Resolution: Duplicate
  • OS: os_x
  • CPU: x86
  • Submitted: 2019-07-11
  • Updated: 2021-07-07
  • Resolved: 2021-07-07
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 :  
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



Comments
This problem is cured as of JDK 17 b24 due to the fix for JDK-8256372 which starts to use the harfbuzz AAT font support rather than having harfbuzz to coretext.
07-07-2021

I think this is just an issue with AAT fonts on Mac and its been there since 9 when harfbuzz was introduced. Apps should be using the more modern OpenType fonts anyway. Probably a duplicate of some other open bug.
17-07-2019

Reported as a regression in JDK 12.0.1, using TextLayout with specific fonts like Times, Helvetica, etc. subscripted and superscripted text is rendered with incorrect character spacing. Checked this with reported versions and could confirm the issue. See attached image as reference. Issue is restricted to macOS as it works fine with Windows. Result (mac): ======== 8u212: OK 9: Fail 12.0.1: Fail 13 ea b29: Fail 14 ea b05: Fail To verify, run the attached test case with specific JDK version.
17-07-2019