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

Problem with font #445

Open Jankbyte opened 2 years ago

Jankbyte commented 2 years ago

Description Im tried to use syntaxtextarea in JavaFX (v18.0.1) and got problem with fonts

Screenshots Снимок1 Java version Java 17 LTS

The code of JavaFX program ` import java.io.InputStream; import java.io.IOException; import javafx.geometry.Pos; import javafx.application.Application; import javafx.stage.Stage; import javafx.scene.Scene; import javafx.scene.layout.BorderPane; import javafx.scene.layout.StackPane; import javafx.embed.swing.SwingNode; import javax.swing.UIManager; import javax.swing.UnsupportedLookAndFeelException; import javax.swing.JButton; import javax.swing.JPanel; import javax.swing.JComponent; import javax.swing.SwingUtilities; import java.awt.BorderLayout; import javafx.scene.control.Button; import org.fife.ui.rtextarea.RTextScrollPane; import org.fife.ui.rsyntaxtextarea.TextEditorPane; import org.fife.ui.rsyntaxtextarea.Theme; import org.fife.ui.rsyntaxtextarea.SyntaxConstants; import java.awt.BorderLayout;

public class MyApplication extends Application {

@Override
public void start(Stage stage) {
    stage.setTitle("Swing in JavaFX");

    final SwingNode sn = new SwingNode();
    createSwingContent(sn);

    BorderPane pane = new BorderPane();

    Button fxButton = new Button("The Java FX button");
    pane.setAlignment(fxButton, Pos.CENTER);

    pane.setTop(fxButton);
    pane.setCenter(sn);

    stage.setScene(new Scene(pane, 400, 200));
    stage.show();
}

private void createSwingContent(final SwingNode swingNode) {
    SwingUtilities.invokeLater(() -> {
        swingNode.setContent(getEditorPanel());
    });
}

private JComponent getEditorPanel() {
    JPanel panel = new JPanel(new BorderLayout());
    TextEditorPane textArea = new TextEditorPane();
    textArea.setSyntaxEditingStyle(SyntaxConstants.SYNTAX_STYLE_JAVA);
    setThemeToEditor(textArea);
    //var color = new java.awt.Color(31, 27, 27);
    //textArea.setBackground(color);
    //textArea.setCurrentLineHighlightColor(color);
    textArea.setText("""
                package somepackage;

                import java.lang.System;

                public class Main {
                    public static void main(String[] args) {
                        System.out.println("Hello world!");
                    }
                }""");
    textArea.setCodeFoldingEnabled(true);
    RTextScrollPane scrollerPane = new RTextScrollPane(textArea);

    panel.add(scrollerPane);
    return panel;
}

private void setThemeToEditor(final TextEditorPane textArea) {
    try {
        InputStream is = MyApplication.this.getClass().getResourceAsStream("/org/fife/ui/rsyntaxtextarea/themes/idea.xml");
        Theme theme = Theme.load(is);
        theme.apply(textArea);
        is.close();
    } catch(IOException e) {
        e.printStackTrace();
    }
}

} `

omegaui commented 2 years ago

@ByteC0d3 I also came across this problem when I tried wrapping RSyntaxTextArea in SwingNode (when I initially started writing my IDE) and this is not related to fonts. In the end, it was JavaFX that was causing this problem due to some mismatch or unsupported antialiasing support for Swing Components, I didn't actually dig deeper into it and switched the entire app to Swing(flexible API rocks).

You could try handling GraphicsContext of your SwingNode to see if the problem persists.

bobbylight commented 2 years ago

There have been (still are, probably?) other issues with embedding RSTA in JavaFX apps. I agree with @omegaui and don't think embedding custom-rendered Swing components in JavaFX is fully baked.

What does RSyntaxUtilities.getBestPossibleAntiAliasHints() return in each test case? This might be the starting point of the issue - that e.g. awt.font.desktophints is null and so RSTA is resorting to Swing's software font rendering.

Jankbyte commented 1 year ago

There have been (still are, probably?) other issues with embedding RSTA in JavaFX apps. I agree with @omegaui and don't think embedding custom-rendered Swing components in JavaFX is fully baked.

What does RSyntaxUtilities.getBestPossibleAntiAliasHints() return in each test case? This might be the starting point of the issue - that e.g. awt.font.desktophints is null and so RSTA is resorting to Swing's software font rendering.

Sorry for so long answer about that (i returned back to my project). Idk how i can test RSyntaxUtilities.getBestPossibleAntiAliasHints(), but i run this inside main method of my application:

public static void main(String[] args) {
    Map<?,?> values = getBestPossibleAntiAliasHints();
    values.entrySet().forEach(entry -> {
       System.out.printf("%s, %s\n", entry.getKey(), entry.getValue());
    });
    launch(args);
}

And got next results at console:

Text-specific antialiasing enable key, LCD HRGB antialiasing text mode
Text-specific LCD contrast key, 120

P.S. After sometime i changed my OS from win10 to win11 (x64, x64) - and problem wasn't solved with this "pixels".