Skip to content

Using custom registered font, setFont() is not working, but getFont*() overridden makes it work #596

@ron190

Description

@ron190

Hello, thank you for your work on this great library!

Description
Using setFont() on RSyntaxTextArea is not working for registered font (or for any unclear root cause), the custom font is ignored and the default font is used instead.

Issue is fixed when we override the methods getFont*() (see below).

Steps to Reproduce

  • First load a font :
var graphicsEnvironment = GraphicsEnvironment.getLocalGraphicsEnvironment();
InputStream fontStream = new BufferedInputStream(
    UiUtil.class.getClassLoader().getResourceAsStream("swing/font/UbuntuMono-R-ctrlchar.ttf")
);
var ubuntuFont = Font.createFont(Font.TRUETYPE_FONT, fontStream);
graphicsEnvironment.registerFont(ubuntuFont);
  • The next code displays the default font instead of the custom font (see Actual behavior):
RSyntaxTextArea textArea = new RSyntaxTextArea("text");
textArea.setSyntaxEditingStyle(SyntaxConstants.SYNTAX_STYLE_SQL);
textArea.setFont(new Font("Ubuntu Mono", Font.PLAIN, 12));
  • The next code overrides the methods getFont*() and displays the custom font properly (see Expected behavior):
RSyntaxTextArea textArea = new RSyntaxTextArea("text") {
        @Override
        public Font getFont() {
            return new Font("Ubuntu Mono", Font.PLAIN, 12);
        }
        @Override
        public Font getFontForToken(Token token) {
            return new Font("Ubuntu Mono", Font.PLAIN, 12);
        }
        @Override
        public Font getFontForTokenType(int type) {
            return new Font("Ubuntu Mono", Font.PLAIN, 12);
        }
    };
textArea.setSyntaxEditingStyle(SyntaxConstants.SYNTAX_STYLE_SQL);

Expected behavior
Correct custom font is displayed when overridden :

Image

Actual behavior
Unexpected default font is displayed using setFont() :

Image

Java version
Same issue and fix tested on jdk11, jdk17, jdk23.

Metadata

Metadata

Assignees

Labels

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions