RSyntaxTextArea icon indicating copy to clipboard operation
RSyntaxTextArea copied to clipboard

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

Open iczelia opened this issue 2 years ago • 3 comments

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

iczelia avatar Feb 20 '23 19:02 iczelia

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

iczelia avatar Feb 20 '23 19:02 iczelia

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

bobbylight avatar Feb 25 '23 01:02 bobbylight

@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.

iczelia avatar Feb 25 '23 09:02 iczelia