0%

解决ubuntu系统monospace字体显示异常的问题

很长的一段时间,被xubuntu下的一个monospace字体显示问题困扰着,在网上找了很多方法也没有解决问题,在一次误打误撞的情况下解决了,简单把自己问题的过程记录下来,希望给一些小伙伴们帮助

问题描述

xubuntu下monospace在浏览器以及vscode下显示异常,具体如下图所示

浏览器chrome/firefox下的monospace

monofont display incorrect in browser

vscode下的monospace

monofont display incorrect in vscode

网上搜索结果大部分指示,是由于libfreetype6 2.8.1-2ubuntu2的一个bug导致,需要升级到最新的libfreetype2.9或者降级到libfreetype2.8.0,按照搜索的文档进行libfreetype的升级/降级后,显示效果依旧,问题没有得到解决

解决过程

在我放弃治疗后过了一段时间,突然想起,一直以来都忽略了验证linux的字体信息。首先,获取显示异常的字体font-family,如下图

browser get font family

使用fc-match验证字体

1
2
3
4
5
6
7
8
9
10
11
12
13
14
$ fc-match monospace
DejaVuSansMono.ttf: "DejaVu Sans Mono" "Book"

$ fc-match consolas
DejaVuSansMono.ttf: "DejaVu Sans Mono" "Book"

$ fc-match Menlo
DejaVuSans.ttf: "DejaVu Sans" "Book"

$ fc-match "PingFang SC"
DejaVuSans.ttf: "DejaVu Sans" "Book"

$ fc-match "Microsoft YaHei"
NotoSansCJK-Regular.ttc: "Noto Sans CJK SC" "Regular"

可以看到Menlo、”PingFang SC”、”Microsoft YaHei”的实际字体跟monospace并不一致,抱着死马当活马医的心态,编辑字体配置文件~/.config/fontconfig/fonts.conf,内容如下,

1
2
3
4
5
6
7
8
<?xml version='1.0'?>
<!DOCTYPE fontconfig SYSTEM 'fonts.dtd'>
<fontconfig>
<alias>
<family>Menlo</family>
<prefer><family>DejaVu Sans Mono</family></prefer>
</alias>
</fontconfig>

退出,重新登录,发现这个困扰了很久的问题终于解决,如下图

monofont the correct view

vscode可以通过更改font-family恢复正常的显示效果

注意

ubuntu/xubuntu、中文/英文的系统默认的字体会有些许差异,以上的操作可能不一定生效,可以新增~/.config/fontconfig/conf.d/00-menlo.conf

1
2
3
4
5
6
7
8
9
10
<?xml version="1.0"?>
<!DOCTYPE fontconfig SYSTEM "fonts.dtd">
<fontconfig>
<alias binding="same">
<family>Menlo</family>
<accept>
<family>DejaVu Sans Mono</family>
</accept>
</alias>
</fontconfig>

参考文档

[Ubuntu 18.04 LTS] Font not rendering properly
Ubuntu 18.04 LTS Font not rendering properly
Why doesn’t fc-match respect my match and edit rule for Courier when it does for Consolas?