Search code examples
javaswingfontsawtarabic

Some words in Arabic appear calligraphic in Swing


It seems that Swing auto decorate some Arabic words by making them look some kind calligraphic. One of those words is Muhammad which is spelled in Arabic as محمد.

enter image description here

import java.awt.Font;

import javax.swing.JFrame;
import javax.swing.JLabel;

public class TestProject extends JFrame {
    
    public static void main(String[] args) {
        TestProject frame = new TestProject();
        frame.setVisible(true);
    }
    
    public TestProject() {
        this.setSize(200, 100);
        this.setResizable(false);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        JLabel label = new JLabel("محمد");
        label.setFont(new Font("Arial", Font.BOLD, 28));
        this.add(label);
    }

}

The problem is applied to all widely used Fonts at least.

I'm using Windows 10 & Java 8. I've tried the same program on another PC (also Windows 10) same problem.

enter image description here

How can we disable that?


Solution

  • This seems to be a bug in the old (i.e. up to Java 8) font rendering engine.

    This is how it looks with Java 8 on my machine:

    Font rendering with Java 8

    And this is the result with Java 9:

    Font rendering with Java 9

    I don’t know whether an explicit bug report has been reported for this specific issue. But the font engine has been replaced entirely, due to JEP 258: HarfBuzz Font-Layout Engine.

    Replace the existing ICU OpenType font-layout engine with HarfBuzz.

    So it’s not surprising that bugs of the old font rendering are gone.