bobbylight / RSyntaxTextArea

A syntax highlighting, code folding text editor for Java Swing applications.
BSD 3-Clause "New" or "Revised" License
1.12k stars 259 forks source link

Font rendering broken for certain tokens in RSyntaxTextArea on M1 MacOS 13.2.1 #494

Open kspalaiologos opened 1 year ago

kspalaiologos commented 1 year ago

I have implemented my own highlighter and style the text as follows:

        scheme.getStyle(Token.COMMENT_EOL).foreground = Color.decode("#676B79");
        scheme.getStyle(Token.LITERAL_NUMBER_DECIMAL_INT).foreground = Color.decode("#FFB86C");
        scheme.getStyle(Token.LITERAL_NUMBER_FLOAT).foreground = Color.decode("#FFB86C");
        scheme.getStyle(Token.LITERAL_CHAR).foreground = Color.decode("#FFB86C");
        scheme.getStyle(Token.LITERAL_BOOLEAN).foreground = Color.decode("#45A9F9");
        scheme.getStyle(Token.VARIABLE).foreground = Color.decode("#E6E6E6");
        scheme.getStyle(Token.RESERVED_WORD).foreground = Color.decode("#FF75B5");
        scheme.getStyle(Token.OPERATOR).foreground = Color.decode("#B084EB");
        scheme.getStyle(Token.FUNCTION).foreground = Color.decode("#FFCC95");
        scheme.getStyle(Token.LITERAL_STRING_DOUBLE_QUOTE).foreground = Color.decode("#6FC1FF");

Using the following font:

    public static final Font aplFont;

    static {
        try {
            aplFont = Font.createFont(Font.TRUETYPE_FONT, Objects.requireNonNull(IDE.class.getResourceAsStream("/APL385.ttf"))).deriveFont(Font.BOLD, 15f);
        } catch (FontFormatException | IOException e) {
            throw new RuntimeException(e);
        }
    }

This is the result on MacOS:

obraz

This is the result on Debian Linux (OpenJDK 17, 18, 19 all work the same):

obraz

The problem is clear - flt64:lerch-phi is highlighted using a completely different font from the rest of the text area. Interestingly enough, this issue manifests itself only on MacOS, only with the token type RESERVED_WORD.

The code: https://github.com/kspalaiologos/kamilalisp/tree/ide

kspalaiologos commented 1 year ago

Originally discovered by https://github.com/mrbenjadmin - I do not use MacOS and am reporting the issue on his behalf.

bobbylight commented 1 year ago

What a cool issue! My first guess is macOS automatically uses a fallback font when glyphs can't be rendered. The text in the screenshots is clearly all 7-bit ASCII so my guess is it's not the codepoints themselves but has to do with TokenTypes.RESERVED_WORD defaulting to bold whereas other token types don't. Not sure yet why other OS's wouldn't also use fallback fonts or show a placeholder char like the empty rectangle, but different OS's surely have different rendering engines, so maybe there's just some limitation in the macOS JVM's font rendering implementation?

Anyway I'll try to reproduce on my Macbook

kspalaiologos commented 1 year ago

@bobbylight I found a workaround!

Turns out that you have to reset the font in the styles, not only just call setFont on the RSTA:

        scheme.restoreDefaults(IDE.aplFont, false);

It is incredibly weird that this issue manifested only in very specific circumstances, though.