JavaFX 对所有控件使用等宽字体

JavaFX uses monospaced font for all controls

我在 Red Hat Enterprise Linux 工作站版本 6.6(圣地亚哥)上使用 JavaFX 1.8u40。当我 运行 建立一个标准的 JavaFX 应用程序时,即使没有额外的 CSS,所有标签、菜单等都使用等宽字体显示。

我使用的系统只有以下TTF字体可用

/usr/share/fonts/liberation
/usr/share/fonts/liberation/LiberationSerif-Regular.ttf
/usr/share/fonts/liberation/LiberationMono-BoldItalic.ttf
/usr/share/fonts/liberation/fonts.dir
/usr/share/fonts/liberation/LiberationMono-Italic.ttf
/usr/share/fonts/liberation/LiberationSans-Bold.ttf
/usr/share/fonts/liberation/LiberationSerif-Bold.ttf
/usr/share/fonts/liberation/LiberationMono-Regular.ttf
/usr/share/fonts/liberation/LiberationSans-BoldItalic.ttf
/usr/share/fonts/liberation/LiberationSerif-Italic.ttf
/usr/share/fonts/liberation/LiberationSerif-BoldItalic.ttf
/usr/share/fonts/liberation/fonts.scale
/usr/share/fonts/liberation/LiberationSans-Regular.ttf
/usr/share/fonts/liberation/LiberationSans-Italic.ttf
/usr/share/fonts/liberation/LiberationMono-Bold.ttf

我使用 OpenJFX fonts guide 中的 -Dprism.debugfonts 对此进行了调试。输出如下。

Loading FontFactory com.sun.javafx.font.freetype.FTFactory
Subpixel: enabled
Freetype2 Loaded (version 2.3.11)
LCD support Enabled
Time spent accessing fontconfig=4ms.
FC font sans:regular:roman maps to Liberation Mono in file /usr/share/fonts/liberation/LiberationMono-Regular.ttf
 0) Family=Liberation Mono, Style=Regular, Fullname=Liberation Mono, File=/usr/share/fonts/liberation/LiberationMono-Regular.ttf
 1) Family=Liberation Sans, Style=Regular, Fullname=Liberation Sans, File=/usr/share/fonts/liberation/LiberationSans-Regular.ttf
 2) Family=Liberation Serif, Style=Regular, Fullname=Liberation Serif, File=/usr/share/fonts/liberation/LiberationSerif-Regular.ttf
FC font sans:bold:roman maps to Liberation Mono Bold in file /usr/share/fonts/liberation/LiberationMono-Bold.ttf
 0) Family=Liberation Mono, Style=Bold, Fullname=Liberation Mono Bold, File=/usr/share/fonts/liberation/LiberationMono-Bold.ttf
 1) Family=Liberation Sans, Style=Bold, Fullname=Liberation Sans Bold, File=/usr/share/fonts/liberation/LiberationSans-Bold.ttf
 2) Family=Liberation Serif, Style=Regular, Fullname=Liberation Serif, File=/usr/share/fonts/liberation/LiberationSerif-Regular.ttf
FC font sans:regular:italic maps to Liberation Mono Italic in file /usr/share/fonts/liberation/LiberationMono-Italic.ttf
 0) Family=Liberation Mono, Style=Italic, Fullname=Liberation Mono Italic, File=/usr/share/fonts/liberation/LiberationMono-Italic.ttf
 1) Family=Liberation Sans, Style=Italic, Fullname=Liberation Sans Italic, File=/usr/share/fonts/liberation/LiberationSans-Italic.ttf
 2) Family=Liberation Serif, Style=Italic, Fullname=Liberation Serif Italic, File=/usr/share/fonts/liberation/LiberationSerif-Italic.ttf
FC font sans:bold:italic maps to Liberation Sans Bold Italic in file /usr/share/fonts/liberation/LiberationSans-BoldItalic.ttf
 0) Family=Liberation Sans, Style=Bold Italic, Fullname=Liberation Sans Bold Italic, File=/usr/share/fonts/liberation/LiberationSans-BoldItalic.ttf
 1) Family=Liberation Mono, Style=Bold Italic, Fullname=Liberation Mono Bold Italic, File=/usr/share/fonts/liberation/LiberationMono-BoldItalic.ttf
 2) Family=Liberation Serif, Style=Italic, Fullname=Liberation Serif Italic, File=/usr/share/fonts/liberation/LiberationSerif-Italic.ttf
FC font serif:regular:roman maps to Liberation Mono in file /usr/share/fonts/liberation/LiberationMono-Regular.ttf
 0) Family=Liberation Mono, Style=Regular, Fullname=Liberation Mono, File=/usr/share/fonts/liberation/LiberationMono-Regular.ttf
 1) Family=Liberation Sans, Style=Regular, Fullname=Liberation Sans, File=/usr/share/fonts/liberation/LiberationSans-Regular.ttf
 2) Family=Liberation Serif, Style=Regular, Fullname=Liberation Serif, File=/usr/share/fonts/liberation/LiberationSerif-Regular.ttf
FC font serif:bold:roman maps to Liberation Mono Bold in file /usr/share/fonts/liberation/LiberationMono-Bold.ttf
 0) Family=Liberation Mono, Style=Bold, Fullname=Liberation Mono Bold, File=/usr/share/fonts/liberation/LiberationMono-Bold.ttf
 1) Family=Liberation Sans, Style=Bold, Fullname=Liberation Sans Bold, File=/usr/share/fonts/liberation/LiberationSans-Bold.ttf
 2) Family=Liberation Serif, Style=Regular, Fullname=Liberation Serif, File=/usr/share/fonts/liberation/LiberationSerif-Regular.ttf
FC font serif:regular:italic maps to Liberation Mono Italic in file /usr/share/fonts/liberation/LiberationMono-Italic.ttf
 0) Family=Liberation Mono, Style=Italic, Fullname=Liberation Mono Italic, File=/usr/share/fonts/liberation/LiberationMono-Italic.ttf
 1) Family=Liberation Sans, Style=Italic, Fullname=Liberation Sans Italic, File=/usr/share/fonts/liberation/LiberationSans-Italic.ttf
 2) Family=Liberation Serif, Style=Italic, Fullname=Liberation Serif Italic, File=/usr/share/fonts/liberation/LiberationSerif-Italic.ttf
FC font serif:bold:italic maps to Liberation Sans Bold Italic in file /usr/share/fonts/liberation/LiberationSans-BoldItalic.ttf
 0) Family=Liberation Sans, Style=Bold Italic, Fullname=Liberation Sans Bold Italic, File=/usr/share/fonts/liberation/LiberationSans-BoldItalic.ttf
 1) Family=Liberation Mono, Style=Bold Italic, Fullname=Liberation Mono Bold Italic, File=/usr/share/fonts/liberation/LiberationMono-BoldItalic.ttf
 2) Family=Liberation Serif, Style=Italic, Fullname=Liberation Serif Italic, File=/usr/share/fonts/liberation/LiberationSerif-Italic.ttf
FC font monospace:regular:roman maps to Liberation Mono in file /usr/share/fonts/liberation/LiberationMono-Regular.ttf
 0) Family=Liberation Mono, Style=Regular, Fullname=Liberation Mono, File=/usr/share/fonts/liberation/LiberationMono-Regular.ttf
 1) Family=Liberation Sans, Style=Regular, Fullname=Liberation Sans, File=/usr/share/fonts/liberation/LiberationSans-Regular.ttf
 2) Family=Liberation Serif, Style=Regular, Fullname=Liberation Serif, File=/usr/share/fonts/liberation/LiberationSerif-Regular.ttf
FC font monospace:bold:roman maps to Liberation Mono Bold in file /usr/share/fonts/liberation/LiberationMono-Bold.ttf
 0) Family=Liberation Mono, Style=Bold, Fullname=Liberation Mono Bold, File=/usr/share/fonts/liberation/LiberationMono-Bold.ttf
 1) Family=Liberation Sans, Style=Bold, Fullname=Liberation Sans Bold, File=/usr/share/fonts/liberation/LiberationSans-Bold.ttf
 2) Family=Liberation Serif, Style=Regular, Fullname=Liberation Serif, File=/usr/share/fonts/liberation/LiberationSerif-Regular.ttf
FC font monospace:regular:italic maps to Liberation Mono Italic in file /usr/share/fonts/liberation/LiberationMono-Italic.ttf
 0) Family=Liberation Mono, Style=Italic, Fullname=Liberation Mono Italic, File=/usr/share/fonts/liberation/LiberationMono-Italic.ttf
 1) Family=Liberation Sans, Style=Italic, Fullname=Liberation Sans Italic, File=/usr/share/fonts/liberation/LiberationSans-Italic.ttf
 2) Family=Liberation Serif, Style=Italic, Fullname=Liberation Serif Italic, File=/usr/share/fonts/liberation/LiberationSerif-Italic.ttf
FC font monospace:bold:italic maps to Liberation Sans Bold Italic in file /usr/share/fonts/liberation/LiberationSans-BoldItalic.ttf
 0) Family=Liberation Sans, Style=Bold Italic, Fullname=Liberation Sans Bold Italic, File=/usr/share/fonts/liberation/LiberationSans-BoldItalic.ttf
 1) Family=Liberation Mono, Style=Bold Italic, Fullname=Liberation Mono Bold Italic, File=/usr/share/fonts/liberation/LiberationMono-BoldItalic.ttf
 2) Family=Liberation Serif, Style=Italic, Fullname=Liberation Serif Italic, File=/usr/share/fonts/liberation/LiberationSerif-Italic.ttf
FC name=sans style=0 uses Liberation Mono in file: /usr/share/fonts/liberation/LiberationMono-Regular.ttf

我发现使用 -Dprism.useFontConfig=false 会导致 JavaFX 默认使用内置 Lucida 字体,但是我需要使用 /usr/share/fonts

中的 Liberation 文件

问题

更新

问题似乎出在 /etc/fonts/conf.d/ 中的 fontconfig 规则中,而不是 JavaFX。我还没有想出办法解决这个问题,但是使用 fc-match 会产生相同的结果

fc-match sans:regular:roman 
"LiberationMono-Regular.ttf": "Liberation Mono" "Regular"

我在另一个论坛上发现了类似的问题:Font issue, monospace used for serif and sans-serif families,但没有解决方案。

添加字体配置文件,例如/etc/fonts/conf.d/88-liberation-hack.conf 将解决问题

<?xml version="1.0"?>
<!DOCTYPE fontconfig SYSTEM "fonts.dtd">
<fontconfig>

<alias>
  <family>sans-serif</family>
  <prefer><family>Liberation Sans</family></prefer>
</alias>

<alias>
  <family>serif</family>
  <prefer><family>Liberation Serif</family></prefer>
</alias>


<alias>
  <family>monospace</family>
  <prefer><family>Liberation Mono</family></prefer>
</alias>

</fontconfig>