SWT - OS agnostic way to get monospaced font

前端 未结 5 1850
陌清茗
陌清茗 2020-12-01 10:56

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:



        
5条回答
  •  忘掉有多难
    2020-12-01 11:16

    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...

提交回复
热议问题