Best way to render overlined text with custom font settings

100 Views Asked by At

I'm writing a Swing application and I'm trying to figure out a way to display text while being able to control the following text properties:

  • font family and size
  • text overline
  • lower index

Of course the more flexible the approach, the better, but those are strictly mandatory for me.

What I already tried:

  • using JTextPane and setting text-decoration: overline attribute - doesn't work, because JTextPane html/css engine is dated and doesn't support this
  • using JTextPane with setting upper border via css - doesn't work, because it can only be applied to html nodes with display:block (such as divs), and I need this inline (setting display: inline-block is not supported in JTextPane, so I can't use that)
  • using WebView from JavaFX - this is where I'm currently at, but I can't figure out how to load a custom font programmatically. It only seems to be working when the font is loaded at OS level (e.g. when the font file is present in ~/.fonts on Linux)

This is a working example. Mostly taken from Integrating JavaFX into Swing Applications.

Mind the font file, which you might have to specify yourself (here's the file I used):

import javafx.application.Platform;
import javafx.embed.swing.JFXPanel;
import javafx.scene.Scene;
import javafx.scene.text.Font;
import javafx.scene.web.WebView;
import javax.swing.JFrame;
import javax.swing.SwingUtilities;

public class Test {

    private static void initAndShowGUI() {
        JFrame frame = new JFrame("Swing and JavaFX");
        final JFXPanel fxPanel = new JFXPanel();
        frame.add(fxPanel);
        frame.setSize(300, 200);
        frame.setVisible(true);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    
        Platform.runLater(() -> initFX(fxPanel));
    }
    
    private static void initFX(JFXPanel fxPanel) {
        Scene scene = createScene();
        fxPanel.setScene(scene);
    }
    
    private static Scene createScene() {
        var wb = new WebView();
    
        // ???
        var f = Font.loadFont("lmroman10-regular.otf", 10);
        assert f != null;
    
        wb.getEngine().loadContent(
                "<html><body>" +
                        "<span style=\"" +
                            "text-decoration: overline; " +
                            "font-size: 24px;" +
                            "font-family: 'LM Roman 10';\">" +
                                "foo" +
                        "</span>" +
                        "bar" +
                        "</body></html>");
    
        return new Scene(wb);
    }
    
    public static void main(String[] args) {
        SwingUtilities.invokeLater(() -> initAndShowGUI());
    }
}
0

There are 0 best solutions below