SWT - 获取等宽字体的操作系统不可知方式 [英] SWT - OS agnostic way to get monospaced font

查看:25
本文介绍了SWT - 获取等宽字体的操作系统不可知方式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 SWT 中是否有一种方法可以简单地获得等宽字体,适用于各种操作系统?

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

<前><代码>Font mono = new Font(parent.getDisplay(), "Mono", 10, SWT.NONE);

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

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

解决方案

根据 字体配置文件="noreferrer">国际化支持相关API,逻辑字体的概念用于定义某些平台无关的字体​​,这些字体映射到默认字体配置文件中的物理字体:

<块引用>

Java 平台定义了每个实现都必须支持的五个逻辑字体名称:Serif、SansSerif、Monospaced、Dialog 和 DialogInput.这些逻辑字体名称以实现相关的方式映射到物理字体.

所以在你的情况下,我会尝试

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

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

编辑:SWT 似乎对逻辑字体一无所知(Bug 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆