SWT 有没有一种方法可以简单地获得跨各种操作系统的等宽字体?

例如。这适用于 Linux,但不适用于 Windows:


Font mono = new Font(parent.getDisplay(), "Mono", 10, SWT.NONE);

或者我是否需要一种方法来尝试加载不同的字体(Consolas、Terminal、Monaco、Mono)直到其中一个不为空?或者,我可以在启动时在属性文件中指定它。

我尝试从 Display 获取系统字体,但它不是等宽的。

有帮助吗?

解决方案

根据有关部分 字体配置文件 在 JDK 文档中 国际化支持相关API的概念 逻辑字体s 用于定义某些与平台无关的字体​​,这些字体映射到默认字体配置文件中的物理字体:

Java 平台定义了每个实现都必须支持的五种逻辑字体名称:Serif、SansSerif、等宽字体、Dialog 和 DialogInput。这些逻辑字体名称以依赖于实现的方式映射到物理字体。

所以对于你的情况,我会尝试

Font mono = new Font(parent.getDisplay(), "Monospaced", 10, SWT.NONE);

获取当前代码运行平台的物理等宽字体的句柄。

编辑:看来 SWT 对逻辑字体一无所知(错误 48055 eclipse.org 上详细描述了这一点)。在此错误报告中,建议使用一种 hackish 解决方法,其中可以从 AWT 字体中检索物理字体的名称...

其他提示

我花了一些时间来反对这个,直到我意识到显然eclipse必须能够访问monospace字体,以便在其文本字段,控制台等中使用。稍微挖掘了一下:

Font terminalFont = JFaceResources.getFont(JFaceResources.TEXT_FONT);

如果您感兴趣的是获取一些等宽字体,那么哪个有效。

编辑或根据@ ctron的评论:

Font font = JFaceResources.getTextFont();

编辑:警告(根据@ Lii的评论):这将为您提供已配置的文本字体,可由用户覆盖,也可能不是等宽字体。但是,它将与编辑器和控制台中使用的字体一致,可能是你想要的字体。

据我所知,AWT API不会暴露底层的Font信息。如果你能够实现它,我希望它是依赖于实现的。当然,比较一些JRE lib目录中的字体映射文件,我可以看到它们没有以一致的方式定义。

您可以加载自己的字体,但这似乎有点浪费,因为您知道平台附带了您需要的内容。

这是一个加载JRE字体的黑客:

private static Font loadMonospacedFont(Display display) {
    String jreHome = System.getProperty("java.home");
    File file = new File(jreHome, "/lib/fonts/LucidaTypewriterRegular.ttf");
    if (!file.exists()) {
        throw new IllegalStateException(file.toString());
    }
    if (!display.loadFont(file.toString())) {
        throw new IllegalStateException(file.toString());
    }
    final Font font = new Font(display, "Lucida Sans Typewriter", 10,
            SWT.NORMAL);
    display.addListener(SWT.Dispose, new Listener() {
        public void handleEvent(Event event) {
            font.dispose();
        }
    });
    return font;
}

适用于IBM / Win32 / JRE1.4,Sun / Win32 / JRE1.6,Sun / Linux / JRE1.6,但这是一种非常脆弱的方法。根据您对I18N的需求,也可能会遇到麻烦(我没有检查过)。

另一个黑客是测试平台上可用的字体:

public class Monotest {

    private static boolean isMonospace(GC gc) {
        final String wide = "wgh8";
        final String narrow = "1l;.";
        assert wide.length() == narrow.length();
        return gc.textExtent(wide).x == gc.textExtent(narrow).x;
    }

    private static void testFont(Display display, Font font) {
        Image image = new Image(display, 100, 100);
        try {
            GC gc = new GC(image);
            try {
                gc.setFont(font);
                System.out.println(isMonospace(gc) + "\t"
                        + font.getFontData()[0].getName());
            } finally {
                gc.dispose();
            }
        } finally {
            image.dispose();
        }
    }

    private static void walkFonts(Display display) {
        final boolean scalable = true;
        for (FontData fontData : display.getFontList(null, scalable)) {
            Font font = new Font(display, fontData);
            try {
                testFont(display, font);
            } finally {
                font.dispose();
            }
        }
    }

    public static void main(String[] args) {
        Display display = new Display();
        try {
            walkFonts(display);
        } finally {
            display.dispose();
        }
    }

}

这可能不是一个好方法,因为它可能会让您暴露于区域设置问题。此外,你不知道你遇到的第一个等宽字体是不是一些绕组图标集。

最好的方法可能是根据字体/区域设置映射白名单进行最佳猜测,并确保用户可以通过 FontDialog

对于有相同问题的人,您可以下载任何字体ttf文件,将其放入资源文件夹(在我的情况下为/font/**.ttf)并将此方法添加到您的应用。这是100%的工作。

public Font loadDigitalFont(int policeSize) {
    URL fontFile = YouClassName.class
            .getResource("/fonts/DS-DIGI.TTF");
    boolean isLoaded = Display.getCurrent().loadFont(fontFile.getPath());
    if (isLoaded) {
        FontData[] fd = Display.getCurrent().getFontList(null, true);
        FontData fontdata = null;
        for (int i = 0; i < fd.length; i++) {
            if (fd[i].getName().equals("DS-Digital")) {
                fontdata = fd[i];
                break;
            }}
        if (fontdata != null) {
            fontdata.setHeight(policeSize);
            fontdata.setStyle(SWT.BOLD);return new Font(getDisplay(), fontdata));}
    }return null;   }

如果您只想使用Monospaced字体,请使用“Courier”字样。 =&GT; new Font(display,&quot; Courier&quot;,10,SWT.NORMAL)

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top