SWT - 操作系统不可知的方式来获得等宽字体 [英] SWT - OS agnostic way to get monospaced font
问题描述
。这适用于Linux,但不适用于Windows:
字体mono = new Font(parent.getDisplay (),Mono,10,SWT.NONE);
或者我需要一个方法来加载不同的字体(Consolas,Terminal ,摩纳哥,单声道),直到一个不是空的?我可以在启动时在属性文件中指定它。
我试图从Display中获取系统字体,但是没有等宽。 b
根据字体配置文件 rel =noreferrer>国际化支持相关API, Logical Font 的概念用于定义某些与平台无关的字体,这些字体映射到默认字体配置文件中的物理字体:
$ b
Java平台定义了每个实现必须支持的五个逻辑字体名称:Serif,SansSerif,Monospaced,Dialog和DialogInput。这些逻辑字体名称以实现依赖的方式映射到物理字体。
所以在你的情况下,我会尝试
$ b $
Font mono = new Font(parent.getDisplay(),Monospaced,10,SWT.NONE);
来获取当前平台的物理等宽字体的句柄,您的代码正在运行。
编辑:SWT似乎对逻辑字体不了解( Bug eclipse.org上的48055 详细描述了这一点)。在这个错误报告中,提出了一个骇人的解决方法,其中物理字体的名称可以从AWT字体中检索出来...
Is there a way in SWT to get a monospaced font simply, that works across various operating systems?
For example. this works on Linux, but not Windows:
Font mono = new Font(parent.getDisplay(), "Mono", 10, SWT.NONE);
or do I need to have a method that tries loading varying fonts (Consolas, Terminal, Monaco, Mono) until one isn't null? Alternatively I could specify it in a properties file on startup.
I tried getting the system font from Display, but that wasn't monospaced.
According to the section on Font Configuration Files in the JDK documentation of Internationalization Support-related APIs, the concept of Logical Fonts is used to define certain platform-independent fonts which are mapped to physical fonts in the default font configuration files:
The Java Platform defines five logical font names that every implementation must support: Serif, SansSerif, Monospaced, Dialog, and DialogInput. These logical font names are mapped to physical fonts in implementation dependent ways.
So in your case, I'd try
Font mono = new Font(parent.getDisplay(), "Monospaced", 10, SWT.NONE);
to get a handle to the physical monospaced font of the current platform your code is running on.
Edit: It seems that SWT doesn't know anything about logical fonts (Bug 48055 on eclipse.org describes this in detail). In this bug report a hackish workaround was suggested, where the name of the physical font may be retrieved from an AWT font...
这篇关于SWT - 操作系统不可知的方式来获得等宽字体的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!