Monospaced font/symbols for JTextPane

我的未来我决定 提交于 2019-12-05 01:29:37
CodeBlind

Ok, first off, it sounds to me like you're trying to address a couple of different things here, so I'll try to address them both separately.

1. You need a font that is monospaced for all unicode characters, symbols or otherwise.

According to this page, there were 12886 alphanumeric and "symbol" characters defined by the Unicode 3.2 standard. Unicode is now at 6.0, so we can probably assume that number is larger now. I'm also assuming here that "alphanumeric" means English characters, because Unicode supports over 100000 characters spanning many languages. At any rate, 12886 English-recognized characters and symbols is still A LOT, and I doubt that there are many free fonts that support all of them.

That said, I end up using Courier New for most of my Java applications that need a mono-spaced font with character support. It supports the '\u2588' character you mentioned above as well as many other important ones like the "degrees" symbol.

2. This mono-spaced font needs to be "cross-platform"

I know for certain that Mac OS X and all flavors of Windows support Courier New, and the versions of Linux I run on do too (RedHat... can't remember the version number), although not all Linux versions have this font natively. Anyway... you might try Courier New and see if it works for you. If not, you can probably find free tools online for testing font/character support.

Final thoughts

I hate to say it, but I doubt that there are many fonts out there that support all alphanumeric/symbol characters defined by the Unicode standard, least of all being mono-spaced, cross-platform, and free. If possible, it might be more worth your time to try to figure out what symbols you will need for certain, then choose a font that supports those symbols and is in turn supported by the platforms you know you will run your application on. If you absolutely need to have support for all Unicode symbols, unfortunately, it probably won't come free.

Monospaced font works on OS X, but not Windows. The weird thing is that monospaced is configured to use Courier New on Windows via the fontconfig.properties.

I've actually logged this as a bug with Oracle; here's some sample code that renders correctly on OS X but the monospaced font produces square boxes on Windows.

import java.awt.BorderLayout;
import java.awt.Font;

import javax.swing.JEditorPane;
import javax.swing.JFrame;

public class Test {

    public static void main(String[] args) {
        JFrame f = new JFrame("editor test");
        f.getContentPane().setLayout(new BorderLayout());
        f.add(pane("monospaced"), BorderLayout.NORTH);
        f.add(pane("Courier New"), BorderLayout.SOUTH);
        f.pack();
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.setVisible(true);
    }

  private static JEditorPane pane(final String name) {
    JEditorPane p = new JEditorPane();
    final Font currFont = p.getFont();
    p.setFont(new Font(name, currFont.getStyle(), currFont.getSize()));
    p.setText(name + " - 8\u1d00.\u1d0d.");
    return p;
  }
}

I tried adding \u2588 to it and that looks fine on OS X monospaced too.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!