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

AppleSystemUIFont ("San Francisco" font) has exaggerated letter spacing

XMLWordPrintable

    • 2d
    • x86_64
    • os_x

      ADDITIONAL SYSTEM INFORMATION :
      Bug confirmed on OpenJDK 21.0.4 and 22.0.2 on MacOS Sonoma 14.6.1.

      A DESCRIPTION OF THE PROBLEM :
      The "San Francisco" font, which has been the default UI font on MacOS since MacOS Catalina (released October 2019), does not display correctly in Java; the characters display with very wide spacing. See the screenshot at AppleSystemFontExhibitComparison.png

      The font in question is a "hidden" font which can be loaded with either new Font(".AppleSystemUIFont", Font.PLAIN, 13) or new Font(".SFNS-Regular", Font.PLAIN, 13).

      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      Run the example JFrame in the attached sample code on MacOS Catalina or later. The string "The Quick Brown Fox Jumps Over the Lazy Dog" should display in a JLabel.

      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      The string should be displayed with the same visual appearance as in MacOS Finder (see the earlier-referenced screenshot).
      ACTUAL -
      The displayed string has excessive spacing between the characters, making the overall rendered string significantly wider (see the earlier-referenced screenshot).

      ---------- BEGIN SOURCE ----------
      import java.awt.*;
      import java.awt.font.TextAttribute;
      import java.util.HashMap;
      import java.util.Map;
      import javax.swing.*;

      public class ExhibitAppleSystemFontFrame extends JFrame {
        public static final boolean ENABLE_WORKAROUND = false;

        public ExhibitAppleSystemFontFrame() {
          setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
          setBackground(new java.awt.Color(255, 255, 255));

          JLabel label = new JLabel(
              "The Quick Brown Fox Jumps Over the Lazy Dog (workaround=" + ENABLE_WORKAROUND + ")");
          getContentPane().add(label, BorderLayout.CENTER);

          // Set colors for easy comparison with e.g. file names in the MacOS Finder
          label.setForeground(new Color(38, 38, 38));
          label.setBackground(Color.WHITE);
          label.setOpaque(true);

          /* This font is available on MacOS Catalina (10.15, released October 2019) and later.
          The name ".SFNS-Regular" seems to yield the same font. */
          Font font = new Font(".AppleSystemUIFont", Font.PLAIN, 13);
          Map<TextAttribute,Object> attrs = new HashMap<>();
          if (ENABLE_WORKAROUND) {
            attrs.put(TextAttribute.TRACKING, -0.048f);
          }
          /* Including the KERNING_ON hint can make kerning slightly better in some cases, and slightly
          worse in some other cases. It does not fix the exaggerated tracking. */
          // attrs.put(TextAttribute.KERNING, TextAttribute.KERNING_ON);
          font = font.deriveFont(attrs);
          label.setFont(font);

          setSize(new Dimension(600, 100));
        }

        public static void main(String args[]) throws Exception {
          UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
          SwingUtilities.invokeLater(() -> {
            new ExhibitAppleSystemFontFrame().setVisible(true);
          });
        }
      }
      ---------- END SOURCE ----------

      CUSTOMER SUBMITTED WORKAROUND :
      I was able to simulate correct character spacing by setting the TextAttribute.TRACKING text attribute to -0.048. This can be tested by setting ENABLE_WORKAROUND = true in the pasted sample code.

      This is probably a rather fragile hack, though, and kerning is still off in various places when the font is used as a general UI font.

      The bug was also reported in IntelliJ:
      https://youtrack.jetbrains.com/issue/JBR-1756/San-Francisco-font-has-wide-spacing-on-Catalina
      In that case they fixed it with a custom OpenJDK patch:
      https://github.com/JetBrains/JetBrainsRuntime/commit/02da4439e833dba780d28b0d735ae93754cf8e2e

      FREQUENCY : always


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

              Created:
              Updated: